@paykit-sdk/polar 1.1.96 → 1.1.98

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 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 = Object.fromEntries(request.headers.entries());
90
- await webhook.handle({ body, headers, fullUrl: request.url });
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; // Raw buffer from express.raw()
135
- const headers = req.headers;
136
- await webhook.handle({ body, headers });
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
- ## Subscription Management
178
-
179
- ```typescript
180
- // Update subscription
181
- await paykit.subscriptions.update('sub_123', {
182
- metadata: { plan: 'enterprise' },
183
- });
184
-
185
- // Cancel subscription
186
- await paykit.subscriptions.cancel('sub_123');
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
@@ -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
  };
@@ -4110,7 +4110,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
4110
4110
  status,
4111
4111
  subscription_id: invoice.subscription?.id ?? null,
4112
4112
  paid_at: new Date(invoice.createdAt).toISOString(),
4113
- line_items: []
4113
+ line_items: invoice.items.map(({ productPriceId }) => ({
4114
+ id: productPriceId ?? "",
4115
+ quantity: invoice.items.length
4116
+ }))
4114
4117
  };
4115
4118
  };
4116
4119
  var paykitPayment$InboundSchema = (checkout) => {
@@ -4127,8 +4130,10 @@ var paykitPayment$InboundSchema = (checkout) => {
4127
4130
  currency: checkout.currency,
4128
4131
  customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
4129
4132
  status: statusMap[checkout.status],
4130
- metadata: checkout.metadata ?? {},
4131
- item_id: checkout.products.length > 0 ? checkout.products[0].id : null
4133
+ metadata: core.omitInternalMetadata(checkout.metadata) ?? {},
4134
+ item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
4135
+ requires_action: checkout.status === "open" ? true : false,
4136
+ payment_url: checkout.status === "open" ? checkout.url : null
4132
4137
  };
4133
4138
  };
4134
4139
  var mapRefundReason = (debug, reason) => {
@@ -4186,10 +4191,16 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4186
4191
  const { metadata, item_id, provider_metadata } = data;
4187
4192
  const checkoutMetadata = core.stringifyMetadataValues(metadata ?? {});
4188
4193
  const checkoutCreateOptions = {
4194
+ ...provider_metadata,
4189
4195
  metadata: checkoutMetadata,
4190
4196
  products: [item_id],
4191
- ...provider_metadata
4197
+ successUrl: data.success_url
4192
4198
  };
4199
+ if (typeof data.customer === "object" && "email" in data.customer) {
4200
+ checkoutCreateOptions.customerEmail = data.customer.email;
4201
+ } else if (typeof data.customer === "string") {
4202
+ checkoutCreateOptions.customerId = data.customer;
4203
+ }
4193
4204
  if (data.billing) {
4194
4205
  checkoutCreateOptions.customerBillingAddress = {
4195
4206
  line1: data.billing.address.line1,
@@ -4324,9 +4335,15 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4324
4335
  if (error) {
4325
4336
  throw core.ValidationError.fromZodError(error, this.providerName, "updateSubscription");
4326
4337
  }
4338
+ if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
4339
+ throw new core.ValidationError(
4340
+ "Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
4341
+ { provider: this.providerName, method: "updateSubscription" }
4342
+ );
4343
+ }
4327
4344
  const response = await this.polar.subscriptions.update({
4328
4345
  id,
4329
- subscriptionUpdate: { ...data.metadata ?? {} }
4346
+ subscriptionUpdate: data.provider_metadata
4330
4347
  });
4331
4348
  return paykitSubscription$InboundSchema(response);
4332
4349
  };
@@ -4384,10 +4401,11 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4384
4401
  const checkoutResponse = await this.polar.checkouts.update({
4385
4402
  id,
4386
4403
  checkoutUpdate: {
4387
- ...rest,
4404
+ ...provider_metadata,
4388
4405
  ...rest.metadata && { metadata: paymentMetadata },
4389
4406
  ...rest.item_id && { products: [rest.item_id] },
4390
- ...provider_metadata
4407
+ ...rest.amount && { amount: rest.amount },
4408
+ ...rest.currency && { currency: rest.currency }
4391
4409
  }
4392
4410
  });
4393
4411
  return paykitPayment$InboundSchema(checkoutResponse);
@@ -4448,71 +4466,67 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4448
4466
  */
4449
4467
  "order.paid": (data2) => {
4450
4468
  const { status, metadata } = data2;
4451
- if (status == "paid") {
4452
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4469
+ if (status !== "paid") {
4470
+ return null;
4480
4471
  }
4481
- return null;
4472
+ const isSubscription = ["subscription_create", "subscription_cycle"].includes(
4473
+ data2.billingReason
4474
+ );
4475
+ const invoice = paykitInvoice$InboundSchema({
4476
+ ...data2,
4477
+ billingMode: core.billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
4478
+ metadata: { ...metadata ?? {} }
4479
+ });
4480
+ const payment = {
4481
+ id: data2.id,
4482
+ amount: data2.totalAmount,
4483
+ currency: data2.currency,
4484
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4485
+ status: data2.status === "paid" ? "succeeded" : "pending",
4486
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
4487
+ item_id: data2.product.id,
4488
+ requires_action: false,
4489
+ payment_url: null
4490
+ };
4491
+ return [
4492
+ core.paykitEvent$InboundSchema({
4493
+ type: "payment.updated",
4494
+ created: parseInt(webhookTimestamp),
4495
+ id: webhookId,
4496
+ data: payment
4497
+ }),
4498
+ core.paykitEvent$InboundSchema({
4499
+ type: "invoice.generated",
4500
+ created: parseInt(webhookTimestamp),
4501
+ id: webhookId,
4502
+ data: invoice
4503
+ })
4504
+ ];
4482
4505
  },
4483
4506
  "order.created": (data2) => {
4484
4507
  const { billingReason, metadata, status } = data2;
4485
- if (["subscription_create", "subscription_cycle"].includes(billingReason) && status == "paid") {
4486
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4508
+ if (status !== "paid") {
4509
+ return null;
4514
4510
  }
4515
- return null;
4511
+ const payment = {
4512
+ id: data2.id,
4513
+ amount: data2.totalAmount,
4514
+ currency: data2.currency,
4515
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4516
+ status: data2.status === "paid" ? "succeeded" : "pending",
4517
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
4518
+ item_id: data2.product.id,
4519
+ requires_action: data2.status === "paid" ? false : true,
4520
+ payment_url: null
4521
+ };
4522
+ return [
4523
+ core.paykitEvent$InboundSchema({
4524
+ type: "payment.created",
4525
+ created: parseInt(webhookTimestamp),
4526
+ id: webhookId,
4527
+ data: payment
4528
+ })
4529
+ ];
4516
4530
  },
4517
4531
  /**
4518
4532
  * Customer
@@ -4602,8 +4616,12 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4602
4616
  if (!handler)
4603
4617
  throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4604
4618
  const results = handler(data);
4605
- if (!results)
4606
- throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4619
+ if (!results) {
4620
+ console.log(
4621
+ `Skipping event ${type} for provider: ${this.providerName} as no action needed`
4622
+ );
4623
+ return [];
4624
+ }
4607
4625
  return results;
4608
4626
  };
4609
4627
  const { accessToken, isSandbox, debug = true, ...rest } = config;
package/dist/index.mjs CHANGED
@@ -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
  };
@@ -4108,7 +4108,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
4108
4108
  status,
4109
4109
  subscription_id: invoice.subscription?.id ?? null,
4110
4110
  paid_at: new Date(invoice.createdAt).toISOString(),
4111
- line_items: []
4111
+ line_items: invoice.items.map(({ productPriceId }) => ({
4112
+ id: productPriceId ?? "",
4113
+ quantity: invoice.items.length
4114
+ }))
4112
4115
  };
4113
4116
  };
4114
4117
  var paykitPayment$InboundSchema = (checkout) => {
@@ -4125,8 +4128,10 @@ var paykitPayment$InboundSchema = (checkout) => {
4125
4128
  currency: checkout.currency,
4126
4129
  customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
4127
4130
  status: statusMap[checkout.status],
4128
- metadata: checkout.metadata ?? {},
4129
- item_id: checkout.products.length > 0 ? checkout.products[0].id : null
4131
+ metadata: omitInternalMetadata(checkout.metadata) ?? {},
4132
+ item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
4133
+ requires_action: checkout.status === "open" ? true : false,
4134
+ payment_url: checkout.status === "open" ? checkout.url : null
4130
4135
  };
4131
4136
  };
4132
4137
  var mapRefundReason = (debug, reason) => {
@@ -4184,10 +4189,16 @@ var PolarProvider = class extends AbstractPayKitProvider {
4184
4189
  const { metadata, item_id, provider_metadata } = data;
4185
4190
  const checkoutMetadata = stringifyMetadataValues(metadata ?? {});
4186
4191
  const checkoutCreateOptions = {
4192
+ ...provider_metadata,
4187
4193
  metadata: checkoutMetadata,
4188
4194
  products: [item_id],
4189
- ...provider_metadata
4195
+ successUrl: data.success_url
4190
4196
  };
4197
+ if (typeof data.customer === "object" && "email" in data.customer) {
4198
+ checkoutCreateOptions.customerEmail = data.customer.email;
4199
+ } else if (typeof data.customer === "string") {
4200
+ checkoutCreateOptions.customerId = data.customer;
4201
+ }
4191
4202
  if (data.billing) {
4192
4203
  checkoutCreateOptions.customerBillingAddress = {
4193
4204
  line1: data.billing.address.line1,
@@ -4322,9 +4333,15 @@ var PolarProvider = class extends AbstractPayKitProvider {
4322
4333
  if (error) {
4323
4334
  throw ValidationError.fromZodError(error, this.providerName, "updateSubscription");
4324
4335
  }
4336
+ if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
4337
+ throw new ValidationError(
4338
+ "Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
4339
+ { provider: this.providerName, method: "updateSubscription" }
4340
+ );
4341
+ }
4325
4342
  const response = await this.polar.subscriptions.update({
4326
4343
  id,
4327
- subscriptionUpdate: { ...data.metadata ?? {} }
4344
+ subscriptionUpdate: data.provider_metadata
4328
4345
  });
4329
4346
  return paykitSubscription$InboundSchema(response);
4330
4347
  };
@@ -4382,10 +4399,11 @@ var PolarProvider = class extends AbstractPayKitProvider {
4382
4399
  const checkoutResponse = await this.polar.checkouts.update({
4383
4400
  id,
4384
4401
  checkoutUpdate: {
4385
- ...rest,
4402
+ ...provider_metadata,
4386
4403
  ...rest.metadata && { metadata: paymentMetadata },
4387
4404
  ...rest.item_id && { products: [rest.item_id] },
4388
- ...provider_metadata
4405
+ ...rest.amount && { amount: rest.amount },
4406
+ ...rest.currency && { currency: rest.currency }
4389
4407
  }
4390
4408
  });
4391
4409
  return paykitPayment$InboundSchema(checkoutResponse);
@@ -4446,71 +4464,67 @@ var PolarProvider = class extends AbstractPayKitProvider {
4446
4464
  */
4447
4465
  "order.paid": (data2) => {
4448
4466
  const { status, metadata } = data2;
4449
- if (status == "paid") {
4450
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4467
+ if (status !== "paid") {
4468
+ return null;
4478
4469
  }
4479
- return null;
4470
+ const isSubscription = ["subscription_create", "subscription_cycle"].includes(
4471
+ data2.billingReason
4472
+ );
4473
+ const invoice = paykitInvoice$InboundSchema({
4474
+ ...data2,
4475
+ billingMode: billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
4476
+ metadata: { ...metadata ?? {} }
4477
+ });
4478
+ const payment = {
4479
+ id: data2.id,
4480
+ amount: data2.totalAmount,
4481
+ currency: data2.currency,
4482
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4483
+ status: data2.status === "paid" ? "succeeded" : "pending",
4484
+ metadata: stringifyMetadataValues(metadata ?? {}),
4485
+ item_id: data2.product.id,
4486
+ requires_action: false,
4487
+ payment_url: null
4488
+ };
4489
+ return [
4490
+ paykitEvent$InboundSchema({
4491
+ type: "payment.updated",
4492
+ created: parseInt(webhookTimestamp),
4493
+ id: webhookId,
4494
+ data: payment
4495
+ }),
4496
+ paykitEvent$InboundSchema({
4497
+ type: "invoice.generated",
4498
+ created: parseInt(webhookTimestamp),
4499
+ id: webhookId,
4500
+ data: invoice
4501
+ })
4502
+ ];
4480
4503
  },
4481
4504
  "order.created": (data2) => {
4482
4505
  const { billingReason, metadata, status } = data2;
4483
- if (["subscription_create", "subscription_cycle"].includes(billingReason) && status == "paid") {
4484
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4506
+ if (status !== "paid") {
4507
+ return null;
4512
4508
  }
4513
- return null;
4509
+ const payment = {
4510
+ id: data2.id,
4511
+ amount: data2.totalAmount,
4512
+ currency: data2.currency,
4513
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4514
+ status: data2.status === "paid" ? "succeeded" : "pending",
4515
+ metadata: stringifyMetadataValues(metadata ?? {}),
4516
+ item_id: data2.product.id,
4517
+ requires_action: data2.status === "paid" ? false : true,
4518
+ payment_url: null
4519
+ };
4520
+ return [
4521
+ paykitEvent$InboundSchema({
4522
+ type: "payment.created",
4523
+ created: parseInt(webhookTimestamp),
4524
+ id: webhookId,
4525
+ data: payment
4526
+ })
4527
+ ];
4514
4528
  },
4515
4529
  /**
4516
4530
  * Customer
@@ -4600,8 +4614,12 @@ var PolarProvider = class extends AbstractPayKitProvider {
4600
4614
  if (!handler)
4601
4615
  throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4602
4616
  const results = handler(data);
4603
- if (!results)
4604
- throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4617
+ if (!results) {
4618
+ console.log(
4619
+ `Skipping event ${type} for provider: ${this.providerName} as no action needed`
4620
+ );
4621
+ return [];
4622
+ }
4605
4623
  return results;
4606
4624
  };
4607
4625
  const { accessToken, isSandbox, debug = true, ...rest } = config;
@@ -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
  };
@@ -4110,7 +4110,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
4110
4110
  status,
4111
4111
  subscription_id: invoice.subscription?.id ?? null,
4112
4112
  paid_at: new Date(invoice.createdAt).toISOString(),
4113
- line_items: []
4113
+ line_items: invoice.items.map(({ productPriceId }) => ({
4114
+ id: productPriceId ?? "",
4115
+ quantity: invoice.items.length
4116
+ }))
4114
4117
  };
4115
4118
  };
4116
4119
  var paykitPayment$InboundSchema = (checkout) => {
@@ -4127,8 +4130,10 @@ var paykitPayment$InboundSchema = (checkout) => {
4127
4130
  currency: checkout.currency,
4128
4131
  customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
4129
4132
  status: statusMap[checkout.status],
4130
- metadata: checkout.metadata ?? {},
4131
- item_id: checkout.products.length > 0 ? checkout.products[0].id : null
4133
+ metadata: core.omitInternalMetadata(checkout.metadata) ?? {},
4134
+ item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
4135
+ requires_action: checkout.status === "open" ? true : false,
4136
+ payment_url: checkout.status === "open" ? checkout.url : null
4132
4137
  };
4133
4138
  };
4134
4139
  var mapRefundReason = (debug, reason) => {
@@ -4186,10 +4191,16 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4186
4191
  const { metadata, item_id, provider_metadata } = data;
4187
4192
  const checkoutMetadata = core.stringifyMetadataValues(metadata ?? {});
4188
4193
  const checkoutCreateOptions = {
4194
+ ...provider_metadata,
4189
4195
  metadata: checkoutMetadata,
4190
4196
  products: [item_id],
4191
- ...provider_metadata
4197
+ successUrl: data.success_url
4192
4198
  };
4199
+ if (typeof data.customer === "object" && "email" in data.customer) {
4200
+ checkoutCreateOptions.customerEmail = data.customer.email;
4201
+ } else if (typeof data.customer === "string") {
4202
+ checkoutCreateOptions.customerId = data.customer;
4203
+ }
4193
4204
  if (data.billing) {
4194
4205
  checkoutCreateOptions.customerBillingAddress = {
4195
4206
  line1: data.billing.address.line1,
@@ -4324,9 +4335,15 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4324
4335
  if (error) {
4325
4336
  throw core.ValidationError.fromZodError(error, this.providerName, "updateSubscription");
4326
4337
  }
4338
+ if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
4339
+ throw new core.ValidationError(
4340
+ "Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
4341
+ { provider: this.providerName, method: "updateSubscription" }
4342
+ );
4343
+ }
4327
4344
  const response = await this.polar.subscriptions.update({
4328
4345
  id,
4329
- subscriptionUpdate: { ...data.metadata ?? {} }
4346
+ subscriptionUpdate: data.provider_metadata
4330
4347
  });
4331
4348
  return paykitSubscription$InboundSchema(response);
4332
4349
  };
@@ -4384,10 +4401,11 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4384
4401
  const checkoutResponse = await this.polar.checkouts.update({
4385
4402
  id,
4386
4403
  checkoutUpdate: {
4387
- ...rest,
4404
+ ...provider_metadata,
4388
4405
  ...rest.metadata && { metadata: paymentMetadata },
4389
4406
  ...rest.item_id && { products: [rest.item_id] },
4390
- ...provider_metadata
4407
+ ...rest.amount && { amount: rest.amount },
4408
+ ...rest.currency && { currency: rest.currency }
4391
4409
  }
4392
4410
  });
4393
4411
  return paykitPayment$InboundSchema(checkoutResponse);
@@ -4448,71 +4466,67 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4448
4466
  */
4449
4467
  "order.paid": (data2) => {
4450
4468
  const { status, metadata } = data2;
4451
- if (status == "paid") {
4452
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4469
+ if (status !== "paid") {
4470
+ return null;
4480
4471
  }
4481
- return null;
4472
+ const isSubscription = ["subscription_create", "subscription_cycle"].includes(
4473
+ data2.billingReason
4474
+ );
4475
+ const invoice = paykitInvoice$InboundSchema({
4476
+ ...data2,
4477
+ billingMode: core.billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
4478
+ metadata: { ...metadata ?? {} }
4479
+ });
4480
+ const payment = {
4481
+ id: data2.id,
4482
+ amount: data2.totalAmount,
4483
+ currency: data2.currency,
4484
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4485
+ status: data2.status === "paid" ? "succeeded" : "pending",
4486
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
4487
+ item_id: data2.product.id,
4488
+ requires_action: false,
4489
+ payment_url: null
4490
+ };
4491
+ return [
4492
+ core.paykitEvent$InboundSchema({
4493
+ type: "payment.updated",
4494
+ created: parseInt(webhookTimestamp),
4495
+ id: webhookId,
4496
+ data: payment
4497
+ }),
4498
+ core.paykitEvent$InboundSchema({
4499
+ type: "invoice.generated",
4500
+ created: parseInt(webhookTimestamp),
4501
+ id: webhookId,
4502
+ data: invoice
4503
+ })
4504
+ ];
4482
4505
  },
4483
4506
  "order.created": (data2) => {
4484
4507
  const { billingReason, metadata, status } = data2;
4485
- if (["subscription_create", "subscription_cycle"].includes(billingReason) && status == "paid") {
4486
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4508
+ if (status !== "paid") {
4509
+ return null;
4514
4510
  }
4515
- return null;
4511
+ const payment = {
4512
+ id: data2.id,
4513
+ amount: data2.totalAmount,
4514
+ currency: data2.currency,
4515
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4516
+ status: data2.status === "paid" ? "succeeded" : "pending",
4517
+ metadata: core.stringifyMetadataValues(metadata ?? {}),
4518
+ item_id: data2.product.id,
4519
+ requires_action: data2.status === "paid" ? false : true,
4520
+ payment_url: null
4521
+ };
4522
+ return [
4523
+ core.paykitEvent$InboundSchema({
4524
+ type: "payment.created",
4525
+ created: parseInt(webhookTimestamp),
4526
+ id: webhookId,
4527
+ data: payment
4528
+ })
4529
+ ];
4516
4530
  },
4517
4531
  /**
4518
4532
  * Customer
@@ -4602,8 +4616,12 @@ var PolarProvider = class extends core.AbstractPayKitProvider {
4602
4616
  if (!handler)
4603
4617
  throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4604
4618
  const results = handler(data);
4605
- if (!results)
4606
- throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4619
+ if (!results) {
4620
+ console.log(
4621
+ `Skipping event ${type} for provider: ${this.providerName} as no action needed`
4622
+ );
4623
+ return [];
4624
+ }
4607
4625
  return results;
4608
4626
  };
4609
4627
  const { accessToken, isSandbox, debug = true, ...rest } = config;
@@ -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
  };
@@ -4108,7 +4108,10 @@ var paykitInvoice$InboundSchema = (invoice) => {
4108
4108
  status,
4109
4109
  subscription_id: invoice.subscription?.id ?? null,
4110
4110
  paid_at: new Date(invoice.createdAt).toISOString(),
4111
- line_items: []
4111
+ line_items: invoice.items.map(({ productPriceId }) => ({
4112
+ id: productPriceId ?? "",
4113
+ quantity: invoice.items.length
4114
+ }))
4112
4115
  };
4113
4116
  };
4114
4117
  var paykitPayment$InboundSchema = (checkout) => {
@@ -4125,8 +4128,10 @@ var paykitPayment$InboundSchema = (checkout) => {
4125
4128
  currency: checkout.currency,
4126
4129
  customer: checkout.customerId ? checkout.customerId : { email: checkout.customerEmail ?? "" },
4127
4130
  status: statusMap[checkout.status],
4128
- metadata: checkout.metadata ?? {},
4129
- item_id: checkout.products.length > 0 ? checkout.products[0].id : null
4131
+ metadata: omitInternalMetadata(checkout.metadata) ?? {},
4132
+ item_id: checkout.products.length > 0 ? checkout.products[0].id : null,
4133
+ requires_action: checkout.status === "open" ? true : false,
4134
+ payment_url: checkout.status === "open" ? checkout.url : null
4130
4135
  };
4131
4136
  };
4132
4137
  var mapRefundReason = (debug, reason) => {
@@ -4184,10 +4189,16 @@ var PolarProvider = class extends AbstractPayKitProvider {
4184
4189
  const { metadata, item_id, provider_metadata } = data;
4185
4190
  const checkoutMetadata = stringifyMetadataValues(metadata ?? {});
4186
4191
  const checkoutCreateOptions = {
4192
+ ...provider_metadata,
4187
4193
  metadata: checkoutMetadata,
4188
4194
  products: [item_id],
4189
- ...provider_metadata
4195
+ successUrl: data.success_url
4190
4196
  };
4197
+ if (typeof data.customer === "object" && "email" in data.customer) {
4198
+ checkoutCreateOptions.customerEmail = data.customer.email;
4199
+ } else if (typeof data.customer === "string") {
4200
+ checkoutCreateOptions.customerId = data.customer;
4201
+ }
4191
4202
  if (data.billing) {
4192
4203
  checkoutCreateOptions.customerBillingAddress = {
4193
4204
  line1: data.billing.address.line1,
@@ -4322,9 +4333,15 @@ var PolarProvider = class extends AbstractPayKitProvider {
4322
4333
  if (error) {
4323
4334
  throw ValidationError.fromZodError(error, this.providerName, "updateSubscription");
4324
4335
  }
4336
+ if (!data.provider_metadata || Object.keys(data.provider_metadata).length === 0) {
4337
+ throw new ValidationError(
4338
+ "Polar requires specific update type via provider_metadata. Use one of: { productId: string } | { discountId: string } | { trialEnd: Date }",
4339
+ { provider: this.providerName, method: "updateSubscription" }
4340
+ );
4341
+ }
4325
4342
  const response = await this.polar.subscriptions.update({
4326
4343
  id,
4327
- subscriptionUpdate: { ...data.metadata ?? {} }
4344
+ subscriptionUpdate: data.provider_metadata
4328
4345
  });
4329
4346
  return paykitSubscription$InboundSchema(response);
4330
4347
  };
@@ -4382,10 +4399,11 @@ var PolarProvider = class extends AbstractPayKitProvider {
4382
4399
  const checkoutResponse = await this.polar.checkouts.update({
4383
4400
  id,
4384
4401
  checkoutUpdate: {
4385
- ...rest,
4402
+ ...provider_metadata,
4386
4403
  ...rest.metadata && { metadata: paymentMetadata },
4387
4404
  ...rest.item_id && { products: [rest.item_id] },
4388
- ...provider_metadata
4405
+ ...rest.amount && { amount: rest.amount },
4406
+ ...rest.currency && { currency: rest.currency }
4389
4407
  }
4390
4408
  });
4391
4409
  return paykitPayment$InboundSchema(checkoutResponse);
@@ -4446,71 +4464,67 @@ var PolarProvider = class extends AbstractPayKitProvider {
4446
4464
  */
4447
4465
  "order.paid": (data2) => {
4448
4466
  const { status, metadata } = data2;
4449
- if (status == "paid") {
4450
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4467
+ if (status !== "paid") {
4468
+ return null;
4478
4469
  }
4479
- return null;
4470
+ const isSubscription = ["subscription_create", "subscription_cycle"].includes(
4471
+ data2.billingReason
4472
+ );
4473
+ const invoice = paykitInvoice$InboundSchema({
4474
+ ...data2,
4475
+ billingMode: billingModeSchema.parse(isSubscription ? "recurring" : "one_time"),
4476
+ metadata: { ...metadata ?? {} }
4477
+ });
4478
+ const payment = {
4479
+ id: data2.id,
4480
+ amount: data2.totalAmount,
4481
+ currency: data2.currency,
4482
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4483
+ status: data2.status === "paid" ? "succeeded" : "pending",
4484
+ metadata: stringifyMetadataValues(metadata ?? {}),
4485
+ item_id: data2.product.id,
4486
+ requires_action: false,
4487
+ payment_url: null
4488
+ };
4489
+ return [
4490
+ paykitEvent$InboundSchema({
4491
+ type: "payment.updated",
4492
+ created: parseInt(webhookTimestamp),
4493
+ id: webhookId,
4494
+ data: payment
4495
+ }),
4496
+ paykitEvent$InboundSchema({
4497
+ type: "invoice.generated",
4498
+ created: parseInt(webhookTimestamp),
4499
+ id: webhookId,
4500
+ data: invoice
4501
+ })
4502
+ ];
4480
4503
  },
4481
4504
  "order.created": (data2) => {
4482
4505
  const { billingReason, metadata, status } = data2;
4483
- if (["subscription_create", "subscription_cycle"].includes(billingReason) && status == "paid") {
4484
- const invoice = paykitInvoice$InboundSchema({
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
- ];
4506
+ if (status !== "paid") {
4507
+ return null;
4512
4508
  }
4513
- return null;
4509
+ const payment = {
4510
+ id: data2.id,
4511
+ amount: data2.totalAmount,
4512
+ currency: data2.currency,
4513
+ customer: data2.customerId ? data2.customerId : { email: data2.customer.email ?? "" },
4514
+ status: data2.status === "paid" ? "succeeded" : "pending",
4515
+ metadata: stringifyMetadataValues(metadata ?? {}),
4516
+ item_id: data2.product.id,
4517
+ requires_action: data2.status === "paid" ? false : true,
4518
+ payment_url: null
4519
+ };
4520
+ return [
4521
+ paykitEvent$InboundSchema({
4522
+ type: "payment.created",
4523
+ created: parseInt(webhookTimestamp),
4524
+ id: webhookId,
4525
+ data: payment
4526
+ })
4527
+ ];
4514
4528
  },
4515
4529
  /**
4516
4530
  * Customer
@@ -4600,8 +4614,12 @@ var PolarProvider = class extends AbstractPayKitProvider {
4600
4614
  if (!handler)
4601
4615
  throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4602
4616
  const results = handler(data);
4603
- if (!results)
4604
- throw new Error(`Unhandled event type: ${type} for provider: ${this.providerName}`);
4617
+ if (!results) {
4618
+ console.log(
4619
+ `Skipping event ${type} for provider: ${this.providerName} as no action needed`
4620
+ );
4621
+ return [];
4622
+ }
4605
4623
  return results;
4606
4624
  };
4607
4625
  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.96",
3
+ "version": "1.1.98",
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": "^0.33.0"
20
+ "@polar-sh/sdk": "0.37.0"
21
21
  },
22
22
  "peerDependencies": {
23
- "@paykit-sdk/core": ">=1.1.96"
23
+ "@paykit-sdk/core": ">=1.1.98"
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.96"
29
+ "@paykit-sdk/core": "1.1.98"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"