@yosle/tropipayjs 0.2.3 → 0.3.0

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.
Files changed (47) hide show
  1. package/README.md +69 -9
  2. package/index.d.ts +358 -76
  3. package/index.js +421 -342
  4. package/index.js.map +1 -0
  5. package/package.json +10 -2
  6. package/.nvmrc +0 -1
  7. package/.vscode/launch.json +0 -18
  8. package/SECURITY.md +0 -43
  9. package/api/TropipayAPI.d.ts +0 -133
  10. package/config/TropipayConfig.d.ts +0 -2
  11. package/constants/TropipayConstants.d.ts +0 -2
  12. package/depositAccount/depositAccounts.d.ts +0 -36
  13. package/docs/readme.md +0 -3
  14. package/globals.d.ts +0 -75
  15. package/hooks/TropipayHooks.d.ts +0 -38
  16. package/interfaces/countries/index.d.ts +0 -1
  17. package/interfaces/depositaccounts/index.d.ts +0 -68
  18. package/interfaces/index.d.ts +0 -176
  19. package/interfaces/mediation/index.d.ts +0 -22
  20. package/interfaces/paymentlink/index.d.ts +0 -87
  21. package/jest.config.js +0 -5
  22. package/mediationPaymentCard/MediationPaymentCard.d.ts +0 -17
  23. package/paymentcard/PaymentCard.d.ts +0 -37
  24. package/paymentcard/paymentcard.spec.d.ts +0 -1
  25. package/rollup.config.js +0 -23
  26. package/src/api/TropipayAPI.ts +0 -459
  27. package/src/config/TropipayConfig.ts +0 -2
  28. package/src/constants/TropipayConstants.ts +0 -2
  29. package/src/depositAccount/depositAccounts.ts +0 -139
  30. package/src/hooks/TropipayHooks.ts +0 -154
  31. package/src/index.ts +0 -29
  32. package/src/interfaces/depositaccounts/index.ts +0 -70
  33. package/src/interfaces/index.js +0 -2
  34. package/src/interfaces/index.ts +0 -212
  35. package/src/interfaces/mediation/index.ts +0 -23
  36. package/src/interfaces/paymentlink/index.d.ts +0 -36
  37. package/src/interfaces/paymentlink/index.js +0 -3
  38. package/src/interfaces/paymentlink/index.ts +0 -91
  39. package/src/mediationPaymentCard/MediationPaymentCard.ts +0 -42
  40. package/src/mocks/tropipay.ts +0 -0
  41. package/src/paymentcard/PaymentCard.ts +0 -123
  42. package/src/paymentcard/paymentcard.spec.ts +0 -79
  43. package/src/utils/TropipayUtils.ts +0 -126
  44. package/src/utils/errors.ts +0 -92
  45. package/tsconfig.json +0 -96
  46. package/utils/TropipayUtils.d.ts +0 -45
  47. package/utils/errors.d.ts +0 -16
@@ -1,459 +0,0 @@
1
- /**
2
- * Tropipayjs is a Typescript/Javascript library for the Tropipay API.
3
- *
4
- * @author Yosleivy Baez Acosta
5
- *
6
- */
7
-
8
- import axios, { Axios, AxiosError, AxiosRequestConfig } from "axios";
9
- import {
10
- TropipayConfig,
11
- AccountBalance,
12
- Country,
13
- PaymentLinkPayload,
14
- PaymentLink,
15
- MediationPaymentCardConfig,
16
- LoginResponse,
17
- AccountDeposits,
18
- RefundResponse,
19
- } from "../interfaces";
20
- type ServerMode = "Development" | "Production";
21
- import TropipayHooks from "../hooks/TropipayHooks";
22
- import PaymentCard from "../paymentcard/PaymentCard";
23
- import MediationPaymentCard from "../mediationPaymentCard/MediationPaymentCard";
24
- import DepositAccounts from "../depositAccount/depositAccounts";
25
- import { TropipayJSException, handleExceptions } from "../utils/errors";
26
- export class Tropipay {
27
- readonly clientId: string;
28
- readonly clientSecret: string;
29
- readonly scopes: String[];
30
- public request: Axios;
31
- public static accessToken: string | null;
32
- public static refreshToken: string | null;
33
- public static expiresIn: number | null;
34
- public serverMode: ServerMode;
35
- public hooks: TropipayHooks;
36
- public paymentCards: PaymentCard;
37
- public depositAccounts: DepositAccounts;
38
- public mediationPaymentCard: MediationPaymentCard;
39
-
40
- /**
41
- * Initializes a new instance of the Tropipay class.
42
- *
43
- * @param {TropipayConfig} config - The configuration object.
44
- */
45
-
46
- constructor(config: TropipayConfig) {
47
- // use all scopes if none is passed
48
- if (!config?.scopes) {
49
- this.scopes = [
50
- "ALLOW_GET_PROFILE_DATA",
51
- "ALLOW_PAYMENT_IN",
52
- "ALLOW_EXTERNAL_CHARGE",
53
- "KYC3_FULL_ALLOW",
54
- "ALLOW_PAYMENT_OUT",
55
- "ALLOW_MARKET_PURCHASES",
56
- "ALLOW_GET_BALANCE",
57
- "ALLOW_GET_MOVEMENT_LIST",
58
- "ALLOW_GET_CREDENTIAL",
59
- "ALLOW_REFUND",
60
- ];
61
- } else {
62
- this.scopes = config.scopes;
63
- }
64
-
65
- if (!config.clientId || !config.clientSecret) {
66
- throw new TropipayJSException(
67
- `You must pass clientId and clientSecret in Tropipay constructor`,
68
- 400,
69
- null
70
- );
71
- }
72
-
73
- this.clientId = config.clientId;
74
- this.clientSecret = config.clientSecret;
75
- this.serverMode = config.serverMode || "Development";
76
-
77
- const tpp_env = this.serverMode === "Production"
78
- ? "https://www.tropipay.com"
79
- : "https://tropipay-dev.herokuapp.com";
80
- this.request = axios.create({
81
- baseURL: config.customTropipayUrl || tpp_env,
82
- headers: {
83
- "Content-Type": "application/json",
84
- Accept: "application/json",
85
- Authorization: `Bearer ${Tropipay.accessToken}`,
86
- },
87
- });
88
-
89
-
90
- // Add request interceptor for Token expired
91
- this.request.interceptors.request.use(
92
- async (config: any) => {
93
- const currentTimestamp = Math.floor(Date.now() / 1000); // Current time in seconds
94
-
95
- if (Tropipay.expiresIn && Tropipay.expiresIn < currentTimestamp) {
96
- // Token has expired, attempt to refresh it
97
- try {
98
- await this.login();
99
- } catch (error) {
100
- // Handle token refresh error
101
- Tropipay.accessToken = null;
102
- Tropipay.refreshToken = null;
103
- throw handleExceptions(error as any);
104
- }
105
- }
106
-
107
- return config;
108
- },
109
- (error: AxiosError) => {
110
- return Promise.reject(error);
111
- }
112
- );
113
-
114
- this.hooks = new TropipayHooks(this);
115
- this.paymentCards = new PaymentCard(this);
116
- this.mediationPaymentCard = new MediationPaymentCard(this);
117
-
118
- this.depositAccounts = new DepositAccounts(this);
119
-
120
-
121
- }
122
-
123
- public async login() {
124
- try {
125
- if (Tropipay.refreshToken) {
126
- const { data } = await this.request.post<LoginResponse>(
127
- "/api/v2/access/token",
128
- {
129
- client_id: this.clientId,
130
- client_secret: this.clientSecret,
131
- grant_type: "refresh_token",
132
- refresh_token: Tropipay.refreshToken,
133
- },
134
- {
135
- headers: {
136
- "Content-Type": "application/json",
137
- Accept: "application/json",
138
- },
139
- }
140
- );
141
- return data;
142
- }
143
-
144
- // normal credetials login
145
- const { data } = await this.request.post<LoginResponse>(
146
- "/api/v2/access/token",
147
- {
148
- client_id: this.clientId,
149
- client_secret: this.clientSecret,
150
- grant_type: "client_credentials",
151
- scope: this.scopes.join(" "), // "ALLOW_GET_PROFILE_DATA ALLOW_PAYMENT_IN ALLOW_EXTERNAL_CHARGE KYC3_FULL_ALLOW ALLOW_PAYMENT_OUT ALLOW_MARKET_PURCHASES ALLOW_GET_BALANCE ALLOW_GET_MOVEMENT_LIST ALLOW_GET_CREDENTIAL",
152
- },
153
- {
154
- headers: {
155
- "Content-Type": "application/json",
156
- Accept: "application/json",
157
- },
158
- }
159
- );
160
-
161
- Tropipay.accessToken = data.access_token;
162
- Tropipay.refreshToken = data.refresh_token;
163
- Tropipay.expiresIn = data.expires_in;
164
- return data;
165
- } catch (error) {
166
- Tropipay.accessToken = null;
167
- Tropipay.refreshToken = null;
168
- Tropipay.expiresIn = null;
169
- throw handleExceptions(error as any);
170
- }
171
- }
172
-
173
- /**
174
- * Get the list of all supported countries by Tropipay.
175
- * @returns Array of Countries Data
176
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/bfac21259e2ff-getting-users-countries-list
177
- */
178
- async countries(): Promise<Country[]> {
179
- try {
180
- const countries = await this.request.get("/api/v2/countries");
181
- return countries.data;
182
- } catch (error) {
183
- throw handleExceptions(error as any);
184
- }
185
- }
186
- /**
187
- * Get user balance
188
- * @returns balance Object { balance: number, pendingIn: number, pendingOut: number }
189
- */
190
- async getBalance(): Promise<AccountBalance> {
191
- if (!Tropipay.accessToken) {
192
- await this.login();
193
- }
194
- try {
195
- const balance = await this.request.get("/api/v2/users/balance", {
196
- headers: {
197
- "Content-Type": "application/json",
198
- Authorization: `Bearer ${Tropipay.accessToken}`,
199
- Accept: "application/json",
200
- },
201
- });
202
- return balance.data;
203
- } catch (error) {
204
- throw handleExceptions(error as any);
205
- }
206
- }
207
-
208
- /**
209
- * Get the list of all detination countries supported by Tropipay.
210
- * Obtaining the list of valid countries to send funds to. Useful
211
- * when adding new beneficiaries to some user.
212
- *
213
- * @returns Array of Country Objects
214
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/3cfe5504f0524-getting-list-of-beneficiary-countries
215
- */
216
- async destinations(): Promise<Country[]> {
217
- try {
218
- const countries = await this.request.get(
219
- "/api/v2/countries/destinations"
220
- );
221
- return countries.data;
222
- } catch (error) {
223
- throw new Error(`Could not retrieve the destination countries list`);
224
- }
225
- }
226
-
227
- /**
228
- * Get list of all the favorites payment links.
229
- * @returns Array of account Object or throws an error
230
- */
231
- async favorites() {
232
- if (!Tropipay.accessToken) {
233
- await this.login();
234
- }
235
- try {
236
- const favoritesList = await this.request.get(
237
- "/api/v2/paymentcards/favorites",
238
- {
239
- headers: {
240
- "Content-Type": "application/json",
241
- Authorization: `Bearer ${Tropipay.accessToken}`,
242
- Accept: "application/json",
243
- },
244
- }
245
- );
246
- return favoritesList?.data?.rows;
247
- } catch (error) {
248
- throw handleExceptions(error as any);
249
- }
250
- }
251
-
252
- /**
253
- * List all account movements. You can optionaly specify
254
- * offset and limit params for pagination.
255
- * @returns
256
- */
257
- async movements(offset = 0, limit = 10) {
258
- if (!Tropipay.accessToken) await this.login();
259
-
260
- try {
261
- const movements = await this.request.get("/api/v2/movements", {
262
- params: { limit: limit, offset: offset },
263
- headers: {
264
- "Content-Type": "application/json",
265
- Accept: "application/json",
266
- Authorization: `Bearer ${Tropipay.accessToken}`,
267
- },
268
- });
269
- return movements.data;
270
- } catch (error) {
271
- throw handleExceptions(error as any);
272
- }
273
- }
274
-
275
- /**
276
- * Return profile data for current Tropipay account.
277
- * @returns account object
278
- */
279
- async profile() {
280
- if (!Tropipay.accessToken) await this.login();
281
- try {
282
- const profile = await this.request.get("/api/users/profile");
283
- return profile.data;
284
- } catch (error) {
285
- throw handleExceptions(error as any);
286
- }
287
- }
288
-
289
- /**
290
- * Obtain current Tropipay conversion rate. For example USD to EUR
291
- * targetCurrency must be 'EUR'
292
- * @param originCurrency Target currency code supported by Tropipay.
293
- * @param targetCurrency Must be 'EUR'? (not documented by Tropipay)
294
- * @returns Conversion rate (number)
295
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/85163f6f28b23-get-rate
296
- */
297
- async rates(
298
- originCurrency: string,
299
- targetCurrency: string = "EUR"
300
- ): Promise<number | Error> {
301
- if (!Tropipay.accessToken) {
302
- await this.login();
303
- }
304
- try {
305
- const rates = await this.request.post(
306
- "/api/v2/movements/get_rate",
307
- {
308
- currencyFrom: originCurrency,
309
- currencyTo: targetCurrency,
310
- },
311
- {
312
- headers: {
313
- "Content-Type": "application/json",
314
- Accept: "application/json",
315
- Authorization: `Bearer ${Tropipay.accessToken}`,
316
- },
317
- }
318
- );
319
- return rates.data.rate;
320
- } catch (error) {
321
- throw handleExceptions(error as any);
322
- }
323
- }
324
-
325
- /**
326
- * (ONLY in Bussiness Accounts)
327
- * An escrow payment link. This allows a payment to be made to persons
328
- * belonging or not to the TropiPay platform with the particularity
329
- * that the payment will be held in custody or retained until it is
330
- * released with the approval of the payer.
331
- * @deprecated This method is no longer supported and may be removed in a future release.
332
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/12a128ff971e4-creating-a-mediation-payment-card
333
- * @param config Payload with the payment details
334
- */
335
- async createMediationPaymentCard(
336
- config: MediationPaymentCardConfig
337
- ): Promise<PaymentLink> {
338
- if (!Tropipay.accessToken) await this.login();
339
- try {
340
- const mediation = await this.request.post(
341
- "/api/v2/paymentcards/mediation",
342
- config,
343
- {
344
- headers: {
345
- "Content-Type": "application/json",
346
- Authorization: `Bearer ${Tropipay.accessToken}`,
347
- Accept: "application/json",
348
- },
349
- }
350
- );
351
- return mediation.data as PaymentLink;
352
- } catch (error) {
353
- throw handleExceptions(error as any);
354
- }
355
- }
356
-
357
- /**
358
- * Refund a completed transaction.
359
- * Requires 2FA enabled and ALLOW_REFUND permission on the Tropipay account.
360
- *
361
- * In Development mode, `securityCode` defaults to `"123456"` when omitted.
362
- * In Production mode, you must first call `requestSecurityCode()` to receive
363
- * the 2FA code via SMS, then pass it as the `securityCode` parameter.
364
- *
365
- * @see https://doc.tropipay.com/docs/api-reference/movements#refund-a-transaction
366
- * @param orderCode Code of the order/transaction to refund (e.g. "ORD-123456")
367
- * @param amount Amount to refund in cents (e.g. 5000 = 50.00 USD/EUR)
368
- * @param securityCode 2FA confirmation code. Use "123456" in Development or
369
- * the code received via SMS in Production (obtained via requestSecurityCode()).
370
- */
371
- async refundMovement(
372
- orderCode: string,
373
- amount: number,
374
- securityCode: string
375
- ): Promise<RefundResponse> {
376
- if (!Tropipay.accessToken) {
377
- await this.login();
378
- }
379
-
380
- if (!this.scopes.includes("ALLOW_REFUND")) {
381
- throw new TropipayJSException(
382
- "The credential does not have the ALLOW_REFUND scope required to perform refunds. If you believe your account should have this permission, please contact Tropipay support.",
383
- 403,
384
- null
385
- );
386
- }
387
-
388
- try {
389
- const response = await this.request.post(
390
- "/api/v3/movements/in/refund",
391
- { orderCode, amount, securityCode },
392
- {
393
- headers: {
394
- "Content-Type": "application/json",
395
- Accept: "application/json",
396
- Authorization: `Bearer ${Tropipay.accessToken}`,
397
- },
398
- }
399
- );
400
- return response.data as RefundResponse;
401
- } catch (error) {
402
- throw handleExceptions(error as any);
403
- }
404
- }
405
-
406
- /**
407
- * Request a 2FA security code to be sent via SMS for confirming sensitive
408
- * operations like refunds.
409
- *
410
- * In Production mode, you must call this method first to trigger an SMS with
411
- * the code, then use the received code when calling `refundMovement()`.
412
- * In Development mode this is not required as the default code "123456"
413
- * is used automatically.
414
- *
415
- * @see https://doc.tropipay.com/docs/api-reference/movements#refund-a-transaction
416
- * @returns The API response confirming the code was sent
417
- */
418
- async requestSecurityCode(type: "sms" | "email" = "sms"): Promise<any> {
419
- if (!Tropipay.accessToken) {
420
- await this.login();
421
- }
422
-
423
- try {
424
- const response = await this.request.post(
425
- "/api/v3/users/sendSecurityCode",
426
- { type },
427
- {
428
- headers: {
429
- "Content-Type": "application/json",
430
- Accept: "application/json",
431
- Authorization: `Bearer ${Tropipay.accessToken}`,
432
- },
433
- }
434
- );
435
- return response.data;
436
- } catch (error) {
437
- throw handleExceptions(error as any);
438
- }
439
- }
440
-
441
- }
442
-
443
- export class ClientSideUtils {
444
- constructor(tropipayInstance: Tropipay) {
445
- throw Error(`Not implemented yet`);
446
- }
447
- }
448
-
449
- export const Scopes = {
450
- ALLOW_PAYMENT_IN: "ALLOW_PAYMENT_IN",
451
- ALLOW_EXTERNAL_CHARGE: "ALLOW_EXTERNAL_CHARGE",
452
- KYC3_FULL_ALLOW: "KYC3_FULL_ALLOW",
453
- ALLOW_PAYMENT_OUT: "ALLOW_PAYMENT_OUT",
454
- ALLOW_MARKET_PURCHASES: "ALLOW_MARKET_PURCHASES",
455
- ALLOW_GET_BALANCE: "ALLOW_GET_BALANCE",
456
- ALLOW_GET_MOVEMENT_LIST: "ALLOW_GET_MOVEMENT_LIST",
457
- ALLOW_GET_CREDENTIAL: "ALLOW_GET_CREDENTIAL",
458
- ALLOW_REFUND: "ALLOW_REFUND",
459
- };
@@ -1,2 +0,0 @@
1
- import { ServerMode } from "../constants/TropipayConstants";
2
- export const SERVER_MODE: ServerMode = "Development"; // Move the constant here
@@ -1,2 +0,0 @@
1
- export type ServerMode = "Development" | "Production";
2
- export const MAX_IMAGE_SIZE_MB = 1;
@@ -1,139 +0,0 @@
1
- import { AxiosError } from "axios";
2
- import { Tropipay } from "../api/TropipayAPI";
3
- import { DepositAccountConfig } from "../interfaces/depositaccounts";
4
- import { handleExceptions } from "../utils/errors";
5
- export default class DepositAccounts {
6
- private tropipay: Tropipay;
7
- constructor(tropipayInstance: Tropipay) {
8
- this.tropipay = tropipayInstance;
9
- }
10
-
11
- /**
12
- * List od all beneficiaries of this account
13
- * @returns Array of DepositAccounts
14
- */
15
- public async list() {
16
- if (!Tropipay.accessToken) {
17
- await this.tropipay.login();
18
- }
19
- try {
20
- const deposit = await this.tropipay.request.get(
21
- `/api/v2/deposit_accounts`,
22
- {
23
- headers: {
24
- "Content-Type": "application/json",
25
- Authorization: `Bearer ${Tropipay.accessToken}`,
26
- Accept: "application/json",
27
- },
28
- }
29
- );
30
- return deposit.data;
31
- } catch (error) {
32
- return handleExceptions(error as unknown as any);
33
- }
34
- }
35
- /**
36
- * Adds a new beneficiary to the user account.
37
- * @param payload
38
- * @returns
39
- */
40
- public async create(depositAccountObj: DepositAccountConfig) {
41
- if (!Tropipay.accessToken) {
42
- await this.tropipay.login();
43
- }
44
- try {
45
- const deposit = await this.tropipay.request.post(
46
- "/api/v2/deposit_accounts",
47
- depositAccountObj,
48
- {
49
- headers: {
50
- "Content-Type": "application/json",
51
- Authorization: `Bearer ${Tropipay.accessToken}`,
52
- Accept: "application/json",
53
- },
54
- }
55
- );
56
- return deposit.data;
57
- } catch (error) {
58
- return handleExceptions(error as unknown as any);
59
- }
60
- }
61
- /**
62
- * This returns details of a specific
63
- * Deposit Account (beneficiary) specified by its ID
64
- * @param id
65
- * @returns
66
- */
67
- public async get(id: string) {
68
- if (!Tropipay.accessToken) {
69
- await this.tropipay.login();
70
- }
71
- try {
72
- const deposit = await this.tropipay.request.get(
73
- `/api/v2/deposit_accounts/${id}`,
74
- {
75
- headers: {
76
- "Content-Type": "application/json",
77
- Authorization: `Bearer ${Tropipay.accessToken}`,
78
- Accept: "application/json",
79
- },
80
- }
81
- );
82
- return deposit.data;
83
- } catch (error) {
84
- return handleExceptions(error as unknown as any);
85
- }
86
- }
87
-
88
- /**
89
- * Updates certain beneficiary data.
90
- * @param depositAccountObj
91
- * @returns
92
- */
93
- public async update(depositAccountObj: Partial<DepositAccountConfig>) {
94
- if (!Tropipay.accessToken) {
95
- await this.tropipay.login();
96
- }
97
- try {
98
- const deposit = await this.tropipay.request.put(
99
- `/api/v2/deposit_accounts/`,
100
- {
101
- headers: {
102
- "Content-Type": "application/json",
103
- Authorization: `Bearer ${Tropipay.accessToken}`,
104
- Accept: "application/json",
105
- },
106
- }
107
- );
108
- return deposit.data;
109
- } catch (error) {
110
- return handleExceptions(error as unknown as any);
111
- }
112
- }
113
-
114
- /**
115
- * (UNTESTED) Deletes the beneficiary indicated by id
116
- * @param id
117
- * @returns
118
- */
119
- public async delete(id: number) {
120
- if (!Tropipay.accessToken) {
121
- await this.tropipay.login();
122
- }
123
- try {
124
- const deposit = await this.tropipay.request.delete(
125
- `/api/v2/deposit_accounts/${id}`,
126
- {
127
- headers: {
128
- "Content-Type": "application/json",
129
- Authorization: `Bearer ${Tropipay.accessToken}`,
130
- Accept: "application/json",
131
- },
132
- }
133
- );
134
- return deposit.data;
135
- } catch (error) {
136
- return handleExceptions(error as unknown as any);
137
- }
138
- }
139
- }