@apexara/stripe 1.0.2
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 +1003 -0
- package/dist/apex-stripe.d.ts +23 -0
- package/dist/apex-stripe.d.ts.map +1 -0
- package/dist/apex-stripe.js +41 -0
- package/dist/client.d.ts +7 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +14 -0
- package/dist/errors/http-error.d.ts +7 -0
- package/dist/errors/http-error.d.ts.map +1 -0
- package/dist/errors/http-error.js +14 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/models/config.model.d.ts +31 -0
- package/dist/models/config.model.d.ts.map +1 -0
- package/dist/models/config.model.js +2 -0
- package/dist/models/index.d.ts +5 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +20 -0
- package/dist/models/stripe.model.d.ts +69 -0
- package/dist/models/stripe.model.d.ts.map +1 -0
- package/dist/models/stripe.model.js +2 -0
- package/dist/models/subscription.model.d.ts +24 -0
- package/dist/models/subscription.model.d.ts.map +1 -0
- package/dist/models/subscription.model.js +2 -0
- package/dist/models/webhook.model.d.ts +43 -0
- package/dist/models/webhook.model.d.ts.map +1 -0
- package/dist/models/webhook.model.js +2 -0
- package/dist/services/checkout/checkout.service.d.ts +31 -0
- package/dist/services/checkout/checkout.service.d.ts.map +1 -0
- package/dist/services/checkout/checkout.service.js +119 -0
- package/dist/services/connected-account/connected-account.service.d.ts +25 -0
- package/dist/services/connected-account/connected-account.service.d.ts.map +1 -0
- package/dist/services/connected-account/connected-account.service.js +46 -0
- package/dist/services/customer/customer.service.d.ts +14 -0
- package/dist/services/customer/customer.service.d.ts.map +1 -0
- package/dist/services/customer/customer.service.js +23 -0
- package/dist/services/invoice/invoice.service.d.ts +24 -0
- package/dist/services/invoice/invoice.service.d.ts.map +1 -0
- package/dist/services/invoice/invoice.service.js +65 -0
- package/dist/services/payment-link/payment-link.service.d.ts +18 -0
- package/dist/services/payment-link/payment-link.service.d.ts.map +1 -0
- package/dist/services/payment-link/payment-link.service.js +53 -0
- package/dist/services/payment-method/payment-method.service.d.ts +43 -0
- package/dist/services/payment-method/payment-method.service.d.ts.map +1 -0
- package/dist/services/payment-method/payment-method.service.js +89 -0
- package/dist/services/price/price.service.d.ts +17 -0
- package/dist/services/price/price.service.d.ts.map +1 -0
- package/dist/services/price/price.service.js +32 -0
- package/dist/services/subscription/subscription.service.d.ts +89 -0
- package/dist/services/subscription/subscription.service.d.ts.map +1 -0
- package/dist/services/subscription/subscription.service.js +272 -0
- package/dist/tests/helpers.d.ts +5 -0
- package/dist/tests/helpers.d.ts.map +1 -0
- package/dist/tests/helpers.js +96 -0
- package/dist/utils/helpers.d.ts +15 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +41 -0
- package/dist/webhooks/webhook.dispatcher.d.ts +25 -0
- package/dist/webhooks/webhook.dispatcher.d.ts.map +1 -0
- package/dist/webhooks/webhook.dispatcher.js +105 -0
- package/dist/webhooks/webhook.handler.d.ts +39 -0
- package/dist/webhooks/webhook.handler.d.ts.map +1 -0
- package/dist/webhooks/webhook.handler.js +70 -0
- package/package.json +57 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StripeWebhookHandler = void 0;
|
|
7
|
+
const stripe_1 = __importDefault(require("stripe"));
|
|
8
|
+
const webhook_dispatcher_1 = require("./webhook.dispatcher");
|
|
9
|
+
class StripeWebhookHandler {
|
|
10
|
+
constructor(hooks, stripe, config, checkoutService, invoiceService) {
|
|
11
|
+
this.hooks = hooks;
|
|
12
|
+
this.stripe = stripe;
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.checkoutService = checkoutService;
|
|
15
|
+
this.invoiceService = invoiceService;
|
|
16
|
+
/**
|
|
17
|
+
* Express request handler for the main Stripe webhook endpoint.
|
|
18
|
+
* Requires the raw request body on req (use express.raw() middleware before this route).
|
|
19
|
+
* Pass next to delegate errors to Express error middleware; omit it for standalone HTTP error responses.
|
|
20
|
+
*/
|
|
21
|
+
this.handle = async (req, res, next) => {
|
|
22
|
+
await this.processWebhook(req, res, next, (payload, sig) => this.stripe.webhooks.constructEvent(payload, sig, this.config.webhookSecret), (event) => this.dispatcher.dispatchMainEvent(event));
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Express request handler for the Stripe connected account webhook endpoint.
|
|
26
|
+
* Requires the raw request body on req (use express.raw() middleware before this route).
|
|
27
|
+
* Throws if connectedWebhookSecret is not configured.
|
|
28
|
+
*/
|
|
29
|
+
this.handleConnected = async (req, res, next) => {
|
|
30
|
+
await this.processWebhook(req, res, next, (payload, sig) => {
|
|
31
|
+
if (!this.config.connectedWebhookSecret) {
|
|
32
|
+
throw new Error('connectedWebhookSecret is not configured');
|
|
33
|
+
}
|
|
34
|
+
return this.stripe.webhooks.constructEvent(payload, sig, this.config.connectedWebhookSecret);
|
|
35
|
+
}, (event) => this.dispatcher.dispatchConnectedEvent(event));
|
|
36
|
+
};
|
|
37
|
+
this.dispatcher = new webhook_dispatcher_1.WebhookDispatcher(hooks, checkoutService, invoiceService);
|
|
38
|
+
}
|
|
39
|
+
async processWebhook(req, res, next, constructEvent, dispatch) {
|
|
40
|
+
const payload = req.rawBody ?? req.body;
|
|
41
|
+
const signature = req.headers['stripe-signature'];
|
|
42
|
+
try {
|
|
43
|
+
const event = constructEvent(payload, signature);
|
|
44
|
+
// Idempotency check — skip if already processed (atomic insert)
|
|
45
|
+
const isNewEvent = await this.hooks.eventStore.tryInsert(event.id, event.type);
|
|
46
|
+
if (!isNewEvent) {
|
|
47
|
+
res.status(200).json();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await dispatch(event);
|
|
51
|
+
res.status(200).json();
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
if (next) {
|
|
55
|
+
// Express mode — delegate to the app's error middleware
|
|
56
|
+
next(err);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Standalone mode — handle the error and respond ourselves
|
|
60
|
+
const status = this.isVerificationError(err) ? 400 : 500;
|
|
61
|
+
const message = err instanceof Error ? err.message : 'Webhook processing error';
|
|
62
|
+
res.status(status).json({ error: message });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
isVerificationError(err) {
|
|
67
|
+
return err instanceof stripe_1.default.errors.StripeSignatureVerificationError;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.StripeWebhookHandler = StripeWebhookHandler;
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@apexara/stripe",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Stripe service layer and webhook dispatcher",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p tsconfig.build.json",
|
|
15
|
+
"build:watch": "tsc -p tsconfig.build.json --watch",
|
|
16
|
+
"type-check": "tsc --noEmit",
|
|
17
|
+
"test": "vitest run",
|
|
18
|
+
"test:watch": "vitest",
|
|
19
|
+
"test:coverage": "vitest run --coverage"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"stripe",
|
|
26
|
+
"subscriptions",
|
|
27
|
+
"webhooks"
|
|
28
|
+
],
|
|
29
|
+
"author": "Apexara LLC",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"private": false,
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"stripe": "^18.5.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@types/express": "^5.0.0",
|
|
40
|
+
"express": "^5.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"express": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"@types/express": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/express": "^5.0.0",
|
|
52
|
+
"@types/node": "^22.0.0",
|
|
53
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
54
|
+
"typescript": "^5.0.0",
|
|
55
|
+
"vitest": "^3.2.4"
|
|
56
|
+
}
|
|
57
|
+
}
|