@payloadcms/plugin-stripe 0.0.19 → 3.0.0-beta.23

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 (70) hide show
  1. package/README.md +3 -285
  2. package/dist/admin.d.ts +4 -0
  3. package/dist/admin.d.ts.map +1 -0
  4. package/dist/admin.js +34 -0
  5. package/dist/admin.js.map +1 -0
  6. package/dist/fields/getFields.d.ts +12 -0
  7. package/dist/fields/getFields.d.ts.map +1 -0
  8. package/dist/fields/getFields.js +46 -0
  9. package/dist/fields/getFields.js.map +1 -0
  10. package/dist/hooks/createNewInStripe.d.ts +7 -2
  11. package/dist/hooks/createNewInStripe.d.ts.map +1 -0
  12. package/dist/hooks/createNewInStripe.js +70 -124
  13. package/dist/hooks/createNewInStripe.js.map +1 -1
  14. package/dist/hooks/deleteFromStripe.d.ts +7 -2
  15. package/dist/hooks/deleteFromStripe.d.ts.map +1 -0
  16. package/dist/hooks/deleteFromStripe.js +30 -87
  17. package/dist/hooks/deleteFromStripe.js.map +1 -1
  18. package/dist/hooks/syncExistingWithStripe.d.ts +7 -2
  19. package/dist/hooks/syncExistingWithStripe.d.ts.map +1 -0
  20. package/dist/hooks/syncExistingWithStripe.js +42 -94
  21. package/dist/hooks/syncExistingWithStripe.js.map +1 -1
  22. package/dist/index.d.ts +5 -3
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +88 -166
  25. package/dist/index.js.map +1 -1
  26. package/dist/routes/rest.d.ts +4 -6
  27. package/dist/routes/rest.d.ts.map +1 -0
  28. package/dist/routes/rest.js +33 -76
  29. package/dist/routes/rest.js.map +1 -1
  30. package/dist/routes/webhooks.d.ts +4 -6
  31. package/dist/routes/webhooks.d.ts.map +1 -0
  32. package/dist/routes/webhooks.js +49 -94
  33. package/dist/routes/webhooks.js.map +1 -1
  34. package/dist/types.d.ts +138 -11
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/types.js +2 -2
  37. package/dist/types.js.map +1 -1
  38. package/dist/ui/LinkToDoc.d.ts +4 -7
  39. package/dist/ui/LinkToDoc.d.ts.map +1 -0
  40. package/dist/ui/LinkToDoc.js +29 -31
  41. package/dist/ui/LinkToDoc.js.map +1 -1
  42. package/dist/utilities/deepen.d.ts +1 -0
  43. package/dist/utilities/deepen.d.ts.map +1 -0
  44. package/dist/utilities/deepen.js +9 -16
  45. package/dist/utilities/deepen.js.map +1 -1
  46. package/dist/utilities/stripeProxy.d.ts +2 -1
  47. package/dist/utilities/stripeProxy.d.ts.map +1 -0
  48. package/dist/utilities/stripeProxy.js +37 -88
  49. package/dist/utilities/stripeProxy.js.map +1 -1
  50. package/dist/webhooks/handleCreatedOrUpdated.d.ts +3 -2
  51. package/dist/webhooks/handleCreatedOrUpdated.d.ts.map +1 -0
  52. package/dist/webhooks/handleCreatedOrUpdated.js +115 -196
  53. package/dist/webhooks/handleCreatedOrUpdated.js.map +1 -1
  54. package/dist/webhooks/handleDeleted.d.ts +2 -1
  55. package/dist/webhooks/handleDeleted.d.ts.map +1 -0
  56. package/dist/webhooks/handleDeleted.js +44 -102
  57. package/dist/webhooks/handleDeleted.js.map +1 -1
  58. package/dist/webhooks/index.d.ts +2 -1
  59. package/dist/webhooks/index.d.ts.map +1 -0
  60. package/dist/webhooks/index.js +46 -90
  61. package/dist/webhooks/index.js.map +1 -1
  62. package/package.json +50 -45
  63. package/types.d.ts +1 -1
  64. package/types.js +1 -1
  65. package/dist/extendWebpackConfig.d.ts +0 -3
  66. package/dist/extendWebpackConfig.js +0 -31
  67. package/dist/extendWebpackConfig.js.map +0 -1
  68. package/dist/mocks/serverModule.d.ts +0 -2
  69. package/dist/mocks/serverModule.js +0 -6
  70. package/dist/mocks/serverModule.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"createNewInStripe.js","sourceRoot":"","sources":["../../src/hooks/createNewInStripe.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCA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C,4CAAqC,GAAG,CAAE,CAAC,CAAA;;;qBAK5E,CAAA,SAAS,KAAK,QAAQ,CAAA,EAAtB,wBAAsB;gBACxB,IAAI,IAAI;oBACN,OAAO,CAAC,MAAM,CAAC,IAAI,CACjB,iBAAU,gBAAc,yDAA+C,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,EAAE,8BAA2B,CAC3G,CAAA;;;;gBAGD,IAAI,IAAI;oBACN,OAAO,CAAC,MAAM,CAAC,IAAI,CACjB,0BAAmB,UAAU,CAAC,kBAAkB,4BAAyB,CAC1E,CAAA;gBAGoB,qBAAM,CAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,UAAU,CAAC,kBAAkB,CAAC,0CAAE,MAAM,CAC1E,YAAmB,CACpB,CAAA,EAAA;;gBAFK,cAAc,GAAG,SAEtB;gBAED,IAAI,IAAI;oBACN,OAAO,CAAC,MAAM,CAAC,IAAI,CACjB,2CAA+B,UAAU,CAAC,kBAAkB,4CAAkC,cAAc,CAAC,EAAE,OAAI,CACpH,CAAA;gBAEH,OAAO,CAAC,QAAQ,GAAG,cAAc,CAAC,EAAE,CAAA;gBAEpC,+DAA+D;gBAC/D,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAA;;;;gBAEjB,GAAG,GAAG,OAAK,YAAY,KAAK,CAAC,CAAC,CAAC,OAAK,CAAC,OAAO,CAAC,CAAC,CAAC,OAAK,CAAA;gBAC1D,MAAM,IAAI,iBAAQ,CAChB,gCAAyB,UAAU,CAAC,kBAAkB,mCAAyB,GAAG,CAAE,CACrF,CAAA;oBAOX,sBAAO,OAAO,EAAA;;;KACf,CAAA;AAzGY,QAAA,iBAAiB,qBAyG7B"}
1
+ {"version":3,"sources":["../../src/hooks/createNewInStripe.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload/types'\n\nimport { APIError } from 'payload/errors'\nimport Stripe from 'stripe'\n\nimport type { StripeConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = Omit<\n Parameters<CollectionBeforeValidateHook>[0],\n 'collection'\n> & {\n collection: CollectionConfig\n}\n\nexport type CollectionBeforeValidateHookWithArgs = (\n args: HookArgsWithCustomCollection & {\n collection?: CollectionConfig\n stripeConfig?: StripeConfig\n },\n) => void\n\nexport const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (args) => {\n const { collection, data, operation, req, stripeConfig } = args\n\n const { logs, sync } = stripeConfig || {}\n\n const payload = req?.payload\n\n const dataRef = data || {}\n\n if (process.env.NODE_ENV === 'test') {\n dataRef.stripeID = 'test'\n return dataRef\n }\n\n if (payload) {\n if (data?.skipSync) {\n if (logs) payload.logger.info(`Bypassing collection-level hooks.`)\n } else {\n // initialize as 'false' so that all Payload admin events sync to Stripe\n // then conditionally set to 'true' to for events that originate from webhooks\n // this will prevent webhook events from triggering an unnecessary sync / infinite loop\n dataRef.skipSync = false\n\n const { slug: collectionSlug } = collection || {}\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n // combine all fields of this object and match their respective values within the document\n let syncedFields = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[stripeProperty] = dataRef[fieldPath]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedFields = deepen(syncedFields)\n\n if (operation === 'update') {\n if (logs)\n payload.logger.info(\n `A '${collectionSlug}' document has changed in Payload with ID: '${data?.id}', syncing with Stripe...`,\n )\n\n // NOTE: the Stripe document will be created in the \"afterChange\" hook, so create a new stripe document here if no stripeID exists\n if (!dataRef.stripeID) {\n try {\n // NOTE: Typed as \"any\" because the \"create\" method is not standard across all Stripe resources\n const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(\n // @ts-expect-error\n syncedFields,\n )\n\n if (logs)\n payload.logger.info(\n `✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`,\n )\n\n dataRef.stripeID = stripeResource.id\n\n // NOTE: this is to prevent sync in the \"afterChange\" hook\n dataRef.skipSync = true\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) payload.logger.error(`- Error creating Stripe document: ${msg}`)\n }\n }\n }\n\n if (operation === 'create') {\n if (logs)\n payload.logger.info(\n `A new '${collectionSlug}' document was created in Payload with ID: '${data?.id}', syncing with Stripe...`,\n )\n\n try {\n if (logs)\n payload.logger.info(\n `- Creating new '${syncConfig.stripeResourceType}' resource in Stripe...`,\n )\n\n // NOTE: Typed as \"any\" because the \"create\" method is not standard across all Stripe resources\n const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(\n // @ts-expect-error\n syncedFields,\n )\n\n if (logs)\n payload.logger.info(\n `✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`,\n )\n\n dataRef.stripeID = stripeResource.id\n\n // IMPORTANT: this is to prevent sync in the \"afterChange\" hook\n dataRef.skipSync = true\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(\n `Failed to create new '${syncConfig.stripeResourceType}' resource in Stripe: ${msg}`,\n )\n }\n }\n }\n }\n }\n\n return dataRef\n}\n"],"names":["APIError","Stripe","deepen","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","createNewInStripe","args","collection","data","operation","req","stripeConfig","logs","sync","payload","dataRef","NODE_ENV","stripeID","skipSync","logger","info","slug","collectionSlug","syncConfig","find","conf","syncedFields","fields","reduce","acc","field","fieldPath","stripeProperty","id","stripeResource","stripeResourceType","create","error","msg","Error","message"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,QAAQ,QA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lB,QAAQE,QAAQ,GAAGiB,eAAeD,EAAE;wBAEpC,+DAA+D;wBAC/DlB,QAAQG,QAAQ,GAAG;oBACrB,EAAE,OAAOmB,OAAgB;wBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;wBACrD,MAAM,IAAIzC,SACR,CAAC,sBAAsB,EAAE2B,WAAWY,kBAAkB,CAAC,sBAAsB,EAAEG,IAAI,CAAC;oBAExF;gBACF;YACF;QACF;IACF;IAEA,OAAOvB;AACT,EAAC"}
@@ -1,7 +1,12 @@
1
1
  import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload/types';
2
- import type { StripeConfig } from '../types';
3
- export type CollectionAfterDeleteHookWithArgs = (args: Parameters<CollectionAfterDeleteHook>[0] & {
2
+ import type { StripeConfig } from '../types.js';
3
+ type HookArgsWithCustomCollection = Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'> & {
4
+ collection: CollectionConfig;
5
+ };
6
+ export type CollectionAfterDeleteHookWithArgs = (args: HookArgsWithCustomCollection & {
4
7
  collection?: CollectionConfig;
5
8
  stripeConfig?: StripeConfig;
6
9
  }) => void;
7
10
  export declare const deleteFromStripe: CollectionAfterDeleteHookWithArgs;
11
+ export {};
12
+ //# sourceMappingURL=deleteFromStripe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deleteFromStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/deleteFromStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAKhF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAM/C,KAAK,4BAA4B,GAAG,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG;IACjG,UAAU,EAAE,gBAAgB,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,iCAAiC,GAAG,CAC9C,IAAI,EAAE,4BAA4B,GAAG;IACnC,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAA;CAC5B,KACE,IAAI,CAAA;AAET,eAAO,MAAM,gBAAgB,EAAE,iCAwC9B,CAAA"}
@@ -1,91 +1,34 @@
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 (g && (g = 0, op[0] && (_ = 0)), _) 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;
1
+ import { APIError } from 'payload/errors';
2
+ import Stripe from 'stripe';
3
+ const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
4
+ // api version can only be the latest, stripe recommends ts ignoring it
5
+ const stripe = new Stripe(stripeSecretKey || '', {
6
+ apiVersion: '2022-08-01'
7
+ });
8
+ export const deleteFromStripe = async (args)=>{
9
+ const { collection, doc, req, stripeConfig } = args;
10
+ const { logs, sync } = stripeConfig || {};
11
+ const { payload } = req;
12
+ const { slug: collectionSlug } = collection || {};
13
+ if (logs) payload.logger.info(`Document with ID: '${doc?.id}' in collection: '${collectionSlug}' has been deleted, deleting from Stripe...`);
14
+ if (process.env.NODE_ENV !== 'test') {
15
+ if (logs) payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`);
16
+ const syncConfig = sync?.find((conf)=>conf.collection === collectionSlug);
17
+ if (syncConfig) {
18
+ try {
19
+ const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID);
20
+ if (found) {
21
+ await stripe?.[syncConfig.stripeResourceType]?.del(doc.stripeID);
22
+ if (logs) payload.logger.info(`✅ Successfully deleted Stripe document with ID: '${doc.stripeID}'.`);
23
+ } else {
24
+ if (logs) payload.logger.info(`- Stripe document with ID: '${doc.stripeID}' not found, skipping...`);
25
+ }
26
+ } catch (error) {
27
+ const msg = error instanceof Error ? error.message : error;
28
+ throw new APIError(`Failed to delete Stripe document with ID: '${doc.stripeID}': ${msg}`);
32
29
  }
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 };
30
+ }
36
31
  }
37
32
  };
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 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 deleteFromStripe = function (args) { return __awaiter(void 0, void 0, void 0, function () {
48
- var req, doc, stripeConfig, collection, _a, logs, sync, payload, collectionSlug, syncConfig, found, error_1, msg;
49
- var _b, _c;
50
- return __generator(this, function (_d) {
51
- switch (_d.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*/, 7];
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 (conf) { return conf.collection === collectionSlug; });
63
- if (!syncConfig) return [3 /*break*/, 7];
64
- _d.label = 1;
65
- case 1:
66
- _d.trys.push([1, 6, , 7]);
67
- return [4 /*yield*/, ((_b = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig.stripeResourceType]) === null || _b === void 0 ? void 0 : _b.retrieve(doc.stripeID))];
68
- case 2:
69
- found = _d.sent();
70
- if (!found) return [3 /*break*/, 4];
71
- return [4 /*yield*/, ((_c = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig.stripeResourceType]) === null || _c === void 0 ? void 0 : _c.del(doc.stripeID))];
72
- case 3:
73
- _d.sent();
74
- if (logs)
75
- payload.logger.info("\u2705 Successfully deleted Stripe document with ID: '".concat(doc.stripeID, "'."));
76
- return [3 /*break*/, 5];
77
- case 4:
78
- if (logs)
79
- payload.logger.info("- Stripe document with ID: '".concat(doc.stripeID, "' not found, skipping..."));
80
- _d.label = 5;
81
- case 5: return [3 /*break*/, 7];
82
- case 6:
83
- error_1 = _d.sent();
84
- msg = error_1 instanceof Error ? error_1.message : error_1;
85
- throw new errors_1.APIError("Failed to delete Stripe document with ID: '".concat(doc.stripeID, "': ").concat(msg));
86
- case 7: return [2 /*return*/];
87
- }
88
- });
89
- }); };
90
- exports.deleteFromStripe = deleteFromStripe;
33
+
91
34
  //# sourceMappingURL=deleteFromStripe.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"deleteFromStripe.js","sourceRoot":"","sources":["../../src/hooks/deleteFromStripe.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAyC;AAEzC,kDAA2B;AAI3B,IAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAA;AACrD,IAAM,MAAM,GAAG,IAAI,gBAAM,CAAC,eAAe,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAA;AASvE,IAAM,gBAAgB,GAAsC,UAAM,IAAI;;;;;;gBACnE,GAAG,GAAoC,IAAI,IAAxC,EAAE,GAAG,GAA+B,IAAI,IAAnC,EAAE,YAAY,GAAiB,IAAI,aAArB,EAAE,UAAU,GAAK,IAAI,WAAT,CAAS;gBAE7C,KAAiB,YAAY,IAAI,EAAE,EAAjC,IAAI,UAAA,EAAE,IAAI,UAAA,CAAuB;gBAEjC,OAAO,GAAK,GAAG,QAAR,CAAQ;gBACT,cAAc,GAAK,CAAA,UAAU,IAAI,EAAE,CAAA,KAArB,CAAqB;gBAEjD,IAAI,IAAI;oBACN,OAAO,CAAC,MAAM,CAAC,IAAI,CACjB,6BA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AE,CAAC,CAAA;;;;KAIhG,CAAA;AAxCY,QAAA,gBAAgB,oBAwC5B"}
1
+ {"version":3,"sources":["../../src/hooks/deleteFromStripe.ts"],"sourcesContent":["import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload/types'\n\nimport { APIError } from 'payload/errors'\nimport Stripe from 'stripe'\n\nimport type { StripeConfig } from '../types.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'> & {\n collection: CollectionConfig\n}\n\nexport type CollectionAfterDeleteHookWithArgs = (\n args: HookArgsWithCustomCollection & {\n collection?: CollectionConfig\n stripeConfig?: StripeConfig\n },\n) => void\n\nexport const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args) => {\n const { collection, doc, req, stripeConfig } = args\n\n const { logs, sync } = stripeConfig || {}\n\n const { payload } = req\n const { slug: collectionSlug } = collection || {}\n\n if (logs)\n payload.logger.info(\n `Document with ID: '${doc?.id}' in collection: '${collectionSlug}' has been deleted, deleting from Stripe...`,\n )\n\n if (process.env.NODE_ENV !== 'test') {\n if (logs) payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`)\n\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n try {\n const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID)\n\n if (found) {\n await stripe?.[syncConfig.stripeResourceType]?.del(doc.stripeID)\n if (logs)\n payload.logger.info(\n `✅ Successfully deleted Stripe document with ID: '${doc.stripeID}'.`,\n )\n } else {\n if (logs)\n payload.logger.info(\n `- Stripe document with ID: '${doc.stripeID}' not found, skipping...`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(`Failed to delete Stripe document with ID: '${doc.stripeID}': ${msg}`)\n }\n }\n }\n}\n"],"names":["APIError","Stripe","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","deleteFromStripe","args","collection","doc","req","stripeConfig","logs","sync","payload","slug","collectionSlug","logger","info","id","NODE_ENV","stripeID","syncConfig","find","conf","found","stripeResourceType","retrieve","del","error","msg","Error","message"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,OAAOC,YAAY,SAAQ;AAI3B,MAAMC,kBAAkBC,QAAQC,GAAG,CAACC,iBAAiB;AACrD,uEAAuE;AACvE,MAAMC,SAAS,IAAIL,OAAOC,mBAAmB,IAAI;IAAEK,YAAY;AAAa;AAa5E,OAAO,MAAMC,mBAAsD,OAAOC;IACxE,MAAM,EAAEC,UAAU,EAAEC,GAAG,EAAEC,GAAG,EAAEC,YAAY,EAAE,GAAGJ;IAE/C,MAAM,EAAEK,IAAI,EAAEC,IAAI,EAAE,GAAGF,gBAAgB,CAAC;IAExC,MAAM,EAAEG,OAAO,EAAE,GAAGJ;IACpB,MAAM,EAAEK,MAAMC,cAAc,EAAE,GAAGR,cAAc,CAAC;IAEhD,IAAII,MACFE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,mBAAmB,EAAET,KAAKU,GAAG,kBAAkB,EAAEH,eAAe,2CAA2C,CAAC;IAGjH,IAAIf,QAAQC,GAAG,CAACkB,QAAQ,KAAK,QAAQ;QACnC,IAAIR,MAAME,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,qCAAqC,EAAET,IAAIY,QAAQ,CAAC,IAAI,CAAC;QAExF,MAAMC,aAAaT,MAAMU,KAAK,CAACC,OAASA,KAAKhB,UAAU,KAAKQ;QAE5D,IAAIM,YAAY;YACd,IAAI;gBACF,MAAMG,QAAQ,MAAMrB,QAAQ,CAACkB,WAAWI,kBAAkB,CAAC,EAAEC,SAASlB,IAAIY,QAAQ;gBAElF,IAAII,OAAO;oBACT,MAAMrB,QAAQ,CAACkB,WAAWI,kBAAkB,CAAC,EAAEE,IAAInB,IAAIY,QAAQ;oBAC/D,IAAIT,MACFE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,iDAAiD,EAAET,IAAIY,QAAQ,CAAC,EAAE,CAAC;gBAE1E,OAAO;oBACL,IAAIT,MACFE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,4BAA4B,EAAET,IAAIY,QAAQ,CAAC,wBAAwB,CAAC;gBAE3E;YACF,EAAE,OAAOQ,OAAgB;gBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;gBACrD,MAAM,IAAI/B,SAAS,CAAC,2CAA2C,EAAEW,IAAIY,QAAQ,CAAC,GAAG,EAAES,IAAI,CAAC;YAC1F;QACF;IACF;AACF,EAAC"}
@@ -1,7 +1,12 @@
1
1
  import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types';
2
- import type { StripeConfig } from '../types';
3
- export type CollectionBeforeChangeHookWithArgs = (args: Parameters<CollectionBeforeChangeHook>[0] & {
2
+ import type { StripeConfig } from '../types.js';
3
+ type HookArgsWithCustomCollection = Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'> & {
4
+ collection: CollectionConfig;
5
+ };
6
+ export type CollectionBeforeChangeHookWithArgs = (args: HookArgsWithCustomCollection & {
4
7
  collection?: CollectionConfig;
5
8
  stripeConfig?: StripeConfig;
6
9
  }) => void;
7
10
  export declare const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs;
11
+ export {};
12
+ //# sourceMappingURL=syncExistingWithStripe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syncExistingWithStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/syncExistingWithStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAKjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAQ/C,KAAK,4BAA4B,GAAG,IAAI,CACtC,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,EACzC,YAAY,CACb,GAAG;IACF,UAAU,EAAE,gBAAgB,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,kCAAkC,GAAG,CAC/C,IAAI,EAAE,4BAA4B,GAAG;IACnC,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAA;CAC5B,KACE,IAAI,CAAA;AAET,eAAO,MAAM,sBAAsB,EAAE,kCA8DpC,CAAA"}
@@ -1,99 +1,47 @@
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 (g && (g = 0, op[0] && (_ = 0)), _) 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 errors_1 = require("payload/errors");
44
- var stripe_1 = __importDefault(require("stripe"));
45
- var deepen_1 = require("../utilities/deepen");
46
- var stripeSecretKey = process.env.STRIPE_SECRET_KEY;
47
- var stripe = new stripe_1.default(stripeSecretKey || '', { apiVersion: '2022-08-01' });
48
- var syncExistingWithStripe = function (args) { return __awaiter(void 0, void 0, void 0, function () {
49
- var req, operation, data, originalDoc, collection, stripeConfig, _a, logs, sync, payload, collectionSlug, syncConfig, syncedFields, stripeResource, error_1, msg;
50
- var _b;
51
- return __generator(this, function (_c) {
52
- switch (_c.label) {
53
- case 0:
54
- req = args.req, operation = args.operation, data = args.data, originalDoc = args.originalDoc, collection = args.collection, stripeConfig = args.stripeConfig;
55
- _a = stripeConfig || {}, logs = _a.logs, sync = _a.sync;
56
- payload = req.payload;
57
- collectionSlug = (collection || {}).slug;
58
- if (!(process.env.NODE_ENV !== 'test' && !data.skipSync)) return [3 /*break*/, 5];
59
- syncConfig = sync === null || sync === void 0 ? void 0 : sync.find(function (conf) { return conf.collection === collectionSlug; });
60
- if (!syncConfig) return [3 /*break*/, 5];
61
- if (!(operation === 'update')) return [3 /*break*/, 5];
62
- syncedFields = syncConfig.fields.reduce(function (acc, field) {
63
- var fieldPath = field.fieldPath, stripeProperty = field.stripeProperty;
1
+ import { APIError } from 'payload/errors';
2
+ import Stripe from 'stripe';
3
+ import { deepen } from '../utilities/deepen.js';
4
+ const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
5
+ // api version can only be the latest, stripe recommends ts ignoring it
6
+ const stripe = new Stripe(stripeSecretKey || '', {
7
+ apiVersion: '2022-08-01'
8
+ });
9
+ export const syncExistingWithStripe = async (args)=>{
10
+ const { collection, data, operation, originalDoc, req, stripeConfig } = args;
11
+ const { logs, sync } = stripeConfig || {};
12
+ const { payload } = req;
13
+ const { slug: collectionSlug } = collection || {};
14
+ if (process.env.NODE_ENV !== 'test' && !data.skipSync) {
15
+ const syncConfig = sync?.find((conf)=>conf.collection === collectionSlug);
16
+ if (syncConfig) {
17
+ if (operation === 'update') {
18
+ // combine all fields of this object and match their respective values within the document
19
+ let syncedFields = syncConfig.fields.reduce((acc, field)=>{
20
+ const { fieldPath, stripeProperty } = field;
64
21
  acc[stripeProperty] = data[fieldPath];
65
22
  return acc;
66
23
  }, {});
67
- syncedFields = (0, deepen_1.deepen)(syncedFields);
68
- if (logs)
69
- payload.logger.info("A '".concat(collectionSlug, "' document has changed in Payload with ID: '").concat(originalDoc === null || originalDoc === void 0 ? void 0 : originalDoc._id, "', syncing with Stripe..."));
70
- if (!!data.stripeID) return [3 /*break*/, 1];
71
- // NOTE: the "beforeValidate" hook populates this
72
- if (logs)
73
- payload.logger.error("- There is no Stripe ID for this document, skipping.");
74
- return [3 /*break*/, 5];
75
- case 1:
76
- if (logs)
77
- payload.logger.info("- Syncing to Stripe resource with ID: '".concat(data.stripeID, "'..."));
78
- _c.label = 2;
79
- case 2:
80
- _c.trys.push([2, 4, , 5]);
81
- return [4 /*yield*/, ((_b = stripe === null || stripe === void 0 ? void 0 : stripe[syncConfig === null || syncConfig === void 0 ? void 0 : syncConfig.stripeResourceType]) === null || _b === void 0 ? void 0 : _b.update(data.stripeID, syncedFields))];
82
- case 3:
83
- stripeResource = _c.sent();
84
- if (logs)
85
- payload.logger.info("\u2705 Successfully synced Stripe resource with ID: '".concat(stripeResource.id, "'."));
86
- return [3 /*break*/, 5];
87
- case 4:
88
- error_1 = _c.sent();
89
- msg = error_1 instanceof Error ? error_1.message : error_1;
90
- throw new errors_1.APIError("Failed to sync document with ID: '".concat(data.id, "' to Stripe: ").concat(msg));
91
- case 5:
92
- // Set back to 'false' so that all changes continue to sync to Stripe, see note in './createNewInStripe.ts'
93
- data.skipSync = false;
94
- return [2 /*return*/, data];
24
+ syncedFields = deepen(syncedFields);
25
+ if (logs) payload.logger.info(`A '${collectionSlug}' document has changed in Payload with ID: '${originalDoc?._id}', syncing with Stripe...`);
26
+ if (!data.stripeID) {
27
+ // NOTE: the "beforeValidate" hook populates this
28
+ if (logs) payload.logger.error(`- There is no Stripe ID for this document, skipping.`);
29
+ } else {
30
+ if (logs) payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`);
31
+ try {
32
+ const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(data.stripeID, syncedFields);
33
+ if (logs) payload.logger.info(`✅ Successfully synced Stripe resource with ID: '${stripeResource.id}'.`);
34
+ } catch (error) {
35
+ const msg = error instanceof Error ? error.message : error;
36
+ throw new APIError(`Failed to sync document with ID: '${data.id}' to Stripe: ${msg}`);
37
+ }
38
+ }
39
+ }
95
40
  }
96
- });
97
- }); };
98
- exports.syncExistingWithStripe = syncExistingWithStripe;
41
+ }
42
+ // Set back to 'false' so that all changes continue to sync to Stripe, see note in './createNewInStripe.ts'
43
+ data.skipSync = false;
44
+ return data;
45
+ };
46
+
99
47
  //# sourceMappingURL=syncExistingWithStripe.js.map
@@ -1 +1 @@
1
- {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AAO,IAAI,EAAA;;;KACZ,CAAA;AA3DY,QAAA,sBAAsB,0BA2DlC"}
1
+ {"version":3,"sources":["../../src/hooks/syncExistingWithStripe.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types'\n\nimport { APIError } from 'payload/errors'\nimport Stripe from 'stripe'\n\nimport type { StripeConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = Omit<\n Parameters<CollectionBeforeChangeHook>[0],\n 'collection'\n> & {\n collection: CollectionConfig\n}\n\nexport type CollectionBeforeChangeHookWithArgs = (\n args: HookArgsWithCustomCollection & {\n collection?: CollectionConfig\n stripeConfig?: StripeConfig\n },\n) => void\n\nexport const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async (args) => {\n const { collection, data, operation, originalDoc, req, stripeConfig } = args\n\n const { logs, sync } = stripeConfig || {}\n\n const { payload } = req\n\n const { slug: collectionSlug } = collection || {}\n\n if (process.env.NODE_ENV !== 'test' && !data.skipSync) {\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n if (operation === 'update') {\n // combine all fields of this object and match their respective values within the document\n let syncedFields = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[stripeProperty] = data[fieldPath]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedFields = deepen(syncedFields)\n\n if (logs)\n payload.logger.info(\n `A '${collectionSlug}' document has changed in Payload with ID: '${originalDoc?._id}', syncing with Stripe...`,\n )\n\n if (!data.stripeID) {\n // NOTE: the \"beforeValidate\" hook populates this\n if (logs) payload.logger.error(`- There is no Stripe ID for this document, skipping.`)\n } else {\n if (logs)\n payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`)\n\n try {\n const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(\n data.stripeID,\n syncedFields,\n )\n\n if (logs)\n payload.logger.info(\n `✅ Successfully synced Stripe resource with ID: '${stripeResource.id}'.`,\n )\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(`Failed to sync document with ID: '${data.id}' to Stripe: ${msg}`)\n }\n }\n }\n }\n }\n\n // Set back to 'false' so that all changes continue to sync to Stripe, see note in './createNewInStripe.ts'\n data.skipSync = false\n\n return data\n}\n"],"names":["APIError","Stripe","deepen","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","syncExistingWithStripe","args","collection","data","operation","originalDoc","req","stripeConfig","logs","sync","payload","slug","collectionSlug","NODE_ENV","skipSync","syncConfig","find","conf","syncedFields","fields","reduce","acc","field","fieldPath","stripeProperty","logger","info","_id","stripeID","error","stripeResource","stripeResourceType","update","id","msg","Error","message"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,OAAOC,YAAY,SAAQ;AAI3B,SAASC,MAAM,QAAQ,yBAAwB;AAE/C,MAAMC,kBAAkBC,QAAQC,GAAG,CAACC,iBAAiB;AACrD,uEAAuE;AACvE,MAAMC,SAAS,IAAIN,OAAOE,mBAAmB,IAAI;IAAEK,YAAY;AAAa;AAgB5E,OAAO,MAAMC,yBAA6D,OAAOC;IAC/E,MAAM,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,WAAW,EAAEC,GAAG,EAAEC,YAAY,EAAE,GAAGN;IAExE,MAAM,EAAEO,IAAI,EAAEC,IAAI,EAAE,GAAGF,gBAAgB,CAAC;IAExC,MAAM,EAAEG,OAAO,EAAE,GAAGJ;IAEpB,MAAM,EAAEK,MAAMC,cAAc,EAAE,GAAGV,cAAc,CAAC;IAEhD,IAAIP,QAAQC,GAAG,CAACiB,QAAQ,K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Q,GAAG;IAEhB,OAAOX;AACT,EAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { Config } from 'payload/config';
2
- import type { StripeConfig } from './types';
3
- declare const stripePlugin: (incomingStripeConfig: StripeConfig) => (config: Config) => Config;
4
- export default stripePlugin;
2
+ import type { StripeConfig } from './types.js';
3
+ export { LinkToDoc } from './ui/LinkToDoc.js';
4
+ export { stripeProxy } from './utilities/stripeProxy.js';
5
+ export declare const stripePlugin: (incomingStripeConfig: StripeConfig) => (config: Config) => Config;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,gBAAgB,CAAA;AAEtD,OAAO,KAAK,EAAyB,YAAY,EAAE,MAAM,YAAY,CAAA;AASrE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAExD,eAAO,MAAM,YAAY,yBACA,YAAY,cAC1B,MAAM,KAAG,MAoGjB,CAAA"}