@yosle/tropipayjs 0.1.8 → 0.1.10

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/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "@yosle/tropipayjs",
3
- "version": "0.1.8",
4
- "description": "Javascript / Typescript SDK for the Tropipay API",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "_clear": "rimraf src/dist/*",
9
- "_tsc": "tsc",
10
- "_make-bundle": "rollup --config",
11
- "build": "yarn _clear && yarn _tsc && yarn _make-bundle"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "https://github.com/yosle/tropipayjs"
16
- },
17
- "keywords": [
18
- "Javascript",
19
- "Typescript",
20
- "Tropipay",
21
- "payments",
22
- "gateway",
23
- "API"
24
- ],
25
- "author": "Yosleivy Baez Acosta <yosle007@gmail.com>",
26
- "license": "MIT",
27
- "dependencies": {
28
- "axios": "^0.27.2",
29
- "tslib": "^2.4.0"
30
- },
31
- "devDependencies": {
32
- "@rollup/plugin-typescript": "^8.5.0",
33
- "rimraf": "^3.0.2",
34
- "rollup": "^2.79.1",
35
- "rollup-plugin-dts": "^4.2.2",
36
- "run-s": "^0.0.0",
37
- "typescript": "^5.0.4"
38
- },
39
- "funding": {
40
- "type": "individual",
41
- "url": "https://tppay.me/l94qaa3h"
42
- }
43
- }
1
+ {
2
+ "name": "@yosle/tropipayjs",
3
+ "version": "0.1.10",
4
+ "description": "Javascript / Typescript SDK for the Tropipay API",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "_clear": "rimraf src/dist/*",
9
+ "_tsc": "tsc",
10
+ "_make-bundle": "rollup --config",
11
+ "build": "yarn _clear && yarn _tsc && yarn _make-bundle"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/yosle/tropipayjs"
16
+ },
17
+ "keywords": [
18
+ "Javascript",
19
+ "Typescript",
20
+ "Tropipay",
21
+ "payments",
22
+ "gateway",
23
+ "API"
24
+ ],
25
+ "author": "Yosleivy Baez Acosta <yosle007@gmail.com>",
26
+ "license": "MIT",
27
+ "dependencies": {
28
+ "axios": "^0.27.2",
29
+ "tslib": "^2.4.0"
30
+ },
31
+ "devDependencies": {
32
+ "@rollup/plugin-typescript": "^8.5.0",
33
+ "rimraf": "^3.0.2",
34
+ "rollup": "^2.79.1",
35
+ "rollup-plugin-dts": "^4.2.2",
36
+ "run-s": "^0.0.0",
37
+ "typescript": "^5.0.4"
38
+ },
39
+ "funding": {
40
+ "type": "individual",
41
+ "url": "https://tppay.me/l94qaa3h"
42
+ }
43
+ }
package/rollup.config.js CHANGED
@@ -1,23 +1,24 @@
1
- // Contents of the file /rollup.config.js
2
- import typescript from '@rollup/plugin-typescript';
3
- import dts from "rollup-plugin-dts";
4
- const config = [
5
- {
6
- input: 'dist/index.js',
7
- output: {
8
- file: 'index.js',
9
- format: 'cjs',
10
- sourcemap: true,
11
- },
12
- external: ['axios'],
13
- plugins: [typescript()]
14
- }, {
15
- input: 'dist/index.d.ts',
16
- output: {
17
- file: 'index.d.ts',
18
- format: 'es'
19
- },
20
- plugins: [dts()]
21
- }
22
- ];
23
- export default config;
1
+ // Contents of the file /rollup.config.js
2
+ import typescript from "@rollup/plugin-typescript";
3
+ import dts from "rollup-plugin-dts";
4
+ const config = [
5
+ {
6
+ input: "dist/index.js",
7
+ output: {
8
+ file: "index.js",
9
+ format: "cjs",
10
+ sourcemap: true,
11
+ },
12
+ external: ["axios", "crypto"],
13
+ plugins: [typescript()],
14
+ },
15
+ {
16
+ input: "dist/index.d.ts",
17
+ output: {
18
+ file: "index.d.ts",
19
+ format: "es",
20
+ },
21
+ plugins: [dts()],
22
+ },
23
+ ];
24
+ export default config;
@@ -0,0 +1,319 @@
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 } from "axios";
9
+ import {
10
+ TropipayConfig,
11
+ AccountBalance,
12
+ Country,
13
+ PaymentLinkPayload,
14
+ PaymentLink,
15
+ mediationPaymentCardConfig,
16
+ LoginResponse,
17
+ AccountDeposits,
18
+ } from "../interfaces";
19
+ type ServerMode = "Development" | "Production";
20
+ import { TropipayHooks } from "../hooks/TropipayHooks";
21
+ export class Tropipay {
22
+ readonly clientId: string;
23
+ readonly clientSecret: string;
24
+ public request: Axios;
25
+ public static accessToken: string | undefined;
26
+ public static refreshToken: string | undefined;
27
+ public serverMode: ServerMode;
28
+ public hooks: TropipayHooks;
29
+
30
+ constructor(config: TropipayConfig) {
31
+ this.clientId = config.clientId;
32
+ this.clientSecret = config.clientSecret;
33
+ this.serverMode = config.serverMode || "Development";
34
+ this.request = axios.create({
35
+ baseURL:
36
+ this.serverMode === "Production"
37
+ ? "https://www.tropipay.com"
38
+ : "https://tropipay-dev.herokuapp.com",
39
+ headers: {
40
+ "Content-Type": "application/json",
41
+ Accept: "application/json",
42
+ Authorization: `Bearer ${Tropipay.accessToken}`,
43
+ },
44
+ });
45
+ this.hooks = new TropipayHooks(this);
46
+ }
47
+
48
+ public async login() {
49
+ try {
50
+ const { data } = await this.request.post<LoginResponse>(
51
+ "/api/v2/access/token",
52
+ {
53
+ client_id: this.clientId,
54
+ client_secret: this.clientSecret,
55
+ grant_type: "client_credentials",
56
+ scope:
57
+ "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",
58
+ },
59
+ {
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ Accept: "application/json",
63
+ },
64
+ }
65
+ );
66
+
67
+ Tropipay.accessToken = data.access_token;
68
+ Tropipay.refreshToken = data.refresh_token;
69
+ return data;
70
+ } catch (error) {
71
+ if (axios.isAxiosError(error)) {
72
+ throw new Error(
73
+ `Could not obtain the access token from credentials ${error}`
74
+ );
75
+ }
76
+ throw new Error(
77
+ `Could not obtain the access token from credentials ${error}`
78
+ );
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Create a paymentLink with the specified options.
84
+ * @param payload PaymentLinkPayload Object.
85
+ * @returns Promise<PaymentLink> or throws an Exception.
86
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6ODgyNTM3OQ-create-a-new-pay-link-charge
87
+ */
88
+ async createPaymentCard(payload: PaymentLinkPayload): Promise<PaymentLink> {
89
+ if (!Tropipay.accessToken) {
90
+ await this.login();
91
+ }
92
+ try {
93
+ const paylink = await this.request.post("/api/v2/paymentcards", payload, {
94
+ headers: {
95
+ "Content-Type": "application/json",
96
+ Authorization: `Bearer ${Tropipay.accessToken}`,
97
+ Accept: "application/json",
98
+ },
99
+ });
100
+ return paylink.data as PaymentLink;
101
+ } catch (error) {
102
+ throw new Error(
103
+ `Could not obtain the access token with the given credentials.`
104
+ );
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Get all deposits in this account.
110
+ * @returns A Promise of an Array of AccountDeposits or throws an Exception
111
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6OTgyOTQ1Mg-get-deposit-accounts-list
112
+ */
113
+ async getDepositAccounts(): Promise<AccountDeposits[] | Error> {
114
+ if (!Tropipay.accessToken) {
115
+ await this.login();
116
+ }
117
+ try {
118
+ const deposits = await this.request.get("/api/v2/deposit_accounts", {
119
+ headers: {
120
+ "Content-Type": "application/json",
121
+ Authorization: `Bearer ${Tropipay.accessToken}`,
122
+ Accept: "application/json",
123
+ },
124
+ });
125
+ return deposits.data;
126
+ } catch (error) {
127
+ throw new Error(`Could not retrieve the account deposits ${error}`);
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Get the list of all supported countries by Tropipay.
133
+ * @returns Array of Countries Data
134
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/bfac21259e2ff-getting-users-countries-list
135
+ */
136
+ async countries(): Promise<Country[]> {
137
+ try {
138
+ const countries = await this.request.get("/api/v2/countries");
139
+ return countries.data;
140
+ } catch (error) {
141
+ throw new Error(`Could not retrieve the countries list`);
142
+ }
143
+ }
144
+ /**
145
+ * Get user balance
146
+ * @returns balance Object { balance: number, pendingIn: number, pendingOut: number }
147
+ */
148
+ async getBalance(): Promise<AccountBalance> {
149
+ if (!Tropipay.accessToken) {
150
+ await this.login();
151
+ }
152
+ try {
153
+ const balance = await this.request.get("/api/v2/users/balance", {
154
+ headers: {
155
+ "Content-Type": "application/json",
156
+ Authorization: `Bearer ${Tropipay.accessToken}`,
157
+ Accept: "application/json",
158
+ },
159
+ });
160
+ return balance.data;
161
+ } catch (error) {
162
+ throw new Error(
163
+ `TropipayJS Error - Could not retrieve the user's balance`
164
+ );
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Get the list of all detination countries supported by Tropipay.
170
+ * Obtaining the list of valid countries to send funds to. Useful
171
+ * when adding new beneficiaries to some user.
172
+ *
173
+ * @returns Array of Country Objects
174
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/3cfe5504f0524-getting-list-of-beneficiary-countries
175
+ */
176
+ async destinations(): Promise<Country[]> {
177
+ try {
178
+ const countries = await this.request.get(
179
+ "/api/v2/countries/destinations"
180
+ );
181
+ return countries.data;
182
+ } catch (error) {
183
+ throw new Error(`Could not retrieve the destination countries list`);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Get list of all the favorites payment links.
189
+ * @returns Array of account Object or throws an error
190
+ */
191
+ async favorites() {
192
+ if (!Tropipay.accessToken) {
193
+ await this.login();
194
+ }
195
+ try {
196
+ const favoritesList = await this.request.get(
197
+ "/api/v2/paymentcards/favorites",
198
+ {
199
+ headers: {
200
+ "Content-Type": "application/json",
201
+ Authorization: `Bearer ${Tropipay.accessToken}`,
202
+ Accept: "application/json",
203
+ },
204
+ }
205
+ );
206
+ return favoritesList?.data?.rows;
207
+ } catch (error) {
208
+ throw new Error(`Could not retrieve favorites list ${error}`);
209
+ }
210
+ }
211
+
212
+ /**
213
+ * List all account movements. You can optionaly specify
214
+ * offset and limit params for pagination.
215
+ * @returns
216
+ */
217
+ async movements(offset = 0, limit = 10) {
218
+ if (!Tropipay.accessToken) await this.login();
219
+
220
+ try {
221
+ const movements = await this.request.get("/api/v2/movements", {
222
+ params: { limit: limit, offset: offset },
223
+ headers: {
224
+ "Content-Type": "application/json",
225
+ Accept: "application/json",
226
+ Authorization: `Bearer ${Tropipay.accessToken}`,
227
+ },
228
+ });
229
+ return movements.data;
230
+ } catch (error) {
231
+ throw new Error(`Could not retrieve movements list ${error}`);
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Return profile data for this account.
237
+ * @returns
238
+ */
239
+ async profile() {
240
+ if (!Tropipay.accessToken) await this.login();
241
+ try {
242
+ const profile = await this.request.get("/api/users/profile");
243
+ return profile.data;
244
+ } catch (error) {
245
+ throw new Error(`Could not retrieve movements list ${error}`);
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Obtain current Tropipay conversion rate. For example USD to EUR
251
+ * targetCurrency must be 'EUR'
252
+ * @param originCurrency Target currency code supported by Tropipay.
253
+ * @param targetCurrency Must be 'EUR'? (not documented by Tropipay)
254
+ * @returns Conversion rate (number)
255
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/85163f6f28b23-get-rate
256
+ */
257
+ async rates(
258
+ originCurrency: string,
259
+ targetCurrency: string = "EUR"
260
+ ): Promise<number | Error> {
261
+ if (!Tropipay.accessToken) {
262
+ await this.login();
263
+ }
264
+ try {
265
+ const rates = await this.request.post(
266
+ "/api/v2/movements/get_rate",
267
+ {
268
+ currencyFrom: originCurrency,
269
+ currencyTo: targetCurrency,
270
+ },
271
+ {
272
+ headers: {
273
+ "Content-Type": "application/json",
274
+ Accept: "application/json",
275
+ Authorization: `Bearer ${Tropipay.accessToken}`,
276
+ },
277
+ }
278
+ );
279
+ return rates.data.rate;
280
+ } catch (error) {
281
+ throw new Error(`Could not retrieve rates ${error}`);
282
+ }
283
+ }
284
+
285
+ /**
286
+ * (ONLY in Bussiness Accounts)
287
+ * An escrow payment link. This allows a payment to be made to persons
288
+ * belonging or not to the TropiPay platform with the particularity
289
+ * that the payment will be held in custody or retained until it is
290
+ * released with the approval of the payer.
291
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/12a128ff971e4-creating-a-mediation-payment-card
292
+ * @param config Payload with the payment details
293
+ */
294
+ async createMediationPaymentCard(
295
+ config: mediationPaymentCardConfig
296
+ ): Promise<PaymentLink> {
297
+ if (!Tropipay.accessToken) await this.login();
298
+ try {
299
+ const mediation = await this.request.post(
300
+ "/api/v2/paymentcards/mediation",
301
+ config,
302
+ {
303
+ headers: {
304
+ "Content-Type": "application/json",
305
+ Authorization: `Bearer ${Tropipay.accessToken}`,
306
+ Accept: "application/json",
307
+ },
308
+ }
309
+ );
310
+ return mediation.data as PaymentLink;
311
+ } catch (error) {
312
+ throw new Error(`Could not generate mediation paymentCard ${error}`);
313
+ }
314
+ }
315
+ }
316
+
317
+ export class ClientSideUtils {
318
+ constructor(tropipayInstance: Tropipay) {}
319
+ }
@@ -0,0 +1,2 @@
1
+ import { ServerMode } from "../constants/TropipayConstants";
2
+ export const SERVER_MODE: ServerMode = "Development"; // Move the constant here
@@ -0,0 +1 @@
1
+ export type ServerMode = "Development" | "Production";
@@ -0,0 +1,52 @@
1
+ import { HookEventType, UserHookSubscribed } from "../interfaces";
2
+ import { Tropipay } from "../api/TropipayAPI";
3
+ export class TropipayHooks {
4
+ private tropipay: Tropipay;
5
+ // ... hook-related functionality ...
6
+ constructor(tropipayInstance: Tropipay) {
7
+ this.tropipay = tropipayInstance;
8
+ }
9
+
10
+ public static async subscribeHook(
11
+ eventType: HookEventType,
12
+ target: string,
13
+ value: string
14
+ ) {}
15
+ /**
16
+ * Get hook the sucbcribed hook info by his eventType.
17
+ * If no eventType is passed it will return
18
+ * all subscribed hooks or empty Array if none hooks exist.
19
+ * @param eventType or no params for retrieving all hooks
20
+ * @returns
21
+ */
22
+ public async getSubscribedHook(
23
+ eventType?: HookEventType
24
+ ): Promise<UserHookSubscribed[]> {
25
+ if (!Tropipay.accessToken) {
26
+ await this.tropipay.login();
27
+ }
28
+ try {
29
+ const hooks = await this.tropipay.request.get(
30
+ `/api/v2/hooks/${eventType || ""}`,
31
+ {
32
+ headers: {
33
+ "Content-Type": "application/json",
34
+ Authorization: `Bearer ${Tropipay.accessToken}`,
35
+ Accept: "application/json",
36
+ },
37
+ }
38
+ );
39
+ return hooks.data;
40
+ } catch (error) {
41
+ throw new Error(`Could not get subscribed hooks ${error}`);
42
+ }
43
+ }
44
+ public async updateSubscribedHook(eventType?: string) {
45
+ //TODO
46
+ throw new Error("Not implementet yet");
47
+ }
48
+ async deleteSubscribedHook(eventType?: string) {
49
+ //TODO
50
+ throw new Error("Not implementet yet");
51
+ }
52
+ }