@yosle/tropipayjs 0.1.7 → 0.1.9

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/src/index.ts CHANGED
@@ -7,27 +7,34 @@
7
7
 
8
8
  import axios, { Axios } from "axios";
9
9
  import { PaymentLink, PaymentLinkPayload } from './interfaces/paymentlink'
10
-
10
+ import { mediationPaymentCardConfig } from "./interfaces/mediation";
11
11
 
12
12
  type ServerMode = 'Development' | 'Production'
13
13
 
14
+ interface TropipayConfig {
15
+ clientId: string;
16
+ clientSecret: string;
17
+ serverMode?: ServerMode;
18
+ }
19
+
20
+ type AccountBalance = {
21
+ balance: number,
22
+ pendingIn: number,
23
+ pendingOut: number
24
+ }
14
25
 
15
26
  export class Tropipay {
16
- readonly clientId: string;
17
- readonly clientSecret: string;
27
+ protected clientId: string;
28
+ protected clientSecret: string;
18
29
  protected request: Axios;
19
- protected accessToken: string | undefined;
20
- protected refreshToken: string | undefined;
30
+ protected static accessToken: string | undefined;
31
+ protected static refreshToken: string | undefined;
21
32
  protected serverMode: ServerMode;
22
33
 
23
- constructor(
24
- client_id: string,
25
- client_secret: string,
26
- server_mode: ServerMode = 'Development') {
27
- this.clientId = client_id;
28
- this.clientSecret = client_secret
29
- this.serverMode = server_mode
30
-
34
+ constructor(config: TropipayConfig) {
35
+ this.clientId = config.clientId;
36
+ this.clientSecret = config.clientSecret;
37
+ this.serverMode = config.serverMode || 'Development';
31
38
  this.request = axios.create({
32
39
  baseURL: this.serverMode === 'Production'
33
40
  ? 'https://www.tropipay.com'
@@ -35,10 +42,9 @@ export class Tropipay {
35
42
  headers: {
36
43
  'Content-Type': 'application/json',
37
44
  Accept: 'application/json',
38
- Authorization: `Bearer ${this.accessToken}`,
39
-
40
- }
41
- })
45
+ Authorization: `Bearer ${Tropipay.accessToken}`,
46
+ }
47
+ });
42
48
  }
43
49
 
44
50
 
@@ -50,20 +56,20 @@ export class Tropipay {
50
56
  grant_type: "client_credentials",
51
57
  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"
52
58
  }, {
53
- // headers: {
54
- // 'Content-Type': 'application/json',
55
- // Accept: 'application/json',
56
- // }
57
- })
59
+ headers: {
60
+ 'Content-Type': 'application/json',
61
+ Accept: 'application/json',
62
+ }
63
+ });
58
64
 
59
- this.accessToken = data.access_token
60
- this.refreshToken = data.refresh_token
65
+ Tropipay.accessToken = data.access_token
66
+ Tropipay.refreshToken = data.refresh_token
61
67
  return data
62
68
  } catch (error) {
63
69
  if (axios.isAxiosError(error)) {
64
70
  throw new Error(`Could not obtain the access token from credentials ${error}`);
65
- }
66
- throw new Error(`Could not obtain the access token from credentials ${error}`);
71
+ }
72
+ throw new Error(`Could not obtain the access token from credentials ${error}`);
67
73
 
68
74
  }
69
75
  }
@@ -74,22 +80,21 @@ export class Tropipay {
74
80
  * @returns Promise<PaymentLink> or throws an Exception.
75
81
  * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6ODgyNTM3OQ-create-a-new-pay-link-charge
76
82
  */
77
- async createPayLink(payload: PaymentLinkPayload): Promise<PaymentLink> {
78
- if (!this.accessToken) {
83
+ async createPaymentCard(payload: PaymentLinkPayload): Promise<PaymentLink> {
84
+ if (!Tropipay.accessToken) {
79
85
  await this.login()
80
86
  }
81
87
  try {
82
88
  const paylink = await this.request.post('/api/v2/paymentcards', payload, {
83
89
  headers: {
84
- // 'Content-Type': 'application/json',
85
- Authorization: `Bearer ${this.accessToken}`,
86
- // Accept: 'application/json'
90
+ 'Content-Type': 'application/json',
91
+ Authorization: `Bearer ${Tropipay.accessToken}`,
92
+ Accept: 'application/json'
87
93
  }
88
94
  })
89
95
  return paylink.data as PaymentLink
90
96
  } catch (error) {
91
97
  throw new Error(`Could not obtain the access token with the given credentials.`);
92
-
93
98
  }
94
99
  }
95
100
 
@@ -98,15 +103,21 @@ export class Tropipay {
98
103
  * @returns A Promise of an Array of AccountDeposits or throws an Exception
99
104
  * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6OTgyOTQ1Mg-get-deposit-accounts-list
100
105
  */
101
- async getDepositAccounts(): Promise<AccountDeposits[] | Error>{
102
- if (!this.accessToken) {
106
+ async getDepositAccounts(): Promise<AccountDeposits[] | Error> {
107
+ if (!Tropipay.accessToken) {
103
108
  await this.login()
104
109
  }
105
110
  try {
106
- const deposits = await this.request.get('/api/v2/deposit_accounts',{ })
111
+ const deposits = await this.request.get('/api/v2/deposit_accounts', {
112
+ headers: {
113
+ 'Content-Type': 'application/json',
114
+ Authorization: `Bearer ${Tropipay.accessToken}`,
115
+ Accept: 'application/json'
116
+ }
117
+ })
107
118
  return deposits.data
108
119
  } catch (error) {
109
- throw new Error(`Could not retrieve the account deposits ${error}`);
120
+ throw new Error(`Could not retrieve the account deposits ${error}`);
110
121
  }
111
122
  }
112
123
 
@@ -115,16 +126,35 @@ export class Tropipay {
115
126
  * @returns Array of Countries Data
116
127
  * @see https://tpp.stoplight.io/docs/tropipay-api-doc/bfac21259e2ff-getting-users-countries-list
117
128
  */
118
- async countries(): Promise<Country[] > {
119
-
129
+ async countries(): Promise<Country[]> {
120
130
  try {
121
- const countries = await this.request.get('/api/v2/countries')
131
+ const countries = await this.request.get('/api/v2/countries')
122
132
  return countries.data
123
133
  } catch (error) {
124
- throw new Error(`Could not retrieve the countries list`);
134
+ throw new Error(`TropipayJS Error - Could not retrieve the countries list`);
135
+ }
136
+ }
137
+ /**
138
+ * Get user balance
139
+ * @returns balance Object { balance: number, pendingIn: number, pendingOut: number }
140
+ */
141
+ async getBalance(): Promise<AccountBalance> {
142
+ if (!Tropipay.accessToken) {
143
+ await this.login()
144
+ }
145
+ try {
146
+ const balance = await this.request.get('/api/v2/users/balance', {
147
+ headers: {
148
+ 'Content-Type': 'application/json',
149
+ Authorization: `Bearer ${Tropipay.accessToken}`,
150
+ Accept: 'application/json'
151
+ }
152
+ });
153
+ return balance.data
154
+ } catch (error) {
155
+ throw new Error(`TropipayJS Error - Could not retrieve the user's balance`);
125
156
  }
126
157
  }
127
-
128
158
 
129
159
  /**
130
160
  * Get the list of all detination countries supported by Tropipay.
@@ -136,28 +166,32 @@ export class Tropipay {
136
166
  */
137
167
  async destinations(): Promise<Country[]> {
138
168
  try {
139
- const countries = await this.request.get('/api/v2/countries/destinations')
169
+ const countries = await this.request.get('/api/v2/countries/destinations')
140
170
  return countries.data
141
171
  } catch (error) {
142
- throw new Error(`Could not retrieve the destination countries list`);
172
+ throw new Error(`Could not retrieve the destination countries list`);
143
173
  }
144
174
  }
145
175
 
146
176
  /**
147
- * Get list of all the favorites accounts. This endpoint is not documented
148
- * in the official Tropipay documentation.
149
- * @returns
177
+ * Get list of all the favorites payment links.
178
+ * @returns Array of account Object or throws an error
150
179
  */
151
- async favorites(){
152
- if (!this.accessToken) {
180
+ async favorites() {
181
+ if (!Tropipay.accessToken) {
153
182
  await this.login()
154
183
  }
155
184
  try {
156
- const favoritesList = await this.request.get('/api/v2/paymentcards/favorites')
157
- console.log(favoritesList)
158
- return favoritesList.data
159
- } catch (error) {
160
- throw new Error(`Could not retrieve favorites list ${error}`);
185
+ const favoritesList = await this.request.get('/api/v2/paymentcards/favorites', {
186
+ headers: {
187
+ 'Content-Type': 'application/json',
188
+ Authorization: `Bearer ${Tropipay.accessToken}`,
189
+ Accept: 'application/json'
190
+ }
191
+ });
192
+ return favoritesList?.data?.rows
193
+ } catch (error) {
194
+ throw new Error(`Could not retrieve favorites list ${error}`);
161
195
  }
162
196
  }
163
197
 
@@ -166,41 +200,39 @@ export class Tropipay {
166
200
  * offset and limit params for pagination.
167
201
  * @returns
168
202
  */
169
- async movements(offset = 0 , limit = 10){
170
- if (!this.accessToken) {
171
- await this.login()
172
- }
173
- try {
174
- const movements = await this.request.get('/api/v2/movements',
175
- {params: {limit: limit, offset: offset},
176
- headers: {
177
- 'Content-Type': 'application/json',
178
- Accept: 'application/json',
179
- Authorization: `Bearer ${this.accessToken}`,
203
+ async movements(offset = 0, limit = 10) {
204
+ if (!Tropipay.accessToken) await this.login()
180
205
 
181
- } })
182
- return movements.data
206
+ try {
207
+ const movements = await this.request.get('/api/v2/movements',
208
+ {
209
+ params: { limit: limit, offset: offset },
210
+ headers: {
211
+ 'Content-Type': 'application/json',
212
+ Accept: 'application/json',
213
+ Authorization: `Bearer ${Tropipay.accessToken}`
214
+ }
215
+ })
216
+ return movements.data
183
217
  } catch (error) {
184
- throw new Error(`Could not retrieve movements list`);
218
+ throw new Error(`Could not retrieve movements list ${error}`);
185
219
  }
186
-
187
220
  }
188
221
 
189
222
  /**
190
223
  * Return profile data for this account.
191
224
  * @returns
192
225
  */
193
- async profile(){
194
- if (!this.accessToken) {
195
- await this.login()
196
- }
226
+ async profile() {
227
+ if (!Tropipay.accessToken) await this.login();
197
228
  try {
198
- const profile = await this.request.get('/api/users/profile')
199
- return profile.data
229
+ const profile = await this.request.get('/api/users/profile')
230
+ return profile.data
200
231
  } catch (error) {
201
- throw new Error(`Could not retrieve movements list ${error}`);
232
+ throw new Error(`Could not retrieve movements list ${error}`);
202
233
  }
203
234
  }
235
+
204
236
  /**
205
237
  * Obtain current Tropipay conversion rate. For example USD to EUR
206
238
  * targetCurrency must be 'EUR'
@@ -209,28 +241,56 @@ export class Tropipay {
209
241
  * @returns Conversion rate (number)
210
242
  * @see https://tpp.stoplight.io/docs/tropipay-api-doc/85163f6f28b23-get-rate
211
243
  */
212
- async rates(originCurrency: string , targetCurrency: string = 'EUR'): Promise<number | Error> {
213
- if (!this.accessToken) {
244
+ async rates(originCurrency: string, targetCurrency: string = 'EUR'): Promise<number | Error> {
245
+ if (!Tropipay.accessToken) {
214
246
  await this.login()
215
247
  }
216
- try {
217
- const rates = await this.request.post('/api/v2/movements/get_rate',
218
- {
219
- currencyFrom: originCurrency,
220
- currencyTo: targetCurrency
221
- },{
222
- headers: {
223
- 'Content-Type': 'application/json',
224
- Accept: 'application/json',
225
- Authorization: `Bearer ${this.accessToken}`,
226
-
227
- } })
228
- return rates.data.rate
229
- } catch (error) {
230
- throw new Error(`Could not retrieve rates ${error}`);
248
+ try {
249
+ const rates = await this.request.post('/api/v2/movements/get_rate',
250
+ {
251
+ currencyFrom: originCurrency,
252
+ currencyTo: targetCurrency
253
+ }, {
254
+ headers: {
255
+ 'Content-Type': 'application/json',
256
+ Accept: 'application/json',
257
+ Authorization: `Bearer ${Tropipay.accessToken}`,
258
+
259
+ }
260
+ })
261
+ return rates.data.rate
262
+ } catch (error) {
263
+ throw new Error(`Could not retrieve rates ${error}`);
264
+ }
265
+
266
+ }
267
+
268
+ /**
269
+ * (ONLY in Bussiness Accounts)
270
+ * An escrow payment link. This allows a payment to be made to persons
271
+ * belonging or not to the TropiPay platform with the particularity
272
+ * that the payment will be held in custody or retained until it is
273
+ * released with the approval of the payer.
274
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/12a128ff971e4-creating-a-mediation-payment-card
275
+ * @param config Payload with the payment details
276
+ */
277
+ async createMediationPaymentCard(config: mediationPaymentCardConfig): Promise<PaymentLink> {
278
+ if (!Tropipay.accessToken) await this.login();
279
+ try {
280
+ const mediation = await this.request.post('/api/v2/paymentcards/mediation', config, {
281
+ headers: {
282
+ 'Content-Type': 'application/json',
283
+ Authorization: `Bearer ${Tropipay.accessToken}`,
284
+ Accept: 'application/json'
285
+ }
286
+ })
287
+ return mediation.data as PaymentLink;
288
+ } catch (error) {
289
+ throw new Error(`Could not generate mediation paymentCard ${error}`);
290
+ }
291
+
292
+
231
293
  }
232
-
233
- }
234
294
 
235
295
  }
236
296
 
@@ -0,0 +1,23 @@
1
+
2
+ /**
3
+ *
4
+ */
5
+ export interface mediationPaymentCardConfig {
6
+ amount: number //5000,
7
+ currency: "EUR" | "USD",
8
+ concept: string //Celular,
9
+ description: string,
10
+ reference: string, //458424548,
11
+ singleUse: boolean,
12
+ lang: string// es,
13
+ productUrl?: string,
14
+ buyer: null | any //null,
15
+ seller: {
16
+ sellerId?: number,
17
+ type?: number,
18
+ email?: string
19
+ },
20
+ feePercent?: number,//in cents
21
+ feeFixed?: number,
22
+ sendMail: boolean
23
+ }
package/docs/README.MD DELETED
@@ -1,4 +0,0 @@
1
- Documentation
2
- =============
3
-
4
- This will be added eventually.