@yosle/tropipayjs 0.1.6 → 0.1.8

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