|
|
@@ -11,7 +11,7 @@ import {
|
|
|
} from '@vendure/core';
|
|
|
import { BraintreeGateway } from 'braintree';
|
|
|
|
|
|
-import { extractMetadataFromTransaction, getGateway } from './braintree-common';
|
|
|
+import { defaultExtractMetadataFn, getGateway } from './braintree-common';
|
|
|
import { BRAINTREE_PLUGIN_OPTIONS, loggerCtx } from './constants';
|
|
|
import { BraintreePluginOptions } from './types';
|
|
|
|
|
|
@@ -38,17 +38,27 @@ export const braintreePaymentMethodHandler = new PaymentMethodHandler({
|
|
|
async createPayment(ctx, order, amount, args, metadata) {
|
|
|
const gateway = getGateway(args, options);
|
|
|
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 {
|
|
|
await entityHydrator.hydrate(ctx, order, { relations: ['customer'] });
|
|
|
const customer = order.customer;
|
|
|
if (options.storeCustomersInBraintree && ctx.activeUserId && customer) {
|
|
|
customerId = await getBraintreeCustomerId(ctx, gateway, customer);
|
|
|
}
|
|
|
- return processPayment(ctx, gateway, order, amount, metadata.nonce, customerId);
|
|
|
+ return processPayment(ctx, gateway, order, amount, nonce, customerId, options, storeCardInVault);
|
|
|
} catch (e: any) {
|
|
|
Logger.error(e, loggerCtx);
|
|
|
return {
|
|
|
- amount: order.total,
|
|
|
+ amount,
|
|
|
state: 'Error' as const,
|
|
|
transactionId: '',
|
|
|
errorMessage: e.toString(),
|
|
|
@@ -69,7 +79,7 @@ export const braintreePaymentMethodHandler = new PaymentMethodHandler({
|
|
|
if (!response.success) {
|
|
|
return {
|
|
|
state: 'Failed' as const,
|
|
|
- transactionId: response.transaction.id,
|
|
|
+ transactionId: response.transaction?.id,
|
|
|
metadata: response,
|
|
|
};
|
|
|
}
|
|
|
@@ -88,6 +98,8 @@ async function processPayment(
|
|
|
amount: number,
|
|
|
paymentMethodNonce: any,
|
|
|
customerId: string | undefined,
|
|
|
+ pluginOptions: BraintreePluginOptions,
|
|
|
+ storeCardInVault = true,
|
|
|
) {
|
|
|
const response = await gateway.transaction.sale({
|
|
|
customerId,
|
|
|
@@ -96,23 +108,25 @@ async function processPayment(
|
|
|
paymentMethodNonce,
|
|
|
options: {
|
|
|
submitForSettlement: true,
|
|
|
- storeInVaultOnSuccess: !!customerId,
|
|
|
+ storeInVaultOnSuccess: !!customerId && storeCardInVault,
|
|
|
},
|
|
|
});
|
|
|
+ const extractMetadataFn = pluginOptions.extractMetadata ?? defaultExtractMetadataFn;
|
|
|
+ const metadata = extractMetadataFn(response.transaction);
|
|
|
if (!response.success) {
|
|
|
return {
|
|
|
amount,
|
|
|
state: 'Declined' as const,
|
|
|
transactionId: response.transaction.id,
|
|
|
errorMessage: response.message,
|
|
|
- metadata: extractMetadataFromTransaction(response.transaction),
|
|
|
+ metadata,
|
|
|
};
|
|
|
}
|
|
|
return {
|
|
|
amount,
|
|
|
state: 'Settled' as const,
|
|
|
transactionId: response.transaction.id,
|
|
|
- metadata: extractMetadataFromTransaction(response.transaction),
|
|
|
+ metadata,
|
|
|
};
|
|
|
}
|
|
|
|