@payloadcms/plugin-stripe 0.0.2 → 0.0.4
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/README.md +36 -10
- package/dist/extendWebpackConfig.d.ts +3 -0
- package/dist/extendWebpackConfig.js +31 -0
- package/dist/extendWebpackConfig.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/mocks/serverModule.d.ts +1 -0
- package/dist/mocks/serverModule.js +3 -0
- package/dist/mocks/serverModule.js.map +1 -0
- package/dist/routes/webhooks.js +3 -3
- package/dist/routes/webhooks.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -24,11 +24,11 @@ In the `plugins` array of your [Payload config](https://payloadcms.com/docs/conf
|
|
|
24
24
|
|
|
25
25
|
```js
|
|
26
26
|
import { buildConfig } from 'payload/config';
|
|
27
|
-
import
|
|
27
|
+
import stripePlugin from '@payloadcms/plugin-stripe';
|
|
28
28
|
|
|
29
29
|
const config = buildConfig({
|
|
30
30
|
plugins: [
|
|
31
|
-
|
|
31
|
+
stripePlugin({
|
|
32
32
|
stripeSecretKey: process.env.STRIPE_SECRET_KEY,
|
|
33
33
|
})
|
|
34
34
|
]
|
|
@@ -43,7 +43,7 @@ export default config;
|
|
|
43
43
|
|
|
44
44
|
Required. Your Stripe secret key.
|
|
45
45
|
|
|
46
|
-
- `
|
|
46
|
+
- `stripeWebhooksEndpointSecret`
|
|
47
47
|
|
|
48
48
|
Optional. Your Stripe webhook endpoint secret. This is needed only if you wish to sync data from Stripe to Payload.
|
|
49
49
|
|
|
@@ -53,7 +53,7 @@ export default config;
|
|
|
53
53
|
|
|
54
54
|
### Endpoints
|
|
55
55
|
|
|
56
|
-
One core functionality of this plugin is to enable a two-way communication channel between Stripe and Payload. To do this, the following custom endpoints are automatically opened for you
|
|
56
|
+
One core functionality of this plugin is to enable a two-way communication channel between Stripe and Payload. To do this, the following custom endpoints are automatically opened for you.
|
|
57
57
|
|
|
58
58
|
>NOTE: the `/api` part of these routes may be different based on the settings defined in your Payload config.
|
|
59
59
|
|
|
@@ -84,20 +84,25 @@ One core functionality of this plugin is to enable a two-way communication chann
|
|
|
84
84
|
|
|
85
85
|
### Webhooks
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
[Stripe webhooks](https://stripe.com/docs/webhooks) are used to sync from Stripe to Payload. Webhooks listen for events on your Stripe account so you can trigger reactions to them. Follow the steps below to enable webhooks.
|
|
88
88
|
|
|
89
|
+
Development:
|
|
90
|
+
1. Login using Stripe cli `stripe login`
|
|
91
|
+
1. Forward events to localhost `stripe listen --forward-to localhost:3000/api/stripe/webhooks`
|
|
92
|
+
|
|
93
|
+
Production:
|
|
89
94
|
1. Login and [create a new webhook](https://dashboard.stripe.com/test/webhooks/create) from the Stripe dashboard
|
|
90
|
-
1. Paste
|
|
95
|
+
1. Paste `YOUR_DOMAIN_NAME/api/stripe/webhooks` as the "Webhook Endpoint URL"
|
|
91
96
|
1. Select which events to broadcast
|
|
92
97
|
1. Then, handle these events using the `webhooks` portion of this plugin's config:
|
|
93
98
|
|
|
94
99
|
```js
|
|
95
100
|
import { buildConfig } from 'payload/config';
|
|
96
|
-
import
|
|
101
|
+
import stripePlugin from '@payloadcms/plugin-stripe';
|
|
97
102
|
|
|
98
103
|
const config = buildConfig({
|
|
99
104
|
plugins: [
|
|
100
|
-
|
|
105
|
+
stripePlugin({
|
|
101
106
|
stripeSecretKey: process.env.STRIPE_SECRET_KEY,
|
|
102
107
|
stripeWebhooksEndpointSecret: process.env.STRIPE_WEBHOOKS_ENDPOINT_SECRET,
|
|
103
108
|
webhooks: {
|
|
@@ -114,7 +119,28 @@ For a full list of available webhooks, see [here](https://stripe.com/docs/cli/tr
|
|
|
114
119
|
|
|
115
120
|
### Node
|
|
116
121
|
|
|
117
|
-
|
|
122
|
+
On the server you should interface with Stripe directly using the [stripe](https://www.npmjs.com/package/stripe) npm module. That might look something like this:
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
import Stripe from 'stripe';
|
|
126
|
+
|
|
127
|
+
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
|
|
128
|
+
const stripe = new Stripe(stripeSecretKey, { apiVersion: '2022-08-01' });
|
|
129
|
+
|
|
130
|
+
export const MyFunction = async () => {
|
|
131
|
+
try {
|
|
132
|
+
const customer = await stripe.customers.create({
|
|
133
|
+
email: data.email,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// DO SOMETHING
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error(error.message);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Alternatively, you can interface with the Stripe using the `stripeProxy`, which is exactly what the `/api/stripe/rest` endpoint does behind-the-scenes. Here's the same example as above, but piped through the proxy:
|
|
118
144
|
|
|
119
145
|
```js
|
|
120
146
|
import { stripeProxy } from '@payloadcms/plugin-stripe';
|
|
@@ -156,7 +182,7 @@ import {
|
|
|
156
182
|
|
|
157
183
|
### Development
|
|
158
184
|
|
|
159
|
-
|
|
185
|
+
For development purposes, there is a full working example of how this plugin might be used in the [demo](./demo) of this repo. This demo can be developed locally using any Stripe account, you just need a working API key. Then:
|
|
160
186
|
|
|
161
187
|
```bash
|
|
162
188
|
git clone git@github.com:payloadcms/plugin-stripe.git \
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.extendWebpackConfig = void 0;
|
|
18
|
+
var path_1 = __importDefault(require("path"));
|
|
19
|
+
var mockModulePath = path_1.default.resolve(__dirname, 'mocks/serverModule.js');
|
|
20
|
+
var extendWebpackConfig = function (config) {
|
|
21
|
+
return function (webpackConfig) {
|
|
22
|
+
var _a;
|
|
23
|
+
var _b, _c;
|
|
24
|
+
var existingWebpackConfig = typeof ((_b = config.admin) === null || _b === void 0 ? void 0 : _b.webpack) === 'function'
|
|
25
|
+
? config.admin.webpack(webpackConfig)
|
|
26
|
+
: webpackConfig;
|
|
27
|
+
return __assign(__assign({}, existingWebpackConfig), { resolve: __assign(__assign({}, (existingWebpackConfig.resolve || {})), { alias: __assign(__assign({}, (((_c = existingWebpackConfig.resolve) === null || _c === void 0 ? void 0 : _c.alias) ? existingWebpackConfig.resolve.alias : {})), (_a = { "stripe": mockModulePath }, _a[path_1.default.resolve(__dirname, 'routes/rest')] = mockModulePath, _a[path_1.default.resolve(__dirname, 'routes/webhooks')] = mockModulePath, _a)) }) });
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.extendWebpackConfig = extendWebpackConfig;
|
|
31
|
+
//# sourceMappingURL=extendWebpackConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extendWebpackConfig.js","sourceRoot":"","sources":["../src/extendWebpackConfig.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,8CAAuB;AAGvB,IAAM,cAAc,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,uBAAuB,CAAC,CAAC;AAEjE,IAAM,mBAAmB,GAAG,UAAC,MAAc;IAChD,OAAA,UAAA,aAAa;;;QACX,IAAM,qBAAqB,GACzB,OAAO,CAAA,MAAA,MAAM,CAAC,KAAK,0CAAE,OAAO,CAAA,KAAK,UAAU;YACzC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC;YACrC,CAAC,CAAC,aAAa,CAAA;QAEnB,6BACK,qBAAqB,KACxB,OAAO,wBACF,CAAC,qBAAqB,CAAC,OAAO,IAAI,EAAE,CAAC,KACxC,KAAK,wBACA,CAAC,CAAA,MAAA,qBAAqB,CAAC,OAAO,0CAAE,KAAK,EAAC,CAAC,CAAC,qBAAqB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,WACpF,QAAQ,EAAE,cAAc,OACvB,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,aAAa,CAAC,IAAG,cAAc,KACvD,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,iBAAiB,CAAC,IAAG,cAAc,aAGjE;IACH,CAAC;AAlBD,CAkBC,CAAA;AAnBU,QAAA,mBAAmB,uBAmB7B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Config } from 'payload/config';
|
|
2
2
|
import { StripeConfig } from './types';
|
|
3
|
-
declare const
|
|
4
|
-
export default
|
|
3
|
+
declare const stripePlugin: (stripeConfig: StripeConfig) => (config: Config) => Config;
|
|
4
|
+
export default stripePlugin;
|
package/dist/index.js
CHANGED
|
@@ -22,8 +22,9 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
var rest_1 = require("./routes/rest");
|
|
24
24
|
var webhooks_1 = require("./routes/webhooks");
|
|
25
|
-
var
|
|
26
|
-
|
|
25
|
+
var extendWebpackConfig_1 = require("./extendWebpackConfig");
|
|
26
|
+
var stripePlugin = function (stripeConfig) { return function (config) {
|
|
27
|
+
return (__assign(__assign({}, config), { admin: __assign(__assign({}, config.admin), { webpack: (0, extendWebpackConfig_1.extendWebpackConfig)(config) }), endpoints: __spreadArray(__spreadArray([], (config === null || config === void 0 ? void 0 : config.endpoints) || [], true), [
|
|
27
28
|
{
|
|
28
29
|
path: '/stripe/webhooks',
|
|
29
30
|
method: 'post',
|
|
@@ -40,5 +41,5 @@ var stripe = function (stripeConfig) { return function (config) {
|
|
|
40
41
|
},
|
|
41
42
|
], false) }));
|
|
42
43
|
}; };
|
|
43
|
-
exports.default =
|
|
44
|
+
exports.default = stripePlugin;
|
|
44
45
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,sCAA2C;AAC3C,8CAAmD;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,sCAA2C;AAC3C,8CAAmD;AAEnD,6DAA4D;AAE5D,IAAM,YAAY,GAAG,UAAC,YAA0B,IAAK,OAAA,UAAC,MAAc;IAClE,OAAO,uBACF,MAAM,KACT,KAAK,wBACA,MAAM,CAAC,KAAK,KACf,OAAO,EAAE,IAAA,yCAAmB,EAAC,MAAM,CAAC,KAEtC,SAAS,kCACJ,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,SAAS,KAAI,EAAE;YAC1B;gBACE,IAAI,EAAE,kBAAkB;gBACxB,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,UAAC,GAAG,EAAE,GAAG,EAAE,IAAI;oBACtB,IAAA,yBAAc,EAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;gBAC9C,CAAC;aACF;YACD;gBACE,IAAI,EAAE,cAAc;gBACpB,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,UAAC,GAAG,EAAE,GAAG,EAAE,IAAI;oBACtB,IAAA,iBAAU,EAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;gBAC1C,CAAC;aACF;qBAEH,CAAA;AACJ,CAAC,EAzBoD,CAyBpD,CAAC;AAEF,kBAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverModule.js","sourceRoot":"","sources":["../../src/mocks/serverModule.js"],"names":[],"mappings":";AAAA,MAAM,CAAC,OAAO,GAAG,EAAE,CAAA"}
|
package/dist/routes/webhooks.js
CHANGED
|
@@ -6,14 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.stripeWebhooks = void 0;
|
|
7
7
|
var stripe_1 = __importDefault(require("stripe"));
|
|
8
8
|
var stripeWebhooks = function (req, res, next, stripeConfig) {
|
|
9
|
-
var stripeSecretKey = stripeConfig.stripeSecretKey,
|
|
10
|
-
if (webhooks &&
|
|
9
|
+
var stripeSecretKey = stripeConfig.stripeSecretKey, stripeWebhooksEndpointSecret = stripeConfig.stripeWebhooksEndpointSecret, webhooks = stripeConfig.webhooks;
|
|
10
|
+
if (webhooks && stripeWebhooksEndpointSecret) {
|
|
11
11
|
var stripe = new stripe_1.default(stripeSecretKey, { apiVersion: '2022-08-01' });
|
|
12
12
|
var stripeSignature = req.headers['stripe-signature'];
|
|
13
13
|
if (stripeSignature) {
|
|
14
14
|
var event_1;
|
|
15
15
|
try {
|
|
16
|
-
event_1 = stripe.webhooks.constructEvent(req.body, stripeSignature,
|
|
16
|
+
event_1 = stripe.webhooks.constructEvent(req.body, stripeSignature, stripeWebhooksEndpointSecret);
|
|
17
17
|
}
|
|
18
18
|
catch (err) {
|
|
19
19
|
res.status(400).send("Webhook Error: ".concat(err.message));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/routes/webhooks.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4B;AAKrB,IAAM,cAAc,GAAG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C;AApCW,QAAA,cAAc,kBAoCzB"}
|
package/dist/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import Stripe from "stripe";
|
|
|
2
2
|
export declare type StripeWebhookHandler = (event: any, stripe: Stripe, stripeConfig: StripeConfig) => void;
|
|
3
3
|
export declare type StripeConfig = {
|
|
4
4
|
stripeSecretKey: string;
|
|
5
|
-
|
|
5
|
+
stripeWebhooksEndpointSecret?: string;
|
|
6
6
|
webhooks?: {
|
|
7
7
|
[webhookName: string]: StripeWebhookHandler;
|
|
8
8
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-stripe",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"homepage:": "https://payloadcms.com",
|
|
5
5
|
"repository": "git@github.com:payloadcms/plugin-stripe.git",
|
|
6
6
|
"description": "Stripe plugin for Payload CMS",
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
"cms",
|
|
17
17
|
"plugin",
|
|
18
18
|
"typescript",
|
|
19
|
-
"payments"
|
|
19
|
+
"payments",
|
|
20
|
+
"saas",
|
|
21
|
+
"subscriptions",
|
|
22
|
+
"licensing"
|
|
20
23
|
],
|
|
21
24
|
"author": "dev@payloadcms.com",
|
|
22
25
|
"license": "MIT",
|