@darraghor/nest-backend-libs 5.7.4 → 6.0.0
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 +87 -36
- package/dist/cli/commands/WriteApiClientCommand.js +1 -2
- package/dist/cli/commands/WriteApiClientCommand.js.map +1 -1
- package/dist/database-sqlite/SqliteTypeOrmConfigurationProvider.js +5 -4
- package/dist/database-sqlite/SqliteTypeOrmConfigurationProvider.js.map +1 -1
- package/dist/exports/authorization.d.ts +14 -0
- package/dist/exports/authorization.js +12 -0
- package/dist/exports/authorization.js.map +1 -0
- package/dist/exports/core.d.ts +11 -0
- package/dist/exports/core.js +10 -0
- package/dist/exports/core.js.map +1 -0
- package/dist/exports/database.d.ts +5 -0
- package/dist/exports/database.js +6 -0
- package/dist/exports/database.js.map +1 -0
- package/dist/exports/email.d.ts +4 -0
- package/dist/exports/email.js +4 -0
- package/dist/exports/email.js.map +1 -0
- package/dist/exports/invitations.d.ts +6 -0
- package/dist/exports/invitations.js +6 -0
- package/dist/exports/invitations.js.map +1 -0
- package/dist/exports/organisations.d.ts +11 -0
- package/dist/exports/organisations.js +12 -0
- package/dist/exports/organisations.js.map +1 -0
- package/dist/exports/stripe-legacy.d.ts +6 -0
- package/dist/exports/stripe-legacy.js +7 -0
- package/dist/exports/stripe-legacy.js.map +1 -0
- package/dist/exports/stripe.d.ts +11 -0
- package/dist/exports/stripe.js +9 -0
- package/dist/exports/stripe.js.map +1 -0
- package/dist/exports/twitter.d.ts +5 -0
- package/dist/exports/twitter.js +5 -0
- package/dist/exports/twitter.js.map +1 -0
- package/dist/exports/users.d.ts +10 -0
- package/dist/exports/users.js +11 -0
- package/dist/exports/users.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -1
- package/dist/organisation-subscriptions/entities/organisation-subscription.entity.js +2 -1
- package/dist/organisation-subscriptions/entities/organisation-subscription.entity.js.map +1 -1
- package/dist/stripe-client/StripeClientConfigurationService.js +1 -0
- package/dist/stripe-client/StripeClientConfigurationService.js.map +1 -1
- package/dist/stripe-client/StripeClientProvider.js +1 -1
- package/dist/stripe-client/StripeClientProvider.js.map +1 -1
- package/dist/stripe-client/controllers/stripe-checkout-controller.d.ts +1 -0
- package/dist/stripe-client/controllers/stripe-checkout-controller.js +1 -0
- package/dist/stripe-client/controllers/stripe-checkout-controller.js.map +1 -1
- package/dist/stripe-client/controllers/stripe-events-controller.js +1 -0
- package/dist/stripe-client/controllers/stripe-events-controller.js.map +1 -1
- package/dist/stripe-client/controllers/stripe-unauthenticated-checkout-controller.js +1 -0
- package/dist/stripe-client/controllers/stripe-unauthenticated-checkout-controller.js.map +1 -1
- package/dist/stripe-client/controllers/stripe-webhook-controller.js +1 -4
- package/dist/stripe-client/controllers/stripe-webhook-controller.js.map +1 -1
- package/dist/stripe-client/services/auth-stripe-checkout.service.js +1 -0
- package/dist/stripe-client/services/auth-stripe-checkout.service.js.map +1 -1
- package/dist/stripe-client/services/queued-payment-event.handler.d.ts +1 -0
- package/dist/stripe-client/services/queued-payment-event.handler.js +2 -0
- package/dist/stripe-client/services/queued-payment-event.handler.js.map +1 -1
- package/dist/stripe-client/services/stripe-checkout.service.d.ts +1 -0
- package/dist/stripe-client/services/stripe-checkout.service.js +2 -0
- package/dist/stripe-client/services/stripe-checkout.service.js.map +1 -1
- package/dist/stripe-client/stripe-account.module.d.ts +1 -0
- package/dist/stripe-client/stripe-account.module.js +2 -0
- package/dist/stripe-client/stripe-account.module.js.map +1 -1
- package/dist/stripe-client/stripe-account.options.d.ts +2 -0
- package/dist/stripe-client/stripe-account.options.js.map +1 -1
- package/dist/stripe-payments/controllers/secure-stripe-checkout.controller.d.ts +10 -0
- package/dist/stripe-payments/controllers/secure-stripe-checkout.controller.js +65 -0
- package/dist/stripe-payments/controllers/secure-stripe-checkout.controller.js.map +1 -0
- package/dist/stripe-payments/controllers/secure-stripe-events.controller.d.ts +9 -0
- package/dist/stripe-payments/controllers/secure-stripe-events.controller.js +65 -0
- package/dist/stripe-payments/controllers/secure-stripe-events.controller.js.map +1 -0
- package/dist/stripe-payments/controllers/secure-stripe-webhook.controller.d.ts +14 -0
- package/dist/stripe-payments/controllers/secure-stripe-webhook.controller.js +112 -0
- package/dist/stripe-payments/controllers/secure-stripe-webhook.controller.js.map +1 -0
- package/dist/stripe-payments/entities/stripe-checkout-attempt.entity.d.ts +15 -0
- package/dist/stripe-payments/entities/stripe-checkout-attempt.entity.js +88 -0
- package/dist/stripe-payments/entities/stripe-checkout-attempt.entity.js.map +1 -0
- package/dist/stripe-payments/entities/stripe-payment-event.entity.d.ts +16 -0
- package/dist/stripe-payments/entities/stripe-payment-event.entity.js +101 -0
- package/dist/stripe-payments/entities/stripe-payment-event.entity.js.map +1 -0
- package/dist/stripe-payments/entities/stripe-payment-state.entity.d.ts +9 -0
- package/dist/stripe-payments/entities/stripe-payment-state.entity.js +53 -0
- package/dist/stripe-payments/entities/stripe-payment-state.entity.js.map +1 -0
- package/dist/stripe-payments/models/secure-checkout.dto.d.ts +14 -0
- package/dist/stripe-payments/models/secure-checkout.dto.js +105 -0
- package/dist/stripe-payments/models/secure-checkout.dto.js.map +1 -0
- package/dist/stripe-payments/models/secure-payment-response.dto.d.ts +7 -0
- package/dist/stripe-payments/models/secure-payment-response.dto.js +30 -0
- package/dist/stripe-payments/models/secure-payment-response.dto.js.map +1 -0
- package/dist/stripe-payments/services/default-stripe-payments-entitlement-store.d.ts +12 -0
- package/dist/stripe-payments/services/default-stripe-payments-entitlement-store.js +49 -0
- package/dist/stripe-payments/services/default-stripe-payments-entitlement-store.js.map +1 -0
- package/dist/stripe-payments/services/secure-stripe-checkout.service.d.ts +24 -0
- package/dist/stripe-payments/services/secure-stripe-checkout.service.js +213 -0
- package/dist/stripe-payments/services/secure-stripe-checkout.service.js.map +1 -0
- package/dist/stripe-payments/services/secure-stripe-client.provider.d.ts +2 -0
- package/dist/stripe-payments/services/secure-stripe-client.provider.js +15 -0
- package/dist/stripe-payments/services/secure-stripe-client.provider.js.map +1 -0
- package/dist/stripe-payments/services/secure-stripe-product.service.d.ts +9 -0
- package/dist/stripe-payments/services/secure-stripe-product.service.js +54 -0
- package/dist/stripe-payments/services/secure-stripe-product.service.js.map +1 -0
- package/dist/stripe-payments/services/secure-stripe-webhook.service.d.ts +14 -0
- package/dist/stripe-payments/services/secure-stripe-webhook.service.js +58 -0
- package/dist/stripe-payments/services/secure-stripe-webhook.service.js.map +1 -0
- package/dist/stripe-payments/services/secure-subscription.service.d.ts +18 -0
- package/dist/stripe-payments/services/secure-subscription.service.js +217 -0
- package/dist/stripe-payments/services/secure-subscription.service.js.map +1 -0
- package/dist/stripe-payments/services/stripe-payment-event.processor.d.ts +15 -0
- package/dist/stripe-payments/services/stripe-payment-event.processor.js +78 -0
- package/dist/stripe-payments/services/stripe-payment-event.processor.js.map +1 -0
- package/dist/stripe-payments/services/stripe-payment-event.service.d.ts +20 -0
- package/dist/stripe-payments/services/stripe-payment-event.service.js +178 -0
- package/dist/stripe-payments/services/stripe-payment-event.service.js.map +1 -0
- package/dist/stripe-payments/services/stripe-payment-fulfillment.service.d.ts +31 -0
- package/dist/stripe-payments/services/stripe-payment-fulfillment.service.js +331 -0
- package/dist/stripe-payments/services/stripe-payment-fulfillment.service.js.map +1 -0
- package/dist/stripe-payments/services/stripe-payments-operations.service.d.ts +25 -0
- package/dist/stripe-payments/services/stripe-payments-operations.service.js +59 -0
- package/dist/stripe-payments/services/stripe-payments-operations.service.js.map +1 -0
- package/dist/stripe-payments/services/stripe-payments-telemetry.service.d.ts +7 -0
- package/dist/stripe-payments/services/stripe-payments-telemetry.service.js +40 -0
- package/dist/stripe-payments/services/stripe-payments-telemetry.service.js.map +1 -0
- package/dist/stripe-payments/stripe-payment.config.d.ts +19 -0
- package/dist/stripe-payments/stripe-payment.config.js +98 -0
- package/dist/stripe-payments/stripe-payment.config.js.map +1 -0
- package/dist/stripe-payments/stripe-payment.config.test.d.ts +1 -0
- package/dist/stripe-payments/stripe-payment.config.test.js +59 -0
- package/dist/stripe-payments/stripe-payment.config.test.js.map +1 -0
- package/dist/stripe-payments/stripe-payments.extensions.d.ts +69 -0
- package/dist/stripe-payments/stripe-payments.extensions.js +30 -0
- package/dist/stripe-payments/stripe-payments.extensions.js.map +1 -0
- package/dist/stripe-payments/stripe-payments.module.d.ts +12 -0
- package/dist/stripe-payments/stripe-payments.module.js +118 -0
- package/dist/stripe-payments/stripe-payments.module.js.map +1 -0
- package/dist/stripe-payments/stripe-payments.options.d.ts +21 -0
- package/dist/stripe-payments/stripe-payments.options.js +2 -0
- package/dist/stripe-payments/stripe-payments.options.js.map +1 -0
- package/dist/typeorm/entities.d.ts +15 -0
- package/dist/typeorm/entities.js +32 -0
- package/dist/typeorm/entities.js.map +1 -0
- package/package.json +85 -38
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var StripePaymentFulfillmentService_1;
|
|
14
|
+
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
|
+
import Stripe from "stripe";
|
|
16
|
+
import { InjectRepository } from "@nestjs/typeorm";
|
|
17
|
+
import { Repository } from "typeorm";
|
|
18
|
+
import { StripeCheckoutAttempt } from "../entities/stripe-checkout-attempt.entity.js";
|
|
19
|
+
import { PaymentSessionService } from "../../payment-sessions/payment-session.service.js";
|
|
20
|
+
import { SecureStripeProductService } from "./secure-stripe-product.service.js";
|
|
21
|
+
import { STRIPE_PAYMENTS_ENTITLEMENT_STORE, } from "../stripe-payments.extensions.js";
|
|
22
|
+
function getMetadata(value) {
|
|
23
|
+
return value.metadata ?? {};
|
|
24
|
+
}
|
|
25
|
+
function getCustomer(customer) {
|
|
26
|
+
if (typeof customer === "string") {
|
|
27
|
+
return { id: customer, email: "unknown" };
|
|
28
|
+
}
|
|
29
|
+
if (!customer || "deleted" in customer) {
|
|
30
|
+
return { id: customer?.id ?? "unknown", email: "unknown" };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
id: customer.id,
|
|
34
|
+
email: customer.email ?? "unknown",
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function addGracePeriod(date) {
|
|
38
|
+
return new Date(date.getTime() + 2 * 24 * 60 * 60 * 1000);
|
|
39
|
+
}
|
|
40
|
+
function getLifetimeDate() {
|
|
41
|
+
const lifetime = new Date();
|
|
42
|
+
lifetime.setFullYear(lifetime.getFullYear() + 500);
|
|
43
|
+
return lifetime;
|
|
44
|
+
}
|
|
45
|
+
let StripePaymentFulfillmentService = StripePaymentFulfillmentService_1 = class StripePaymentFulfillmentService {
|
|
46
|
+
stripe;
|
|
47
|
+
attemptRepository;
|
|
48
|
+
paymentSessionService;
|
|
49
|
+
productService;
|
|
50
|
+
entitlementStore;
|
|
51
|
+
logger = new Logger(StripePaymentFulfillmentService_1.name);
|
|
52
|
+
constructor(stripe, attemptRepository, paymentSessionService, productService, entitlementStore) {
|
|
53
|
+
this.stripe = stripe;
|
|
54
|
+
this.attemptRepository = attemptRepository;
|
|
55
|
+
this.paymentSessionService = paymentSessionService;
|
|
56
|
+
this.productService = productService;
|
|
57
|
+
this.entitlementStore = entitlementStore;
|
|
58
|
+
}
|
|
59
|
+
async process(event) {
|
|
60
|
+
switch (event.type) {
|
|
61
|
+
case "checkout.session.completed":
|
|
62
|
+
await this.processCheckoutSession(event, false);
|
|
63
|
+
return;
|
|
64
|
+
case "checkout.session.async_payment_succeeded":
|
|
65
|
+
await this.processCheckoutSession(event, true);
|
|
66
|
+
return;
|
|
67
|
+
case "checkout.session.async_payment_failed":
|
|
68
|
+
case "checkout.session.expired":
|
|
69
|
+
await this.markCheckoutAttemptFailed(event);
|
|
70
|
+
return;
|
|
71
|
+
case "customer.subscription.created":
|
|
72
|
+
case "customer.subscription.updated":
|
|
73
|
+
case "customer.subscription.deleted":
|
|
74
|
+
case "customer.subscription.paused":
|
|
75
|
+
case "customer.subscription.resumed":
|
|
76
|
+
await this.processSubscriptionEvent(event);
|
|
77
|
+
return;
|
|
78
|
+
case "invoice.paid":
|
|
79
|
+
case "invoice.payment_failed":
|
|
80
|
+
await this.processInvoiceEvent(event);
|
|
81
|
+
return;
|
|
82
|
+
case "charge.refunded":
|
|
83
|
+
await this.processRefundEvent(event);
|
|
84
|
+
return;
|
|
85
|
+
case "charge.dispute.created":
|
|
86
|
+
await this.processDisputeEvent(event);
|
|
87
|
+
return;
|
|
88
|
+
default:
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async processCheckoutSession(event, isAsyncPayment) {
|
|
93
|
+
const sessionEvent = event.data.object;
|
|
94
|
+
const session = await this.stripe.checkout.sessions.retrieve(sessionEvent.id, {
|
|
95
|
+
expand: [
|
|
96
|
+
"line_items.data.price.product",
|
|
97
|
+
"customer",
|
|
98
|
+
"subscription",
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
if (!isAsyncPayment && session.payment_status !== "paid") {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const lineItems = session.line_items?.data ?? [];
|
|
105
|
+
if (lineItems.length !== 1 || lineItems[0]?.quantity !== 1) {
|
|
106
|
+
throw new Error("Checkout session did not contain the expected product");
|
|
107
|
+
}
|
|
108
|
+
const lineItem = lineItems[0];
|
|
109
|
+
const price = lineItem.price;
|
|
110
|
+
if (!price || typeof price === "string") {
|
|
111
|
+
throw new Error("Checkout session price was not expanded");
|
|
112
|
+
}
|
|
113
|
+
const configuredProduct = this.productService.getByPriceId(price.id);
|
|
114
|
+
const metadata = getMetadata(session);
|
|
115
|
+
if (metadata.productKey &&
|
|
116
|
+
metadata.productKey !== configuredProduct.key) {
|
|
117
|
+
throw new Error("Checkout metadata did not match the configured product");
|
|
118
|
+
}
|
|
119
|
+
const organisationUuid = await this.resolveOrganisationUuid(session, metadata);
|
|
120
|
+
const customer = getCustomer(session.customer);
|
|
121
|
+
const subscriptionId = this.getSubscriptionId(session.subscription);
|
|
122
|
+
let validUntil = getLifetimeDate();
|
|
123
|
+
if (configuredProduct.mode === "subscription") {
|
|
124
|
+
if (!subscriptionId) {
|
|
125
|
+
throw new Error("Subscription checkout did not create a subscription");
|
|
126
|
+
}
|
|
127
|
+
const subscription = await this.retrieveSubscription(subscriptionId);
|
|
128
|
+
validUntil = this.getSubscriptionValidUntil(subscription);
|
|
129
|
+
}
|
|
130
|
+
const productObject = typeof price.product === "string" ? undefined : price.product;
|
|
131
|
+
const input = {
|
|
132
|
+
paymentSystemTransactionId: subscriptionId ?? session.id,
|
|
133
|
+
paymentSystemProductId: productObject?.id ?? price.id,
|
|
134
|
+
paymentSystemCustomerId: customer.id,
|
|
135
|
+
paymentSystemCustomerEmail: customer.email !== "unknown"
|
|
136
|
+
? customer.email
|
|
137
|
+
: (session.customer_details?.email ?? "unknown"),
|
|
138
|
+
paymentSystemMode: configuredProduct.mode,
|
|
139
|
+
paymentSystemName: "stripe",
|
|
140
|
+
validUntil,
|
|
141
|
+
internalSku: configuredProduct.internalSku,
|
|
142
|
+
productDisplayName: productObject && !("deleted" in productObject)
|
|
143
|
+
? productObject.name
|
|
144
|
+
: configuredProduct.displayName,
|
|
145
|
+
organisationUuid,
|
|
146
|
+
stripeEventId: event.id,
|
|
147
|
+
stripeEventCreatedAt: this.eventCreatedAt(event),
|
|
148
|
+
stripeStatus: configuredProduct.mode === "subscription" ? "active" : "paid",
|
|
149
|
+
};
|
|
150
|
+
await this.entitlementStore.save(input);
|
|
151
|
+
}
|
|
152
|
+
async markCheckoutAttemptFailed(event) {
|
|
153
|
+
const session = event.data.object;
|
|
154
|
+
await this.attemptRepository.update({ stripeSessionId: session.id }, {
|
|
155
|
+
status: "failed",
|
|
156
|
+
errorMessage: event.type === "checkout.session.expired"
|
|
157
|
+
? "Checkout session expired"
|
|
158
|
+
: "Asynchronous payment failed",
|
|
159
|
+
});
|
|
160
|
+
this.logger.warn("Stripe Checkout attempt did not complete", {
|
|
161
|
+
eventId: event.id,
|
|
162
|
+
sessionId: session.id,
|
|
163
|
+
eventType: event.type,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async processSubscriptionEvent(event) {
|
|
167
|
+
const subscriptionEvent = event.data.object;
|
|
168
|
+
const subscription = await this.retrieveSubscription(subscriptionEvent.id);
|
|
169
|
+
await this.saveSubscription(subscription, event);
|
|
170
|
+
}
|
|
171
|
+
async processInvoiceEvent(event) {
|
|
172
|
+
const invoice = event.data.object;
|
|
173
|
+
const parent = invoice.parent;
|
|
174
|
+
const subscriptionId = parent?.type === "subscription_details"
|
|
175
|
+
? this.getSubscriptionId(parent.subscription)
|
|
176
|
+
: undefined;
|
|
177
|
+
if (!subscriptionId) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const subscription = await this.retrieveSubscription(subscriptionId);
|
|
181
|
+
await this.saveSubscription(subscription, event);
|
|
182
|
+
}
|
|
183
|
+
async processRefundEvent(event) {
|
|
184
|
+
const charge = event.data.object;
|
|
185
|
+
await this.revokeChargeEntitlement(charge, event, "refunded");
|
|
186
|
+
}
|
|
187
|
+
async processDisputeEvent(event) {
|
|
188
|
+
const dispute = event.data.object;
|
|
189
|
+
const chargeId = this.getId(dispute.charge);
|
|
190
|
+
if (!chargeId) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const charge = await this.stripe.charges.retrieve(chargeId);
|
|
194
|
+
await this.revokeChargeEntitlement(charge, event, "disputed");
|
|
195
|
+
}
|
|
196
|
+
async revokeChargeEntitlement(charge, event, status) {
|
|
197
|
+
const input = {
|
|
198
|
+
stripeEventId: event.id,
|
|
199
|
+
stripeEventCreatedAt: this.eventCreatedAt(event),
|
|
200
|
+
stripeStatus: status,
|
|
201
|
+
};
|
|
202
|
+
const paymentIntentId = this.getId(charge.payment_intent);
|
|
203
|
+
if (paymentIntentId) {
|
|
204
|
+
const paymentIntent = await this.stripe.paymentIntents.retrieve(paymentIntentId);
|
|
205
|
+
const paymentIntentMetadata = paymentIntent.metadata;
|
|
206
|
+
const checkoutAttemptId = paymentIntentMetadata.checkoutAttemptId;
|
|
207
|
+
if (/^\d+$/.test(checkoutAttemptId ?? "")) {
|
|
208
|
+
const attempt = await this.attemptRepository.findOne({
|
|
209
|
+
where: { id: Number(checkoutAttemptId) },
|
|
210
|
+
});
|
|
211
|
+
if (attempt?.stripeSessionId) {
|
|
212
|
+
const revoked = await this.entitlementStore.revokePayment(attempt.stripeSessionId, input);
|
|
213
|
+
if (revoked) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const customerId = getCustomer(charge.customer).id;
|
|
220
|
+
if (customerId !== "unknown") {
|
|
221
|
+
await this.entitlementStore.revokeCustomerPayments(customerId, input);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async saveSubscription(subscription, event) {
|
|
225
|
+
if (subscription.items.data.length === 0) {
|
|
226
|
+
throw new Error("Subscription has no line items");
|
|
227
|
+
}
|
|
228
|
+
const firstItem = subscription.items.data[0];
|
|
229
|
+
const price = firstItem.price;
|
|
230
|
+
if (typeof price === "string") {
|
|
231
|
+
throw new Error("Subscription price was not expanded");
|
|
232
|
+
}
|
|
233
|
+
const configuredProduct = this.productService.getByPriceId(price.id);
|
|
234
|
+
const metadata = getMetadata(subscription);
|
|
235
|
+
const existing = await this.entitlementStore.findByTransactionId(subscription.id);
|
|
236
|
+
const organisationUuid = metadata.organisationUuid ?? existing?.organisationUuid;
|
|
237
|
+
if (!organisationUuid) {
|
|
238
|
+
throw new Error("Subscription event could not be matched to an organisation");
|
|
239
|
+
}
|
|
240
|
+
const customer = getCustomer(subscription.customer);
|
|
241
|
+
const productObject = typeof price.product === "string" ? undefined : price.product;
|
|
242
|
+
const status = event.type === "customer.subscription.deleted"
|
|
243
|
+
? "canceled"
|
|
244
|
+
: subscription.status;
|
|
245
|
+
await this.entitlementStore.save({
|
|
246
|
+
paymentSystemTransactionId: subscription.id,
|
|
247
|
+
paymentSystemProductId: productObject?.id ?? price.id,
|
|
248
|
+
paymentSystemCustomerId: customer.id,
|
|
249
|
+
paymentSystemCustomerEmail: customer.email,
|
|
250
|
+
paymentSystemMode: "subscription",
|
|
251
|
+
paymentSystemName: "stripe",
|
|
252
|
+
validUntil: this.getSubscriptionValidUntil(subscription, status),
|
|
253
|
+
internalSku: configuredProduct.internalSku,
|
|
254
|
+
productDisplayName: productObject && !("deleted" in productObject)
|
|
255
|
+
? productObject.name
|
|
256
|
+
: configuredProduct.displayName,
|
|
257
|
+
organisationUuid,
|
|
258
|
+
stripeEventId: event.id,
|
|
259
|
+
stripeEventCreatedAt: this.eventCreatedAt(event),
|
|
260
|
+
stripeStatus: status,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
async resolveOrganisationUuid(session, metadata) {
|
|
264
|
+
if (metadata.organisationUuid) {
|
|
265
|
+
return metadata.organisationUuid;
|
|
266
|
+
}
|
|
267
|
+
const attempt = await this.attemptRepository.findOne({
|
|
268
|
+
where: { stripeSessionId: session.id },
|
|
269
|
+
});
|
|
270
|
+
if (attempt) {
|
|
271
|
+
return attempt.organisationUuid;
|
|
272
|
+
}
|
|
273
|
+
// Preserve compatibility with sessions created by the legacy shared
|
|
274
|
+
// Stripe module, which used a PaymentSessionReference UUID as the
|
|
275
|
+
// Checkout client_reference_id.
|
|
276
|
+
const referenceId = this.getClientReferenceId(session);
|
|
277
|
+
if (referenceId) {
|
|
278
|
+
const paymentReference = await this.paymentSessionService.findSessionByUuid(referenceId);
|
|
279
|
+
if (paymentReference?.organisationUuid) {
|
|
280
|
+
return paymentReference.organisationUuid;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
throw new Error("Checkout session could not be matched to an organisation");
|
|
284
|
+
}
|
|
285
|
+
async retrieveSubscription(id) {
|
|
286
|
+
return this.stripe.subscriptions.retrieve(id, {
|
|
287
|
+
expand: ["items.data.price.product", "customer"],
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
getSubscriptionValidUntil(subscription, status = subscription.status) {
|
|
291
|
+
if (status === "canceled" ||
|
|
292
|
+
status === "unpaid" ||
|
|
293
|
+
status === "incomplete_expired" ||
|
|
294
|
+
status === "paused") {
|
|
295
|
+
return new Date();
|
|
296
|
+
}
|
|
297
|
+
const periodEnd = subscription.items.data[0]?.current_period_end;
|
|
298
|
+
if (!periodEnd) {
|
|
299
|
+
throw new Error("Stripe subscription has no current period end");
|
|
300
|
+
}
|
|
301
|
+
return addGracePeriod(new Date(periodEnd * 1000));
|
|
302
|
+
}
|
|
303
|
+
getSubscriptionId(subscription) {
|
|
304
|
+
return typeof subscription === "string"
|
|
305
|
+
? subscription
|
|
306
|
+
: subscription?.id;
|
|
307
|
+
}
|
|
308
|
+
getId(value) {
|
|
309
|
+
return typeof value === "string" ? value : value?.id;
|
|
310
|
+
}
|
|
311
|
+
getClientReferenceId(session) {
|
|
312
|
+
return typeof session.client_reference_id === "string"
|
|
313
|
+
? session.client_reference_id
|
|
314
|
+
: undefined;
|
|
315
|
+
}
|
|
316
|
+
eventCreatedAt(event) {
|
|
317
|
+
return new Date(event.created * 1000);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
StripePaymentFulfillmentService = StripePaymentFulfillmentService_1 = __decorate([
|
|
321
|
+
Injectable(),
|
|
322
|
+
__param(0, Inject("SecureStripeClient")),
|
|
323
|
+
__param(1, InjectRepository(StripeCheckoutAttempt)),
|
|
324
|
+
__param(4, Inject(STRIPE_PAYMENTS_ENTITLEMENT_STORE)),
|
|
325
|
+
__metadata("design:paramtypes", [Stripe,
|
|
326
|
+
Repository,
|
|
327
|
+
PaymentSessionService,
|
|
328
|
+
SecureStripeProductService, Object])
|
|
329
|
+
], StripePaymentFulfillmentService);
|
|
330
|
+
export { StripePaymentFulfillmentService };
|
|
331
|
+
//# sourceMappingURL=stripe-payment-fulfillment.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe-payment-fulfillment.service.js","sourceRoot":"","sources":["../../../src/stripe-payments/services/stripe-payment-fulfillment.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAC,MAAM,gBAAgB,CAAC;AAC1D,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AACnC,OAAO,EAAC,qBAAqB,EAAC,MAAM,+CAA+C,CAAC;AACpF,OAAO,EAAC,qBAAqB,EAAC,MAAM,mDAAmD,CAAC;AACxF,OAAO,EAAC,0BAA0B,EAAC,MAAM,oCAAoC,CAAC;AAC9E,OAAO,EACH,iCAAiC,GAGpC,MAAM,kCAAkC,CAAC;AAI1C,SAAS,WAAW,CAAC,KAEpB;IACG,OAAO,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAC;AAChC,CAAC;AAED,SAAS,WAAW,CAChB,QAAkE;IAElE,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,EAAC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|
|
2
|
+
import { Repository } from "typeorm";
|
|
3
|
+
import { StripeCheckoutEvent } from "../entities/stripe-payment-event.entity.js";
|
|
4
|
+
export interface StripePaymentsOperationsSnapshot {
|
|
5
|
+
status: "up" | "degraded";
|
|
6
|
+
checkedAt: string;
|
|
7
|
+
events: {
|
|
8
|
+
received: number;
|
|
9
|
+
processing: number;
|
|
10
|
+
failed: number;
|
|
11
|
+
staleProcessing: number;
|
|
12
|
+
};
|
|
13
|
+
queue: {
|
|
14
|
+
waiting: number;
|
|
15
|
+
active: number;
|
|
16
|
+
delayed: number;
|
|
17
|
+
failed: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export declare class StripePaymentsOperationsService {
|
|
21
|
+
private readonly eventRepository;
|
|
22
|
+
private readonly eventQueue;
|
|
23
|
+
constructor(eventRepository: Repository<StripeCheckoutEvent>, eventQueue: Queue);
|
|
24
|
+
getSnapshot(): Promise<StripePaymentsOperationsSnapshot>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { InjectQueue } from "@nestjs/bullmq";
|
|
14
|
+
import { Injectable } from "@nestjs/common";
|
|
15
|
+
import { InjectRepository } from "@nestjs/typeorm";
|
|
16
|
+
import { Queue } from "bullmq";
|
|
17
|
+
import { LessThan, Repository } from "typeorm";
|
|
18
|
+
import { StripeCheckoutEvent } from "../entities/stripe-payment-event.entity.js";
|
|
19
|
+
let StripePaymentsOperationsService = class StripePaymentsOperationsService {
|
|
20
|
+
eventRepository;
|
|
21
|
+
eventQueue;
|
|
22
|
+
constructor(eventRepository, eventQueue) {
|
|
23
|
+
this.eventRepository = eventRepository;
|
|
24
|
+
this.eventQueue = eventQueue;
|
|
25
|
+
}
|
|
26
|
+
async getSnapshot() {
|
|
27
|
+
const staleCutoff = new Date(Date.now() - 5 * 60 * 1000);
|
|
28
|
+
const [received, processing, failed, staleProcessing, queueCounts] = await Promise.all([
|
|
29
|
+
this.eventRepository.countBy({ status: "received" }),
|
|
30
|
+
this.eventRepository.countBy({ status: "processing" }),
|
|
31
|
+
this.eventRepository.countBy({ status: "failed" }),
|
|
32
|
+
this.eventRepository.countBy({
|
|
33
|
+
status: "processing",
|
|
34
|
+
processingStartedAt: LessThan(staleCutoff),
|
|
35
|
+
}),
|
|
36
|
+
this.eventQueue.getJobCounts("waiting", "active", "delayed", "failed"),
|
|
37
|
+
]);
|
|
38
|
+
return {
|
|
39
|
+
status: failed > 0 || staleProcessing > 0 ? "degraded" : "up",
|
|
40
|
+
checkedAt: new Date().toISOString(),
|
|
41
|
+
events: { received, processing, failed, staleProcessing },
|
|
42
|
+
queue: {
|
|
43
|
+
waiting: queueCounts.waiting,
|
|
44
|
+
active: queueCounts.active,
|
|
45
|
+
delayed: queueCounts.delayed,
|
|
46
|
+
failed: queueCounts.failed,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
StripePaymentsOperationsService = __decorate([
|
|
52
|
+
Injectable(),
|
|
53
|
+
__param(0, InjectRepository(StripeCheckoutEvent)),
|
|
54
|
+
__param(1, InjectQueue("stripe-events")),
|
|
55
|
+
__metadata("design:paramtypes", [Repository,
|
|
56
|
+
Queue])
|
|
57
|
+
], StripePaymentsOperationsService);
|
|
58
|
+
export { StripePaymentsOperationsService };
|
|
59
|
+
//# sourceMappingURL=stripe-payments-operations.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe-payments-operations.service.js","sourceRoot":"","sources":["../../../src/stripe-payments/services/stripe-payments-operations.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAC,UAAU,EAAC,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAC,KAAK,EAAC,MAAM,QAAQ,CAAC;AAC7B,OAAO,EAAC,QAAQ,EAAE,UAAU,EAAC,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAC,mBAAmB,EAAC,MAAM,4CAA4C,CAAC;AAoBxE,IAAM,+BAA+B,GAArC,MAAM,+BAA+B;IAGnB;IAC8B;IAHnD,YAEqB,eAAgD,EAClB,UAAiB;QAD/C,oBAAe,GAAf,eAAe,CAAiC;QAClB,eAAU,GAAV,UAAU,CAAO;IACjE,CAAC;IAEJ,KAAK,CAAC,WAAW;QACb,MAAM,WAAW,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,eAAe,EAAE,WAAW,CAAC,GAC9D,MAAM,OAAO,CAAC,GAAG,CAAC;YACd,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAC,MAAM,EAAE,UAAU,EAAC,CAAC;YAClD,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAC,MAAM,EAAE,YAAY,EAAC,CAAC;YACpD,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAC,CAAC;YAChD,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC;gBACzB,MAAM,EAAE,YAAY;gBACpB,mBAAmB,EAAE,QAAQ,CAAC,WAAW,CAAC;aAC7C,CAAC;YACF,IAAI,CAAC,UAAU,CAAC,YAAY,CACxB,SAAS,EACT,QAAQ,EACR,SAAS,EACT,QAAQ,CACX;SACJ,CAAC,CAAC;QAEP,OAAO;YACH,MAAM,EAAE,MAAM,GAAG,CAAC,IAAI,eAAe,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI;YAC7D,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,MAAM,EAAE,EAAC,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,eAAe,EAAC;YACvD,KAAK,EAAE;gBACH,OAAO,EAAE,WAAW,CAAC,OAAO;gBAC5B,MAAM,EAAE,WAAW,CAAC,MAAM;gBAC1B,OAAO,EAAE,WAAW,CAAC,OAAO;gBAC5B,MAAM,EAAE,WAAW,CAAC,MAAM;aAC7B;SACJ,CAAC;IACN,CAAC;CACJ,CAAA;AAtCY,+BAA+B;IAD3C,UAAU,EAAE;IAGJ,WAAA,gBAAgB,CAAC,mBAAmB,CAAC,CAAA;IAErC,WAAA,WAAW,CAAC,eAAe,CAAC,CAAA;qCADK,UAAU;QACe,KAAK;GAJ3D,+BAA+B,CAsC3C"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { StripePaymentsTelemetry, StripePaymentsTelemetryEvent } from "../stripe-payments.extensions.js";
|
|
2
|
+
export declare class StripePaymentsTelemetryService {
|
|
3
|
+
private readonly telemetry;
|
|
4
|
+
private readonly logger;
|
|
5
|
+
constructor(telemetry: StripePaymentsTelemetry);
|
|
6
|
+
record(event: StripePaymentsTelemetryEvent): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var StripePaymentsTelemetryService_1;
|
|
14
|
+
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
15
|
+
import { STRIPE_PAYMENTS_TELEMETRY, } from "../stripe-payments.extensions.js";
|
|
16
|
+
let StripePaymentsTelemetryService = StripePaymentsTelemetryService_1 = class StripePaymentsTelemetryService {
|
|
17
|
+
telemetry;
|
|
18
|
+
logger = new Logger(StripePaymentsTelemetryService_1.name);
|
|
19
|
+
constructor(telemetry) {
|
|
20
|
+
this.telemetry = telemetry;
|
|
21
|
+
}
|
|
22
|
+
async record(event) {
|
|
23
|
+
try {
|
|
24
|
+
await this.telemetry.record(event);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
this.logger.warn("Stripe payment telemetry sink failed", {
|
|
28
|
+
eventName: event.name,
|
|
29
|
+
error: error instanceof Error ? error.message : String(error),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
StripePaymentsTelemetryService = StripePaymentsTelemetryService_1 = __decorate([
|
|
35
|
+
Injectable(),
|
|
36
|
+
__param(0, Inject(STRIPE_PAYMENTS_TELEMETRY)),
|
|
37
|
+
__metadata("design:paramtypes", [Object])
|
|
38
|
+
], StripePaymentsTelemetryService);
|
|
39
|
+
export { StripePaymentsTelemetryService };
|
|
40
|
+
//# sourceMappingURL=stripe-payments-telemetry.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe-payments-telemetry.service.js","sourceRoot":"","sources":["../../../src/stripe-payments/services/stripe-payments-telemetry.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAC,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EACH,yBAAyB,GAG5B,MAAM,kCAAkC,CAAC;AAGnC,IAAM,8BAA8B,sCAApC,MAAM,8BAA8B;IAKlB;IAJJ,MAAM,GAAG,IAAI,MAAM,CAAC,gCAA8B,CAAC,IAAI,CAAC,CAAC;IAE1E,YAEqB,SAAkC;QAAlC,cAAS,GAAT,SAAS,CAAyB;IACpD,CAAC;IAEJ,KAAK,CAAC,MAAM,CAAC,KAAmC;QAC5C,IAAI,CAAC;YACD,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,sCAAsC,EAAE;gBACrD,SAAS,EAAE,KAAK,CAAC,IAAI;gBACrB,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAChE,CAAC,CAAC;QACP,CAAC;IACL,CAAC;CACJ,CAAA;AAlBY,8BAA8B;IAD1C,UAAU,EAAE;IAKJ,WAAA,MAAM,CAAC,yBAAyB,CAAC,CAAA;;GAJ7B,8BAA8B,CAkB1C"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Provider } from "@nestjs/common";
|
|
2
|
+
import { StripePaymentsModuleOptions, StripePaymentsMode, StripePaymentsProduct } from "./stripe-payments.options.js";
|
|
3
|
+
export interface SecureStripeProduct {
|
|
4
|
+
key: string;
|
|
5
|
+
priceId: string;
|
|
6
|
+
mode: StripePaymentsMode;
|
|
7
|
+
internalSku: string;
|
|
8
|
+
displayName: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SecureStripeConfiguration {
|
|
11
|
+
accessToken: string;
|
|
12
|
+
webhookVerificationKey: string;
|
|
13
|
+
redirectsBaseUrl: string;
|
|
14
|
+
products: ReadonlyMap<string, SecureStripeProduct>;
|
|
15
|
+
}
|
|
16
|
+
export declare const SECURE_STRIPE_CONFIGURATION = "SecureStripeConfiguration";
|
|
17
|
+
export declare const secureStripeConfigurationProvider: Provider;
|
|
18
|
+
export declare function parseStripeProductCatalog(value: string): StripePaymentsProduct[];
|
|
19
|
+
export declare function createSecureStripeConfiguration(options: StripePaymentsModuleOptions): SecureStripeConfiguration;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { STRIPE_PAYMENTS_OPTIONS, } from "./stripe-payments.options.js";
|
|
2
|
+
export const SECURE_STRIPE_CONFIGURATION = "SecureStripeConfiguration";
|
|
3
|
+
export const secureStripeConfigurationProvider = {
|
|
4
|
+
provide: SECURE_STRIPE_CONFIGURATION,
|
|
5
|
+
inject: [STRIPE_PAYMENTS_OPTIONS],
|
|
6
|
+
useFactory: (options) => createSecureStripeConfiguration(options),
|
|
7
|
+
};
|
|
8
|
+
function parseProduct(product) {
|
|
9
|
+
const mode = product.mode;
|
|
10
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(product.key) ||
|
|
11
|
+
!/^price_[A-Za-z0-9]+$/.test(product.priceId) ||
|
|
12
|
+
(mode !== "subscription" && mode !== "payment")) {
|
|
13
|
+
throw new Error(`Invalid Stripe product configuration: ${product.key}`);
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
key: product.key,
|
|
17
|
+
priceId: product.priceId,
|
|
18
|
+
mode,
|
|
19
|
+
internalSku: product.internalSku ?? product.key,
|
|
20
|
+
displayName: product.displayName ?? product.key,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function parseStripeProductCatalog(value) {
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = JSON.parse(value);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error("Stripe product catalog must contain valid JSON", {
|
|
30
|
+
cause: error,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (typeof parsed !== "object" ||
|
|
34
|
+
parsed === null ||
|
|
35
|
+
Array.isArray(parsed)) {
|
|
36
|
+
throw new Error("Stripe product catalog must be an object keyed by product key");
|
|
37
|
+
}
|
|
38
|
+
return Object.entries(parsed).map(([key, product]) => {
|
|
39
|
+
if (typeof product !== "object" || product === null) {
|
|
40
|
+
throw new Error(`Invalid Stripe product configuration: ${key}`);
|
|
41
|
+
}
|
|
42
|
+
const candidate = product;
|
|
43
|
+
return {
|
|
44
|
+
key,
|
|
45
|
+
priceId: candidate.priceId ?? "",
|
|
46
|
+
mode: candidate.mode ?? "payment",
|
|
47
|
+
internalSku: candidate.internalSku,
|
|
48
|
+
displayName: candidate.displayName,
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export function createSecureStripeConfiguration(options) {
|
|
53
|
+
if (!options.accessToken) {
|
|
54
|
+
throw new Error("Stripe accessToken is required");
|
|
55
|
+
}
|
|
56
|
+
if (!options.webhookVerificationKey) {
|
|
57
|
+
throw new Error("Stripe webhookVerificationKey is required");
|
|
58
|
+
}
|
|
59
|
+
if (!Array.isArray(options.products) || options.products.length === 0) {
|
|
60
|
+
throw new Error("Stripe products must contain at least one product");
|
|
61
|
+
}
|
|
62
|
+
const products = options.products;
|
|
63
|
+
let redirectsBaseUrl;
|
|
64
|
+
try {
|
|
65
|
+
redirectsBaseUrl = new URL(options.redirectsBaseUrl);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
throw new Error("Stripe redirectsBaseUrl must be a valid URL", {
|
|
69
|
+
cause: error,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (!/^https?:$/.test(redirectsBaseUrl.protocol) ||
|
|
73
|
+
redirectsBaseUrl.pathname !== "/" ||
|
|
74
|
+
redirectsBaseUrl.search ||
|
|
75
|
+
redirectsBaseUrl.hash) {
|
|
76
|
+
throw new Error("Stripe redirectsBaseUrl must contain only an HTTP(S) origin");
|
|
77
|
+
}
|
|
78
|
+
const productsByKey = new Map();
|
|
79
|
+
const priceIds = new Set();
|
|
80
|
+
for (const productInput of products) {
|
|
81
|
+
const product = parseProduct(productInput);
|
|
82
|
+
if (productsByKey.has(product.key)) {
|
|
83
|
+
throw new Error(`Duplicate Stripe product key: ${product.key}`);
|
|
84
|
+
}
|
|
85
|
+
if (priceIds.has(product.priceId)) {
|
|
86
|
+
throw new Error(`Duplicate Stripe price ID: ${product.priceId}`);
|
|
87
|
+
}
|
|
88
|
+
productsByKey.set(product.key, product);
|
|
89
|
+
priceIds.add(product.priceId);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
accessToken: options.accessToken,
|
|
93
|
+
webhookVerificationKey: options.webhookVerificationKey,
|
|
94
|
+
redirectsBaseUrl: redirectsBaseUrl.origin,
|
|
95
|
+
products: productsByKey,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=stripe-payment.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stripe-payment.config.js","sourceRoot":"","sources":["../../src/stripe-payments/stripe-payment.config.ts"],"names":[],"mappings":"AACA,OAAO,EACH,uBAAuB,GAI1B,MAAM,8BAA8B,CAAC;AAiBtC,MAAM,C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