@paykit-sdk/gopay 1.1.1 → 1.1.2
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/dist/gopay-provider.d.mts +1 -5
- package/dist/gopay-provider.d.ts +1 -5
- package/dist/gopay-provider.js +69 -89
- package/dist/gopay-provider.mjs +70 -90
- package/dist/index.js +73 -91
- package/dist/index.mjs +74 -92
- package/dist/schema.d.mts +1 -15
- package/dist/schema.d.ts +1 -15
- package/package.json +7 -7
- package/dist/controllers/auth.d.mts +0 -19
- package/dist/controllers/auth.d.ts +0 -19
- package/dist/controllers/auth.js +0 -61
- package/dist/controllers/auth.mjs +0 -59
|
@@ -13,10 +13,6 @@ interface GoPayOptions extends PaykitProviderOptions {
|
|
|
13
13
|
* The GoID for the GoPay API
|
|
14
14
|
*/
|
|
15
15
|
goId: string;
|
|
16
|
-
/**
|
|
17
|
-
* Whether to use the sandbox environment
|
|
18
|
-
*/
|
|
19
|
-
isSandbox: boolean;
|
|
20
16
|
/**
|
|
21
17
|
* The webhook URL for the GoPay API
|
|
22
18
|
*/
|
|
@@ -27,7 +23,7 @@ declare class GoPayProvider extends AbstractPayKitProvider implements PayKitProv
|
|
|
27
23
|
readonly providerName = "gopay";
|
|
28
24
|
private _client;
|
|
29
25
|
private baseUrl;
|
|
30
|
-
private
|
|
26
|
+
private tokenManager;
|
|
31
27
|
constructor(opts: GoPayOptions);
|
|
32
28
|
createCheckout: (params: CreateCheckoutSchema) => Promise<Checkout>;
|
|
33
29
|
retrieveCheckout: (id: string) => Promise<Checkout | null>;
|
package/dist/gopay-provider.d.ts
CHANGED
|
@@ -13,10 +13,6 @@ interface GoPayOptions extends PaykitProviderOptions {
|
|
|
13
13
|
* The GoID for the GoPay API
|
|
14
14
|
*/
|
|
15
15
|
goId: string;
|
|
16
|
-
/**
|
|
17
|
-
* Whether to use the sandbox environment
|
|
18
|
-
*/
|
|
19
|
-
isSandbox: boolean;
|
|
20
16
|
/**
|
|
21
17
|
* The webhook URL for the GoPay API
|
|
22
18
|
*/
|
|
@@ -27,7 +23,7 @@ declare class GoPayProvider extends AbstractPayKitProvider implements PayKitProv
|
|
|
27
23
|
readonly providerName = "gopay";
|
|
28
24
|
private _client;
|
|
29
25
|
private baseUrl;
|
|
30
|
-
private
|
|
26
|
+
private tokenManager;
|
|
31
27
|
constructor(opts: GoPayOptions);
|
|
32
28
|
createCheckout: (params: CreateCheckoutSchema) => Promise<Checkout>;
|
|
33
29
|
retrieveCheckout: (id: string) => Promise<Checkout | null>;
|
package/dist/gopay-provider.js
CHANGED
|
@@ -4068,60 +4068,6 @@ var coerce = {
|
|
|
4068
4068
|
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4069
4069
|
};
|
|
4070
4070
|
var NEVER = INVALID;
|
|
4071
|
-
var AuthController = class {
|
|
4072
|
-
constructor(opts) {
|
|
4073
|
-
this.opts = opts;
|
|
4074
|
-
const debug = opts.debug ?? true;
|
|
4075
|
-
this._client = new core.HTTPClient({
|
|
4076
|
-
baseUrl: this.opts.baseUrl,
|
|
4077
|
-
headers: {},
|
|
4078
|
-
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
4079
|
-
});
|
|
4080
|
-
}
|
|
4081
|
-
_client;
|
|
4082
|
-
_accessToken = null;
|
|
4083
|
-
getAccessToken = async () => {
|
|
4084
|
-
if (this._accessToken) {
|
|
4085
|
-
const [token, , expiryStr] = this._accessToken.split("::paykit::");
|
|
4086
|
-
const expiry = parseInt(expiryStr || "0", 10);
|
|
4087
|
-
if (expiry > Date.now()) return token;
|
|
4088
|
-
}
|
|
4089
|
-
const credentials = Buffer.from(
|
|
4090
|
-
`${this.opts.clientId}:${this.opts.clientSecret}`
|
|
4091
|
-
).toString("base64");
|
|
4092
|
-
const body = new URLSearchParams({
|
|
4093
|
-
grant_type: "client_credentials",
|
|
4094
|
-
scope: "payment-all"
|
|
4095
|
-
}).toString();
|
|
4096
|
-
const response = await this._client.post("/oauth2/token", {
|
|
4097
|
-
headers: {
|
|
4098
|
-
Authorization: `Basic ${credentials}`,
|
|
4099
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
4100
|
-
Accept: "application/json"
|
|
4101
|
-
},
|
|
4102
|
-
body
|
|
4103
|
-
});
|
|
4104
|
-
if (!response.ok || !response.value?.access_token) {
|
|
4105
|
-
throw new core.OperationFailedError("getAccessToken", "gopay", {
|
|
4106
|
-
cause: new Error(
|
|
4107
|
-
`Failed to obtain GoPay access token: ${JSON.stringify(response.value || response.error)}`
|
|
4108
|
-
)
|
|
4109
|
-
});
|
|
4110
|
-
}
|
|
4111
|
-
const expiryTime = Date.now() + (response.value.expires_in - 300) * 1e3;
|
|
4112
|
-
const accessToken = `${response.value.access_token}::paykit::${expiryTime}`;
|
|
4113
|
-
this._accessToken = accessToken;
|
|
4114
|
-
return response.value.access_token;
|
|
4115
|
-
};
|
|
4116
|
-
getAuthHeaders = async () => {
|
|
4117
|
-
const token = await this.getAccessToken();
|
|
4118
|
-
return {
|
|
4119
|
-
Authorization: `Bearer ${token}`,
|
|
4120
|
-
"Content-Type": "application/json",
|
|
4121
|
-
Accept: "application/json"
|
|
4122
|
-
};
|
|
4123
|
-
};
|
|
4124
|
-
};
|
|
4125
4071
|
var decodeHtmlEntities = (str) => {
|
|
4126
4072
|
return str.replace(/"/g, '"').replace(/'/g, "'").replace(/"/g, '"').replace(/'/g, "'");
|
|
4127
4073
|
};
|
|
@@ -4328,16 +4274,36 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4328
4274
|
headers: {},
|
|
4329
4275
|
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
4330
4276
|
});
|
|
4331
|
-
this.
|
|
4277
|
+
this.tokenManager = new core.OAuth2TokenManager({
|
|
4278
|
+
client: this._client,
|
|
4279
|
+
provider: this.providerName,
|
|
4280
|
+
tokenEndpoint: "/oauth2/token",
|
|
4281
|
+
credentials: { username: opts.clientId, password: opts.clientSecret },
|
|
4282
|
+
responseAdapter: (response) => ({
|
|
4283
|
+
accessToken: response.access_token,
|
|
4284
|
+
expiresIn: response.expires_in
|
|
4285
|
+
}),
|
|
4286
|
+
expiryBuffer: 5 * 60,
|
|
4287
|
+
// 5 minutes
|
|
4288
|
+
requestBody: "grant_type=client_credentials&scope=payment-all",
|
|
4289
|
+
requestHeaders: {
|
|
4290
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
4291
|
+
Accept: "application/json"
|
|
4292
|
+
},
|
|
4293
|
+
authHeaders: {
|
|
4294
|
+
"Content-Type": "application/json",
|
|
4295
|
+
Accept: "application/json"
|
|
4296
|
+
}
|
|
4297
|
+
});
|
|
4332
4298
|
}
|
|
4333
4299
|
providerName = providerName;
|
|
4334
4300
|
_client;
|
|
4335
4301
|
baseUrl;
|
|
4336
|
-
|
|
4302
|
+
tokenManager;
|
|
4337
4303
|
createCheckout = async (params) => {
|
|
4338
4304
|
const { error, data } = core.createCheckoutSchema.safeParse(params);
|
|
4339
4305
|
if (error) throw core.ValidationError.fromZodError(error, "gopay", "createCheckout");
|
|
4340
|
-
if (
|
|
4306
|
+
if (!core.isEmailCustomer(data.customer)) {
|
|
4341
4307
|
throw new core.InvalidTypeError("customer", "object (customer) with email", "string", {
|
|
4342
4308
|
provider: this.providerName,
|
|
4343
4309
|
method: "createCheckout"
|
|
@@ -4397,7 +4363,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4397
4363
|
"/payments/payment",
|
|
4398
4364
|
{
|
|
4399
4365
|
body: JSON.stringify(goPayRequest),
|
|
4400
|
-
headers: await this.
|
|
4366
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4401
4367
|
}
|
|
4402
4368
|
);
|
|
4403
4369
|
if (!response.ok) {
|
|
@@ -4412,7 +4378,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4412
4378
|
`/payments/payment/${id}`,
|
|
4413
4379
|
{
|
|
4414
4380
|
headers: {
|
|
4415
|
-
...await this.
|
|
4381
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4416
4382
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4417
4383
|
}
|
|
4418
4384
|
}
|
|
@@ -4440,23 +4406,36 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4440
4406
|
});
|
|
4441
4407
|
};
|
|
4442
4408
|
createCustomer = async (params) => {
|
|
4409
|
+
if (this.cloudClient) {
|
|
4410
|
+
return this.cloudClient.customers.create(params);
|
|
4411
|
+
}
|
|
4443
4412
|
throw new core.ProviderNotSupportedError("createCustomer", "gopay", {
|
|
4444
|
-
reason: "Gopay doesn't support creating customers"
|
|
4413
|
+
reason: "Gopay doesn't support creating customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4445
4414
|
});
|
|
4446
4415
|
};
|
|
4447
4416
|
updateCustomer = async (id, params) => {
|
|
4417
|
+
if (this.cloudClient) {
|
|
4418
|
+
return this.cloudClient.customers.update(id, params);
|
|
4419
|
+
}
|
|
4448
4420
|
throw new core.ProviderNotSupportedError("updateCustomer", "gopay", {
|
|
4449
|
-
reason: "Gopay doesn't support updating customers"
|
|
4421
|
+
reason: "Gopay doesn't support updating customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4450
4422
|
});
|
|
4451
4423
|
};
|
|
4452
4424
|
deleteCustomer = async (id) => {
|
|
4425
|
+
if (this.cloudClient) {
|
|
4426
|
+
this.cloudClient.customers.delete(id);
|
|
4427
|
+
return null;
|
|
4428
|
+
}
|
|
4453
4429
|
throw new core.ProviderNotSupportedError("deleteCustomer", "gopay", {
|
|
4454
|
-
reason: "Gopay doesn't support deleting customers"
|
|
4430
|
+
reason: "Gopay doesn't support deleting customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4455
4431
|
});
|
|
4456
4432
|
};
|
|
4457
4433
|
retrieveCustomer = async (id) => {
|
|
4434
|
+
if (this.cloudClient) {
|
|
4435
|
+
return this.cloudClient.customers.retrieve(id);
|
|
4436
|
+
}
|
|
4458
4437
|
throw new core.ProviderNotSupportedError("retrieveCustomer", "gopay", {
|
|
4459
|
-
reason: "Gopay doesn't support retrieving customers"
|
|
4438
|
+
reason: "Gopay doesn't support retrieving customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4460
4439
|
});
|
|
4461
4440
|
};
|
|
4462
4441
|
createSubscription = async (params) => {
|
|
@@ -4474,9 +4453,9 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4474
4453
|
"The following fields must be present in the provider_metadata of createCheckout: {keys}"
|
|
4475
4454
|
);
|
|
4476
4455
|
if (this.opts.debug) {
|
|
4477
|
-
if (data.billing_interval
|
|
4456
|
+
if (data.billing_interval === "year") {
|
|
4478
4457
|
console.info(
|
|
4479
|
-
"GoPay does not support yearly subscriptions,
|
|
4458
|
+
"GoPay does not support yearly subscriptions, the available options are `day`, `week`, `month` and `ON_DEMAND` \n raise an issue at https://github.com/usepaykit/paykit-sdk/issues if you need this feature"
|
|
4480
4459
|
);
|
|
4481
4460
|
}
|
|
4482
4461
|
if (!data.provider_metadata?.description) {
|
|
@@ -4489,21 +4468,29 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4489
4468
|
day: "DAY",
|
|
4490
4469
|
week: "WEEK",
|
|
4491
4470
|
month: "MONTH",
|
|
4492
|
-
year: "
|
|
4471
|
+
year: "ON_DEMAND"
|
|
4493
4472
|
};
|
|
4473
|
+
const recurrenceCycle = intervalMap?.[data.billing_interval] ?? "ON_DEMAND";
|
|
4494
4474
|
const currentPeriodEnd = (() => {
|
|
4495
|
-
if (
|
|
4475
|
+
if (recurrenceCycle === "DAY") {
|
|
4496
4476
|
return new Date(Date.now() + 1 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4497
4477
|
}
|
|
4498
|
-
if (
|
|
4478
|
+
if (recurrenceCycle === "WEEK") {
|
|
4499
4479
|
return new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4500
4480
|
}
|
|
4501
|
-
if (
|
|
4481
|
+
if (recurrenceCycle === "MONTH") {
|
|
4502
4482
|
return new Date(Date.now() + 30 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4503
4483
|
}
|
|
4504
|
-
|
|
4484
|
+
if (recurrenceCycle === "ON_DEMAND") {
|
|
4485
|
+
throw new core.ConfigurationError(
|
|
4486
|
+
"ON_DEMAND subscriptions is not yet implemented by PayKit, please open an issue at https://github.com/usepaykit/paykit-sdk/issues",
|
|
4487
|
+
{
|
|
4488
|
+
provider: this.providerName
|
|
4489
|
+
}
|
|
4490
|
+
);
|
|
4491
|
+
}
|
|
4492
|
+
throw new Error("Invalid billing interval: " + data.billing_interval);
|
|
4505
4493
|
})();
|
|
4506
|
-
const recurrenceCycle = intervalMap[data.billing_interval] ?? "ON_DEMAND";
|
|
4507
4494
|
const goPaySubscriptionOptions = {
|
|
4508
4495
|
payer: {
|
|
4509
4496
|
allowed_payment_instruments: ["PAYMENT_CARD"],
|
|
@@ -4537,7 +4524,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4537
4524
|
"/payments/payment",
|
|
4538
4525
|
{
|
|
4539
4526
|
body: JSON.stringify(goPaySubscriptionOptions),
|
|
4540
|
-
headers: await this.
|
|
4527
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4541
4528
|
}
|
|
4542
4529
|
);
|
|
4543
4530
|
if (!response.ok) {
|
|
@@ -4566,7 +4553,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4566
4553
|
}
|
|
4567
4554
|
const response = await this._client.post(`/payments/payment/${id}/void-recurrence`, {
|
|
4568
4555
|
headers: {
|
|
4569
|
-
...await this.
|
|
4556
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4570
4557
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4571
4558
|
}
|
|
4572
4559
|
});
|
|
@@ -4583,7 +4570,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4583
4570
|
const response = await this._client.get(
|
|
4584
4571
|
`/payments/payment/${id}`,
|
|
4585
4572
|
{
|
|
4586
|
-
headers: await this.
|
|
4573
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4587
4574
|
}
|
|
4588
4575
|
);
|
|
4589
4576
|
if (!response.ok) {
|
|
@@ -4638,7 +4625,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4638
4625
|
"/payments/payment",
|
|
4639
4626
|
{
|
|
4640
4627
|
body: JSON.stringify(goPayRequest),
|
|
4641
|
-
headers: await this.
|
|
4628
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4642
4629
|
}
|
|
4643
4630
|
);
|
|
4644
4631
|
if (!response.ok) {
|
|
@@ -4653,7 +4640,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4653
4640
|
`/payments/payment/${id}`,
|
|
4654
4641
|
{
|
|
4655
4642
|
headers: {
|
|
4656
|
-
...await this.
|
|
4643
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4657
4644
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4658
4645
|
}
|
|
4659
4646
|
}
|
|
@@ -4674,9 +4661,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4674
4661
|
capturePayment = async (id, params) => {
|
|
4675
4662
|
const payment = await this._client.get(
|
|
4676
4663
|
`/payments/payment/${id}/capture`,
|
|
4677
|
-
{
|
|
4678
|
-
headers: await this.authController.getAuthHeaders()
|
|
4679
|
-
}
|
|
4664
|
+
{ headers: await this.tokenManager.getAuthHeaders() }
|
|
4680
4665
|
);
|
|
4681
4666
|
if (!payment.ok) {
|
|
4682
4667
|
throw new core.OperationFailedError("capturePayment", this.providerName, {
|
|
@@ -4701,19 +4686,16 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4701
4686
|
`/payments/payment/${id}/capture`,
|
|
4702
4687
|
{
|
|
4703
4688
|
body: JSON.stringify(captureBody),
|
|
4704
|
-
headers: await this.
|
|
4689
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4705
4690
|
}
|
|
4706
4691
|
);
|
|
4707
4692
|
return paykitPayment$InboundSchema(payment.value);
|
|
4708
4693
|
};
|
|
4709
4694
|
cancelPayment = async (id) => {
|
|
4710
|
-
|
|
4695
|
+
await this._client.post(
|
|
4711
4696
|
`/payments/payment/${id}/void-authorization`,
|
|
4712
|
-
{
|
|
4713
|
-
headers: await this.authController.getAuthHeaders()
|
|
4714
|
-
}
|
|
4697
|
+
{ headers: await this.tokenManager.getAuthHeaders() }
|
|
4715
4698
|
);
|
|
4716
|
-
console.dir({ response }, { depth: 100 });
|
|
4717
4699
|
const payment = await this.retrievePayment(id);
|
|
4718
4700
|
if (!payment) {
|
|
4719
4701
|
throw new core.OperationFailedError("cancelPayment", this.providerName, {
|
|
@@ -4749,7 +4731,7 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4749
4731
|
const response = await this._client.post(`/payments/payment/${data.payment_id}/refund`, {
|
|
4750
4732
|
body: new URLSearchParams({ amount: String(data.amount) }).toString(),
|
|
4751
4733
|
headers: {
|
|
4752
|
-
...await this.
|
|
4734
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4753
4735
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4754
4736
|
}
|
|
4755
4737
|
});
|
|
@@ -4770,7 +4752,6 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4770
4752
|
const { fullUrl } = payload;
|
|
4771
4753
|
const paymentId = new URL(fullUrl).searchParams.get("id");
|
|
4772
4754
|
const parentId = new URL(fullUrl).searchParams.get("parent_id");
|
|
4773
|
-
console.log({ paymentId, parentId });
|
|
4774
4755
|
if (!paymentId) {
|
|
4775
4756
|
throw new core.WebhookError("Payment ID is required", { provider: this.providerName });
|
|
4776
4757
|
}
|
|
@@ -4780,12 +4761,11 @@ var GoPayProvider = class extends core.AbstractPayKitProvider {
|
|
|
4780
4761
|
const [payment, error] = await core.tryCatchAsync(
|
|
4781
4762
|
this._client.get(`/payments/payment/${paymentId}`, {
|
|
4782
4763
|
headers: {
|
|
4783
|
-
...await this.
|
|
4764
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4784
4765
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4785
4766
|
}
|
|
4786
4767
|
})
|
|
4787
4768
|
);
|
|
4788
|
-
console.log({ payment });
|
|
4789
4769
|
if (error) {
|
|
4790
4770
|
throw new core.WebhookError("Failed to retrieve payment", {
|
|
4791
4771
|
provider: this.providerName
|
package/dist/gopay-provider.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { schema, AbstractPayKitProvider, HTTPClient, createCheckoutSchema, ValidationError, InvalidTypeError, validateRequiredKeys, PAYKIT_METADATA_KEY, OperationFailedError, ProviderNotSupportedError, createSubscriptionSchema,
|
|
1
|
+
import { schema, AbstractPayKitProvider, HTTPClient, OAuth2TokenManager, createCheckoutSchema, ValidationError, isEmailCustomer, InvalidTypeError, validateRequiredKeys, PAYKIT_METADATA_KEY, OperationFailedError, ProviderNotSupportedError, createSubscriptionSchema, ConfigurationError, createPaymentSchema, createRefundSchema, WebhookError, tryCatchAsync, paykitEvent$InboundSchema, omitInternalMetadata } from '@paykit-sdk/core';
|
|
2
2
|
import * as crypto from 'crypto';
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -4046,60 +4046,6 @@ var coerce = {
|
|
|
4046
4046
|
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4047
4047
|
};
|
|
4048
4048
|
var NEVER = INVALID;
|
|
4049
|
-
var AuthController = class {
|
|
4050
|
-
constructor(opts) {
|
|
4051
|
-
this.opts = opts;
|
|
4052
|
-
const debug = opts.debug ?? true;
|
|
4053
|
-
this._client = new HTTPClient({
|
|
4054
|
-
baseUrl: this.opts.baseUrl,
|
|
4055
|
-
headers: {},
|
|
4056
|
-
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
4057
|
-
});
|
|
4058
|
-
}
|
|
4059
|
-
_client;
|
|
4060
|
-
_accessToken = null;
|
|
4061
|
-
getAccessToken = async () => {
|
|
4062
|
-
if (this._accessToken) {
|
|
4063
|
-
const [token, , expiryStr] = this._accessToken.split("::paykit::");
|
|
4064
|
-
const expiry = parseInt(expiryStr || "0", 10);
|
|
4065
|
-
if (expiry > Date.now()) return token;
|
|
4066
|
-
}
|
|
4067
|
-
const credentials = Buffer.from(
|
|
4068
|
-
`${this.opts.clientId}:${this.opts.clientSecret}`
|
|
4069
|
-
).toString("base64");
|
|
4070
|
-
const body = new URLSearchParams({
|
|
4071
|
-
grant_type: "client_credentials",
|
|
4072
|
-
scope: "payment-all"
|
|
4073
|
-
}).toString();
|
|
4074
|
-
const response = await this._client.post("/oauth2/token", {
|
|
4075
|
-
headers: {
|
|
4076
|
-
Authorization: `Basic ${credentials}`,
|
|
4077
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
4078
|
-
Accept: "application/json"
|
|
4079
|
-
},
|
|
4080
|
-
body
|
|
4081
|
-
});
|
|
4082
|
-
if (!response.ok || !response.value?.access_token) {
|
|
4083
|
-
throw new OperationFailedError("getAccessToken", "gopay", {
|
|
4084
|
-
cause: new Error(
|
|
4085
|
-
`Failed to obtain GoPay access token: ${JSON.stringify(response.value || response.error)}`
|
|
4086
|
-
)
|
|
4087
|
-
});
|
|
4088
|
-
}
|
|
4089
|
-
const expiryTime = Date.now() + (response.value.expires_in - 300) * 1e3;
|
|
4090
|
-
const accessToken = `${response.value.access_token}::paykit::${expiryTime}`;
|
|
4091
|
-
this._accessToken = accessToken;
|
|
4092
|
-
return response.value.access_token;
|
|
4093
|
-
};
|
|
4094
|
-
getAuthHeaders = async () => {
|
|
4095
|
-
const token = await this.getAccessToken();
|
|
4096
|
-
return {
|
|
4097
|
-
Authorization: `Bearer ${token}`,
|
|
4098
|
-
"Content-Type": "application/json",
|
|
4099
|
-
Accept: "application/json"
|
|
4100
|
-
};
|
|
4101
|
-
};
|
|
4102
|
-
};
|
|
4103
4049
|
var decodeHtmlEntities = (str) => {
|
|
4104
4050
|
return str.replace(/"/g, '"').replace(/'/g, "'").replace(/"/g, '"').replace(/'/g, "'");
|
|
4105
4051
|
};
|
|
@@ -4306,16 +4252,36 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4306
4252
|
headers: {},
|
|
4307
4253
|
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
4308
4254
|
});
|
|
4309
|
-
this.
|
|
4255
|
+
this.tokenManager = new OAuth2TokenManager({
|
|
4256
|
+
client: this._client,
|
|
4257
|
+
provider: this.providerName,
|
|
4258
|
+
tokenEndpoint: "/oauth2/token",
|
|
4259
|
+
credentials: { username: opts.clientId, password: opts.clientSecret },
|
|
4260
|
+
responseAdapter: (response) => ({
|
|
4261
|
+
accessToken: response.access_token,
|
|
4262
|
+
expiresIn: response.expires_in
|
|
4263
|
+
}),
|
|
4264
|
+
expiryBuffer: 5 * 60,
|
|
4265
|
+
// 5 minutes
|
|
4266
|
+
requestBody: "grant_type=client_credentials&scope=payment-all",
|
|
4267
|
+
requestHeaders: {
|
|
4268
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
4269
|
+
Accept: "application/json"
|
|
4270
|
+
},
|
|
4271
|
+
authHeaders: {
|
|
4272
|
+
"Content-Type": "application/json",
|
|
4273
|
+
Accept: "application/json"
|
|
4274
|
+
}
|
|
4275
|
+
});
|
|
4310
4276
|
}
|
|
4311
4277
|
providerName = providerName;
|
|
4312
4278
|
_client;
|
|
4313
4279
|
baseUrl;
|
|
4314
|
-
|
|
4280
|
+
tokenManager;
|
|
4315
4281
|
createCheckout = async (params) => {
|
|
4316
4282
|
const { error, data } = createCheckoutSchema.safeParse(params);
|
|
4317
4283
|
if (error) throw ValidationError.fromZodError(error, "gopay", "createCheckout");
|
|
4318
|
-
if (
|
|
4284
|
+
if (!isEmailCustomer(data.customer)) {
|
|
4319
4285
|
throw new InvalidTypeError("customer", "object (customer) with email", "string", {
|
|
4320
4286
|
provider: this.providerName,
|
|
4321
4287
|
method: "createCheckout"
|
|
@@ -4375,7 +4341,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4375
4341
|
"/payments/payment",
|
|
4376
4342
|
{
|
|
4377
4343
|
body: JSON.stringify(goPayRequest),
|
|
4378
|
-
headers: await this.
|
|
4344
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4379
4345
|
}
|
|
4380
4346
|
);
|
|
4381
4347
|
if (!response.ok) {
|
|
@@ -4390,7 +4356,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4390
4356
|
`/payments/payment/${id}`,
|
|
4391
4357
|
{
|
|
4392
4358
|
headers: {
|
|
4393
|
-
...await this.
|
|
4359
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4394
4360
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4395
4361
|
}
|
|
4396
4362
|
}
|
|
@@ -4418,23 +4384,36 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4418
4384
|
});
|
|
4419
4385
|
};
|
|
4420
4386
|
createCustomer = async (params) => {
|
|
4387
|
+
if (this.cloudClient) {
|
|
4388
|
+
return this.cloudClient.customers.create(params);
|
|
4389
|
+
}
|
|
4421
4390
|
throw new ProviderNotSupportedError("createCustomer", "gopay", {
|
|
4422
|
-
reason: "Gopay doesn't support creating customers"
|
|
4391
|
+
reason: "Gopay doesn't support creating customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4423
4392
|
});
|
|
4424
4393
|
};
|
|
4425
4394
|
updateCustomer = async (id, params) => {
|
|
4395
|
+
if (this.cloudClient) {
|
|
4396
|
+
return this.cloudClient.customers.update(id, params);
|
|
4397
|
+
}
|
|
4426
4398
|
throw new ProviderNotSupportedError("updateCustomer", "gopay", {
|
|
4427
|
-
reason: "Gopay doesn't support updating customers"
|
|
4399
|
+
reason: "Gopay doesn't support updating customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4428
4400
|
});
|
|
4429
4401
|
};
|
|
4430
4402
|
deleteCustomer = async (id) => {
|
|
4403
|
+
if (this.cloudClient) {
|
|
4404
|
+
this.cloudClient.customers.delete(id);
|
|
4405
|
+
return null;
|
|
4406
|
+
}
|
|
4431
4407
|
throw new ProviderNotSupportedError("deleteCustomer", "gopay", {
|
|
4432
|
-
reason: "Gopay doesn't support deleting customers"
|
|
4408
|
+
reason: "Gopay doesn't support deleting customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4433
4409
|
});
|
|
4434
4410
|
};
|
|
4435
4411
|
retrieveCustomer = async (id) => {
|
|
4412
|
+
if (this.cloudClient) {
|
|
4413
|
+
return this.cloudClient.customers.retrieve(id);
|
|
4414
|
+
}
|
|
4436
4415
|
throw new ProviderNotSupportedError("retrieveCustomer", "gopay", {
|
|
4437
|
-
reason: "Gopay doesn't support retrieving customers"
|
|
4416
|
+
reason: "Gopay doesn't support retrieving customers, use the cloud API instead by setting `cloudApiKey` in the options"
|
|
4438
4417
|
});
|
|
4439
4418
|
};
|
|
4440
4419
|
createSubscription = async (params) => {
|
|
@@ -4452,9 +4431,9 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4452
4431
|
"The following fields must be present in the provider_metadata of createCheckout: {keys}"
|
|
4453
4432
|
);
|
|
4454
4433
|
if (this.opts.debug) {
|
|
4455
|
-
if (data.billing_interval
|
|
4434
|
+
if (data.billing_interval === "year") {
|
|
4456
4435
|
console.info(
|
|
4457
|
-
"GoPay does not support yearly subscriptions,
|
|
4436
|
+
"GoPay does not support yearly subscriptions, the available options are `day`, `week`, `month` and `ON_DEMAND` \n raise an issue at https://github.com/usepaykit/paykit-sdk/issues if you need this feature"
|
|
4458
4437
|
);
|
|
4459
4438
|
}
|
|
4460
4439
|
if (!data.provider_metadata?.description) {
|
|
@@ -4467,21 +4446,29 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4467
4446
|
day: "DAY",
|
|
4468
4447
|
week: "WEEK",
|
|
4469
4448
|
month: "MONTH",
|
|
4470
|
-
year: "
|
|
4449
|
+
year: "ON_DEMAND"
|
|
4471
4450
|
};
|
|
4451
|
+
const recurrenceCycle = intervalMap?.[data.billing_interval] ?? "ON_DEMAND";
|
|
4472
4452
|
const currentPeriodEnd = (() => {
|
|
4473
|
-
if (
|
|
4453
|
+
if (recurrenceCycle === "DAY") {
|
|
4474
4454
|
return new Date(Date.now() + 1 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4475
4455
|
}
|
|
4476
|
-
if (
|
|
4456
|
+
if (recurrenceCycle === "WEEK") {
|
|
4477
4457
|
return new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4478
4458
|
}
|
|
4479
|
-
if (
|
|
4459
|
+
if (recurrenceCycle === "MONTH") {
|
|
4480
4460
|
return new Date(Date.now() + 30 * 24 * 60 * 60 * 1e3).toISOString();
|
|
4481
4461
|
}
|
|
4482
|
-
|
|
4462
|
+
if (recurrenceCycle === "ON_DEMAND") {
|
|
4463
|
+
throw new ConfigurationError(
|
|
4464
|
+
"ON_DEMAND subscriptions is not yet implemented by PayKit, please open an issue at https://github.com/usepaykit/paykit-sdk/issues",
|
|
4465
|
+
{
|
|
4466
|
+
provider: this.providerName
|
|
4467
|
+
}
|
|
4468
|
+
);
|
|
4469
|
+
}
|
|
4470
|
+
throw new Error("Invalid billing interval: " + data.billing_interval);
|
|
4483
4471
|
})();
|
|
4484
|
-
const recurrenceCycle = intervalMap[data.billing_interval] ?? "ON_DEMAND";
|
|
4485
4472
|
const goPaySubscriptionOptions = {
|
|
4486
4473
|
payer: {
|
|
4487
4474
|
allowed_payment_instruments: ["PAYMENT_CARD"],
|
|
@@ -4515,7 +4502,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4515
4502
|
"/payments/payment",
|
|
4516
4503
|
{
|
|
4517
4504
|
body: JSON.stringify(goPaySubscriptionOptions),
|
|
4518
|
-
headers: await this.
|
|
4505
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4519
4506
|
}
|
|
4520
4507
|
);
|
|
4521
4508
|
if (!response.ok) {
|
|
@@ -4544,7 +4531,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4544
4531
|
}
|
|
4545
4532
|
const response = await this._client.post(`/payments/payment/${id}/void-recurrence`, {
|
|
4546
4533
|
headers: {
|
|
4547
|
-
...await this.
|
|
4534
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4548
4535
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4549
4536
|
}
|
|
4550
4537
|
});
|
|
@@ -4561,7 +4548,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4561
4548
|
const response = await this._client.get(
|
|
4562
4549
|
`/payments/payment/${id}`,
|
|
4563
4550
|
{
|
|
4564
|
-
headers: await this.
|
|
4551
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4565
4552
|
}
|
|
4566
4553
|
);
|
|
4567
4554
|
if (!response.ok) {
|
|
@@ -4616,7 +4603,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4616
4603
|
"/payments/payment",
|
|
4617
4604
|
{
|
|
4618
4605
|
body: JSON.stringify(goPayRequest),
|
|
4619
|
-
headers: await this.
|
|
4606
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4620
4607
|
}
|
|
4621
4608
|
);
|
|
4622
4609
|
if (!response.ok) {
|
|
@@ -4631,7 +4618,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4631
4618
|
`/payments/payment/${id}`,
|
|
4632
4619
|
{
|
|
4633
4620
|
headers: {
|
|
4634
|
-
...await this.
|
|
4621
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4635
4622
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4636
4623
|
}
|
|
4637
4624
|
}
|
|
@@ -4652,9 +4639,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4652
4639
|
capturePayment = async (id, params) => {
|
|
4653
4640
|
const payment = await this._client.get(
|
|
4654
4641
|
`/payments/payment/${id}/capture`,
|
|
4655
|
-
{
|
|
4656
|
-
headers: await this.authController.getAuthHeaders()
|
|
4657
|
-
}
|
|
4642
|
+
{ headers: await this.tokenManager.getAuthHeaders() }
|
|
4658
4643
|
);
|
|
4659
4644
|
if (!payment.ok) {
|
|
4660
4645
|
throw new OperationFailedError("capturePayment", this.providerName, {
|
|
@@ -4679,19 +4664,16 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4679
4664
|
`/payments/payment/${id}/capture`,
|
|
4680
4665
|
{
|
|
4681
4666
|
body: JSON.stringify(captureBody),
|
|
4682
|
-
headers: await this.
|
|
4667
|
+
headers: await this.tokenManager.getAuthHeaders()
|
|
4683
4668
|
}
|
|
4684
4669
|
);
|
|
4685
4670
|
return paykitPayment$InboundSchema(payment.value);
|
|
4686
4671
|
};
|
|
4687
4672
|
cancelPayment = async (id) => {
|
|
4688
|
-
|
|
4673
|
+
await this._client.post(
|
|
4689
4674
|
`/payments/payment/${id}/void-authorization`,
|
|
4690
|
-
{
|
|
4691
|
-
headers: await this.authController.getAuthHeaders()
|
|
4692
|
-
}
|
|
4675
|
+
{ headers: await this.tokenManager.getAuthHeaders() }
|
|
4693
4676
|
);
|
|
4694
|
-
console.dir({ response }, { depth: 100 });
|
|
4695
4677
|
const payment = await this.retrievePayment(id);
|
|
4696
4678
|
if (!payment) {
|
|
4697
4679
|
throw new OperationFailedError("cancelPayment", this.providerName, {
|
|
@@ -4727,7 +4709,7 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4727
4709
|
const response = await this._client.post(`/payments/payment/${data.payment_id}/refund`, {
|
|
4728
4710
|
body: new URLSearchParams({ amount: String(data.amount) }).toString(),
|
|
4729
4711
|
headers: {
|
|
4730
|
-
...await this.
|
|
4712
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4731
4713
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4732
4714
|
}
|
|
4733
4715
|
});
|
|
@@ -4748,7 +4730,6 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4748
4730
|
const { fullUrl } = payload;
|
|
4749
4731
|
const paymentId = new URL(fullUrl).searchParams.get("id");
|
|
4750
4732
|
const parentId = new URL(fullUrl).searchParams.get("parent_id");
|
|
4751
|
-
console.log({ paymentId, parentId });
|
|
4752
4733
|
if (!paymentId) {
|
|
4753
4734
|
throw new WebhookError("Payment ID is required", { provider: this.providerName });
|
|
4754
4735
|
}
|
|
@@ -4758,12 +4739,11 @@ var GoPayProvider = class extends AbstractPayKitProvider {
|
|
|
4758
4739
|
const [payment, error] = await tryCatchAsync(
|
|
4759
4740
|
this._client.get(`/payments/payment/${paymentId}`, {
|
|
4760
4741
|
headers: {
|
|
4761
|
-
...await this.
|
|
4742
|
+
...await this.tokenManager.getAuthHeaders(),
|
|
4762
4743
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
4763
4744
|
}
|
|
4764
4745
|
})
|
|
4765
4746
|
);
|
|
4766
|
-
console.log({ payment });
|
|
4767
4747
|
if (error) {
|
|
4768
4748
|
throw new WebhookError("Failed to retrieve payment", {
|
|
4769
4749
|
provider: this.providerName
|