@yosle/tropipayjs 0.1.9 → 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/index.d.ts CHANGED
@@ -1,5 +1,41 @@
1
1
  import { Axios } from 'axios';
2
2
 
3
+ type ServerMode$1 = "Development" | "Production";
4
+
5
+ type AccountBalance = {
6
+ balance: number;
7
+ pendingIn: number;
8
+ pendingOut: number;
9
+ };
10
+ interface TropipayConfig {
11
+ clientId: string;
12
+ clientSecret: string;
13
+ serverMode?: ServerMode$1;
14
+ }
15
+ type TropipayCredentials = {
16
+ clientId: string;
17
+ clientSecret: string;
18
+ };
19
+ type HookEventType = "transaction_completed" | "transaction_charged" | "transaction_guarded";
20
+ type HookTargetType = "web" | "email";
21
+ interface UserHook {
22
+ event: HookEventType;
23
+ target: string;
24
+ value: string;
25
+ }
26
+ interface UserHook {
27
+ event: HookEventType;
28
+ target: string;
29
+ value: string;
30
+ }
31
+ interface UserHookSubscribed extends UserHook {
32
+ createdAt: string;
33
+ updatedAt: string;
34
+ }
35
+ interface UserHookSubscribed extends UserHook {
36
+ createdAt: string;
37
+ updatedAt: string;
38
+ }
3
39
  interface PaymentLinkPayload {
4
40
  reference: string;
5
41
  concept: string;
@@ -36,10 +72,6 @@ interface PaymentLink extends PaymentLinkPayload {
36
72
  shortUrl: string;
37
73
  paymentUrl: string;
38
74
  }
39
-
40
- /**
41
- *
42
- */
43
75
  interface mediationPaymentCardConfig {
44
76
  amount: number;
45
77
  currency: "EUR" | "USD";
@@ -59,6 +91,91 @@ interface mediationPaymentCardConfig {
59
91
  feeFixed?: number;
60
92
  sendMail: boolean;
61
93
  }
94
+ type LoginResponse = {
95
+ access_token: string;
96
+ refresh_token: string;
97
+ token_type: "Bearer";
98
+ expires_in: number;
99
+ scope: string;
100
+ };
101
+ type LoginError = {
102
+ error: string;
103
+ };
104
+ type Country = {
105
+ id: number;
106
+ name: string;
107
+ sepaZone: boolean;
108
+ state: number;
109
+ slug: string;
110
+ slugn: number;
111
+ callingCode: number;
112
+ isDestination: boolean;
113
+ isRisky: boolean;
114
+ currentCurrency: string | null;
115
+ createdAt: string;
116
+ updatedAt: string;
117
+ isFavorite: boolean;
118
+ position: any;
119
+ };
120
+ type Deposit = {
121
+ id: number;
122
+ accountNumber: string;
123
+ alias: string;
124
+ swift: string;
125
+ type: number;
126
+ country: number | null;
127
+ firstName: string;
128
+ default: null;
129
+ state: number;
130
+ userId: string;
131
+ countryDestinationId: number;
132
+ lastName: string;
133
+ documentNumber: number;
134
+ userRelationTypeId: number;
135
+ city: string;
136
+ postalCode: string;
137
+ address: string;
138
+ phone: string;
139
+ checked: boolean;
140
+ province: string;
141
+ beneficiaryType: number;
142
+ relatedUserId: null | string;
143
+ currency: string;
144
+ correspondent?: any;
145
+ location: any;
146
+ office: any;
147
+ officeValue: any;
148
+ paymentType: number;
149
+ paymentEntityBeneficiaryId: number;
150
+ paymentEntityAccountId: number;
151
+ verified: any;
152
+ paymentEntityInfo: any;
153
+ documentTypeId: any;
154
+ documentExpirationDate: Date;
155
+ createdAt: Date;
156
+ updatedAt: Date;
157
+ countryDestination: Country;
158
+ };
159
+ type AccountDeposits = {
160
+ count: number;
161
+ rows: Deposit[];
162
+ };
163
+
164
+ declare class TropipayHooks {
165
+ private tropipay;
166
+ constructor(tropipayInstance: Tropipay);
167
+ static subscribeHook(eventType: HookEventType, target: string, value: string): Promise<void>;
168
+ /**
169
+ * Get hook the sucbcribed hook info by his eventType.
170
+ * If no eventType is passed it will return
171
+ * all subscribed hooks or empty Array if none hooks exist.
172
+ * @param eventType or no params for retrieving all hooks
173
+ * @returns
174
+ */
175
+ getSubscribedHook(eventType?: HookEventType): Promise<UserHookSubscribed[]>;
176
+ updateSubscribedHook(eventType?: string): Promise<void>;
177
+ deleteSubscribedHook(eventType?: string): Promise<void>;
178
+ }
62
179
 
63
180
  /**
64
181
  * Tropipayjs is a Typescript/Javascript library for the Tropipay API.
@@ -67,24 +184,16 @@ interface mediationPaymentCardConfig {
67
184
  *
68
185
  */
69
186
 
70
- type ServerMode = 'Development' | 'Production';
71
- interface TropipayConfig {
72
- clientId: string;
73
- clientSecret: string;
74
- serverMode?: ServerMode;
75
- }
76
- type AccountBalance = {
77
- balance: number;
78
- pendingIn: number;
79
- pendingOut: number;
80
- };
187
+ type ServerMode = "Development" | "Production";
188
+
81
189
  declare class Tropipay {
82
- protected clientId: string;
83
- protected clientSecret: string;
84
- protected request: Axios;
85
- protected static accessToken: string | undefined;
86
- protected static refreshToken: string | undefined;
87
- protected serverMode: ServerMode;
190
+ readonly clientId: string;
191
+ readonly clientSecret: string;
192
+ request: Axios;
193
+ static accessToken: string | undefined;
194
+ static refreshToken: string | undefined;
195
+ serverMode: ServerMode;
196
+ hooks: TropipayHooks;
88
197
  constructor(config: TropipayConfig);
89
198
  login(): Promise<LoginResponse>;
90
199
  /**
@@ -108,7 +217,7 @@ declare class Tropipay {
108
217
  countries(): Promise<Country[]>;
109
218
  /**
110
219
  * Get user balance
111
- * @returns balance object
220
+ * @returns balance Object { balance: number, pendingIn: number, pendingOut: number }
112
221
  */
113
222
  getBalance(): Promise<AccountBalance>;
114
223
  /**
@@ -156,5 +265,27 @@ declare class Tropipay {
156
265
  */
157
266
  createMediationPaymentCard(config: mediationPaymentCardConfig): Promise<PaymentLink>;
158
267
  }
268
+ declare class ClientSideUtils {
269
+ constructor(tropipayInstance: Tropipay);
270
+ }
271
+
272
+ declare class ServerSideUtils {
273
+ private tropipay;
274
+ constructor(tropipayInstance: Tropipay);
275
+ /**
276
+ * Verify Topipay's signature on webhooks.
277
+ * @param credentials Credential object or Tropipay instance
278
+ * @param {String} originalCurrencyAmount
279
+ * @param bankOrderCode
280
+ * @param signature
281
+ * @returns {Boolean}
282
+ */
283
+ static verifySignature(credentials: {
284
+ clientId: string;
285
+ clientSecret: string;
286
+ } | Tropipay, originalCurrencyAmount: string, bankOrderCode: string, signature: string): boolean;
287
+ }
288
+
289
+ declare const SERVER_MODE: ServerMode$1;
159
290
 
160
- export { Tropipay };
291
+ export { AccountBalance, AccountDeposits, ClientSideUtils, Country, Deposit, HookEventType, HookTargetType, LoginError, LoginResponse, PaymentLink, PaymentLinkPayload, SERVER_MODE, ServerMode$1 as ServerMode, ServerSideUtils, Tropipay, TropipayConfig, TropipayCredentials, TropipayHooks, UserHook, UserHookSubscribed, mediationPaymentCardConfig };
package/index.js CHANGED
@@ -3,10 +3,72 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var axios = require('axios');
6
+ var crypto = require('crypto');
6
7
 
7
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
9
 
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n["default"] = e;
25
+ return Object.freeze(n);
26
+ }
27
+
9
28
  var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
29
+ var crypto__namespace = /*#__PURE__*/_interopNamespace(crypto);
30
+
31
+ class TropipayHooks {
32
+ tropipay;
33
+ // ... hook-related functionality ...
34
+ constructor(tropipayInstance) {
35
+ this.tropipay = tropipayInstance;
36
+ }
37
+ static async subscribeHook(eventType, target, value) { }
38
+ /**
39
+ * Get hook the sucbcribed hook info by his eventType.
40
+ * If no eventType is passed it will return
41
+ * all subscribed hooks or empty Array if none hooks exist.
42
+ * @param eventType or no params for retrieving all hooks
43
+ * @returns
44
+ */
45
+ async getSubscribedHook(eventType) {
46
+ if (!Tropipay.accessToken) {
47
+ await this.tropipay.login();
48
+ }
49
+ try {
50
+ const hooks = await this.tropipay.request.get(`/api/v2/hooks/${eventType || ""}`, {
51
+ headers: {
52
+ "Content-Type": "application/json",
53
+ Authorization: `Bearer ${Tropipay.accessToken}`,
54
+ Accept: "application/json",
55
+ },
56
+ });
57
+ return hooks.data;
58
+ }
59
+ catch (error) {
60
+ throw new Error(`Could not get subscribed hooks ${error}`);
61
+ }
62
+ }
63
+ async updateSubscribedHook(eventType) {
64
+ //TODO
65
+ throw new Error("Not implementet yet");
66
+ }
67
+ async deleteSubscribedHook(eventType) {
68
+ //TODO
69
+ throw new Error("Not implementet yet");
70
+ }
71
+ }
10
72
 
11
73
  /**
12
74
  * Tropipayjs is a Typescript/Javascript library for the Tropipay API.
@@ -21,33 +83,35 @@ class Tropipay {
21
83
  static accessToken;
22
84
  static refreshToken;
23
85
  serverMode;
86
+ hooks;
24
87
  constructor(config) {
25
88
  this.clientId = config.clientId;
26
89
  this.clientSecret = config.clientSecret;
27
- this.serverMode = config.serverMode || 'Development';
90
+ this.serverMode = config.serverMode || "Development";
28
91
  this.request = axios__default["default"].create({
29
- baseURL: this.serverMode === 'Production'
30
- ? 'https://www.tropipay.com'
31
- : 'https://tropipay-dev.herokuapp.com',
92
+ baseURL: this.serverMode === "Production"
93
+ ? "https://www.tropipay.com"
94
+ : "https://tropipay-dev.herokuapp.com",
32
95
  headers: {
33
- 'Content-Type': 'application/json',
34
- Accept: 'application/json',
96
+ "Content-Type": "application/json",
97
+ Accept: "application/json",
35
98
  Authorization: `Bearer ${Tropipay.accessToken}`,
36
- }
99
+ },
37
100
  });
101
+ this.hooks = new TropipayHooks(this);
38
102
  }
39
103
  async login() {
40
104
  try {
41
- const { data } = await this.request.post('/api/v2/access/token', {
105
+ const { data } = await this.request.post("/api/v2/access/token", {
42
106
  client_id: this.clientId,
43
107
  client_secret: this.clientSecret,
44
108
  grant_type: "client_credentials",
45
- 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"
109
+ 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",
46
110
  }, {
47
111
  headers: {
48
- 'Content-Type': 'application/json',
49
- Accept: 'application/json',
50
- }
112
+ "Content-Type": "application/json",
113
+ Accept: "application/json",
114
+ },
51
115
  });
52
116
  Tropipay.accessToken = data.access_token;
53
117
  Tropipay.refreshToken = data.refresh_token;
@@ -71,12 +135,12 @@ class Tropipay {
71
135
  await this.login();
72
136
  }
73
137
  try {
74
- const paylink = await this.request.post('/api/v2/paymentcards', payload, {
138
+ const paylink = await this.request.post("/api/v2/paymentcards", payload, {
75
139
  headers: {
76
- 'Content-Type': 'application/json',
140
+ "Content-Type": "application/json",
77
141
  Authorization: `Bearer ${Tropipay.accessToken}`,
78
- Accept: 'application/json'
79
- }
142
+ Accept: "application/json",
143
+ },
80
144
  });
81
145
  return paylink.data;
82
146
  }
@@ -94,12 +158,12 @@ class Tropipay {
94
158
  await this.login();
95
159
  }
96
160
  try {
97
- const deposits = await this.request.get('/api/v2/deposit_accounts', {
161
+ const deposits = await this.request.get("/api/v2/deposit_accounts", {
98
162
  headers: {
99
- 'Content-Type': 'application/json',
163
+ "Content-Type": "application/json",
100
164
  Authorization: `Bearer ${Tropipay.accessToken}`,
101
- Accept: 'application/json'
102
- }
165
+ Accept: "application/json",
166
+ },
103
167
  });
104
168
  return deposits.data;
105
169
  }
@@ -114,33 +178,32 @@ class Tropipay {
114
178
  */
115
179
  async countries() {
116
180
  try {
117
- const countries = await this.request.get('/api/v2/countries');
181
+ const countries = await this.request.get("/api/v2/countries");
118
182
  return countries.data;
119
183
  }
120
184
  catch (error) {
121
- throw new Error(`TropipayJS Error - Could not retrieve the countries list`);
185
+ throw new Error(`Could not retrieve the countries list`);
122
186
  }
123
187
  }
124
188
  /**
125
189
  * Get user balance
126
- * @returns balance object
190
+ * @returns balance Object { balance: number, pendingIn: number, pendingOut: number }
127
191
  */
128
192
  async getBalance() {
129
193
  if (!Tropipay.accessToken) {
130
194
  await this.login();
131
195
  }
132
196
  try {
133
- const balance = await this.request.get('/api/v2/users/balance', {
197
+ const balance = await this.request.get("/api/v2/users/balance", {
134
198
  headers: {
135
- 'Content-Type': 'application/json',
199
+ "Content-Type": "application/json",
136
200
  Authorization: `Bearer ${Tropipay.accessToken}`,
137
- Accept: 'application/json'
138
- }
201
+ Accept: "application/json",
202
+ },
139
203
  });
140
204
  return balance.data;
141
205
  }
142
206
  catch (error) {
143
- console.log("el error es ", error);
144
207
  throw new Error(`TropipayJS Error - Could not retrieve the user's balance`);
145
208
  }
146
209
  }
@@ -154,7 +217,7 @@ class Tropipay {
154
217
  */
155
218
  async destinations() {
156
219
  try {
157
- const countries = await this.request.get('/api/v2/countries/destinations');
220
+ const countries = await this.request.get("/api/v2/countries/destinations");
158
221
  return countries.data;
159
222
  }
160
223
  catch (error) {
@@ -170,12 +233,12 @@ class Tropipay {
170
233
  await this.login();
171
234
  }
172
235
  try {
173
- const favoritesList = await this.request.get('/api/v2/paymentcards/favorites', {
236
+ const favoritesList = await this.request.get("/api/v2/paymentcards/favorites", {
174
237
  headers: {
175
- 'Content-Type': 'application/json',
238
+ "Content-Type": "application/json",
176
239
  Authorization: `Bearer ${Tropipay.accessToken}`,
177
- Accept: 'application/json'
178
- }
240
+ Accept: "application/json",
241
+ },
179
242
  });
180
243
  return favoritesList?.data?.rows;
181
244
  }
@@ -192,13 +255,13 @@ class Tropipay {
192
255
  if (!Tropipay.accessToken)
193
256
  await this.login();
194
257
  try {
195
- const movements = await this.request.get('/api/v2/movements', {
258
+ const movements = await this.request.get("/api/v2/movements", {
196
259
  params: { limit: limit, offset: offset },
197
260
  headers: {
198
- 'Content-Type': 'application/json',
199
- Accept: 'application/json',
200
- Authorization: `Bearer ${Tropipay.accessToken}`
201
- }
261
+ "Content-Type": "application/json",
262
+ Accept: "application/json",
263
+ Authorization: `Bearer ${Tropipay.accessToken}`,
264
+ },
202
265
  });
203
266
  return movements.data;
204
267
  }
@@ -214,7 +277,7 @@ class Tropipay {
214
277
  if (!Tropipay.accessToken)
215
278
  await this.login();
216
279
  try {
217
- const profile = await this.request.get('/api/users/profile');
280
+ const profile = await this.request.get("/api/users/profile");
218
281
  return profile.data;
219
282
  }
220
283
  catch (error) {
@@ -229,20 +292,20 @@ class Tropipay {
229
292
  * @returns Conversion rate (number)
230
293
  * @see https://tpp.stoplight.io/docs/tropipay-api-doc/85163f6f28b23-get-rate
231
294
  */
232
- async rates(originCurrency, targetCurrency = 'EUR') {
295
+ async rates(originCurrency, targetCurrency = "EUR") {
233
296
  if (!Tropipay.accessToken) {
234
297
  await this.login();
235
298
  }
236
299
  try {
237
- const rates = await this.request.post('/api/v2/movements/get_rate', {
300
+ const rates = await this.request.post("/api/v2/movements/get_rate", {
238
301
  currencyFrom: originCurrency,
239
- currencyTo: targetCurrency
302
+ currencyTo: targetCurrency,
240
303
  }, {
241
304
  headers: {
242
- 'Content-Type': 'application/json',
243
- Accept: 'application/json',
305
+ "Content-Type": "application/json",
306
+ Accept: "application/json",
244
307
  Authorization: `Bearer ${Tropipay.accessToken}`,
245
- }
308
+ },
246
309
  });
247
310
  return rates.data.rate;
248
311
  }
@@ -263,12 +326,12 @@ class Tropipay {
263
326
  if (!Tropipay.accessToken)
264
327
  await this.login();
265
328
  try {
266
- const mediation = await this.request.post('/api/v2/paymentcards/mediation', config, {
329
+ const mediation = await this.request.post("/api/v2/paymentcards/mediation", config, {
267
330
  headers: {
268
- 'Content-Type': 'application/json',
331
+ "Content-Type": "application/json",
269
332
  Authorization: `Bearer ${Tropipay.accessToken}`,
270
- Accept: 'application/json'
271
- }
333
+ Accept: "application/json",
334
+ },
272
335
  });
273
336
  return mediation.data;
274
337
  }
@@ -277,6 +340,43 @@ class Tropipay {
277
340
  }
278
341
  }
279
342
  }
343
+ class ClientSideUtils {
344
+ constructor(tropipayInstance) { }
345
+ }
346
+
347
+ class ServerSideUtils {
348
+ tropipay;
349
+ constructor(tropipayInstance) {
350
+ this.tropipay = tropipayInstance;
351
+ }
352
+ /**
353
+ * Verify Topipay's signature on webhooks.
354
+ * @param credentials Credential object or Tropipay instance
355
+ * @param {String} originalCurrencyAmount
356
+ * @param bankOrderCode
357
+ * @param signature
358
+ * @returns {Boolean}
359
+ */
360
+ static verifySignature(credentials, originalCurrencyAmount, bankOrderCode, signature) {
361
+ const localSignature = crypto__namespace
362
+ .createHash("sha256")
363
+ .update(bankOrderCode +
364
+ credentials.clientId +
365
+ crypto__namespace
366
+ .createHash("sha1")
367
+ .update(credentials.clientSecret)
368
+ .digest("hex") +
369
+ originalCurrencyAmount)
370
+ .digest("hex");
371
+ return localSignature === signature;
372
+ }
373
+ }
374
+
375
+ const SERVER_MODE = "Development"; // Move the constant here
280
376
 
377
+ exports.ClientSideUtils = ClientSideUtils;
378
+ exports.SERVER_MODE = SERVER_MODE;
379
+ exports.ServerSideUtils = ServerSideUtils;
281
380
  exports.Tropipay = Tropipay;
381
+ exports.TropipayHooks = TropipayHooks;
282
382
  //# sourceMappingURL=index.js.map