|
|
@@ -1,6 +1,7 @@
|
|
|
import { Inject } from '@nestjs/common';
|
|
|
import { Args, Query, Resolver } from '@nestjs/graphql';
|
|
|
import {
|
|
|
+ ActiveOrderService,
|
|
|
Ctx,
|
|
|
ID,
|
|
|
InternalServerError,
|
|
|
@@ -21,12 +22,24 @@ export class BraintreeResolver {
|
|
|
constructor(
|
|
|
private connection: TransactionalConnection,
|
|
|
private orderService: OrderService,
|
|
|
+ private activeOrderService: ActiveOrderService,
|
|
|
@Inject(BRAINTREE_PLUGIN_OPTIONS) private options: BraintreePluginOptions,
|
|
|
) {}
|
|
|
|
|
|
@Query()
|
|
|
- async generateBraintreeClientToken(@Ctx() ctx: RequestContext, @Args() { orderId }: { orderId: ID }) {
|
|
|
- const order = await this.orderService.findOne(ctx, orderId);
|
|
|
+ async generateBraintreeClientToken(@Ctx() ctx: RequestContext, @Args() { orderId }: { orderId?: ID }) {
|
|
|
+ if (orderId) {
|
|
|
+ Logger.warn(
|
|
|
+ `The orderId argument to the generateBraintreeClientToken mutation has been deprecated and may be omitted.`,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const sessionOrder = await this.activeOrderService.getOrderFromContext(ctx);
|
|
|
+ if (!sessionOrder) {
|
|
|
+ throw new InternalServerError(
|
|
|
+ `Cannot generate Braintree clientToken as there is no active Order.`,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const order = await this.orderService.findOne(ctx, sessionOrder.id);
|
|
|
if (order && order.customer) {
|
|
|
const customerId = order.customer.customFields.braintreeCustomerId ?? undefined;
|
|
|
const args = await this.getPaymentMethodArgs(ctx);
|