This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
p2p.compat-bill-payments.ts
201 lines (188 loc) · 5.16 KB
/
p2p.compat-bill-payments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* istanbul ignore file */
import { formatOffsetDate } from "../shared/time";
import { generateUUID } from "../shared/uuid";
import type { ReadonlyRecord } from "../shared/types";
import { P2p } from "./p2p";
import {
BillCreateParameters,
BillCurrency,
BillCurrencyAny,
BillFormParameters,
BillRefundStatusData,
BillStatusBody,
BillStatusData
} from "./p2p.types";
/**
*
* @deprecated Это класс для тех, кто мигрирует с `@qiwi/bill-payments-node-js-sdk`.
* Остальным рекомендуется использовать класс {@link P2p}
* @export
* @class _P2pCompatBillPayments
* @extends {P2p}
*/
export class _P2pCompatBillPayments extends P2p {
/**
* Creates an instance of _P2pCompatBillPayments.
* @param {string} secretKey
* @param {string} publicKey
* @memberof _P2pCompatBillPayments
*/
constructor(
public readonly secretKey: string,
public readonly publicKey: string = ""
) {
super({
http: _P2pCompatBillPayments.httpClientFactory(secretKey),
publicKey,
secretKey
});
}
/**
*
*
* @readonly
* @memberof _P2pCompatBillPaymentsUniversal
*/
get _key() {
return this.secretKey;
}
/**
*
*
* @readonly
* @memberof _P2pCompatBillPaymentsUniversal
*/
get key() {
return this.secretKey;
}
/**
* Normalize amount
*
* @param {(string | number)} amount The amount
* @return {string} string
* @memberof _P2pCompatBillPaymentsUniversal
*/
public _normalizeAmount(amount: string | number): string {
return this.bills["_normalizeAmount"](amount);
}
/**
* Checks notification data signature
*
* @param {string} signature The signature
* @param {BillStatusBody} body The notification body
* @param {string} [merchantSecret=this.secretKey] The merchant key for validating signature
* @return {*} boolean
* @memberof _P2pCompatBillPaymentsUniversal
*/
checkNotificationSignature(
signature: string,
body: BillStatusBody,
merchantSecret = this.secretKey
): boolean {
return this.bills.checkNotificationSignature(signature, body, merchantSecret);
}
/**
* Generate lifetime in format
*
* @param {number} [days=45] Days of lifetime
* @return {string} Lifetime in ISO
* @memberof _P2pCompatBillPaymentsUniversal
*/
getLifetimeByDay(days = 45): string {
return formatOffsetDate(days, "day");
}
/**
* Generate id
*
* @return {string} Return uuid v4
* @memberof _P2pCompatBillPaymentsUniversal
*/
generateId() {
return generateUUID();
}
/**
* Creating checkout link
*
* @param {*} parameters
* @return {string} string
* @memberof _P2pCompatBillPaymentsUniversal
*/
createPaymentForm(
parameters: Pick<BillFormParameters, "billId" | "amount" | "successUrl"> & {
publicKey?: string;
} & ReadonlyRecord<string, any>
): string {
return this.bills.createFormUrl(parameters);
}
/**
* Creating bill
*
* @param {string} billId
* @param {BillCreateParameters} parameters
* @return {*} Promise<BillStatusData>
* @memberof _P2pCompatBillPaymentsUniversal
*/
async createBill(
billId: string,
parameters: BillCreateParameters
): Promise<BillStatusData> {
return await this.bills.create({ ...parameters, billId });
}
/**
* Getting bill info
*
* @param {string} billId The bill identifier
* @return {Promise<BillStatusData>} Promise<BillStatusData>
* @memberof _P2pCompatBillPaymentsUniversal
*/
async getBillInfo(billId: string): Promise<BillStatusData> {
return await this.bills.getStatus(billId);
}
/**
* Cancelling unpaid bill
*
* @param {string} billId The bill identifier
* @return {Promise<BillStatusData>} Promise<BillStatusData>
* @memberof _P2pCompatBillPaymentsUniversal
*/
async cancelBill(billId: string): Promise<BillStatusData> {
return await this.bills.reject(billId);
}
/**
* Getting refund info
* Method is not available for individuals
*
* @param {(string | number)} billId The bill identifier
* @param {(string | number)} refundId The refund identifier
* @return {*} Promise<BillRefundStatusData>
* @memberof _P2pCompatBillPaymentsUniversal
*/
async getRefundInfo(
billId: string | number,
refundId: string | number
): Promise<BillRefundStatusData> {
return await this.bills.getRefundStatus(billId.toString(), refundId.toString());
}
/**
* Refund paid bill
* Method is not available for individuals
*
* @param {(string | number)} billId The bill identifier
* @param {(string | number)} refundId The refund identifier
* @param {number} [amount=0] The amount
* @param {BillCurrencyAny} [currency=BillCurrency.RUB] The currency
* @return {Promise<BillRefundStatusData>} Promise<BillRefundStatusData>
* @memberof _P2pCompatBillPaymentsUniversal
*/
async refund(
billId: string | number,
refundId: string | number,
amount = 0,
currency: BillCurrencyAny = BillCurrency.RUB
): Promise<BillRefundStatusData> {
return await this.bills.refund(billId.toString(), {
refundId: refundId.toString(),
amount: { value: amount, currency }
});
}
}