@gopaycz/gopay-js-sdk 1.6.6
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/CHANGELOG.md +441 -0
- package/README.md +633 -0
- package/dist/index.cjs +1166 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +303 -0
- package/dist/index.d.ts +303 -0
- package/dist/index.js +1135 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
- package/src/config.ts +4 -0
- package/src/env.d.ts +1 -0
- package/src/errors.ts +6 -0
- package/src/gopay-sdk.ts +34 -0
- package/src/index.ts +12 -0
- package/src/modules/auth/auth.module.ts +105 -0
- package/src/modules/cards/cards.module.ts +55 -0
- package/src/modules/links/links.module.ts +51 -0
- package/src/modules/payments/payments.module.ts +244 -0
- package/src/modules/recurrences/recurrences.module.ts +100 -0
- package/src/modules/refunds/refunds.module.ts +55 -0
- package/src/types/generated.ts +1 -0
- package/src/types/index.ts +10 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertHttpsOrigin,
|
|
3
|
+
awaitCharge,
|
|
4
|
+
type AwaitChargeOptions as CoreAwaitChargeOptions,
|
|
5
|
+
type GoPaySDKError,
|
|
6
|
+
type HttpClient,
|
|
7
|
+
requireNonEmptyString,
|
|
8
|
+
} from '@gopay-internal/core';
|
|
9
|
+
import type { components } from '../../types/generated.js';
|
|
10
|
+
|
|
11
|
+
type PaymentCreateRequest = components['schemas']['Payment-Create-Request'];
|
|
12
|
+
type PaymentDetails = components['schemas']['Payment-Details'];
|
|
13
|
+
type PaymentChargeRequest = components['schemas']['Payment-Charge-Input'];
|
|
14
|
+
type PaymentChargeResponse = components['schemas']['Payment-Charge-Response'];
|
|
15
|
+
type PaymentChargeStatusResponse =
|
|
16
|
+
components['schemas']['Payment-Charge-Status-Response'];
|
|
17
|
+
|
|
18
|
+
/** Options for {@link awaitChargeState}. */
|
|
19
|
+
export type AwaitChargeOptions =
|
|
20
|
+
CoreAwaitChargeOptions<PaymentChargeStatusResponse>;
|
|
21
|
+
type GooglePayInfoResponse =
|
|
22
|
+
components['responses']['Google-Pay-Info-Response']['content']['application/json'];
|
|
23
|
+
type ApplePayInfoResponse =
|
|
24
|
+
components['responses']['Apple-Pay-Info-Response']['content']['application/json'];
|
|
25
|
+
type ValidateMerchantResponse =
|
|
26
|
+
components['schemas']['Validate-Merchant-Response'];
|
|
27
|
+
type QRPaymentDetails = components['schemas']['QR-Payment-Details'];
|
|
28
|
+
|
|
29
|
+
export function createPaymentsApi(client: HttpClient) {
|
|
30
|
+
return {
|
|
31
|
+
/**
|
|
32
|
+
* Retrieve the current status of an existing payment.
|
|
33
|
+
*
|
|
34
|
+
* GET /payments/{payment_id}
|
|
35
|
+
*
|
|
36
|
+
* @param paymentId - Payment session ID returned by {@link createPayment}
|
|
37
|
+
*/
|
|
38
|
+
async getPaymentStatus(paymentId: string): Promise<PaymentDetails> {
|
|
39
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
40
|
+
return client.get<PaymentDetails>(`/payments/${pid}`);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a new payment session.
|
|
45
|
+
*
|
|
46
|
+
* POST /eshops/{goid}/payments
|
|
47
|
+
*
|
|
48
|
+
* The response includes a `gw_url` field — **ignore it**. It exists only for
|
|
49
|
+
* backward compatibility with pre-SDK redirect-based integrations. This SDK's
|
|
50
|
+
* flow is always: create → charge (via card token, Apple Pay, or Google Pay).
|
|
51
|
+
*
|
|
52
|
+
* @param goid - Merchant's GoPay ID (eshop identifier)
|
|
53
|
+
* @param params - Payment creation parameters
|
|
54
|
+
*/
|
|
55
|
+
async createPayment(
|
|
56
|
+
goid: string,
|
|
57
|
+
params: PaymentCreateRequest,
|
|
58
|
+
): Promise<PaymentDetails> {
|
|
59
|
+
return client.post<PaymentDetails>(
|
|
60
|
+
`/eshops/${goid}/payments`,
|
|
61
|
+
params,
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Charge a payment using a payment instrument.
|
|
67
|
+
*
|
|
68
|
+
* POST /payments/{payment_id}/charge
|
|
69
|
+
*
|
|
70
|
+
* @param paymentId - Payment session ID returned by {@link createPayment}
|
|
71
|
+
* @param params - Charge parameters including payment instrument details
|
|
72
|
+
*/
|
|
73
|
+
async chargePayment(
|
|
74
|
+
paymentId: string,
|
|
75
|
+
params: PaymentChargeRequest,
|
|
76
|
+
): Promise<PaymentChargeResponse> {
|
|
77
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
78
|
+
return client.post<PaymentChargeResponse>(
|
|
79
|
+
`/payments/${pid}/charge`,
|
|
80
|
+
params,
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Retrieve the current state of a payment charge.
|
|
86
|
+
*
|
|
87
|
+
* GET /payments/{payment_id}/charge
|
|
88
|
+
*
|
|
89
|
+
* @param paymentId - Payment session ID returned by {@link createPayment}
|
|
90
|
+
*/
|
|
91
|
+
async getChargeState(
|
|
92
|
+
paymentId: string,
|
|
93
|
+
): Promise<PaymentChargeStatusResponse> {
|
|
94
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
95
|
+
return client.get<PaymentChargeStatusResponse>(
|
|
96
|
+
`/payments/${pid}/charge`,
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Retrieve Google Pay configuration for this payment.
|
|
102
|
+
*
|
|
103
|
+
* GET /payments/{payment_id}/google-pay/info
|
|
104
|
+
*
|
|
105
|
+
* @param paymentId - Payment session ID
|
|
106
|
+
*/
|
|
107
|
+
async getGooglePayInfo(
|
|
108
|
+
paymentId: string,
|
|
109
|
+
): Promise<GooglePayInfoResponse> {
|
|
110
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
111
|
+
return client.get<GooglePayInfoResponse>(
|
|
112
|
+
`/payments/${pid}/google-pay/info`,
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Retrieve Apple Pay configuration for this payment.
|
|
118
|
+
*
|
|
119
|
+
* GET /payments/{payment_id}/apple-pay/info
|
|
120
|
+
*
|
|
121
|
+
* @param paymentId - Payment session ID
|
|
122
|
+
*/
|
|
123
|
+
async getApplePayInfo(
|
|
124
|
+
paymentId: string,
|
|
125
|
+
): Promise<ApplePayInfoResponse> {
|
|
126
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
127
|
+
return client.get<ApplePayInfoResponse>(
|
|
128
|
+
`/payments/${pid}/apple-pay/info`,
|
|
129
|
+
);
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Wire merchant validation onto an ApplePaySession and begin it.
|
|
134
|
+
* Handles the onvalidatemerchant callback via POST /payments/{payment_id}/apple-pay/validate.
|
|
135
|
+
*
|
|
136
|
+
* @param paymentId - Payment session ID
|
|
137
|
+
* @param session - ApplePaySession instance
|
|
138
|
+
* @param origin - Merchant origin (https:); defaults to current page origin
|
|
139
|
+
* @param callbacks - Optional lifecycle callbacks
|
|
140
|
+
*/
|
|
141
|
+
startApplePaySession(
|
|
142
|
+
paymentId: string,
|
|
143
|
+
session: {
|
|
144
|
+
onvalidatemerchant: ((event: unknown) => void) | null;
|
|
145
|
+
oncancel: ((event: unknown) => void) | null;
|
|
146
|
+
completeMerchantValidation(merchantSession: unknown): void;
|
|
147
|
+
abort(): void;
|
|
148
|
+
begin(): void;
|
|
149
|
+
},
|
|
150
|
+
origin: string = globalThis.location?.origin ?? '',
|
|
151
|
+
callbacks?: { oncancel?: (event: unknown) => void },
|
|
152
|
+
): void {
|
|
153
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
154
|
+
if (origin) {
|
|
155
|
+
try {
|
|
156
|
+
assertHttpsOrigin(
|
|
157
|
+
origin,
|
|
158
|
+
'[GoPaySDK] startApplePaySession',
|
|
159
|
+
);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
throw client.emitError(e as GoPaySDKError);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
session.onvalidatemerchant = (event: unknown) => {
|
|
165
|
+
const validationURL =
|
|
166
|
+
event != null &&
|
|
167
|
+
typeof event === 'object' &&
|
|
168
|
+
'validationURL' in event &&
|
|
169
|
+
typeof (event as Record<string, unknown>).validationURL ===
|
|
170
|
+
'string'
|
|
171
|
+
? ((event as Record<string, unknown>)
|
|
172
|
+
.validationURL as string)
|
|
173
|
+
: undefined;
|
|
174
|
+
const headers: Record<string, string> = origin
|
|
175
|
+
? { Origin: origin }
|
|
176
|
+
: {};
|
|
177
|
+
const body = validationURL
|
|
178
|
+
? { validationUrl: validationURL }
|
|
179
|
+
: undefined;
|
|
180
|
+
client
|
|
181
|
+
.post<ValidateMerchantResponse>(
|
|
182
|
+
`/payments/${pid}/apple-pay/validate`,
|
|
183
|
+
body,
|
|
184
|
+
{ headers },
|
|
185
|
+
)
|
|
186
|
+
.then((merchantSession) =>
|
|
187
|
+
session.completeMerchantValidation(merchantSession),
|
|
188
|
+
)
|
|
189
|
+
.catch(() => session.abort());
|
|
190
|
+
};
|
|
191
|
+
session.oncancel = (event) => {
|
|
192
|
+
callbacks?.oncancel?.(event);
|
|
193
|
+
};
|
|
194
|
+
session.begin();
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Retrieve QR payment info for this payment.
|
|
199
|
+
* Returns recipient details and base64-encoded QR code image(s).
|
|
200
|
+
*
|
|
201
|
+
* GET /payments/{payment_id}/qr-payment/info
|
|
202
|
+
*
|
|
203
|
+
* @param paymentId - Payment session ID
|
|
204
|
+
* @param format - Image format of the QR code: 'png' (default) or 'svg'
|
|
205
|
+
*/
|
|
206
|
+
async getQRPaymentInfo(
|
|
207
|
+
paymentId: string,
|
|
208
|
+
format?: 'png' | 'svg',
|
|
209
|
+
): Promise<QRPaymentDetails> {
|
|
210
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
211
|
+
const path = format
|
|
212
|
+
? `/payments/${pid}/qr-payment/info?format=${format}`
|
|
213
|
+
: `/payments/${pid}/qr-payment/info`;
|
|
214
|
+
return client.get<QRPaymentDetails>(path);
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Poll the charge state until a terminal outcome.
|
|
219
|
+
*
|
|
220
|
+
* Resolves on `SUCCEEDED`. Rejects with `CHARGE_FAILED` on `FAILED`,
|
|
221
|
+
* or `CHARGE_TIMEOUT` if the charge does not leave `REQUESTED`/
|
|
222
|
+
* `PROCESSING` within `initialTimeoutMs` (default 30 s).
|
|
223
|
+
*
|
|
224
|
+
* Use `options.onActionRequired` to handle 3DS redirects
|
|
225
|
+
* (e.g. redirect the customer or open a popup).
|
|
226
|
+
*
|
|
227
|
+
* @param paymentId - Payment session ID
|
|
228
|
+
* @param options - Polling configuration and callbacks
|
|
229
|
+
*/
|
|
230
|
+
awaitChargeState(
|
|
231
|
+
paymentId: string,
|
|
232
|
+
options?: AwaitChargeOptions,
|
|
233
|
+
): Promise<PaymentChargeStatusResponse> {
|
|
234
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
235
|
+
return awaitCharge(
|
|
236
|
+
() =>
|
|
237
|
+
client.get<PaymentChargeStatusResponse>(
|
|
238
|
+
`/payments/${pid}/charge`,
|
|
239
|
+
),
|
|
240
|
+
options,
|
|
241
|
+
);
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { type HttpClient, requireNonEmptyString } from '@gopay-internal/core';
|
|
2
|
+
|
|
3
|
+
import type { components } from '../../types/generated.js';
|
|
4
|
+
|
|
5
|
+
type RecurrenceCreateRequest =
|
|
6
|
+
components['schemas']['Recurrence-Create-Request'];
|
|
7
|
+
type RecurrenceDetails = components['schemas']['Recurrence-Details'];
|
|
8
|
+
type RecurrenceNextBody = components['schemas']['Payment-Instance-Override'];
|
|
9
|
+
type PaymentDetails = components['schemas']['Payment-Details'];
|
|
10
|
+
|
|
11
|
+
export function createRecurrencesApi(client: HttpClient) {
|
|
12
|
+
return {
|
|
13
|
+
/**
|
|
14
|
+
* Create a recurrence.
|
|
15
|
+
* Requires the `payment:write` OAuth2 scope.
|
|
16
|
+
*
|
|
17
|
+
* POST /eshops/{goid}/recurrences
|
|
18
|
+
*
|
|
19
|
+
* @param goid - Merchant's GoPay ID (eshop identifier)
|
|
20
|
+
* @param params - Recurrence creation parameters including type, schedule, and payment data
|
|
21
|
+
*/
|
|
22
|
+
async createRecurrence(
|
|
23
|
+
goid: string,
|
|
24
|
+
params: RecurrenceCreateRequest,
|
|
25
|
+
): Promise<RecurrenceDetails> {
|
|
26
|
+
return client.post<RecurrenceDetails>(
|
|
27
|
+
`/eshops/${goid}/recurrences`,
|
|
28
|
+
params,
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Recurrence status.
|
|
34
|
+
* Requires the `payment:read` OAuth2 scope.
|
|
35
|
+
*
|
|
36
|
+
* GET /recurrences/{rec_id}
|
|
37
|
+
*
|
|
38
|
+
* @param recId - Recurrence ID returned by {@link createRecurrence}
|
|
39
|
+
*/
|
|
40
|
+
async recurrenceStatus(recId: string): Promise<RecurrenceDetails> {
|
|
41
|
+
const rid = requireNonEmptyString(recId, 'recId');
|
|
42
|
+
return client.get<RecurrenceDetails>(`/recurrences/${rid}`);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Stop a recurrence.
|
|
47
|
+
* Requires the `payment:write` OAuth2 scope.
|
|
48
|
+
*
|
|
49
|
+
* DELETE /recurrences/{rec_id}
|
|
50
|
+
*
|
|
51
|
+
* @param recId - Recurrence ID returned by {@link createRecurrence}
|
|
52
|
+
*/
|
|
53
|
+
async stopRecurrence(recId: string): Promise<void> {
|
|
54
|
+
const rid = requireNonEmptyString(recId, 'recId');
|
|
55
|
+
return client.delete(`/recurrences/${rid}`);
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Start a recurrence.
|
|
60
|
+
* Triggers the first charge of a recurrence that is in the `NEW` state.
|
|
61
|
+
* Requires the `payment:write` OAuth2 scope.
|
|
62
|
+
*
|
|
63
|
+
* POST /recurrences/{rec_id}/start
|
|
64
|
+
*
|
|
65
|
+
* @param recId - Recurrence ID returned by {@link createRecurrence}
|
|
66
|
+
* @param params - Optional payment overrides (amount, order_number, callback, etc.)
|
|
67
|
+
*/
|
|
68
|
+
async startRecurrence(
|
|
69
|
+
recId: string,
|
|
70
|
+
params?: RecurrenceNextBody,
|
|
71
|
+
): Promise<PaymentDetails> {
|
|
72
|
+
const rid = requireNonEmptyString(recId, 'recId');
|
|
73
|
+
return client.post<PaymentDetails>(
|
|
74
|
+
`/recurrences/${rid}/start`,
|
|
75
|
+
params,
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Create a next payment for a recurrence.
|
|
81
|
+
* Charges the next instalment for a recurrence that is already `STARTED`.
|
|
82
|
+
* Requires the `payment:write` OAuth2 scope.
|
|
83
|
+
*
|
|
84
|
+
* POST /recurrences/{rec_id}/next
|
|
85
|
+
*
|
|
86
|
+
* @param recId - Recurrence ID returned by {@link createRecurrence}
|
|
87
|
+
* @param params - Optional payment overrides (amount, order_number, callback, etc.)
|
|
88
|
+
*/
|
|
89
|
+
async recurrenceNext(
|
|
90
|
+
recId: string,
|
|
91
|
+
params?: RecurrenceNextBody,
|
|
92
|
+
): Promise<PaymentDetails> {
|
|
93
|
+
const rid = requireNonEmptyString(recId, 'recId');
|
|
94
|
+
return client.post<PaymentDetails>(
|
|
95
|
+
`/recurrences/${rid}/next`,
|
|
96
|
+
params,
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type HttpClient, requireNonEmptyString } from '@gopay-internal/core';
|
|
2
|
+
import type { components } from '../../types/generated.js';
|
|
3
|
+
|
|
4
|
+
type RefundCreateRequest = components['schemas']['Refund-Create-Request'];
|
|
5
|
+
type RefundDetails = components['schemas']['Refund-Details'];
|
|
6
|
+
|
|
7
|
+
export function createRefundsApi(client: HttpClient) {
|
|
8
|
+
return {
|
|
9
|
+
/**
|
|
10
|
+
* Refund a payment (fully or partially).
|
|
11
|
+
* Requires the `payment:write` OAuth2 scope.
|
|
12
|
+
*
|
|
13
|
+
* POST /payments/{payment_id}/refunds
|
|
14
|
+
*
|
|
15
|
+
* @param paymentId - Payment session ID returned by {@link createPayment}
|
|
16
|
+
* @param params - Refund parameters, including the amount in cents
|
|
17
|
+
*/
|
|
18
|
+
async refundPayment(
|
|
19
|
+
paymentId: string,
|
|
20
|
+
params: RefundCreateRequest,
|
|
21
|
+
): Promise<RefundDetails> {
|
|
22
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
23
|
+
return client.post<RefundDetails>(
|
|
24
|
+
`/payments/${pid}/refunds`,
|
|
25
|
+
params,
|
|
26
|
+
);
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* List all refunds for a payment.
|
|
31
|
+
* Requires the `payment:read` OAuth2 scope.
|
|
32
|
+
*
|
|
33
|
+
* GET /payments/{payment_id}/refunds
|
|
34
|
+
*
|
|
35
|
+
* @param paymentId - Payment session ID returned by {@link createPayment}
|
|
36
|
+
*/
|
|
37
|
+
async listRefunds(paymentId: string): Promise<RefundDetails[]> {
|
|
38
|
+
const pid = requireNonEmptyString(paymentId, 'paymentId');
|
|
39
|
+
return client.get<RefundDetails[]>(`/payments/${pid}/refunds`);
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve details of a single refund.
|
|
44
|
+
* Requires the `payment:read` OAuth2 scope.
|
|
45
|
+
*
|
|
46
|
+
* GET /refunds/{refund_id}
|
|
47
|
+
*
|
|
48
|
+
* @param refundId - Refund ID returned by {@link refundPayment}
|
|
49
|
+
*/
|
|
50
|
+
async getRefund(refundId: string): Promise<RefundDetails> {
|
|
51
|
+
const rid = requireNonEmptyString(refundId, 'refundId');
|
|
52
|
+
return client.get<RefundDetails>(`/refunds/${rid}`);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from '@gopay-internal/core/types/generated.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { BrowserData, GoPayEnvironment } from '@gopay-internal/core';
|
|
2
|
+
|
|
3
|
+
export interface ClientCredentialsRequest {
|
|
4
|
+
grant_type: 'client_credentials';
|
|
5
|
+
client_id: string;
|
|
6
|
+
client_secret: string;
|
|
7
|
+
scope: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type AuthenticateRequest = ClientCredentialsRequest;
|
package/src/version.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SDK_VERSION: string = __GOPAY_SDK_VERSION__;
|