Skip to content

Commit

Permalink
fix(plugin-stripe): passed api key was not always used
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Jan 19, 2025
1 parent 9215f03 commit 8a970a6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
7 changes: 3 additions & 4 deletions packages/plugin-stripe/src/hooks/createNewInStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import type { StripePluginConfig } from '../types.js'

import { deepen } from '../utilities/deepen.js'

const stripeSecretKey = process.env.STRIPE_SECRET_KEY
// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })

type HookArgsWithCustomCollection = {
collection: CollectionConfig
} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>
Expand Down Expand Up @@ -64,6 +60,9 @@ export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (ar

syncedFields = deepen(syncedFields)

// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', { apiVersion: '2022-08-01' })

if (operation === 'update') {
if (logs) {
payload.logger.info(
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin-stripe/src/hooks/deleteFromStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import Stripe from 'stripe'

import type { StripePluginConfig } from '../types.js'

const stripeSecretKey = process.env.STRIPE_SECRET_KEY
// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })

type HookArgsWithCustomCollection = {
collection: CollectionConfig
} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>
Expand Down Expand Up @@ -43,6 +39,9 @@ export const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args)

if (syncConfig) {
try {
// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', { apiVersion: '2022-08-01' })

const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID)

if (found) {
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-stripe/src/hooks/syncExistingWithStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import type { StripePluginConfig } from '../types.js'

import { deepen } from '../utilities/deepen.js'

const stripeSecretKey = process.env.STRIPE_SECRET_KEY
// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })

type HookArgsWithCustomCollection = {
collection: CollectionConfig
} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>
Expand Down Expand Up @@ -66,6 +62,11 @@ export const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async
}

try {
// api version can only be the latest, stripe recommends ts ignoring it
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', {
apiVersion: '2022-08-01',
})

const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(
data.stripeID,
syncedFields,
Expand Down
94 changes: 46 additions & 48 deletions packages/plugin-stripe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,57 +59,55 @@ export const stripePlugin =
})
}

return {
...config,
collections: collections?.map((collection) => {
const { hooks: existingHooks } = collection
for (const collection of collections) {
const { hooks: existingHooks } = collection

const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)
const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)

if (syncConfig) {
const fields = getFields({
if (!syncConfig) {
continue
}
const fields = getFields({
collection,
pluginConfig,
syncConfig,
})
collection.fields = fields

if (!collection.hooks) {
collection.hooks = {}
}

collection.hooks.afterDelete = [
...(existingHooks?.afterDelete || []),
(args) =>
deleteFromStripe({
...args,
collection,
pluginConfig,
syncConfig,
})
return {
...collection,
fields,
hooks: {
...collection.hooks,
afterDelete: [
...(existingHooks?.afterDelete || []),
(args) =>
deleteFromStripe({
...args,
collection,
pluginConfig,
}),
],
beforeChange: [
...(existingHooks?.beforeChange || []),
(args) =>
syncExistingWithStripe({
...args,
collection,
pluginConfig,
}),
],
beforeValidate: [
...(existingHooks?.beforeValidate || []),
(args) =>
createNewInStripe({
...args,
collection,
pluginConfig,
}),
],
},
}
}

return collection
}),
endpoints,
}),
]
collection.hooks.beforeChange = [
...(existingHooks?.beforeChange || []),
(args) =>
syncExistingWithStripe({
...args,
collection,
pluginConfig,
}),
]
collection.hooks.beforeValidate = [
...(existingHooks?.beforeValidate || []),
(args) =>
createNewInStripe({
...args,
collection,
pluginConfig,
}),
]
}

config.endpoints = endpoints

return config
}

0 comments on commit 8a970a6

Please sign in to comment.