Skip to content

Commit d2f63dc

Browse files
authored
fix(plugin-stripe): hooks did not use api key from plugin config (#10671)
Fixes #10668
1 parent 9215f03 commit d2f63dc

File tree

4 files changed

+57
-60
lines changed

4 files changed

+57
-60
lines changed

packages/plugin-stripe/src/hooks/createNewInStripe.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import type { StripePluginConfig } from '../types.js'
77

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

10-
const stripeSecretKey = process.env.STRIPE_SECRET_KEY
11-
// api version can only be the latest, stripe recommends ts ignoring it
12-
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })
13-
1410
type HookArgsWithCustomCollection = {
1511
collection: CollectionConfig
1612
} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>
@@ -64,6 +60,9 @@ export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (ar
6460

6561
syncedFields = deepen(syncedFields)
6662

63+
// api version can only be the latest, stripe recommends ts ignoring it
64+
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', { apiVersion: '2022-08-01' })
65+
6766
if (operation === 'update') {
6867
if (logs) {
6968
payload.logger.info(

packages/plugin-stripe/src/hooks/deleteFromStripe.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import Stripe from 'stripe'
55

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

8-
const stripeSecretKey = process.env.STRIPE_SECRET_KEY
9-
// api version can only be the latest, stripe recommends ts ignoring it
10-
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })
11-
128
type HookArgsWithCustomCollection = {
139
collection: CollectionConfig
1410
} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>
@@ -43,6 +39,9 @@ export const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args)
4339

4440
if (syncConfig) {
4541
try {
42+
// api version can only be the latest, stripe recommends ts ignoring it
43+
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', { apiVersion: '2022-08-01' })
44+
4645
const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID)
4746

4847
if (found) {

packages/plugin-stripe/src/hooks/syncExistingWithStripe.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import type { StripePluginConfig } from '../types.js'
77

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

10-
const stripeSecretKey = process.env.STRIPE_SECRET_KEY
11-
// api version can only be the latest, stripe recommends ts ignoring it
12-
const stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })
13-
1410
type HookArgsWithCustomCollection = {
1511
collection: CollectionConfig
1612
} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>
@@ -66,6 +62,11 @@ export const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async
6662
}
6763

6864
try {
65+
// api version can only be the latest, stripe recommends ts ignoring it
66+
const stripe = new Stripe(pluginConfig.stripeSecretKey || '', {
67+
apiVersion: '2022-08-01',
68+
})
69+
6970
const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(
7071
data.stripeID,
7172
syncedFields,

packages/plugin-stripe/src/index.ts

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,57 +59,55 @@ export const stripePlugin =
5959
})
6060
}
6161

62-
return {
63-
...config,
64-
collections: collections?.map((collection) => {
65-
const { hooks: existingHooks } = collection
62+
for (const collection of collections) {
63+
const { hooks: existingHooks } = collection
6664

67-
const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)
65+
const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)
6866

69-
if (syncConfig) {
70-
const fields = getFields({
67+
if (!syncConfig) {
68+
continue
69+
}
70+
const fields = getFields({
71+
collection,
72+
pluginConfig,
73+
syncConfig,
74+
})
75+
collection.fields = fields
76+
77+
if (!collection.hooks) {
78+
collection.hooks = {}
79+
}
80+
81+
collection.hooks.afterDelete = [
82+
...(existingHooks?.afterDelete || []),
83+
(args) =>
84+
deleteFromStripe({
85+
...args,
7186
collection,
7287
pluginConfig,
73-
syncConfig,
74-
})
75-
return {
76-
...collection,
77-
fields,
78-
hooks: {
79-
...collection.hooks,
80-
afterDelete: [
81-
...(existingHooks?.afterDelete || []),
82-
(args) =>
83-
deleteFromStripe({
84-
...args,
85-
collection,
86-
pluginConfig,
87-
}),
88-
],
89-
beforeChange: [
90-
...(existingHooks?.beforeChange || []),
91-
(args) =>
92-
syncExistingWithStripe({
93-
...args,
94-
collection,
95-
pluginConfig,
96-
}),
97-
],
98-
beforeValidate: [
99-
...(existingHooks?.beforeValidate || []),
100-
(args) =>
101-
createNewInStripe({
102-
...args,
103-
collection,
104-
pluginConfig,
105-
}),
106-
],
107-
},
108-
}
109-
}
110-
111-
return collection
112-
}),
113-
endpoints,
88+
}),
89+
]
90+
collection.hooks.beforeChange = [
91+
...(existingHooks?.beforeChange || []),
92+
(args) =>
93+
syncExistingWithStripe({
94+
...args,
95+
collection,
96+
pluginConfig,
97+
}),
98+
]
99+
collection.hooks.beforeValidate = [
100+
...(existingHooks?.beforeValidate || []),
101+
(args) =>
102+
createNewInStripe({
103+
...args,
104+
collection,
105+
pluginConfig,
106+
}),
107+
]
114108
}
109+
110+
config.endpoints = endpoints
111+
112+
return config
115113
}

0 commit comments

Comments
 (0)