@paykit-sdk/gopay 1.0.5 → 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/README.md +105 -68
- package/dist/gopay-provider.d.mts +1 -5
- package/dist/gopay-provider.d.ts +1 -5
- package/dist/gopay-provider.js +290 -179
- package/dist/gopay-provider.mjs +291 -180
- package/dist/index.js +296 -182
- package/dist/index.mjs +297 -183
- package/dist/schema.d.mts +55 -51
- package/dist/schema.d.ts +55 -51
- 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 +8 -8
- package/dist/controllers/auth.d.mts +0 -19
- package/dist/controllers/auth.d.ts +0 -19
- package/dist/controllers/auth.js +0 -57
- package/dist/controllers/auth.mjs +0 -55
package/dist/utils/mapper.mjs
CHANGED
|
@@ -1,10 +1,53 @@
|
|
|
1
1
|
import { PAYKIT_METADATA_KEY, omitInternalMetadata } from '@paykit-sdk/core';
|
|
2
2
|
|
|
3
3
|
// src/utils/mapper.ts
|
|
4
|
+
var decodeHtmlEntities = (str) => {
|
|
5
|
+
return str.replace(/"/g, '"').replace(/'/g, "'").replace(/"/g, '"').replace(/'/g, "'");
|
|
6
|
+
};
|
|
4
7
|
var paykitPayment$InboundSchema = (data) => {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
const { item } = JSON.parse(
|
|
9
|
+
decodeHtmlEntities(
|
|
10
|
+
data.additional_params?.find((param) => param.name === PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
11
|
+
)
|
|
12
|
+
);
|
|
13
|
+
const metadata = omitInternalMetadata(
|
|
14
|
+
data.additional_params?.reduce(
|
|
15
|
+
(acc, param) => {
|
|
16
|
+
acc[param.name] = String(param.value);
|
|
17
|
+
return acc;
|
|
18
|
+
},
|
|
19
|
+
{}
|
|
20
|
+
) ?? {}
|
|
21
|
+
);
|
|
22
|
+
const statusMap = {
|
|
23
|
+
CREATED: "pending",
|
|
24
|
+
PAYMENT_METHOD_CHOSEN: "processing",
|
|
25
|
+
PAID: "succeeded",
|
|
26
|
+
AUTHORIZED: "requires_capture",
|
|
27
|
+
CANCELED: "canceled",
|
|
28
|
+
TIMEOUTED: "canceled",
|
|
29
|
+
REFUNDED: "canceled",
|
|
30
|
+
PARTIALLY_REFUNDED: "canceled"
|
|
31
|
+
};
|
|
32
|
+
const requiresAction = data.state === "CREATED" || data.state === "AUTHORIZED" ? true : false;
|
|
33
|
+
return {
|
|
34
|
+
id: data.id.toString(),
|
|
35
|
+
amount: data.amount,
|
|
36
|
+
currency: data.currency,
|
|
37
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
38
|
+
status: statusMap[data.state],
|
|
39
|
+
item_id: item,
|
|
40
|
+
metadata,
|
|
41
|
+
requires_action: requiresAction,
|
|
42
|
+
payment_url: data.gw_url ?? null
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
var paykitCheckout$InboundSchema = (data) => {
|
|
46
|
+
const { item, qty, type } = JSON.parse(
|
|
47
|
+
decodeHtmlEntities(
|
|
48
|
+
data.additional_params?.find((param) => param.name === PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
49
|
+
)
|
|
50
|
+
);
|
|
8
51
|
const metadata = omitInternalMetadata(
|
|
9
52
|
data.additional_params?.reduce(
|
|
10
53
|
(acc, param) => {
|
|
@@ -18,24 +61,24 @@ var paykitPayment$InboundSchema = (data) => {
|
|
|
18
61
|
id: data.id.toString(),
|
|
19
62
|
amount: data.amount,
|
|
20
63
|
currency: data.currency,
|
|
21
|
-
customer: data.payer
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
64
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
65
|
+
payment_url: data.gw_url ?? "",
|
|
66
|
+
metadata,
|
|
67
|
+
session_type: type,
|
|
68
|
+
products: [{ id: item, quantity: parseInt(qty) }]
|
|
25
69
|
};
|
|
26
70
|
};
|
|
27
71
|
var paykitInvoice$InboundSchema = (data, isSubscription) => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
).itemId;
|
|
72
|
+
const { item, qty } = JSON.parse(
|
|
73
|
+
decodeHtmlEntities(
|
|
74
|
+
data.additional_params?.find((param) => param.name === PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
75
|
+
)
|
|
76
|
+
);
|
|
34
77
|
const status = (() => {
|
|
35
78
|
if (data.state === "PAID") return "paid";
|
|
36
79
|
return "open";
|
|
37
80
|
})();
|
|
38
|
-
const paidAt = isSubscription ? new Date(data.recurrence?.
|
|
81
|
+
const paidAt = isSubscription ? new Date(data.recurrence?.recurrence_date_to ?? "") : /* @__PURE__ */ new Date();
|
|
39
82
|
const metadata = omitInternalMetadata(
|
|
40
83
|
data.additional_params?.reduce(
|
|
41
84
|
(acc, param) => {
|
|
@@ -49,20 +92,22 @@ var paykitInvoice$InboundSchema = (data, isSubscription) => {
|
|
|
49
92
|
id: data.id.toString(),
|
|
50
93
|
amount_paid: data.amount,
|
|
51
94
|
currency: data.currency,
|
|
52
|
-
customer: data.payer
|
|
95
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
53
96
|
status,
|
|
54
97
|
paid_at: paidAt.toISOString(),
|
|
55
98
|
metadata: metadata ?? {},
|
|
56
99
|
custom_fields: null,
|
|
57
100
|
subscription_id: isSubscription ? data.id.toString() : null,
|
|
58
101
|
billing_mode: isSubscription ? "recurring" : "one_time",
|
|
59
|
-
line_items: [{ id:
|
|
102
|
+
line_items: [{ id: item, quantity: parseInt(qty) }]
|
|
60
103
|
};
|
|
61
104
|
};
|
|
62
105
|
var paykitSubscription$InboundSchema = (data) => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
106
|
+
const { item } = JSON.parse(
|
|
107
|
+
decodeHtmlEntities(
|
|
108
|
+
data.additional_params?.find((param) => param.name === PAYKIT_METADATA_KEY)?.value ?? "{}"
|
|
109
|
+
)
|
|
110
|
+
);
|
|
66
111
|
const billingIntervalMap = {
|
|
67
112
|
DAY: "day",
|
|
68
113
|
WEEK: "week",
|
|
@@ -70,8 +115,28 @@ var paykitSubscription$InboundSchema = (data) => {
|
|
|
70
115
|
ON_DEMAND: "month"
|
|
71
116
|
};
|
|
72
117
|
const billingInterval = billingIntervalMap[data.recurrence?.recurrence_cycle ?? "ON_DEMAND"];
|
|
73
|
-
const
|
|
118
|
+
const recurrencePeriod = data.recurrence?.recurrence_period ?? 1;
|
|
119
|
+
const recurrenceCycle = data.recurrence?.recurrence_cycle;
|
|
74
120
|
const currentPeriodEnd = new Date(data.recurrence?.recurrence_date_to ?? "");
|
|
121
|
+
const currentPeriodStart = (() => {
|
|
122
|
+
if (!currentPeriodEnd) return /* @__PURE__ */ new Date();
|
|
123
|
+
const endDate = new Date(currentPeriodEnd);
|
|
124
|
+
const startDate = new Date(endDate);
|
|
125
|
+
switch (recurrenceCycle) {
|
|
126
|
+
case "DAY":
|
|
127
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod);
|
|
128
|
+
break;
|
|
129
|
+
case "WEEK":
|
|
130
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod * 7);
|
|
131
|
+
break;
|
|
132
|
+
case "MONTH":
|
|
133
|
+
startDate.setMonth(startDate.getMonth() - recurrencePeriod);
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
startDate.setDate(startDate.getDate() - recurrencePeriod);
|
|
137
|
+
}
|
|
138
|
+
return startDate;
|
|
139
|
+
})();
|
|
75
140
|
const metadata = omitInternalMetadata(
|
|
76
141
|
data.additional_params?.reduce(
|
|
77
142
|
(acc, param) => {
|
|
@@ -81,18 +146,32 @@ var paykitSubscription$InboundSchema = (data) => {
|
|
|
81
146
|
{}
|
|
82
147
|
) ?? {}
|
|
83
148
|
);
|
|
149
|
+
const requiresAction = data.state === "CREATED" ? true : false;
|
|
150
|
+
const status = (() => {
|
|
151
|
+
if (data.state === "CREATED") return "pending";
|
|
152
|
+
if (data.state === "PAID") return "active";
|
|
153
|
+
if (data.state === "CANCELED") return "canceled";
|
|
154
|
+
if (data.state === "TIMEOUTED") return "canceled";
|
|
155
|
+
if (data.state === "REFUNDED") return "canceled";
|
|
156
|
+
if (data.state === "PARTIALLY_REFUNDED") return "canceled";
|
|
157
|
+
if (data.state === "AUTHORIZED") return "active";
|
|
158
|
+
if (data.state === "PAYMENT_METHOD_CHOSEN") return "active";
|
|
159
|
+
return "pending";
|
|
160
|
+
})();
|
|
84
161
|
return {
|
|
85
162
|
id: data.id.toString(),
|
|
86
|
-
status
|
|
87
|
-
customer: data.payer
|
|
88
|
-
item_id:
|
|
163
|
+
status,
|
|
164
|
+
customer: data.payer?.contact?.email ? { email: data.payer.contact.email } : "",
|
|
165
|
+
item_id: item,
|
|
89
166
|
billing_interval: billingInterval,
|
|
90
167
|
currency: data.currency,
|
|
91
168
|
amount: data.amount,
|
|
92
|
-
metadata
|
|
169
|
+
metadata,
|
|
93
170
|
custom_fields: null,
|
|
94
171
|
current_period_start: currentPeriodStart,
|
|
95
|
-
current_period_end: currentPeriodEnd
|
|
172
|
+
current_period_end: currentPeriodEnd,
|
|
173
|
+
requires_action: requiresAction,
|
|
174
|
+
payment_url: requiresAction ? data.gw_url ?? null : null
|
|
96
175
|
};
|
|
97
176
|
};
|
|
98
177
|
var paykitRefund$InboundSchema = (data) => {
|
|
@@ -105,4 +184,4 @@ var paykitRefund$InboundSchema = (data) => {
|
|
|
105
184
|
};
|
|
106
185
|
};
|
|
107
186
|
|
|
108
|
-
export { paykitInvoice$InboundSchema, paykitPayment$InboundSchema, paykitRefund$InboundSchema, paykitSubscription$InboundSchema };
|
|
187
|
+
export { decodeHtmlEntities, paykitCheckout$InboundSchema, paykitInvoice$InboundSchema, paykitPayment$InboundSchema, paykitRefund$InboundSchema, paykitSubscription$InboundSchema };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/gopay",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "GoPay provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup"
|
|
20
|
+
},
|
|
18
21
|
"keywords": [
|
|
19
22
|
"paykit",
|
|
20
23
|
"gopay"
|
|
@@ -22,13 +25,13 @@
|
|
|
22
25
|
"author": "Emmanuel Odii",
|
|
23
26
|
"license": "ISC",
|
|
24
27
|
"peerDependencies": {
|
|
25
|
-
"@paykit-sdk/core": ">=1.1.
|
|
28
|
+
"@paykit-sdk/core": ">=1.1.102"
|
|
26
29
|
},
|
|
27
30
|
"devDependencies": {
|
|
31
|
+
"@paykit-sdk/core": "workspace:*",
|
|
28
32
|
"tsup": "^8.5.0",
|
|
29
33
|
"typescript": "^5.9.2",
|
|
30
|
-
"zod": "^3.24.2"
|
|
31
|
-
"@paykit-sdk/core": "1.1.97"
|
|
34
|
+
"zod": "^3.24.2"
|
|
32
35
|
},
|
|
33
36
|
"publishConfig": {
|
|
34
37
|
"access": "public"
|
|
@@ -39,8 +42,5 @@
|
|
|
39
42
|
},
|
|
40
43
|
"bugs": {
|
|
41
44
|
"url": "https://github.com/usepaykit/paykit-sdk/issues"
|
|
42
|
-
},
|
|
43
|
-
"scripts": {
|
|
44
|
-
"build": "tsup"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { GoPayOptions } from '../gopay-provider.mjs';
|
|
2
|
-
import '@paykit-sdk/core';
|
|
3
|
-
|
|
4
|
-
declare class AuthController {
|
|
5
|
-
private opts;
|
|
6
|
-
private _client;
|
|
7
|
-
private _accessToken;
|
|
8
|
-
constructor(opts: GoPayOptions & {
|
|
9
|
-
baseUrl: string;
|
|
10
|
-
});
|
|
11
|
-
getAccessToken: () => Promise<string>;
|
|
12
|
-
getAuthHeaders: () => Promise<{
|
|
13
|
-
Authorization: string;
|
|
14
|
-
'Content-Type': string;
|
|
15
|
-
Accept: string;
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { AuthController };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { GoPayOptions } from '../gopay-provider.js';
|
|
2
|
-
import '@paykit-sdk/core';
|
|
3
|
-
|
|
4
|
-
declare class AuthController {
|
|
5
|
-
private opts;
|
|
6
|
-
private _client;
|
|
7
|
-
private _accessToken;
|
|
8
|
-
constructor(opts: GoPayOptions & {
|
|
9
|
-
baseUrl: string;
|
|
10
|
-
});
|
|
11
|
-
getAccessToken: () => Promise<string>;
|
|
12
|
-
getAuthHeaders: () => Promise<{
|
|
13
|
-
Authorization: string;
|
|
14
|
-
'Content-Type': string;
|
|
15
|
-
Accept: string;
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { AuthController };
|
package/dist/controllers/auth.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var core = require('@paykit-sdk/core');
|
|
4
|
-
|
|
5
|
-
// src/controllers/auth.ts
|
|
6
|
-
var AuthController = class {
|
|
7
|
-
constructor(opts) {
|
|
8
|
-
this.opts = opts;
|
|
9
|
-
const debug = opts.debug ?? true;
|
|
10
|
-
this._client = new core.HTTPClient({
|
|
11
|
-
baseUrl: this.opts.baseUrl,
|
|
12
|
-
headers: {},
|
|
13
|
-
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
_client;
|
|
17
|
-
_accessToken = null;
|
|
18
|
-
getAccessToken = async () => {
|
|
19
|
-
if (this._accessToken) {
|
|
20
|
-
const [token, , expiryStr] = this._accessToken.split("::paykit::");
|
|
21
|
-
const expiry = parseInt(expiryStr || "0", 10);
|
|
22
|
-
if (expiry > Date.now()) return token;
|
|
23
|
-
}
|
|
24
|
-
const credentials = Buffer.from(
|
|
25
|
-
`${this.opts.clientId}:${this.opts.clientSecret}`
|
|
26
|
-
).toString("base64");
|
|
27
|
-
const response = await this._client.post("/oauth2/token", {
|
|
28
|
-
headers: {
|
|
29
|
-
Authorization: `Basic ${credentials}`,
|
|
30
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
31
|
-
},
|
|
32
|
-
body: new URLSearchParams({
|
|
33
|
-
grant_type: "client_credentials",
|
|
34
|
-
scope: "payment-all"
|
|
35
|
-
}).toString()
|
|
36
|
-
});
|
|
37
|
-
if (!response.ok || !response.value.access_token) {
|
|
38
|
-
throw new core.OperationFailedError("getAccessToken", "gopay", {
|
|
39
|
-
cause: new Error("Failed to obtain GoPay access token")
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
const expiryTime = Date.now() + (response.value.expires_in - 300) * 1e3;
|
|
43
|
-
const accessToken = `${response.value.access_token}::paykit::${expiryTime}`;
|
|
44
|
-
this._accessToken = accessToken;
|
|
45
|
-
return accessToken;
|
|
46
|
-
};
|
|
47
|
-
getAuthHeaders = async () => {
|
|
48
|
-
const token = await this.getAccessToken();
|
|
49
|
-
return {
|
|
50
|
-
Authorization: `Bearer ${token}`,
|
|
51
|
-
"Content-Type": "application/json",
|
|
52
|
-
Accept: "application/json"
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
exports.AuthController = AuthController;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { HTTPClient, OperationFailedError } from '@paykit-sdk/core';
|
|
2
|
-
|
|
3
|
-
// src/controllers/auth.ts
|
|
4
|
-
var AuthController = class {
|
|
5
|
-
constructor(opts) {
|
|
6
|
-
this.opts = opts;
|
|
7
|
-
const debug = opts.debug ?? true;
|
|
8
|
-
this._client = new HTTPClient({
|
|
9
|
-
baseUrl: this.opts.baseUrl,
|
|
10
|
-
headers: {},
|
|
11
|
-
retryOptions: { max: 3, baseDelay: 1e3, debug }
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
_client;
|
|
15
|
-
_accessToken = null;
|
|
16
|
-
getAccessToken = async () => {
|
|
17
|
-
if (this._accessToken) {
|
|
18
|
-
const [token, , expiryStr] = this._accessToken.split("::paykit::");
|
|
19
|
-
const expiry = parseInt(expiryStr || "0", 10);
|
|
20
|
-
if (expiry > Date.now()) return token;
|
|
21
|
-
}
|
|
22
|
-
const credentials = Buffer.from(
|
|
23
|
-
`${this.opts.clientId}:${this.opts.clientSecret}`
|
|
24
|
-
).toString("base64");
|
|
25
|
-
const response = await this._client.post("/oauth2/token", {
|
|
26
|
-
headers: {
|
|
27
|
-
Authorization: `Basic ${credentials}`,
|
|
28
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
29
|
-
},
|
|
30
|
-
body: new URLSearchParams({
|
|
31
|
-
grant_type: "client_credentials",
|
|
32
|
-
scope: "payment-all"
|
|
33
|
-
}).toString()
|
|
34
|
-
});
|
|
35
|
-
if (!response.ok || !response.value.access_token) {
|
|
36
|
-
throw new OperationFailedError("getAccessToken", "gopay", {
|
|
37
|
-
cause: new Error("Failed to obtain GoPay access token")
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
const expiryTime = Date.now() + (response.value.expires_in - 300) * 1e3;
|
|
41
|
-
const accessToken = `${response.value.access_token}::paykit::${expiryTime}`;
|
|
42
|
-
this._accessToken = accessToken;
|
|
43
|
-
return accessToken;
|
|
44
|
-
};
|
|
45
|
-
getAuthHeaders = async () => {
|
|
46
|
-
const token = await this.getAccessToken();
|
|
47
|
-
return {
|
|
48
|
-
Authorization: `Bearer ${token}`,
|
|
49
|
-
"Content-Type": "application/json",
|
|
50
|
-
Accept: "application/json"
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export { AuthController };
|