@paykit-sdk/comgate 1.0.7 → 1.0.9

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.
@@ -4047,12 +4047,12 @@ var coerce = {
4047
4047
  date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
4048
4048
  };
4049
4049
  var NEVER = INVALID;
4050
- var paykitPayment$InboundSchema = (webhookResponse, status) => {
4050
+ var Payment$inboundSchema = (webhookResponse, status) => {
4051
4051
  return {
4052
4052
  id: webhookResponse.transId,
4053
4053
  amount: webhookResponse.price,
4054
4054
  currency: webhookResponse.curr,
4055
- customer: webhookResponse.payerId ?? { email: webhookResponse.email },
4055
+ customer: webhookResponse.payerId ? { id: webhookResponse.payerId } : { email: webhookResponse.email },
4056
4056
  status,
4057
4057
  metadata: core.omitInternalMetadata(
4058
4058
  JSON.parse(webhookResponse.refId)
@@ -4062,7 +4062,7 @@ var paykitPayment$InboundSchema = (webhookResponse, status) => {
4062
4062
  payment_url: ""
4063
4063
  };
4064
4064
  };
4065
- var paykitInvoice$InboundSchema = (webhookResponse) => {
4065
+ var Invoice$inboundSchema = (webhookResponse) => {
4066
4066
  const status = (() => {
4067
4067
  if (webhookResponse.status == "PAID") return "paid";
4068
4068
  return "open";
@@ -4073,11 +4073,10 @@ var paykitInvoice$InboundSchema = (webhookResponse) => {
4073
4073
  paid_at: (/* @__PURE__ */ new Date()).toISOString(),
4074
4074
  amount_paid: webhookResponse.price,
4075
4075
  currency: webhookResponse.curr,
4076
- customer: webhookResponse.payerId ?? { email: webhookResponse.email },
4076
+ customer: webhookResponse.payerId ? { id: webhookResponse.payerId } : { email: webhookResponse.email },
4077
4077
  custom_fields: null,
4078
4078
  subscription_id: null,
4079
4079
  billing_mode: "one_time",
4080
- // comgate does not support recurring payments
4081
4080
  line_items: webhookResponse.name ? [{ id: webhookResponse.name, quantity: 1 }] : [],
4082
4081
  metadata: core.omitInternalMetadata(
4083
4082
  JSON.parse(webhookResponse.refId)
@@ -4109,45 +4108,66 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4109
4108
  },
4110
4109
  retryOptions: { max: 3, baseDelay: 1e3, debug }
4111
4110
  });
4111
+ this.isSandbox = opts.isSandbox;
4112
4112
  }
4113
4113
  providerName = providerName;
4114
4114
  baseUrl;
4115
+ isSandbox;
4115
4116
  _client;
4117
+ get _native() {
4118
+ return null;
4119
+ }
4116
4120
  _throwOnError = (req, message) => {
4117
4121
  if (!req.ok)
4118
4122
  throw new core.OperationFailedError(message, this.providerName, {
4119
4123
  cause: new Error(req.error)
4120
4124
  });
4121
4125
  if (req.value.code == 1100) {
4122
- throw new core.OperationFailedError("Unknown error", this.providerName, {
4123
- cause: new Error("Unknown error")
4124
- });
4126
+ throw new core.OperationFailedError(
4127
+ "Unknown error",
4128
+ this.providerName,
4129
+ {
4130
+ cause: new Error("Unknown error")
4131
+ }
4132
+ );
4125
4133
  }
4126
4134
  if (req.value.code == 1200) {
4127
- throw new core.OperationFailedError("Database error", this.providerName, {
4128
- cause: new Error("Database error")
4129
- });
4135
+ throw new core.OperationFailedError(
4136
+ "Database error",
4137
+ this.providerName,
4138
+ {
4139
+ cause: new Error("Database error")
4140
+ }
4141
+ );
4130
4142
  }
4131
4143
  if (req.value.code == 1400) {
4132
- throw new core.OperationFailedError("Wrong query error", this.providerName, {
4133
- cause: new Error("Wrong query error")
4134
- });
4144
+ throw new core.OperationFailedError(
4145
+ "Wrong query error",
4146
+ this.providerName,
4147
+ {
4148
+ cause: new Error("Wrong query error")
4149
+ }
4150
+ );
4135
4151
  }
4136
4152
  if (req.value.code == 1500) {
4137
- throw new core.OperationFailedError("Unexpected error", this.providerName, {
4138
- cause: new Error("Unexpected error")
4139
- });
4153
+ throw new core.OperationFailedError(
4154
+ "Unexpected error",
4155
+ this.providerName,
4156
+ {
4157
+ cause: new Error("Unexpected error")
4158
+ }
4159
+ );
4140
4160
  }
4141
4161
  return req.value;
4142
4162
  };
4143
4163
  createCheckout = async (params) => {
4144
4164
  const { error, data } = core.createPaymentSchema.safeParse(params);
4145
4165
  if (error) throw new Error(error.message.split("\n").join(" "));
4146
- if (core.isIdCustomer(data.customer)) {
4166
+ if (!core.isEmailCustomer(data.customer)) {
4147
4167
  throw new core.InvalidTypeError(
4148
4168
  "customer",
4149
- "object (customer) with email",
4150
- "string (customer ID)",
4169
+ "object with email",
4170
+ data.customer === null ? "null" : "object with id",
4151
4171
  {
4152
4172
  provider: this.providerName,
4153
4173
  method: "createCheckout"
@@ -4165,7 +4185,10 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4165
4185
  label: data.provider_metadata?.label ? String(data.provider_metadata.label) : "Order from Eshop"
4166
4186
  });
4167
4187
  if (this.opts.debug) {
4168
- console.log("Creating payment with data:", requestBody.toString());
4188
+ console.log(
4189
+ "Creating payment with data:",
4190
+ requestBody.toString()
4191
+ );
4169
4192
  }
4170
4193
  const response = await this._client.post(
4171
4194
  `/v2.0/paymentRedirect/merchant/${this.opts.merchant}`,
@@ -4174,9 +4197,13 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4174
4197
  }
4175
4198
  );
4176
4199
  if (!response.ok) {
4177
- throw new core.OperationFailedError(`Failed to create payment`, this.providerName, {
4178
- cause: new Error("Unknown error")
4179
- });
4200
+ throw new core.OperationFailedError(
4201
+ `Failed to create payment`,
4202
+ this.providerName,
4203
+ {
4204
+ cause: new Error("Unknown error")
4205
+ }
4206
+ );
4180
4207
  }
4181
4208
  this._throwOnError(response, "Failed to create payment");
4182
4209
  return null;
@@ -4194,10 +4221,14 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4194
4221
  });
4195
4222
  };
4196
4223
  retrieveCheckout = async (id) => {
4197
- throw new core.ProviderNotSupportedError("retrieveCheckout", "Comgate", {
4198
- reason: "Comgate does not support retrieving checkouts, use the payment API instead",
4199
- alternative: "Use the payment API instead"
4200
- });
4224
+ throw new core.ProviderNotSupportedError(
4225
+ "retrieveCheckout",
4226
+ "Comgate",
4227
+ {
4228
+ reason: "Comgate does not support retrieving checkouts, use the payment API instead",
4229
+ alternative: "Use the payment API instead"
4230
+ }
4231
+ );
4201
4232
  };
4202
4233
  createCustomer = async (params) => {
4203
4234
  throw new core.ProviderNotSupportedError("createCustomer", "Comgate", {
@@ -4206,10 +4237,14 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4206
4237
  });
4207
4238
  };
4208
4239
  retrieveCustomer = async (id) => {
4209
- throw new core.ProviderNotSupportedError("retrieveCustomer", "Comgate", {
4210
- reason: "Comgate does not support retrieving customers",
4211
- alternative: "Use the payment API instead"
4212
- });
4240
+ throw new core.ProviderNotSupportedError(
4241
+ "retrieveCustomer",
4242
+ "Comgate",
4243
+ {
4244
+ reason: "Comgate does not support retrieving customers",
4245
+ alternative: "Use the payment API instead"
4246
+ }
4247
+ );
4213
4248
  };
4214
4249
  updateCustomer = async (id, params) => {
4215
4250
  throw new core.ProviderNotSupportedError("updateCustomer", "Comgate", {
@@ -4226,28 +4261,41 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4226
4261
  createPayment = async (params) => {
4227
4262
  const { error, data } = core.createPaymentSchema.safeParse(params);
4228
4263
  if (error) {
4229
- throw core.ValidationError.fromZodError(error, this.providerName, "createPayment");
4264
+ throw core.ValidationError.fromZodError(
4265
+ error,
4266
+ this.providerName,
4267
+ "createPayment"
4268
+ );
4230
4269
  }
4231
4270
  const { customer } = data;
4232
- if (typeof customer === "object") {
4233
- throw new core.InvalidTypeError("customer", "string (customer ID)", "object", {
4234
- provider: this.providerName,
4235
- method: "createPayment"
4236
- });
4271
+ if (!core.isIdCustomer(customer)) {
4272
+ throw new core.InvalidTypeError(
4273
+ "customer",
4274
+ "object with id",
4275
+ customer === null ? "null" : "object with email",
4276
+ {
4277
+ provider: this.providerName,
4278
+ method: "createPayment"
4279
+ }
4280
+ );
4237
4281
  }
4282
+ const payerId = String(customer.id);
4238
4283
  const { email, paymentLabel = "Order from Eshop" } = core.validateRequiredKeys(
4239
4284
  ["email", "paymentLabel"],
4240
4285
  data.provider_metadata ?? {},
4241
4286
  "Missing required provider metadata: {keys}"
4242
4287
  );
4243
4288
  if (this.opts.debug) {
4244
- console.log("Creating payment with metadata:", { email, paymentLabel });
4289
+ console.log("Creating payment with metadata:", {
4290
+ email,
4291
+ paymentLabel
4292
+ });
4245
4293
  }
4246
4294
  const requestBody = new URLSearchParams({
4247
4295
  code: "0",
4248
4296
  test: this.opts.isSandbox ? "true" : "false",
4249
4297
  refId: JSON.stringify(data.metadata ?? {}),
4250
- payerId: customer,
4298
+ payerId,
4251
4299
  price: String(data.amount),
4252
4300
  email,
4253
4301
  curr: String(data.currency),
@@ -4266,9 +4314,12 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4266
4314
  currency: data.currency,
4267
4315
  status: "pending",
4268
4316
  metadata: Object.fromEntries(
4269
- Object.entries(data.metadata ?? {}).map(([key, value]) => [key, String(value)])
4317
+ Object.entries(data.metadata ?? {}).map(([key, value]) => [
4318
+ key,
4319
+ String(value)
4320
+ ])
4270
4321
  ),
4271
- customer,
4322
+ customer: core.isIdCustomer(customer) ? customer : null,
4272
4323
  item_id: data.item_id ?? null,
4273
4324
  requires_action: false,
4274
4325
  payment_url: response.value.redirect ?? null
@@ -4282,36 +4333,55 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4282
4333
  });
4283
4334
  };
4284
4335
  deletePayment = async (id) => {
4285
- const response = await this._client.delete(`/v2.0/payment/${id}.json`);
4336
+ const response = await this._client.delete(
4337
+ `/v2.0/payment/${id}.json`
4338
+ );
4286
4339
  if (!response.ok) {
4287
- throw new core.OperationFailedError(`Failed to delete payment`, this.providerName, {
4288
- cause: new Error("Unknown error")
4289
- });
4340
+ throw new core.OperationFailedError(
4341
+ `Failed to delete payment`,
4342
+ this.providerName,
4343
+ {
4344
+ cause: new Error("Unknown error")
4345
+ }
4346
+ );
4290
4347
  }
4291
4348
  return null;
4292
4349
  };
4293
4350
  retrievePayment = async (id) => {
4294
4351
  const { error } = core.createPaymentSchema.safeParse({ id });
4295
- if (error) throw new Error(`Payment retrieval validation failed: ${error.message}`);
4352
+ if (error)
4353
+ throw new Error(
4354
+ `Payment retrieval validation failed: ${error.message}`
4355
+ );
4296
4356
  const response = await this._client.get(
4297
4357
  `/v2.0/payment/${id}.json`
4298
4358
  );
4299
4359
  if (!response.ok) {
4300
- throw new core.OperationFailedError(`Failed to retrieve payment`, this.providerName, {
4301
- cause: new Error("Unknown error")
4302
- });
4360
+ throw new core.OperationFailedError(
4361
+ `Failed to retrieve payment`,
4362
+ this.providerName,
4363
+ {
4364
+ cause: new Error("Unknown error")
4365
+ }
4366
+ );
4303
4367
  }
4304
4368
  return response.value;
4305
4369
  };
4306
4370
  capturePayment = async (id, params) => {
4307
4371
  const { error, data } = core.capturePaymentSchema.safeParse(params);
4308
4372
  if (error) {
4309
- throw core.ValidationError.fromZodError(error, this.providerName, "capturePayment");
4373
+ throw core.ValidationError.fromZodError(
4374
+ error,
4375
+ this.providerName,
4376
+ "capturePayment"
4377
+ );
4310
4378
  }
4311
4379
  const response = await this._client.post(
4312
4380
  `/v2.0/preauth/${id}/confirm.json`,
4313
4381
  {
4314
- body: new URLSearchParams({ amount: String(data.amount) }).toString()
4382
+ body: new URLSearchParams({
4383
+ amount: String(data.amount)
4384
+ }).toString()
4315
4385
  }
4316
4386
  );
4317
4387
  this._throwOnError(response, "Failed to capture payment");
@@ -4321,7 +4391,7 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4321
4391
  currency: "CZK",
4322
4392
  status: "succeeded",
4323
4393
  metadata: {},
4324
- customer: "",
4394
+ customer: null,
4325
4395
  item_id: null,
4326
4396
  requires_action: false,
4327
4397
  payment_url: response.value.redirect ?? null
@@ -4348,64 +4418,96 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4348
4418
  test: this.opts.isSandbox ? "true" : "false",
4349
4419
  refId: JSON.stringify(data.metadata ?? {})
4350
4420
  });
4351
- const response = await this._client.post("/v2.0/refund.json", {
4352
- body: requestBody.toString()
4353
- });
4421
+ const response = await this._client.post(
4422
+ "/v2.0/refund.json",
4423
+ {
4424
+ body: requestBody.toString()
4425
+ }
4426
+ );
4354
4427
  this._throwOnError(response, "Failed to create refund");
4355
4428
  const refundObject = {
4356
4429
  id: `paykit:refund:${Math.random().toString(36).substring(2, 15)}`,
4357
4430
  amount: data.amount,
4358
4431
  currency: "CZK",
4359
4432
  reason: data.reason,
4360
- metadata: Object.fromEntries(
4361
- Object.entries(data.metadata ?? {}).map(([key, value]) => [key, String(value)])
4362
- )
4433
+ metadata: {
4434
+ ...Object.fromEntries(
4435
+ Object.entries(data.metadata ?? {}).map(([key, value]) => [
4436
+ key,
4437
+ String(value)
4438
+ ])
4439
+ ),
4440
+ ...response.value?.code ? { __comgate_refund_code: String(response.value.code) } : {},
4441
+ ...response.value?.message && {
4442
+ __comgate_refund_message: response.value.message
4443
+ }
4444
+ }
4363
4445
  };
4364
4446
  return refundObject;
4365
4447
  };
4366
4448
  createSubscription = async (params) => {
4367
- throw new core.ProviderNotSupportedError("createSubscription", "Comgate", {
4368
- reason: "Comgate does not support creating subscriptions",
4369
- alternative: "Use the payment API instead and create a subscription manually"
4370
- });
4449
+ throw new core.ProviderNotSupportedError(
4450
+ "createSubscription",
4451
+ "Comgate",
4452
+ {
4453
+ reason: "Comgate does not support creating subscriptions",
4454
+ alternative: "Use the payment API instead and create a subscription manually"
4455
+ }
4456
+ );
4371
4457
  };
4372
4458
  deleteSubscription = async (id) => {
4373
- throw new core.ProviderNotSupportedError("deleteSubscription", "Comgate", {
4374
- reason: "Comgate does not support deleting subscriptions",
4375
- alternative: "Use the payment API instead and delete the subscription manually"
4376
- });
4459
+ throw new core.ProviderNotSupportedError(
4460
+ "deleteSubscription",
4461
+ "Comgate",
4462
+ {
4463
+ reason: "Comgate does not support deleting subscriptions",
4464
+ alternative: "Use the payment API instead and delete the subscription manually"
4465
+ }
4466
+ );
4377
4467
  };
4378
4468
  updateSubscription = async (id, params) => {
4379
- throw new core.ProviderNotSupportedError("updateSubscription", "Comgate", {
4380
- reason: "Comgate does not support updating subscriptions",
4381
- alternative: "Use the payment API instead and update the subscription manually"
4382
- });
4469
+ throw new core.ProviderNotSupportedError(
4470
+ "updateSubscription",
4471
+ "Comgate",
4472
+ {
4473
+ reason: "Comgate does not support updating subscriptions",
4474
+ alternative: "Use the payment API instead and update the subscription manually"
4475
+ }
4476
+ );
4383
4477
  };
4384
4478
  retrieveSubscription = async (id) => {
4385
- throw new core.ProviderNotSupportedError("retrieveSubscription", "Comgate", {
4386
- reason: "Comgate does not support retrieving subscriptions",
4387
- alternative: "Use the payment API instead and retrieve the subscription manually"
4388
- });
4479
+ throw new core.ProviderNotSupportedError(
4480
+ "retrieveSubscription",
4481
+ "Comgate",
4482
+ {
4483
+ reason: "Comgate does not support retrieving subscriptions",
4484
+ alternative: "Use the payment API instead and retrieve the subscription manually"
4485
+ }
4486
+ );
4389
4487
  };
4390
4488
  cancelSubscription = async (id) => {
4391
- throw new core.ProviderNotSupportedError("cancelSubscription", "Comgate", {
4392
- reason: "Comgate does not support canceling subscriptions",
4393
- alternative: "Use the payment API instead and cancel the subscription manually"
4394
- });
4489
+ throw new core.ProviderNotSupportedError(
4490
+ "cancelSubscription",
4491
+ "Comgate",
4492
+ {
4493
+ reason: "Comgate does not support canceling subscriptions",
4494
+ alternative: "Use the payment API instead and cancel the subscription manually"
4495
+ }
4496
+ );
4395
4497
  };
4396
- handleWebhook = async ({
4397
- body: rawBody,
4398
- headers
4399
- }) => {
4498
+ handleWebhook = async (payload, webhookSecret) => {
4499
+ const { body: rawBody, headersAsObject } = payload;
4400
4500
  let body;
4401
- const contentType = headers.get("content-type");
4501
+ const contentType = headersAsObject["content-type"];
4402
4502
  if (contentType === "application/json") {
4403
4503
  body = JSON.parse(rawBody);
4404
4504
  } else {
4405
4505
  body = Object.fromEntries(new URLSearchParams(rawBody));
4406
4506
  }
4407
4507
  if (!body || typeof body !== "object") {
4408
- throw new core.WebhookError("Invalid webhook payload", { provider: this.providerName });
4508
+ throw new core.WebhookError("Invalid webhook payload", {
4509
+ provider: this.providerName
4510
+ });
4409
4511
  }
4410
4512
  if (this.opts.debug) {
4411
4513
  console.info("Verifying webhook...");
@@ -4421,7 +4523,9 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4421
4523
  "Missing required webhook parameters: {keys}"
4422
4524
  );
4423
4525
  if (secret !== this.opts.secret) {
4424
- throw new core.WebhookError("Webhook secret mismatch", { provider: this.providerName });
4526
+ throw new core.WebhookError("Webhook secret mismatch", {
4527
+ provider: this.providerName
4528
+ });
4425
4529
  }
4426
4530
  if (merchant !== this.opts.merchant) {
4427
4531
  throw new core.WebhookError("Webhook merchant mismatch", {
@@ -4447,7 +4551,9 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4447
4551
  );
4448
4552
  }
4449
4553
  if (!comgateWebhookApiResponse.status) {
4450
- throw new core.WebhookError("Failed to verify webhook: no status returned");
4554
+ throw new core.WebhookError(
4555
+ "Failed to verify webhook: no status returned"
4556
+ );
4451
4557
  }
4452
4558
  if (comgateWebhookApiResponse.status !== webhookStatusOut) {
4453
4559
  throw new core.WebhookError(
@@ -4463,7 +4569,7 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4463
4569
  const status = statusMap[comgateWebhookApiResponse.status];
4464
4570
  const webhookHandlers = {
4465
4571
  pending: (data) => {
4466
- const payment = paykitPayment$InboundSchema(data, "pending");
4572
+ const payment = Payment$inboundSchema(data, "pending");
4467
4573
  return [
4468
4574
  core.paykitEvent$InboundSchema({
4469
4575
  type: "payment.created",
@@ -4474,7 +4580,10 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4474
4580
  ];
4475
4581
  },
4476
4582
  requires_capture: (data) => {
4477
- const payment = paykitPayment$InboundSchema(data, "requires_capture");
4583
+ const payment = Payment$inboundSchema(
4584
+ data,
4585
+ "requires_capture"
4586
+ );
4478
4587
  return [
4479
4588
  core.paykitEvent$InboundSchema({
4480
4589
  type: "payment.updated",
@@ -4485,10 +4594,10 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4485
4594
  ];
4486
4595
  },
4487
4596
  canceled: (data) => {
4488
- const payment = paykitPayment$InboundSchema(data, "canceled");
4597
+ const payment = Payment$inboundSchema(data, "canceled");
4489
4598
  return [
4490
4599
  core.paykitEvent$InboundSchema({
4491
- type: "payment.canceled",
4600
+ type: "payment.failed",
4492
4601
  created: (/* @__PURE__ */ new Date()).getTime(),
4493
4602
  id: `paykit:payment:${Math.random().toString(36).substring(2, 15)}`,
4494
4603
  data: payment
@@ -4496,11 +4605,11 @@ var ComgateProvider = class extends core.AbstractPayKitProvider {
4496
4605
  ];
4497
4606
  },
4498
4607
  succeeded: (data) => {
4499
- const invoice = paykitInvoice$InboundSchema(data);
4500
- const payment = paykitPayment$InboundSchema(data, "succeeded");
4608
+ const invoice = Invoice$inboundSchema(data);
4609
+ const payment = Payment$inboundSchema(data, "succeeded");
4501
4610
  return [
4502
4611
  core.paykitEvent$InboundSchema({
4503
- type: "payment.created",
4612
+ type: "payment.succeeded",
4504
4613
  created: (/* @__PURE__ */ new Date()).getTime(),
4505
4614
  id: `paykit:payment:${Math.random().toString(36).substring(2, 15)}`,
4506
4615
  data: payment