|
@@ -38,17 +38,27 @@ export const braintreePaymentMethodHandler = new PaymentMethodHandler({
|
|
|
async createPayment(ctx, order, amount, args, metadata) {
|
|
async createPayment(ctx, order, amount, args, metadata) {
|
|
|
const gateway = getGateway(args, options);
|
|
const gateway = getGateway(args, options);
|
|
|
let customerId: string | undefined;
|
|
let customerId: string | undefined;
|
|
|
|
|
+ const { nonce, storeCardInVault } = metadata;
|
|
|
|
|
+ if (!nonce) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ amount,
|
|
|
|
|
+ state: 'Error' as const,
|
|
|
|
|
+ transactionId: '',
|
|
|
|
|
+ errorMessage: `No "nonce" value was specified in the metadata`,
|
|
|
|
|
+ metadata,
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
try {
|
|
try {
|
|
|
await entityHydrator.hydrate(ctx, order, { relations: ['customer'] });
|
|
await entityHydrator.hydrate(ctx, order, { relations: ['customer'] });
|
|
|
const customer = order.customer;
|
|
const customer = order.customer;
|
|
|
if (options.storeCustomersInBraintree && ctx.activeUserId && customer) {
|
|
if (options.storeCustomersInBraintree && ctx.activeUserId && customer) {
|
|
|
customerId = await getBraintreeCustomerId(ctx, gateway, customer);
|
|
customerId = await getBraintreeCustomerId(ctx, gateway, customer);
|
|
|
}
|
|
}
|
|
|
- return processPayment(ctx, gateway, order, amount, metadata.nonce, customerId, options);
|
|
|
|
|
|
|
+ return processPayment(ctx, gateway, order, amount, nonce, customerId, options, storeCardInVault);
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
Logger.error(e, loggerCtx);
|
|
Logger.error(e, loggerCtx);
|
|
|
return {
|
|
return {
|
|
|
- amount: order.total,
|
|
|
|
|
|
|
+ amount,
|
|
|
state: 'Error' as const,
|
|
state: 'Error' as const,
|
|
|
transactionId: '',
|
|
transactionId: '',
|
|
|
errorMessage: e.toString(),
|
|
errorMessage: e.toString(),
|
|
@@ -89,6 +99,7 @@ async function processPayment(
|
|
|
paymentMethodNonce: any,
|
|
paymentMethodNonce: any,
|
|
|
customerId: string | undefined,
|
|
customerId: string | undefined,
|
|
|
pluginOptions: BraintreePluginOptions,
|
|
pluginOptions: BraintreePluginOptions,
|
|
|
|
|
+ storeCardInVault = true,
|
|
|
) {
|
|
) {
|
|
|
const response = await gateway.transaction.sale({
|
|
const response = await gateway.transaction.sale({
|
|
|
customerId,
|
|
customerId,
|
|
@@ -97,7 +108,7 @@ async function processPayment(
|
|
|
paymentMethodNonce,
|
|
paymentMethodNonce,
|
|
|
options: {
|
|
options: {
|
|
|
submitForSettlement: true,
|
|
submitForSettlement: true,
|
|
|
- storeInVaultOnSuccess: !!customerId,
|
|
|
|
|
|
|
+ storeInVaultOnSuccess: !!customerId && storeCardInVault,
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
const extractMetadataFn = pluginOptions.extractMetadata ?? defaultExtractMetadataFn;
|
|
const extractMetadataFn = pluginOptions.extractMetadata ?? defaultExtractMetadataFn;
|