@paykit-sdk/polar 1.1.97 → 1.1.99
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 +20 -18
- package/dist/index.js +106 -78
- package/dist/index.mjs +106 -78
- package/dist/polar-provider.js +106 -78
- package/dist/polar-provider.mjs +106 -78
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -86,10 +86,11 @@ export async function POST(request: NextRequest) {
|
|
|
86
86
|
});
|
|
87
87
|
|
|
88
88
|
const body = await request.text();
|
|
89
|
-
const headers =
|
|
90
|
-
|
|
89
|
+
const headers = request.headers;
|
|
90
|
+
const fullUrl = request.url;
|
|
91
|
+
|
|
92
|
+
await webhook.handle({ body, headers, fullUrl });
|
|
91
93
|
|
|
92
|
-
// Return immediately, processing happens in background
|
|
93
94
|
return NextResponse.json({ success: true });
|
|
94
95
|
}
|
|
95
96
|
```
|
|
@@ -131,11 +132,12 @@ app.post(
|
|
|
131
132
|
console.log('Refund created:', event.data);
|
|
132
133
|
});
|
|
133
134
|
|
|
134
|
-
const body = req.body
|
|
135
|
-
const headers = req.headers;
|
|
136
|
-
|
|
135
|
+
const body = typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
|
|
136
|
+
const headers = new Headers(Object.entries(req.headers) as [string, string][]);
|
|
137
|
+
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
|
|
138
|
+
|
|
139
|
+
await webhook.handle({ body, headers, fullUrl });
|
|
137
140
|
|
|
138
|
-
// Return immediately, processing happens in background
|
|
139
141
|
res.json({ success: true });
|
|
140
142
|
} catch (error) {
|
|
141
143
|
console.error('Webhook error:', error);
|
|
@@ -174,17 +176,17 @@ app.listen(3000, () => {
|
|
|
174
176
|
});
|
|
175
177
|
```
|
|
176
178
|
|
|
177
|
-
##
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
179
|
+
## Webhook Events
|
|
180
|
+
|
|
181
|
+
- order.paid
|
|
182
|
+
- order.created
|
|
183
|
+
- customer.created
|
|
184
|
+
- customer.updated
|
|
185
|
+
- customer.deleted
|
|
186
|
+
- subscription.updated
|
|
187
|
+
- subscription.created
|
|
188
|
+
- subscription.revoked
|
|
189
|
+
- refund.created
|
|
188
190
|
|
|
189
191
|
## Environment Variables
|
|
190
192
|
|
package/dist/index.js
CHANGED
|
@@ -4040,14 +4040,14 @@ var ostring = () => stringType().optional();
|
|
|
4040
4040
|
var onumber = () => numberType().optional();
|
|
4041
4041
|
var oboolean = () => booleanType().optional();
|
|
4042
4042
|
var coerce = {
|
|
4043
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4044
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4045
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4043
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4044
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4045
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4046
4046
|
...arg,
|
|
4047
4047
|
coerce: true
|
|
4048
|
-
}),
|
|
4049
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4050
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4048
|
+
})),
|
|
4049
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4050
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4051
4051
|
};
|
|
4052
4052
|
var NEVER = INVALID;
|
|
4053
4053
|
var paykitCheckout$InboundSchema = (checkout) => {
|
|
@@ -4057,7 +4057,7 @@ var paykitCheckout$InboundSchema = (checkout) => {
|
|
|
4057
4057
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4058
4058
|
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
4059
4059
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
4060
|
-
metadata: checkout.metadata ?? null,
|
|
4060
|
+
metadata: core.omitInternalMetadata(checkout.metadata) ?? null,
|
|
4061
4061
|
currency: checkout.currency,
|
|
4062
4062
|
amount: checkout.amount
|
|
4063
4063
|
};
|
|
@@ -4069,7 +4069,15 @@ var paykitCustomer$InboundSchema = (customer) => {
|
|
|
4069
4069
|
email: customer.email,
|
|
4070
4070
|
name: customer.name ?? "",
|
|
4071
4071
|
phone,
|
|
4072
|
-
metadata: core.omitInternalMetadata(customer.metadata ?? {})
|
|
4072
|
+
metadata: core.omitInternalMetadata(customer.metadata ?? {}),
|
|
4073
|
+
created_at: customer.createdAt,
|
|
4074
|
+
updated_at: customer.modifiedAt ?? null,
|
|
4075
|
+
custom_fields: {
|
|
4076
|
+
emailVerified: customer.emailVerified,
|
|
4077
|
+
taxId: customer.taxId,
|
|
4078
|
+
avatarUrl: customer.avatarUrl,
|
|
4079
|
+
billingAddress: customer.billingAddress
|
|
4080
|
+
}
|
|
4073
4081
|
};
|
|
4074
4082
|
};
|
|
4075
4083
|
var toPaykitSubscriptionStatus = (status) => {
|
|
@@ -4091,7 +4099,9 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
4091
4099
|
item_id: subscription.productId,
|
|
4092
4100
|
billing_interval: subscription.recurringInterval,
|
|
4093
4101
|
currency: subscription.currency,
|
|
4094
|
-
amount: subscription.amount
|
|
4102
|
+
amount: subscription.amount,
|
|
4103
|
+
requires_action: false,
|
|
4104
|
+
payment_url: null
|
|
4095
4105
|
};
|
|
4096
4106
|
};
|
|
4097
4107
|
var paykitInvoice$InboundSchema = (invoice) => {
|
|
@@ -4110,7 +4120,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
|
|
|
4110
4120
|
status,
|
|
4111
4121
|
subscription_id: invoice.subscription?.id ?? null,
|
|
4112
4122
|
paid_at: new Date(invoice.createdAt).toISOString(),
|
|
4113
|
-
line_items:
|
|
4123
|
+
line_items: invoice.items.map(({ productPriceId }) => ({
|
|
4124
|
+
id: productPriceId ?? "",
|
|
4125
|
+
quantity: invoice.items.length
|
|
4126
|
+
}))
|
|
4114
4127
|
};
|
|
4115
4128
|
};
|
|
4116
4129
|
var paykitPayment$InboundSchema = (checkout) => {
|
|
@@ -4127,8 +4140,10 @@ var paykitPayment$InboundSchema = (checkout) => {
|
|
|
4127
4140
|
currency: checkout.currency,
|
|
4128
4141
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4129
4142
|
status: statusMap[checkout.status],
|
|
4130
|
-
metadata: checkout.metadata ?? {},
|
|
4131
|
-
item_id: checkout.products.length > 0 ? checkout.products[0].id : null
|
|
4143
|
+
metadata: core.omitInternalMetadata(checkout.metadata) ?? {},
|
|
4144
|
+
item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
|
|
4145
|
+
requires_action: checkout.status === "open" ? true : false,
|
|
4146
|
+
payment_url: checkout.status === "open" ? checkout.url : null
|
|
4132
4147
|
};
|
|
4133
4148
|
};
|
|
4134
4149
|
var mapRefundReason = (debug, reason) => {
|
|
@@ -4186,10 +4201,16 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4186
4201
|
const { metadata, item_id, provider_metadata } = data;
|
|
4187
4202
|
const checkoutMetadata = core.stringifyMetadataValues(metadata ?? {});
|
|
4188
4203
|
const checkoutCreateOptions = {
|
|
4204
|
+
...provider_metadata,
|
|
4189
4205
|
metadata: checkoutMetadata,
|
|
4190
4206
|
products: [item_id],
|
|
4191
|
-
|
|
4207
|
+
successUrl: data.success_url
|
|
4192
4208
|
};
|
|
4209
|
+
if (typeof data.customer === "object" && "email" in data.customer) {
|
|
4210
|
+
checkoutCreateOptions.customerEmail = data.customer.email;
|
|
4211
|
+
} else if (typeof data.customer === "string") {
|
|
4212
|
+
checkoutCreateOptions.customerId = data.customer;
|
|
4213
|
+
}
|
|
4193
4214
|
if (data.billing) {
|
|
4194
4215
|
checkoutCreateOptions.customerBillingAddress = {
|
|
4195
4216
|
line1: data.billing.address.line1,
|
|
@@ -4324,9 +4345,15 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4324
4345
|
if (error) {
|
|
4325
4346
|
throw core.ValidationError.fromZodError(error, this.providerName, "updateSubscription");
|
|
4326
4347
|
}
|
|
4348
|
+
if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
|
|
4349
|
+
throw new core.ValidationError(
|
|
4350
|
+
"Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
|
|
4351
|
+
{ provider: this.providerName, method: "updateSubscription" }
|
|
4352
|
+
);
|
|
4353
|
+
}
|
|
4327
4354
|
const response = await this.polar.subscriptions.update({
|
|
4328
4355
|
id,
|
|
4329
|
-
subscriptionUpdate:
|
|
4356
|
+
subscriptionUpdate: data.provider_metadata
|
|
4330
4357
|
});
|
|
4331
4358
|
return paykitSubscription$InboundSchema(response);
|
|
4332
4359
|
};
|
|
@@ -4384,10 +4411,11 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4384
4411
|
const checkoutResponse = await this.polar.checkouts.update({
|
|
4385
4412
|
id,
|
|
4386
4413
|
checkoutUpdate: {
|
|
4387
|
-
...
|
|
4414
|
+
...provider_metadata,
|
|
4388
4415
|
...rest.metadata && { metadata: paymentMetadata },
|
|
4389
4416
|
...rest.item_id && { products: [rest.item_id] },
|
|
4390
|
-
...
|
|
4417
|
+
...rest.amount && { amount: rest.amount },
|
|
4418
|
+
...rest.currency && { currency: rest.currency }
|
|
4391
4419
|
}
|
|
4392
4420
|
});
|
|
4393
4421
|
return paykitPayment$InboundSchema(checkoutResponse);
|
|
@@ -4448,71 +4476,67 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4448
4476
|
*/
|
|
4449
4477
|
"order.paid": (data2) => {
|
|
4450
4478
|
const { status, metadata } = data2;
|
|
4451
|
-
if (status
|
|
4452
|
-
|
|
4453
|
-
...data2,
|
|
4454
|
-
billingMode: core.billingModeSchema.parse("one_time"),
|
|
4455
|
-
metadata: { ...metadata ?? {} }
|
|
4456
|
-
});
|
|
4457
|
-
const payment = {
|
|
4458
|
-
id: data2.id,
|
|
4459
|
-
amount: data2.totalAmount,
|
|
4460
|
-
currency: data2.currency,
|
|
4461
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4462
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4463
|
-
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4464
|
-
item_id: data2.product.id
|
|
4465
|
-
};
|
|
4466
|
-
return [
|
|
4467
|
-
core.paykitEvent$InboundSchema({
|
|
4468
|
-
type: "payment.updated",
|
|
4469
|
-
created: parseInt(webhookTimestamp),
|
|
4470
|
-
id: webhookId,
|
|
4471
|
-
data: payment
|
|
4472
|
-
}),
|
|
4473
|
-
core.paykitEvent$InboundSchema({
|
|
4474
|
-
type: "invoice.generated",
|
|
4475
|
-
created: parseInt(webhookTimestamp),
|
|
4476
|
-
id: webhookId,
|
|
4477
|
-
data: invoice
|
|
4478
|
-
})
|
|
4479
|
-
];
|
|
4479
|
+
if (status !== "paid") {
|
|
4480
|
+
return null;
|
|
4480
4481
|
}
|
|
4481
|
-
|
|
4482
|
+
const isSubscription = ["subscription_create", "subscription_cycle"].includes(
|
|
4483
|
+
data2.billingReason
|
|
4484
|
+
);
|
|
4485
|
+
const invoice = paykitInvoice$InboundSchema({
|
|
4486
|
+
...data2,
|
|
4487
|
+
billingMode: core.billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
|
|
4488
|
+
metadata: { ...metadata ?? {} }
|
|
4489
|
+
});
|
|
4490
|
+
const payment = {
|
|
4491
|
+
id: data2.id,
|
|
4492
|
+
amount: data2.totalAmount,
|
|
4493
|
+
currency: data2.currency,
|
|
4494
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4495
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4496
|
+
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4497
|
+
item_id: data2.product.id,
|
|
4498
|
+
requires_action: false,
|
|
4499
|
+
payment_url: null
|
|
4500
|
+
};
|
|
4501
|
+
return [
|
|
4502
|
+
core.paykitEvent$InboundSchema({
|
|
4503
|
+
type: "payment.updated",
|
|
4504
|
+
created: parseInt(webhookTimestamp),
|
|
4505
|
+
id: webhookId,
|
|
4506
|
+
data: payment
|
|
4507
|
+
}),
|
|
4508
|
+
core.paykitEvent$InboundSchema({
|
|
4509
|
+
type: "invoice.generated",
|
|
4510
|
+
created: parseInt(webhookTimestamp),
|
|
4511
|
+
id: webhookId,
|
|
4512
|
+
data: invoice
|
|
4513
|
+
})
|
|
4514
|
+
];
|
|
4482
4515
|
},
|
|
4483
4516
|
"order.created": (data2) => {
|
|
4484
4517
|
const { billingReason, metadata, status } = data2;
|
|
4485
|
-
if (
|
|
4486
|
-
|
|
4487
|
-
...data2,
|
|
4488
|
-
billingMode: core.billingModeSchema.parse("recurring"),
|
|
4489
|
-
metadata: { ...metadata ?? {} }
|
|
4490
|
-
});
|
|
4491
|
-
const payment = {
|
|
4492
|
-
id: data2.id,
|
|
4493
|
-
amount: data2.totalAmount,
|
|
4494
|
-
currency: data2.currency,
|
|
4495
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4496
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4497
|
-
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4498
|
-
item_id: data2.product.id
|
|
4499
|
-
};
|
|
4500
|
-
return [
|
|
4501
|
-
core.paykitEvent$InboundSchema({
|
|
4502
|
-
type: "payment.created",
|
|
4503
|
-
created: parseInt(webhookTimestamp),
|
|
4504
|
-
id: webhookId,
|
|
4505
|
-
data: payment
|
|
4506
|
-
}),
|
|
4507
|
-
core.paykitEvent$InboundSchema({
|
|
4508
|
-
type: "invoice.generated",
|
|
4509
|
-
created: parseInt(webhookTimestamp),
|
|
4510
|
-
id: webhookId,
|
|
4511
|
-
data: invoice
|
|
4512
|
-
})
|
|
4513
|
-
];
|
|
4518
|
+
if (status !== "paid") {
|
|
4519
|
+
return null;
|
|
4514
4520
|
}
|
|
4515
|
-
|
|
4521
|
+
const payment = {
|
|
4522
|
+
id: data2.id,
|
|
4523
|
+
amount: data2.totalAmount,
|
|
4524
|
+
currency: data2.currency,
|
|
4525
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4526
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4527
|
+
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4528
|
+
item_id: data2.product.id,
|
|
4529
|
+
requires_action: data2.status === "paid" ? false : true,
|
|
4530
|
+
payment_url: null
|
|
4531
|
+
};
|
|
4532
|
+
return [
|
|
4533
|
+
core.paykitEvent$InboundSchema({
|
|
4534
|
+
type: "payment.created",
|
|
4535
|
+
created: parseInt(webhookTimestamp),
|
|
4536
|
+
id: webhookId,
|
|
4537
|
+
data: payment
|
|
4538
|
+
})
|
|
4539
|
+
];
|
|
4516
4540
|
},
|
|
4517
4541
|
/**
|
|
4518
4542
|
* Customer
|
|
@@ -4602,8 +4626,12 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4602
4626
|
if (!handler)
|
|
4603
4627
|
throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
|
|
4604
4628
|
const results = handler(data);
|
|
4605
|
-
if (!results)
|
|
4606
|
-
|
|
4629
|
+
if (!results) {
|
|
4630
|
+
console.log(
|
|
4631
|
+
`Skipping event ${type} for provider: ${this.providerName} as no action needed`
|
|
4632
|
+
);
|
|
4633
|
+
return [];
|
|
4634
|
+
}
|
|
4607
4635
|
return results;
|
|
4608
4636
|
};
|
|
4609
4637
|
const { accessToken, isSandbox, debug = true, ...rest } = config;
|
package/dist/index.mjs
CHANGED
|
@@ -4038,14 +4038,14 @@ var ostring = () => stringType().optional();
|
|
|
4038
4038
|
var onumber = () => numberType().optional();
|
|
4039
4039
|
var oboolean = () => booleanType().optional();
|
|
4040
4040
|
var coerce = {
|
|
4041
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4042
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4043
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4041
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4042
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4043
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4044
4044
|
...arg,
|
|
4045
4045
|
coerce: true
|
|
4046
|
-
}),
|
|
4047
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4048
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4046
|
+
})),
|
|
4047
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4048
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4049
4049
|
};
|
|
4050
4050
|
var NEVER = INVALID;
|
|
4051
4051
|
var paykitCheckout$InboundSchema = (checkout) => {
|
|
@@ -4055,7 +4055,7 @@ var paykitCheckout$InboundSchema = (checkout) => {
|
|
|
4055
4055
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4056
4056
|
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
4057
4057
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
4058
|
-
metadata: checkout.metadata ?? null,
|
|
4058
|
+
metadata: omitInternalMetadata(checkout.metadata) ?? null,
|
|
4059
4059
|
currency: checkout.currency,
|
|
4060
4060
|
amount: checkout.amount
|
|
4061
4061
|
};
|
|
@@ -4067,7 +4067,15 @@ var paykitCustomer$InboundSchema = (customer) => {
|
|
|
4067
4067
|
email: customer.email,
|
|
4068
4068
|
name: customer.name ?? "",
|
|
4069
4069
|
phone,
|
|
4070
|
-
metadata: omitInternalMetadata(customer.metadata ?? {})
|
|
4070
|
+
metadata: omitInternalMetadata(customer.metadata ?? {}),
|
|
4071
|
+
created_at: customer.createdAt,
|
|
4072
|
+
updated_at: customer.modifiedAt ?? null,
|
|
4073
|
+
custom_fields: {
|
|
4074
|
+
emailVerified: customer.emailVerified,
|
|
4075
|
+
taxId: customer.taxId,
|
|
4076
|
+
avatarUrl: customer.avatarUrl,
|
|
4077
|
+
billingAddress: customer.billingAddress
|
|
4078
|
+
}
|
|
4071
4079
|
};
|
|
4072
4080
|
};
|
|
4073
4081
|
var toPaykitSubscriptionStatus = (status) => {
|
|
@@ -4089,7 +4097,9 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
4089
4097
|
item_id: subscription.productId,
|
|
4090
4098
|
billing_interval: subscription.recurringInterval,
|
|
4091
4099
|
currency: subscription.currency,
|
|
4092
|
-
amount: subscription.amount
|
|
4100
|
+
amount: subscription.amount,
|
|
4101
|
+
requires_action: false,
|
|
4102
|
+
payment_url: null
|
|
4093
4103
|
};
|
|
4094
4104
|
};
|
|
4095
4105
|
var paykitInvoice$InboundSchema = (invoice) => {
|
|
@@ -4108,7 +4118,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
|
|
|
4108
4118
|
status,
|
|
4109
4119
|
subscription_id: invoice.subscription?.id ?? null,
|
|
4110
4120
|
paid_at: new Date(invoice.createdAt).toISOString(),
|
|
4111
|
-
line_items:
|
|
4121
|
+
line_items: invoice.items.map(({ productPriceId }) => ({
|
|
4122
|
+
id: productPriceId ?? "",
|
|
4123
|
+
quantity: invoice.items.length
|
|
4124
|
+
}))
|
|
4112
4125
|
};
|
|
4113
4126
|
};
|
|
4114
4127
|
var paykitPayment$InboundSchema = (checkout) => {
|
|
@@ -4125,8 +4138,10 @@ var paykitPayment$InboundSchema = (checkout) => {
|
|
|
4125
4138
|
currency: checkout.currency,
|
|
4126
4139
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4127
4140
|
status: statusMap[checkout.status],
|
|
4128
|
-
metadata: checkout.metadata ?? {},
|
|
4129
|
-
item_id: checkout.products.length > 0 ? checkout.products[0].id : null
|
|
4141
|
+
metadata: omitInternalMetadata(checkout.metadata) ?? {},
|
|
4142
|
+
item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
|
|
4143
|
+
requires_action: checkout.status === "open" ? true : false,
|
|
4144
|
+
payment_url: checkout.status === "open" ? checkout.url : null
|
|
4130
4145
|
};
|
|
4131
4146
|
};
|
|
4132
4147
|
var mapRefundReason = (debug, reason) => {
|
|
@@ -4184,10 +4199,16 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4184
4199
|
const { metadata, item_id, provider_metadata } = data;
|
|
4185
4200
|
const checkoutMetadata = stringifyMetadataValues(metadata ?? {});
|
|
4186
4201
|
const checkoutCreateOptions = {
|
|
4202
|
+
...provider_metadata,
|
|
4187
4203
|
metadata: checkoutMetadata,
|
|
4188
4204
|
products: [item_id],
|
|
4189
|
-
|
|
4205
|
+
successUrl: data.success_url
|
|
4190
4206
|
};
|
|
4207
|
+
if (typeof data.customer === "object" && "email" in data.customer) {
|
|
4208
|
+
checkoutCreateOptions.customerEmail = data.customer.email;
|
|
4209
|
+
} else if (typeof data.customer === "string") {
|
|
4210
|
+
checkoutCreateOptions.customerId = data.customer;
|
|
4211
|
+
}
|
|
4191
4212
|
if (data.billing) {
|
|
4192
4213
|
checkoutCreateOptions.customerBillingAddress = {
|
|
4193
4214
|
line1: data.billing.address.line1,
|
|
@@ -4322,9 +4343,15 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4322
4343
|
if (error) {
|
|
4323
4344
|
throw ValidationError.fromZodError(error, this.providerName, "updateSubscription");
|
|
4324
4345
|
}
|
|
4346
|
+
if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
|
|
4347
|
+
throw new ValidationError(
|
|
4348
|
+
"Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
|
|
4349
|
+
{ provider: this.providerName, method: "updateSubscription" }
|
|
4350
|
+
);
|
|
4351
|
+
}
|
|
4325
4352
|
const response = await this.polar.subscriptions.update({
|
|
4326
4353
|
id,
|
|
4327
|
-
subscriptionUpdate:
|
|
4354
|
+
subscriptionUpdate: data.provider_metadata
|
|
4328
4355
|
});
|
|
4329
4356
|
return paykitSubscription$InboundSchema(response);
|
|
4330
4357
|
};
|
|
@@ -4382,10 +4409,11 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4382
4409
|
const checkoutResponse = await this.polar.checkouts.update({
|
|
4383
4410
|
id,
|
|
4384
4411
|
checkoutUpdate: {
|
|
4385
|
-
...
|
|
4412
|
+
...provider_metadata,
|
|
4386
4413
|
...rest.metadata && { metadata: paymentMetadata },
|
|
4387
4414
|
...rest.item_id && { products: [rest.item_id] },
|
|
4388
|
-
...
|
|
4415
|
+
...rest.amount && { amount: rest.amount },
|
|
4416
|
+
...rest.currency && { currency: rest.currency }
|
|
4389
4417
|
}
|
|
4390
4418
|
});
|
|
4391
4419
|
return paykitPayment$InboundSchema(checkoutResponse);
|
|
@@ -4446,71 +4474,67 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4446
4474
|
*/
|
|
4447
4475
|
"order.paid": (data2) => {
|
|
4448
4476
|
const { status, metadata } = data2;
|
|
4449
|
-
if (status
|
|
4450
|
-
|
|
4451
|
-
...data2,
|
|
4452
|
-
billingMode: billingModeSchema.parse("one_time"),
|
|
4453
|
-
metadata: { ...metadata ?? {} }
|
|
4454
|
-
});
|
|
4455
|
-
const payment = {
|
|
4456
|
-
id: data2.id,
|
|
4457
|
-
amount: data2.totalAmount,
|
|
4458
|
-
currency: data2.currency,
|
|
4459
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4460
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4461
|
-
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4462
|
-
item_id: data2.product.id
|
|
4463
|
-
};
|
|
4464
|
-
return [
|
|
4465
|
-
paykitEvent$InboundSchema({
|
|
4466
|
-
type: "payment.updated",
|
|
4467
|
-
created: parseInt(webhookTimestamp),
|
|
4468
|
-
id: webhookId,
|
|
4469
|
-
data: payment
|
|
4470
|
-
}),
|
|
4471
|
-
paykitEvent$InboundSchema({
|
|
4472
|
-
type: "invoice.generated",
|
|
4473
|
-
created: parseInt(webhookTimestamp),
|
|
4474
|
-
id: webhookId,
|
|
4475
|
-
data: invoice
|
|
4476
|
-
})
|
|
4477
|
-
];
|
|
4477
|
+
if (status !== "paid") {
|
|
4478
|
+
return null;
|
|
4478
4479
|
}
|
|
4479
|
-
|
|
4480
|
+
const isSubscription = ["subscription_create", "subscription_cycle"].includes(
|
|
4481
|
+
data2.billingReason
|
|
4482
|
+
);
|
|
4483
|
+
const invoice = paykitInvoice$InboundSchema({
|
|
4484
|
+
...data2,
|
|
4485
|
+
billingMode: billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
|
|
4486
|
+
metadata: { ...metadata ?? {} }
|
|
4487
|
+
});
|
|
4488
|
+
const payment = {
|
|
4489
|
+
id: data2.id,
|
|
4490
|
+
amount: data2.totalAmount,
|
|
4491
|
+
currency: data2.currency,
|
|
4492
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4493
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4494
|
+
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4495
|
+
item_id: data2.product.id,
|
|
4496
|
+
requires_action: false,
|
|
4497
|
+
payment_url: null
|
|
4498
|
+
};
|
|
4499
|
+
return [
|
|
4500
|
+
paykitEvent$InboundSchema({
|
|
4501
|
+
type: "payment.updated",
|
|
4502
|
+
created: parseInt(webhookTimestamp),
|
|
4503
|
+
id: webhookId,
|
|
4504
|
+
data: payment
|
|
4505
|
+
}),
|
|
4506
|
+
paykitEvent$InboundSchema({
|
|
4507
|
+
type: "invoice.generated",
|
|
4508
|
+
created: parseInt(webhookTimestamp),
|
|
4509
|
+
id: webhookId,
|
|
4510
|
+
data: invoice
|
|
4511
|
+
})
|
|
4512
|
+
];
|
|
4480
4513
|
},
|
|
4481
4514
|
"order.created": (data2) => {
|
|
4482
4515
|
const { billingReason, metadata, status } = data2;
|
|
4483
|
-
if (
|
|
4484
|
-
|
|
4485
|
-
...data2,
|
|
4486
|
-
billingMode: billingModeSchema.parse("recurring"),
|
|
4487
|
-
metadata: { ...metadata ?? {} }
|
|
4488
|
-
});
|
|
4489
|
-
const payment = {
|
|
4490
|
-
id: data2.id,
|
|
4491
|
-
amount: data2.totalAmount,
|
|
4492
|
-
currency: data2.currency,
|
|
4493
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4494
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4495
|
-
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4496
|
-
item_id: data2.product.id
|
|
4497
|
-
};
|
|
4498
|
-
return [
|
|
4499
|
-
paykitEvent$InboundSchema({
|
|
4500
|
-
type: "payment.created",
|
|
4501
|
-
created: parseInt(webhookTimestamp),
|
|
4502
|
-
id: webhookId,
|
|
4503
|
-
data: payment
|
|
4504
|
-
}),
|
|
4505
|
-
paykitEvent$InboundSchema({
|
|
4506
|
-
type: "invoice.generated",
|
|
4507
|
-
created: parseInt(webhookTimestamp),
|
|
4508
|
-
id: webhookId,
|
|
4509
|
-
data: invoice
|
|
4510
|
-
})
|
|
4511
|
-
];
|
|
4516
|
+
if (status !== "paid") {
|
|
4517
|
+
return null;
|
|
4512
4518
|
}
|
|
4513
|
-
|
|
4519
|
+
const payment = {
|
|
4520
|
+
id: data2.id,
|
|
4521
|
+
amount: data2.totalAmount,
|
|
4522
|
+
currency: data2.currency,
|
|
4523
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4524
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4525
|
+
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4526
|
+
item_id: data2.product.id,
|
|
4527
|
+
requires_action: data2.status === "paid" ? false : true,
|
|
4528
|
+
payment_url: null
|
|
4529
|
+
};
|
|
4530
|
+
return [
|
|
4531
|
+
paykitEvent$InboundSchema({
|
|
4532
|
+
type: "payment.created",
|
|
4533
|
+
created: parseInt(webhookTimestamp),
|
|
4534
|
+
id: webhookId,
|
|
4535
|
+
data: payment
|
|
4536
|
+
})
|
|
4537
|
+
];
|
|
4514
4538
|
},
|
|
4515
4539
|
/**
|
|
4516
4540
|
* Customer
|
|
@@ -4600,8 +4624,12 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4600
4624
|
if (!handler)
|
|
4601
4625
|
throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
|
|
4602
4626
|
const results = handler(data);
|
|
4603
|
-
if (!results)
|
|
4604
|
-
|
|
4627
|
+
if (!results) {
|
|
4628
|
+
console.log(
|
|
4629
|
+
`Skipping event ${type} for provider: ${this.providerName} as no action needed`
|
|
4630
|
+
);
|
|
4631
|
+
return [];
|
|
4632
|
+
}
|
|
4605
4633
|
return results;
|
|
4606
4634
|
};
|
|
4607
4635
|
const { accessToken, isSandbox, debug = true, ...rest } = config;
|
package/dist/polar-provider.js
CHANGED
|
@@ -4040,14 +4040,14 @@ var ostring = () => stringType().optional();
|
|
|
4040
4040
|
var onumber = () => numberType().optional();
|
|
4041
4041
|
var oboolean = () => booleanType().optional();
|
|
4042
4042
|
var coerce = {
|
|
4043
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4044
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4045
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4043
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4044
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4045
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4046
4046
|
...arg,
|
|
4047
4047
|
coerce: true
|
|
4048
|
-
}),
|
|
4049
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4050
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4048
|
+
})),
|
|
4049
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4050
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4051
4051
|
};
|
|
4052
4052
|
var NEVER = INVALID;
|
|
4053
4053
|
var paykitCheckout$InboundSchema = (checkout) => {
|
|
@@ -4057,7 +4057,7 @@ var paykitCheckout$InboundSchema = (checkout) => {
|
|
|
4057
4057
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4058
4058
|
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
4059
4059
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
4060
|
-
metadata: checkout.metadata ?? null,
|
|
4060
|
+
metadata: core.omitInternalMetadata(checkout.metadata) ?? null,
|
|
4061
4061
|
currency: checkout.currency,
|
|
4062
4062
|
amount: checkout.amount
|
|
4063
4063
|
};
|
|
@@ -4069,7 +4069,15 @@ var paykitCustomer$InboundSchema = (customer) => {
|
|
|
4069
4069
|
email: customer.email,
|
|
4070
4070
|
name: customer.name ?? "",
|
|
4071
4071
|
phone,
|
|
4072
|
-
metadata: core.omitInternalMetadata(customer.metadata ?? {})
|
|
4072
|
+
metadata: core.omitInternalMetadata(customer.metadata ?? {}),
|
|
4073
|
+
created_at: customer.createdAt,
|
|
4074
|
+
updated_at: customer.modifiedAt ?? null,
|
|
4075
|
+
custom_fields: {
|
|
4076
|
+
emailVerified: customer.emailVerified,
|
|
4077
|
+
taxId: customer.taxId,
|
|
4078
|
+
avatarUrl: customer.avatarUrl,
|
|
4079
|
+
billingAddress: customer.billingAddress
|
|
4080
|
+
}
|
|
4073
4081
|
};
|
|
4074
4082
|
};
|
|
4075
4083
|
var toPaykitSubscriptionStatus = (status) => {
|
|
@@ -4091,7 +4099,9 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
4091
4099
|
item_id: subscription.productId,
|
|
4092
4100
|
billing_interval: subscription.recurringInterval,
|
|
4093
4101
|
currency: subscription.currency,
|
|
4094
|
-
amount: subscription.amount
|
|
4102
|
+
amount: subscription.amount,
|
|
4103
|
+
requires_action: false,
|
|
4104
|
+
payment_url: null
|
|
4095
4105
|
};
|
|
4096
4106
|
};
|
|
4097
4107
|
var paykitInvoice$InboundSchema = (invoice) => {
|
|
@@ -4110,7 +4120,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
|
|
|
4110
4120
|
status,
|
|
4111
4121
|
subscription_id: invoice.subscription?.id ?? null,
|
|
4112
4122
|
paid_at: new Date(invoice.createdAt).toISOString(),
|
|
4113
|
-
line_items:
|
|
4123
|
+
line_items: invoice.items.map(({ productPriceId }) => ({
|
|
4124
|
+
id: productPriceId ?? "",
|
|
4125
|
+
quantity: invoice.items.length
|
|
4126
|
+
}))
|
|
4114
4127
|
};
|
|
4115
4128
|
};
|
|
4116
4129
|
var paykitPayment$InboundSchema = (checkout) => {
|
|
@@ -4127,8 +4140,10 @@ var paykitPayment$InboundSchema = (checkout) => {
|
|
|
4127
4140
|
currency: checkout.currency,
|
|
4128
4141
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4129
4142
|
status: statusMap[checkout.status],
|
|
4130
|
-
metadata: checkout.metadata ?? {},
|
|
4131
|
-
item_id: checkout.products.length > 0 ? checkout.products[0].id : null
|
|
4143
|
+
metadata: core.omitInternalMetadata(checkout.metadata) ?? {},
|
|
4144
|
+
item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
|
|
4145
|
+
requires_action: checkout.status === "open" ? true : false,
|
|
4146
|
+
payment_url: checkout.status === "open" ? checkout.url : null
|
|
4132
4147
|
};
|
|
4133
4148
|
};
|
|
4134
4149
|
var mapRefundReason = (debug, reason) => {
|
|
@@ -4186,10 +4201,16 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4186
4201
|
const { metadata, item_id, provider_metadata } = data;
|
|
4187
4202
|
const checkoutMetadata = core.stringifyMetadataValues(metadata ?? {});
|
|
4188
4203
|
const checkoutCreateOptions = {
|
|
4204
|
+
...provider_metadata,
|
|
4189
4205
|
metadata: checkoutMetadata,
|
|
4190
4206
|
products: [item_id],
|
|
4191
|
-
|
|
4207
|
+
successUrl: data.success_url
|
|
4192
4208
|
};
|
|
4209
|
+
if (typeof data.customer === "object" && "email" in data.customer) {
|
|
4210
|
+
checkoutCreateOptions.customerEmail = data.customer.email;
|
|
4211
|
+
} else if (typeof data.customer === "string") {
|
|
4212
|
+
checkoutCreateOptions.customerId = data.customer;
|
|
4213
|
+
}
|
|
4193
4214
|
if (data.billing) {
|
|
4194
4215
|
checkoutCreateOptions.customerBillingAddress = {
|
|
4195
4216
|
line1: data.billing.address.line1,
|
|
@@ -4324,9 +4345,15 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4324
4345
|
if (error) {
|
|
4325
4346
|
throw core.ValidationError.fromZodError(error, this.providerName, "updateSubscription");
|
|
4326
4347
|
}
|
|
4348
|
+
if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
|
|
4349
|
+
throw new core.ValidationError(
|
|
4350
|
+
"Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
|
|
4351
|
+
{ provider: this.providerName, method: "updateSubscription" }
|
|
4352
|
+
);
|
|
4353
|
+
}
|
|
4327
4354
|
const response = await this.polar.subscriptions.update({
|
|
4328
4355
|
id,
|
|
4329
|
-
subscriptionUpdate:
|
|
4356
|
+
subscriptionUpdate: data.provider_metadata
|
|
4330
4357
|
});
|
|
4331
4358
|
return paykitSubscription$InboundSchema(response);
|
|
4332
4359
|
};
|
|
@@ -4384,10 +4411,11 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4384
4411
|
const checkoutResponse = await this.polar.checkouts.update({
|
|
4385
4412
|
id,
|
|
4386
4413
|
checkoutUpdate: {
|
|
4387
|
-
...
|
|
4414
|
+
...provider_metadata,
|
|
4388
4415
|
...rest.metadata && { metadata: paymentMetadata },
|
|
4389
4416
|
...rest.item_id && { products: [rest.item_id] },
|
|
4390
|
-
...
|
|
4417
|
+
...rest.amount && { amount: rest.amount },
|
|
4418
|
+
...rest.currency && { currency: rest.currency }
|
|
4391
4419
|
}
|
|
4392
4420
|
});
|
|
4393
4421
|
return paykitPayment$InboundSchema(checkoutResponse);
|
|
@@ -4448,71 +4476,67 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4448
4476
|
*/
|
|
4449
4477
|
"order.paid": (data2) => {
|
|
4450
4478
|
const { status, metadata } = data2;
|
|
4451
|
-
if (status
|
|
4452
|
-
|
|
4453
|
-
...data2,
|
|
4454
|
-
billingMode: core.billingModeSchema.parse("one_time"),
|
|
4455
|
-
metadata: { ...metadata ?? {} }
|
|
4456
|
-
});
|
|
4457
|
-
const payment = {
|
|
4458
|
-
id: data2.id,
|
|
4459
|
-
amount: data2.totalAmount,
|
|
4460
|
-
currency: data2.currency,
|
|
4461
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4462
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4463
|
-
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4464
|
-
item_id: data2.product.id
|
|
4465
|
-
};
|
|
4466
|
-
return [
|
|
4467
|
-
core.paykitEvent$InboundSchema({
|
|
4468
|
-
type: "payment.updated",
|
|
4469
|
-
created: parseInt(webhookTimestamp),
|
|
4470
|
-
id: webhookId,
|
|
4471
|
-
data: payment
|
|
4472
|
-
}),
|
|
4473
|
-
core.paykitEvent$InboundSchema({
|
|
4474
|
-
type: "invoice.generated",
|
|
4475
|
-
created: parseInt(webhookTimestamp),
|
|
4476
|
-
id: webhookId,
|
|
4477
|
-
data: invoice
|
|
4478
|
-
})
|
|
4479
|
-
];
|
|
4479
|
+
if (status !== "paid") {
|
|
4480
|
+
return null;
|
|
4480
4481
|
}
|
|
4481
|
-
|
|
4482
|
+
const isSubscription = ["subscription_create", "subscription_cycle"].includes(
|
|
4483
|
+
data2.billingReason
|
|
4484
|
+
);
|
|
4485
|
+
const invoice = paykitInvoice$InboundSchema({
|
|
4486
|
+
...data2,
|
|
4487
|
+
billingMode: core.billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
|
|
4488
|
+
metadata: { ...metadata ?? {} }
|
|
4489
|
+
});
|
|
4490
|
+
const payment = {
|
|
4491
|
+
id: data2.id,
|
|
4492
|
+
amount: data2.totalAmount,
|
|
4493
|
+
currency: data2.currency,
|
|
4494
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4495
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4496
|
+
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4497
|
+
item_id: data2.product.id,
|
|
4498
|
+
requires_action: false,
|
|
4499
|
+
payment_url: null
|
|
4500
|
+
};
|
|
4501
|
+
return [
|
|
4502
|
+
core.paykitEvent$InboundSchema({
|
|
4503
|
+
type: "payment.updated",
|
|
4504
|
+
created: parseInt(webhookTimestamp),
|
|
4505
|
+
id: webhookId,
|
|
4506
|
+
data: payment
|
|
4507
|
+
}),
|
|
4508
|
+
core.paykitEvent$InboundSchema({
|
|
4509
|
+
type: "invoice.generated",
|
|
4510
|
+
created: parseInt(webhookTimestamp),
|
|
4511
|
+
id: webhookId,
|
|
4512
|
+
data: invoice
|
|
4513
|
+
})
|
|
4514
|
+
];
|
|
4482
4515
|
},
|
|
4483
4516
|
"order.created": (data2) => {
|
|
4484
4517
|
const { billingReason, metadata, status } = data2;
|
|
4485
|
-
if (
|
|
4486
|
-
|
|
4487
|
-
...data2,
|
|
4488
|
-
billingMode: core.billingModeSchema.parse("recurring"),
|
|
4489
|
-
metadata: { ...metadata ?? {} }
|
|
4490
|
-
});
|
|
4491
|
-
const payment = {
|
|
4492
|
-
id: data2.id,
|
|
4493
|
-
amount: data2.totalAmount,
|
|
4494
|
-
currency: data2.currency,
|
|
4495
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4496
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4497
|
-
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4498
|
-
item_id: data2.product.id
|
|
4499
|
-
};
|
|
4500
|
-
return [
|
|
4501
|
-
core.paykitEvent$InboundSchema({
|
|
4502
|
-
type: "payment.created",
|
|
4503
|
-
created: parseInt(webhookTimestamp),
|
|
4504
|
-
id: webhookId,
|
|
4505
|
-
data: payment
|
|
4506
|
-
}),
|
|
4507
|
-
core.paykitEvent$InboundSchema({
|
|
4508
|
-
type: "invoice.generated",
|
|
4509
|
-
created: parseInt(webhookTimestamp),
|
|
4510
|
-
id: webhookId,
|
|
4511
|
-
data: invoice
|
|
4512
|
-
})
|
|
4513
|
-
];
|
|
4518
|
+
if (status !== "paid") {
|
|
4519
|
+
return null;
|
|
4514
4520
|
}
|
|
4515
|
-
|
|
4521
|
+
const payment = {
|
|
4522
|
+
id: data2.id,
|
|
4523
|
+
amount: data2.totalAmount,
|
|
4524
|
+
currency: data2.currency,
|
|
4525
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4526
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4527
|
+
metadata: core.stringifyMetadataValues(metadata ?? {}),
|
|
4528
|
+
item_id: data2.product.id,
|
|
4529
|
+
requires_action: data2.status === "paid" ? false : true,
|
|
4530
|
+
payment_url: null
|
|
4531
|
+
};
|
|
4532
|
+
return [
|
|
4533
|
+
core.paykitEvent$InboundSchema({
|
|
4534
|
+
type: "payment.created",
|
|
4535
|
+
created: parseInt(webhookTimestamp),
|
|
4536
|
+
id: webhookId,
|
|
4537
|
+
data: payment
|
|
4538
|
+
})
|
|
4539
|
+
];
|
|
4516
4540
|
},
|
|
4517
4541
|
/**
|
|
4518
4542
|
* Customer
|
|
@@ -4602,8 +4626,12 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
|
|
|
4602
4626
|
if (!handler)
|
|
4603
4627
|
throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
|
|
4604
4628
|
const results = handler(data);
|
|
4605
|
-
if (!results)
|
|
4606
|
-
|
|
4629
|
+
if (!results) {
|
|
4630
|
+
console.log(
|
|
4631
|
+
`Skipping event ${type} for provider: ${this.providerName} as no action needed`
|
|
4632
|
+
);
|
|
4633
|
+
return [];
|
|
4634
|
+
}
|
|
4607
4635
|
return results;
|
|
4608
4636
|
};
|
|
4609
4637
|
const { accessToken, isSandbox, debug = true, ...rest } = config;
|
package/dist/polar-provider.mjs
CHANGED
|
@@ -4038,14 +4038,14 @@ var ostring = () => stringType().optional();
|
|
|
4038
4038
|
var onumber = () => numberType().optional();
|
|
4039
4039
|
var oboolean = () => booleanType().optional();
|
|
4040
4040
|
var coerce = {
|
|
4041
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4042
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4043
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4041
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4042
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4043
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4044
4044
|
...arg,
|
|
4045
4045
|
coerce: true
|
|
4046
|
-
}),
|
|
4047
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4048
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4046
|
+
})),
|
|
4047
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4048
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4049
4049
|
};
|
|
4050
4050
|
var NEVER = INVALID;
|
|
4051
4051
|
var paykitCheckout$InboundSchema = (checkout) => {
|
|
@@ -4055,7 +4055,7 @@ var paykitCheckout$InboundSchema = (checkout) => {
|
|
|
4055
4055
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4056
4056
|
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
4057
4057
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
4058
|
-
metadata: checkout.metadata ?? null,
|
|
4058
|
+
metadata: omitInternalMetadata(checkout.metadata) ?? null,
|
|
4059
4059
|
currency: checkout.currency,
|
|
4060
4060
|
amount: checkout.amount
|
|
4061
4061
|
};
|
|
@@ -4067,7 +4067,15 @@ var paykitCustomer$InboundSchema = (customer) => {
|
|
|
4067
4067
|
email: customer.email,
|
|
4068
4068
|
name: customer.name ?? "",
|
|
4069
4069
|
phone,
|
|
4070
|
-
metadata: omitInternalMetadata(customer.metadata ?? {})
|
|
4070
|
+
metadata: omitInternalMetadata(customer.metadata ?? {}),
|
|
4071
|
+
created_at: customer.createdAt,
|
|
4072
|
+
updated_at: customer.modifiedAt ?? null,
|
|
4073
|
+
custom_fields: {
|
|
4074
|
+
emailVerified: customer.emailVerified,
|
|
4075
|
+
taxId: customer.taxId,
|
|
4076
|
+
avatarUrl: customer.avatarUrl,
|
|
4077
|
+
billingAddress: customer.billingAddress
|
|
4078
|
+
}
|
|
4071
4079
|
};
|
|
4072
4080
|
};
|
|
4073
4081
|
var toPaykitSubscriptionStatus = (status) => {
|
|
@@ -4089,7 +4097,9 @@ var paykitSubscription$InboundSchema = (subscription) => {
|
|
|
4089
4097
|
item_id: subscription.productId,
|
|
4090
4098
|
billing_interval: subscription.recurringInterval,
|
|
4091
4099
|
currency: subscription.currency,
|
|
4092
|
-
amount: subscription.amount
|
|
4100
|
+
amount: subscription.amount,
|
|
4101
|
+
requires_action: false,
|
|
4102
|
+
payment_url: null
|
|
4093
4103
|
};
|
|
4094
4104
|
};
|
|
4095
4105
|
var paykitInvoice$InboundSchema = (invoice) => {
|
|
@@ -4108,7 +4118,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
|
|
|
4108
4118
|
status,
|
|
4109
4119
|
subscription_id: invoice.subscription?.id ?? null,
|
|
4110
4120
|
paid_at: new Date(invoice.createdAt).toISOString(),
|
|
4111
|
-
line_items:
|
|
4121
|
+
line_items: invoice.items.map(({ productPriceId }) => ({
|
|
4122
|
+
id: productPriceId ?? "",
|
|
4123
|
+
quantity: invoice.items.length
|
|
4124
|
+
}))
|
|
4112
4125
|
};
|
|
4113
4126
|
};
|
|
4114
4127
|
var paykitPayment$InboundSchema = (checkout) => {
|
|
@@ -4125,8 +4138,10 @@ var paykitPayment$InboundSchema = (checkout) => {
|
|
|
4125
4138
|
currency: checkout.currency,
|
|
4126
4139
|
customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
|
|
4127
4140
|
status: statusMap[checkout.status],
|
|
4128
|
-
metadata: checkout.metadata ?? {},
|
|
4129
|
-
item_id: checkout.products.length > 0 ? checkout.products[0].id : null
|
|
4141
|
+
metadata: omitInternalMetadata(checkout.metadata) ?? {},
|
|
4142
|
+
item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
|
|
4143
|
+
requires_action: checkout.status === "open" ? true : false,
|
|
4144
|
+
payment_url: checkout.status === "open" ? checkout.url : null
|
|
4130
4145
|
};
|
|
4131
4146
|
};
|
|
4132
4147
|
var mapRefundReason = (debug, reason) => {
|
|
@@ -4184,10 +4199,16 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4184
4199
|
const { metadata, item_id, provider_metadata } = data;
|
|
4185
4200
|
const checkoutMetadata = stringifyMetadataValues(metadata ?? {});
|
|
4186
4201
|
const checkoutCreateOptions = {
|
|
4202
|
+
...provider_metadata,
|
|
4187
4203
|
metadata: checkoutMetadata,
|
|
4188
4204
|
products: [item_id],
|
|
4189
|
-
|
|
4205
|
+
successUrl: data.success_url
|
|
4190
4206
|
};
|
|
4207
|
+
if (typeof data.customer === "object" && "email" in data.customer) {
|
|
4208
|
+
checkoutCreateOptions.customerEmail = data.customer.email;
|
|
4209
|
+
} else if (typeof data.customer === "string") {
|
|
4210
|
+
checkoutCreateOptions.customerId = data.customer;
|
|
4211
|
+
}
|
|
4191
4212
|
if (data.billing) {
|
|
4192
4213
|
checkoutCreateOptions.customerBillingAddress = {
|
|
4193
4214
|
line1: data.billing.address.line1,
|
|
@@ -4322,9 +4343,15 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4322
4343
|
if (error) {
|
|
4323
4344
|
throw ValidationError.fromZodError(error, this.providerName, "updateSubscription");
|
|
4324
4345
|
}
|
|
4346
|
+
if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
|
|
4347
|
+
throw new ValidationError(
|
|
4348
|
+
"Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
|
|
4349
|
+
{ provider: this.providerName, method: "updateSubscription" }
|
|
4350
|
+
);
|
|
4351
|
+
}
|
|
4325
4352
|
const response = await this.polar.subscriptions.update({
|
|
4326
4353
|
id,
|
|
4327
|
-
subscriptionUpdate:
|
|
4354
|
+
subscriptionUpdate: data.provider_metadata
|
|
4328
4355
|
});
|
|
4329
4356
|
return paykitSubscription$InboundSchema(response);
|
|
4330
4357
|
};
|
|
@@ -4382,10 +4409,11 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4382
4409
|
const checkoutResponse = await this.polar.checkouts.update({
|
|
4383
4410
|
id,
|
|
4384
4411
|
checkoutUpdate: {
|
|
4385
|
-
...
|
|
4412
|
+
...provider_metadata,
|
|
4386
4413
|
...rest.metadata && { metadata: paymentMetadata },
|
|
4387
4414
|
...rest.item_id && { products: [rest.item_id] },
|
|
4388
|
-
...
|
|
4415
|
+
...rest.amount && { amount: rest.amount },
|
|
4416
|
+
...rest.currency && { currency: rest.currency }
|
|
4389
4417
|
}
|
|
4390
4418
|
});
|
|
4391
4419
|
return paykitPayment$InboundSchema(checkoutResponse);
|
|
@@ -4446,71 +4474,67 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4446
4474
|
*/
|
|
4447
4475
|
"order.paid": (data2) => {
|
|
4448
4476
|
const { status, metadata } = data2;
|
|
4449
|
-
if (status
|
|
4450
|
-
|
|
4451
|
-
...data2,
|
|
4452
|
-
billingMode: billingModeSchema.parse("one_time"),
|
|
4453
|
-
metadata: { ...metadata ?? {} }
|
|
4454
|
-
});
|
|
4455
|
-
const payment = {
|
|
4456
|
-
id: data2.id,
|
|
4457
|
-
amount: data2.totalAmount,
|
|
4458
|
-
currency: data2.currency,
|
|
4459
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4460
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4461
|
-
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4462
|
-
item_id: data2.product.id
|
|
4463
|
-
};
|
|
4464
|
-
return [
|
|
4465
|
-
paykitEvent$InboundSchema({
|
|
4466
|
-
type: "payment.updated",
|
|
4467
|
-
created: parseInt(webhookTimestamp),
|
|
4468
|
-
id: webhookId,
|
|
4469
|
-
data: payment
|
|
4470
|
-
}),
|
|
4471
|
-
paykitEvent$InboundSchema({
|
|
4472
|
-
type: "invoice.generated",
|
|
4473
|
-
created: parseInt(webhookTimestamp),
|
|
4474
|
-
id: webhookId,
|
|
4475
|
-
data: invoice
|
|
4476
|
-
})
|
|
4477
|
-
];
|
|
4477
|
+
if (status !== "paid") {
|
|
4478
|
+
return null;
|
|
4478
4479
|
}
|
|
4479
|
-
|
|
4480
|
+
const isSubscription = ["subscription_create", "subscription_cycle"].includes(
|
|
4481
|
+
data2.billingReason
|
|
4482
|
+
);
|
|
4483
|
+
const invoice = paykitInvoice$InboundSchema({
|
|
4484
|
+
...data2,
|
|
4485
|
+
billingMode: billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
|
|
4486
|
+
metadata: { ...metadata ?? {} }
|
|
4487
|
+
});
|
|
4488
|
+
const payment = {
|
|
4489
|
+
id: data2.id,
|
|
4490
|
+
amount: data2.totalAmount,
|
|
4491
|
+
currency: data2.currency,
|
|
4492
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4493
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4494
|
+
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4495
|
+
item_id: data2.product.id,
|
|
4496
|
+
requires_action: false,
|
|
4497
|
+
payment_url: null
|
|
4498
|
+
};
|
|
4499
|
+
return [
|
|
4500
|
+
paykitEvent$InboundSchema({
|
|
4501
|
+
type: "payment.updated",
|
|
4502
|
+
created: parseInt(webhookTimestamp),
|
|
4503
|
+
id: webhookId,
|
|
4504
|
+
data: payment
|
|
4505
|
+
}),
|
|
4506
|
+
paykitEvent$InboundSchema({
|
|
4507
|
+
type: "invoice.generated",
|
|
4508
|
+
created: parseInt(webhookTimestamp),
|
|
4509
|
+
id: webhookId,
|
|
4510
|
+
data: invoice
|
|
4511
|
+
})
|
|
4512
|
+
];
|
|
4480
4513
|
},
|
|
4481
4514
|
"order.created": (data2) => {
|
|
4482
4515
|
const { billingReason, metadata, status } = data2;
|
|
4483
|
-
if (
|
|
4484
|
-
|
|
4485
|
-
...data2,
|
|
4486
|
-
billingMode: billingModeSchema.parse("recurring"),
|
|
4487
|
-
metadata: { ...metadata ?? {} }
|
|
4488
|
-
});
|
|
4489
|
-
const payment = {
|
|
4490
|
-
id: data2.id,
|
|
4491
|
-
amount: data2.totalAmount,
|
|
4492
|
-
currency: data2.currency,
|
|
4493
|
-
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4494
|
-
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4495
|
-
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4496
|
-
item_id: data2.product.id
|
|
4497
|
-
};
|
|
4498
|
-
return [
|
|
4499
|
-
paykitEvent$InboundSchema({
|
|
4500
|
-
type: "payment.created",
|
|
4501
|
-
created: parseInt(webhookTimestamp),
|
|
4502
|
-
id: webhookId,
|
|
4503
|
-
data: payment
|
|
4504
|
-
}),
|
|
4505
|
-
paykitEvent$InboundSchema({
|
|
4506
|
-
type: "invoice.generated",
|
|
4507
|
-
created: parseInt(webhookTimestamp),
|
|
4508
|
-
id: webhookId,
|
|
4509
|
-
data: invoice
|
|
4510
|
-
})
|
|
4511
|
-
];
|
|
4516
|
+
if (status !== "paid") {
|
|
4517
|
+
return null;
|
|
4512
4518
|
}
|
|
4513
|
-
|
|
4519
|
+
const payment = {
|
|
4520
|
+
id: data2.id,
|
|
4521
|
+
amount: data2.totalAmount,
|
|
4522
|
+
currency: data2.currency,
|
|
4523
|
+
customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
|
|
4524
|
+
status: data2.status === "paid" ? "succeeded" : "pending",
|
|
4525
|
+
metadata: stringifyMetadataValues(metadata ?? {}),
|
|
4526
|
+
item_id: data2.product.id,
|
|
4527
|
+
requires_action: data2.status === "paid" ? false : true,
|
|
4528
|
+
payment_url: null
|
|
4529
|
+
};
|
|
4530
|
+
return [
|
|
4531
|
+
paykitEvent$InboundSchema({
|
|
4532
|
+
type: "payment.created",
|
|
4533
|
+
created: parseInt(webhookTimestamp),
|
|
4534
|
+
id: webhookId,
|
|
4535
|
+
data: payment
|
|
4536
|
+
})
|
|
4537
|
+
];
|
|
4514
4538
|
},
|
|
4515
4539
|
/**
|
|
4516
4540
|
* Customer
|
|
@@ -4600,8 +4624,12 @@ var PolarProvider = class extends AbstractPayKitProvider {
|
|
|
4600
4624
|
if (!handler)
|
|
4601
4625
|
throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
|
|
4602
4626
|
const results = handler(data);
|
|
4603
|
-
if (!results)
|
|
4604
|
-
|
|
4627
|
+
if (!results) {
|
|
4628
|
+
console.log(
|
|
4629
|
+
`Skipping event ${type} for provider: ${this.providerName} as no action needed`
|
|
4630
|
+
);
|
|
4631
|
+
return [];
|
|
4632
|
+
}
|
|
4605
4633
|
return results;
|
|
4606
4634
|
};
|
|
4607
4635
|
const { accessToken, isSandbox, debug = true, ...rest } = config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/polar",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.99",
|
|
4
4
|
"description": "Polar provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
"author": "Emmanuel Odii",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@polar-sh/sdk": "
|
|
20
|
+
"@polar-sh/sdk": "0.37.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@paykit-sdk/core": ">=1.1.
|
|
23
|
+
"@paykit-sdk/core": ">=1.1.102"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"tsup": "^8.0.0",
|
|
27
27
|
"typescript": "^5.0.0",
|
|
28
28
|
"zod": "^3.24.2",
|
|
29
|
-
"@paykit-sdk/core": "1.1.
|
|
29
|
+
"@paykit-sdk/core": "1.1.102"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|