@paykit-sdk/gopay 1.0.4 → 1.1.1
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 +105 -68
- package/dist/controllers/auth.js +12 -8
- package/dist/controllers/auth.mjs +12 -8
- package/dist/gopay-provider.js +249 -118
- package/dist/gopay-provider.mjs +249 -118
- package/dist/index.js +251 -119
- package/dist/index.mjs +251 -119
- package/dist/schema.d.mts +56 -38
- package/dist/schema.d.ts +56 -38
- package/dist/utils/mapper.d.mts +13 -7
- package/dist/utils/mapper.d.ts +13 -7
- package/dist/utils/mapper.js +106 -25
- package/dist/utils/mapper.mjs +105 -26
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -4058,14 +4058,14 @@ var ostring = () => stringType().optional();
|
|
|
4058
4058
|
var onumber = () => numberType().optional();
|
|
4059
4059
|
var oboolean = () => booleanType().optional();
|
|
4060
4060
|
var coerce = {
|
|
4061
|
-
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4062
|
-
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4063
|
-
boolean: (arg) => ZodBoolean.create({
|
|
4061
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4062
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4063
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4064
4064
|
...arg,
|
|
4065
4065
|
coerce: true
|
|
4066
|
-
}),
|
|
4067
|
-
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4068
|
-
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4066
|
+
})),
|
|
4067
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4068
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4069
4069
|
};
|
|
4070
4070
|
var NEVER = INVALID;
|
|
4071
4071
|
var AuthController = class {
|
|
@@ -4089,25 +4089,29 @@ var AuthController = class {
|
|
|
4089
4089
|
const credentials = Buffer.from(
|
|
4090
4090
|
`${this.opts.clientId}:${this.opts.clientSecret}`
|
|
4091
4091
|
).toString("base64");
|
|
4092
|
+
const body = new URLSearchParams({
|
|
4093
|
+
grant_type: "client_credentials",
|
|
4094
|
+
scope: "payment-all"
|
|
4095
|
+
}).toString();
|
|
4092
4096
|
const response = await this._client.post("/oauth2/token", {
|
|
4093
4097
|
headers: {
|
|
4094
4098
|
Authorization: `Basic ${credentials}`,
|
|
4095
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
4099
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
4100
|
+
Accept: "application/json"
|
|
4096
4101
|
},
|
|
4097
|
-
body
|
|
4098
|
-
grant_type: "client_credentials",
|
|
4099
|
-
scope: "payment-all"
|
|
4100
|
-
}).toString()
|
|
4102
|
+
body
|
|
4101
4103
|
});
|
|
4102
|
-
if (!response.ok || !response.value
|
|
4104
|
+
if (!response.ok || !response.value?.access_token) {
|
|
4103
4105
|
throw new core.OperationFailedError("getAccessToken", "gopay", {
|
|
4104
|
-
cause: new Error(
|
|
4106
|
+
cause: new Error(
|
|
4107
|
+
`Failed to obtain GoPay access token: ${JSON.stringify(response.value || response.error)}`
|
|
4108
|
+
)
|
|
4105
4109
|
});
|
|
4106
4110
|
}
|
|
4107
4111
|
const expiryTime = Date.now() + (response.value.expires_in - 300) * 1e3;
|
|
4108
4112
|
const accessToken = `${response.value.access_token}::paykit::${expiryTime}`;
|
|
4109
4113
|
this._accessToken = accessToken;
|
|
4110
|
-
return
|
|
4114
|
+
return response.value.access_token;
|
|
4111
4115
|
};
|
|
4112
4116
|
getAuthHeaders = async () => {
|
|
4113
4117
|
const token = await this.getAccessToken();
|
|
@@ -4118,10 +4122,15 @@ var AuthController = class {
|
|
|
4118
4122
|
};
|
|
4119
4123
|
};
|
|
4120
4124
|
};
|
|
4125
|
+
var decodeHtmlEntities = (str) => {
|
|
4126
|
+
return str.replace(/"/g, '"').replace(/'/g, "'").replace(/"/g, '"').replace(/'/g, "'");
|
|
4127
|
+
};
|
|
4121
4128
|
var paykitPayment$InboundSchema = (data) => {
|
|
4122
|
-
const
|
|
4123
|
-
|
|
4124
|
-
|
|
4129
|
+
const { item } = JSON.parse(
|
|
4130
|
+
decodeHtmlEntities(
|
|
4131
|
+
data.additional_params?.find((param) => param.name === core.PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
4132
|
+
)
|
|
4133
|
+
);
|
|
4125
4134
|
const metadata = core.omitInternalMetadata(
|
|
4126
4135
|
data.additional_params?.reduce(
|
|
4127
4136
|
(acc, param) => {
|
|
@@ -4131,28 +4140,66 @@ var paykitPayment$InboundSchema = (data) => {
|
|
|
4131
4140
|
{}
|
|
4132
4141
|
) ?? {}
|
|
4133
4142
|
);
|
|
4143
|
+
const statusMap = {
|
|
4144
|
+
CREATED: "pending",
|
|
4145
|
+
PAYMENT_METHOD_CHOSEN: "processing",
|
|
4146
|
+
PAID: "succeeded",
|
|
4147
|
+
AUTHORIZED: "requires_capture",
|
|
4148
|
+
CANCELED: "canceled",
|
|
4149
|
+
TIMEOUTED: "canceled",
|
|
4150
|
+
REFUNDED: "canceled",
|
|
4151
|
+
PARTIALLY_REFUNDED: "canceled"
|
|
4152
|
+
};
|
|
4153
|
+
const requiresAction = data.state === "CREATED" || data.state === "AUTHORIZED" ? true : false;
|
|
4134
4154
|
return {
|
|
4135
4155
|
id: data.id.toString(),
|
|
4136
4156
|
amount: data.amount,
|
|
4137
4157
|
currency: data.currency,
|
|
4138
|
-
customer: data.payer
|
|
4139
|
-
status: data.state,
|
|
4140
|
-
item_id:
|
|
4141
|
-
metadata
|
|
4158
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
4159
|
+
status: statusMap[data.state],
|
|
4160
|
+
item_id: item,
|
|
4161
|
+
metadata,
|
|
4162
|
+
requires_action: requiresAction,
|
|
4163
|
+
payment_url: data.gw_url ?? null
|
|
4164
|
+
};
|
|
4165
|
+
};
|
|
4166
|
+
var paykitCheckout$InboundSchema = (data) => {
|
|
4167
|
+
const { item, qty, type } = JSON.parse(
|
|
4168
|
+
decodeHtmlEntities(
|
|
4169
|
+
data.additional_params?.find((param) => param.name === core.PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
4170
|
+
)
|
|
4171
|
+
);
|
|
4172
|
+
const metadata = core.omitInternalMetadata(
|
|
4173
|
+
data.additional_params?.reduce(
|
|
4174
|
+
(acc, param) => {
|
|
4175
|
+
acc[param.name] = String(param.value);
|
|
4176
|
+
return acc;
|
|
4177
|
+
},
|
|
4178
|
+
{}
|
|
4179
|
+
) ?? {}
|
|
4180
|
+
);
|
|
4181
|
+
return {
|
|
4182
|
+
id: data.id.toString(),
|
|
4183
|
+
amount: data.amount,
|
|
4184
|
+
currency: data.currency,
|
|
4185
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
4186
|
+
payment_url: data.gw_url ?? "",
|
|
4187
|
+
metadata,
|
|
4188
|
+
session_type: type,
|
|
4189
|
+
products: [{ id: item, quantity: parseInt(qty) }]
|
|
4142
4190
|
};
|
|
4143
4191
|
};
|
|
4144
4192
|
var paykitInvoice$InboundSchema = (data, isSubscription) => {
|
|
4145
|
-
const
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
).itemId;
|
|
4193
|
+
const { item, qty } = JSON.parse(
|
|
4194
|
+
decodeHtmlEntities(
|
|
4195
|
+
data.additional_params?.find((param) => param.name === core.PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
4196
|
+
)
|
|
4197
|
+
);
|
|
4151
4198
|
const status = (() => {
|
|
4152
4199
|
if (data.state === "PAID") return "paid";
|
|
4153
4200
|
return "open";
|
|
4154
4201
|
})();
|
|
4155
|
-
const paidAt = isSubscription ? new Date(data.recurrence?.
|
|
4202
|
+
const paidAt = isSubscription ? new Date(data.recurrence?.recurrence_date_to ?? "") : /* @__PURE__ */ new Date();
|
|
4156
4203
|
const metadata = core.omitInternalMetadata(
|
|
4157
4204
|
data.additional_params?.reduce(
|
|
4158
4205
|
(acc, param) => {
|
|
@@ -4166,20 +4213,22 @@ var paykitInvoice$InboundSchema = (data, isSubscription) => {
|
|
|
4166
4213
|
id: data.id.toString(),
|
|
4167
4214
|
amount_paid: data.amount,
|
|
4168
4215
|
currency: data.currency,
|
|
4169
|
-
customer: data.payer
|
|
4216
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
4170
4217
|
status,
|
|
4171
4218
|
paid_at: paidAt.toISOString(),
|
|
4172
4219
|
metadata: metadata ?? {},
|
|
4173
4220
|
custom_fields: null,
|
|
4174
4221
|
subscription_id: isSubscription ? data.id.toString() : null,
|
|
4175
4222
|
billing_mode: isSubscription ? "recurring" : "one_time",
|
|
4176
|
-
line_items: [{ id:
|
|
4223
|
+
line_items: [{ id: item, quantity: parseInt(qty) }]
|
|
4177
4224
|
};
|
|
4178
4225
|
};
|
|
4179
4226
|
var paykitSubscription$InboundSchema = (data) => {
|
|
4180
|
-
const
|
|
4181
|
-
|
|
4182
|
-
|
|
4227
|
+
const { item } = JSON.parse(
|
|
4228
|
+
decodeHtmlEntities(
|
|
4229
|
+
data.additional_params?.find((param) => param.name === core.PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
4230
|
+
)
|
|
4231
|
+
);
|
|
4183
4232
|
const billingIntervalMap = {
|
|
4184
4233
|
DAY: "day",
|
|
4185
4234
|
WEEK: "week",
|
|
@@ -4187,8 +4236,28 @@ var paykitSubscription$InboundSchema = (data) => {
|
|
|
4187
4236
|
ON_DEMAND: "month"
|
|
4188
4237
|
};
|
|
4189
4238
|
const billingInterval = billingIntervalMap[data.recurrence?.recurrence_cycle ?? "ON_DEMAND"];
|
|
4190
|
-
const
|
|
4239
|
+
const recurrencePeriod = data.recurrence?.recurrence_period ?? 1;
|
|
4240
|
+
const recurrenceCycle = data.recurrence?.recurrence_cycle;
|
|
4191
4241
|
const currentPeriodEnd = new Date(data.recurrence?.recurrence_date_to ?? "");
|
|
4242
|
+
const currentPeriodStart = (() => {
|
|
4243
|
+
if (!currentPeriodEnd) return /* @__PURE__ */ new Date();
|
|
4244
|
+
const endDate = new Date(currentPeriodEnd);
|
|
4245
|
+
const startDate = new Date(endDate);
|
|
4246
|
+
switch (recurrenceCycle) {
|
|
4247
|
+
case "DAY":
|
|
4248
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod);
|
|
4249
|
+
break;
|
|
4250
|
+
case "WEEK":
|
|
4251
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod * 7);
|
|
4252
|
+
break;
|
|
4253
|
+
case "MONTH":
|
|
4254
|
+
startDate.setMonth(startDate.getMonth() - recurrencePeriod);
|
|
4255
|
+
break;
|
|
4256
|
+
default:
|
|
4257
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod);
|
|
4258
|
+
}
|
|
4259
|
+
return startDate;
|
|
4260
|
+
})();
|
|
4192
4261
|
const metadata = core.omitInternalMetadata(
|
|
4193
4262
|
data.additional_params?.reduce(
|
|
4194
4263
|
(acc, param) => {
|
|
@@ -4198,18 +4267,32 @@ var paykitSubscription$InboundSchema = (data) => {
|
|
|
4198
4267
|
{}
|
|
4199
4268
|
) ?? {}
|
|
4200
4269
|
);
|
|
4270
|
+
const requiresAction = data.state === "CREATED" ? true : false;
|
|
4271
|
+
const status = (() => {
|
|
4272
|
+
if (data.state === "CREATED") return "pending";
|
|
4273
|
+
if (data.state === "PAID") return "active";
|
|
4274
|
+
if (data.state === "CANCELED") return "canceled";
|
|
4275
|
+
if (data.state === "TIMEOUTED") return "canceled";
|
|
4276
|
+
if (data.state === "REFUNDED") return "canceled";
|
|
4277
|
+
if (data.state === "PARTIALLY_REFUNDED") return "canceled";
|
|
4278
|
+
if (data.state === "AUTHORIZED") return "active";
|
|
4279
|
+
if (data.state === "PAYMENT_METHOD_CHOSEN") return "active";
|
|
4280
|
+
return "pending";
|
|
4281
|
+
})();
|
|
4201
4282
|
return {
|
|
4202
4283
|
id: data.id.toString(),
|
|
4203
|
-
status
|
|
4204
|
-
customer: data.payer
|
|
4205
|
-
item_id:
|
|
4284
|
+
status,
|
|
4285
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
4286
|
+
item_id: item,
|
|
4206
4287
|
billing_interval: billingInterval,
|
|
4207
4288
|
currency: data.currency,
|
|
4208
4289
|
amount: data.amount,
|
|
4209
|
-
metadata
|
|
4290
|
+
metadata,
|
|
4210
4291
|
custom_fields: null,
|
|
4211
4292
|
current_period_start: currentPeriodStart,
|
|
4212
|
-
current_period_end: currentPeriodEnd
|
|
4293
|
+
current_period_end: currentPeriodEnd,
|
|
4294
|
+
requires_action: requiresAction,
|
|
4295
|
+
payment_url: requiresAction ? data.gw_url ?? null : null
|
|
4213
4296
|
};
|
|
4214
4297
|
};
|
|
4215
4298
|
var paykitRefund$InboundSchema = (data) => {
|
|
@@ -4239,7 +4322,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4239
4322
|
super(gopayOptionsSchema, opts, providerName);
|
|
4240
4323
|
this.opts = opts;
|
|
4241
4324
|
const debug = opts.debug ?? true;
|
|
4242
|
-
this.baseUrl = opts.isSandbox ? "https://
|
|
4325
|
+
this.baseUrl = opts.isSandbox ? "https://gw.sandbox.gopay.com/api" : "https://gate.gopay.cz/api";
|
|
4243
4326
|
this._client = new core.HTTPClient({
|
|
4244
4327
|
baseUrl: this.baseUrl,
|
|
4245
4328
|
headers: {},
|
|
@@ -4267,7 +4350,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4267
4350
|
);
|
|
4268
4351
|
if (this.opts.debug) {
|
|
4269
4352
|
console.info(
|
|
4270
|
-
"Specify `
|
|
4353
|
+
"Specify `language` in the `provider_metadata` of createCheckout to set the language of the checkout, default is `EN`"
|
|
4271
4354
|
);
|
|
4272
4355
|
console.info("Creating checkout with metadata:", data.metadata);
|
|
4273
4356
|
}
|
|
@@ -4296,31 +4379,42 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4296
4379
|
type: "ITEM"
|
|
4297
4380
|
}
|
|
4298
4381
|
],
|
|
4299
|
-
lang: data.provider_metadata?.
|
|
4382
|
+
lang: data.provider_metadata?.language ? data.provider_metadata.language : "EN",
|
|
4300
4383
|
callback: { return_url: data.success_url, notification_url: this.opts.webhookUrl },
|
|
4301
4384
|
additional_params: Object.entries({
|
|
4302
4385
|
...data.metadata,
|
|
4303
4386
|
[core.PAYKIT_METADATA_KEY]: JSON.stringify({
|
|
4304
|
-
|
|
4305
|
-
qty: data.quantity
|
|
4387
|
+
item: data.item_id,
|
|
4388
|
+
qty: data.quantity,
|
|
4389
|
+
type: data.session_type
|
|
4306
4390
|
})
|
|
4307
4391
|
}).map(([name, value]) => ({
|
|
4308
4392
|
name,
|
|
4309
4393
|
value: String(value)
|
|
4310
4394
|
}))
|
|
4311
4395
|
};
|
|
4312
|
-
const response = await this._client.post(
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4396
|
+
const response = await this._client.post(
|
|
4397
|
+
"/payments/payment",
|
|
4398
|
+
{
|
|
4399
|
+
body: JSON.stringify(goPayRequest),
|
|
4400
|
+
headers: await this.authController.getAuthHeaders()
|
|
4401
|
+
}
|
|
4402
|
+
);
|
|
4403
|
+
if (!response.ok) {
|
|
4404
|
+
throw new core.OperationFailedError("createCheckout", this.providerName, {
|
|
4405
|
+
cause: new Error("Failed to create checkout")
|
|
4406
|
+
});
|
|
4407
|
+
}
|
|
4408
|
+
return paykitCheckout$InboundSchema(response.value);
|
|
4318
4409
|
};
|
|
4319
4410
|
retrieveCheckout = async (id) => {
|
|
4320
4411
|
const response = await this._client.get(
|
|
4321
4412
|
`/payments/payment/${id}`,
|
|
4322
4413
|
{
|
|
4323
|
-
headers:
|
|
4414
|
+
headers: {
|
|
4415
|
+
...await this.authController.getAuthHeaders(),
|
|
4416
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
4417
|
+
}
|
|
4324
4418
|
}
|
|
4325
4419
|
);
|
|
4326
4420
|
if (!response.ok) {
|
|
@@ -4328,12 +4422,13 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4328
4422
|
cause: new Error("Failed to retrieve checkout")
|
|
4329
4423
|
});
|
|
4330
4424
|
}
|
|
4331
|
-
|
|
4332
|
-
return response.value;
|
|
4425
|
+
return paykitCheckout$InboundSchema(response.value);
|
|
4333
4426
|
};
|
|
4334
4427
|
updateCheckout = async (id, params) => {
|
|
4335
4428
|
if (this.opts.debug) {
|
|
4336
|
-
console.info(
|
|
4429
|
+
console.info(
|
|
4430
|
+
"Gopay doesn't support updating checkouts, returning existing checkout"
|
|
4431
|
+
);
|
|
4337
4432
|
}
|
|
4338
4433
|
const existing = await this.retrieveCheckout(id);
|
|
4339
4434
|
return existing;
|
|
@@ -4373,11 +4468,23 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4373
4468
|
method: "createCheckout"
|
|
4374
4469
|
});
|
|
4375
4470
|
}
|
|
4376
|
-
const {
|
|
4377
|
-
["
|
|
4471
|
+
const { success_url } = core.validateRequiredKeys(
|
|
4472
|
+
["success_url"],
|
|
4378
4473
|
data.provider_metadata,
|
|
4379
4474
|
"The following fields must be present in the provider_metadata of createCheckout: {keys}"
|
|
4380
4475
|
);
|
|
4476
|
+
if (this.opts.debug) {
|
|
4477
|
+
if (data.billing_interval == "year") {
|
|
4478
|
+
console.info(
|
|
4479
|
+
"GoPay does not support yearly subscriptions, using monthly instead"
|
|
4480
|
+
);
|
|
4481
|
+
}
|
|
4482
|
+
if (!data.provider_metadata?.description) {
|
|
4483
|
+
console.info(
|
|
4484
|
+
`No description provided for the subscription \`provider_metadata.description\`, using default description \`Subscription by ${data.customer.email}\``
|
|
4485
|
+
);
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4381
4488
|
const intervalMap = {
|
|
4382
4489
|
day: "DAY",
|
|
4383
4490
|
week: "WEEK",
|
|
@@ -4407,54 +4514,66 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4407
4514
|
amount: Number(data.amount),
|
|
4408
4515
|
currency: data.currency ?? "CZK",
|
|
4409
4516
|
order_number: crypto__namespace.randomBytes(8).toString("hex").slice(0, 15),
|
|
4410
|
-
order_description: data.
|
|
4411
|
-
items: [
|
|
4412
|
-
{ name: data.item_id, amount: Number(data.amount), count: parseInt(quantity) }
|
|
4413
|
-
],
|
|
4517
|
+
order_description: data.provider_metadata?.description ? data.provider_metadata.description : "Subscription by " + data.customer.email,
|
|
4518
|
+
items: [{ name: data.item_id, amount: Number(data.amount), count: data.quantity }],
|
|
4414
4519
|
recurrence: {
|
|
4415
4520
|
recurrence_cycle: recurrenceCycle,
|
|
4416
|
-
recurrence_period:
|
|
4521
|
+
recurrence_period: data.quantity,
|
|
4417
4522
|
recurrence_date_to: currentPeriodEnd
|
|
4418
4523
|
},
|
|
4419
|
-
callback: { return_url: success_url, notification_url: this.opts.webhookUrl }
|
|
4524
|
+
callback: { return_url: success_url, notification_url: this.opts.webhookUrl },
|
|
4525
|
+
additional_params: Object.entries({
|
|
4526
|
+
...data.metadata,
|
|
4527
|
+
[core.PAYKIT_METADATA_KEY]: JSON.stringify({
|
|
4528
|
+
item: data.item_id,
|
|
4529
|
+
qty: data.quantity
|
|
4530
|
+
})
|
|
4531
|
+
}).map(([name, value]) => ({
|
|
4532
|
+
name,
|
|
4533
|
+
value: String(value)
|
|
4534
|
+
}))
|
|
4420
4535
|
};
|
|
4421
|
-
const response = await this._client.post(
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4536
|
+
const response = await this._client.post(
|
|
4537
|
+
"/payments/payment",
|
|
4538
|
+
{
|
|
4539
|
+
body: JSON.stringify(goPaySubscriptionOptions),
|
|
4540
|
+
headers: await this.authController.getAuthHeaders()
|
|
4541
|
+
}
|
|
4542
|
+
);
|
|
4425
4543
|
if (!response.ok) {
|
|
4426
4544
|
throw new core.OperationFailedError("createSubscription", this.providerName, {
|
|
4427
4545
|
cause: new Error("Failed to create subscription")
|
|
4428
4546
|
});
|
|
4429
4547
|
}
|
|
4430
|
-
|
|
4431
|
-
return response.value;
|
|
4548
|
+
return paykitSubscription$InboundSchema(response.value);
|
|
4432
4549
|
};
|
|
4433
4550
|
updateSubscription = async (id, params) => {
|
|
4434
|
-
console.info("Gopay doesn't support updating subscriptions");
|
|
4435
4551
|
const subscription = await this.retrieveSubscription(id);
|
|
4436
4552
|
if (!subscription) {
|
|
4437
|
-
throw new core.
|
|
4438
|
-
|
|
4553
|
+
throw new core.ProviderNotSupportedError("updateSubscription", this.providerName, {
|
|
4554
|
+
reason: "Gopay doesn't support updating subscriptions",
|
|
4555
|
+
alternative: "Use the payment API instead and update the subscription manually"
|
|
4439
4556
|
});
|
|
4440
4557
|
}
|
|
4441
4558
|
return subscription;
|
|
4442
4559
|
};
|
|
4443
4560
|
cancelSubscription = async (id) => {
|
|
4444
|
-
const
|
|
4445
|
-
|
|
4446
|
-
{
|
|
4447
|
-
headers: await this.authController.getAuthHeaders()
|
|
4448
|
-
}
|
|
4449
|
-
);
|
|
4450
|
-
console.dir({ response }, { depth: 100 });
|
|
4451
|
-
const subscription = await this.retrieveSubscription(id);
|
|
4452
|
-
if (!subscription) {
|
|
4561
|
+
const existingSubscription = await this.retrieveSubscription(id);
|
|
4562
|
+
if (!existingSubscription) {
|
|
4453
4563
|
throw new core.OperationFailedError("cancelSubscription", this.providerName, {
|
|
4454
4564
|
cause: new Error("Failed to retrieve subscription")
|
|
4455
4565
|
});
|
|
4456
4566
|
}
|
|
4457
|
-
|
|
4567
|
+
const response = await this._client.post(`/payments/payment/${id}/void-recurrence`, {
|
|
4568
|
+
headers: {
|
|
4569
|
+
...await this.authController.getAuthHeaders(),
|
|
4570
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
4571
|
+
}
|
|
4572
|
+
});
|
|
4573
|
+
return {
|
|
4574
|
+
...existingSubscription,
|
|
4575
|
+
...response.value?.result == "FINISHED" && { status: "canceled" }
|
|
4576
|
+
};
|
|
4458
4577
|
};
|
|
4459
4578
|
deleteSubscription = async (id) => {
|
|
4460
4579
|
await this.cancelSubscription(id);
|
|
@@ -4472,14 +4591,13 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4472
4591
|
cause: new Error("Failed to retrieve subscription")
|
|
4473
4592
|
});
|
|
4474
4593
|
}
|
|
4475
|
-
|
|
4476
|
-
return response.value;
|
|
4594
|
+
return paykitSubscription$InboundSchema(response.value);
|
|
4477
4595
|
};
|
|
4478
4596
|
createPayment = async (params) => {
|
|
4479
4597
|
const { error, data } = core.createPaymentSchema.safeParse(params);
|
|
4480
4598
|
if (error)
|
|
4481
4599
|
throw core.ValidationError.fromZodError(error, this.providerName, "createPayment");
|
|
4482
|
-
if (typeof data.customer == "string" || typeof data.customer === "object" && !data.customer
|
|
4600
|
+
if (typeof data.customer == "string" || typeof data.customer === "object" && !data.customer?.email) {
|
|
4483
4601
|
throw new core.InvalidTypeError("customer", "object (customer) with email", "string", {
|
|
4484
4602
|
provider: this.providerName,
|
|
4485
4603
|
method: "createPayment"
|
|
@@ -4516,23 +4634,28 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4516
4634
|
value: String(value)
|
|
4517
4635
|
}))
|
|
4518
4636
|
};
|
|
4519
|
-
const response = await this._client.post(
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4637
|
+
const response = await this._client.post(
|
|
4638
|
+
"/payments/payment",
|
|
4639
|
+
{
|
|
4640
|
+
body: JSON.stringify(goPayRequest),
|
|
4641
|
+
headers: await this.authController.getAuthHeaders()
|
|
4642
|
+
}
|
|
4643
|
+
);
|
|
4523
4644
|
if (!response.ok) {
|
|
4524
4645
|
throw new core.OperationFailedError("createPayment", this.providerName, {
|
|
4525
4646
|
cause: new Error("Failed to create payment")
|
|
4526
4647
|
});
|
|
4527
4648
|
}
|
|
4528
|
-
|
|
4529
|
-
return response.value;
|
|
4649
|
+
return paykitPayment$InboundSchema(response.value);
|
|
4530
4650
|
};
|
|
4531
4651
|
retrievePayment = async (id) => {
|
|
4532
4652
|
const response = await this._client.get(
|
|
4533
4653
|
`/payments/payment/${id}`,
|
|
4534
4654
|
{
|
|
4535
|
-
headers:
|
|
4655
|
+
headers: {
|
|
4656
|
+
...await this.authController.getAuthHeaders(),
|
|
4657
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
4658
|
+
}
|
|
4536
4659
|
}
|
|
4537
4660
|
);
|
|
4538
4661
|
if (!response.ok) {
|
|
@@ -4540,8 +4663,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4540
4663
|
cause: new Error("Failed to retrieve payment")
|
|
4541
4664
|
});
|
|
4542
4665
|
}
|
|
4543
|
-
|
|
4544
|
-
return response.value;
|
|
4666
|
+
return paykitPayment$InboundSchema(response.value);
|
|
4545
4667
|
};
|
|
4546
4668
|
deletePayment = async (id) => {
|
|
4547
4669
|
throw new core.ProviderNotSupportedError("deletePayment", this.providerName, {
|
|
@@ -4551,7 +4673,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4551
4673
|
};
|
|
4552
4674
|
capturePayment = async (id, params) => {
|
|
4553
4675
|
const payment = await this._client.get(
|
|
4554
|
-
`/payments/payment/${id}`,
|
|
4676
|
+
`/payments/payment/${id}/capture`,
|
|
4555
4677
|
{
|
|
4556
4678
|
headers: await this.authController.getAuthHeaders()
|
|
4557
4679
|
}
|
|
@@ -4561,12 +4683,11 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4561
4683
|
cause: new Error("Failed to retrieve payment")
|
|
4562
4684
|
});
|
|
4563
4685
|
}
|
|
4564
|
-
const
|
|
4565
|
-
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4569
|
-
).qty;
|
|
4686
|
+
const { item, qty } = JSON.parse(
|
|
4687
|
+
decodeHtmlEntities(
|
|
4688
|
+
payment.value.additional_params?.find((param) => param.name === core.PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
4689
|
+
)
|
|
4690
|
+
);
|
|
4570
4691
|
if (!payment) {
|
|
4571
4692
|
throw new core.OperationFailedError("capturePayment", this.providerName, {
|
|
4572
4693
|
cause: new Error("Payment not found after capture")
|
|
@@ -4574,12 +4695,15 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4574
4695
|
}
|
|
4575
4696
|
const captureBody = {
|
|
4576
4697
|
amount: params.amount,
|
|
4577
|
-
items: [{ name:
|
|
4698
|
+
items: [{ name: item, amount: params.amount, count: qty }]
|
|
4578
4699
|
};
|
|
4579
|
-
await this._client.post(
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4700
|
+
await this._client.post(
|
|
4701
|
+
`/payments/payment/${id}/capture`,
|
|
4702
|
+
{
|
|
4703
|
+
body: JSON.stringify(captureBody),
|
|
4704
|
+
headers: await this.authController.getAuthHeaders()
|
|
4705
|
+
}
|
|
4706
|
+
);
|
|
4583
4707
|
return paykitPayment$InboundSchema(payment.value);
|
|
4584
4708
|
};
|
|
4585
4709
|
cancelPayment = async (id) => {
|
|
@@ -4613,24 +4737,22 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4613
4737
|
};
|
|
4614
4738
|
async createRefund(params) {
|
|
4615
4739
|
const { error, data } = core.createRefundSchema.safeParse(params);
|
|
4616
|
-
if (error)
|
|
4740
|
+
if (error) {
|
|
4617
4741
|
throw core.ValidationError.fromZodError(error, this.providerName, "createRefund");
|
|
4742
|
+
}
|
|
4618
4743
|
const payment = await this.retrievePayment(data.payment_id);
|
|
4619
4744
|
if (!payment) {
|
|
4620
4745
|
throw new core.OperationFailedError("createRefund", this.providerName, {
|
|
4621
4746
|
cause: new Error("Failed to retrieve payment")
|
|
4622
4747
|
});
|
|
4623
4748
|
}
|
|
4624
|
-
const response = await this._client.post(
|
|
4625
|
-
|
|
4626
|
-
{
|
|
4627
|
-
|
|
4628
|
-
|
|
4629
|
-
...await this.authController.getAuthHeaders(),
|
|
4630
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
4631
|
-
}
|
|
4749
|
+
const response = await this._client.post(`/payments/payment/${data.payment_id}/refund`, {
|
|
4750
|
+
body: new URLSearchParams({ amount: String(data.amount) }).toString(),
|
|
4751
|
+
headers: {
|
|
4752
|
+
...await this.authController.getAuthHeaders(),
|
|
4753
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
4632
4754
|
}
|
|
4633
|
-
);
|
|
4755
|
+
});
|
|
4634
4756
|
if (!response.ok) {
|
|
4635
4757
|
throw new core.OperationFailedError("createRefund", this.providerName, {
|
|
4636
4758
|
cause: new Error("Failed to create refund")
|
|
@@ -4648,6 +4770,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4648
4770
|
const { fullUrl } = payload;
|
|
4649
4771
|
const paymentId = new URL(fullUrl).searchParams.get("id");
|
|
4650
4772
|
const parentId = new URL(fullUrl).searchParams.get("parent_id");
|
|
4773
|
+
console.log({ paymentId, parentId });
|
|
4651
4774
|
if (!paymentId) {
|
|
4652
4775
|
throw new core.WebhookError("Payment ID is required", { provider: this.providerName });
|
|
4653
4776
|
}
|
|
@@ -4656,9 +4779,13 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4656
4779
|
}
|
|
4657
4780
|
const [payment, error] = await core.tryCatchAsync(
|
|
4658
4781
|
this._client.get(`/payments/payment/${paymentId}`, {
|
|
4659
|
-
headers:
|
|
4782
|
+
headers: {
|
|
4783
|
+
...await this.authController.getAuthHeaders(),
|
|
4784
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
4785
|
+
}
|
|
4660
4786
|
})
|
|
4661
4787
|
);
|
|
4788
|
+
console.log({ payment });
|
|
4662
4789
|
if (error) {
|
|
4663
4790
|
throw new core.WebhookError("Failed to retrieve payment", {
|
|
4664
4791
|
provider: this.providerName
|
|
@@ -4735,7 +4862,9 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4735
4862
|
canceled: (data) => {
|
|
4736
4863
|
const payment2 = paykitPayment$InboundSchema(data);
|
|
4737
4864
|
const isCancellingSubscription = parentId && data.recurrence?.recurrence_state == "STOPPED";
|
|
4738
|
-
const subscription = paykitSubscription$InboundSchema(
|
|
4865
|
+
const subscription = paykitSubscription$InboundSchema(
|
|
4866
|
+
data
|
|
4867
|
+
);
|
|
4739
4868
|
const subscriptionCanceledWebhookEvent = {
|
|
4740
4869
|
type: "subscription.canceled",
|
|
4741
4870
|
created: (/* @__PURE__ */ new Date()).getTime(),
|
|
@@ -4766,7 +4895,9 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4766
4895
|
succeeded: (data) => {
|
|
4767
4896
|
const payment2 = paykitPayment$InboundSchema(data);
|
|
4768
4897
|
const invoice = paykitInvoice$InboundSchema(data, !!parentId);
|
|
4769
|
-
const subscription = paykitSubscription$InboundSchema(
|
|
4898
|
+
const subscription = paykitSubscription$InboundSchema(
|
|
4899
|
+
data
|
|
4900
|
+
);
|
|
4770
4901
|
const subscriptionCreatedWebhookEvent = {
|
|
4771
4902
|
type: "subscription.created",
|
|
4772
4903
|
created: (/* @__PURE__ */ new Date()).getTime(),
|
|
@@ -4836,7 +4967,8 @@ var gopay = () => {
|
|
|
4836
4967
|
clientSecret: envVars.GOPAY_CLIENT_SECRET,
|
|
4837
4968
|
goId: envVars.GOPAY_GO_ID,
|
|
4838
4969
|
isSandbox: envVars.GOPAY_SANDBOX === "true",
|
|
4839
|
-
webhookUrl: envVars.GOPAY_WEBHOOK_URL
|
|
4970
|
+
webhookUrl: envVars.GOPAY_WEBHOOK_URL,
|
|
4971
|
+
debug: true
|
|
4840
4972
|
});
|
|
4841
4973
|
};
|
|
4842
4974
|
|