@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
@@ -0,0 +1,80 @@
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.syncExistingStripeCustomer = 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 syncExistingStripeCustomer = function (_a) {
47
+ var req = _a.req, operation = _a.operation, doc = _a.doc;
48
+ return __awaiter(void 0, void 0, void 0, function () {
49
+ var payload, customer, 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' && !doc.isSyncedToStripe)) return [3 /*break*/, 4];
55
+ if (!(operation === 'update')) return [3 /*break*/, 4];
56
+ console.log("Syncing Payload customer with ID: '".concat(doc.id, "' to Stripe."));
57
+ _b.label = 1;
58
+ case 1:
59
+ _b.trys.push([1, 3, , 4]);
60
+ return [4 /*yield*/, stripe.customers.update(doc.stripeCustomerID, {
61
+ email: doc.email,
62
+ name: doc.name,
63
+ })];
64
+ case 2:
65
+ customer = _b.sent();
66
+ // Set to false so that all Payload events sync to Stripe, EXCEPT for those that originate from webhooks
67
+ doc.isSyncedToStripe = false;
68
+ console.log("Successfully updated customer ID: '".concat(customer.id, "' in Stripe."));
69
+ return [3 /*break*/, 4];
70
+ case 3:
71
+ error_1 = _b.sent();
72
+ payload.logger.error("Error updating customer ID: '".concat(doc.stripeCustomerID, "' in Stripe: ").concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
73
+ return [3 /*break*/, 4];
74
+ case 4: return [2 /*return*/, doc];
75
+ }
76
+ });
77
+ });
78
+ };
79
+ exports.syncExistingStripeCustomer = syncExistingStripeCustomer;
80
+ //# sourceMappingURL=syncExistingStripeCustomer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syncExistingStripeCustomer.js","sourceRoot":"","sources":["../../src/hooks/syncExistingStripeCustomer.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,0BAA0B,GAA8B,UAAO,EAAuB;QAArB,GAAG,SAAA,EAAE,SAAS,eAAA,EAAE,GAAG,SAAA;;;;;;oBACvF,OAAO,GAAK,GAAG,QAAR,CAAS;yBAEpB,CAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAA,EAAxD,wBAAwD;yBACtD,CAAA,SAAS,KAAK,QAAQ,CAAA,EAAtB,wBAAsB;oBACxB,OAAO,CAAC,GAAG,CAAC,6CAAsC,GAAG,CAAC,EAAE,iBAAc,CAAC,CAAC;;;;oBAGrD,qBAAM,MAAM,CAAC,SAAS,CAAC,MAAM,CAC5C,GAAG,CAAC,gBAAgB,EACpB;4BACE,KAAK,EAAE,GAAG,CAAC,KAAK;4BAChB,IAAI,EAAE,GAAG,CAAC,IAAI;yBACf,CACF,EAAA;;oBANK,QAAQ,GAAG,SAMhB;oBAED,wGAAwG;oBACxG,GAAG,CAAC,gBAAgB,GAAG,KAAK,CAAC;oBAE7B,OAAO,CAAC,GAAG,CAAC,6CAAsC,QAAQ,CAAC,EAAE,iBAAc,CAAC,CAAC;;;;oBAE7E,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,uCAAgC,GAAG,CAAC,gBAAgB,0BAAgB,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAC;;wBAKvH,sBAAO,GAAG,EAAC;;;;CACZ,CAAA;AA3BY,QAAA,0BAA0B,8BA2BtC"}
@@ -0,0 +1,7 @@
1
+ import type { CollectionAfterChangeHook, CollectionConfig } from 'payload/types';
2
+ import { StripeConfig } from '../types';
3
+ export declare type CollectionAfterChangeHookWithArgs = (args: Parameters<CollectionAfterChangeHook>[0] & {
4
+ collection?: CollectionConfig;
5
+ stripeConfig?: StripeConfig;
6
+ }) => void;
7
+ export declare const syncExistingWithStripe: CollectionAfterChangeHookWithArgs;
@@ -0,0 +1,96 @@
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.syncExistingWithStripe = 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 syncExistingWithStripe = function (args) { return __awaiter(void 0, void 0, void 0, function () {
48
+ var req, operation, doc, collection, stripeConfig, _a, logs, sync, payload, collectionSlug, syncConfig, syncedFields, stripeResource, error_1;
49
+ var _b;
50
+ return __generator(this, function (_c) {
51
+ switch (_c.label) {
52
+ case 0:
53
+ req = args.req, operation = args.operation, doc = args.doc, collection = args.collection, stripeConfig = args.stripeConfig;
54
+ _a = stripeConfig || {}, logs = _a.logs, sync = _a.sync;
55
+ payload = req.payload;
56
+ collectionSlug = (collection || {}).slug;
57
+ if (!(process.env.NODE_ENV !== 'test' && !doc.skipSync)) return [3 /*break*/, 5];
58
+ syncConfig = sync === null || sync === void 0 ? void 0 : sync.find(function (syncConfig) { return syncConfig.collection === collectionSlug; });
59
+ if (!syncConfig) return [3 /*break*/, 5];
60
+ syncedFields = syncConfig.fields.reduce(function (acc, field) {
61
+ var fieldName = field.field, property = field.property;
62
+ acc[fieldName] = doc[property];
63
+ return acc;
64
+ }, {});
65
+ if (!(operation === 'update')) return [3 /*break*/, 5];
66
+ if (logs)
67
+ payload.logger.info("A '".concat(collectionSlug, "' document has changed in Payload with ID: '").concat(doc === null || doc === void 0 ? void 0 : doc.id, "', syncing with Stripe..."));
68
+ if (!!doc.stripeID) return [3 /*break*/, 1];
69
+ // NOTE: the "beforeValidate" hook populates this
70
+ if (logs)
71
+ payload.logger.error("- There is no Stripe ID for this document, skipping.");
72
+ return [3 /*break*/, 5];
73
+ case 1:
74
+ if (logs)
75
+ payload.logger.info("- Syncing to Stripe ID: '".concat(doc.stripeID, "'..."));
76
+ _c.label = 2;
77
+ case 2:
78
+ _c.trys.push([2, 4, , 5]);
79
+ return [4 /*yield*/, ((_b = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig === null || syncConfig === void 0 ? void 0 : syncConfig.resource]) === null || _b === void 0 ? void 0 : _b.update(doc.stripeID, syncedFields))];
80
+ case 3:
81
+ stripeResource = _c.sent();
82
+ if (logs)
83
+ payload.logger.info("\u2705 Successfully synced Stripe document ID: '".concat(stripeResource.id, "'."));
84
+ return [3 /*break*/, 5];
85
+ case 4:
86
+ error_1 = _c.sent();
87
+ throw new errors_1.APIError("Failed to sync document with ID: '".concat(doc.id, "' to Stripe: ").concat((error_1 === null || error_1 === void 0 ? void 0 : error_1.message) || ''));
88
+ case 5:
89
+ // Set back to false so that all changes continue to sync to Stripe, see note in './createNewInStripe.ts'
90
+ doc.skipSync = false;
91
+ return [2 /*return*/, doc];
92
+ }
93
+ });
94
+ }); };
95
+ exports.syncExistingWithStripe = syncExistingWithStripe;
96
+ //# sourceMappingURL=syncExistingWithStripe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syncExistingWithStripe.js","sourceRoot":"","sources":["../../src/hooks/syncExistingWithStripe.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,sBAAsB,GAAsC,UAAO,IAAI;;;;;;gBAEhF,GAAG,GAKD,IAAI,IALH,EACH,SAAS,GAIP,IAAI,UAJG,EACT,GAAG,GAGD,IAAI,IAHH,EACH,UAAU,GAER,IAAI,WAFI,EACV,YAAY,GACV,IAAI,aADM,CACL;gBAEH,KAGF,YAAY,IAAI,EAAE,EAFpB,IAAI,UAAA,EACJ,IAAI,UAAA,CACiB;gBAEf,OAA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,QAAQ,CAAC,0CAAE,MAAM,CACjE,GAAG,CAAC,QAAQ,EACZ,YAAY,CACb,CAAA,EAAA;;gBAHK,cAAc,GAAG,SAGtB;gBAED,IAAI,IAAI;oBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,0DAA8C,cAAc,CAAC,EAAE,OAAI,CAAC,CAAC;;;;gBAEnG,MAAM,IAAI,iBAAQ,CAAC,4CAAqC,GAAG,CAAC,EAAE,0BAAgB,CAAA,OAAK,aAAL,OAAK,uBAAL,OAAK,CAAE,OAAO,KAAI,EAAE,CAAE,CAAC,CAAC;;gBAOhH,yGAAyG;gBACzG,GAAG,CAAC,QAAQ,GAAG,KAAK,CAAC;gBAErB,sBAAO,GAAG,EAAC;;;KACZ,CAAA;AAzDY,QAAA,sBAAsB,0BAyDlC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Config } from 'payload/config';
2
2
  import { StripeConfig } from './types';
3
- declare const stripePlugin: (stripeConfig: StripeConfig) => (config: Config) => Config;
3
+ declare const stripePlugin: (incomingStripeConfig: StripeConfig) => (config: Config) => Config;
4
4
  export default stripePlugin;
package/dist/index.js CHANGED
@@ -10,6 +10,42 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
13
49
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
50
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
51
  if (ar || !(i in from)) {
@@ -19,27 +55,104 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
55
  }
20
56
  return to.concat(ar || Array.prototype.slice.call(from));
21
57
  };
58
+ var __importDefault = (this && this.__importDefault) || function (mod) {
59
+ return (mod && mod.__esModule) ? mod : { "default": mod };
60
+ };
22
61
  Object.defineProperty(exports, "__esModule", { value: true });
23
62
  var rest_1 = require("./routes/rest");
24
63
  var webhooks_1 = require("./routes/webhooks");
64
+ var express_1 = __importDefault(require("express"));
25
65
  var extendWebpackConfig_1 = require("./extendWebpackConfig");
26
- var stripePlugin = function (stripeConfig) { return function (config) {
27
- return (__assign(__assign({}, config), { admin: __assign(__assign({}, config.admin), { webpack: (0, extendWebpackConfig_1.extendWebpackConfig)(config) }), endpoints: __spreadArray(__spreadArray([], (config === null || config === void 0 ? void 0 : config.endpoints) || [], true), [
28
- {
29
- path: '/stripe/webhooks',
30
- method: 'post',
31
- handler: function (req, res, next) {
32
- (0, webhooks_1.stripeWebhooks)(req, res, next, stripeConfig);
66
+ var createNewInStripe_1 = require("./hooks/createNewInStripe");
67
+ var syncExistingWithStripe_1 = require("./hooks/syncExistingWithStripe");
68
+ var deleteFromStripe_1 = require("./hooks/deleteFromStripe");
69
+ var stripePlugin = function (incomingStripeConfig) { return function (config) {
70
+ var collections = config.collections;
71
+ if (incomingStripeConfig === null || incomingStripeConfig === void 0 ? void 0 : incomingStripeConfig.stripeSecretKey) {
72
+ // set config defaults here
73
+ var stripeConfig_1 = __assign(__assign({}, incomingStripeConfig), { sync: (incomingStripeConfig === null || incomingStripeConfig === void 0 ? void 0 : incomingStripeConfig.sync) || [] });
74
+ return (__assign(__assign({}, config), { admin: __assign(__assign({}, config.admin), { webpack: (0, extendWebpackConfig_1.extendWebpackConfig)(config) }), endpoints: __spreadArray(__spreadArray([], (config === null || config === void 0 ? void 0 : config.endpoints) || [], true), [
75
+ {
76
+ path: '/stripe/webhooks',
77
+ method: 'post',
78
+ root: true,
79
+ handler: [
80
+ express_1.default.raw({ type: 'application/json' }),
81
+ function (req, res, next) {
82
+ (0, webhooks_1.stripeWebhooks)({
83
+ req: req,
84
+ res: res,
85
+ next: next,
86
+ config: config,
87
+ stripeConfig: stripeConfig_1
88
+ });
89
+ }
90
+ ]
91
+ },
92
+ {
93
+ path: '/stripe/rest',
94
+ method: 'post',
95
+ handler: function (req, res, next) {
96
+ (0, rest_1.stripeREST)({
97
+ req: req,
98
+ res: res,
99
+ next: next,
100
+ stripeConfig: stripeConfig_1
101
+ });
102
+ }
103
+ },
104
+ ], false), collections: collections === null || collections === void 0 ? void 0 : collections.map(function (collection) {
105
+ var _a;
106
+ var existingHooks = collection.hooks;
107
+ var syncConfig = (_a = stripeConfig_1.sync) === null || _a === void 0 ? void 0 : _a.find(function (sync) { return sync.collection === collection.slug; });
108
+ if (syncConfig) {
109
+ return __assign(__assign({}, collection), { hooks: __assign(__assign({}, collection.hooks), { beforeValidate: __spreadArray(__spreadArray([], ((existingHooks === null || existingHooks === void 0 ? void 0 : existingHooks.beforeValidate) || []), true), [
110
+ function (args) { return __awaiter(void 0, void 0, void 0, function () {
111
+ return __generator(this, function (_a) {
112
+ return [2 /*return*/, (0, createNewInStripe_1.createNewInStripe)(__assign(__assign({}, args), { collection: collection, stripeConfig: stripeConfig_1 }))];
113
+ });
114
+ }); }
115
+ ], false), afterChange: __spreadArray(__spreadArray([], ((existingHooks === null || existingHooks === void 0 ? void 0 : existingHooks.afterChange) || []), true), [
116
+ function (args) { return __awaiter(void 0, void 0, void 0, function () {
117
+ return __generator(this, function (_a) {
118
+ return [2 /*return*/, (0, syncExistingWithStripe_1.syncExistingWithStripe)(__assign(__assign({}, args), { collection: collection, stripeConfig: stripeConfig_1 }))];
119
+ });
120
+ }); }
121
+ ], false), afterDelete: __spreadArray(__spreadArray([], ((existingHooks === null || existingHooks === void 0 ? void 0 : existingHooks.afterDelete) || []), true), [
122
+ function (args) { return __awaiter(void 0, void 0, void 0, function () {
123
+ return __generator(this, function (_a) {
124
+ return [2 /*return*/, (0, deleteFromStripe_1.deleteFromStripe)(__assign(__assign({}, args), { collection: collection, stripeConfig: stripeConfig_1 }))];
125
+ });
126
+ }); }
127
+ ], false) }), fields: __spreadArray(__spreadArray([], collection.fields, true), [
128
+ {
129
+ name: 'stripeID',
130
+ label: 'Stripe ID',
131
+ type: 'text',
132
+ saveToJWT: true,
133
+ admin: {
134
+ position: 'sidebar',
135
+ readOnly: true,
136
+ },
137
+ },
138
+ {
139
+ name: 'skipSync',
140
+ label: 'Synced To Sync',
141
+ type: 'checkbox',
142
+ admin: {
143
+ position: 'sidebar',
144
+ readOnly: true,
145
+ },
146
+ }
147
+ ], false) });
33
148
  }
34
- },
35
- {
36
- path: '/stripe/rest',
37
- method: 'post',
38
- handler: function (req, res, next) {
39
- (0, rest_1.stripeREST)(req, res, next, stripeConfig);
40
- }
41
- },
42
- ], false) }));
149
+ return collection;
150
+ }) }));
151
+ }
152
+ else {
153
+ console.error("Stripe plugin: No 'stripeSecretKey' provided. Plugin not enabled.");
154
+ }
155
+ return config;
43
156
  }; };
44
157
  exports.default = stripePlugin;
45
158
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,sCAA2C;AAC3C,8CAAmD;AAEnD,6DAA4D;AAE5D,IAAM,YAAY,GAAG,UAAC,YAA0B,IAAK,OAAA,UAAC,MAAc;IAClE,OAAO,uBACF,MAAM,KACT,KAAK,wBACA,MAAM,CAAC,KAAK,KACf,OAAO,EAAE,IAAA,yCAAmB,EAAC,MAAM,CAAC,KAEtC,SAAS,kCACJ,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,SAAS,KAAI,EAAE;YAC1B;gBACE,IAAI,EAAE,kBAAkB;gBACxB,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,UAAC,GAAG,EAAE,GAAG,EAAE,IAAI;oBACtB,IAAA,yBAAc,EAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;gBAC9C,CAAC;aACF;YACD;gBACE,IAAI,EAAE,cAAc;gBACpB,MAAM,EAAE,MAAM;gBACd,OAAO,E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,CAAC;gCACrC,UAAO,IAAI;;wCAAK,sBAAA,IAAA,mCAAgB,wBAC3B,IAAI,KACP,UAAU,YAAA,EACV,YAAY,gBAAA,IACZ,EAAA;;qCAAA;0CAGN,MAAM,kCACD,UAAU,CAAC,MAAM;4BACpB;gCACE,IAAI,EAAE,UAAU;gCAChB,KAAK,EAAE,WAAW;gCAClB,IAAI,EAAE,MAAM;gCACZ,SAAS,EAAE,IAAI;gCACf,KAAK,EAAE;oCACL,QAAQ,EAAE,SAAS;oCACnB,QAAQ,EAAE,IAAI;iCACf;6BACF;4BACD;gCACE,IAAI,EAAE,UAAU;gCAChB,KAAK,EAAE,gBAAgB;gCACvB,IAAI,EAAE,UAAU;gCAChB,KAAK,EAAE;oCACL,QAAQ,EAAE,SAAS;oCACnB,QAAQ,EAAE,IAAI;iCACf;6BACF;qCAEH;iBACH;gBAED,OAAO,UAAU,CAAC;YACpB,CAAC,CAAC,IACF,CAAA;KACH;SAAM;QACL,OAAO,CAAC,KAAK,CAAC,mEAAmE,CAAC,CAAC;KACpF;IAED,OAAO,MAAM,CAAA;AACf,CAAC,EAtH4D,CAsH5D,CAAC;AAEF,kBAAe,YAAY,CAAC"}
@@ -1 +1,2 @@
1
- export {};
1
+ export function url(): void;
2
+ export function raw(): void;
@@ -1,3 +1,6 @@
1
1
  "use strict";
2
- module.exports = {};
2
+ module.exports = {
3
+ url: function () { },
4
+ raw: function () { },
5
+ };
3
6
  //# sourceMappingURL=serverModule.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"serverModule.js","sourceRoot":"","sources":["../../src/mocks/serverModule.js"],"names":[],"mappings":";AAAA,MAAM,CAAC,OAAO,GAAG,EAAE,CAAA"}
1
+ {"version":3,"file":"serverModule.js","sourceRoot":"","sources":["../../src/mocks/serverModule.js"],"names":[],"mappings":";AAAA,MAAM,CAAC,OAAO,GAAG;IACf,GAAG,EAAE,cAAQ,CAAC;IACd,GAAG,EAAE,cAAQ,CAAC;CACf,CAAA"}
@@ -1,4 +1,9 @@
1
1
  import { PayloadRequest } from 'payload/types';
2
2
  import { StripeConfig } from '../types';
3
3
  import { Response } from 'express';
4
- export declare const stripeREST: (req: PayloadRequest, res: Response, next: any, stripeConfig: StripeConfig) => Promise<Response<any, Record<string, any>> | undefined>;
4
+ export declare const stripeREST: (args: {
5
+ req: PayloadRequest;
6
+ res: Response;
7
+ next: any;
8
+ stripeConfig: StripeConfig;
9
+ }) => Promise<Response<any, Record<string, any>> | undefined>;
@@ -39,12 +39,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.stripeREST = void 0;
40
40
  var errors_1 = require("payload/errors");
41
41
  var stripeProxy_1 = require("../utilities/stripeProxy");
42
- var stripeREST = function (req, res, next, stripeConfig) { return __awaiter(void 0, void 0, void 0, function () {
43
- var payload, user, _a, stripeMethod, stripeArgs // example: 'cus_MGgt3Tuj3D66f2'
42
+ var stripeREST = function (args) { return __awaiter(void 0, void 0, void 0, function () {
43
+ var req, res, next, stripeConfig, payload, user, _a, stripeMethod, stripeArgs // example: 'cus_MGgt3Tuj3D66f2'
44
44
  , stripeSecretKey, pluginRes, status_1, error_1, message;
45
45
  return __generator(this, function (_b) {
46
46
  switch (_b.label) {
47
47
  case 0:
48
+ req = args.req, res = args.res, next = args.next, stripeConfig = args.stripeConfig;
48
49
  payload = req.payload, user = req.user, _a = req.body, stripeMethod = _a.stripeMethod, stripeArgs = _a.stripeArgs;
49
50
  stripeSecretKey = stripeConfig.stripeSecretKey;
50
51
  _b.label = 1;
@@ -1 +1 @@
1
- {"version":3,"file":"rest.js","sourceRoot":"","sources":["../../src/routes/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,yCAA2C;AAG3C,wDAAuD;AAEhD,IAAM,UAAU,GAAG,UACxB,GAAmB,EACnB,GAAa,EACb,IAAS,EACT,YAA0B;;;;;;gBAGxB,OAAO,GAML,GAAG,QANE,EACP,IAAI,GAKF,GAAG,KALD,EACJ,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,EAAE,EAAE,+CAA+C;oBAC1D,MAAM,IAAI,kBAAS,EAAE,CAAC;iBACvB;gBAEiB,qBAAM,IAAA,yBAAW,EAAC;wBAClC,eAAe,iBAAA;wBACf,YAAY,cAAA;wBACZ,UAAU,YAAA,CAAC,gCAAgC;qBAC5C,CAAC,EAAA;;gBAJI,SAAS,GAAG,SAIhB;gBAGA,WACE,SAAS,OADL,CACM;gBAEd,GAAG,CAAC,MAAM,CAAC,QAAM,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;;;;gBAG7B,OAAO,GAAG,oEAA6D,OAAK,MAAG,CAAC;gBACtF,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBAC9B,sBAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;wBAC1B,OAAO,SAAA;qBACR,CAAC,EAAC;;;;KAEN,CAAC;AAhDW,QAAA,UAAU,cAgDrB"}
@@ -1,4 +1,11 @@
1
1
  import { PayloadRequest } from 'payload/dist/types';
2
2
  import { StripeConfig } from '../types';
3
3
  import { Response } from 'express';
4
- export declare const stripeWebhooks: (req: PayloadRequest, res: Response, next: any, stripeConfig: StripeConfig) => void;
4
+ import { Config as PayloadConfig } from 'payload/config';
5
+ export declare const stripeWebhooks: (args: {
6
+ req: PayloadRequest;
7
+ res: Response;
8
+ next: any;
9
+ config: PayloadConfig;
10
+ stripeConfig: StripeConfig;
11
+ }) => Promise<void>;
@@ -1,33 +1,110 @@
1
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
+ };
2
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
40
  };
5
41
  Object.defineProperty(exports, "__esModule", { value: true });
6
42
  exports.stripeWebhooks = void 0;
7
43
  var stripe_1 = __importDefault(require("stripe"));
8
- var stripeWebhooks = function (req, res, next, stripeConfig) {
9
- var stripeSecretKey = stripeConfig.stripeSecretKey, stripeWebhooksEndpointSecret = stripeConfig.stripeWebhooksEndpointSecret, webhooks = stripeConfig.webhooks;
10
- if (webhooks && stripeWebhooksEndpointSecret) {
11
- var stripe = new stripe_1.default(stripeSecretKey, { apiVersion: '2022-08-01' });
12
- var stripeSignature = req.headers['stripe-signature'];
13
- if (stripeSignature) {
14
- var event_1;
15
- try {
16
- event_1 = stripe.webhooks.constructEvent(req.body, stripeSignature, stripeWebhooksEndpointSecret);
17
- }
18
- catch (err) {
19
- res.status(400).send("Webhook Error: ".concat(err.message));
20
- }
21
- if (event_1) {
22
- var webhookEventHandler = webhooks[event_1.type];
23
- if (typeof webhookEventHandler === 'function') {
24
- webhookEventHandler(event_1, stripe, stripeConfig);
44
+ var webhooks_1 = require("../webhooks");
45
+ var stripeWebhooks = function (args) { return __awaiter(void 0, void 0, void 0, function () {
46
+ var req, res, next, config, stripeConfig, stripeSecretKey, stripeWebhooksEndpointSecret, webhooks, stripe, stripeSignature, event_1, webhookEventHandler;
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0:
50
+ req = args.req, res = args.res, next = args.next, config = args.config, stripeConfig = args.stripeConfig;
51
+ stripeSecretKey = stripeConfig.stripeSecretKey, stripeWebhooksEndpointSecret = stripeConfig.stripeWebhooksEndpointSecret, webhooks = stripeConfig.webhooks;
52
+ if (!stripeWebhooksEndpointSecret) return [3 /*break*/, 2];
53
+ stripe = new stripe_1.default(stripeSecretKey, {
54
+ apiVersion: '2022-08-01',
55
+ appInfo: {
56
+ name: 'Stripe Payload Plugin',
57
+ url: 'https://payloadcms.com',
58
+ }
59
+ });
60
+ stripeSignature = req.headers['stripe-signature'];
61
+ if (!stripeSignature) return [3 /*break*/, 2];
62
+ try {
63
+ event_1 = stripe.webhooks.constructEvent(req.body, stripeSignature, stripeWebhooksEndpointSecret);
25
64
  }
26
- ;
27
- }
65
+ catch (err) {
66
+ req.payload.logger.error((err === null || err === void 0 ? void 0 : err.message) || 'Error constructing Stripe event');
67
+ res.status(400);
68
+ }
69
+ if (!event_1) return [3 /*break*/, 2];
70
+ return [4 /*yield*/, (0, webhooks_1.handleWebhooks)({
71
+ payload: req.payload,
72
+ event: event_1,
73
+ stripe: stripe,
74
+ config: config,
75
+ stripeConfig: stripeConfig
76
+ })];
77
+ case 1:
78
+ _a.sent();
79
+ // Fire external webhook handlers if they exist
80
+ if (typeof webhooks === 'function') {
81
+ webhooks({
82
+ payload: req.payload,
83
+ event: event_1,
84
+ stripe: stripe,
85
+ config: config,
86
+ stripeConfig: stripeConfig
87
+ });
88
+ }
89
+ if (typeof webhooks === 'object') {
90
+ webhookEventHandler = webhooks[event_1.type];
91
+ if (typeof webhookEventHandler === 'function') {
92
+ webhookEventHandler({
93
+ payload: req.payload,
94
+ event: event_1,
95
+ stripe: stripe,
96
+ config: config,
97
+ stripeConfig: stripeConfig
98
+ });
99
+ }
100
+ ;
101
+ }
102
+ _a.label = 2;
103
+ case 2:
104
+ res.json({ received: true });
105
+ return [2 /*return*/];
28
106
  }
29
- }
30
- res.json({ received: true });
31
- };
107
+ });
108
+ }); };
32
109
  exports.stripeWebhooks = stripeWebhooks;
33
110
  //# sourceMappingURL=webhooks.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/routes/webhooks.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4B;AAKrB,IAAM,cAAc,GAAG,UAC5B,GAAmB,EACnB,GAAa,EACb,IAAS,EACT,YAA0B;IAGxB,IAAA,eAAe,GAGb,YAAY,gBAHC,EACf,4BAA4B,GAE1B,YAAY,6BAFc,EAC5B,QAAQ,GACN,YAAY,SADN,CACO;IAEjB,IAAI,QAAQ,IAAI,4BAA4B,EAAE;QAC5C,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,EAAE,EAAE,UAAU,EAAE,YA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