@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
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ # License 1
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2025-present Aexol
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+
13
+ # License 2
14
+
15
+ The MIT License
16
+
17
+ Copyright (c) 2018-2025 Michael Bromley
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # @deenruv/payments-plugin
2
+
3
+ A collection of payment provider integrations for Deenruv, including Stripe, Mollie, and Braintree. Each provider is a separate sub-plugin that can be used independently.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @deenruv/payments-plugin
9
+ ```
10
+
11
+ Additionally, install the client library for your chosen payment provider(s):
12
+
13
+ ```bash
14
+ # Stripe
15
+ pnpm add stripe
16
+
17
+ # Mollie
18
+ pnpm add @mollie/api-client
19
+
20
+ # Braintree
21
+ pnpm add braintree
22
+ ```
23
+
24
+ ## Sub-Plugins
25
+
26
+ ### StripePlugin
27
+
28
+ Processes payments via the [Stripe](https://stripe.com/docs) Payment Intents API with webhook-based order settlement.
29
+
30
+ ```typescript
31
+ import { StripePlugin } from '@deenruv/payments-plugin/package/stripe';
32
+
33
+ plugins: [
34
+ StripePlugin.init({
35
+ storeCustomersInStripe: true,
36
+ }),
37
+ ];
38
+ ```
39
+
40
+ **Options:**
41
+
42
+ | Option | Type | Description |
43
+ |--------|------|-------------|
44
+ | `storeCustomersInStripe` | `boolean` | Stores Deenruv customers as Stripe customers (adds `stripeCustomerId` custom field) |
45
+
46
+ **API Extensions (Shop):**
47
+
48
+ - `createStripePaymentIntent` mutation - Creates a Stripe PaymentIntent and returns a client secret
49
+
50
+ **Webhook:** Listens at `/payments/stripe` for `payment_intent.succeeded` and `payment_intent.payment_failed` events.
51
+
52
+ ### MolliePlugin
53
+
54
+ Processes payments via the [Mollie](https://docs.mollie.com/) Order API with support for multiple payment methods including pay-later options (Klarna).
55
+
56
+ ```typescript
57
+ import { MolliePlugin } from '@deenruv/payments-plugin/package/mollie';
58
+
59
+ plugins: [
60
+ MolliePlugin.init({
61
+ deenruvHost: 'https://your-deenruv-server.io',
62
+ }),
63
+ ];
64
+ ```
65
+
66
+ **Options:**
67
+
68
+ | Option | Type | Description |
69
+ |--------|------|-------------|
70
+ | `deenruvHost` | `string` | Your Deenruv server host URL (used for Mollie webhooks) |
71
+ | `enabledPaymentMethodsParams` | `fn` | Optional function to provide additional params (locale, billingCountry) to the Mollie methods API |
72
+
73
+ **API Extensions (Shop):**
74
+
75
+ - `createMolliePaymentIntent` mutation - Creates a Mollie payment and returns a redirect URL
76
+ - `molliePaymentMethods` query - Lists available Mollie payment methods
77
+
78
+ **API Extensions (Admin):**
79
+
80
+ - Mollie-specific admin resolvers for payment management
81
+
82
+ ### BraintreePlugin
83
+
84
+ Processes payments via [Braintree](https://www.braintreepayments.com/) with Drop-in UI integration and optional vault (stored payment methods).
85
+
86
+ ```typescript
87
+ import { BraintreePlugin } from '@deenruv/payments-plugin/package/braintree';
88
+ import { Environment } from 'braintree';
89
+
90
+ plugins: [
91
+ BraintreePlugin.init({
92
+ environment: Environment.Sandbox,
93
+ storeCustomersInBraintree: true,
94
+ }),
95
+ ];
96
+ ```
97
+
98
+ **Options:**
99
+
100
+ | Option | Type | Description |
101
+ |--------|------|-------------|
102
+ | `storeCustomersInBraintree` | `boolean` | Enables Braintree vault for stored payment methods (adds `braintreeCustomerId` custom field) |
103
+
104
+ **API Extensions (Shop):**
105
+
106
+ - `generateBraintreeClientToken` query - Generates a client token for the Braintree Drop-in UI
107
+
108
+ ## Features
109
+
110
+ - Stripe Payment Intents API with webhook-based settlement
111
+ - Mollie Order API with 20+ payment methods including pay-later (Klarna)
112
+ - Braintree Drop-in UI with vault for stored payment methods
113
+ - Webhook handlers for async payment confirmation
114
+ - Customer storage in payment provider for returning customers
115
+ - Custom fields added to Order/Customer entities as needed
116
+
117
+ ## Admin UI
118
+
119
+ Server-only plugin. Payment methods are configured through the standard Deenruv Admin UI PaymentMethod settings.
120
+
121
+ ## Development
122
+
123
+ ### Mollie local development
124
+
125
+ For testing out changes to the Mollie plugin locally, with a real Mollie account, follow the steps below. These steps
126
+ will create an order, set Mollie as payment method, and create a payment intent link to the Mollie platform.
127
+
128
+ 1. Get a test api key from your Mollie
129
+ dashboard: https://help.mollie.com/hc/en-us/articles/115000328205-Where-can-I-find-the-API-key-
130
+ 2. Create the file `packages/payments-plugin/.env` with content `MOLLIE_APIKEY=your-test-apikey`
131
+ 3. `cd packages/payments-plugin`
132
+ 4. `npm run dev-server:mollie`
133
+ 5. Watch the logs for `Mollie payment link` and click the link to finalize the test payment.
134
+
135
+ You can change the order flow, payment methods and more in the file `e2e/mollie-dev-server`, and restart the devserver.
136
+
137
+ ### Stripe local development
138
+
139
+ For testing out changes to the Stripe plugin locally, with a real Stripe account, follow the steps below. These steps
140
+ will create an order, set Stripe as payment method, and create a payment secret.
141
+
142
+ 1. Get a test api key from your Stripe
143
+ dashboard: https://dashboard.stripe.com/test/apikeys
144
+ 2. Use Ngrok or Localtunnel to make your localhost publicly available and create a webhook as described here: https://deenruv.com/docs/typescript-api/payments-plugin/stripe-plugin/
145
+ 3. Create the file `packages/payments-plugin/.env` with these contents:
146
+
147
+ ```sh
148
+ STRIPE_APIKEY=sk_test_xxxx
149
+ STRIPE_WEBHOOK_SECRET=webhook-secret
150
+ STRIPE_PUBLISHABLE_KEY=pk_test_xxxx
151
+ ```
152
+
153
+ 1. `cd packages/payments-plugin`
154
+ 2. `yarn dev-server:stripe`
155
+ 3. Watch the logs for the link or go to `http://localhost:3050/checkout` to test the checkout.
156
+
157
+ After checkout completion you can see your payment in https://dashboard.stripe.com/test/payments/
@@ -0,0 +1,11 @@
1
+ import { BraintreeGateway, Transaction } from "braintree";
2
+ import { BraintreePluginOptions, PaymentMethodArgsHash } from "./types";
3
+ export declare function getGateway(args: PaymentMethodArgsHash, options: BraintreePluginOptions): BraintreeGateway;
4
+ /**
5
+ * @description
6
+ * Returns a subset of the Transaction object of interest to the Administrator, plus some
7
+ * public data which may be useful to display in the storefront account area.
8
+ */
9
+ export declare function defaultExtractMetadataFn(transaction: Transaction): {
10
+ [key: string]: any;
11
+ };
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultExtractMetadataFn = exports.getGateway = void 0;
4
+ const braintree_1 = require("braintree");
5
+ function getGateway(args, options) {
6
+ return new braintree_1.BraintreeGateway({
7
+ environment: options.environment || braintree_1.Environment.Sandbox,
8
+ merchantId: args.merchantId,
9
+ privateKey: args.privateKey,
10
+ publicKey: args.publicKey,
11
+ });
12
+ }
13
+ exports.getGateway = getGateway;
14
+ /**
15
+ * @description
16
+ * Returns a subset of the Transaction object of interest to the Administrator, plus some
17
+ * public data which may be useful to display in the storefront account area.
18
+ */
19
+ function defaultExtractMetadataFn(transaction) {
20
+ const metadata = {
21
+ status: transaction.status,
22
+ currencyIsoCode: transaction.currencyIsoCode,
23
+ merchantAccountId: transaction.merchantAccountId,
24
+ cvvCheck: decodeAvsCode(transaction.cvvResponseCode),
25
+ avsPostCodeCheck: decodeAvsCode(transaction.avsPostalCodeResponseCode),
26
+ avsStreetAddressCheck: decodeAvsCode(transaction.avsStreetAddressResponseCode),
27
+ processorAuthorizationCode: transaction.processorAuthorizationCode,
28
+ processorResponseText: transaction.processorResponseText,
29
+ paymentMethod: transaction.paymentInstrumentType,
30
+ public: {},
31
+ };
32
+ if (transaction.creditCard && transaction.creditCard.cardType) {
33
+ const cardData = {
34
+ cardType: transaction.creditCard.cardType,
35
+ last4: transaction.creditCard.last4,
36
+ expirationDate: transaction.creditCard.expirationDate,
37
+ };
38
+ metadata.cardData = cardData;
39
+ metadata.public.cardData = cardData;
40
+ }
41
+ if (transaction.paypalAccount && transaction.paypalAccount.authorizationId) {
42
+ metadata.paypalData = {
43
+ payerEmail: transaction.paypalAccount.payerEmail,
44
+ paymentId: transaction.paypalAccount.paymentId,
45
+ authorizationId: transaction.paypalAccount.authorizationId,
46
+ payerStatus: transaction.paypalAccount.payerStatus,
47
+ sellerProtectionStatus: transaction.paypalAccount.sellerProtectionStatus,
48
+ transactionFeeAmount: transaction.paypalAccount.transactionFeeAmount,
49
+ };
50
+ metadata.public.paypalData = {
51
+ authorizationId: transaction.paypalAccount.authorizationId,
52
+ };
53
+ }
54
+ return metadata;
55
+ }
56
+ exports.defaultExtractMetadataFn = defaultExtractMetadataFn;
57
+ function decodeAvsCode(code) {
58
+ switch (code) {
59
+ case "I":
60
+ return "Not Provided";
61
+ case "M":
62
+ return "Matched";
63
+ case "N":
64
+ return "Not Matched";
65
+ case "U":
66
+ return "Not Verified";
67
+ case "S":
68
+ return "Not Supported";
69
+ case "E":
70
+ return "AVS System Error";
71
+ case "A":
72
+ return "Not Applicable";
73
+ case "B":
74
+ return "Skipped";
75
+ default:
76
+ return "Unknown";
77
+ }
78
+ }
79
+ //# sourceMappingURL=braintree-common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"braintree-common.js","sourceRoot":"","sources":["../../src/braintree/braintree-common.ts"],"names":[],"mappings":";;;AAAA,yCAAuE;AAIvE,SAAgB,UAAU,CACxB,IAA2B,EAC3B,OAA+B;IAE/B,OAAO,IAAI,4BAAgB,CAAC;QAC1B,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,uBAAW,CAAC,OAAO;QACvD,UAAU,EAAE,IAAI,CAAC,UAAU;QAC3B,UAAU,EAAE,IAAI,CAAC,UAAU;QAC3B,SAAS,EAAE,IAAI,CAAC,SAAS;KAC1B,CAAC,CAAC;AACL,CAAC;AAVD,gCAUC;AAED;;;;GAIG;AACH,SAAgB,wBAAwB,CAAC,WAAwB;IAG/D,MAAM,QAAQ,GAA2B;QACvC,MAAM,EAAE,WAAW,CAAC,MAAM;QAC1B,eAAe,EAAE,WAAW,CAAC,eAAe;QAC5C,iBAAiB,EAAE,WAAW,CAAC,iBAAiB;QAChD,QAAQ,EAAE,aAAa,CAAC,WAAW,CAAC,eAAe,CAAC;QACpD,gBAAgB,EAAE,aAAa,CAAC,WAAW,CAAC,yBAAyB,CAAC;QACtE,qBAAqB,EAAE,aAAa,CAClC,WAAW,CAAC,4BAA4B,CACzC;QACD,0BAA0B,EAAE,WAAW,CAAC,0BAA0B;QAClE,qBAAqB,EAAE,WAAW,CAAC,qBAAqB;QACxD,aAAa,EAAE,WAAW,CAAC,qBAAqB;QAChD,MAAM,EAAE,EAAE;KACX,CAAC;IACF,IAAI,WAAW,CAAC,UAAU,IAAI,WAAW,CAAC,UAAU,CAAC,QAAQ,EAAE;QAC7D,MAAM,QAAQ,GAAG;YACf,QAAQ,EAAE,WAAW,CAAC,UAAU,CAAC,QAAQ;YACzC,KAAK,EAAE,WAAW,CAAC,UAAU,CAAC,KAAK;YACnC,cAAc,EAAE,WAAW,CAAC,UAAU,CAAC,cAAc;SACtD,CAAC;QACF,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAC7B,QAAQ,CAAC,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;KACrC;IACD,IAAI,WAAW,CAAC,aAAa,IAAI,WAAW,CAAC,aAAa,CAAC,eAAe,EAAE;QAC1E,QAAQ,CAAC,UAAU,GAAG;YACpB,UAAU,EAAE,WAAW,CAAC,aAAa,CAAC,UAAU;YAChD,SAAS,EAAE,WAAW,CAAC,aAAa,CAAC,SAAS;YAC9C,eAAe,EAAE,WAAW,CAAC,aAAa,CAAC,eAAe;YAC1D,WAAW,EAAE,WAAW,CAAC,aAAa,CAAC,WAAW;YAClD,sBAAsB,EAAE,WAAW,CAAC,aAAa,CAAC,sBAAsB;YACxE,oBAAoB,EAAE,WAAW,CAAC,aAAa,CAAC,oBAAoB;SACrE,CAAC;QACF,QAAQ,CAAC,MAAM,CAAC,UAAU,GAAG;YAC3B,eAAe,EAAE,WAAW,CAAC,aAAa,CAAC,eAAe;SAC3D,CAAC;KACH;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAxCD,4DAwCC;AAED,SAAS,aAAa,CAAC,IAAY;IACjC,QAAQ,IAAI,EAAE;QACZ,KAAK,GAAG;YACN,OAAO,cAAc,CAAC;QACxB,KAAK,GAAG;YACN,OAAO,SAAS,CAAC;QACnB,KAAK,GAAG;YACN,OAAO,aAAa,CAAC;QACvB,KAAK,GAAG;YACN,OAAO,cAAc,CAAC;QACxB,KAAK,GAAG;YACN,OAAO,eAAe,CAAC;QACzB,KAAK,GAAG;YACN,OAAO,kBAAkB,CAAC;QAC5B,KAAK,GAAG;YACN,OAAO,gBAAgB,CAAC;QAC1B,KAAK,GAAG;YACN,OAAO,SAAS,CAAC;QACnB;YACE,OAAO,SAAS,CAAC;KACpB;AACH,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { LanguageCode } from "@deenruv/common/lib/generated-types";
2
+ import { PaymentMethodHandler } from "@deenruv/core";
3
+ /**
4
+ * The handler for Braintree payments.
5
+ */
6
+ export declare const braintreePaymentMethodHandler: PaymentMethodHandler<{
7
+ merchantId: {
8
+ type: "string";
9
+ label: {
10
+ languageCode: LanguageCode.en;
11
+ value: string;
12
+ }[];
13
+ };
14
+ publicKey: {
15
+ type: "string";
16
+ label: {
17
+ languageCode: LanguageCode.en;
18
+ value: string;
19
+ }[];
20
+ };
21
+ privateKey: {
22
+ type: "string";
23
+ label: {
24
+ languageCode: LanguageCode.en;
25
+ value: string;
26
+ }[];
27
+ };
28
+ }>;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.braintreePaymentMethodHandler = void 0;
4
+ const generated_types_1 = require("@deenruv/common/lib/generated-types");
5
+ const core_1 = require("@deenruv/core");
6
+ const braintree_common_1 = require("./braintree-common");
7
+ const constants_1 = require("./constants");
8
+ let options;
9
+ let connection;
10
+ let entityHydrator;
11
+ /**
12
+ * The handler for Braintree payments.
13
+ */
14
+ exports.braintreePaymentMethodHandler = new core_1.PaymentMethodHandler({
15
+ code: "braintree",
16
+ description: [{ languageCode: generated_types_1.LanguageCode.en, value: "Braintree payments" }],
17
+ args: {
18
+ merchantId: {
19
+ type: "string",
20
+ label: [{ languageCode: generated_types_1.LanguageCode.en, value: "Merchant ID" }],
21
+ },
22
+ publicKey: {
23
+ type: "string",
24
+ label: [{ languageCode: generated_types_1.LanguageCode.en, value: "Public Key" }],
25
+ },
26
+ privateKey: {
27
+ type: "string",
28
+ label: [{ languageCode: generated_types_1.LanguageCode.en, value: "Private Key" }],
29
+ },
30
+ },
31
+ init(injector) {
32
+ options = injector.get(constants_1.BRAINTREE_PLUGIN_OPTIONS);
33
+ connection = injector.get(core_1.TransactionalConnection);
34
+ entityHydrator = injector.get(core_1.EntityHydrator);
35
+ },
36
+ async createPayment(ctx, order, amount, args, metadata) {
37
+ const gateway = (0, braintree_common_1.getGateway)(args, options);
38
+ let customerId;
39
+ try {
40
+ await entityHydrator.hydrate(ctx, order, { relations: ["customer"] });
41
+ const customer = order.customer;
42
+ if (options.storeCustomersInBraintree &&
43
+ ctx.activeUserId &&
44
+ customer &&
45
+ metadata.includeCustomerId !== false) {
46
+ customerId = await getBraintreeCustomerId(ctx, gateway, customer);
47
+ }
48
+ return processPayment(ctx, gateway, order, amount, metadata.nonce, customerId, options);
49
+ }
50
+ catch (e) {
51
+ core_1.Logger.error(e, constants_1.loggerCtx);
52
+ return {
53
+ amount,
54
+ state: "Error",
55
+ transactionId: "",
56
+ errorMessage: e.toString(),
57
+ metadata: e,
58
+ };
59
+ }
60
+ },
61
+ settlePayment() {
62
+ return {
63
+ success: true,
64
+ };
65
+ },
66
+ async createRefund(ctx, input, total, order, payment, args) {
67
+ var _a;
68
+ const gateway = (0, braintree_common_1.getGateway)(args, options);
69
+ const response = await gateway.transaction.refund(payment.transactionId, (total / 100).toString(10));
70
+ if (!response.success) {
71
+ return {
72
+ state: "Failed",
73
+ transactionId: (_a = response.transaction) === null || _a === void 0 ? void 0 : _a.id,
74
+ metadata: response,
75
+ };
76
+ }
77
+ return {
78
+ state: "Settled",
79
+ transactionId: response.transaction.id,
80
+ metadata: response,
81
+ };
82
+ },
83
+ });
84
+ async function processPayment(ctx, gateway, order, amount, paymentMethodNonce, customerId, pluginOptions) {
85
+ var _a;
86
+ const response = await gateway.transaction.sale({
87
+ customerId,
88
+ amount: (amount / 100).toString(10),
89
+ orderId: order.code,
90
+ paymentMethodNonce,
91
+ options: {
92
+ submitForSettlement: true,
93
+ storeInVaultOnSuccess: !!customerId,
94
+ },
95
+ });
96
+ const extractMetadataFn = (_a = pluginOptions.extractMetadata) !== null && _a !== void 0 ? _a : braintree_common_1.defaultExtractMetadataFn;
97
+ const metadata = extractMetadataFn(response.transaction);
98
+ if (!response.success) {
99
+ return {
100
+ amount,
101
+ state: "Declined",
102
+ transactionId: response.transaction.id,
103
+ errorMessage: response.message,
104
+ metadata,
105
+ };
106
+ }
107
+ return {
108
+ amount,
109
+ state: "Settled",
110
+ transactionId: response.transaction.id,
111
+ metadata,
112
+ };
113
+ }
114
+ /**
115
+ * If the Customer has no braintreeCustomerId, create one, else return the existing braintreeCustomerId.
116
+ */
117
+ async function getBraintreeCustomerId(ctx, gateway, customer) {
118
+ if (!customer.customFields.braintreeCustomerId) {
119
+ try {
120
+ const result = await gateway.customer.create({
121
+ firstName: customer.firstName,
122
+ lastName: customer.lastName,
123
+ email: customer.emailAddress,
124
+ });
125
+ if (result.success) {
126
+ const customerId = result.customer.id;
127
+ core_1.Logger.verbose(`Created Braintree Customer record for customerId ${customer.id}`, constants_1.loggerCtx);
128
+ customer.customFields.braintreeCustomerId = customerId;
129
+ await connection
130
+ .getRepository(ctx, core_1.Customer)
131
+ .save(customer, { reload: false });
132
+ return customerId;
133
+ }
134
+ else {
135
+ core_1.Logger.error(`Failed to create Braintree Customer record for customerId ${customer.id}. View Debug level logs for details.`, constants_1.loggerCtx);
136
+ core_1.Logger.debug(JSON.stringify(result.errors, null, 2), constants_1.loggerCtx);
137
+ }
138
+ }
139
+ catch (e) {
140
+ core_1.Logger.error(e.message, constants_1.loggerCtx, e.stack);
141
+ }
142
+ }
143
+ else {
144
+ return customer.customFields.braintreeCustomerId;
145
+ }
146
+ }
147
+ //# sourceMappingURL=braintree.handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"braintree.handler.js","sourceRoot":"","sources":["../../src/braintree/braintree.handler.ts"],"names":[],"mappings":";;;AAAA,yEAAmE;AACnE,wCASuB;AAGvB,yDAA0E;AAC1E,2CAAkE;AAGlE,IAAI,OAA+B,CAAC;AACpC,IAAI,UAAmC,CAAC;AACxC,IAAI,cAA8B,CAAC;AAEnC;;GAEG;AACU,QAAA,6BAA6B,GAAG,IAAI,2BAAoB,CAAC;IACpE,IAAI,EAAE,WAAW;IACjB,WAAW,EAAE,CAAC,EAAE,YAAY,EAAE,8BAAY,CAAC,EAAE,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC;IAC7E,IAAI,EAAE;QACJ,UAAU,EAAE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