@parsrun/payments 0.1.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 +184 -0
- package/dist/billing/index.d.ts +121 -0
- package/dist/billing/index.js +1082 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing-service-LsAFesou.d.ts +578 -0
- package/dist/dunning/index.d.ts +310 -0
- package/dist/dunning/index.js +2677 -0
- package/dist/dunning/index.js.map +1 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +7698 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/index.d.ts +5 -0
- package/dist/providers/index.js +1396 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/iyzico.d.ts +250 -0
- package/dist/providers/iyzico.js +469 -0
- package/dist/providers/iyzico.js.map +1 -0
- package/dist/providers/paddle.d.ts +66 -0
- package/dist/providers/paddle.js +437 -0
- package/dist/providers/paddle.js.map +1 -0
- package/dist/providers/stripe.d.ts +122 -0
- package/dist/providers/stripe.js +586 -0
- package/dist/providers/stripe.js.map +1 -0
- package/dist/schema-C5Zcju_j.d.ts +4191 -0
- package/dist/types.d.ts +388 -0
- package/dist/types.js +74 -0
- package/dist/types.js.map +1 -0
- package/dist/usage/index.d.ts +2674 -0
- package/dist/usage/index.js +2916 -0
- package/dist/usage/index.js.map +1 -0
- package/dist/webhooks/index.d.ts +89 -0
- package/dist/webhooks/index.js +188 -0
- package/dist/webhooks/index.js.map +1 -0
- package/package.json +91 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
import {
|
|
3
|
+
type,
|
|
4
|
+
currencyCode,
|
|
5
|
+
money,
|
|
6
|
+
paymentCustomer,
|
|
7
|
+
createCustomerRequest,
|
|
8
|
+
cardDetails,
|
|
9
|
+
paymentMethod,
|
|
10
|
+
paymentIntentStatus,
|
|
11
|
+
paymentIntent,
|
|
12
|
+
createPaymentIntentRequest,
|
|
13
|
+
subscriptionStatus,
|
|
14
|
+
priceInterval,
|
|
15
|
+
price,
|
|
16
|
+
subscription,
|
|
17
|
+
createSubscriptionRequest,
|
|
18
|
+
refundStatus,
|
|
19
|
+
refund,
|
|
20
|
+
createRefundRequest,
|
|
21
|
+
webhookEventType,
|
|
22
|
+
webhookEvent,
|
|
23
|
+
stripeConfig,
|
|
24
|
+
paddleConfig,
|
|
25
|
+
iyzicoConfig,
|
|
26
|
+
paymentsConfig
|
|
27
|
+
} from "@parsrun/types";
|
|
28
|
+
var PaymentError = class extends Error {
|
|
29
|
+
constructor(message, code, cause) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.cause = cause;
|
|
33
|
+
this.name = "PaymentError";
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var PaymentErrorCodes = {
|
|
37
|
+
INVALID_CONFIG: "INVALID_CONFIG",
|
|
38
|
+
CUSTOMER_NOT_FOUND: "CUSTOMER_NOT_FOUND",
|
|
39
|
+
SUBSCRIPTION_NOT_FOUND: "SUBSCRIPTION_NOT_FOUND",
|
|
40
|
+
CHECKOUT_FAILED: "CHECKOUT_FAILED",
|
|
41
|
+
PAYMENT_FAILED: "PAYMENT_FAILED",
|
|
42
|
+
WEBHOOK_VERIFICATION_FAILED: "WEBHOOK_VERIFICATION_FAILED",
|
|
43
|
+
API_ERROR: "API_ERROR",
|
|
44
|
+
RATE_LIMITED: "RATE_LIMITED"
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/providers/paddle.ts
|
|
48
|
+
var PaddleProvider = class {
|
|
49
|
+
type = "paddle";
|
|
50
|
+
apiKey;
|
|
51
|
+
webhookSecret;
|
|
52
|
+
baseUrl;
|
|
53
|
+
constructor(config) {
|
|
54
|
+
this.apiKey = config.apiKey;
|
|
55
|
+
this.webhookSecret = config.webhookSecret;
|
|
56
|
+
this.baseUrl = config.environment === "production" ? "https://api.paddle.com" : "https://sandbox-api.paddle.com";
|
|
57
|
+
}
|
|
58
|
+
async request(endpoint, options = {}) {
|
|
59
|
+
const { method = "GET", body } = options;
|
|
60
|
+
const headers = {
|
|
61
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
62
|
+
"Content-Type": "application/json"
|
|
63
|
+
};
|
|
64
|
+
const fetchOptions = {
|
|
65
|
+
method,
|
|
66
|
+
headers
|
|
67
|
+
};
|
|
68
|
+
if (body) {
|
|
69
|
+
fetchOptions.body = JSON.stringify(body);
|
|
70
|
+
}
|
|
71
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, fetchOptions);
|
|
72
|
+
const data = await response.json();
|
|
73
|
+
if (!response.ok || data.error) {
|
|
74
|
+
const errorMessage = data.error?.detail ?? `HTTP ${response.status}`;
|
|
75
|
+
throw new PaymentError(
|
|
76
|
+
`Paddle API error: ${errorMessage}`,
|
|
77
|
+
data.error?.code ?? PaymentErrorCodes.API_ERROR,
|
|
78
|
+
data.error
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return data.data;
|
|
82
|
+
}
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// Customer
|
|
85
|
+
// ============================================================================
|
|
86
|
+
async createCustomer(options) {
|
|
87
|
+
const body = {
|
|
88
|
+
email: options.email
|
|
89
|
+
};
|
|
90
|
+
if (options.name) body["name"] = options.name;
|
|
91
|
+
if (options.metadata) body["custom_data"] = options.metadata;
|
|
92
|
+
const result = await this.request("/customers", {
|
|
93
|
+
method: "POST",
|
|
94
|
+
body
|
|
95
|
+
});
|
|
96
|
+
return this.mapCustomer(result);
|
|
97
|
+
}
|
|
98
|
+
async getCustomer(customerId) {
|
|
99
|
+
try {
|
|
100
|
+
const result = await this.request(`/customers/${customerId}`);
|
|
101
|
+
return this.mapCustomer(result);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (err instanceof PaymentError && err.code === "not_found") {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async updateCustomer(customerId, options) {
|
|
110
|
+
const body = {};
|
|
111
|
+
if (options.email) body["email"] = options.email;
|
|
112
|
+
if (options.name) body["name"] = options.name;
|
|
113
|
+
if (options.metadata) body["custom_data"] = options.metadata;
|
|
114
|
+
const result = await this.request(`/customers/${customerId}`, {
|
|
115
|
+
method: "PATCH",
|
|
116
|
+
body
|
|
117
|
+
});
|
|
118
|
+
return this.mapCustomer(result);
|
|
119
|
+
}
|
|
120
|
+
async deleteCustomer(_customerId) {
|
|
121
|
+
throw new PaymentError(
|
|
122
|
+
"Paddle does not support customer deletion",
|
|
123
|
+
PaymentErrorCodes.API_ERROR
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
mapCustomer(paddle) {
|
|
127
|
+
return {
|
|
128
|
+
id: paddle.id,
|
|
129
|
+
email: paddle.email,
|
|
130
|
+
name: paddle.name ?? void 0,
|
|
131
|
+
metadata: paddle.custom_data ?? void 0,
|
|
132
|
+
providerData: paddle
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// ============================================================================
|
|
136
|
+
// Checkout
|
|
137
|
+
// ============================================================================
|
|
138
|
+
async createCheckout(options) {
|
|
139
|
+
const items = options.lineItems.map((item) => ({
|
|
140
|
+
price_id: item.priceId,
|
|
141
|
+
quantity: item.quantity
|
|
142
|
+
}));
|
|
143
|
+
const body = {
|
|
144
|
+
items
|
|
145
|
+
};
|
|
146
|
+
if (options.customerId) body["customer_id"] = options.customerId;
|
|
147
|
+
if (options.customerEmail) {
|
|
148
|
+
body["customer"] = { email: options.customerEmail };
|
|
149
|
+
}
|
|
150
|
+
if (options.metadata) body["custom_data"] = options.metadata;
|
|
151
|
+
body["settings"] = {
|
|
152
|
+
success_url: options.successUrl
|
|
153
|
+
};
|
|
154
|
+
const result = await this.request("/transactions", {
|
|
155
|
+
method: "POST",
|
|
156
|
+
body
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
id: result.id,
|
|
160
|
+
url: result.checkout?.url ?? "",
|
|
161
|
+
customerId: result.customer_id ?? void 0,
|
|
162
|
+
status: result.status === "completed" ? "complete" : "open",
|
|
163
|
+
mode: result.subscription_id ? "subscription" : "payment",
|
|
164
|
+
amountTotal: this.parsePaddleAmount(result.details?.totals?.total),
|
|
165
|
+
currency: result.currency_code,
|
|
166
|
+
providerData: result
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async getCheckout(sessionId) {
|
|
170
|
+
try {
|
|
171
|
+
const result = await this.request(`/transactions/${sessionId}`);
|
|
172
|
+
return {
|
|
173
|
+
id: result.id,
|
|
174
|
+
url: result.checkout?.url ?? "",
|
|
175
|
+
customerId: result.customer_id ?? void 0,
|
|
176
|
+
status: result.status === "completed" ? "complete" : "open",
|
|
177
|
+
mode: result.subscription_id ? "subscription" : "payment",
|
|
178
|
+
amountTotal: this.parsePaddleAmount(result.details?.totals?.total),
|
|
179
|
+
currency: result.currency_code,
|
|
180
|
+
providerData: result
|
|
181
|
+
};
|
|
182
|
+
} catch (err) {
|
|
183
|
+
if (err instanceof PaymentError && err.code === "not_found") {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
throw err;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// ============================================================================
|
|
190
|
+
// Subscriptions
|
|
191
|
+
// ============================================================================
|
|
192
|
+
async createSubscription(_options) {
|
|
193
|
+
throw new PaymentError(
|
|
194
|
+
"Paddle subscriptions must be created through checkout",
|
|
195
|
+
PaymentErrorCodes.API_ERROR
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
async getSubscription(subscriptionId) {
|
|
199
|
+
try {
|
|
200
|
+
const result = await this.request(
|
|
201
|
+
`/subscriptions/${subscriptionId}`
|
|
202
|
+
);
|
|
203
|
+
return this.mapSubscription(result);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
if (err instanceof PaymentError && err.code === "not_found") {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async updateSubscription(subscriptionId, options) {
|
|
212
|
+
const body = {};
|
|
213
|
+
if (options.priceId) {
|
|
214
|
+
body["items"] = [{ price_id: options.priceId, quantity: 1 }];
|
|
215
|
+
}
|
|
216
|
+
if (options.metadata) body["custom_data"] = options.metadata;
|
|
217
|
+
if (options.prorationBehavior) {
|
|
218
|
+
body["proration_billing_mode"] = options.prorationBehavior === "none" ? "do_not_bill" : "prorated_immediately";
|
|
219
|
+
}
|
|
220
|
+
const result = await this.request(
|
|
221
|
+
`/subscriptions/${subscriptionId}`,
|
|
222
|
+
{ method: "PATCH", body }
|
|
223
|
+
);
|
|
224
|
+
return this.mapSubscription(result);
|
|
225
|
+
}
|
|
226
|
+
async cancelSubscription(subscriptionId, cancelAtPeriodEnd = true) {
|
|
227
|
+
const body = {
|
|
228
|
+
effective_from: cancelAtPeriodEnd ? "next_billing_period" : "immediately"
|
|
229
|
+
};
|
|
230
|
+
const result = await this.request(
|
|
231
|
+
`/subscriptions/${subscriptionId}/cancel`,
|
|
232
|
+
{ method: "POST", body }
|
|
233
|
+
);
|
|
234
|
+
return this.mapSubscription(result);
|
|
235
|
+
}
|
|
236
|
+
async listSubscriptions(customerId) {
|
|
237
|
+
const result = await this.request(
|
|
238
|
+
`/subscriptions?customer_id=${customerId}`
|
|
239
|
+
);
|
|
240
|
+
return result.map((sub) => this.mapSubscription(sub));
|
|
241
|
+
}
|
|
242
|
+
mapSubscription(paddle) {
|
|
243
|
+
const item = paddle.items?.[0];
|
|
244
|
+
const statusMap = {
|
|
245
|
+
active: "active",
|
|
246
|
+
canceled: "canceled",
|
|
247
|
+
past_due: "past_due",
|
|
248
|
+
paused: "paused",
|
|
249
|
+
trialing: "trialing"
|
|
250
|
+
};
|
|
251
|
+
return {
|
|
252
|
+
id: paddle.id,
|
|
253
|
+
customerId: paddle.customer_id,
|
|
254
|
+
status: statusMap[paddle.status] ?? "active",
|
|
255
|
+
priceId: item?.price?.id ?? "",
|
|
256
|
+
productId: item?.price?.product_id,
|
|
257
|
+
currentPeriodStart: new Date(paddle.current_billing_period?.starts_at ?? Date.now()),
|
|
258
|
+
currentPeriodEnd: new Date(paddle.current_billing_period?.ends_at ?? Date.now()),
|
|
259
|
+
cancelAtPeriodEnd: paddle.scheduled_change?.action === "cancel",
|
|
260
|
+
canceledAt: paddle.canceled_at ? new Date(paddle.canceled_at) : void 0,
|
|
261
|
+
trialStart: paddle.started_at ? new Date(paddle.started_at) : void 0,
|
|
262
|
+
trialEnd: paddle.first_billed_at ? new Date(paddle.first_billed_at) : void 0,
|
|
263
|
+
metadata: paddle.custom_data ?? void 0,
|
|
264
|
+
providerData: paddle
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// ============================================================================
|
|
268
|
+
// Portal
|
|
269
|
+
// ============================================================================
|
|
270
|
+
async createPortalSession(options) {
|
|
271
|
+
const customer = await this.getCustomer(options.customerId);
|
|
272
|
+
if (!customer) {
|
|
273
|
+
throw new PaymentError(
|
|
274
|
+
"Customer not found",
|
|
275
|
+
PaymentErrorCodes.CUSTOMER_NOT_FOUND
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
const result = await this.request(
|
|
279
|
+
`/customers/${options.customerId}/portal-sessions`,
|
|
280
|
+
{ method: "POST" }
|
|
281
|
+
);
|
|
282
|
+
return {
|
|
283
|
+
url: result.urls.general.overview,
|
|
284
|
+
returnUrl: options.returnUrl
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
// ============================================================================
|
|
288
|
+
// Products & Prices
|
|
289
|
+
// ============================================================================
|
|
290
|
+
async getProduct(productId) {
|
|
291
|
+
try {
|
|
292
|
+
const result = await this.request(`/products/${productId}`);
|
|
293
|
+
return this.mapProduct(result);
|
|
294
|
+
} catch (err) {
|
|
295
|
+
if (err instanceof PaymentError && err.code === "not_found") {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
throw err;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
async getPrice(priceId) {
|
|
302
|
+
try {
|
|
303
|
+
const result = await this.request(`/prices/${priceId}`);
|
|
304
|
+
return this.mapPrice(result);
|
|
305
|
+
} catch (err) {
|
|
306
|
+
if (err instanceof PaymentError && err.code === "not_found") {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
throw err;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async listPrices(productId) {
|
|
313
|
+
let endpoint = "/prices?status=active";
|
|
314
|
+
if (productId) {
|
|
315
|
+
endpoint += `&product_id=${productId}`;
|
|
316
|
+
}
|
|
317
|
+
const result = await this.request(endpoint);
|
|
318
|
+
return result.map((price2) => this.mapPrice(price2));
|
|
319
|
+
}
|
|
320
|
+
mapProduct(paddle) {
|
|
321
|
+
return {
|
|
322
|
+
id: paddle.id,
|
|
323
|
+
name: paddle.name,
|
|
324
|
+
description: paddle.description ?? void 0,
|
|
325
|
+
active: paddle.status === "active",
|
|
326
|
+
metadata: paddle.custom_data ?? void 0,
|
|
327
|
+
providerData: paddle
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
mapPrice(paddle) {
|
|
331
|
+
const amount = paddle.unit_price?.amount ? parseInt(paddle.unit_price.amount, 10) : 0;
|
|
332
|
+
return {
|
|
333
|
+
id: paddle.id,
|
|
334
|
+
productId: paddle.product_id,
|
|
335
|
+
unitAmount: amount,
|
|
336
|
+
currency: paddle.unit_price?.currency_code ?? "USD",
|
|
337
|
+
recurring: paddle.billing_cycle ? {
|
|
338
|
+
interval: paddle.billing_cycle.interval,
|
|
339
|
+
intervalCount: paddle.billing_cycle.frequency
|
|
340
|
+
} : void 0,
|
|
341
|
+
active: paddle.status === "active",
|
|
342
|
+
metadata: paddle.custom_data ?? void 0,
|
|
343
|
+
providerData: paddle
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
parsePaddleAmount(amount) {
|
|
347
|
+
if (!amount) return void 0;
|
|
348
|
+
return parseInt(amount, 10);
|
|
349
|
+
}
|
|
350
|
+
// ============================================================================
|
|
351
|
+
// Webhooks
|
|
352
|
+
// ============================================================================
|
|
353
|
+
async verifyWebhook(payload, signature) {
|
|
354
|
+
if (!this.webhookSecret) {
|
|
355
|
+
throw new PaymentError(
|
|
356
|
+
"Webhook secret not configured",
|
|
357
|
+
PaymentErrorCodes.INVALID_CONFIG
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
const payloadString = typeof payload === "string" ? payload : new TextDecoder().decode(payload);
|
|
361
|
+
const signatureParts = signature.split(";").reduce((acc, part) => {
|
|
362
|
+
const [key, value] = part.split("=");
|
|
363
|
+
if (key && value) {
|
|
364
|
+
acc[key] = value;
|
|
365
|
+
}
|
|
366
|
+
return acc;
|
|
367
|
+
}, {});
|
|
368
|
+
const timestamp = signatureParts["ts"];
|
|
369
|
+
const expectedSignature = signatureParts["h1"];
|
|
370
|
+
if (!timestamp || !expectedSignature) {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
const signedPayload = `${timestamp}:${payloadString}`;
|
|
374
|
+
const computedSignature = await this.computeHmacSignature(
|
|
375
|
+
signedPayload,
|
|
376
|
+
this.webhookSecret
|
|
377
|
+
);
|
|
378
|
+
if (!this.secureCompare(computedSignature, expectedSignature)) {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
const event = JSON.parse(payloadString);
|
|
382
|
+
return {
|
|
383
|
+
id: event.event_id,
|
|
384
|
+
type: this.mapEventType(event.event_type),
|
|
385
|
+
data: event.data,
|
|
386
|
+
created: new Date(event.occurred_at),
|
|
387
|
+
provider: "paddle",
|
|
388
|
+
raw: event
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
async computeHmacSignature(payload, secret) {
|
|
392
|
+
const encoder = new TextEncoder();
|
|
393
|
+
const keyData = encoder.encode(secret);
|
|
394
|
+
const messageData = encoder.encode(payload);
|
|
395
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
396
|
+
"raw",
|
|
397
|
+
keyData,
|
|
398
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
399
|
+
false,
|
|
400
|
+
["sign"]
|
|
401
|
+
);
|
|
402
|
+
const signature = await crypto.subtle.sign("HMAC", cryptoKey, messageData);
|
|
403
|
+
const signatureArray = new Uint8Array(signature);
|
|
404
|
+
return Array.from(signatureArray).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
405
|
+
}
|
|
406
|
+
secureCompare(a, b) {
|
|
407
|
+
if (a.length !== b.length) return false;
|
|
408
|
+
let result = 0;
|
|
409
|
+
for (let i = 0; i < a.length; i++) {
|
|
410
|
+
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
411
|
+
}
|
|
412
|
+
return result === 0;
|
|
413
|
+
}
|
|
414
|
+
mapEventType(paddleType) {
|
|
415
|
+
const mapping = {
|
|
416
|
+
"transaction.completed": "checkout.session.completed",
|
|
417
|
+
"customer.created": "customer.created",
|
|
418
|
+
"customer.updated": "customer.updated",
|
|
419
|
+
"subscription.created": "subscription.created",
|
|
420
|
+
"subscription.updated": "subscription.updated",
|
|
421
|
+
"subscription.canceled": "subscription.deleted",
|
|
422
|
+
"subscription.past_due": "subscription.updated",
|
|
423
|
+
"subscription.activated": "subscription.created",
|
|
424
|
+
"transaction.payment_failed": "payment.failed",
|
|
425
|
+
"adjustment.created": "refund.created"
|
|
426
|
+
};
|
|
427
|
+
return mapping[paddleType] ?? "unknown";
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
function createPaddleProvider(config) {
|
|
431
|
+
return new PaddleProvider(config);
|
|
432
|
+
}
|
|
433
|
+
export {
|
|
434
|
+
PaddleProvider,
|
|
435
|
+
createPaddleProvider
|
|
436
|
+
};
|
|
437
|
+
//# sourceMappingURL=paddle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts","../../src/providers/paddle.ts"],"sourcesContent":["/**\n * @parsrun/payments - Type Definitions\n * Payment types and interfaces\n */\n\n// Re-export types from @parsrun/types for convenience\nexport {\n type,\n currencyCode,\n money,\n paymentCustomer,\n createCustomerRequest,\n cardDetails,\n paymentMethod,\n paymentIntentStatus,\n paymentIntent as parsPaymentIntent,\n createPaymentIntentRequest,\n subscriptionStatus as parsSubscriptionStatus,\n priceInterval,\n price as parsPrice,\n subscription as parsSubscription,\n createSubscriptionRequest,\n refundStatus,\n refund,\n createRefundRequest,\n webhookEventType,\n webhookEvent,\n stripeConfig,\n paddleConfig,\n iyzicoConfig,\n paymentsConfig,\n type CurrencyCode as ParsCurrencyCode,\n type Money,\n type PaymentCustomer,\n type CreateCustomerRequest as ParsCreateCustomerRequest,\n type CardDetails,\n type PaymentMethod as ParsPaymentMethod,\n type PaymentIntentStatus,\n type PaymentIntent as ParsPaymentIntentType,\n type CreatePaymentIntentRequest,\n type SubscriptionStatus as ParsSubscriptionStatus,\n type PriceInterval,\n type Price as ParsPrice,\n type Subscription as ParsSubscription,\n type CreateSubscriptionRequest as ParsCreateSubscriptionRequest,\n type RefundStatus,\n type Refund,\n type CreateRefundRequest,\n type WebhookEventType as ParsWebhookEventType,\n type WebhookEvent as ParsWebhookEvent,\n type StripeConfig,\n type PaddleConfig,\n type IyzicoConfig,\n type PaymentsConfig,\n} from \"@parsrun/types\";\n\n/**\n * Payment provider type\n */\nexport type PaymentProviderType = \"stripe\" | \"paddle\" | \"iyzico\";\n\n/**\n * Currency code (ISO 4217)\n */\nexport type CurrencyCode = \"USD\" | \"EUR\" | \"GBP\" | \"TRY\" | \"JPY\" | \"CAD\" | \"AUD\" | string;\n\n/**\n * Payment status\n */\nexport type PaymentStatus =\n | \"pending\"\n | \"processing\"\n | \"succeeded\"\n | \"failed\"\n | \"canceled\"\n | \"refunded\"\n | \"partially_refunded\";\n\n/**\n * Subscription status\n */\nexport type SubscriptionStatus =\n | \"active\"\n | \"past_due\"\n | \"unpaid\"\n | \"canceled\"\n | \"incomplete\"\n | \"incomplete_expired\"\n | \"trialing\"\n | \"paused\";\n\n/**\n * Billing interval\n */\nexport type BillingInterval = \"day\" | \"week\" | \"month\" | \"year\";\n\n// ============================================================================\n// Customer\n// ============================================================================\n\n/**\n * Customer data\n */\nexport interface Customer {\n /** Provider customer ID */\n id: string;\n /** Customer email */\n email: string;\n /** Customer name */\n name?: string | undefined;\n /** Phone number */\n phone?: string | undefined;\n /** Billing address */\n address?: Address | undefined;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n/**\n * Address\n */\nexport interface Address {\n line1?: string | undefined;\n line2?: string | undefined;\n city?: string | undefined;\n state?: string | undefined;\n postalCode?: string | undefined;\n country?: string | undefined;\n}\n\n/**\n * Create customer options\n */\nexport interface CreateCustomerOptions {\n email: string;\n name?: string | undefined;\n phone?: string | undefined;\n address?: Address | undefined;\n metadata?: Record<string, string> | undefined;\n}\n\n// ============================================================================\n// Products & Prices\n// ============================================================================\n\n/**\n * Product\n */\nexport interface Product {\n /** Provider product ID */\n id: string;\n /** Product name */\n name: string;\n /** Description */\n description?: string | undefined;\n /** Active status */\n active: boolean;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n/**\n * Price\n */\nexport interface Price {\n /** Provider price ID */\n id: string;\n /** Product ID */\n productId: string;\n /** Price in smallest currency unit (cents) */\n unitAmount: number;\n /** Currency */\n currency: CurrencyCode;\n /** Recurring billing details */\n recurring?: {\n interval: BillingInterval;\n intervalCount: number;\n } | undefined;\n /** Active status */\n active: boolean;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n// ============================================================================\n// Checkout\n// ============================================================================\n\n/**\n * Checkout line item\n */\nexport interface CheckoutLineItem {\n /** Price ID */\n priceId: string;\n /** Quantity */\n quantity: number;\n}\n\n/**\n * Create checkout options\n */\nexport interface CreateCheckoutOptions {\n /** Customer ID (optional, creates new if not provided) */\n customerId?: string | undefined;\n /** Customer email (for new customers) */\n customerEmail?: string | undefined;\n /** Line items */\n lineItems: CheckoutLineItem[];\n /** Success redirect URL */\n successUrl: string;\n /** Cancel redirect URL */\n cancelUrl: string;\n /** Checkout mode */\n mode: \"payment\" | \"subscription\" | \"setup\";\n /** Allow promotion codes */\n allowPromotionCodes?: boolean | undefined;\n /** Trial period days (subscription only) */\n trialDays?: number | undefined;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Tenant ID for multi-tenant */\n tenantId?: string | undefined;\n}\n\n/**\n * Checkout session\n */\nexport interface CheckoutSession {\n /** Provider session ID */\n id: string;\n /** Checkout URL */\n url: string;\n /** Customer ID */\n customerId?: string | undefined;\n /** Payment status */\n status: \"open\" | \"complete\" | \"expired\";\n /** Mode */\n mode: \"payment\" | \"subscription\" | \"setup\";\n /** Amount total */\n amountTotal?: number | undefined;\n /** Currency */\n currency?: CurrencyCode | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n// ============================================================================\n// Subscriptions\n// ============================================================================\n\n/**\n * Subscription\n */\nexport interface Subscription {\n /** Provider subscription ID */\n id: string;\n /** Customer ID */\n customerId: string;\n /** Status */\n status: SubscriptionStatus;\n /** Price ID */\n priceId: string;\n /** Product ID */\n productId?: string | undefined;\n /** Current period start */\n currentPeriodStart: Date;\n /** Current period end */\n currentPeriodEnd: Date;\n /** Cancel at period end */\n cancelAtPeriodEnd: boolean;\n /** Canceled at */\n canceledAt?: Date | undefined;\n /** Trial start */\n trialStart?: Date | undefined;\n /** Trial end */\n trialEnd?: Date | undefined;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n/**\n * Create subscription options\n */\nexport interface CreateSubscriptionOptions {\n /** Customer ID */\n customerId: string;\n /** Price ID */\n priceId: string;\n /** Trial period days */\n trialDays?: number | undefined;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Payment behavior */\n paymentBehavior?: \"default_incomplete\" | \"error_if_incomplete\" | \"allow_incomplete\" | undefined;\n}\n\n/**\n * Update subscription options\n */\nexport interface UpdateSubscriptionOptions {\n /** New price ID */\n priceId?: string | undefined;\n /** Cancel at period end */\n cancelAtPeriodEnd?: boolean | undefined;\n /** Custom metadata */\n metadata?: Record<string, string> | undefined;\n /** Proration behavior */\n prorationBehavior?: \"create_prorations\" | \"none\" | \"always_invoice\" | undefined;\n}\n\n// ============================================================================\n// Payments & Invoices\n// ============================================================================\n\n/**\n * Payment intent\n */\nexport interface PaymentIntent {\n /** Provider payment ID */\n id: string;\n /** Amount */\n amount: number;\n /** Currency */\n currency: CurrencyCode;\n /** Status */\n status: PaymentStatus;\n /** Customer ID */\n customerId?: string | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n/**\n * Invoice\n */\nexport interface Invoice {\n /** Provider invoice ID */\n id: string;\n /** Customer ID */\n customerId: string;\n /** Subscription ID */\n subscriptionId?: string | undefined;\n /** Status */\n status: \"draft\" | \"open\" | \"paid\" | \"void\" | \"uncollectible\";\n /** Amount due */\n amountDue: number;\n /** Amount paid */\n amountPaid: number;\n /** Currency */\n currency: CurrencyCode;\n /** Invoice URL */\n hostedInvoiceUrl?: string | undefined;\n /** PDF URL */\n invoicePdf?: string | undefined;\n /** Due date */\n dueDate?: Date | undefined;\n /** Provider-specific data */\n providerData?: unknown;\n}\n\n// ============================================================================\n// Portal\n// ============================================================================\n\n/**\n * Customer portal session\n */\nexport interface PortalSession {\n /** Portal URL */\n url: string;\n /** Return URL */\n returnUrl: string;\n}\n\n/**\n * Create portal options\n */\nexport interface CreatePortalOptions {\n /** Customer ID */\n customerId: string;\n /** Return URL */\n returnUrl: string;\n}\n\n// ============================================================================\n// Webhooks\n// ============================================================================\n\n/**\n * Webhook event types\n */\nexport type WebhookEventType =\n // Checkout\n | \"checkout.session.completed\"\n | \"checkout.session.expired\"\n // Customer\n | \"customer.created\"\n | \"customer.updated\"\n | \"customer.deleted\"\n // Subscription\n | \"subscription.created\"\n | \"subscription.updated\"\n | \"subscription.deleted\"\n | \"subscription.trial_will_end\"\n // Payment\n | \"payment.succeeded\"\n | \"payment.failed\"\n // Invoice\n | \"invoice.created\"\n | \"invoice.paid\"\n | \"invoice.payment_failed\"\n | \"invoice.upcoming\"\n // Refund\n | \"refund.created\"\n | \"refund.updated\";\n\n/**\n * Webhook event\n */\nexport interface WebhookEvent<T = unknown> {\n /** Event ID */\n id: string;\n /** Event type */\n type: WebhookEventType;\n /** Event data */\n data: T;\n /** Created timestamp */\n created: Date;\n /** Provider type */\n provider: PaymentProviderType;\n /** Raw event data */\n raw: unknown;\n}\n\n/**\n * Webhook handler\n */\nexport type WebhookHandler<T = unknown> = (\n event: WebhookEvent<T>\n) => void | Promise<void>;\n\n// ============================================================================\n// Provider Interface\n// ============================================================================\n\n/**\n * Payment provider interface\n */\nexport interface PaymentProvider {\n /** Provider type */\n readonly type: PaymentProviderType;\n\n // Customer\n createCustomer(options: CreateCustomerOptions): Promise<Customer>;\n getCustomer(customerId: string): Promise<Customer | null>;\n updateCustomer(customerId: string, options: Partial<CreateCustomerOptions>): Promise<Customer>;\n deleteCustomer(customerId: string): Promise<void>;\n\n // Checkout\n createCheckout(options: CreateCheckoutOptions): Promise<CheckoutSession>;\n getCheckout(sessionId: string): Promise<CheckoutSession | null>;\n\n // Subscriptions\n createSubscription(options: CreateSubscriptionOptions): Promise<Subscription>;\n getSubscription(subscriptionId: string): Promise<Subscription | null>;\n updateSubscription(subscriptionId: string, options: UpdateSubscriptionOptions): Promise<Subscription>;\n cancelSubscription(subscriptionId: string, cancelAtPeriodEnd?: boolean): Promise<Subscription>;\n listSubscriptions(customerId: string): Promise<Subscription[]>;\n\n // Portal\n createPortalSession(options: CreatePortalOptions): Promise<PortalSession>;\n\n // Webhooks\n verifyWebhook(payload: string | Uint8Array, signature: string): Promise<WebhookEvent | null>;\n\n // Products & Prices (optional)\n getProduct?(productId: string): Promise<Product | null>;\n getPrice?(priceId: string): Promise<Price | null>;\n listPrices?(productId?: string): Promise<Price[]>;\n}\n\n// ============================================================================\n// Provider Config\n// ============================================================================\n\n/**\n * Stripe provider config\n */\nexport interface StripeProviderConfig {\n /** Stripe secret key */\n secretKey: string;\n /** Webhook signing secret */\n webhookSecret?: string | undefined;\n /** API version */\n apiVersion?: string | undefined;\n}\n\n/**\n * Paddle provider config\n */\nexport interface PaddleProviderConfig {\n /** Paddle API key */\n apiKey: string;\n /** Paddle environment */\n environment?: \"sandbox\" | \"production\" | undefined;\n /** Webhook secret key */\n webhookSecret?: string | undefined;\n /** Seller ID */\n sellerId?: string | undefined;\n}\n\n// ============================================================================\n// Service Config\n// ============================================================================\n\n/**\n * Payment service config\n */\nexport interface PaymentServiceConfig {\n /** Payment provider */\n provider: PaymentProvider;\n /** Enable debug logging */\n debug?: boolean | undefined;\n}\n\n// ============================================================================\n// Errors\n// ============================================================================\n\n/**\n * Payment error\n */\nexport class PaymentError extends Error {\n constructor(\n message: string,\n public readonly code: string,\n public readonly cause?: unknown\n ) {\n super(message);\n this.name = \"PaymentError\";\n }\n}\n\n/**\n * Common payment error codes\n */\nexport const PaymentErrorCodes = {\n INVALID_CONFIG: \"INVALID_CONFIG\",\n CUSTOMER_NOT_FOUND: \"CUSTOMER_NOT_FOUND\",\n SUBSCRIPTION_NOT_FOUND: \"SUBSCRIPTION_NOT_FOUND\",\n CHECKOUT_FAILED: \"CHECKOUT_FAILED\",\n PAYMENT_FAILED: \"PAYMENT_FAILED\",\n WEBHOOK_VERIFICATION_FAILED: \"WEBHOOK_VERIFICATION_FAILED\",\n API_ERROR: \"API_ERROR\",\n RATE_LIMITED: \"RATE_LIMITED\",\n} as const;\n","/**\n * @parsrun/payments - Paddle Provider\n * Edge-compatible Paddle provider using fetch API (Paddle Billing API v2)\n */\n\nimport type {\n CheckoutSession,\n CreateCheckoutOptions,\n CreateCustomerOptions,\n CreatePortalOptions,\n CreateSubscriptionOptions,\n Customer,\n PaddleProviderConfig,\n PaymentProvider,\n PortalSession,\n Price,\n Product,\n Subscription,\n SubscriptionStatus,\n UpdateSubscriptionOptions,\n WebhookEvent,\n WebhookEventType,\n} from \"../types.js\";\nimport { PaymentError, PaymentErrorCodes } from \"../types.js\";\n\n/**\n * Paddle Payment Provider\n * Edge-compatible using fetch API (Paddle Billing API v2)\n *\n * @example\n * ```typescript\n * const paddle = new PaddleProvider({\n * apiKey: process.env.PADDLE_API_KEY,\n * environment: 'sandbox', // or 'production'\n * webhookSecret: process.env.PADDLE_WEBHOOK_SECRET,\n * });\n *\n * const checkout = await paddle.createCheckout({\n * lineItems: [{ priceId: 'pri_xxx', quantity: 1 }],\n * successUrl: 'https://example.com/success',\n * cancelUrl: 'https://example.com/cancel',\n * mode: 'subscription',\n * });\n * ```\n */\nexport class PaddleProvider implements PaymentProvider {\n readonly type = \"paddle\" as const;\n\n private apiKey: string;\n private webhookSecret: string | undefined;\n private baseUrl: string;\n\n constructor(config: PaddleProviderConfig) {\n this.apiKey = config.apiKey;\n this.webhookSecret = config.webhookSecret;\n this.baseUrl =\n config.environment === \"production\"\n ? \"https://api.paddle.com\"\n : \"https://sandbox-api.paddle.com\";\n }\n\n private async request<T>(\n endpoint: string,\n options: {\n method?: string;\n body?: Record<string, unknown>;\n } = {}\n ): Promise<T> {\n const { method = \"GET\", body } = options;\n\n const headers: Record<string, string> = {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n const fetchOptions: RequestInit = {\n method,\n headers,\n };\n\n if (body) {\n fetchOptions.body = JSON.stringify(body);\n }\n\n const response = await fetch(`${this.baseUrl}${endpoint}`, fetchOptions);\n\n const data = await response.json() as {\n data?: T;\n error?: { type: string; code: string; detail: string };\n };\n\n if (!response.ok || data.error) {\n const errorMessage = data.error?.detail ?? `HTTP ${response.status}`;\n throw new PaymentError(\n `Paddle API error: ${errorMessage}`,\n data.error?.code ?? PaymentErrorCodes.API_ERROR,\n data.error\n );\n }\n\n return data.data as T;\n }\n\n // ============================================================================\n // Customer\n // ============================================================================\n\n async createCustomer(options: CreateCustomerOptions): Promise<Customer> {\n const body: Record<string, unknown> = {\n email: options.email,\n };\n\n if (options.name) body[\"name\"] = options.name;\n if (options.metadata) body[\"custom_data\"] = options.metadata;\n\n const result = await this.request<PaddleCustomer>(\"/customers\", {\n method: \"POST\",\n body,\n });\n\n return this.mapCustomer(result);\n }\n\n async getCustomer(customerId: string): Promise<Customer | null> {\n try {\n const result = await this.request<PaddleCustomer>(`/customers/${customerId}`);\n return this.mapCustomer(result);\n } catch (err) {\n if (err instanceof PaymentError && err.code === \"not_found\") {\n return null;\n }\n throw err;\n }\n }\n\n async updateCustomer(\n customerId: string,\n options: Partial<CreateCustomerOptions>\n ): Promise<Customer> {\n const body: Record<string, unknown> = {};\n\n if (options.email) body[\"email\"] = options.email;\n if (options.name) body[\"name\"] = options.name;\n if (options.metadata) body[\"custom_data\"] = options.metadata;\n\n const result = await this.request<PaddleCustomer>(`/customers/${customerId}`, {\n method: \"PATCH\",\n body,\n });\n\n return this.mapCustomer(result);\n }\n\n async deleteCustomer(_customerId: string): Promise<void> {\n // Paddle doesn't support customer deletion via API\n // Customers can only be archived\n throw new PaymentError(\n \"Paddle does not support customer deletion\",\n PaymentErrorCodes.API_ERROR\n );\n }\n\n private mapCustomer(paddle: PaddleCustomer): Customer {\n return {\n id: paddle.id,\n email: paddle.email,\n name: paddle.name ?? undefined,\n metadata: paddle.custom_data ?? undefined,\n providerData: paddle,\n };\n }\n\n // ============================================================================\n // Checkout\n // ============================================================================\n\n async createCheckout(options: CreateCheckoutOptions): Promise<CheckoutSession> {\n const items = options.lineItems.map((item) => ({\n price_id: item.priceId,\n quantity: item.quantity,\n }));\n\n const body: Record<string, unknown> = {\n items,\n };\n\n if (options.customerId) body[\"customer_id\"] = options.customerId;\n if (options.customerEmail) {\n body[\"customer\"] = { email: options.customerEmail };\n }\n if (options.metadata) body[\"custom_data\"] = options.metadata;\n\n // Paddle handles return URLs differently - they're configured in dashboard\n // or passed as settings\n body[\"settings\"] = {\n success_url: options.successUrl,\n };\n\n const result = await this.request<PaddleTransaction>(\"/transactions\", {\n method: \"POST\",\n body,\n });\n\n return {\n id: result.id,\n url: result.checkout?.url ?? \"\",\n customerId: result.customer_id ?? undefined,\n status: result.status === \"completed\" ? \"complete\" : \"open\",\n mode: result.subscription_id ? \"subscription\" : \"payment\",\n amountTotal: this.parsePaddleAmount(result.details?.totals?.total),\n currency: result.currency_code,\n providerData: result,\n };\n }\n\n async getCheckout(sessionId: string): Promise<CheckoutSession | null> {\n try {\n const result = await this.request<PaddleTransaction>(`/transactions/${sessionId}`);\n\n return {\n id: result.id,\n url: result.checkout?.url ?? \"\",\n customerId: result.customer_id ?? undefined,\n status: result.status === \"completed\" ? \"complete\" : \"open\",\n mode: result.subscription_id ? \"subscription\" : \"payment\",\n amountTotal: this.parsePaddleAmount(result.details?.totals?.total),\n currency: result.currency_code,\n providerData: result,\n };\n } catch (err) {\n if (err instanceof PaymentError && err.code === \"not_found\") {\n return null;\n }\n throw err;\n }\n }\n\n // ============================================================================\n // Subscriptions\n // ============================================================================\n\n async createSubscription(_options: CreateSubscriptionOptions): Promise<Subscription> {\n // Paddle subscriptions are created through the checkout flow\n // Direct subscription creation is not supported\n throw new PaymentError(\n \"Paddle subscriptions must be created through checkout\",\n PaymentErrorCodes.API_ERROR\n );\n }\n\n async getSubscription(subscriptionId: string): Promise<Subscription | null> {\n try {\n const result = await this.request<PaddleSubscription>(\n `/subscriptions/${subscriptionId}`\n );\n return this.mapSubscription(result);\n } catch (err) {\n if (err instanceof PaymentError && err.code === \"not_found\") {\n return null;\n }\n throw err;\n }\n }\n\n async updateSubscription(\n subscriptionId: string,\n options: UpdateSubscriptionOptions\n ): Promise<Subscription> {\n const body: Record<string, unknown> = {};\n\n if (options.priceId) {\n body[\"items\"] = [{ price_id: options.priceId, quantity: 1 }];\n }\n if (options.metadata) body[\"custom_data\"] = options.metadata;\n if (options.prorationBehavior) {\n body[\"proration_billing_mode\"] =\n options.prorationBehavior === \"none\" ? \"do_not_bill\" : \"prorated_immediately\";\n }\n\n const result = await this.request<PaddleSubscription>(\n `/subscriptions/${subscriptionId}`,\n { method: \"PATCH\", body }\n );\n\n return this.mapSubscription(result);\n }\n\n async cancelSubscription(\n subscriptionId: string,\n cancelAtPeriodEnd = true\n ): Promise<Subscription> {\n const body: Record<string, unknown> = {\n effective_from: cancelAtPeriodEnd ? \"next_billing_period\" : \"immediately\",\n };\n\n const result = await this.request<PaddleSubscription>(\n `/subscriptions/${subscriptionId}/cancel`,\n { method: \"POST\", body }\n );\n\n return this.mapSubscription(result);\n }\n\n async listSubscriptions(customerId: string): Promise<Subscription[]> {\n const result = await this.request<PaddleSubscription[]>(\n `/subscriptions?customer_id=${customerId}`\n );\n\n return result.map((sub) => this.mapSubscription(sub));\n }\n\n private mapSubscription(paddle: PaddleSubscription): Subscription {\n const item = paddle.items?.[0];\n\n const statusMap: Record<string, SubscriptionStatus> = {\n active: \"active\",\n canceled: \"canceled\",\n past_due: \"past_due\",\n paused: \"paused\",\n trialing: \"trialing\",\n };\n\n return {\n id: paddle.id,\n customerId: paddle.customer_id,\n status: statusMap[paddle.status] ?? \"active\",\n priceId: item?.price?.id ?? \"\",\n productId: item?.price?.product_id,\n currentPeriodStart: new Date(paddle.current_billing_period?.starts_at ?? Date.now()),\n currentPeriodEnd: new Date(paddle.current_billing_period?.ends_at ?? Date.now()),\n cancelAtPeriodEnd: paddle.scheduled_change?.action === \"cancel\",\n canceledAt: paddle.canceled_at ? new Date(paddle.canceled_at) : undefined,\n trialStart: paddle.started_at ? new Date(paddle.started_at) : undefined,\n trialEnd: paddle.first_billed_at ? new Date(paddle.first_billed_at) : undefined,\n metadata: paddle.custom_data ?? undefined,\n providerData: paddle,\n };\n }\n\n // ============================================================================\n // Portal\n // ============================================================================\n\n async createPortalSession(options: CreatePortalOptions): Promise<PortalSession> {\n // Paddle uses customer portal links that are generated per customer\n // Get customer to retrieve portal session\n const customer = await this.getCustomer(options.customerId);\n if (!customer) {\n throw new PaymentError(\n \"Customer not found\",\n PaymentErrorCodes.CUSTOMER_NOT_FOUND\n );\n }\n\n // In Paddle Billing, you need to create a portal session\n // This creates a session link for the customer portal\n const result = await this.request<{ urls: { general: { overview: string } } }>(\n `/customers/${options.customerId}/portal-sessions`,\n { method: \"POST\" }\n );\n\n return {\n url: result.urls.general.overview,\n returnUrl: options.returnUrl,\n };\n }\n\n // ============================================================================\n // Products & Prices\n // ============================================================================\n\n async getProduct(productId: string): Promise<Product | null> {\n try {\n const result = await this.request<PaddleProduct>(`/products/${productId}`);\n return this.mapProduct(result);\n } catch (err) {\n if (err instanceof PaymentError && err.code === \"not_found\") {\n return null;\n }\n throw err;\n }\n }\n\n async getPrice(priceId: string): Promise<Price | null> {\n try {\n const result = await this.request<PaddlePrice>(`/prices/${priceId}`);\n return this.mapPrice(result);\n } catch (err) {\n if (err instanceof PaymentError && err.code === \"not_found\") {\n return null;\n }\n throw err;\n }\n }\n\n async listPrices(productId?: string): Promise<Price[]> {\n let endpoint = \"/prices?status=active\";\n if (productId) {\n endpoint += `&product_id=${productId}`;\n }\n\n const result = await this.request<PaddlePrice[]>(endpoint);\n return result.map((price) => this.mapPrice(price));\n }\n\n private mapProduct(paddle: PaddleProduct): Product {\n return {\n id: paddle.id,\n name: paddle.name,\n description: paddle.description ?? undefined,\n active: paddle.status === \"active\",\n metadata: paddle.custom_data ?? undefined,\n providerData: paddle,\n };\n }\n\n private mapPrice(paddle: PaddlePrice): Price {\n const amount = paddle.unit_price?.amount\n ? parseInt(paddle.unit_price.amount, 10)\n : 0;\n\n return {\n id: paddle.id,\n productId: paddle.product_id,\n unitAmount: amount,\n currency: paddle.unit_price?.currency_code ?? \"USD\",\n recurring: paddle.billing_cycle\n ? {\n interval: paddle.billing_cycle.interval as \"day\" | \"week\" | \"month\" | \"year\",\n intervalCount: paddle.billing_cycle.frequency,\n }\n : undefined,\n active: paddle.status === \"active\",\n metadata: paddle.custom_data ?? undefined,\n providerData: paddle,\n };\n }\n\n private parsePaddleAmount(amount?: string): number | undefined {\n if (!amount) return undefined;\n return parseInt(amount, 10);\n }\n\n // ============================================================================\n // Webhooks\n // ============================================================================\n\n async verifyWebhook(\n payload: string | Uint8Array,\n signature: string\n ): Promise<WebhookEvent | null> {\n if (!this.webhookSecret) {\n throw new PaymentError(\n \"Webhook secret not configured\",\n PaymentErrorCodes.INVALID_CONFIG\n );\n }\n\n const payloadString = typeof payload === \"string\" ? payload : new TextDecoder().decode(payload);\n\n // Parse Paddle signature header (ts=xxx;h1=xxx)\n const signatureParts = signature.split(\";\").reduce((acc, part) => {\n const [key, value] = part.split(\"=\");\n if (key && value) {\n acc[key] = value;\n }\n return acc;\n }, {} as Record<string, string>);\n\n const timestamp = signatureParts[\"ts\"];\n const expectedSignature = signatureParts[\"h1\"];\n\n if (!timestamp || !expectedSignature) {\n return null;\n }\n\n // Compute expected signature\n const signedPayload = `${timestamp}:${payloadString}`;\n const computedSignature = await this.computeHmacSignature(\n signedPayload,\n this.webhookSecret\n );\n\n // Constant-time comparison\n if (!this.secureCompare(computedSignature, expectedSignature)) {\n return null;\n }\n\n // Parse event\n const event = JSON.parse(payloadString) as PaddleWebhookEvent;\n\n return {\n id: event.event_id,\n type: this.mapEventType(event.event_type),\n data: event.data,\n created: new Date(event.occurred_at),\n provider: \"paddle\",\n raw: event,\n };\n }\n\n private async computeHmacSignature(payload: string, secret: string): Promise<string> {\n const encoder = new TextEncoder();\n const keyData = encoder.encode(secret);\n const messageData = encoder.encode(payload);\n\n const cryptoKey = await crypto.subtle.importKey(\n \"raw\",\n keyData,\n { name: \"HMAC\", hash: \"SHA-256\" },\n false,\n [\"sign\"]\n );\n\n const signature = await crypto.subtle.sign(\"HMAC\", cryptoKey, messageData);\n const signatureArray = new Uint8Array(signature);\n\n return Array.from(signatureArray)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n }\n\n private secureCompare(a: string, b: string): boolean {\n if (a.length !== b.length) return false;\n\n let result = 0;\n for (let i = 0; i < a.length; i++) {\n result |= a.charCodeAt(i) ^ b.charCodeAt(i);\n }\n return result === 0;\n }\n\n private mapEventType(paddleType: string): WebhookEventType {\n const mapping: Record<string, WebhookEventType> = {\n \"transaction.completed\": \"checkout.session.completed\",\n \"customer.created\": \"customer.created\",\n \"customer.updated\": \"customer.updated\",\n \"subscription.created\": \"subscription.created\",\n \"subscription.updated\": \"subscription.updated\",\n \"subscription.canceled\": \"subscription.deleted\",\n \"subscription.past_due\": \"subscription.updated\",\n \"subscription.activated\": \"subscription.created\",\n \"transaction.payment_failed\": \"payment.failed\",\n \"adjustment.created\": \"refund.created\",\n };\n\n return mapping[paddleType] ?? (\"unknown\" as WebhookEventType);\n }\n}\n\n// ============================================================================\n// Paddle API Types\n// ============================================================================\n\ninterface PaddleCustomer {\n id: string;\n email: string;\n name: string | null;\n custom_data: Record<string, string> | null;\n}\n\ninterface PaddleTransaction {\n id: string;\n status: string;\n customer_id: string | null;\n subscription_id: string | null;\n currency_code: string;\n checkout?: {\n url: string;\n };\n details?: {\n totals?: {\n total: string;\n };\n };\n}\n\ninterface PaddleSubscription {\n id: string;\n customer_id: string;\n status: string;\n items?: Array<{\n price: {\n id: string;\n product_id: string;\n };\n }>;\n current_billing_period?: {\n starts_at: string;\n ends_at: string;\n };\n scheduled_change?: {\n action: string;\n };\n started_at: string | null;\n first_billed_at: string | null;\n canceled_at: string | null;\n custom_data: Record<string, string> | null;\n}\n\ninterface PaddleProduct {\n id: string;\n name: string;\n description: string | null;\n status: string;\n custom_data: Record<string, string> | null;\n}\n\ninterface PaddlePrice {\n id: string;\n product_id: string;\n status: string;\n unit_price?: {\n amount: string;\n currency_code: string;\n };\n billing_cycle?: {\n interval: string;\n frequency: number;\n };\n custom_data: Record<string, string> | null;\n}\n\ninterface PaddleWebhookEvent {\n event_id: string;\n event_type: string;\n occurred_at: string;\n data: unknown;\n}\n\n/**\n * Create a Paddle provider\n */\nexport function createPaddleProvider(config: PaddleProviderConfig): PaddleProvider {\n return new PaddleProvider(config);\n}\n"],"mappings":";AAMA;AAAA,EACE;AAAA,EACA;AAAA,EACA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,aAAa,YAAsC;AACzD,UAAM,UAA4C;AAAA,MAChD,yBAAyB;AAAA,MACzB,oBAAoB;AAAA,MACpB,oBAAoB;AAAA,MACpB,wBAAwB;AAAA,MACxB,wBAAwB;AAAA,MACxB,yBAAyB;AAAA,MACzB,yBAAyB;AAAA,MACzB,0BAA0B;AAAA,MAC1B,8BAA8B;AAAA,MAC9B,sBAAsB;AAAA,IACxB;AAEA,WAAO,QAAQ,UAAU,KAAM;AAAA,EACjC;AACF;AAqFO,SAAS,qBAAqB,QAA8C;AACjF,SAAO,IAAI,eAAe,MAAM;AAClC;","names":["price"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { PaymentProvider, StripeProviderConfig, CreateCustomerOptions, Customer, CreateCheckoutOptions, CheckoutSession, CreateSubscriptionOptions, Subscription, UpdateSubscriptionOptions, CreatePortalOptions, PortalSession, Product, Price, WebhookEvent } from '../types.js';
|
|
2
|
+
import '@parsrun/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @parsrun/payments - Stripe Provider
|
|
6
|
+
* Edge-compatible Stripe provider using fetch API
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Stripe Payment Provider
|
|
11
|
+
* Edge-compatible using fetch API
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const stripe = new StripeProvider({
|
|
16
|
+
* secretKey: process.env.STRIPE_SECRET_KEY,
|
|
17
|
+
* webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* const checkout = await stripe.createCheckout({
|
|
21
|
+
* lineItems: [{ priceId: 'price_xxx', quantity: 1 }],
|
|
22
|
+
* successUrl: 'https://example.com/success',
|
|
23
|
+
* cancelUrl: 'https://example.com/cancel',
|
|
24
|
+
* mode: 'subscription',
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare class StripeProvider implements PaymentProvider {
|
|
29
|
+
readonly type: "stripe";
|
|
30
|
+
private secretKey;
|
|
31
|
+
private webhookSecret;
|
|
32
|
+
private baseUrl;
|
|
33
|
+
private apiVersion;
|
|
34
|
+
constructor(config: StripeProviderConfig);
|
|
35
|
+
private request;
|
|
36
|
+
private encodeFormData;
|
|
37
|
+
createCustomer(options: CreateCustomerOptions): Promise<Customer>;
|
|
38
|
+
getCustomer(customerId: string): Promise<Customer | null>;
|
|
39
|
+
updateCustomer(customerId: string, options: Partial<CreateCustomerOptions>): Promise<Customer>;
|
|
40
|
+
deleteCustomer(customerId: string): Promise<void>;
|
|
41
|
+
private mapCustomer;
|
|
42
|
+
createCheckout(options: CreateCheckoutOptions): Promise<CheckoutSession>;
|
|
43
|
+
getCheckout(sessionId: string): Promise<CheckoutSession | null>;
|
|
44
|
+
private mapCheckoutSession;
|
|
45
|
+
createSubscription(options: CreateSubscriptionOptions): Promise<Subscription>;
|
|
46
|
+
getSubscription(subscriptionId: string): Promise<Subscription | null>;
|
|
47
|
+
updateSubscription(subscriptionId: string, options: UpdateSubscriptionOptions): Promise<Subscription>;
|
|
48
|
+
cancelSubscription(subscriptionId: string, cancelAtPeriodEnd?: boolean): Promise<Subscription>;
|
|
49
|
+
listSubscriptions(customerId: string): Promise<Subscription[]>;
|
|
50
|
+
private mapSubscription;
|
|
51
|
+
createPortalSession(options: CreatePortalOptions): Promise<PortalSession>;
|
|
52
|
+
getProduct(productId: string): Promise<Product | null>;
|
|
53
|
+
getPrice(priceId: string): Promise<Price | null>;
|
|
54
|
+
listPrices(productId?: string): Promise<Price[]>;
|
|
55
|
+
private mapProduct;
|
|
56
|
+
private mapPrice;
|
|
57
|
+
verifyWebhook(payload: string | Uint8Array, signature: string): Promise<WebhookEvent | null>;
|
|
58
|
+
private computeHmacSignature;
|
|
59
|
+
private secureCompare;
|
|
60
|
+
private mapEventType;
|
|
61
|
+
/**
|
|
62
|
+
* Report usage for metered billing
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // Report 100 API calls for a subscription item
|
|
67
|
+
* await stripe.reportUsage({
|
|
68
|
+
* subscriptionItemId: "si_xxx",
|
|
69
|
+
* quantity: 100,
|
|
70
|
+
* action: "increment", // or "set" to replace
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
reportUsage(record: {
|
|
75
|
+
subscriptionItemId: string;
|
|
76
|
+
quantity: number;
|
|
77
|
+
timestamp?: Date;
|
|
78
|
+
action?: "increment" | "set";
|
|
79
|
+
idempotencyKey?: string;
|
|
80
|
+
}): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Report multiple usage records (batch)
|
|
83
|
+
* Note: Stripe doesn't have a batch API, so this is sequential
|
|
84
|
+
*/
|
|
85
|
+
reportUsageBatch(records: Array<{
|
|
86
|
+
subscriptionItemId: string;
|
|
87
|
+
quantity: number;
|
|
88
|
+
timestamp?: Date;
|
|
89
|
+
action?: "increment" | "set";
|
|
90
|
+
idempotencyKey?: string;
|
|
91
|
+
}>): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Get subscription item ID for a subscription and price
|
|
94
|
+
*/
|
|
95
|
+
getSubscriptionItemId(subscriptionId: string, priceId: string): Promise<string | null>;
|
|
96
|
+
/**
|
|
97
|
+
* Get usage records for a subscription item
|
|
98
|
+
*/
|
|
99
|
+
getUsageRecords(subscriptionItemId: string, options?: {
|
|
100
|
+
startingAfter?: string;
|
|
101
|
+
endingBefore?: string;
|
|
102
|
+
limit?: number;
|
|
103
|
+
}): Promise<{
|
|
104
|
+
data: Array<{
|
|
105
|
+
id: string;
|
|
106
|
+
quantity: number;
|
|
107
|
+
timestamp: Date;
|
|
108
|
+
subscriptionItem: string;
|
|
109
|
+
}>;
|
|
110
|
+
hasMore: boolean;
|
|
111
|
+
}>;
|
|
112
|
+
/**
|
|
113
|
+
* Get current period usage total for a subscription item
|
|
114
|
+
*/
|
|
115
|
+
getCurrentUsage(subscriptionItemId: string): Promise<number>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a Stripe provider
|
|
119
|
+
*/
|
|
120
|
+
declare function createStripeProvider(config: StripeProviderConfig): StripeProvider;
|
|
121
|
+
|
|
122
|
+
export { StripeProvider, createStripeProvider };
|