@payloadcms/plugin-stripe 3.2.3-canary.673b4b5 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/createNewInStripe.js.map +1 -1
- package/dist/hooks/deleteFromStripe.js.map +1 -1
- package/dist/hooks/syncExistingWithStripe.js.map +1 -1
- package/dist/routes/webhooks.js.map +1 -1
- package/dist/ui/LinkToDoc.js.map +1 -1
- package/dist/utilities/stripeProxy.js.map +1 -1
- package/dist/webhooks/handleCreatedOrUpdated.js.map +1 -1
- package/dist/webhooks/handleDeleted.js.map +1 -1
- package/package.json +6 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/createNewInStripe.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>\n\nexport type CollectionBeforeValidateHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (args) => {\n const { collection, data, operation, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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) {\n payload.logger.info(`Bypassing collection-level hooks.`)\n }\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\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\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) {\n payload.logger.error(`- Error creating Stripe document: ${msg}`)\n }\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\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${syncConfig.stripeResourceType}' resource in Stripe...`,\n )\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\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","pluginConfig","req","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"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,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;AAa5E,OAAO,MAAMC,oBAA0D,OAAOC;IAC5E,MAAM,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAGL;IAE3D,MAAM,EAAEM,IAAI,EAAEC,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAExC,MAAMI,UAAUH,KAAKG;IAErB,MAAMC,UAAUP,QAAQ,CAAC;IAEzB,IAAIR,QAAQC,GAAG,CAACe,QAAQ,KAAK,QAAQ;QACnCD,QAAQE,QAAQ,GAAG;QACnB,OAAOF;IACT;IAEA,IAAID,SAAS;QACX,IAAIN,MAAMU,UAAU;YAClB,IAAIN,MAAM;gBACRE,QAAQK,MAAM,CAACC,IAAI,CAAC,CAAC,iCAA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AK,CAAC,CAAC,kCAAkC,EAAEC,
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/createNewInStripe.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>\n\nexport type CollectionBeforeValidateHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (args) => {\n const { collection, data, operation, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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) {\n payload.logger.info(`Bypassing collection-level hooks.`)\n }\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\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\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) {\n payload.logger.error(`- Error creating Stripe document: ${msg}`)\n }\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\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${syncConfig.stripeResourceType}' resource in Stripe...`,\n )\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\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","pluginConfig","req","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"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,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;AAa5E,OAAO,MAAMC,oBAA0D,OAAOC;IAC5E,MAAM,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAGL;IAE3D,MAAM,EAAEM,IAAI,EAAEC,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAExC,MAAMI,UAAUH,KAAKG;IAErB,MAAMC,UAAUP,QAAQ,CAAC;IAEzB,IAAIR,QAAQC,GAAG,CAACe,QAAQ,KAAK,QAAQ;QACnCD,QAAQE,QAAQ,GAAG;QACnB,OAAOF;IACT;IAEA,IAAID,SAAS;QACX,IAAIN,MAAMU,UAAU;YAClB,IAAIN,MAAM;gBACRE,QAAQK,MAAM,CAACC,IAAI,CAAC,CAAC,iCAAiC,CAAC;YACzD;QACF,OAAO;YACL,wEAAwE;YACxE,8EAA8E;YAC9E,uFAAuF;YACvFL,QAAQG,QAAQ,GAAG;YAEnB,MAAM,EAAEG,MAAMC,cAAc,EAAE,GAAGf,cAAc,CAAC;YAChD,MAAMgB,aAAaV,MAAMW,KAAK,CAACC,OAASA,KAAKlB,UAAU,KAAKe;YAE5D,IAAIC,YAAY;gBACd,0FAA0F;gBAC1F,IAAIG,eAAeH,WAAWI,MAAM,CAACC,MAAM,CACzC,CAACC,KAAKC;oBACJ,MAAM,EAAEC,SAAS,EAAEC,cAAc,EAAE,GAAGF;oBAEtCD,GAAG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MD,iBAAiB,MAAM/B,QAAQ,CAACoB,WAAWY,kBAAkB,CAAC,EAAEC,OACpE,mBAAmB;wBACnBV;wBAGF,IAAId,MAAM;4BACRE,QAAQK,MAAM,CAACC,IAAI,CACjB,CAAC,4BAA4B,EAAEG,WAAWY,kBAAkB,CAAC,+BAA+B,EAAED,eAAeD,EAAE,CAAC,EAAE,CAAC;wBAEvH;wBAEAlB,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,KAAK;oBAExF;gBACF;YACF;QACF;IACF;IAEA,OAAOvB;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/deleteFromStripe.ts"],"sourcesContent":["import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>\n\nexport type CollectionAfterDeleteHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<void>\n\nexport const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args) => {\n const { collection, doc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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\n if (process.env.NODE_ENV !== 'test') {\n if (logs) {\n payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`)\n }\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 }\n } else {\n if (logs) {\n payload.logger.info(\n `- Stripe document with ID: '${doc.stripeID}' not found, skipping...`,\n )\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","pluginConfig","req","logs","sync","payload","slug","collectionSlug","logger","info","id","NODE_ENV","stripeID","syncConfig","find","conf","found","stripeResourceType","retrieve","del","error","msg","Error","message"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,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,YAAY,EAAEC,GAAG,EAAE,GAAGJ;IAE/C,MAAM,EAAEK,IAAI,EAAEC,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAExC,MAAM,EAAEI,OAAO,EAAE,GAAGH;IACpB,MAAM,EAAEI,MAAMC,cAAc,EAAE,GAAGR,cAAc,CAAC;IAEhD,IAAII,MAAM;QACRE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,mBAAmB,EAAET,KAAKU,GAAG,kBAAkB,EAAEH,eAAe,2CAA2C,CAAC;IAEjH;IAEA,IAAIf,QAAQC,GAAG,CAACkB,QAAQ,KAAK,QAAQ;QACnC,IAAIR,MAAM;YACRE,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,qCAAqC,EAAET,IAAIY,QAAQ,CAAC,IAAI,CAAC;QAChF;QAEA,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,MAAM;wBACRE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,iDAAiD,EAAET,IAAIY,QAAQ,CAAC,EAAE,CAAC;oBAExE;gBACF,OAAO;oBACL,IAAIT,MAAM;wBACRE,QAAQG,MAAM,CAACC,IAAI,CACjB,CAAC,4BAA4B,EAAET,IAAIY,QAAQ,CAAC,wBAAwB,CAAC;oBAEzE;gBACF;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,
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/deleteFromStripe.ts"],"sourcesContent":["import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>\n\nexport type CollectionAfterDeleteHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<void>\n\nexport const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args) => {\n const { collection, doc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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\n if (process.env.NODE_ENV !== 'test') {\n if (logs) {\n payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`)\n }\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 }\n } else {\n if (logs) {\n payload.logger.info(\n `- Stripe document with ID: '${doc.stripeID}' not found, skipping...`,\n )\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","pluginConfig","req","logs","sync","payload","slug","collectionSlug","logger","info","id","NODE_ENV","stripeID","syncConfig","find","conf","found","stripeResourceType","retrieve","del","error","msg","Error","message"],"mappings":"AAEA,SAASA,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,IAAI,CACjB,CAAC,4BAA4B,EAAET,IAAIY,QAAQ,CAAC,wBAAwB,CAAC;oBAEzE;gBACF;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,KAAK;YAC1F;QACF;IACF;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/syncExistingWithStripe.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>\n\nexport type CollectionBeforeChangeHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async (args) => {\n const { collection, data, operation, originalDoc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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\n if (!data.stripeID) {\n // NOTE: the \"beforeValidate\" hook populates this\n if (logs) {\n payload.logger.error(`- There is no Stripe ID for this document, skipping.`)\n }\n } else {\n if (logs) {\n payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`)\n }\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 }\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","pluginConfig","req","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"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,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;AAa5E,OAAO,MAAMC,yBAA6D,OAAOC;IAC/E,MAAM,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,WAAW,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAG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,EAAE,CAAC;wBAE5E;oBACF,EAAE,OAAOJ,OAAgB;wBACvB,MAAMK,MAAML,iBAAiBM,QAAQN,MAAMO,OAAO,GAAGP;wBACrD,MAAM,IAAItC,SAAS,CAAC,kCAAkC,EAAEY,KAAK8B,EAAE,CAAC,aAAa,EAAEC,
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/syncExistingWithStripe.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } 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 = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>\n\nexport type CollectionBeforeChangeHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async (args) => {\n const { collection, data, operation, originalDoc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\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\n if (!data.stripeID) {\n // NOTE: the \"beforeValidate\" hook populates this\n if (logs) {\n payload.logger.error(`- There is no Stripe ID for this document, skipping.`)\n }\n } else {\n if (logs) {\n payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`)\n }\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 }\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","pluginConfig","req","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"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,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;AAa5E,OAAO,MAAMC,yBAA6D,OAAOC;IAC/E,MAAM,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,WAAW,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAGN;IAExE,MAAM,EAAEO,IAAI,EAAEC,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAExC,MAAM,EAAEI,OAAO,EAAE,GAAGH;IAEpB,MAA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M,IAAItC,SAAS,CAAC,kCAAkC,EAAEY,KAAK8B,EAAE,CAAC,aAAa,EAAEC,KAAK;oBACtF;gBACF;YACF;QACF;IACF;IAEA,2GAA2G;IAC3G/B,KAAKW,QAAQ,GAAG;IAEhB,OAAOX;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/routes/webhooks.ts"],"sourcesContent":["import type { Config as PayloadConfig, PayloadRequest } from 'payload'\n\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { handleWebhooks } from '../webhooks/index.js'\n\nexport const stripeWebhooks = async (args: {\n config: PayloadConfig\n pluginConfig: StripePluginConfig\n req: PayloadRequest\n}): Promise<any> => {\n const { config, pluginConfig, req } = args\n let returnStatus = 200\n\n const { stripeSecretKey, stripeWebhooksEndpointSecret, webhooks } = pluginConfig\n\n if (stripeWebhooksEndpointSecret) {\n const stripe = new Stripe(stripeSecretKey, {\n // api version can only be the latest, stripe recommends ts ignoring it\n apiVersion: '2022-08-01',\n appInfo: {\n name: 'Stripe Payload Plugin',\n url: 'https://payloadcms.com',\n },\n })\n\n const body = await req.text()\n const stripeSignature = req.headers.get('stripe-signature')\n\n if (stripeSignature) {\n let event: Stripe.Event | undefined\n\n try {\n event = stripe.webhooks.constructEvent(body, stripeSignature, stripeWebhooksEndpointSecret)\n } catch (err: unknown) {\n const msg: string = err instanceof Error ? err.message : JSON.stringify(err)\n req.payload.logger.error(`Error constructing Stripe event: ${msg}`)\n returnStatus = 400\n }\n\n if (event) {\n handleWebhooks({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n\n // Fire external webhook handlers if they exist\n if (typeof webhooks === 'function') {\n webhooks({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n }\n\n if (typeof webhooks === 'object') {\n const webhookEventHandler = webhooks[event.type]\n if (typeof webhookEventHandler === 'function') {\n webhookEventHandler({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n }\n }\n }\n }\n }\n\n return Response.json(\n { received: true },\n {\n status: returnStatus,\n },\n )\n}\n"],"names":["Stripe","handleWebhooks","stripeWebhooks","args","config","pluginConfig","req","returnStatus","stripeSecretKey","stripeWebhooksEndpointSecret","webhooks","stripe","apiVersion","appInfo","name","url","body","text","stripeSignature","headers","get","event","constructEvent","err","msg","Error","message","JSON","stringify","payload","logger","error","webhookEventHandler","type","Response","json","received","status"],"mappings":"AAEA,OAAOA,YAAY,SAAQ;AAI3B,SAASC,cAAc,QAAQ,uBAAsB;AAErD,OAAO,MAAMC,iBAAiB,OAAOC;IAKnC,MAAM,EAAEC,MAAM,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAGH;IACtC,IAAII,eAAe;IAEnB,MAAM,EAAEC,eAAe,EAAEC,4BAA4B,EAAEC,QAAQ,EAAE,GAAGL;IAEpE,IAAII,8BAA8B;QAChC,MAAME,SAAS,IAAIX,OAAOQ,iBAAiB;YACzC,uEAAuE;YACvEI,YAAY;YACZC,SAAS;gBACPC,MAAM;gBACNC,KAAK;YACP;QACF;QAEA,MAAMC,OAAO,MAAMV,IAAIW,IAAI;QAC3B,MAAMC,kBAAkBZ,IAAIa,OAAO,CAACC,GAAG,CAAC;QAExC,IAAIF,iBAAiB;YACnB,IAAIG;YAEJ,IAAI;gBACFA,QAAQV,OAAOD,QAAQ,CAACY,cAAc,CAACN,MAAME,iBAAiBT;YAChE,EAAE,OAAOc,KAAc;gBACrB,MAAMC,MAAcD,eAAeE,QAAQF,IAAIG,OAAO,GAAGC,KAAKC,SAAS,CAACL;gBACxEjB,IAAIuB,OAAO,CAACC,MAAM,CAACC,KAAK,CAAC,CAAC,iCAAiC,EAAEP,
|
|
1
|
+
{"version":3,"sources":["../../src/routes/webhooks.ts"],"sourcesContent":["import type { Config as PayloadConfig, PayloadRequest } from 'payload'\n\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { handleWebhooks } from '../webhooks/index.js'\n\nexport const stripeWebhooks = async (args: {\n config: PayloadConfig\n pluginConfig: StripePluginConfig\n req: PayloadRequest\n}): Promise<any> => {\n const { config, pluginConfig, req } = args\n let returnStatus = 200\n\n const { stripeSecretKey, stripeWebhooksEndpointSecret, webhooks } = pluginConfig\n\n if (stripeWebhooksEndpointSecret) {\n const stripe = new Stripe(stripeSecretKey, {\n // api version can only be the latest, stripe recommends ts ignoring it\n apiVersion: '2022-08-01',\n appInfo: {\n name: 'Stripe Payload Plugin',\n url: 'https://payloadcms.com',\n },\n })\n\n const body = await req.text()\n const stripeSignature = req.headers.get('stripe-signature')\n\n if (stripeSignature) {\n let event: Stripe.Event | undefined\n\n try {\n event = stripe.webhooks.constructEvent(body, stripeSignature, stripeWebhooksEndpointSecret)\n } catch (err: unknown) {\n const msg: string = err instanceof Error ? err.message : JSON.stringify(err)\n req.payload.logger.error(`Error constructing Stripe event: ${msg}`)\n returnStatus = 400\n }\n\n if (event) {\n handleWebhooks({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n\n // Fire external webhook handlers if they exist\n if (typeof webhooks === 'function') {\n webhooks({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n }\n\n if (typeof webhooks === 'object') {\n const webhookEventHandler = webhooks[event.type]\n if (typeof webhookEventHandler === 'function') {\n webhookEventHandler({\n config,\n event,\n payload: req.payload,\n pluginConfig,\n req,\n stripe,\n })\n }\n }\n }\n }\n }\n\n return Response.json(\n { received: true },\n {\n status: returnStatus,\n },\n )\n}\n"],"names":["Stripe","handleWebhooks","stripeWebhooks","args","config","pluginConfig","req","returnStatus","stripeSecretKey","stripeWebhooksEndpointSecret","webhooks","stripe","apiVersion","appInfo","name","url","body","text","stripeSignature","headers","get","event","constructEvent","err","msg","Error","message","JSON","stringify","payload","logger","error","webhookEventHandler","type","Response","json","received","status"],"mappings":"AAEA,OAAOA,YAAY,SAAQ;AAI3B,SAASC,cAAc,QAAQ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;gBACF;YACF;QACF;IACF;IAEA,OAAOuB,SAASC,IAAI,CAClB;QAAEC,UAAU;IAAK,GACjB;QACEC,QAAQ9B;IACV;AAEJ,EAAC"}
|
package/dist/ui/LinkToDoc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ui/LinkToDoc.tsx"],"sourcesContent":["'use client'\nimport type { UIFieldClientComponent } from 'payload'\n\nimport { CopyToClipboard, useFormFields } from '@payloadcms/ui'\nimport React from 'react'\n\nexport const LinkToDoc: UIFieldClientComponent = (props) => {\n const {\n field: { admin: { custom = {} } = {} },\n } = props\n const { isTestKey, nameOfIDField, stripeResourceType } = custom\n\n const field = useFormFields(([fields]) => (fields && fields?.[nameOfIDField]) || null)\n const { value: stripeID } = field || {}\n\n const stripeEnv = isTestKey ? 'test/' : ''\n const href = `https://dashboard.stripe.com/${stripeEnv}${stripeResourceType}/${stripeID}`\n\n if (stripeID) {\n return (\n <div>\n <div>\n <span\n className=\"label\"\n style={{\n color: '#9A9A9A',\n }}\n >\n View in Stripe\n </span>\n <CopyToClipboard value={href} />\n </div>\n <div\n style={{\n fontWeight: '600',\n overflow: 'hidden',\n textOverflow: 'ellipsis',\n }}\n >\n <a href={href} rel=\"noreferrer noopener\" target=\"_blank\">\n {href}\n </a>\n </div>\n </div>\n )\n }\n\n return null\n}\n"],"names":["CopyToClipboard","useFormFields","React","LinkToDoc","props","field","admin","custom","isTestKey","nameOfIDField","stripeResourceType","fields","value","stripeID","stripeEnv","href","div","span","className","style","color","fontWeight","overflow","textOverflow","a","rel","target"],"mappings":"AAAA;;AAGA,SAASA,eAAe,EAAEC,aAAa,QAAQ,iBAAgB;AAC/D,OAAOC,WAAW,QAAO;AAEzB,OAAO,MAAMC,YAAoC,CAACC;IAChD,MAAM,EACJC,OAAO,EAAEC,OAAO,EAAEC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,EACvC,GAAGH;IACJ,MAAM,EAAEI,SAAS,EAAEC,aAAa,EAAEC,kBAAkB,EAAE,GAAGH;IAEzD,MAAMF,QAAQJ,cAAc,CAAC,CAACU,OAAO,GAAK,AAACA,UAAUA,QAAQ,CAACF,cAAc,IAAK;IACjF,MAAM,EAAEG,OAAOC,QAAQ,EAAE,GAAGR,SAAS,CAAC;IAEtC,MAAMS,YAAYN,YAAY,UAAU;IACxC,MAAMO,OAAO,CAAC,6BAA6B,EAAED,
|
|
1
|
+
{"version":3,"sources":["../../src/ui/LinkToDoc.tsx"],"sourcesContent":["'use client'\nimport type { UIFieldClientComponent } from 'payload'\n\nimport { CopyToClipboard, useFormFields } from '@payloadcms/ui'\nimport React from 'react'\n\nexport const LinkToDoc: UIFieldClientComponent = (props) => {\n const {\n field: { admin: { custom = {} } = {} },\n } = props\n const { isTestKey, nameOfIDField, stripeResourceType } = custom\n\n const field = useFormFields(([fields]) => (fields && fields?.[nameOfIDField]) || null)\n const { value: stripeID } = field || {}\n\n const stripeEnv = isTestKey ? 'test/' : ''\n const href = `https://dashboard.stripe.com/${stripeEnv}${stripeResourceType}/${stripeID}`\n\n if (stripeID) {\n return (\n <div>\n <div>\n <span\n className=\"label\"\n style={{\n color: '#9A9A9A',\n }}\n >\n View in Stripe\n </span>\n <CopyToClipboard value={href} />\n </div>\n <div\n style={{\n fontWeight: '600',\n overflow: 'hidden',\n textOverflow: 'ellipsis',\n }}\n >\n <a href={href} rel=\"noreferrer noopener\" target=\"_blank\">\n {href}\n </a>\n </div>\n </div>\n )\n }\n\n return null\n}\n"],"names":["CopyToClipboard","useFormFields","React","LinkToDoc","props","field","admin","custom","isTestKey","nameOfIDField","stripeResourceType","fields","value","stripeID","stripeEnv","href","div","span","className","style","color","fontWeight","overflow","textOverflow","a","rel","target"],"mappings":"AAAA;;AAGA,SAASA,eAAe,EAAEC,aAAa,QAAQ,iBAAgB;AAC/D,OAAOC,WAAW,QAAO;AAEzB,OAAO,MAAMC,YAAoC,CAACC;IAChD,MAAM,EACJC,OAAO,EAAEC,OAAO,EAAEC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,EACvC,GAAGH;IACJ,MAAM,EAAEI,SAAS,EAAEC,aAAa,EAAEC,kBAAkB,EAAE,GAAGH;IAEzD,MAAMF,QAAQJ,cAAc,CAAC,CAACU,OAAO,GAAK,AAACA,UAAUA,QAAQ,CAACF,cAAc,IAAK;IACjF,MAAM,EAAEG,OAAOC,QAAQ,EAAE,GAAGR,SAAS,CAAC;IAEtC,MAAMS,YAAYN,YAAY,UAAU;IACxC,MAAMO,OAAO,CAAC,6BAA6B,EAAED,YAAYJ,mBAAmB,CAAC,EAAEG,UAAU;IAEzF,IAAIA,UAAU;QACZ,qBACE,MAACG;;8BACC,MAACA;;sCACC,KAACC;4BACCC,WAAU;4BACVC,OAAO;gCACLC,OAAO;4BACT;sCACD;;sCAGD,KAACpB;4BAAgBY,OAAOG;;;;8BAE1B,KAACC;oBACCG,OAAO;wBACLE,YAAY;wBACZC,UAAU;wBACVC,cAAc;oBAChB;8BAEA,cAAA,KAACC;wBAAET,MAAMA;wBAAMU,KAAI;wBAAsBC,QAAO;kCAC7CX;;;;;IAKX;IAEA,OAAO;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/stripeProxy.ts"],"sourcesContent":["import lodashGet from 'lodash.get'\nimport Stripe from 'stripe'\n\nimport type { StripeProxy } from '../types.js'\n\nexport const stripeProxy: StripeProxy = async ({ stripeArgs, stripeMethod, stripeSecretKey }) => {\n const stripe = new Stripe(stripeSecretKey, {\n apiVersion: '2022-08-01',\n appInfo: {\n name: 'Stripe Payload Plugin',\n url: 'https://payloadcms.com',\n },\n })\n\n if (typeof stripeMethod === 'string') {\n const topLevelMethod = stripeMethod.split('.')[0] as keyof Stripe\n const contextToBind = stripe[topLevelMethod]\n // NOTE: 'lodashGet' uses dot notation to get the property of an object\n // NOTE: Stripe API methods using reference \"this\" within their functions, so we need to bind context\n const foundMethod = lodashGet(stripe, stripeMethod).bind(contextToBind)\n\n if (typeof foundMethod === 'function') {\n if (Array.isArray(stripeArgs)) {\n try {\n const stripeResponse = await foundMethod(...stripeArgs)\n return {\n data: stripeResponse,\n status: 200,\n }\n } catch (error: unknown) {\n return {\n message: `A Stripe API error has occurred: ${error}`,\n status: 404,\n }\n }\n } else {\n throw new Error(`Argument 'stripeArgs' must be an array.`)\n }\n } else {\n throw Error(\n `The provided Stripe method of '${stripeMethod}' is not a part of the Stripe API.`,\n )\n }\n } else {\n throw Error('You must provide a Stripe method to call.')\n }\n}\n"],"names":["lodashGet","Stripe","stripeProxy","stripeArgs","stripeMethod","stripeSecretKey","stripe","apiVersion","appInfo","name","url","topLevelMethod","split","contextToBind","foundMethod","bind","Array","isArray","stripeResponse","data","status","error","message","Error"],"mappings":"AAAA,OAAOA,eAAe,aAAY;AAClC,OAAOC,YAAY,SAAQ;AAI3B,OAAO,MAAMC,cAA2B,OAAO,EAAEC,UAAU,EAAEC,YAAY,EAAEC,eAAe,EAAE;IAC1F,MAAMC,SAAS,IAAIL,OAAOI,iBAAiB;QACzCE,YAAY;QACZC,SAAS;YACPC,MAAM;YACNC,KAAK;QACP;IACF;IAEA,IAAI,OAAON,iBAAiB,UAAU;QACpC,MAAMO,iBAAiBP,aAAaQ,KAAK,CAAC,IAAI,CAAC,EAAE;QACjD,MAAMC,gBAAgBP,MAAM,CAACK,eAAe;QAC5C,uEAAuE;QACvE,qGAAqG;QACrG,MAAMG,cAAcd,UAAUM,QAAQF,cAAcW,IAAI,CAACF;QAEzD,IAAI,OAAOC,gBAAgB,YAAY;YACrC,IAAIE,MAAMC,OAAO,CAACd,aAAa;gBAC7B,IAAI;oBACF,MAAMe,iBAAiB,MAAMJ,eAAeX;oBAC5C,OAAO;wBACLgB,MAAMD;wBACNE,QAAQ;oBACV;gBACF,EAAE,OAAOC,OAAgB;oBACvB,OAAO;wBACLC,SAAS,CAAC,iCAAiC,EAAED,
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/stripeProxy.ts"],"sourcesContent":["import lodashGet from 'lodash.get'\nimport Stripe from 'stripe'\n\nimport type { StripeProxy } from '../types.js'\n\nexport const stripeProxy: StripeProxy = async ({ stripeArgs, stripeMethod, stripeSecretKey }) => {\n const stripe = new Stripe(stripeSecretKey, {\n apiVersion: '2022-08-01',\n appInfo: {\n name: 'Stripe Payload Plugin',\n url: 'https://payloadcms.com',\n },\n })\n\n if (typeof stripeMethod === 'string') {\n const topLevelMethod = stripeMethod.split('.')[0] as keyof Stripe\n const contextToBind = stripe[topLevelMethod]\n // NOTE: 'lodashGet' uses dot notation to get the property of an object\n // NOTE: Stripe API methods using reference \"this\" within their functions, so we need to bind context\n const foundMethod = lodashGet(stripe, stripeMethod).bind(contextToBind)\n\n if (typeof foundMethod === 'function') {\n if (Array.isArray(stripeArgs)) {\n try {\n const stripeResponse = await foundMethod(...stripeArgs)\n return {\n data: stripeResponse,\n status: 200,\n }\n } catch (error: unknown) {\n return {\n message: `A Stripe API error has occurred: ${error}`,\n status: 404,\n }\n }\n } else {\n throw new Error(`Argument 'stripeArgs' must be an array.`)\n }\n } else {\n throw Error(\n `The provided Stripe method of '${stripeMethod}' is not a part of the Stripe API.`,\n )\n }\n } else {\n throw Error('You must provide a Stripe method to call.')\n }\n}\n"],"names":["lodashGet","Stripe","stripeProxy","stripeArgs","stripeMethod","stripeSecretKey","stripe","apiVersion","appInfo","name","url","topLevelMethod","split","contextToBind","foundMethod","bind","Array","isArray","stripeResponse","data","status","error","message","Error"],"mappings":"AAAA,OAAOA,eAAe,aAAY;AAClC,OAAOC,YAAY,SAAQ;AAI3B,OAAO,MAAMC,cAA2B,OAAO,EAAEC,UAAU,EAAEC,YAAY,EAAEC,eAAe,EAAE;IAC1F,MAAMC,SAAS,IAAIL,OAAOI,iBAAiB;QACzCE,YAAY;QACZC,SAAS;YACPC,MAAM;YACNC,KAAK;QACP;IACF;IAEA,IAAI,OAAON,iBAAiB,UAAU;QACpC,MAAMO,iBAAiBP,aAAaQ,KAAK,CAAC,IAAI,CAAC,EAAE;QACjD,MAAMC,gBAAgBP,MAAM,CAACK,eAAe;QAC5C,uEAAuE;QACvE,qGAAqG;QACrG,MAAMG,cAAcd,UAAUM,QAAQF,cAAcW,IAAI,CAACF;QAEzD,IAAI,OAAOC,gBAAgB,YAAY;YACrC,IAAIE,MAAMC,OAAO,CAACd,aAAa;gBAC7B,IAAI;oBACF,MAAMe,iBAAiB,MAAMJ,eAAeX;oBAC5C,OAAO;wBACLgB,MAAMD;wBACNE,QAAQ;oBACV;gBACF,EAAE,OAAOC,OAAgB;oBACvB,OAAO;wBACLC,SAAS,CAAC,iCAAiC,EAAED,OAAO;wBACpDD,QAAQ;oBACV;gBACF;YACF,OAAO;gBACL,MAAM,IAAIG,MAAM,CAAC,uCAAuC,CAAC;YAC3D;QACF,OAAO;YACL,MAAMA,MACJ,CAAC,+BAA+B,EAAEnB,aAAa,kCAAkC,CAAC;QAEtF;IACF,OAAO;QACL,MAAMmB,MAAM;IACd;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"sourcesContent":["import { v4 as uuid } from 'uuid'\n\nimport type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\ntype HandleCreatedOrUpdated = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleCreatedOrUpdated: HandleCreatedOrUpdated = async (args) => {\n const { config: payloadConfig, event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const stripeDoc: any = event?.data?.object || {}\n\n const { id: stripeID, object: eventObject } = stripeDoc\n\n // NOTE: the Stripe API does not nest fields, everything is an object at the top level\n // if the event object and resource type don't match, this change was not top-level\n const isNestedChange = eventObject !== resourceType\n\n // let stripeID = docID;\n // if (isNestedChange) {\n // const parentResource = stripeDoc[resourceType];\n // stripeID = parentResource;\n // }\n\n if (isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- This change occurred on a nested field of ${resourceType}. Nested fields are not yet supported in auto-sync but can be manually setup.`,\n )\n }\n }\n\n if (!isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- A new document was created or updated in Stripe, now syncing to Payload...`,\n )\n }\n\n const collectionSlug = syncConfig?.collection\n\n const isAuthCollection = Boolean(\n payloadConfig?.collections?.find((collection) => collection.slug === collectionSlug)?.auth,\n )\n\n // First, search for an existing document in Payload\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n // combine all properties of the Stripe doc and match their respective fields within the document\n let syncedData = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[fieldPath] = stripeDoc[stripeProperty]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedData = deepen({\n ...syncedData,\n skipSync: true,\n stripeID,\n })\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- No existing '${collectionSlug}' document found with Stripe ID: '${stripeID}', creating new...`,\n )\n }\n\n // auth docs must use unique emails\n let authDoc = null\n\n if (isAuthCollection) {\n try {\n if (stripeDoc?.email) {\n const authQuery = await payload.find({\n collection: collectionSlug,\n where: {\n email: {\n equals: stripeDoc.email,\n },\n },\n })\n\n authDoc = authQuery.docs[0] as any\n\n if (authDoc) {\n if (logs) {\n payload.logger.info(\n `- Account already exists with e-mail: ${stripeDoc.email}, updating now...`,\n )\n }\n\n // account exists by email, so update it instead\n try {\n await payload.update({\n id: authDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload with ID: '${authDoc.id}.'`,\n )\n }\n } catch (err: unknown) {\n const msg = err instanceof Error ? err.message : err\n if (logs) {\n payload.logger.error(\n `- Error updating existing '${collectionSlug}' document: ${msg}`,\n )\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.error(\n `No email was provided from Stripe, cannot create new '${collectionSlug}' document.`,\n )\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error looking up '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n\n if (!isAuthCollection || (isAuthCollection && !authDoc)) {\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n\n // generate a strong, unique password for the new user\n const password: string = uuid()\n\n await payload.create({\n collection: collectionSlug,\n data: {\n ...syncedData,\n password,\n passwordConfirm: password,\n },\n disableVerificationEmail: isAuthCollection ? true : undefined,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully created new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error creating new document in Payload: ${msg}`)\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.info(\n `- Existing '${collectionSlug}' document found in Payload with Stripe ID: '${stripeID}', updating now...`,\n )\n }\n\n try {\n await payload.update({\n id: foundDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload from Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error updating '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n }\n}\n"],"names":["v4","uuid","deepen","handleCreatedOrUpdated","args","config","payloadConfig","event","payload","pluginConfig","resourceType","syncConfig","logs","stripeDoc","data","object","id","stripeID","eventObject","isNestedChange","logger","info","collectionSlug","collection","isAuthCollection","Boolean","collections","find","slug","auth","payloadQuery","where","equals","foundDoc","docs","syncedData","fields","reduce","acc","field","fieldPath","stripeProperty","skipSync","authDoc","email","authQuery","update","err","msg","Error","message","error","password","create","passwordConfirm","disableVerificationEmail","undefined"],"mappings":"AAAA,SAASA,MAAMC,IAAI,QAAQ,OAAM;AAIjC,SAASC,MAAM,QAAQ,yBAAwB;AAS/C,OAAO,MAAMC,yBAAiD,OAAOC;IACnE,MAAM,EAAEC,QAAQC,aAAa,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGP;IAE1F,MAAM,EAAEQ,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,YAAiBN,OAAOO,MAAMC,UAAU,CAAC;IAE/C,MAAM,EAAEC,IAAIC,QAAQ,EAAEF,QAAQG,WAAW,EAAE,GAAGL;IAE9C,sFAAsF;IACtF,mFAAmF;IACnF,MAAMM,iBAAiBD,gBAAgBR;IAEvC,wBAAwB;IACxB,wBAAwB;IACxB,oDAAoD;IACpD,+BAA+B;IAC/B,IAAI;IAEJ,IAAIS,gBAAgB;QAClB,IAAIP,MAAM;YACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4CAA4C,EAAEX,aAAa,6EAA6E,CAAC;QAE9I;IACF;IAEA,IAAI,CAACS,gBAAgB;QACnB,IAAIP,MAAM;YACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4EAA4E,CAAC;QAElF;QAEA,MAAMC,iBAAiBX,YAAYY;QAEnC,MAAMC,mBAAmBC,QACvBnB,eAAeoB,aAAaC,KAAK,CAACJ,aAAeA,WAAWK,IAAI,KAAKN,iBAAiBO;QAGxF,oDAAoD;QACpD,MAAMC,eAAe,MAAMtB,QAAQmB,IAAI,CAAC;YACtCJ,YAAYD;YACZS,OAAO;gBACLd,UAAU;oBACRe,QAAQf;gBACV;YACF;QACF;QAEA,MAAMgB,WAAWH,aAAaI,IAAI,CAAC,EAAE;QAErC,iGAAiG;QACjG,IAAIC,aAAaxB,WAAWyB,MAAM,CA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ACjB,CAAC,YAAY,EAAEC,eAAe,6CAA6C,EAAEL,SAAS,kBAAkB,CAAC;YAE7G;YAEA,IAAI;gBACF,MAAMT,QAAQsC,MAAM,CAAC;oBACnB9B,IAAIiB,SAASjB,EAAE;oBACfO,YAAYD;oBACZR,MAAMqB;gBACR;gBAEA,IAAIvB,MAAM;oBACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,wBAAwB,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;gBAEnG;YACF,EAAE,OAAOkC,OAAgB;gBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;gBACrD,IAAIvC,MAAM;oBACRJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,gBAAgB,EAAE7B,eAAe,uBAAuB,EAAE0B,IAAI,CAAC;gBACvF;YACF;QACF;IACF;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"sourcesContent":["import { v4 as uuid } from 'uuid'\n\nimport type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\ntype HandleCreatedOrUpdated = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleCreatedOrUpdated: HandleCreatedOrUpdated = async (args) => {\n const { config: payloadConfig, event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const stripeDoc: any = event?.data?.object || {}\n\n const { id: stripeID, object: eventObject } = stripeDoc\n\n // NOTE: the Stripe API does not nest fields, everything is an object at the top level\n // if the event object and resource type don't match, this change was not top-level\n const isNestedChange = eventObject !== resourceType\n\n // let stripeID = docID;\n // if (isNestedChange) {\n // const parentResource = stripeDoc[resourceType];\n // stripeID = parentResource;\n // }\n\n if (isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- This change occurred on a nested field of ${resourceType}. Nested fields are not yet supported in auto-sync but can be manually setup.`,\n )\n }\n }\n\n if (!isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- A new document was created or updated in Stripe, now syncing to Payload...`,\n )\n }\n\n const collectionSlug = syncConfig?.collection\n\n const isAuthCollection = Boolean(\n payloadConfig?.collections?.find((collection) => collection.slug === collectionSlug)?.auth,\n )\n\n // First, search for an existing document in Payload\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n // combine all properties of the Stripe doc and match their respective fields within the document\n let syncedData = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[fieldPath] = stripeDoc[stripeProperty]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedData = deepen({\n ...syncedData,\n skipSync: true,\n stripeID,\n })\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- No existing '${collectionSlug}' document found with Stripe ID: '${stripeID}', creating new...`,\n )\n }\n\n // auth docs must use unique emails\n let authDoc = null\n\n if (isAuthCollection) {\n try {\n if (stripeDoc?.email) {\n const authQuery = await payload.find({\n collection: collectionSlug,\n where: {\n email: {\n equals: stripeDoc.email,\n },\n },\n })\n\n authDoc = authQuery.docs[0] as any\n\n if (authDoc) {\n if (logs) {\n payload.logger.info(\n `- Account already exists with e-mail: ${stripeDoc.email}, updating now...`,\n )\n }\n\n // account exists by email, so update it instead\n try {\n await payload.update({\n id: authDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload with ID: '${authDoc.id}.'`,\n )\n }\n } catch (err: unknown) {\n const msg = err instanceof Error ? err.message : err\n if (logs) {\n payload.logger.error(\n `- Error updating existing '${collectionSlug}' document: ${msg}`,\n )\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.error(\n `No email was provided from Stripe, cannot create new '${collectionSlug}' document.`,\n )\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error looking up '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n\n if (!isAuthCollection || (isAuthCollection && !authDoc)) {\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n\n // generate a strong, unique password for the new user\n const password: string = uuid()\n\n await payload.create({\n collection: collectionSlug,\n data: {\n ...syncedData,\n password,\n passwordConfirm: password,\n },\n disableVerificationEmail: isAuthCollection ? true : undefined,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully created new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error creating new document in Payload: ${msg}`)\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.info(\n `- Existing '${collectionSlug}' document found in Payload with Stripe ID: '${stripeID}', updating now...`,\n )\n }\n\n try {\n await payload.update({\n id: foundDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload from Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error updating '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n }\n}\n"],"names":["v4","uuid","deepen","handleCreatedOrUpdated","args","config","payloadConfig","event","payload","pluginConfig","resourceType","syncConfig","logs","stripeDoc","data","object","id","stripeID","eventObject","isNestedChange","logger","info","collectionSlug","collection","isAuthCollection","Boolean","collections","find","slug","auth","payloadQuery","where","equals","foundDoc","docs","syncedData","fields","reduce","acc","field","fieldPath","stripeProperty","skipSync","authDoc","email","authQuery","update","err","msg","Error","message","error","password","create","passwordConfirm","disableVerificationEmail","undefined"],"mappings":"A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AAC;oBACnB9B,IAAIiB,SAASjB,EAAE;oBACfO,YAAYD;oBACZR,MAAMqB;gBACR;gBAEA,IAAIvB,MAAM;oBACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,wBAAwB,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;gBAEnG;YACF,EAAE,OAAOkC,OAAgB;gBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;gBACrD,IAAIvC,MAAM;oBACRJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,gBAAgB,EAAE7B,eAAe,uBAAuB,EAAE0B,KAAK;gBACvF;YACF;QACF;IACF;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/webhooks/handleDeleted.ts"],"sourcesContent":["import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\ntype HandleDeleted = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleDeleted: HandleDeleted = async (args) => {\n const { event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const collectionSlug = syncConfig?.collection\n\n const {\n id: stripeID,\n object: eventObject, // use this to determine if this is a nested field\n }: any = event?.data?.object || {}\n\n // if the event object and resource type don't match, this deletion was not top-level\n const isNestedDelete = eventObject !== resourceType\n\n if (isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- This deletion occurred on a nested field of ${resourceType}. Nested fields are not yet supported.`,\n )\n }\n }\n\n if (!isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- A '${resourceType}' resource was deleted in Stripe, now deleting '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'...`,\n )\n }\n\n try {\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- Nothing to delete, no existing document found with Stripe ID: '${stripeID}'.`,\n )\n }\n }\n\n if (foundDoc) {\n if (logs) {\n payload.logger.info(`- Deleting Payload document with ID: '${foundDoc.id}'...`)\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n payload.delete({\n id: foundDoc.id,\n collection: collectionSlug,\n })\n\n // NOTE: the `afterDelete` hook will trigger, which will attempt to delete the document from Stripe and safely error out\n // There is no known way of preventing this from happening. In other hooks we use the `skipSync` field, but here the document is already deleted.\n if (logs) {\n payload.logger.info(\n `- ✅ Successfully deleted Payload document with ID: '${foundDoc.id}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n}\n"],"names":["handleDeleted","args","event","payload","pluginConfig","resourceType","syncConfig","logs","collectionSlug","collection","id","stripeID","object","eventObject","data","isNestedDelete","logger","info","payloadQuery","find","where","equals","foundDoc","docs","delete","error","msg","Error","message"],"mappings":"AASA,OAAO,MAAMA,gBAA+B,OAAOC;IACjD,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGL;IAEnE,MAAM,EAAEM,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,iBAAiBF,YAAYG;IAEnC,MAAM,EACJC,IAAIC,QAAQ,EACZC,QAAQC,WAAW,EACpB,GAAQX,OAAOY,MAAMF,UAAU,CAAC;IAEjC,qFAAqF;IACrF,MAAMG,iBAAiBF,gBAAgBR;IAEvC,IAAIU,gBAAgB;QAClB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,8CAA8C,EAAEZ,aAAa,sCAAsC,CAAC;QAEzG;IACF;IAEA,IAAI,CAACU,gBAAgB;QACnB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,KAAK,EAAEZ,aAAa,gDAAgD,EAAEG,eAAe,uCAAuC,EAAEG,SAAS,IAAI,CAAC;QAEjJ;QAEA,IAAI;YACF,MAAMO,eAAe,MAAMf,QAAQgB,IAAI,CAAC;gBACtCV,YAAYD;gBACZY,OAAO;oBACLT,UAAU;wBACRU,QAAQV;oBACV;gBACF;YACF;YAEA,MAAMW,WAAWJ,aAAaK,IAAI,CAAC,EAAE;YAErC,IAAI,CAACD,UAAU;gBACb,IAAIf,MAAM;oBACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,iEAAiE,EAAEN,SAAS,EAAE,CAAC;gBAEpF;YACF;YAEA,IAAIW,UAAU;gBACZ,IAAIf,MAAM;oBACRJ,QAAQa,MAAM,CAACC,IAAI,CAAC,CAAC,sCAAsC,EAAEK,SAASZ,EAAE,CAAC,IAAI,CAAC;gBAChF;gBAEA,IAAI;oBACF,mEAAmE;oBACnEP,QAAQqB,MAAM,CAAC;wBACbd,IAAIY,SAASZ,EAAE;wBACfD,YAAYD;oBACd;oBAEA,wHAAwH;oBACxH,iJAAiJ;oBACjJ,IAAID,MAAM;wBACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,oDAAoD,EAAEK,SAASZ,EAAE,CAAC,EAAE,CAAC;oBAE1E;gBACF,EAAE,OAAOe,OAAgB;oBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;oBACrD,IAAIlB,MAAM;wBACRJ,QAAQa,MAAM,CAACS,KAAK,CAAC,CAAC,yBAAyB,EAAEC,
|
|
1
|
+
{"version":3,"sources":["../../src/webhooks/handleDeleted.ts"],"sourcesContent":["import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\ntype HandleDeleted = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleDeleted: HandleDeleted = async (args) => {\n const { event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const collectionSlug = syncConfig?.collection\n\n const {\n id: stripeID,\n object: eventObject, // use this to determine if this is a nested field\n }: any = event?.data?.object || {}\n\n // if the event object and resource type don't match, this deletion was not top-level\n const isNestedDelete = eventObject !== resourceType\n\n if (isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- This deletion occurred on a nested field of ${resourceType}. Nested fields are not yet supported.`,\n )\n }\n }\n\n if (!isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- A '${resourceType}' resource was deleted in Stripe, now deleting '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'...`,\n )\n }\n\n try {\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- Nothing to delete, no existing document found with Stripe ID: '${stripeID}'.`,\n )\n }\n }\n\n if (foundDoc) {\n if (logs) {\n payload.logger.info(`- Deleting Payload document with ID: '${foundDoc.id}'...`)\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n payload.delete({\n id: foundDoc.id,\n collection: collectionSlug,\n })\n\n // NOTE: the `afterDelete` hook will trigger, which will attempt to delete the document from Stripe and safely error out\n // There is no known way of preventing this from happening. In other hooks we use the `skipSync` field, but here the document is already deleted.\n if (logs) {\n payload.logger.info(\n `- ✅ Successfully deleted Payload document with ID: '${foundDoc.id}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n}\n"],"names":["handleDeleted","args","event","payload","pluginConfig","resourceType","syncConfig","logs","collectionSlug","collection","id","stripeID","object","eventObject","data","isNestedDelete","logger","info","payloadQuery","find","where","equals","foundDoc","docs","delete","error","msg","Error","message"],"mappings":"AASA,OAAO,MAAMA,gBAA+B,OAAOC;IACjD,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGL;IAEnE,MAAM,EAAEM,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,iBAAiBF,YAAYG;IAEnC,MAAM,EACJC,IAAIC,QAAQ,EACZC,QAAQC,WAAW,EACpB,GAAQX,OAAOY,MAAMF,UAAU,CAAC;IAEjC,qFAAqF;IACrF,MAAMG,iBAAiBF,gBAAgBR;IAEvC,IAAIU,gBAAgB;QAClB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,8CAA8C,EAAEZ,aAAa,sCAAsC,CAAC;QAEzG;IACF;IAEA,IAAI,CAACU,gBAAgB;QACnB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,KAAK,EAAEZ,aAAa,gDAAgD,EAAEG,eAAe,uCAAuC,EAAEG,SAAS,IAAI,CAAC;QAEjJ;QAEA,IAAI;YACF,MAAMO,eAAe,MAAMf,QAAQgB,IAAI,CAAC;gBACtCV,YAAYD;gBACZY,OAAO;oBACLT,UAAU;wBACRU,QAAQV;oBACV;gBACF;YACF;YAEA,MAAMW,WAAWJ,aAAaK,IAAI,CAAC,EAAE;YAErC,IAAI,CAACD,UAAU;gBACb,IAAIf,MAAM;oBACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,iEAAiE,EAAEN,SAAS,EAAE,CAAC;gBAEpF;YACF;YAEA,IAAIW,UAAU;gBACZ,IAAIf,MAAM;oBACRJ,QAAQa,MAAM,CAACC,IAAI,CAAC,CAAC,sCAAsC,EAAEK,SAASZ,EAAE,CAAC,IAAI,CAAC;gBAChF;gBAEA,IAAI;oBACF,mEAAmE;oBACnEP,QAAQqB,MAAM,CAAC;wBACbd,IAAIY,SAASZ,EAAE;wBACfD,YAAYD;oBACd;oBAEA,wHAAwH;oBACxH,iJAAiJ;oBACjJ,IAAID,MAAM;wBACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,oDAAoD,EAAEK,SAASZ,EAAE,CAAC,EAAE,CAAC;oBAE1E;gBACF,EAAE,OAAOe,OAAgB;oBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;oBACrD,IAAIlB,MAAM;wBACRJ,QAAQa,MAAM,CAACS,KAAK,CAAC,CAAC,yBAAyB,EAAEC,KAAK;oBACxD;gBACF;YACF;QACF,EAAE,OAAOD,OAAgB;YACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;YACrD,IAAIlB,MAAM;gBACRJ,QAAQa,MAAM,CAACS,KAAK,CAAC,CAAC,yBAAyB,EAAEC,KAAK;YACxD;QACF;IACF;AACF,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-stripe",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Stripe plugin for Payload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"lodash.get": "^4.4.2",
|
|
55
55
|
"stripe": "^10.2.0",
|
|
56
56
|
"uuid": "10.0.0",
|
|
57
|
-
"@payloadcms/
|
|
58
|
-
"@payloadcms/
|
|
57
|
+
"@payloadcms/ui": "3.4.0",
|
|
58
|
+
"@payloadcms/translations": "3.4.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@types/lodash.get": "^4.4.7",
|
|
@@ -63,11 +63,11 @@
|
|
|
63
63
|
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
|
|
64
64
|
"@types/uuid": "10.0.0",
|
|
65
65
|
"@payloadcms/eslint-config": "3.0.0",
|
|
66
|
-
"
|
|
67
|
-
"
|
|
66
|
+
"@payloadcms/next": "3.4.0",
|
|
67
|
+
"payload": "3.4.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"payload": "3.
|
|
70
|
+
"payload": "3.4.0"
|
|
71
71
|
},
|
|
72
72
|
"publishConfig": {
|
|
73
73
|
"registry": "https://registry.npmjs.org/"
|