@payloadcms/plugin-stripe 0.0.4 → 0.0.5

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 (49) hide show
  1. package/README.md +84 -8
  2. package/dist/extendWebpackConfig.js +1 -1
  3. package/dist/extendWebpackConfig.js.map +1 -1
  4. package/dist/hooks/createNewInStripe.d.ts +7 -0
  5. package/dist/hooks/createNewInStripe.js +125 -0
  6. package/dist/hooks/createNewInStripe.js.map +1 -0
  7. package/dist/hooks/createNewStripeCustomer.d.ts +2 -0
  8. package/dist/hooks/createNewStripeCustomer.js +104 -0
  9. package/dist/hooks/createNewStripeCustomer.js.map +1 -0
  10. package/dist/hooks/deleteFromStripe.d.ts +7 -0
  11. package/dist/hooks/deleteFromStripe.js +81 -0
  12. package/dist/hooks/deleteFromStripe.js.map +1 -0
  13. package/dist/hooks/deleteStripeCustomer.d.ts +2 -0
  14. package/dist/hooks/deleteStripeCustomer.js +74 -0
  15. package/dist/hooks/deleteStripeCustomer.js.map +1 -0
  16. package/dist/hooks/syncExistingStripeCustomer.d.ts +2 -0
  17. package/dist/hooks/syncExistingStripeCustomer.js +80 -0
  18. package/dist/hooks/syncExistingStripeCustomer.js.map +1 -0
  19. package/dist/hooks/syncExistingWithStripe.d.ts +7 -0
  20. package/dist/hooks/syncExistingWithStripe.js +96 -0
  21. package/dist/hooks/syncExistingWithStripe.js.map +1 -0
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.js +129 -16
  24. package/dist/index.js.map +1 -1
  25. package/dist/mocks/serverModule.d.ts +2 -1
  26. package/dist/mocks/serverModule.js +4 -1
  27. package/dist/mocks/serverModule.js.map +1 -1
  28. package/dist/routes/rest.d.ts +6 -1
  29. package/dist/routes/rest.js +3 -2
  30. package/dist/routes/rest.js.map +1 -1
  31. package/dist/routes/webhooks.d.ts +8 -1
  32. package/dist/routes/webhooks.js +99 -22
  33. package/dist/routes/webhooks.js.map +1 -1
  34. package/dist/types.d.ts +28 -4
  35. package/dist/utilities/stripeProxy.js +7 -1
  36. package/dist/utilities/stripeProxy.js.map +1 -1
  37. package/dist/webhooks/handleCreatedOrUpdated.d.ts +7 -0
  38. package/dist/webhooks/handleCreatedOrUpdated.js +194 -0
  39. package/dist/webhooks/handleCreatedOrUpdated.js.map +1 -0
  40. package/dist/webhooks/handleDeleted.d.ts +7 -0
  41. package/dist/webhooks/handleDeleted.js +106 -0
  42. package/dist/webhooks/handleDeleted.js.map +1 -0
  43. package/dist/webhooks/handleWebhooks.d.ts +2 -0
  44. package/dist/webhooks/handleWebhooks.js +236 -0
  45. package/dist/webhooks/handleWebhooks.js.map +1 -0
  46. package/dist/webhooks/index.d.ts +2 -0
  47. package/dist/webhooks/index.js +94 -0
  48. package/dist/webhooks/index.js.map +1 -0
  49. package/package.json +3 -3
package/README.md CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  [![NPM](https://img.shields.io/npm/v/@payloadcms/plugin-stripe)](https://www.npmjs.com/package/@payloadcms/plugin-stripe)
4
4
 
5
- A plugin for [Payload CMS](https://github.com/payloadcms/payload) to manage [Stripe](https://stripe.com) through Payload.
5
+ A plugin for [Payload CMS](https://github.com/payloadcms/payload) to connect [Stripe](https://stripe.com) and Payload.
6
6
 
7
7
  Core features:
8
- - Layers your Stripe account behind [Payload access control](https://payloadcms.com/docs/access-control/overview)
8
+ - Hides your Stripe credentials when shipping SaaS applications
9
+ - Allows restricted keys through [Payload access control](https://payloadcms.com/docs/access-control/overview)
9
10
  - Enables a two-way communication channel between Stripe and Payload
10
11
  - Proxies the [Stripe REST API](https://stripe.com/docs/api)
11
12
  - Proxies [Stripe webhooks](https://stripe.com/docs/webhooks)
13
+ - Automatically syncs data between the two platforms
12
14
 
13
15
  ## Installation
14
16
 
@@ -43,17 +45,75 @@ export default config;
43
45
 
44
46
  Required. Your Stripe secret key.
45
47
 
48
+ - `sync`
49
+
50
+ Optional. An array of sync configs. This will automatically configure a sync between Payload collections and Stripe resources on create, delete, and update. See [sync](#sync) for more details.
51
+
46
52
  - `stripeWebhooksEndpointSecret`
47
53
 
48
- Optional. Your Stripe webhook endpoint secret. This is needed only if you wish to sync data from Stripe to Payload.
54
+ Optional. Your Stripe webhook endpoint secret. This is needed only if you wish to sync data _from_ Stripe _to_ Payload.
49
55
 
50
56
  - `webhooks`
51
57
 
52
- Optional. An object of Stripe webhook handlers, keyed to the name of the event. See [webhooks](#webhooks) for more details or for a list of all available webhooks, see [here](https://stripe.com/docs/cli/trigger#trigger-event).
58
+ Optional. Either a function to handle all webhooks events, or an object of Stripe webhook handlers, keyed to the name of the event. See [webhooks](#webhooks) for more details or for a list of all available webhooks, see [here](https://stripe.com/docs/cli/trigger#trigger-event).
59
+
60
+ - `logs`
61
+
62
+ Optional. When `true`, logs sync events to the console as they happen.
63
+
64
+ ## Sync
65
+
66
+ This option will setup a basic sync between Payload collections and Stripe resources for you automatically. It will create all the necessary hooks and webhooks handlers, so the only thing you have to do is map your Payload fields to their corresponding Stripe properties. As documents are created, updated, and deleted from either Stripe or Payload, the changes are reflected on either side.
67
+
68
+ > NOTE: If you wish to enable a _two-way_ sync, be sure to setup [`webhooks`](#webhooks) and pass the `stripeWebhooksEndpointSecret` through your config.
69
+
70
+ > NOTE: Due to limitations in the Stripe API, this currently only works with top-level fields. This is because Stripe manages data objects separately, so a webhook originating from a customer's subscription include all customer data. Inversely, you cannot update a customer's subscription by updating the customer. In the future, we can build a pattern around this. But for now, cases like that will need to be hard-coded. See the [demo](./demo) for an example of this.
71
+
72
+ ```js
73
+ import { buildConfig } from 'payload/config';
74
+ import stripePlugin from '@payloadcms/plugin-stripe';
75
+
76
+ const config = buildConfig({
77
+ plugins: [
78
+ stripePlugin({
79
+ stripeSecretKey: process.env.STRIPE_SECRET_KEY,
80
+ stripeWebhooksEndpointSecret: process.env.STRIPE_WEBHOOKS_ENDPOINT_SECRET,
81
+ sync: [
82
+ {
83
+ collection: 'customers',
84
+ object: 'customers', // one of the Stripe object types
85
+ objectSingular: 'customer',
86
+ fields: [
87
+ {
88
+ field: 'name', // this is a field on your own Payload config
89
+ property: 'name' // use dot notation, if applicable
90
+ }
91
+ ]
92
+ }
93
+ ]
94
+ })
95
+ ]
96
+ });
97
+
98
+ export default config;
99
+ ```
100
+
101
+ Using `sync` will do the following:
102
+
103
+ - Adds and maintains a `stripeID` read-only field on each collection. This is a field generated _by Stripe_ and used as a cross-reference.
104
+ - Adds and maintains an `skipSync` read-only flag on each collection to prevent infinite syncs when hooks trigger webhooks
105
+ - Adds the following hooks to each collection:
106
+ - `beforeValidate`: `createNewInStripe`
107
+ - `afterChange`: `syncExistingWithStripe`
108
+ - `afterDelete`: `deleteFromStripe`
109
+ - Handles the following Stripe webhooks
110
+ - `STRIPE_TYPE.created`: `handleCreatedOrUpdated`
111
+ - `STRIPE_TYPE.updated`: `handleCreatedOrUpdated`
112
+ - `STRIPE_TYPE.deleted`: `handleDeleted`
53
113
 
54
114
  ### Endpoints
55
115
 
56
- One core functionality of this plugin is to enable a two-way communication channel between Stripe and Payload. To do this, the following custom endpoints are automatically opened for you.
116
+ The following custom endpoints are automatically opened for you:
57
117
 
58
118
  >NOTE: the `/api` part of these routes may be different based on the settings defined in your Payload config.
59
119
 
@@ -78,7 +138,7 @@ One core functionality of this plugin is to enable a two-way communication chann
78
138
  })
79
139
  ```
80
140
 
81
- - `POST /api/stripe/webhooks`
141
+ - `POST /stripe/webhooks`
82
142
 
83
143
  Returns an http status code. This is where all Stripe webhook events are sent to be handled. See [webhooks](#webhooks).
84
144
 
@@ -88,12 +148,14 @@ One core functionality of this plugin is to enable a two-way communication chann
88
148
 
89
149
  Development:
90
150
  1. Login using Stripe cli `stripe login`
91
- 1. Forward events to localhost `stripe listen --forward-to localhost:3000/api/stripe/webhooks`
151
+ 1. Forward events to localhost `stripe listen --forward-to localhost:3000/stripe/webhooks`
152
+ 1. Paste the given secret into your `.env` file as `STRIPE_WEBHOOKS_ENDPOINT_SECRET`
92
153
 
93
154
  Production:
94
155
  1. Login and [create a new webhook](https://dashboard.stripe.com/test/webhooks/create) from the Stripe dashboard
95
156
  1. Paste `YOUR_DOMAIN_NAME/api/stripe/webhooks` as the "Webhook Endpoint URL"
96
157
  1. Select which events to broadcast
158
+ 1. Paste the given secret into your `.env` file as `STRIPE_WEBHOOKS_ENDPOINT_SECRET`
97
159
  1. Then, handle these events using the `webhooks` portion of this plugin's config:
98
160
 
99
161
  ```js
@@ -106,8 +168,22 @@ const config = buildConfig({
106
168
  stripeSecretKey: process.env.STRIPE_SECRET_KEY,
107
169
  stripeWebhooksEndpointSecret: process.env.STRIPE_WEBHOOKS_ENDPOINT_SECRET,
108
170
  webhooks: {
109
- 'customer.subscription.updated': () => {}
171
+ 'customer.subscription.updated': ({ event, stripe, stripeConfig }) => {
172
+ // DO SOMETHING
173
+ }
110
174
  }
175
+ // NOTE: you can also catch all Stripe webhook events and handle the event types yourself
176
+ // webhooks: (event, stripe, stripeConfig) => {
177
+ // switch (event.type): {
178
+ // case 'customer.subscription.updated': {
179
+ // // DO SOMETHING
180
+ // break;
181
+ // }
182
+ // default: {
183
+ // break;
184
+ // }
185
+ // }
186
+ // }
111
187
  })
112
188
  ]
113
189
  });
@@ -24,7 +24,7 @@ var extendWebpackConfig = function (config) {
24
24
  var existingWebpackConfig = typeof ((_b = config.admin) === null || _b === void 0 ? void 0 : _b.webpack) === 'function'
25
25
  ? config.admin.webpack(webpackConfig)
26
26
  : webpackConfig;
27
- return __assign(__assign({}, existingWebpackConfig), { resolve: __assign(__assign({}, (existingWebpackConfig.resolve || {})), { alias: __assign(__assign({}, (((_c = existingWebpackConfig.resolve) === null || _c === void 0 ? void 0 : _c.alias) ? existingWebpackConfig.resolve.alias : {})), (_a = { "stripe": mockModulePath }, _a[path_1.default.resolve(__dirname, 'routes/rest')] = mockModulePath, _a[path_1.default.resolve(__dirname, 'routes/webhooks')] = mockModulePath, _a)) }) });
27
+ return __assign(__assign({}, existingWebpackConfig), { resolve: __assign(__assign({}, (existingWebpackConfig.resolve || {})), { alias: __assign(__assign({}, (((_c = existingWebpackConfig.resolve) === null || _c === void 0 ? void 0 : _c.alias) ? existingWebpackConfig.resolve.alias : {})), (_a = { "stripe": mockModulePath, "express": mockModulePath }, _a[path_1.default.resolve(__dirname, './routes/rest')] = mockModulePath, _a[path_1.default.resolve(__dirname, './routes/webhooks')] = mockModulePath, _a[path_1.default.resolve(__dirname, './webhooks/handleWebhooks')] = mockModulePath, _a[path_1.default.resolve(__dirname, './hooks/createNewInStripe')] = mockModulePath, _a[path_1.default.resolve(__dirname, './hooks/deleteFromStripe')] = mockModulePath, _a[path_1.default.resolve(__dirname, './hooks/syncExistingWithStripe')] = mockModulePath, _a)) }) });
28
28
  };
29
29
  };
30
30
  exports.extendWebpackConfig = extendWebpackConfig;
@@ -1 +1 @@
1
- {"version":3,"file":"extendWebpackConfig.js","sourceRoot":"","sources":["../src/extendWebpackConfig.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,8CAAuB;AAGvB,IAAM,cAAc,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,uBAAuB,CAAC,CAAC;AAEjE,IAAM,mBAAmB,GAAG,UAAC,MAAc;IAChD,OAAA,UAAA,aAAa;;;QACX,IAAM,qBAAqB,GACzB,OAAO,CAAA,MAAA,MAAM,CAAC,KAAK,0CAAE,OAAO,CAAA,KAAK,UAAU;YACzC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC;YACrC,CAAC,CAAC,aAAa,CAAA;QAEnB,6BACK,qBAAqB,KACxB,OAAO,wBACF,CAAC,qBAAqB,CAAC,OAAO,IAAI,EAAE,CAAC,KACxC,KAAK,wBACA,CAAC,CAAA,MAAA,qBAAqB,CAAC,OAAO,0CAAE,KAAK,EAAC,CAAC,CAAC,qBAAqB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,WACpF,QAAQ,EAAE,cAAc,OACvB,cAAI,CAAC,OAAO,CAAC,SA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CAAC,IAAG,cAAc,KACpE,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,gCAAgC,CAAC,IAAG,cAAc,aAGhF;IACH,CAAC;AAvBD,CAuBC,CAAA;AAxBU,QAAA,mBAAmB,uBAwB7B"}
@@ -0,0 +1,7 @@
1
+ import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload/types';
2
+ import { StripeConfig } from '../types';
3
+ export declare type CollectionBeforeValidateHookWithArgs = (args: Parameters<CollectionBeforeValidateHook>[0] & {
4
+ collection?: CollectionConfig;
5
+ stripeConfig?: StripeConfig;
6
+ }) => void;
7
+ export declare const createNewInStripe: CollectionBeforeValidateHookWithArgs;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.createNewInStripe = void 0;
43
+ var stripe_1 = __importDefault(require("stripe"));
44
+ var errors_1 = require("payload/errors");
45
+ var stripeSecretKey = process.env.STRIPE_SECRET_KEY;
46
+ var stripe = new stripe_1.default(stripeSecretKey || '', { apiVersion: '2022-08-01' });
47
+ var createNewInStripe = function (args) { return __awaiter(void 0, void 0, void 0, function () {
48
+ var req, operation, data, collection, stripeConfig, _a, sync, logs, payload, dataRef, collectionSlug_1, syncConfig, syncedFields, stripeResource, error_1, stripeResource, error_2;
49
+ var _b, _c;
50
+ return __generator(this, function (_d) {
51
+ switch (_d.label) {
52
+ case 0:
53
+ req = args.req, operation = args.operation, data = args.data, collection = args.collection, stripeConfig = args.stripeConfig;
54
+ _a = stripeConfig || {}, sync = _a.sync, logs = _a.logs;
55
+ payload = req === null || req === void 0 ? void 0 : req.payload;
56
+ dataRef = data || {};
57
+ if (process.env.NODE_ENV === 'test') {
58
+ dataRef.stripeID = 'test';
59
+ return [2 /*return*/, dataRef];
60
+ }
61
+ if (!payload) return [3 /*break*/, 9];
62
+ if (!dataRef.skipSync) return [3 /*break*/, 1];
63
+ if (logs)
64
+ payload.logger.info("Bypassing collection-level hooks.");
65
+ return [3 /*break*/, 9];
66
+ case 1:
67
+ // Initialize false so that all Payload events sync to Stripe
68
+ // conditionally set to true to prevent events that originate from webhooks from triggering an unnecessary sync
69
+ dataRef.skipSync = false;
70
+ collectionSlug_1 = (collection || {}).slug;
71
+ syncConfig = sync === null || sync === void 0 ? void 0 : sync.find(function (syncConfig) { return syncConfig.collection === collectionSlug_1; });
72
+ if (!syncConfig) return [3 /*break*/, 9];
73
+ syncedFields = syncConfig.fields.reduce(function (acc, field) {
74
+ var fieldName = field.field, property = field.property;
75
+ acc[fieldName] = dataRef[property];
76
+ return acc;
77
+ }, {});
78
+ if (!(operation === 'update')) return [3 /*break*/, 5];
79
+ if (logs)
80
+ payload.logger.info("A '".concat(collectionSlug_1, "' document has changed in Payload with ID: '").concat(data === null || data === void 0 ? void 0 : data.id, "', syncing with Stripe..."));
81
+ if (!!dataRef.stripeID) return [3 /*break*/, 5];
82
+ _d.label = 2;
83
+ case 2:
84
+ _d.trys.push([2, 4, , 5]);
85
+ return [4 /*yield*/, ((_b = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig.resource]) === null || _b === void 0 ? void 0 : _b.create(syncedFields))];
86
+ case 3:
87
+ stripeResource = _d.sent();
88
+ if (logs)
89
+ payload.logger.info("\u2705 Successfully created new '".concat(syncConfig.resource, "' resource in Stripe with ID: '").concat(stripeResource.id, "'."));
90
+ dataRef.stripeID = stripeResource.id;
91
+ // NOTE: this is to prevent sync in the "afterChange" hook
92
+ dataRef.skipSync = true;
93
+ return [3 /*break*/, 5];
94
+ case 4:
95
+ error_1 = _d.sent();
96
+ if (logs)
97
+ payload.logger.error("- Error creating Stripe document: ".concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
98
+ return [3 /*break*/, 5];
99
+ case 5:
100
+ if (!(operation === 'create')) return [3 /*break*/, 9];
101
+ if (logs)
102
+ payload.logger.info("A new '".concat(collectionSlug_1, "' document was created in Payload with ID: '").concat(data === null || data === void 0 ? void 0 : data.id, "', syncing with Stripe..."));
103
+ _d.label = 6;
104
+ case 6:
105
+ _d.trys.push([6, 8, , 9]);
106
+ if (logs)
107
+ payload.logger.info("- Creating new '".concat(syncConfig.resource, "' resource in Stripe..."));
108
+ return [4 /*yield*/, ((_c = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig.resource]) === null || _c === void 0 ? void 0 : _c.create(syncedFields))];
109
+ case 7:
110
+ stripeResource = _d.sent();
111
+ if (logs)
112
+ payload.logger.info("\u2705 Successfully created new '".concat(syncConfig.resource, "' resource in Stripe with ID: '").concat(stripeResource.id, "'."));
113
+ dataRef.stripeID = stripeResource.id;
114
+ // NOTE: this is to prevent sync in the "afterChange" hook
115
+ dataRef.skipSync = true;
116
+ return [3 /*break*/, 9];
117
+ case 8:
118
+ error_2 = _d.sent();
119
+ throw new errors_1.APIError("Failed to create new '".concat(syncConfig.resource, "' resource in Stripe: ").concat((error_2 === null || error_2 === void 0 ? void 0 : error_2.message) || ''));
120
+ case 9: return [2 /*return*/, dataRef];
121
+ }
122
+ });
123
+ }); };
124
+ exports.createNewInStripe = createNewInStripe;
125
+ //# sourceMappingURL=createNewInStripe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createNewInStripe.js","sourceRoot":"","sources":["../../src/hooks/createNewInStripe.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4B;AAG5B,yCAA0C;AAE1C,IAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACtD,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,Y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C;gBAGxE,qBAAM,CAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,UAAU,CAAC,QAAQ,CAAC,0CAAE,MAAM,CAAC,YAAmB,CAAC,CAAA,EAAA;;gBAAjF,cAAc,GAAG,SAAgE;gBAEvF,IAAI,IAAI;oBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,2CAA+B,UAAU,CAAC,QAAQ,4CAAkC,cAAc,CAAC,EAAE,OAAI,CAAC,CAAC;gBAEzI,OAAO,CAAC,QAAQ,GAAG,cAAc,CAAC,EAAE,CAAC;gBAErC,0DAA0D;gBAC1D,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;;;;gBAExB,MAAM,IAAI,iBAAQ,CAAC,gCAAyB,UAAU,CAAC,QAAQ,mCAAyB,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAC;oBAO1H,sBAAO,OAAO,EAAC;;;KAChB,CAAC;AAvFW,QAAA,iBAAiB,qBAuF5B"}
@@ -0,0 +1,2 @@
1
+ import type { CollectionBeforeValidateHook } from 'payload/types';
2
+ export declare const createNewStripeCustomer: CollectionBeforeValidateHook;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.createNewStripeCustomer = void 0;
43
+ var errors_1 = require("payload/errors");
44
+ var stripe_1 = __importDefault(require("stripe"));
45
+ var stripeSecretKey = process.env.STRIPE_SECRET_KEY;
46
+ var stripe = new stripe_1.default(stripeSecretKey || '', { apiVersion: '2022-08-01' });
47
+ var createNewStripeCustomer = function (_a) {
48
+ var req = _a.req, operation = _a.operation, data = _a.data;
49
+ return __awaiter(void 0, void 0, void 0, function () {
50
+ var dataRef, payload, customerQuery, existingCustomer, customer, error_1;
51
+ return __generator(this, function (_b) {
52
+ switch (_b.label) {
53
+ case 0:
54
+ dataRef = data || {};
55
+ if (!req) return [3 /*break*/, 7];
56
+ payload = req.payload;
57
+ if (!(operation === 'create' && (data === null || data === void 0 ? void 0 : data.email))) return [3 /*break*/, 6];
58
+ return [4 /*yield*/, payload.find({
59
+ collection: 'customers',
60
+ where: {
61
+ email: {
62
+ equals: data.email,
63
+ },
64
+ },
65
+ })];
66
+ case 1:
67
+ customerQuery = _b.sent();
68
+ existingCustomer = customerQuery.docs[0];
69
+ if (existingCustomer) {
70
+ throw new errors_1.APIError("Account already exists with e-mail: ".concat(data.email, ". If this is your e-mail, please log in and checkout again."));
71
+ }
72
+ if (!(process.env.NODE_ENV === 'test')) return [3 /*break*/, 2];
73
+ dataRef.stripeCustomerID = 'test';
74
+ return [3 /*break*/, 6];
75
+ case 2:
76
+ if (!!data.isSyncedToStripe) return [3 /*break*/, 6];
77
+ console.log("Creating new customer in Stripe with email: '".concat(data.email, "'"));
78
+ _b.label = 3;
79
+ case 3:
80
+ _b.trys.push([3, 5, , 6]);
81
+ return [4 /*yield*/, stripe.customers.create({
82
+ email: data.email,
83
+ name: data.name,
84
+ })];
85
+ case 4:
86
+ customer = _b.sent();
87
+ console.log("Successfully created new customer in Stripe. Their customer ID is: '".concat(customer.id, "'."));
88
+ dataRef.stripeCustomerID = customer.id;
89
+ dataRef.isSyncedToStripe = true;
90
+ return [3 /*break*/, 6];
91
+ case 5:
92
+ error_1 = _b.sent();
93
+ throw new errors_1.APIError("Error creating new customer in Stripe: ".concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
94
+ case 6:
95
+ // Set to false so that all Payload events sync to Stripe, EXCEPT for those that originate from webhooks
96
+ dataRef.isSyncedToStripe = false;
97
+ _b.label = 7;
98
+ case 7: return [2 /*return*/, dataRef];
99
+ }
100
+ });
101
+ });
102
+ };
103
+ exports.createNewStripeCustomer = createNewStripeCustomer;
104
+ //# sourceMappingURL=createNewStripeCustomer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createNewStripeCustomer.js","sourceRoot":"","sources":["../../src/hooks/createNewStripeCustomer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAA0C;AAC1C,kDAA4B;AAG5B,IAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACtD,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAC;AAExE,IAAM,uBAAuB,GAAiC,UAAO,EAAwB;QAAtB,GAAG,SAAA,EAAE,SAAS,eAAA,EAAE,IAAI,UAAA;;;;;;oBAC1F,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;yBAEvB,GAAG,EAAH,wBAAG;oBACG,OAAO,GAAK,GAAG,QAAR,CAAS;yBAGpB,CAAA,SAAS,KAAK,QAAQ,KAAI,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,CAAA,CAAA,EAArC,wBAAqC;oBACjB,qBAAM,OAAO,CAAC,IAAI,CAAC;4BACvC,UAAU,EAAE,WAAW;4BACvB,KAAK,EAAE;gCACL,KAAK,EAAE;oCACL,MAAM,EAAE,IAAI,CAAC,KAAK;iCACnB;6BACF;yBACF,CAAC,EAAA;;oBAPI,aAAa,GAAG,SAOpB;oBAEI,gBAAgB,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;oBAE/C,IAAI,gBAAgB,EAAE;wBACpB,MAAM,IAAI,iBAAQ,CAChB,8CAAuC,IAAI,CAAC,KAAK,gEAA6D,CAC/G,CAAC;qBACH;yBAEG,CAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAA,EAA/B,wBAA+B;oBACjC,OAAO,CAAC,gBAAgB,GAAG,MAAM,CAAC;;;yBAE9B,CAAC,IAAI,CAAC,gBAAgB,EAAtB,wBAAsB;oBACxB,OAAO,CAAC,GAAG,CAAC,uDAAgD,IAAI,CAAC,KAAK,MAAG,CAAC,CAAC;;;;oBAGxD,qBAAM,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;4BAC7C,KAAK,EAAE,IAAI,CAAC,KAAK;4BACjB,IAAI,EAAE,IAAI,CAAC,IAAI;yBAChB,CAAC,EAAA;;oBAHI,QAAQ,GAAG,SAGf;oBAEF,OAAO,CAAC,GAAG,CAAC,8EAAuE,QAAQ,CAAC,EAAE,OAAI,CAAC,CAAC;oBAEpG,OAAO,CAAC,gBAAgB,GAAG,QAAQ,CAAC,EAAE,CAAC;oBACvC,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC;;;;oBAEhC,MAAM,IAAI,iBAAQ,CAAC,iDAA0C,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAC;;oBAM7F,wGAAwG;oBACxG,OAAO,CAAC,gBAAgB,GAAG,KAAK,CAAC;;wBAGnC,sBAAO,OAAO,EAAC;;;;CAChB,CAAC;AArDW,QAAA,uBAAuB,2BAqDlC"}
@@ -0,0 +1,7 @@
1
+ import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload/types';
2
+ import { StripeConfig } from '../types';
3
+ export declare type CollectionAfterDeleteHookWithArgs = (args: Parameters<CollectionAfterDeleteHook>[0] & {
4
+ collection?: CollectionConfig;
5
+ stripeConfig?: StripeConfig;
6
+ }) => void;
7
+ export declare const deleteFromStripe: CollectionAfterDeleteHookWithArgs;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.deleteFromStripe = void 0;
43
+ var stripe_1 = __importDefault(require("stripe"));
44
+ var errors_1 = require("payload/errors");
45
+ var stripeSecretKey = process.env.STRIPE_SECRET_KEY;
46
+ var stripe = new stripe_1.default(stripeSecretKey || '', { apiVersion: '2022-08-01' });
47
+ var deleteFromStripe = function (args) { return __awaiter(void 0, void 0, void 0, function () {
48
+ var req, doc, stripeConfig, collection, _a, logs, sync, payload, collectionSlug, syncConfig, error_1;
49
+ var _b;
50
+ return __generator(this, function (_c) {
51
+ switch (_c.label) {
52
+ case 0:
53
+ req = args.req, doc = args.doc, stripeConfig = args.stripeConfig, collection = args.collection;
54
+ _a = stripeConfig || {}, logs = _a.logs, sync = _a.sync;
55
+ payload = req.payload;
56
+ collectionSlug = (collection || {}).slug;
57
+ if (logs)
58
+ payload.logger.info("Document with ID: '".concat(doc === null || doc === void 0 ? void 0 : doc.id, "' in collection: '").concat(collectionSlug, "' has been deleted, deleting from Stripe..."));
59
+ if (!(process.env.NODE_ENV !== 'test')) return [3 /*break*/, 4];
60
+ if (logs)
61
+ payload.logger.info("- Deleting Stripe document with ID: '".concat(doc.stripeID, "'..."));
62
+ syncConfig = sync === null || sync === void 0 ? void 0 : sync.find(function (syncConfig) { return syncConfig.collection === collectionSlug; });
63
+ if (!syncConfig) return [3 /*break*/, 4];
64
+ _c.label = 1;
65
+ case 1:
66
+ _c.trys.push([1, 3, , 4]);
67
+ return [4 /*yield*/, ((_b = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig.resource]) === null || _b === void 0 ? void 0 : _b.del(doc.stripeID))];
68
+ case 2:
69
+ _c.sent();
70
+ if (logs)
71
+ payload.logger.info("\u2705 Successfully deleted Stripe document with ID: '".concat(doc.stripeID, "'."));
72
+ return [3 /*break*/, 4];
73
+ case 3:
74
+ error_1 = _c.sent();
75
+ throw new errors_1.APIError("Failed to delete Stripe document with ID: '".concat(doc.stripeID, "': ").concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
76
+ case 4: return [2 /*return*/];
77
+ }
78
+ });
79
+ }); };
80
+ exports.deleteFromStripe = deleteFromStripe;
81
+ //# sourceMappingURL=deleteFromStripe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteFromStripe.js","sourceRoot":"","sources":["../../src/hooks/deleteFromStripe.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kDAA4B;AAE5B,yCAA0C;AAE1C,IAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACtD,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAC;AAOxE,IAAM,gBAAgB,GAAsC,UAAO,IAAI;;;;;;gBAE1E,GAAG,GAID,IAAI,IAJH,EACH,GAAG,GAGD,IAAI,IAHH,EACH,YAAY,GAEV,IAAI,aAFM,EACZ,UAAU,GACR,IAAI,WADI,CACH;gBAEH,KAGF,YAAY,IAAI,EAAE,EAFpB,IAAI,UAAA,EACJ,IAAI,UAAA,CACiB;gBAEf,OAAO,GAAK,GAAG,QAAR,CAAS;gBACV,cAAc,GAAK,CAAA,UAAU,IAAI,EAAE,CAAA,KAArB,CAAsB;gBAElD,IAAI,IAAI;oBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,6BAAsB,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,EAAE,+BAAqB,cAAc,gDAA6C,CAAC,CAAC;qBAEzI,CAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAA,EAA/B,wBAA+B;gBACjC,IAAI,IAAI;oBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,+CAAwC,GAAG,CAAC,QAAQ,SAAM,CAAC,CAAC;gBAEpF,UAAU,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,IAAI,CAAC,UAAC,UAAU,IAAK,OAAA,UAAU,CAAC,UAAU,KAAK,cAAc,EAAxC,CAAwC,CAAC,CAAC;qBAEpF,UAAU,EAAV,wBAAU;;;;gBAEV,qBAAM,CAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,UAAU,CAAC,QAAQ,CAAC,0CAAE,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA,EAAA;;gBAAtD,SAAsD,CAAC;gBACvD,IAAI,IAAI;oBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,gEAAoD,GAAG,CAAC,QAAQ,OAAI,CAAC,CAAC;;;;gBAEpG,MAAM,IAAI,iBAAQ,CAAC,qDAA8C,GAAG,CAAC,QAAQ,gBAAM,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAA;;;;KAIjH,CAAA;AAhCY,QAAA,gBAAgB,oBAgC5B"}
@@ -0,0 +1,2 @@
1
+ import type { CollectionAfterDeleteHook } from 'payload/types';
2
+ export declare const deleteStripeCustomer: CollectionAfterDeleteHook;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.deleteStripeCustomer = void 0;
43
+ var stripe_1 = __importDefault(require("stripe"));
44
+ var stripeSecretKey = process.env.STRIPE_SECRET_KEY;
45
+ var stripe = new stripe_1.default(stripeSecretKey || '', { apiVersion: '2022-08-01' });
46
+ var deleteStripeCustomer = function (_a) {
47
+ var req = _a.req, doc = _a.doc;
48
+ return __awaiter(void 0, void 0, void 0, function () {
49
+ var payload, error_1;
50
+ return __generator(this, function (_b) {
51
+ switch (_b.label) {
52
+ case 0:
53
+ payload = req.payload;
54
+ if (!(process.env.NODE_ENV !== 'test')) return [3 /*break*/, 4];
55
+ console.log("Deleting customer with ID: '".concat(doc.stripeCustomerID, "' from Stripe."));
56
+ _b.label = 1;
57
+ case 1:
58
+ _b.trys.push([1, 3, , 4]);
59
+ return [4 /*yield*/, stripe.customers.del(doc.stripeCustomerID)];
60
+ case 2:
61
+ _b.sent();
62
+ console.log("Successfully deleted customer ID: '".concat(doc.stripeCustomerID, "' from Stripe."));
63
+ return [3 /*break*/, 4];
64
+ case 3:
65
+ error_1 = _b.sent();
66
+ payload.logger.error("Error deleting customer ID: '".concat(doc.stripeCustomerID, "' from Stripe: ").concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
67
+ return [3 /*break*/, 4];
68
+ case 4: return [2 /*return*/];
69
+ }
70
+ });
71
+ });
72
+ };
73
+ exports.deleteStripeCustomer = deleteStripeCustomer;
74
+ //# sourceMappingURL=deleteStripeCustomer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteStripeCustomer.js","sourceRoot":"","sources":["../../src/hooks/deleteStripeCustomer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kDAA4B;AAE5B,IAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AACtD,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAC;AAExE,IAAM,oBAAoB,GAA8B,UAAO,EAAY;QAAV,GAAG,SAAA,EAAE,GAAG,SAAA;;;;;;oBACtE,OAAO,GAAK,GAAG,QAAR,CAAS;yBAEpB,CAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAA,EAA/B,wBAA+B;oBACjC,OAAO,CAAC,GAAG,CAAC,sCAA+B,GAAG,CAAC,gBAAgB,mBAAgB,CAAC,CAAC;;;;oBAG/E,qBAAM,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC,EAAA;;oBAAhD,SAAgD,CAAC;oBAEjD,OAAO,CAAC,GAAG,CAAC,6CAAsC,GAAG,CAAC,gBAAgB,mBAAgB,CAAC,CAAC;;;;oBAExF,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,uCAAgC,GAAG,CAAC,gBAAgB,4BAAkB,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAC;;;;;;CAGxH,CAAA;AAdY,QAAA,oBAAoB,wBAchC"}
@@ -0,0 +1,2 @@
1
+ import type { CollectionAfterChangeHook } from 'payload/types';
2
+ export declare const syncExistingStripeCustomer: CollectionAfterChangeHook;