Skip to content

Commit 75a9546

Browse files
authored
feat(plugin-stripe): update plugin stripe for v3 (#6019)
1 parent c0ae287 commit 75a9546

File tree

19 files changed

+105
-100
lines changed

19 files changed

+105
-100
lines changed

packages/plugin-stripe/package.json

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"exports": {
2525
".": {
2626
"import": "./src/index.ts",
27-
"require": "./src/index.ts",
28-
"types": "./src/index.ts"
27+
"types": "./src/index.ts",
28+
"default": "./src/index.ts"
2929
}
3030
},
3131
"main": "./src/index.ts",
@@ -36,12 +36,14 @@
3636
"types.d.ts"
3737
],
3838
"scripts": {
39-
"build": "echo \"Build temporarily disabled.\" && exit 0",
39+
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
4040
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
4141
"build:types": "tsc --emitDeclarationOnly --outDir dist",
4242
"clean": "rimraf {dist,*.tsbuildinfo}",
43-
"prepublishOnly": "pnpm clean && pnpm turbo run build && pnpm test",
44-
"test": "echo 'No tests available.'"
43+
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/",
44+
"lint": "eslint src",
45+
"lint:fix": "eslint --fix --ext .ts,.tsx src",
46+
"prepublishOnly": "pnpm clean && pnpm turbo build"
4547
},
4648
"dependencies": {
4749
"@payloadcms/ui": "workspace:*",
@@ -51,17 +53,19 @@
5153
},
5254
"devDependencies": {
5355
"@payloadcms/eslint-config": "workspace:*",
56+
"@payloadcms/next": "workspace:*",
57+
"@payloadcms/translations": "workspace:*",
58+
"@payloadcms/ui": "workspace:*",
5459
"@types/express": "^4.17.9",
5560
"@types/lodash.get": "^4.4.7",
5661
"@types/react": "18.2.74",
5762
"@types/uuid": "^9.0.0",
58-
"payload": "workspace:*",
59-
"prettier": "^2.7.1",
60-
"webpack": "^5.78.0"
63+
"payload": "workspace:*"
6164
},
6265
"peerDependencies": {
63-
"payload": "workspace:*",
64-
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
66+
"@payloadcms/translations": "workspace:*",
67+
"@payloadcms/ui": "workspace:*",
68+
"payload": "workspace:*"
6569
},
6670
"publishConfig": {
6771
"exports": {
@@ -72,6 +76,7 @@
7276
}
7377
},
7478
"main": "./dist/index.js",
79+
"registry": "https://registry.npmjs.org/",
7580
"types": "./dist/index.d.ts"
7681
},
7782
"homepage:": "https://payloadcms.com"

packages/plugin-stripe/src/admin.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Config } from 'payload/config'
22

3-
import type { SanitizedStripeConfig, StripeConfig } from './types'
3+
import type { SanitizedStripeConfig, StripeConfig } from './types.js'
44

5-
import { getFields } from './fields/getFields'
5+
import { getFields } from './fields/getFields.js'
66

7-
const stripePlugin =
7+
export const stripePlugin =
88
(incomingStripeConfig: StripeConfig) =>
99
(config: Config): Config => {
1010
const { collections } = config
@@ -42,5 +42,3 @@ const stripePlugin =
4242
}),
4343
}
4444
}
45-
46-
export default stripePlugin

packages/plugin-stripe/src/fields/getFields.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { CollectionConfig, Field } from 'payload/types'
22

3-
import type { SanitizedStripeConfig } from '../types'
3+
import type { SanitizedStripeConfig } from '../types.js'
44

5-
import { LinkToDoc } from '../ui/LinkToDoc'
5+
import { LinkToDoc } from '../ui/LinkToDoc.js'
66

77
interface Args {
88
collection: CollectionConfig
@@ -39,13 +39,12 @@ export const getFields = ({ collection, stripeConfig, syncConfig }: Args): Field
3939
type: 'ui',
4040
admin: {
4141
components: {
42-
Field: (args) =>
43-
LinkToDoc({
44-
...args,
45-
isTestKey: stripeConfig.isTestKey,
46-
nameOfIDField: 'stripeID',
47-
stripeResourceType: syncConfig.stripeResourceType,
48-
}),
42+
Field: LinkToDoc,
43+
},
44+
custom: {
45+
isTestKey: stripeConfig.isTestKey,
46+
nameOfIDField: 'stripeID',
47+
stripeResourceType: syncConfig.stripeResourceType,
4948
},
5049
position: 'sidebar',
5150
},

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload/typ
33
import { APIError } from 'payload/errors'
44
import Stripe from 'stripe'
55

6-
import type { StripeConfig } from '../types'
6+
import type { StripeConfig } from '../types.js'
77

8-
import { deepen } from '../utilities/deepen'
8+
import { deepen } from '../utilities/deepen.js'
99

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

1314
type HookArgsWithCustomCollection = Omit<
@@ -52,12 +53,15 @@ export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (ar
5253

5354
if (syncConfig) {
5455
// combine all fields of this object and match their respective values within the document
55-
let syncedFields = syncConfig.fields.reduce((acc, field) => {
56-
const { fieldPath, stripeProperty } = field
56+
let syncedFields = syncConfig.fields.reduce(
57+
(acc, field) => {
58+
const { fieldPath, stripeProperty } = field
5759

58-
acc[stripeProperty] = dataRef[fieldPath]
59-
return acc
60-
}, {} as Record<string, any>)
60+
acc[stripeProperty] = dataRef[fieldPath]
61+
return acc
62+
},
63+
{} as Record<string, any>,
64+
)
6165

6266
syncedFields = deepen(syncedFields)
6367

@@ -72,6 +76,7 @@ export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (ar
7276
try {
7377
// NOTE: Typed as "any" because the "create" method is not standard across all Stripe resources
7478
const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(
79+
// @ts-expect-error
7580
syncedFields,
7681
)
7782

@@ -105,6 +110,7 @@ export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (ar
105110

106111
// NOTE: Typed as "any" because the "create" method is not standard across all Stripe resources
107112
const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(
113+
// @ts-expect-error
108114
syncedFields,
109115
)
110116

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload/types'
33
import { APIError } from 'payload/errors'
44
import Stripe from 'stripe'
55

6-
import type { StripeConfig } from '../types'
6+
import type { StripeConfig } from '../types.js'
77

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

1112
type HookArgsWithCustomCollection = Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'> & {

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types
33
import { APIError } from 'payload/errors'
44
import Stripe from 'stripe'
55

6-
import type { StripeConfig } from '../types'
6+
import type { StripeConfig } from '../types.js'
77

8-
import { deepen } from '../utilities/deepen'
8+
import { deepen } from '../utilities/deepen.js'
99

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

1314
type HookArgsWithCustomCollection = Omit<
@@ -39,12 +40,15 @@ export const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async
3940
if (syncConfig) {
4041
if (operation === 'update') {
4142
// combine all fields of this object and match their respective values within the document
42-
let syncedFields = syncConfig.fields.reduce((acc, field) => {
43-
const { fieldPath, stripeProperty } = field
44-
45-
acc[stripeProperty] = data[fieldPath]
46-
return acc
47-
}, {} as Record<string, any>)
43+
let syncedFields = syncConfig.fields.reduce(
44+
(acc, field) => {
45+
const { fieldPath, stripeProperty } = field
46+
47+
acc[stripeProperty] = data[fieldPath]
48+
return acc
49+
},
50+
{} as Record<string, any>,
51+
)
4852

4953
syncedFields = deepen(syncedFields)
5054

packages/plugin-stripe/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { syncExistingWithStripe } from './hooks/syncExistingWithStripe.js'
99
import { stripeREST } from './routes/rest.js'
1010
import { stripeWebhooks } from './routes/webhooks.js'
1111

12-
const stripePlugin =
12+
export { LinkToDoc } from './ui/LinkToDoc.js'
13+
export { stripeProxy } from './utilities/stripeProxy.js'
14+
15+
export const stripePlugin =
1316
(incomingStripeConfig: StripeConfig) =>
1417
(config: Config): Config => {
1518
const { collections } = config
@@ -112,5 +115,3 @@ const stripePlugin =
112115
endpoints,
113116
}
114117
}
115-
116-
export default stripePlugin

packages/plugin-stripe/src/mocks/mockFile.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/plugin-stripe/src/routes/rest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export const stripeREST = async (args: {
3333
}
3434

3535
responseJSON = await stripeProxy({
36+
// @ts-expect-error
3637
stripeArgs,
38+
// @ts-expect-error
3739
stripeMethod,
3840
stripeSecretKey,
3941
})

packages/plugin-stripe/src/routes/webhooks.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,22 @@ export const stripeWebhooks = async (args: {
1919

2020
if (stripeWebhooksEndpointSecret) {
2121
const stripe = new Stripe(stripeSecretKey, {
22+
// api version can only be the latest, stripe recommends ts ignoring it
2223
apiVersion: '2022-08-01',
2324
appInfo: {
2425
name: 'Stripe Payload Plugin',
2526
url: 'https://payloadcms.com',
2627
},
2728
})
2829

30+
const body = await req.text()
2931
const stripeSignature = req.headers.get('stripe-signature')
3032

3133
if (stripeSignature) {
3234
let event: Stripe.Event | undefined
3335

3436
try {
35-
event = stripe.webhooks.constructEvent(
36-
await req.text(),
37-
stripeSignature,
38-
stripeWebhooksEndpointSecret,
39-
)
37+
event = stripe.webhooks.constructEvent(body, stripeSignature, stripeWebhooksEndpointSecret)
4038
} catch (err: unknown) {
4139
const msg: string = err instanceof Error ? err.message : JSON.stringify(err)
4240
req.payload.logger.error(`Error constructing Stripe event: ${msg}`)

0 commit comments

Comments
 (0)