@vendure/payments-plugin 1.4.0-beta.0 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,8 @@ const core_1 = require("@vendure/core");
6
6
  const braintree_common_1 = require("./braintree-common");
7
7
  const constants_1 = require("./constants");
8
8
  let options;
9
+ let connection;
10
+ let entityHydrator;
9
11
  /**
10
12
  * The handler for Braintree payments.
11
13
  */
@@ -19,33 +21,19 @@ exports.braintreePaymentMethodHandler = new core_1.PaymentMethodHandler({
19
21
  },
20
22
  init(injector) {
21
23
  options = injector.get(constants_1.BRAINTREE_PLUGIN_OPTIONS);
24
+ connection = injector.get(core_1.TransactionalConnection);
25
+ entityHydrator = injector.get(core_1.EntityHydrator);
22
26
  },
23
27
  async createPayment(ctx, order, amount, args, metadata) {
24
28
  const gateway = braintree_common_1.getGateway(args, options);
29
+ let customerId;
25
30
  try {
26
- const response = await gateway.transaction.sale({
27
- amount: (amount / 100).toString(10),
28
- orderId: order.code,
29
- paymentMethodNonce: metadata.nonce,
30
- options: {
31
- submitForSettlement: true,
32
- },
33
- });
34
- if (!response.success) {
35
- return {
36
- amount,
37
- state: 'Declined',
38
- transactionId: response.transaction.id,
39
- errorMessage: response.message,
40
- metadata: braintree_common_1.extractMetadataFromTransaction(response.transaction),
41
- };
31
+ await entityHydrator.hydrate(ctx, order, { relations: ['customer'] });
32
+ const customer = order.customer;
33
+ if (options.storeCustomersInBraintree && ctx.activeUserId && customer) {
34
+ customerId = await getBraintreeCustomerId(ctx, gateway, customer);
42
35
  }
43
- return {
44
- amount,
45
- state: 'Settled',
46
- transactionId: response.transaction.id,
47
- metadata: braintree_common_1.extractMetadataFromTransaction(response.transaction),
48
- };
36
+ return processPayment(ctx, gateway, order, amount, metadata.nonce, customerId);
49
37
  }
50
38
  catch (e) {
51
39
  core_1.Logger.error(e, constants_1.loggerCtx);
@@ -80,4 +68,62 @@ exports.braintreePaymentMethodHandler = new core_1.PaymentMethodHandler({
80
68
  };
81
69
  },
82
70
  });
71
+ async function processPayment(ctx, gateway, order, amount, paymentMethodNonce, customerId) {
72
+ const response = await gateway.transaction.sale({
73
+ customerId,
74
+ amount: (amount / 100).toString(10),
75
+ orderId: order.code,
76
+ paymentMethodNonce,
77
+ options: {
78
+ submitForSettlement: true,
79
+ storeInVaultOnSuccess: !!customerId,
80
+ },
81
+ });
82
+ if (!response.success) {
83
+ return {
84
+ amount,
85
+ state: 'Declined',
86
+ transactionId: response.transaction.id,
87
+ errorMessage: response.message,
88
+ metadata: braintree_common_1.extractMetadataFromTransaction(response.transaction),
89
+ };
90
+ }
91
+ return {
92
+ amount,
93
+ state: 'Settled',
94
+ transactionId: response.transaction.id,
95
+ metadata: braintree_common_1.extractMetadataFromTransaction(response.transaction),
96
+ };
97
+ }
98
+ /**
99
+ * If the Customer has no braintreeCustomerId, create one, else return the existing braintreeCustomerId.
100
+ */
101
+ async function getBraintreeCustomerId(ctx, gateway, customer) {
102
+ if (!customer.customFields.braintreeCustomerId) {
103
+ try {
104
+ const result = await gateway.customer.create({
105
+ firstName: customer.firstName,
106
+ lastName: customer.lastName,
107
+ email: customer.emailAddress,
108
+ });
109
+ if (result.success) {
110
+ const customerId = result.customer.id;
111
+ core_1.Logger.verbose(`Created Braintree Customer record for customerId ${customer.id}`, constants_1.loggerCtx);
112
+ customer.customFields.braintreeCustomerId = customerId;
113
+ await connection.getRepository(ctx, core_1.Customer).save(customer, { reload: false });
114
+ return customerId;
115
+ }
116
+ else {
117
+ core_1.Logger.error(`Failed to create Braintree Customer record for customerId ${customer.id}. View Debug level logs for details.`, constants_1.loggerCtx);
118
+ core_1.Logger.debug(JSON.stringify(result.errors, null, 2), constants_1.loggerCtx);
119
+ }
120
+ }
121
+ catch (e) {
122
+ core_1.Logger.error(e.message, constants_1.loggerCtx, e.stack);
123
+ }
124
+ }
125
+ else {
126
+ return customer.customFields.braintreeCustomerId;
127
+ }
128
+ }
83
129
  //# sourceMappingURL=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I,EAAE,CAAC,CAAC,EAAE,qBAAS,CAAC,CAAC;aACnE;SACJ;QAAC,OAAO,CAAC,EAAE;YACR,aAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,qBAAS,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;SAC/C;KACJ;SAAM;QACH,OAAO,QAAQ,CAAC,YAAY,CAAC,mBAAmB,CAAC;KACpD;AACL,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { Type } from '@vendure/core';
1
2
  import { BraintreePluginOptions } from './types';
2
3
  /**
3
4
  * @description
@@ -27,7 +28,12 @@ import { BraintreePluginOptions } from './types';
27
28
  * // ...
28
29
  *
29
30
  * plugins: [
30
- * BraintreePlugin.init({ environment: Environment.Sandbox }),
31
+ * BraintreePlugin.init({
32
+ * environment: Environment.Sandbox,
33
+ * // This allows saving customer payment
34
+ * // methods with Braintree
35
+ * storeCustomersInBraintree: true,
36
+ * }),
31
37
  * ]
32
38
  * ```
33
39
  * 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
@@ -94,6 +100,14 @@ import { BraintreePluginOptions } from './types';
94
100
  * }),
95
101
  * );
96
102
  *
103
+ * // If you are using the `storeCustomersInBraintree` option, then the
104
+ * // customer might already have a stored payment method selected as
105
+ * // soon as the dropin script loads. In this case, show the submit
106
+ * // button immediately.
107
+ * if (dropin.isPaymentMethodRequestable()) {
108
+ * showSubmitButton = true;
109
+ * }
110
+ *
97
111
  * dropin.on('paymentMethodRequestable', (payload) => {
98
112
  * if (payload.type === 'CreditCard') {
99
113
  * showSubmitButton = true;
@@ -172,5 +186,5 @@ import { BraintreePluginOptions } from './types';
172
186
  */
173
187
  export declare class BraintreePlugin {
174
188
  static options: BraintreePluginOptions;
175
- static init(options: BraintreePluginOptions): BraintreePlugin;
189
+ static init(options: BraintreePluginOptions): Type<BraintreePlugin>;
176
190
  }
@@ -41,7 +41,12 @@ const constants_1 = require("./constants");
41
41
  * // ...
42
42
  *
43
43
  * plugins: [
44
- * BraintreePlugin.init({ environment: Environment.Sandbox }),
44
+ * BraintreePlugin.init({
45
+ * environment: Environment.Sandbox,
46
+ * // This allows saving customer payment
47
+ * // methods with Braintree
48
+ * storeCustomersInBraintree: true,
49
+ * }),
45
50
  * ]
46
51
  * ```
47
52
  * 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
@@ -108,6 +113,14 @@ const constants_1 = require("./constants");
108
113
  * }),
109
114
  * );
110
115
  *
116
+ * // If you are using the `storeCustomersInBraintree` option, then the
117
+ * // customer might already have a stored payment method selected as
118
+ * // soon as the dropin script loads. In this case, show the submit
119
+ * // button immediately.
120
+ * if (dropin.isPaymentMethodRequestable()) {
121
+ * showSubmitButton = true;
122
+ * }
123
+ *
111
124
  * dropin.on('paymentMethodRequestable', (payload) => {
112
125
  * if (payload.type === 'CreditCard') {
113
126
  * showSubmitButton = true;
@@ -202,13 +215,23 @@ BraintreePlugin = BraintreePlugin_1 = __decorate([
202
215
  ],
203
216
  configuration: config => {
204
217
  config.paymentOptions.paymentMethodHandlers.push(braintree_handler_1.braintreePaymentMethodHandler);
218
+ if (BraintreePlugin_1.options.storeCustomersInBraintree === true) {
219
+ config.customFields.Customer.push({
220
+ name: 'braintreeCustomerId',
221
+ type: 'string',
222
+ label: [{ languageCode: core_1.LanguageCode.en, value: 'Braintree Customer ID' }],
223
+ nullable: true,
224
+ public: false,
225
+ readonly: true,
226
+ });
227
+ }
205
228
  return config;
206
229
  },
207
230
  shopApiExtensions: {
208
- schema: apollo_server_core_1.gql `
209
- extend type Query {
210
- generateBraintreeClientToken(orderId: ID!): String!
211
- }
231
+ schema: apollo_server_core_1.gql `
232
+ extend type Query {
233
+ generateBraintreeClientToken(orderId: ID!): String!
234
+ }
212
235
  `,
213
236
  resolvers: [braintree_resolver_1.BraintreeResolver],
214
237
  },
@@ -1 +1 @@
1
- {"version":3,"file":"braintree.plugin.js","sourceRoot":"","sources":["../../src/braintree/braintree.plugin.ts"],"names":[],"mappings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+26,14 @@ let BraintreeResolver = class BraintreeResolver {
26
26
  this.options = options;
27
27
  }
28
28
  async generateBraintreeClientToken(ctx, { orderId }) {
29
+ var _a;
29
30
  const order = await this.orderService.findOne(ctx, orderId);
30
31
  if (order && order.customer) {
31
- const customerId = order.customer.id.toString();
32
+ const customerId = (_a = order.customer.customFields.braintreeCustomerId) !== null && _a !== void 0 ? _a : undefined;
32
33
  const args = await this.getPaymentMethodArgs(ctx);
33
34
  const gateway = braintree_common_1.getGateway(args, this.options);
34
35
  try {
35
- const result = await gateway.clientToken.generate({});
36
+ const result = await gateway.clientToken.generate({ customerId });
36
37
  return result.clientToken;
37
38
  }
38
39
  catch (e) {
@@ -1 +1 @@
1
- {"version":3,"file":"braintree.resolver.js","sourceRoot":"","sources":["../../src/braintree/braintree.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwC;AACxC,6CAAwD;AACxD,wCASuB;AAEvB,yDAAgD;AAChD,2DAAoE;AACpE,2CAAkE;AAIlE,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC1B,YACY,UAAmC,EACnC,YAA0B,EACQ,OAA+B;QAFjE,eAAU,GAAV,UAAU,CAAyB;QACnC,iBAAY,GAAZ,YAAY,CAAc;QACQ,YAAO,GAAP,OAAO,CAAwB;IAC1E,CAAC;IAGJ,KAAK,CAAC,4BAA4B,CAAQ,GAAmB,EAAU,EAAE,OAAO,EAAmB;QAC/F,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAC5D,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,EAAE;YACzB,MAAM,UAAU,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;YAChD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAC;YAClD,MAAM,OAAO,GAAG,6BAAU,CAAC,IAAI,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BAAoB,CAAC,GAAmB;QAClD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,GAAG,EAAE,oBAAa,CAAC,CAAC,OAAO,CAAC;YAC3E,KAAK,EAAE;gBACH,IAAI,EAAE,iDAA6B,CAAC,IAAI;aAC3C;SACJ,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,EAAE;YACT,MAAM,IAAI,0BAAmB,CAAC,IAAI,qBAAS,0CAA0C,CAAC,CAAC;SAC1F;QACD,OAAO,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;YAC5C,uCACO,IAAI,KACP,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,IACvB;QACN,CAAC,EAAE,EAA2B,CAAC,CAAC;IACpC,CAAC;CACJ,CAAA;AArCG;IADC,eAAK,EAAE;IAC4B,WAAA,UAAG,EAAE,CAAA;IAAuB,WAAA,cAAI,EAAE,CAAA;;qCAAvB,qBAAc;;qEAmB5D;AA3BQ,iBAAiB;IAD7B,kBAAQ,EAAE;IAKF,WAAA,eAAM,CAAC,oCAAwB,CAAC,CAAA;qCAFb,8BAAuB;QACrB,mBAAY;GAH7B,iBAAiB,CA6C7B;AA7CY,8CAAiB"}
@@ -2,6 +2,11 @@ import { ConfigArgValues } from '@vendure/core/dist/common/configurable-operatio
2
2
  import { Environment } from 'braintree';
3
3
  import { braintreePaymentMethodHandler } from './braintree.handler';
4
4
  export declare type PaymentMethodArgsHash = ConfigArgValues<typeof braintreePaymentMethodHandler['args']>;
5
+ declare module '@vendure/core' {
6
+ interface CustomCustomerFields {
7
+ braintreeCustomerId?: string;
8
+ }
9
+ }
5
10
  /**
6
11
  * @description
7
12
  * Options for the Braintree plugin.
@@ -13,6 +18,18 @@ export interface BraintreePluginOptions {
13
18
  /**
14
19
  * @description
15
20
  * The Braintree environment being targeted, e.g. sandbox or production.
21
+ *
22
+ * @default Environment.Sandbox
16
23
  */
17
24
  environment?: Environment;
25
+ /**
26
+ * @description
27
+ * If set to `true`, a [Customer](https://developer.paypal.com/braintree/docs/guides/customers) object
28
+ * will be created in Braintree, which allows the secure storage of previously-used payment methods.
29
+ * This is done by adding a custom field to the Customer entity to store the Braintree customer ID,
30
+ * so switching this on will require a database migration / synchronization.
31
+ *
32
+ * @default false
33
+ */
34
+ storeCustomersInBraintree?: boolean;
18
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vendure/payments-plugin",
3
- "version": "1.4.0-beta.0",
3
+ "version": "1.4.3",
4
4
  "license": "MIT",
5
5
  "main": "package/index.js",
6
6
  "types": "package/index.d.ts",
@@ -27,13 +27,13 @@
27
27
  "devDependencies": {
28
28
  "@mollie/api-client": "^3.5.1",
29
29
  "@types/braintree": "^2.22.15",
30
- "@vendure/common": "^1.4.0-beta.0",
31
- "@vendure/core": "^1.4.0-beta.0",
32
- "@vendure/testing": "^1.4.0-beta.0",
30
+ "@vendure/common": "^1.4.3",
31
+ "@vendure/core": "^1.4.3",
32
+ "@vendure/testing": "^1.4.3",
33
33
  "braintree": "^3.0.0",
34
34
  "nock": "^13.1.4",
35
35
  "rimraf": "^3.0.2",
36
36
  "typescript": "4.3.5"
37
37
  },
38
- "gitHead": "b4a29d6cff5a7c5b39000719b4c556f60a80b521"
38
+ "gitHead": "6b6cb74b05595091c3b8a34c66b99353037cb92c"
39
39
  }