@yosle/tropipayjs 0.1.5 → 0.1.7

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.
@@ -0,0 +1,74 @@
1
+ type LoginResponse = {
2
+ access_token: string,
3
+ refresh_token: string
4
+ token_type: "Bearer",
5
+ expires_in: number,
6
+ scope: string
7
+ }
8
+
9
+ type LoginError = {
10
+ error: string
11
+ }
12
+
13
+ type Country = {
14
+ id: number,
15
+ name: string,
16
+ sepaZone: boolean,
17
+ state: number,
18
+ slug: string,
19
+ slugn: number,
20
+ callingCode: number,
21
+ isDestination: boolean,
22
+ isRisky: boolean,
23
+ currentCurrency: string | null,
24
+ createdAt: string,
25
+ updatedAt: string,
26
+ isFavorite: boolean,
27
+ position: any
28
+
29
+ }
30
+
31
+ type Deposit = {
32
+ id: number,
33
+ accountNumber: string,
34
+ alias: string,
35
+ swift: string,
36
+ type: number,
37
+ country: number | null,
38
+ firstName: string,
39
+ default: null,
40
+ state: number,
41
+ userId: string,
42
+ countryDestinationId: number,
43
+ lastName: string,
44
+ documentNumber: number,
45
+ userRelationTypeId: number,
46
+ city: string,
47
+ postalCode: string,
48
+ address: string,
49
+ phone: string,
50
+ checked: boolean,
51
+ province: string,
52
+ beneficiaryType: number,
53
+ relatedUserId: null | string,
54
+ currency: string,
55
+ correspondent?: any,
56
+ location: any,
57
+ office: any,
58
+ officeValue: any,
59
+ paymentType: number,
60
+ paymentEntityBeneficiaryId: number,
61
+ paymentEntityAccountId: number,
62
+ verified: any,
63
+ paymentEntityInfo: any,
64
+ documentTypeId: any,
65
+ documentExpirationDate: Date,
66
+ createdAt: Date<string>,
67
+ updatedAt: Date<string>,
68
+ countryDestination: Country
69
+ }
70
+
71
+ type AccountDeposits = {
72
+ count: number,
73
+ rows: Deposit[]
74
+ }
@@ -1,230 +1,239 @@
1
- /**
2
- * Tropipayjs is a Typescript/Javascript library for the Tropipay API. CommonJs and
3
- * ES6 modules are supported.
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
- // 👇️ error: AxiosError<any, any>
65
- throw new Error("Conection error: " + error.message);
66
- }
67
- throw new Error("Could not obtain the access token from credentials ");
68
-
69
- }
70
- }
71
-
72
- /**
73
- * Create a paymentLink with the specified options.
74
- * @param payload PaymentLinkPayload Object.
75
- * @returns Promise<PaymentLink> or throws an Exception.
76
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6ODgyNTM3OQ-create-a-new-pay-link-charge
77
- */
78
- async createPayLink(payload: PaymentLinkPayload): Promise<PaymentLink> {
79
- if (!this.accessToken) {
80
- await this.login()
81
- }
82
- try {
83
- const paylink = await this.request.post('/api/v2/paymentcards', payload, {
84
- headers: {
85
- // 'Content-Type': 'application/json',
86
- Authorization: `Bearer ${this.accessToken}`,
87
- // Accept: 'application/json'
88
- }
89
- })
90
- return paylink.data as PaymentLink
91
- } catch (error) {
92
- throw new Error(`Could not obtain the access tokemn with the given credentials.`);
93
-
94
- }
95
- }
96
-
97
- /**
98
- * Get all deposits in this account.
99
- * @returns A Promise of an Array of AccountDeposits or throws an Exception
100
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6OTgyOTQ1Mg-get-deposit-accounts-list
101
- */
102
- async getDepositAccounts(): Promise<AccountDeposits[] | Error>{
103
- if (!this.accessToken) {
104
- await this.login()
105
- }
106
- try {
107
- const deposits = await this.request.get('/api/v2/deposit_accounts',{ })
108
- return deposits.data
109
- } catch (error) {
110
- throw new Error(`Could not retrieve the account deposits ${error}`);
111
- }
112
- }
113
-
114
- /**
115
- * Get the list of all supported countries by Tropipay.
116
- * @returns Array of Countries Data
117
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/bfac21259e2ff-getting-users-countries-list
118
- */
119
- async countries(): Promise<Country[] > {
120
-
121
- try {
122
- const countries = await this.request.get('/api/v2/countries')
123
- return countries.data
124
- } catch (error) {
125
- throw new Error(`Could not retrieve the countries list`);
126
- }
127
- }
128
-
129
-
130
- /**
131
- * Get the list of all detination countries supported by Tropipay.
132
- * Obtaining the list of valid countries to send funds to. Useful
133
- * when adding new beneficiaries to some user.
134
- *
135
- * @returns Array of Country Objects
136
- * @see https://tpp.stoplight.io/docs/tropipay-api-doc/3cfe5504f0524-getting-list-of-beneficiary-countries
137
- */
138
- async destinations(): Promise<Country[]> {
139
- try {
140
- const countries = await this.request.get('/api/v2/countries/destinations')
141
- return countries.data
142
- } catch (error) {
143
- throw new Error(`Could not retrieve the destination countries list`);
144
- }
145
- }
146
-
147
- /**
148
- * Get list of all the favorites accounts. This endpoint is not documented
149
- * in the official Tropipay documentation.
150
- * @returns
151
- */
152
- async favorites(){
153
- if (!this.accessToken) {
154
- await this.login()
155
- }
156
- try {
157
- const favoritesList = await this.request.get('/api/v2/paymentcards/favorites')
158
- console.log(favoritesList)
159
- return favoritesList.data
160
- } catch (error) {
161
- throw new Error(`Could not retrieve favorites list ${error}`);
162
- }
163
- }
164
-
165
- /**
166
- * List all account movements. You can optionaly specify
167
- * offset and limit params for pagination.
168
- * @returns
169
- */
170
- async movements(offset = 0 , limit = 10){
171
- if (!this.accessToken) {
172
- await this.login()
173
- }
174
- try {
175
- const movements = await this.request.get('/api/v2/movements',
176
- {params: {limit: limit, offset: offset},
177
- headers: {
178
- 'Content-Type': 'application/json',
179
- Accept: 'application/json',
180
- Authorization: `Bearer ${this.accessToken}`,
181
-
182
- } })
183
- return movements.data
184
- } catch (error) {
185
- throw new Error(`Could not retrieve movements list`);
186
- }
187
-
188
- }
189
-
190
- /**
191
- * Return profile data for this account.
192
- * @returns
193
- */
194
- async profile(){
195
- if (!this.accessToken) {
196
- await this.login()
197
- }
198
- try {
199
- const profile = await this.request.get('/api/users/profile')
200
- return profile.data
201
- } catch (error) {
202
- throw new Error(`Could not retrieve movements list ${error}`);
203
- }
204
- }
205
-
206
- async getRates(payload = {currencyFrom: "EUR"}) {
207
- console.log(payload)
208
- if (!this.accessToken) {
209
- await this.login()
210
- }
211
- try {
212
- const rates = await this.request.post('/api/v2/movements/get_rate',{payload,
213
- headers: {
214
- 'Content-Type': 'application/json',
215
- Accept: 'application/json',
216
- Authorization: `Bearer ${this.accessToken}`,
217
-
218
- } })
219
- return rates.data
220
- } catch (error) {
221
- throw new Error(`Could not retrieve rates ${error}`);
222
- }
223
-
224
- }
225
-
226
- }
227
-
228
-
229
-
230
-
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(`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 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
+ * Obtain current Tropipay conversion rate. For example USD to EUR
206
+ * targetCurrency must be 'EUR'
207
+ * @param originCurrency Target currency code supported by Tropipay.
208
+ * @param targetCurrency Must be 'EUR'? (not documented by Tropipay)
209
+ * @returns Conversion rate (number)
210
+ * @see https://tpp.stoplight.io/docs/tropipay-api-doc/85163f6f28b23-get-rate
211
+ */
212
+ async rates(originCurrency: string , targetCurrency: string = 'EUR'): Promise<number | Error> {
213
+ if (!this.accessToken) {
214
+ await this.login()
215
+ }
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}`);
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ export type TropipayCountry = {
2
+
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ import * as countries from './countries'
2
+
3
+
4
+ export {countries}
@@ -0,0 +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,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