@deenruv/payments-plugin 1.0.0

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.
Files changed (102) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +157 -0
  3. package/package/braintree/braintree-common.d.ts +11 -0
  4. package/package/braintree/braintree-common.js +79 -0
  5. package/package/braintree/braintree-common.js.map +1 -0
  6. package/package/braintree/braintree.handler.d.ts +28 -0
  7. package/package/braintree/braintree.handler.js +147 -0
  8. package/package/braintree/braintree.handler.js.map +1 -0
  9. package/package/braintree/braintree.plugin.d.ts +238 -0
  10. package/package/braintree/braintree.plugin.js +294 -0
  11. package/package/braintree/braintree.plugin.js.map +1 -0
  12. package/package/braintree/braintree.resolver.d.ts +14 -0
  13. package/package/braintree/braintree.resolver.js +109 -0
  14. package/package/braintree/braintree.resolver.js.map +1 -0
  15. package/package/braintree/constants.d.ts +2 -0
  16. package/package/braintree/constants.js +6 -0
  17. package/package/braintree/constants.js.map +1 -0
  18. package/package/braintree/index.d.ts +5 -0
  19. package/package/braintree/index.js +22 -0
  20. package/package/braintree/index.js.map +1 -0
  21. package/package/braintree/types.d.ts +94 -0
  22. package/package/braintree/types.js +4 -0
  23. package/package/braintree/types.js.map +1 -0
  24. package/package/index.d.ts +4 -0
  25. package/package/index.js +8 -0
  26. package/package/index.js.map +1 -0
  27. package/package/mollie/api-extensions.d.ts +2 -0
  28. package/package/mollie/api-extensions.js +88 -0
  29. package/package/mollie/api-extensions.js.map +1 -0
  30. package/package/mollie/constants.d.ts +2 -0
  31. package/package/mollie/constants.js +6 -0
  32. package/package/mollie/constants.js.map +1 -0
  33. package/package/mollie/custom-fields.d.ts +7 -0
  34. package/package/mollie/custom-fields.js +12 -0
  35. package/package/mollie/custom-fields.js.map +1 -0
  36. package/package/mollie/extended-mollie-client.d.ts +51 -0
  37. package/package/mollie/extended-mollie-client.js +39 -0
  38. package/package/mollie/extended-mollie-client.js.map +1 -0
  39. package/package/mollie/graphql/generated-shop-types.d.ts +3212 -0
  40. package/package/mollie/graphql/generated-shop-types.js +977 -0
  41. package/package/mollie/graphql/generated-shop-types.js.map +1 -0
  42. package/package/mollie/index.d.ts +3 -0
  43. package/package/mollie/index.js +23 -0
  44. package/package/mollie/index.js.map +1 -0
  45. package/package/mollie/mollie.common-resolver.d.ts +9 -0
  46. package/package/mollie/mollie.common-resolver.js +55 -0
  47. package/package/mollie/mollie.common-resolver.js.map +1 -0
  48. package/package/mollie/mollie.controller.d.ts +10 -0
  49. package/package/mollie/mollie.controller.js +71 -0
  50. package/package/mollie/mollie.controller.js.map +1 -0
  51. package/package/mollie/mollie.handler.d.ts +36 -0
  52. package/package/mollie/mollie.handler.js +141 -0
  53. package/package/mollie/mollie.handler.js.map +1 -0
  54. package/package/mollie/mollie.helpers.d.ts +33 -0
  55. package/package/mollie/mollie.helpers.js +147 -0
  56. package/package/mollie/mollie.helpers.js.map +1 -0
  57. package/package/mollie/mollie.plugin.d.ts +176 -0
  58. package/package/mollie/mollie.plugin.js +167 -0
  59. package/package/mollie/mollie.plugin.js.map +1 -0
  60. package/package/mollie/mollie.service.d.ts +64 -0
  61. package/package/mollie/mollie.service.js +438 -0
  62. package/package/mollie/mollie.service.js.map +1 -0
  63. package/package/mollie/mollie.shop-resolver.d.ts +8 -0
  64. package/package/mollie/mollie.shop-resolver.js +40 -0
  65. package/package/mollie/mollie.shop-resolver.js.map +1 -0
  66. package/package/stripe/constants.d.ts +2 -0
  67. package/package/stripe/constants.js +6 -0
  68. package/package/stripe/constants.js.map +1 -0
  69. package/package/stripe/index.d.ts +1 -0
  70. package/package/stripe/index.js +6 -0
  71. package/package/stripe/index.js.map +1 -0
  72. package/package/stripe/metadata-sanitize.d.ts +13 -0
  73. package/package/stripe/metadata-sanitize.js +33 -0
  74. package/package/stripe/metadata-sanitize.js.map +1 -0
  75. package/package/stripe/raw-body.middleware.d.ts +6 -0
  76. package/package/stripe/raw-body.middleware.js +18 -0
  77. package/package/stripe/raw-body.middleware.js.map +1 -0
  78. package/package/stripe/stripe-client.d.ts +9 -0
  79. package/package/stripe/stripe-client.js +21 -0
  80. package/package/stripe/stripe-client.js.map +1 -0
  81. package/package/stripe/stripe-utils.d.ts +19 -0
  82. package/package/stripe/stripe-utils.js +40 -0
  83. package/package/stripe/stripe-utils.js.map +1 -0
  84. package/package/stripe/stripe.controller.d.ts +15 -0
  85. package/package/stripe/stripe.controller.js +135 -0
  86. package/package/stripe/stripe.controller.js.map +1 -0
  87. package/package/stripe/stripe.handler.d.ts +30 -0
  88. package/package/stripe/stripe.handler.js +103 -0
  89. package/package/stripe/stripe.handler.js.map +1 -0
  90. package/package/stripe/stripe.plugin.d.ts +158 -0
  91. package/package/stripe/stripe.plugin.js +218 -0
  92. package/package/stripe/stripe.plugin.js.map +1 -0
  93. package/package/stripe/stripe.resolver.d.ts +8 -0
  94. package/package/stripe/stripe.resolver.js +48 -0
  95. package/package/stripe/stripe.resolver.js.map +1 -0
  96. package/package/stripe/stripe.service.d.ts +28 -0
  97. package/package/stripe/stripe.service.js +149 -0
  98. package/package/stripe/stripe.service.js.map +1 -0
  99. package/package/stripe/types.d.ts +137 -0
  100. package/package/stripe/types.js +4 -0
  101. package/package/stripe/types.js.map +1 -0
  102. package/package.json +73 -0
@@ -0,0 +1,238 @@
1
+ import { Type } from "@deenruv/core";
2
+ import { BraintreePluginOptions } from "./types";
3
+ /**
4
+ * @description
5
+ * This plugin enables payments to be processed by [Braintree](https://www.braintreepayments.com/), a popular payment provider.
6
+ *
7
+ * ## Requirements
8
+ *
9
+ * 1. You will need to create a Braintree sandbox account as outlined in https://developers.braintreepayments.com/start/overview.
10
+ * 2. Then install `braintree` and `@types/braintree` from npm. This plugin was written with `v3.x` of the Braintree lib.
11
+ * ```shell
12
+ * yarn add \@deenruv/payments-plugin braintree
13
+ * yarn add -D \@types/braintree
14
+ * ```
15
+ * or
16
+ * ```shell
17
+ * npm install \@deenruv/payments-plugin braintree
18
+ * npm install -D \@types/braintree
19
+ * ```
20
+ *
21
+ * ## Setup
22
+ *
23
+ * 1. Add the plugin to your DeenruvConfig `plugins` array:
24
+ * ```ts
25
+ * import { BraintreePlugin } from '\@deenruv/payments-plugin/package/braintree';
26
+ * import { Environment } from 'braintree';
27
+ *
28
+ * // ...
29
+ *
30
+ * plugins: [
31
+ * BraintreePlugin.init({
32
+ * environment: Environment.Sandbox,
33
+ * // This allows saving customer payment
34
+ * // methods with Braintree (see "vaulting"
35
+ * // section below for details)
36
+ * storeCustomersInBraintree: true,
37
+ * }),
38
+ * ]
39
+ * ```
40
+ * 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
41
+ * 2. Fill in the `Merchant ID`, `Public Key` & `Private Key` from your Braintree sandbox account.
42
+ *
43
+ * ## Storefront usage
44
+ *
45
+ * The plugin is designed to work with the [Braintree drop-in UI](https://developers.braintreepayments.com/guides/drop-in/overview/javascript/v3).
46
+ * This is a library provided by Braintree which will handle the payment UI for you. You can install it in your storefront project
47
+ * with:
48
+ *
49
+ * ```shell
50
+ * yarn add braintree-web-drop-in
51
+ * # or
52
+ * npm install braintree-web-drop-in
53
+ * ```
54
+ *
55
+ * The high-level workflow is:
56
+ * 1. Generate a "client token" on the server by executing the `generateBraintreeClientToken` mutation which is exposed by this plugin.
57
+ * 2. Use this client token to instantiate the Braintree Dropin UI.
58
+ * 3. Listen for the `"paymentMethodRequestable"` event which emitted by the Dropin.
59
+ * 4. Use the Dropin's `requestPaymentMethod()` method to get the required payment metadata.
60
+ * 5. Pass that metadata to the `addPaymentToOrder` mutation. The metadata should be an object of type `{ nonce: string; }`
61
+ *
62
+ * Here is an example of how your storefront code will look. Note that this example is attempting to
63
+ * be framework-agnostic, so you'll need to adapt it to fit to your framework of choice.
64
+ *
65
+ * ```ts
66
+ * // The Braintree Dropin instance
67
+ * let dropin: import('braintree-web-drop-in').Dropin;
68
+ *
69
+ * // Used to show/hide a "submit" button, which would be bound to the
70
+ * // `submitPayment()` method below.
71
+ * let showSubmitButton = false;
72
+ *
73
+ * // Used to display a "processing..." spinner
74
+ * let processing = false;
75
+ *
76
+ * //
77
+ * // This method would be invoked when the payment screen is mounted/created.
78
+ * //
79
+ * async function renderDropin(order: Order, clientToken: string) {
80
+ * // Lazy load braintree dropin because it has a reference
81
+ * // to `window` which breaks SSR
82
+ * dropin = await import('braintree-web-drop-in').then((module) =>
83
+ * module.default.create({
84
+ * authorization: clientToken,
85
+ * // This assumes a div in your view with the corresponding ID
86
+ * container: '#dropin-container',
87
+ * card: {
88
+ * cardholderName: {
89
+ * required: true,
90
+ * },
91
+ * overrides: {},
92
+ * },
93
+ * // Additional config is passed here depending on
94
+ * // which payment methods you have enabled in your
95
+ * // Braintree account.
96
+ * paypal: {
97
+ * flow: 'checkout',
98
+ * amount: order.totalWithTax / 100,
99
+ * currency: 'GBP',
100
+ * },
101
+ * }),
102
+ * );
103
+ *
104
+ * // If you are using the `storeCustomersInBraintree` option, then the
105
+ * // customer might already have a stored payment method selected as
106
+ * // soon as the dropin script loads. In this case, show the submit
107
+ * // button immediately.
108
+ * if (dropin.isPaymentMethodRequestable()) {
109
+ * showSubmitButton = true;
110
+ * }
111
+ *
112
+ * dropin.on('paymentMethodRequestable', (payload) => {
113
+ * if (payload.type === 'CreditCard') {
114
+ * showSubmitButton = true;
115
+ * }
116
+ * if (payload.type === 'PayPalAccount') {
117
+ * this.submitPayment();
118
+ * }
119
+ * });
120
+ *
121
+ * dropin.on('noPaymentMethodRequestable', () => {
122
+ * // Display an error
123
+ * });
124
+ * }
125
+ *
126
+ * async function generateClientToken() {
127
+ * const { generateBraintreeClientToken } = await graphQlClient.query(gql`
128
+ * query GenerateBraintreeClientToken {
129
+ * generateBraintreeClientToken
130
+ * }
131
+ * `);
132
+ * return generateBraintreeClientToken;
133
+ * }
134
+ *
135
+ * async submitPayment() {
136
+ * if (!dropin.isPaymentMethodRequestable()) {
137
+ * return;
138
+ * }
139
+ * showSubmitButton = false;
140
+ * processing = true;
141
+ *
142
+ * const paymentResult = await dropin.requestPaymentMethod();
143
+ *
144
+ * const { addPaymentToOrder } = await graphQlClient.query(gql`
145
+ * mutation AddPayment($input: PaymentInput!) {
146
+ * addPaymentToOrder(input: $input) {
147
+ * ... on Order {
148
+ * id
149
+ * payments {
150
+ * id
151
+ * amount
152
+ * errorMessage
153
+ * method
154
+ * state
155
+ * transactionId
156
+ * createdAt
157
+ * }
158
+ * }
159
+ * ... on ErrorResult {
160
+ * errorCode
161
+ * message
162
+ * }
163
+ * }
164
+ * }`, {
165
+ * input: {
166
+ * method: 'braintree', // The code of you Braintree PaymentMethod
167
+ * metadata: paymentResult,
168
+ * },
169
+ * },
170
+ * );
171
+ *
172
+ * switch (addPaymentToOrder?.__typename) {
173
+ * case 'Order':
174
+ * // Adding payment succeeded!
175
+ * break;
176
+ * case 'OrderStateTransitionError':
177
+ * case 'OrderPaymentStateError':
178
+ * case 'PaymentDeclinedError':
179
+ * case 'PaymentFailedError':
180
+ * // Display an error to the customer
181
+ * dropin.clearSelectedPaymentMethod();
182
+ * }
183
+ * }
184
+ * ```
185
+ *
186
+ * ## Storing payment details (vaulting)
187
+ *
188
+ * Braintree has a [vault feature](https://developer.paypal.com/braintree/articles/control-panel/vault/overview) which allows the secure storage
189
+ * of customer's payment information. Using the vault allows you to offer a faster checkout for repeat customers without needing to worry about
190
+ * how to securely store payment details.
191
+ *
192
+ * To enable this feature, set the `storeCustomersInBraintree` option to `true`.
193
+ *
194
+ * ```ts
195
+ * BraintreePlugin.init({
196
+ * environment: Environment.Sandbox,
197
+ * storeCustomersInBraintree: true,
198
+ * }),
199
+ * ```
200
+ *
201
+ * Since v1.8, it is possible to override vaulting on a per-payment basis by passing `includeCustomerId: false` to the `generateBraintreeClientToken`
202
+ * mutation:
203
+ *
204
+ * ```GraphQL
205
+ * const { generateBraintreeClientToken } = await graphQlClient.query(gql`
206
+ * query GenerateBraintreeClientToken($includeCustomerId: Boolean) {
207
+ * generateBraintreeClientToken(includeCustomerId: $includeCustomerId)
208
+ * }
209
+ * `, { includeCustomerId: false });
210
+ * ```
211
+ *
212
+ * as well as in the metadata of the `addPaymentToOrder` mutation:
213
+ *
214
+ * ```ts
215
+ * const { addPaymentToOrder } = await graphQlClient.query(gql`
216
+ * mutation AddPayment($input: PaymentInput!) {
217
+ * addPaymentToOrder(input: $input) {
218
+ * ...Order
219
+ * ...ErrorResult
220
+ * }
221
+ * }`, {
222
+ * input: {
223
+ * method: 'braintree',
224
+ * metadata: {
225
+ * ...paymentResult,
226
+ * includeCustomerId: false,
227
+ * },
228
+ * }
229
+ * );
230
+ * ```
231
+ *
232
+ * @docsCategory core plugins/PaymentsPlugin
233
+ * @docsPage BraintreePlugin
234
+ */
235
+ export declare class BraintreePlugin {
236
+ static options: BraintreePluginOptions;
237
+ static init(options: BraintreePluginOptions): Type<BraintreePlugin>;
238
+ }
@@ -0,0 +1,294 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var BraintreePlugin_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.BraintreePlugin = void 0;
11
+ const core_1 = require("@deenruv/core");
12
+ const graphql_tag_1 = require("graphql-tag");
13
+ const braintree_handler_1 = require("./braintree.handler");
14
+ const braintree_resolver_1 = require("./braintree.resolver");
15
+ const constants_1 = require("./constants");
16
+ /**
17
+ * @description
18
+ * This plugin enables payments to be processed by [Braintree](https://www.braintreepayments.com/), a popular payment provider.
19
+ *
20
+ * ## Requirements
21
+ *
22
+ * 1. You will need to create a Braintree sandbox account as outlined in https://developers.braintreepayments.com/start/overview.
23
+ * 2. Then install `braintree` and `@types/braintree` from npm. This plugin was written with `v3.x` of the Braintree lib.
24
+ * ```shell
25
+ * yarn add \@deenruv/payments-plugin braintree
26
+ * yarn add -D \@types/braintree
27
+ * ```
28
+ * or
29
+ * ```shell
30
+ * npm install \@deenruv/payments-plugin braintree
31
+ * npm install -D \@types/braintree
32
+ * ```
33
+ *
34
+ * ## Setup
35
+ *
36
+ * 1. Add the plugin to your DeenruvConfig `plugins` array:
37
+ * ```ts
38
+ * import { BraintreePlugin } from '\@deenruv/payments-plugin/package/braintree';
39
+ * import { Environment } from 'braintree';
40
+ *
41
+ * // ...
42
+ *
43
+ * plugins: [
44
+ * BraintreePlugin.init({
45
+ * environment: Environment.Sandbox,
46
+ * // This allows saving customer payment
47
+ * // methods with Braintree (see "vaulting"
48
+ * // section below for details)
49
+ * storeCustomersInBraintree: true,
50
+ * }),
51
+ * ]
52
+ * ```
53
+ * 2. Create a new PaymentMethod in the Admin UI, and select "Braintree payments" as the handler.
54
+ * 2. Fill in the `Merchant ID`, `Public Key` & `Private Key` from your Braintree sandbox account.
55
+ *
56
+ * ## Storefront usage
57
+ *
58
+ * The plugin is designed to work with the [Braintree drop-in UI](https://developers.braintreepayments.com/guides/drop-in/overview/javascript/v3).
59
+ * This is a library provided by Braintree which will handle the payment UI for you. You can install it in your storefront project
60
+ * with:
61
+ *
62
+ * ```shell
63
+ * yarn add braintree-web-drop-in
64
+ * # or
65
+ * npm install braintree-web-drop-in
66
+ * ```
67
+ *
68
+ * The high-level workflow is:
69
+ * 1. Generate a "client token" on the server by executing the `generateBraintreeClientToken` mutation which is exposed by this plugin.
70
+ * 2. Use this client token to instantiate the Braintree Dropin UI.
71
+ * 3. Listen for the `"paymentMethodRequestable"` event which emitted by the Dropin.
72
+ * 4. Use the Dropin's `requestPaymentMethod()` method to get the required payment metadata.
73
+ * 5. Pass that metadata to the `addPaymentToOrder` mutation. The metadata should be an object of type `{ nonce: string; }`
74
+ *
75
+ * Here is an example of how your storefront code will look. Note that this example is attempting to
76
+ * be framework-agnostic, so you'll need to adapt it to fit to your framework of choice.
77
+ *
78
+ * ```ts
79
+ * // The Braintree Dropin instance
80
+ * let dropin: import('braintree-web-drop-in').Dropin;
81
+ *
82
+ * // Used to show/hide a "submit" button, which would be bound to the
83
+ * // `submitPayment()` method below.
84
+ * let showSubmitButton = false;
85
+ *
86
+ * // Used to display a "processing..." spinner
87
+ * let processing = false;
88
+ *
89
+ * //
90
+ * // This method would be invoked when the payment screen is mounted/created.
91
+ * //
92
+ * async function renderDropin(order: Order, clientToken: string) {
93
+ * // Lazy load braintree dropin because it has a reference
94
+ * // to `window` which breaks SSR
95
+ * dropin = await import('braintree-web-drop-in').then((module) =>
96
+ * module.default.create({
97
+ * authorization: clientToken,
98
+ * // This assumes a div in your view with the corresponding ID
99
+ * container: '#dropin-container',
100
+ * card: {
101
+ * cardholderName: {
102
+ * required: true,
103
+ * },
104
+ * overrides: {},
105
+ * },
106
+ * // Additional config is passed here depending on
107
+ * // which payment methods you have enabled in your
108
+ * // Braintree account.
109
+ * paypal: {
110
+ * flow: 'checkout',
111
+ * amount: order.totalWithTax / 100,
112
+ * currency: 'GBP',
113
+ * },
114
+ * }),
115
+ * );
116
+ *
117
+ * // If you are using the `storeCustomersInBraintree` option, then the
118
+ * // customer might already have a stored payment method selected as
119
+ * // soon as the dropin script loads. In this case, show the submit
120
+ * // button immediately.
121
+ * if (dropin.isPaymentMethodRequestable()) {
122
+ * showSubmitButton = true;
123
+ * }
124
+ *
125
+ * dropin.on('paymentMethodRequestable', (payload) => {
126
+ * if (payload.type === 'CreditCard') {
127
+ * showSubmitButton = true;
128
+ * }
129
+ * if (payload.type === 'PayPalAccount') {
130
+ * this.submitPayment();
131
+ * }
132
+ * });
133
+ *
134
+ * dropin.on('noPaymentMethodRequestable', () => {
135
+ * // Display an error
136
+ * });
137
+ * }
138
+ *
139
+ * async function generateClientToken() {
140
+ * const { generateBraintreeClientToken } = await graphQlClient.query(gql`
141
+ * query GenerateBraintreeClientToken {
142
+ * generateBraintreeClientToken
143
+ * }
144
+ * `);
145
+ * return generateBraintreeClientToken;
146
+ * }
147
+ *
148
+ * async submitPayment() {
149
+ * if (!dropin.isPaymentMethodRequestable()) {
150
+ * return;
151
+ * }
152
+ * showSubmitButton = false;
153
+ * processing = true;
154
+ *
155
+ * const paymentResult = await dropin.requestPaymentMethod();
156
+ *
157
+ * const { addPaymentToOrder } = await graphQlClient.query(gql`
158
+ * mutation AddPayment($input: PaymentInput!) {
159
+ * addPaymentToOrder(input: $input) {
160
+ * ... on Order {
161
+ * id
162
+ * payments {
163
+ * id
164
+ * amount
165
+ * errorMessage
166
+ * method
167
+ * state
168
+ * transactionId
169
+ * createdAt
170
+ * }
171
+ * }
172
+ * ... on ErrorResult {
173
+ * errorCode
174
+ * message
175
+ * }
176
+ * }
177
+ * }`, {
178
+ * input: {
179
+ * method: 'braintree', // The code of you Braintree PaymentMethod
180
+ * metadata: paymentResult,
181
+ * },
182
+ * },
183
+ * );
184
+ *
185
+ * switch (addPaymentToOrder?.__typename) {
186
+ * case 'Order':
187
+ * // Adding payment succeeded!
188
+ * break;
189
+ * case 'OrderStateTransitionError':
190
+ * case 'OrderPaymentStateError':
191
+ * case 'PaymentDeclinedError':
192
+ * case 'PaymentFailedError':
193
+ * // Display an error to the customer
194
+ * dropin.clearSelectedPaymentMethod();
195
+ * }
196
+ * }
197
+ * ```
198
+ *
199
+ * ## Storing payment details (vaulting)
200
+ *
201
+ * Braintree has a [vault feature](https://developer.paypal.com/braintree/articles/control-panel/vault/overview) which allows the secure storage
202
+ * of customer's payment information. Using the vault allows you to offer a faster checkout for repeat customers without needing to worry about
203
+ * how to securely store payment details.
204
+ *
205
+ * To enable this feature, set the `storeCustomersInBraintree` option to `true`.
206
+ *
207
+ * ```ts
208
+ * BraintreePlugin.init({
209
+ * environment: Environment.Sandbox,
210
+ * storeCustomersInBraintree: true,
211
+ * }),
212
+ * ```
213
+ *
214
+ * Since v1.8, it is possible to override vaulting on a per-payment basis by passing `includeCustomerId: false` to the `generateBraintreeClientToken`
215
+ * mutation:
216
+ *
217
+ * ```GraphQL
218
+ * const { generateBraintreeClientToken } = await graphQlClient.query(gql`
219
+ * query GenerateBraintreeClientToken($includeCustomerId: Boolean) {
220
+ * generateBraintreeClientToken(includeCustomerId: $includeCustomerId)
221
+ * }
222
+ * `, { includeCustomerId: false });
223
+ * ```
224
+ *
225
+ * as well as in the metadata of the `addPaymentToOrder` mutation:
226
+ *
227
+ * ```ts
228
+ * const { addPaymentToOrder } = await graphQlClient.query(gql`
229
+ * mutation AddPayment($input: PaymentInput!) {
230
+ * addPaymentToOrder(input: $input) {
231
+ * ...Order
232
+ * ...ErrorResult
233
+ * }
234
+ * }`, {
235
+ * input: {
236
+ * method: 'braintree',
237
+ * metadata: {
238
+ * ...paymentResult,
239
+ * includeCustomerId: false,
240
+ * },
241
+ * }
242
+ * );
243
+ * ```
244
+ *
245
+ * @docsCategory core plugins/PaymentsPlugin
246
+ * @docsPage BraintreePlugin
247
+ */
248
+ let BraintreePlugin = exports.BraintreePlugin = BraintreePlugin_1 = class BraintreePlugin {
249
+ static init(options) {
250
+ this.options = options;
251
+ return BraintreePlugin_1;
252
+ }
253
+ };
254
+ BraintreePlugin.options = {};
255
+ exports.BraintreePlugin = BraintreePlugin = BraintreePlugin_1 = __decorate([
256
+ (0, core_1.DeenruvPlugin)({
257
+ imports: [core_1.PluginCommonModule],
258
+ providers: [
259
+ {
260
+ provide: constants_1.BRAINTREE_PLUGIN_OPTIONS,
261
+ useFactory: () => BraintreePlugin_1.options,
262
+ },
263
+ ],
264
+ configuration: (config) => {
265
+ config.paymentOptions.paymentMethodHandlers.push(braintree_handler_1.braintreePaymentMethodHandler);
266
+ if (BraintreePlugin_1.options.storeCustomersInBraintree === true) {
267
+ config.customFields.Customer.push({
268
+ name: "braintreeCustomerId",
269
+ type: "string",
270
+ label: [
271
+ { languageCode: core_1.LanguageCode.en, value: "Braintree Customer ID" },
272
+ ],
273
+ nullable: true,
274
+ public: false,
275
+ readonly: true,
276
+ });
277
+ }
278
+ return config;
279
+ },
280
+ shopApiExtensions: {
281
+ schema: (0, graphql_tag_1.gql) `
282
+ extend type Query {
283
+ generateBraintreeClientToken(
284
+ orderId: ID
285
+ includeCustomerId: Boolean
286
+ ): String!
287
+ }
288
+ `,
289
+ resolvers: [braintree_resolver_1.BraintreeResolver],
290
+ },
291
+ compatibility: "^0.0.0",
292
+ })
293
+ ], BraintreePlugin);
294
+ //# sourceMappingURL=braintree.plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"braintree.plugin.js","sourceRoot":"","sources":["../../src/braintree/braintree.plugin.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,wCAKuB;AACvB,6CAAkC;AAElC,2DAAoE;AACpE,6DAAyD;AACzD,2CAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuOG;AAwCI,IAAM,eAAe,iDAArB,MAAM,eAAe;IAE1B,MAAM,CAAC,IAAI,CAAC,OAA+B;QACzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,OAAO,iBAAe,CAAC;IACzB,CAAC;;AAJM,uBAAO,GAA2B,EAAE,AAA7B,CAA8B;0BADjC,eAAe;IAvC3B,IAAA,oBAAa,EAAC;QACb,OAAO,EAAE,CAAC,yBAAkB,CAAC;QAC7B,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,oCAAwB;gBACjC,UAAU,EAAE,GAAG,EAAE,CAAC,iBAAe,CAAC,OAAO;aAC1C;SACF;QACD,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE;YACxB,MAAM,CAAC,cAAc,CAAC,qBAAqB,CAAC,IAAI,CAC9C,iDAA6B,CAC9B,CAAC;YACF,IAAI,iBAAe,CAAC,OAAO,CAAC,yBAAyB,KAAK,IAAI,EAAE;gBAC9D,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAChC,IAAI,EAAE,qBAAqB;oBAC3B,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE;wBACL,EAAE,YAAY,EAAE,mBAAY,CAAC,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE;qBAClE;oBACD,QAAQ,EAAE,IAAI;oBACd,MAAM,EAAE,KAAK;oBACb,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;aACJ;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,iBAAiB,EAAE;YACjB,MAAM,EAAE,IAAA,iBAAG,EAAA;;;;;;;KAOV;YACD,SAAS,EAAE,CAAC,sCAAiB,CAAC;SAC/B;QACD,aAAa,EAAE,QAAQ;KACxB,CAAC;GACW,eAAe,CAM3B"}
@@ -0,0 +1,14 @@
1
+ import { ActiveOrderService, ID, OrderService, RequestContext, TransactionalConnection } from "@deenruv/core";
2
+ import { BraintreePluginOptions } from "./types";
3
+ export declare class BraintreeResolver {
4
+ private connection;
5
+ private orderService;
6
+ private activeOrderService;
7
+ private options;
8
+ constructor(connection: TransactionalConnection, orderService: OrderService, activeOrderService: ActiveOrderService, options: BraintreePluginOptions);
9
+ generateBraintreeClientToken(ctx: RequestContext, { orderId, includeCustomerId, }: {
10
+ orderId?: ID;
11
+ includeCustomerId?: boolean;
12
+ }): Promise<string>;
13
+ private getPaymentMethodArgs;
14
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.BraintreeResolver = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const graphql_1 = require("@nestjs/graphql");
18
+ const core_1 = require("@deenruv/core");
19
+ const braintree_common_1 = require("./braintree-common");
20
+ const braintree_handler_1 = require("./braintree.handler");
21
+ const constants_1 = require("./constants");
22
+ let BraintreeResolver = exports.BraintreeResolver = class BraintreeResolver {
23
+ constructor(connection, orderService, activeOrderService, options) {
24
+ this.connection = connection;
25
+ this.orderService = orderService;
26
+ this.activeOrderService = activeOrderService;
27
+ this.options = options;
28
+ }
29
+ async generateBraintreeClientToken(ctx, { orderId, includeCustomerId, }) {
30
+ var _a, _b, _c;
31
+ if (orderId) {
32
+ core_1.Logger.warn("The orderId argument to the generateBraintreeClientToken mutation has been deprecated and may be omitted.");
33
+ }
34
+ const sessionOrder = await this.activeOrderService.getOrderFromContext(ctx);
35
+ if (!sessionOrder) {
36
+ throw new core_1.InternalServerError("Cannot generate Braintree clientToken as there is no active Order.");
37
+ }
38
+ const order = await this.orderService.findOne(ctx, sessionOrder.id);
39
+ if (order) {
40
+ const customerId = (_b = (_a = order.customer) === null || _a === void 0 ? void 0 : _a.customFields.braintreeCustomerId) !== null && _b !== void 0 ? _b : undefined;
41
+ const args = await this.getPaymentMethodArgs(ctx);
42
+ const gateway = (0, braintree_common_1.getGateway)(args, this.options);
43
+ try {
44
+ let result = await gateway.clientToken.generate({
45
+ customerId: includeCustomerId === false ? undefined : customerId,
46
+ });
47
+ if (result.success === true) {
48
+ return result.clientToken;
49
+ }
50
+ else {
51
+ if (result.message ===
52
+ "Customer specified by customer_id does not exist") {
53
+ // For some reason the custom_id is invalid. This could occur e.g. if the ID was created on the Sandbox endpoint and now
54
+ // we switched to Production. In this case, we will remove it and allow a new one
55
+ // to be generated when the payment is created.
56
+ if (this.options.storeCustomersInBraintree) {
57
+ if ((_c = order.customer) === null || _c === void 0 ? void 0 : _c.customFields.braintreeCustomerId) {
58
+ order.customer.customFields.braintreeCustomerId = undefined;
59
+ await this.connection
60
+ .getRepository(ctx, core_1.Customer)
61
+ .save(order.customer);
62
+ }
63
+ }
64
+ result = await gateway.clientToken.generate({
65
+ customerId: undefined,
66
+ });
67
+ if (result.success === true) {
68
+ return result.clientToken;
69
+ }
70
+ }
71
+ core_1.Logger.error(`Could not generate Braintree clientToken: ${result.message}`, constants_1.loggerCtx);
72
+ throw new core_1.InternalServerError(`Could not generate Braintree clientToken: ${result.message}`);
73
+ }
74
+ }
75
+ catch (e) {
76
+ core_1.Logger.error("Could not generate Braintree clientToken. Check the configured credentials.", constants_1.loggerCtx);
77
+ throw e;
78
+ }
79
+ }
80
+ else {
81
+ throw new core_1.InternalServerError(`[${constants_1.loggerCtx}] Could not find a Customer for the given Order`);
82
+ }
83
+ }
84
+ async getPaymentMethodArgs(ctx) {
85
+ const method = (await this.connection.getRepository(ctx, core_1.PaymentMethod).find()).find((m) => m.handler.code === braintree_handler_1.braintreePaymentMethodHandler.code);
86
+ if (!method) {
87
+ throw new core_1.InternalServerError(`[${constants_1.loggerCtx}] Could not find Braintree PaymentMethod`);
88
+ }
89
+ return method.handler.args.reduce((hash, arg) => {
90
+ return Object.assign(Object.assign({}, hash), { [arg.name]: arg.value });
91
+ }, {});
92
+ }
93
+ };
94
+ __decorate([
95
+ (0, graphql_1.Query)(),
96
+ __param(0, (0, core_1.Ctx)()),
97
+ __param(1, (0, graphql_1.Args)()),
98
+ __metadata("design:type", Function),
99
+ __metadata("design:paramtypes", [core_1.RequestContext, Object]),
100
+ __metadata("design:returntype", Promise)
101
+ ], BraintreeResolver.prototype, "generateBraintreeClientToken", null);
102
+ exports.BraintreeResolver = BraintreeResolver = __decorate([
103
+ (0, graphql_1.Resolver)(),
104
+ __param(3, (0, common_1.Inject)(constants_1.BRAINTREE_PLUGIN_OPTIONS)),
105
+ __metadata("design:paramtypes", [core_1.TransactionalConnection,
106
+ core_1.OrderService,
107
+ core_1.ActiveOrderService, Object])
108
+ ], BraintreeResolver);
109
+ //# sourceMappingURL=braintree.resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"braintree.resolver.js","sourceRoot":"","sources":["../../src/braintree/braintree.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwC;AACxC,6CAAwD;AACxD,wCAWuB;AAEvB,yDAAgD;AAChD,2DAAoE;AACpE,2CAAkE;AAI3D,IAAM,iBAAiB,+BAAvB,MAAM,iBAAiB;IAC5B,YACU,UAAmC,EACnC,YAA0B,EAC1B,kBAAsC,EACJ,OAA+B;QAHjE,eAAU,GAAV,UAAU,CAAyB;QACnC,iBAAY,GAAZ,YAAY,CAAc;QAC1B,uBAAkB,GAAlB,kBAAkB,CAAoB;QACJ,YAAO,GAAP,OAAO,CAAwB;IACxE,CAAC;IAGE,AAAN,KAAK,CAAC,4BAA4B,CACzB,GAAmB,EAE1B,EACE,OAAO,EACP,iBAAiB,GAC6B;;QAEhD,IAAI,OAAO,EAAE;YACX,aAAM,CAAC,IAAI,CACT,2GAA2G,CAC5G,CAAC;SACH;QACD,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC;QAC5E,IAAI,CAAC,YAAY,EAAE;YACjB,MAAM,IAAI,0BAAmB,CAC3B,oEAAoE,CACrE,CAAC;SACH;QACD,MAAM,KAAK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,IAAA,kBAAQ,GAAE;IAMN,WAAA,IAAA,eAAM,EAAC,oCAAwB,CAAC,CAAA;qCAHb,8BAAuB;QACrB,mBAAY;QACN,yBAAkB;GAJrC,iBAAiB,CAwG7B"}
@@ -0,0 +1,2 @@
1
+ export declare const loggerCtx = "BraintreePlugin";
2
+ export declare const BRAINTREE_PLUGIN_OPTIONS: unique symbol;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BRAINTREE_PLUGIN_OPTIONS = exports.loggerCtx = void 0;
4
+ exports.loggerCtx = "BraintreePlugin";
5
+ exports.BRAINTREE_PLUGIN_OPTIONS = Symbol("BRAINTREE_PLUGIN_OPTIONS");
6
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/braintree/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,iBAAiB,CAAC;AAC9B,QAAA,wBAAwB,GAAG,MAAM,CAAC,0BAA0B,CAAC,CAAC"}