@payloadcms/plugin-stripe 0.0.3 → 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.
- package/README.md +117 -15
- package/dist/extendWebpackConfig.d.ts +3 -0
- package/dist/extendWebpackConfig.js +31 -0
- package/dist/extendWebpackConfig.js.map +1 -0
- package/dist/hooks/createNewInStripe.d.ts +7 -0
- package/dist/hooks/createNewInStripe.js +125 -0
- package/dist/hooks/createNewInStripe.js.map +1 -0
- package/dist/hooks/createNewStripeCustomer.d.ts +2 -0
- package/dist/hooks/createNewStripeCustomer.js +104 -0
- package/dist/hooks/createNewStripeCustomer.js.map +1 -0
- package/dist/hooks/deleteFromStripe.d.ts +7 -0
- package/dist/hooks/deleteFromStripe.js +81 -0
- package/dist/hooks/deleteFromStripe.js.map +1 -0
- package/dist/hooks/deleteStripeCustomer.d.ts +2 -0
- package/dist/hooks/deleteStripeCustomer.js +74 -0
- package/dist/hooks/deleteStripeCustomer.js.map +1 -0
- package/dist/hooks/syncExistingStripeCustomer.d.ts +2 -0
- package/dist/hooks/syncExistingStripeCustomer.js +80 -0
- package/dist/hooks/syncExistingStripeCustomer.js.map +1 -0
- package/dist/hooks/syncExistingWithStripe.d.ts +7 -0
- package/dist/hooks/syncExistingWithStripe.js +96 -0
- package/dist/hooks/syncExistingWithStripe.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +131 -20
- package/dist/index.js.map +1 -1
- package/dist/mocks/serverModule.d.ts +2 -0
- package/dist/mocks/serverModule.js +6 -0
- package/dist/mocks/serverModule.js.map +1 -0
- package/dist/routes/rest.d.ts +6 -1
- package/dist/routes/rest.js +3 -2
- package/dist/routes/rest.js.map +1 -1
- package/dist/routes/webhooks.d.ts +8 -1
- package/dist/routes/webhooks.js +99 -22
- package/dist/routes/webhooks.js.map +1 -1
- package/dist/types.d.ts +29 -5
- package/dist/utilities/stripeProxy.js +7 -1
- package/dist/utilities/stripeProxy.js.map +1 -1
- package/dist/webhooks/handleCreatedOrUpdated.d.ts +7 -0
- package/dist/webhooks/handleCreatedOrUpdated.js +194 -0
- package/dist/webhooks/handleCreatedOrUpdated.js.map +1 -0
- package/dist/webhooks/handleDeleted.d.ts +7 -0
- package/dist/webhooks/handleDeleted.js +106 -0
- package/dist/webhooks/handleDeleted.js.map +1 -0
- package/dist/webhooks/handleWebhooks.d.ts +2 -0
- package/dist/webhooks/handleWebhooks.js +236 -0
- package/dist/webhooks/handleWebhooks.js.map +1 -0
- package/dist/webhooks/index.d.ts +2 -0
- package/dist/webhooks/index.js +94 -0
- package/dist/webhooks/index.js.map +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@payloadcms/plugin-stripe)
|
|
4
4
|
|
|
5
|
-
A plugin for [Payload CMS](https://github.com/payloadcms/payload) to
|
|
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
|
-
-
|
|
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
|
|
|
@@ -24,11 +26,11 @@ In the `plugins` array of your [Payload config](https://payloadcms.com/docs/conf
|
|
|
24
26
|
|
|
25
27
|
```js
|
|
26
28
|
import { buildConfig } from 'payload/config';
|
|
27
|
-
import
|
|
29
|
+
import stripePlugin from '@payloadcms/plugin-stripe';
|
|
28
30
|
|
|
29
31
|
const config = buildConfig({
|
|
30
32
|
plugins: [
|
|
31
|
-
|
|
33
|
+
stripePlugin({
|
|
32
34
|
stripeSecretKey: process.env.STRIPE_SECRET_KEY,
|
|
33
35
|
})
|
|
34
36
|
]
|
|
@@ -43,17 +45,75 @@ export default config;
|
|
|
43
45
|
|
|
44
46
|
Required. Your Stripe secret key.
|
|
45
47
|
|
|
46
|
-
- `
|
|
48
|
+
- `sync`
|
|
47
49
|
|
|
48
|
-
Optional.
|
|
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
|
+
|
|
52
|
+
- `stripeWebhooksEndpointSecret`
|
|
53
|
+
|
|
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.
|
|
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
|
-
|
|
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,31 +138,52 @@ One core functionality of this plugin is to enable a two-way communication chann
|
|
|
78
138
|
})
|
|
79
139
|
```
|
|
80
140
|
|
|
81
|
-
- `POST /
|
|
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
|
|
|
85
145
|
### Webhooks
|
|
86
146
|
|
|
87
|
-
[Stripe webhooks](https://stripe.com/docs/webhooks) are used to sync from Stripe to Payload. Webhooks listen for events on your Stripe account so you can trigger reactions to them.
|
|
147
|
+
[Stripe webhooks](https://stripe.com/docs/webhooks) are used to sync from Stripe to Payload. Webhooks listen for events on your Stripe account so you can trigger reactions to them. Follow the steps below to enable webhooks.
|
|
148
|
+
|
|
149
|
+
Development:
|
|
150
|
+
1. Login using Stripe cli `stripe login`
|
|
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`
|
|
88
153
|
|
|
154
|
+
Production:
|
|
89
155
|
1. Login and [create a new webhook](https://dashboard.stripe.com/test/webhooks/create) from the Stripe dashboard
|
|
90
|
-
1. Paste
|
|
156
|
+
1. Paste `YOUR_DOMAIN_NAME/api/stripe/webhooks` as the "Webhook Endpoint URL"
|
|
91
157
|
1. Select which events to broadcast
|
|
158
|
+
1. Paste the given secret into your `.env` file as `STRIPE_WEBHOOKS_ENDPOINT_SECRET`
|
|
92
159
|
1. Then, handle these events using the `webhooks` portion of this plugin's config:
|
|
93
160
|
|
|
94
161
|
```js
|
|
95
162
|
import { buildConfig } from 'payload/config';
|
|
96
|
-
import
|
|
163
|
+
import stripePlugin from '@payloadcms/plugin-stripe';
|
|
97
164
|
|
|
98
165
|
const config = buildConfig({
|
|
99
166
|
plugins: [
|
|
100
|
-
|
|
167
|
+
stripePlugin({
|
|
101
168
|
stripeSecretKey: process.env.STRIPE_SECRET_KEY,
|
|
102
169
|
stripeWebhooksEndpointSecret: process.env.STRIPE_WEBHOOKS_ENDPOINT_SECRET,
|
|
103
170
|
webhooks: {
|
|
104
|
-
'customer.subscription.updated': () => {
|
|
171
|
+
'customer.subscription.updated': ({ event, stripe, stripeConfig }) => {
|
|
172
|
+
// DO SOMETHING
|
|
173
|
+
}
|
|
105
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
|
+
// }
|
|
106
187
|
})
|
|
107
188
|
]
|
|
108
189
|
});
|
|
@@ -114,7 +195,28 @@ For a full list of available webhooks, see [here](https://stripe.com/docs/cli/tr
|
|
|
114
195
|
|
|
115
196
|
### Node
|
|
116
197
|
|
|
117
|
-
|
|
198
|
+
On the server you should interface with Stripe directly using the [stripe](https://www.npmjs.com/package/stripe) npm module. That might look something like this:
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
import Stripe from 'stripe';
|
|
202
|
+
|
|
203
|
+
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
|
|
204
|
+
const stripe = new Stripe(stripeSecretKey, { apiVersion: '2022-08-01' });
|
|
205
|
+
|
|
206
|
+
export const MyFunction = async () => {
|
|
207
|
+
try {
|
|
208
|
+
const customer = await stripe.customers.create({
|
|
209
|
+
email: data.email,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// DO SOMETHING
|
|
213
|
+
} catch (error) {
|
|
214
|
+
console.error(error.message);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Alternatively, you can interface with the Stripe using the `stripeProxy`, which is exactly what the `/api/stripe/rest` endpoint does behind-the-scenes. Here's the same example as above, but piped through the proxy:
|
|
118
220
|
|
|
119
221
|
```js
|
|
120
222
|
import { stripeProxy } from '@payloadcms/plugin-stripe';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.extendWebpackConfig = void 0;
|
|
18
|
+
var path_1 = __importDefault(require("path"));
|
|
19
|
+
var mockModulePath = path_1.default.resolve(__dirname, 'mocks/serverModule.js');
|
|
20
|
+
var extendWebpackConfig = function (config) {
|
|
21
|
+
return function (webpackConfig) {
|
|
22
|
+
var _a;
|
|
23
|
+
var _b, _c;
|
|
24
|
+
var existingWebpackConfig = typeof ((_b = config.admin) === null || _b === void 0 ? void 0 : _b.webpack) === 'function'
|
|
25
|
+
? config.admin.webpack(webpackConfig)
|
|
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, "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
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.extendWebpackConfig = extendWebpackConfig;
|
|
31
|
+
//# sourceMappingURL=extendWebpackConfig.js.map
|
|
@@ -0,0 +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,EACxB,SAAS,EAAE,cAAc,OACxB,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,eAAe,CAAC,IAAG,cAAc,KACzD,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,mBAAmB,CAAC,IAAG,cAAc,KAC7D,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,2BAA2B,CAAC,IAAG,cAAc,KACrE,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,2BAA2B,CAAC,IAAG,cAAc,KACrE,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,0BAA0B,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,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AAC,IAAI,CAAC,0BAAmB,UAAU,CAAC,QAAQ,4BAAyB,CAAC,CAAC;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,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,G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,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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