-
-
Notifications
You must be signed in to change notification settings - Fork 996
/
braintree.plugin.ts
279 lines (277 loc) · 9.11 KB
/
braintree.plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { LanguageCode, PluginCommonModule, Type, VendurePlugin } from '@vendure/core';
import { gql } from 'graphql-tag';
import { braintreePaymentMethodHandler } from './braintree.handler';
import { BraintreeResolver } from './braintree.resolver';
import { BRAINTREE_PLUGIN_OPTIONS } from './constants';
import { BraintreePluginOptions } from './types';
/**
* @description
* This plugin enables payments to be processed by [Braintree](https://www.braintreepayments.com/), a popular payment provider.
*
* ## Requirements
*
* 1. You will need to create a Braintree sandbox account as outlined in https://developers.braintreepayments.com/start/overview.
* 2. Then install `braintree` and `@types/braintree` from npm. This plugin was written with `v3.x` of the Braintree lib.
* ```shell
* yarn add \@vendure/payments-plugin braintree
* yarn add -D \@types/braintree
* ```
* or
* ```shell
* npm install \@vendure/payments-plugin braintree
* npm install -D \@types/braintree
* ```
*
* ## Setup
*
* 1. Add the plugin to your VendureConfig `plugins` array:
* ```ts
* import { BraintreePlugin } from '\@vendure/payments-plugin/package/braintree';
* import { Environment } from 'braintree';
*
* // ...
*
* plugins: [
* BraintreePlugin.init({
* environment: Environment.Sandbox,
* // This allows saving customer payment
* // methods with Braintree (see "vaulting"
* // section below for details)
* storeCustomersInBraintree: true,
* }),
* ]
* ```
* 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
* 2. Fill in the `Merchant ID`, `Public Key` & `Private Key` from your Braintree sandbox account.
*
* ## Storefront usage
*
* The plugin is designed to work with the [Braintree drop-in UI](https://developers.braintreepayments.com/guides/drop-in/overview/javascript/v3).
* This is a library provided by Braintree which will handle the payment UI for you. You can install it in your storefront project
* with:
*
* ```shell
* yarn add braintree-web-drop-in
* # or
* npm install braintree-web-drop-in
* ```
*
* The high-level workflow is:
* 1. Generate a "client token" on the server by executing the `generateBraintreeClientToken` mutation which is exposed by this plugin.
* 2. Use this client token to instantiate the Braintree Dropin UI.
* 3. Listen for the `"paymentMethodRequestable"` event which emitted by the Dropin.
* 4. Use the Dropin's `requestPaymentMethod()` method to get the required payment metadata.
* 5. Pass that metadata to the `addPaymentToOrder` mutation. The metadata should be an object of type `{ nonce: string; }`
*
* Here is an example of how your storefront code will look. Note that this example is attempting to
* be framework-agnostic, so you'll need to adapt it to fit to your framework of choice.
*
* ```ts
* // The Braintree Dropin instance
* let dropin: import('braintree-web-drop-in').Dropin;
*
* // Used to show/hide a "submit" button, which would be bound to the
* // `submitPayment()` method below.
* let showSubmitButton = false;
*
* // Used to display a "processing..." spinner
* let processing = false;
*
* //
* // This method would be invoked when the payment screen is mounted/created.
* //
* async function renderDropin(order: Order, clientToken: string) {
* // Lazy load braintree dropin because it has a reference
* // to `window` which breaks SSR
* dropin = await import('braintree-web-drop-in').then((module) =>
* module.default.create({
* authorization: clientToken,
* // This assumes a div in your view with the corresponding ID
* container: '#dropin-container',
* card: {
* cardholderName: {
* required: true,
* },
* overrides: {},
* },
* // Additional config is passed here depending on
* // which payment methods you have enabled in your
* // Braintree account.
* paypal: {
* flow: 'checkout',
* amount: order.totalWithTax / 100,
* currency: 'GBP',
* },
* }),
* );
*
* // If you are using the `storeCustomersInBraintree` option, then the
* // customer might already have a stored payment method selected as
* // soon as the dropin script loads. In this case, show the submit
* // button immediately.
* if (dropin.isPaymentMethodRequestable()) {
* showSubmitButton = true;
* }
*
* dropin.on('paymentMethodRequestable', (payload) => {
* if (payload.type === 'CreditCard') {
* showSubmitButton = true;
* }
* if (payload.type === 'PayPalAccount') {
* this.submitPayment();
* }
* });
*
* dropin.on('noPaymentMethodRequestable', () => {
* // Display an error
* });
* }
*
* async function generateClientToken() {
* const { generateBraintreeClientToken } = await graphQlClient.query(gql`
* query GenerateBraintreeClientToken {
* generateBraintreeClientToken
* }
* `);
* return generateBraintreeClientToken;
* }
*
* async submitPayment() {
* if (!dropin.isPaymentMethodRequestable()) {
* return;
* }
* showSubmitButton = false;
* processing = true;
*
* const paymentResult = await dropin.requestPaymentMethod();
*
* const { addPaymentToOrder } = await graphQlClient.query(gql`
* mutation AddPayment($input: PaymentInput!) {
* addPaymentToOrder(input: $input) {
* ... on Order {
* id
* payments {
* id
* amount
* errorMessage
* method
* state
* transactionId
* createdAt
* }
* }
* ... on ErrorResult {
* errorCode
* message
* }
* }
* }`, {
* input: {
* method: 'braintree', // The code of you Braintree PaymentMethod
* metadata: paymentResult,
* },
* },
* );
*
* switch (addPaymentToOrder?.__typename) {
* case 'Order':
* // Adding payment succeeded!
* break;
* case 'OrderStateTransitionError':
* case 'OrderPaymentStateError':
* case 'PaymentDeclinedError':
* case 'PaymentFailedError':
* // Display an error to the customer
* dropin.clearSelectedPaymentMethod();
* }
* }
* ```
*
* ## Storing payment details (vaulting)
*
* Braintree has a [vault feature](https://developer.paypal.com/braintree/articles/control-panel/vault/overview) which allows the secure storage
* of customer's payment information. Using the vault allows you to offer a faster checkout for repeat customers without needing to worry about
* how to securely store payment details.
*
* To enable this feature, set the `storeCustomersInBraintree` option to `true`.
*
* ```ts
* BraintreePlugin.init({
* environment: Environment.Sandbox,
* storeCustomersInBraintree: true,
* }),
* ```
*
* Since v1.8, it is possible to override vaulting on a per-payment basis by passing `includeCustomerId: false` to the `generateBraintreeClientToken`
* mutation:
*
* ```GraphQL
* const { generateBraintreeClientToken } = await graphQlClient.query(gql`
* query GenerateBraintreeClientToken($includeCustomerId: Boolean) {
* generateBraintreeClientToken(includeCustomerId: $includeCustomerId)
* }
* `, { includeCustomerId: false });
* ```
*
* as well as in the metadata of the `addPaymentToOrder` mutation:
*
* ```ts
* const { addPaymentToOrder } = await graphQlClient.query(gql`
* mutation AddPayment($input: PaymentInput!) {
* addPaymentToOrder(input: $input) {
* ...Order
* ...ErrorResult
* }
* }`, {
* input: {
* method: 'braintree',
* metadata: {
* ...paymentResult,
* includeCustomerId: false,
* },
* }
* );
* ```
*
* @docsCategory core plugins/PaymentsPlugin
* @docsPage BraintreePlugin
*/
@VendurePlugin({
imports: [PluginCommonModule],
providers: [
{
provide: BRAINTREE_PLUGIN_OPTIONS,
useFactory: () => BraintreePlugin.options,
},
],
configuration: config => {
config.paymentOptions.paymentMethodHandlers.push(braintreePaymentMethodHandler);
if (BraintreePlugin.options.storeCustomersInBraintree === true) {
config.customFields.Customer.push({
name: 'braintreeCustomerId',
type: 'string',
label: [{ languageCode: LanguageCode.en, value: 'Braintree Customer ID' }],
nullable: true,
public: false,
readonly: true,
});
}
return config;
},
shopApiExtensions: {
schema: gql`
extend type Query {
generateBraintreeClientToken(orderId: ID, includeCustomerId: Boolean): String!
}
`,
resolvers: [BraintreeResolver],
},
compatibility: '^3.0.0',
})
export class BraintreePlugin {
static options: BraintreePluginOptions = {};
static init(options: BraintreePluginOptions): Type<BraintreePlugin> {
this.options = options;
return BraintreePlugin;
}
}