Skip to content

Commit

Permalink
feat(payments-plugin): Allow Braintree environment to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Dec 6, 2021
1 parent a7e54fd commit 55d67d9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/payments-plugin/src/braintree/braintree-common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BraintreeGateway, Environment, Transaction } from 'braintree';

import { PaymentMethodArgsHash } from './types';
import { BraintreePluginOptions, PaymentMethodArgsHash } from './types';

export function getGateway(args: PaymentMethodArgsHash): BraintreeGateway {
export function getGateway(args: PaymentMethodArgsHash, options: BraintreePluginOptions): BraintreeGateway {
return new BraintreeGateway({
environment: Environment.Sandbox,
environment: options.environment || Environment.Sandbox,
merchantId: args.merchantId,
privateKey: args.privateKey,
publicKey: args.publicKey,
Expand Down
18 changes: 11 additions & 7 deletions packages/payments-plugin/src/braintree/braintree.handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { LanguageCode } from '@vendure/common/lib/generated-types';
import { Logger, PaymentMethodHandler } from '@vendure/core';
import { Injector, Logger, PaymentMethodHandler } from '@vendure/core';

import { extractMetadataFromTransaction, getGateway } from './braintree-common';
import { loggerCtx } from './constants';
import { BRAINTREE_PLUGIN_OPTIONS, loggerCtx } from './constants';
import { BraintreePluginOptions } from './types';

let options: BraintreePluginOptions;
/**
* The handler for Braintree payments.
*/
Expand All @@ -12,12 +14,14 @@ export const braintreePaymentMethodHandler = new PaymentMethodHandler({
description: [{ languageCode: LanguageCode.en, value: 'Braintree payments' }],
args: {
merchantId: { type: 'string', label: [{ languageCode: LanguageCode.en, value: 'Merchant ID' }] },
publicKey: { type: 'string', label: [{ languageCode: LanguageCode.en, value: 'Private Key' }] },
privateKey: { type: 'string', label: [{ languageCode: LanguageCode.en, value: 'Public Key' }] },
publicKey: { type: 'string', label: [{ languageCode: LanguageCode.en, value: 'Public Key' }] },
privateKey: { type: 'string', label: [{ languageCode: LanguageCode.en, value: 'Private Key' }] },
},
init(injector: Injector) {
options = injector.get<BraintreePluginOptions>(BRAINTREE_PLUGIN_OPTIONS);
},

async createPayment(ctx, order, amount, args, metadata) {
const gateway = getGateway(args);
const gateway = getGateway(args, options);
try {
const response = await gateway.transaction.sale({
amount: (amount / 100).toString(10),
Expand Down Expand Up @@ -61,7 +65,7 @@ export const braintreePaymentMethodHandler = new PaymentMethodHandler({
},

async createRefund(ctx, input, total, order, payment, args) {
const gateway = getGateway(args);
const gateway = getGateway(args, options);
const response = await gateway.transaction.refund(payment.transactionId, (total / 100).toString(10));
if (!response.success) {
return {
Expand Down
20 changes: 17 additions & 3 deletions packages/payments-plugin/src/braintree/braintree.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { gql } from 'apollo-server-core';

import { braintreePaymentMethodHandler } from './braintree.handler';
import { BraintreeResolver } from './braintree.resolver';
import { BRAINTREE_PLUGIN_OPTIONS } from './constants';
import { BraintreePluginOptions } from './types';

/**
* @description
Expand All @@ -27,11 +29,12 @@ import { BraintreeResolver } from './braintree.resolver';
* 1. Add the plugin to your VendureConfig `plugins` array:
* ```TypeScript
* import { BraintreePlugin } from '\@vendure/payments-plugin/package/braintree';
* import { Environment } from 'braintree';
*
* // ...
*
* plugins: [
* BraintreePlugin,
* BraintreePlugin.init({ environment: Environment.Sandbox }),
* ]
* ```
* 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
Expand Down Expand Up @@ -176,7 +179,12 @@ import { BraintreeResolver } from './braintree.resolver';
*/
@VendurePlugin({
imports: [PluginCommonModule],
providers: [],
providers: [
{
provide: BRAINTREE_PLUGIN_OPTIONS,
useFactory: () => BraintreePlugin.options,
},
],
configuration: config => {
config.paymentOptions.paymentMethodHandlers.push(braintreePaymentMethodHandler);
return config;
Expand All @@ -190,4 +198,10 @@ import { BraintreeResolver } from './braintree.resolver';
resolvers: [BraintreeResolver],
},
})
export class BraintreePlugin {}
export class BraintreePlugin {
static options: BraintreePluginOptions = {};
static init(options: BraintreePluginOptions): BraintreePlugin {
this.options = options;
return BraintreePlugin;
}
}
23 changes: 15 additions & 8 deletions packages/payments-plugin/src/braintree/braintree.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Inject } from '@nestjs/common';
import { Args, Query, Resolver } from '@nestjs/graphql';
import {
Ctx,
Expand All @@ -12,27 +13,33 @@ import {

import { getGateway } from './braintree-common';
import { braintreePaymentMethodHandler } from './braintree.handler';
import { loggerCtx } from './constants';
import { PaymentMethodArgsHash } from './types';
import { BRAINTREE_PLUGIN_OPTIONS, loggerCtx } from './constants';
import { BraintreePluginOptions, PaymentMethodArgsHash } from './types';

@Resolver()
export class BraintreeResolver {
constructor(private connection: TransactionalConnection, private orderService: OrderService) {}
constructor(
private connection: TransactionalConnection,
private orderService: OrderService,
@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);
if (order && order.customer) {
const customerId = order.customer.id.toString();
const args = await this.getPaymentMethodArgs(ctx);
const gateway = getGateway(args);
const gateway = getGateway(args, this.options);
try {
const result = await gateway.clientToken.generate({
customerId,
});
const result = await gateway.clientToken.generate({});
return result.clientToken;
} catch (e) {
Logger.error(e);
Logger.error(
`Could not generate Braintree clientToken. Check the configured credentials.`,
loggerCtx,
);
throw e;
}
} else {
throw new InternalServerError(`[${loggerCtx}] Could not find a Customer for the given Order`);
Expand Down
1 change: 1 addition & 0 deletions packages/payments-plugin/src/braintree/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const loggerCtx = 'BraintreePlugin';
export const BRAINTREE_PLUGIN_OPTIONS = Symbol('BRAINTREE_PLUGIN_OPTIONS');
1 change: 1 addition & 0 deletions packages/payments-plugin/src/braintree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './braintree.plugin';
export * from './braintree.handler';
export * from './braintree.resolver';
export * from './braintree-common';
export * from './types';
16 changes: 16 additions & 0 deletions packages/payments-plugin/src/braintree/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { ConfigArgValues } from '@vendure/core/dist/common/configurable-operation';
import { Environment } from 'braintree';

import { braintreePaymentMethodHandler } from './braintree.handler';

export type PaymentMethodArgsHash = ConfigArgValues<typeof braintreePaymentMethodHandler['args']>;

/**
* @description
* Options for the Braintree plugin.
*
* @docsCategory payments-plugin
* @docsPage BraintreePlugin
*/
export interface BraintreePluginOptions {
/**
* @description
* The Braintree environment being targeted, e.g. sandbox or production.
*/
environment?: Environment;
}
1 change: 1 addition & 0 deletions scripts/publish-to-verdaccio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cd ../core && npm publish -reg $VERDACCIO &&\
cd ../create && npm publish -reg $VERDACCIO &&\
cd ../elasticsearch-plugin && npm publish -reg $VERDACCIO &&\
cd ../email-plugin && npm publish -reg $VERDACCIO &&\
cd ../payments-plugin && npm publish -reg $VERDACCIO &&\
cd ../testing && npm publish -reg $VERDACCIO &&\
cd ../ui-devkit && npm publish -reg $VERDACCIO &&\
cd ../job-queue-plugin && npm publish -reg $VERDACCIO &&\
Expand Down

0 comments on commit 55d67d9

Please sign in to comment.