@yosle/tropipayjs 0.1.20 → 0.1.21
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 +1 -1
- package/api/TropipayAPI.d.ts +8 -2
- package/hooks/TropipayHooks.d.ts +1 -1
- package/index.d.ts +10 -3
- package/index.js +48 -5
- package/interfaces/index.d.ts +1 -0
- package/package.json +3 -2
- package/src/api/TropipayAPI.ts +58 -5
- package/src/hooks/TropipayHooks.ts +1 -1
- package/src/interfaces/index.ts +1 -0
- package/src/utils/errors.ts +7 -3
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ or
|
|
|
47
47
|
|
|
48
48
|
## Setting up your app credentials
|
|
49
49
|
|
|
50
|
-

|
|
51
51
|

|
|
52
52
|
|
|
53
53
|
In order to use the Tropipay API you'll need a client ID and a client secret. Sign up with your [Tropipay](www.tropipay.com) account, go to the App Menu->Applications and credentials. You'll be asked to enter a confirmation code (use 123456 in the test enviroment). _Make sure you test everything in the [test environment](https://tropipay-dev.herokuapp.com) first before using your real account._ Create an app and configure the client id an client secret for the app.
|
package/api/TropipayAPI.d.ts
CHANGED
|
@@ -13,13 +13,19 @@ import { DepositAccounts } from "../depositAccount/depositAccounts";
|
|
|
13
13
|
export declare class Tropipay {
|
|
14
14
|
readonly clientId: string;
|
|
15
15
|
readonly clientSecret: string;
|
|
16
|
+
readonly scopes: String[];
|
|
16
17
|
request: Axios;
|
|
17
|
-
static accessToken: string |
|
|
18
|
-
static refreshToken: string |
|
|
18
|
+
static accessToken: string | null;
|
|
19
|
+
static refreshToken: string | null;
|
|
19
20
|
serverMode: ServerMode;
|
|
20
21
|
hooks: TropipayHooks;
|
|
21
22
|
paymentCards: PaymentCard;
|
|
22
23
|
depositAccounts: DepositAccounts;
|
|
24
|
+
/**
|
|
25
|
+
* Initializes a new instance of the Tropipay class.
|
|
26
|
+
*
|
|
27
|
+
* @param {TropipayConfig} config - The configuration object.
|
|
28
|
+
*/
|
|
23
29
|
constructor(config: TropipayConfig);
|
|
24
30
|
login(): Promise<LoginResponse>;
|
|
25
31
|
/**
|
package/hooks/TropipayHooks.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare class TropipayHooks {
|
|
|
25
25
|
value: string;
|
|
26
26
|
}): Promise<any>;
|
|
27
27
|
/**
|
|
28
|
-
* Get the
|
|
28
|
+
* Get the subscribed hook info by his event type.
|
|
29
29
|
* If no event type is passed, it will return
|
|
30
30
|
* all subscribed hooks or empty Array if none exist.
|
|
31
31
|
* @param eventType or no params for retrieving all hooks
|
package/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ type AccountBalance = {
|
|
|
11
11
|
interface TropipayConfig {
|
|
12
12
|
clientId: string;
|
|
13
13
|
clientSecret: string;
|
|
14
|
+
scopes?: string[];
|
|
14
15
|
serverMode?: ServerMode$1;
|
|
15
16
|
}
|
|
16
17
|
type TropipayCredentials = {
|
|
@@ -188,7 +189,7 @@ declare class TropipayHooks {
|
|
|
188
189
|
value: string;
|
|
189
190
|
}): Promise<any>;
|
|
190
191
|
/**
|
|
191
|
-
* Get the
|
|
192
|
+
* Get the subscribed hook info by his event type.
|
|
192
193
|
* If no event type is passed, it will return
|
|
193
194
|
* all subscribed hooks or empty Array if none exist.
|
|
194
195
|
* @param eventType or no params for retrieving all hooks
|
|
@@ -294,13 +295,19 @@ type ServerMode = "Development" | "Production";
|
|
|
294
295
|
declare class Tropipay {
|
|
295
296
|
readonly clientId: string;
|
|
296
297
|
readonly clientSecret: string;
|
|
298
|
+
readonly scopes: String[];
|
|
297
299
|
request: Axios;
|
|
298
|
-
static accessToken: string |
|
|
299
|
-
static refreshToken: string |
|
|
300
|
+
static accessToken: string | null;
|
|
301
|
+
static refreshToken: string | null;
|
|
300
302
|
serverMode: ServerMode;
|
|
301
303
|
hooks: TropipayHooks;
|
|
302
304
|
paymentCards: PaymentCard;
|
|
303
305
|
depositAccounts: DepositAccounts;
|
|
306
|
+
/**
|
|
307
|
+
* Initializes a new instance of the Tropipay class.
|
|
308
|
+
*
|
|
309
|
+
* @param {TropipayConfig} config - The configuration object.
|
|
310
|
+
*/
|
|
304
311
|
constructor(config: TropipayConfig);
|
|
305
312
|
login(): Promise<LoginResponse>;
|
|
306
313
|
/**
|
package/index.js
CHANGED
|
@@ -42,8 +42,8 @@ function handleExceptions(error) {
|
|
|
42
42
|
return new TropipayJSException(errorMessage, axiosResponse.status, axiosResponse.data.error);
|
|
43
43
|
case 404:
|
|
44
44
|
return new TropipayJSException(errorMessage, axiosResponse.status, axiosResponse.data.error);
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
case 429:
|
|
46
|
+
return new TropipayJSException(`${errorMessage} Too many request or rate limited`, axiosResponse.status, axiosResponse.data.error);
|
|
47
47
|
default:
|
|
48
48
|
return new TropipayJSException(errorMessage, axiosResponse.status, axiosResponse.data.error);
|
|
49
49
|
}
|
|
@@ -58,7 +58,7 @@ function handleExceptions(error) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
else {
|
|
61
|
-
return new TropipayJSException(
|
|
61
|
+
return new TropipayJSException("An Unknown error occurred", 500, null);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
class TropipayJSException extends Error {
|
|
@@ -120,7 +120,7 @@ class TropipayHooks {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
|
-
* Get the
|
|
123
|
+
* Get the subscribed hook info by his event type.
|
|
124
124
|
* If no event type is passed, it will return
|
|
125
125
|
* all subscribed hooks or empty Array if none exist.
|
|
126
126
|
* @param eventType or no params for retrieving all hooks
|
|
@@ -408,6 +408,7 @@ class DepositAccounts {
|
|
|
408
408
|
class Tropipay {
|
|
409
409
|
clientId;
|
|
410
410
|
clientSecret;
|
|
411
|
+
scopes;
|
|
411
412
|
request;
|
|
412
413
|
static accessToken;
|
|
413
414
|
static refreshToken;
|
|
@@ -415,7 +416,32 @@ class Tropipay {
|
|
|
415
416
|
hooks;
|
|
416
417
|
paymentCards;
|
|
417
418
|
depositAccounts;
|
|
419
|
+
/**
|
|
420
|
+
* Initializes a new instance of the Tropipay class.
|
|
421
|
+
*
|
|
422
|
+
* @param {TropipayConfig} config - The configuration object.
|
|
423
|
+
*/
|
|
418
424
|
constructor(config) {
|
|
425
|
+
// use all scopes if none is passed
|
|
426
|
+
if (!config?.scopes) {
|
|
427
|
+
this.scopes = [
|
|
428
|
+
"ALLOW_GET_PROFILE_DATA",
|
|
429
|
+
"ALLOW_PAYMENT_IN",
|
|
430
|
+
"ALLOW_EXTERNAL_CHARGE",
|
|
431
|
+
"KYC3_FULL_ALLOW",
|
|
432
|
+
"ALLOW_PAYMENT_OUT",
|
|
433
|
+
"ALLOW_MARKET_PURCHASES",
|
|
434
|
+
"ALLOW_GET_BALANCE",
|
|
435
|
+
"ALLOW_GET_MOVEMENT_LIST",
|
|
436
|
+
"ALLOW_GET_CREDENTIAL",
|
|
437
|
+
];
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
this.scopes = config.scopes;
|
|
441
|
+
}
|
|
442
|
+
if (!config.clientId || !config.clientSecret) {
|
|
443
|
+
throw new TropipayJSException(`You must pass clientId and clientSecret in Tropipay constructor`, 400, null);
|
|
444
|
+
}
|
|
419
445
|
this.clientId = config.clientId;
|
|
420
446
|
this.clientSecret = config.clientSecret;
|
|
421
447
|
this.serverMode = config.serverMode || "Development";
|
|
@@ -435,11 +461,26 @@ class Tropipay {
|
|
|
435
461
|
}
|
|
436
462
|
async login() {
|
|
437
463
|
try {
|
|
464
|
+
if (Tropipay.refreshToken) {
|
|
465
|
+
const { data } = await this.request.post("/api/v2/access/token", {
|
|
466
|
+
client_id: this.clientId,
|
|
467
|
+
client_secret: this.clientSecret,
|
|
468
|
+
grant_type: "refresh_token",
|
|
469
|
+
refresh_token: Tropipay.refreshToken,
|
|
470
|
+
}, {
|
|
471
|
+
headers: {
|
|
472
|
+
"Content-Type": "application/json",
|
|
473
|
+
Accept: "application/json",
|
|
474
|
+
},
|
|
475
|
+
});
|
|
476
|
+
return data;
|
|
477
|
+
}
|
|
478
|
+
// normal credetials login
|
|
438
479
|
const { data } = await this.request.post("/api/v2/access/token", {
|
|
439
480
|
client_id: this.clientId,
|
|
440
481
|
client_secret: this.clientSecret,
|
|
441
482
|
grant_type: "client_credentials",
|
|
442
|
-
scope: "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",
|
|
483
|
+
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",
|
|
443
484
|
}, {
|
|
444
485
|
headers: {
|
|
445
486
|
"Content-Type": "application/json",
|
|
@@ -451,6 +492,8 @@ class Tropipay {
|
|
|
451
492
|
return data;
|
|
452
493
|
}
|
|
453
494
|
catch (error) {
|
|
495
|
+
Tropipay.accessToken = null;
|
|
496
|
+
Tropipay.refreshToken = null;
|
|
454
497
|
throw handleExceptions(error);
|
|
455
498
|
}
|
|
456
499
|
}
|
package/interfaces/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yosle/tropipayjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "Javascript / Typescript SDK for the Tropipay API",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"rollup-plugin-dts": "^4.2.2",
|
|
37
37
|
"run-s": "^0.0.0",
|
|
38
38
|
"typescript": "^5.0.4",
|
|
39
|
-
"vitest": "^1.2.2"
|
|
39
|
+
"vitest": "^1.2.2",
|
|
40
|
+
"@types/node": "^20.12.7"
|
|
40
41
|
},
|
|
41
42
|
"funding": {
|
|
42
43
|
"type": "individual",
|
package/src/api/TropipayAPI.ts
CHANGED
|
@@ -20,19 +20,51 @@ type ServerMode = "Development" | "Production";
|
|
|
20
20
|
import { TropipayHooks } from "../hooks/TropipayHooks";
|
|
21
21
|
import { PaymentCard } from "../paymentcard/PaymentCard";
|
|
22
22
|
import { DepositAccounts } from "../depositAccount/depositAccounts";
|
|
23
|
-
import { handleExceptions } from "../utils/errors";
|
|
23
|
+
import { TropipayJSException, handleExceptions } from "../utils/errors";
|
|
24
24
|
export class Tropipay {
|
|
25
25
|
readonly clientId: string;
|
|
26
26
|
readonly clientSecret: string;
|
|
27
|
+
readonly scopes: String[];
|
|
27
28
|
public request: Axios;
|
|
28
|
-
public static accessToken: string |
|
|
29
|
-
public static refreshToken: string |
|
|
29
|
+
public static accessToken: string | null;
|
|
30
|
+
public static refreshToken: string | null;
|
|
30
31
|
public serverMode: ServerMode;
|
|
31
32
|
public hooks: TropipayHooks;
|
|
32
33
|
public paymentCards: PaymentCard;
|
|
33
34
|
public depositAccounts: DepositAccounts;
|
|
34
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Initializes a new instance of the Tropipay class.
|
|
38
|
+
*
|
|
39
|
+
* @param {TropipayConfig} config - The configuration object.
|
|
40
|
+
*/
|
|
41
|
+
|
|
35
42
|
constructor(config: TropipayConfig) {
|
|
43
|
+
// use all scopes if none is passed
|
|
44
|
+
if (!config?.scopes) {
|
|
45
|
+
this.scopes = [
|
|
46
|
+
"ALLOW_GET_PROFILE_DATA",
|
|
47
|
+
"ALLOW_PAYMENT_IN",
|
|
48
|
+
"ALLOW_EXTERNAL_CHARGE",
|
|
49
|
+
"KYC3_FULL_ALLOW",
|
|
50
|
+
"ALLOW_PAYMENT_OUT",
|
|
51
|
+
"ALLOW_MARKET_PURCHASES",
|
|
52
|
+
"ALLOW_GET_BALANCE",
|
|
53
|
+
"ALLOW_GET_MOVEMENT_LIST",
|
|
54
|
+
"ALLOW_GET_CREDENTIAL",
|
|
55
|
+
];
|
|
56
|
+
} else {
|
|
57
|
+
this.scopes = config.scopes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!config.clientId || !config.clientSecret) {
|
|
61
|
+
throw new TropipayJSException(
|
|
62
|
+
`You must pass clientId and clientSecret in Tropipay constructor`,
|
|
63
|
+
400,
|
|
64
|
+
null
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
36
68
|
this.clientId = config.clientId;
|
|
37
69
|
this.clientSecret = config.clientSecret;
|
|
38
70
|
this.serverMode = config.serverMode || "Development";
|
|
@@ -54,14 +86,33 @@ export class Tropipay {
|
|
|
54
86
|
|
|
55
87
|
public async login() {
|
|
56
88
|
try {
|
|
89
|
+
if (Tropipay.refreshToken) {
|
|
90
|
+
const { data } = await this.request.post<LoginResponse>(
|
|
91
|
+
"/api/v2/access/token",
|
|
92
|
+
{
|
|
93
|
+
client_id: this.clientId,
|
|
94
|
+
client_secret: this.clientSecret,
|
|
95
|
+
grant_type: "refresh_token",
|
|
96
|
+
refresh_token: Tropipay.refreshToken,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
headers: {
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
Accept: "application/json",
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
return data;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// normal credetials login
|
|
57
109
|
const { data } = await this.request.post<LoginResponse>(
|
|
58
110
|
"/api/v2/access/token",
|
|
59
111
|
{
|
|
60
112
|
client_id: this.clientId,
|
|
61
113
|
client_secret: this.clientSecret,
|
|
62
114
|
grant_type: "client_credentials",
|
|
63
|
-
scope:
|
|
64
|
-
"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",
|
|
115
|
+
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",
|
|
65
116
|
},
|
|
66
117
|
{
|
|
67
118
|
headers: {
|
|
@@ -75,6 +126,8 @@ export class Tropipay {
|
|
|
75
126
|
Tropipay.refreshToken = data.refresh_token;
|
|
76
127
|
return data;
|
|
77
128
|
} catch (error) {
|
|
129
|
+
Tropipay.accessToken = null;
|
|
130
|
+
Tropipay.refreshToken = null;
|
|
78
131
|
throw handleExceptions(error as any);
|
|
79
132
|
}
|
|
80
133
|
}
|
|
@@ -57,7 +57,7 @@ export class TropipayHooks {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
|
-
* Get the
|
|
60
|
+
* Get the subscribed hook info by his event type.
|
|
61
61
|
* If no event type is passed, it will return
|
|
62
62
|
* all subscribed hooks or empty Array if none exist.
|
|
63
63
|
* @param eventType or no params for retrieving all hooks
|
package/src/interfaces/index.ts
CHANGED
package/src/utils/errors.ts
CHANGED
|
@@ -57,8 +57,12 @@ export function handleExceptions(
|
|
|
57
57
|
axiosResponse.status,
|
|
58
58
|
axiosResponse.data.error
|
|
59
59
|
);
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
case 429:
|
|
61
|
+
return new TropipayJSException(
|
|
62
|
+
`${errorMessage} Too many request or rate limited`,
|
|
63
|
+
axiosResponse.status,
|
|
64
|
+
axiosResponse.data.error
|
|
65
|
+
);
|
|
62
66
|
default:
|
|
63
67
|
return new TropipayJSException(
|
|
64
68
|
errorMessage,
|
|
@@ -78,7 +82,7 @@ export function handleExceptions(
|
|
|
78
82
|
return new TropipayJSException("An error occurred", 500, null);
|
|
79
83
|
}
|
|
80
84
|
} else {
|
|
81
|
-
return new TropipayJSException(
|
|
85
|
+
return new TropipayJSException("An Unknown error occurred", 500, null);
|
|
82
86
|
}
|
|
83
87
|
}
|
|
84
88
|
|