@yosle/tropipayjs 0.1.27 → 0.1.28
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.js +7 -27
- package/package.json +1 -1
- package/src/api/TropipayAPI.ts +17 -35
package/index.js
CHANGED
|
@@ -533,25 +533,24 @@ class Tropipay {
|
|
|
533
533
|
baseURL: config.customTropipayUrl || tpp_env,
|
|
534
534
|
headers: {
|
|
535
535
|
"Content-Type": "application/json",
|
|
536
|
-
Accept: "application/json"
|
|
537
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
536
|
+
Accept: "application/json"
|
|
538
537
|
},
|
|
539
538
|
});
|
|
540
539
|
// Add request interceptor for Token expired
|
|
541
540
|
this.request.interceptors.request.use(async (config) => {
|
|
542
|
-
|
|
541
|
+
console.log("Executing interceptor!!");
|
|
542
|
+
const currentTimestamp = Math.floor(Date.now() / 1000);
|
|
543
543
|
if (Tropipay.expiresIn && Tropipay.expiresIn < currentTimestamp) {
|
|
544
|
-
|
|
544
|
+
console.debug("Token expired, attempting to log in");
|
|
545
545
|
try {
|
|
546
546
|
await this.login();
|
|
547
547
|
}
|
|
548
548
|
catch (error) {
|
|
549
|
-
// Handle token refresh error
|
|
550
|
-
Tropipay.accessToken = null;
|
|
551
|
-
Tropipay.refreshToken = null;
|
|
552
549
|
throw handleExceptions(error);
|
|
553
550
|
}
|
|
554
551
|
}
|
|
552
|
+
// Update the Authorization header in the config
|
|
553
|
+
config.headers.Authorization = `Bearer ${Tropipay.accessToken}`;
|
|
555
554
|
return config;
|
|
556
555
|
}, (error) => {
|
|
557
556
|
return Promise.reject(error);
|
|
@@ -563,24 +562,6 @@ class Tropipay {
|
|
|
563
562
|
}
|
|
564
563
|
async login() {
|
|
565
564
|
try {
|
|
566
|
-
// if (Tropipay.refreshToken) {
|
|
567
|
-
// const { data } = await this.loginRequest.post<LoginResponse>(
|
|
568
|
-
// "/api/v2/access/token",
|
|
569
|
-
// {
|
|
570
|
-
// client_id: this.clientId,
|
|
571
|
-
// client_secret: this.clientSecret,
|
|
572
|
-
// grant_type: "refresh_token",
|
|
573
|
-
// refresh_token: Tropipay.refreshToken,
|
|
574
|
-
// },
|
|
575
|
-
// {
|
|
576
|
-
// headers: {
|
|
577
|
-
// "Content-Type": "application/json",
|
|
578
|
-
// Accept: "application/json",
|
|
579
|
-
// },
|
|
580
|
-
// }
|
|
581
|
-
// );
|
|
582
|
-
// return data;
|
|
583
|
-
// }
|
|
584
565
|
// normal credetials login
|
|
585
566
|
const { data } = await this.loginRequest.post("/api/v2/access/token", {
|
|
586
567
|
client_id: this.clientId,
|
|
@@ -594,12 +575,11 @@ class Tropipay {
|
|
|
594
575
|
},
|
|
595
576
|
});
|
|
596
577
|
Tropipay.accessToken = data.access_token;
|
|
597
|
-
Tropipay.refreshToken = data.refresh_token;
|
|
598
|
-
Tropipay.expiresIn = data.expires_in;
|
|
599
578
|
Tropipay.expiresIn = data.expires_in;
|
|
600
579
|
return data;
|
|
601
580
|
}
|
|
602
581
|
catch (error) {
|
|
582
|
+
console.log("RAW login error ", error);
|
|
603
583
|
Tropipay.accessToken = null;
|
|
604
584
|
Tropipay.refreshToken = null;
|
|
605
585
|
Tropipay.expiresIn = null;
|
package/package.json
CHANGED
package/src/api/TropipayAPI.ts
CHANGED
|
@@ -90,34 +90,33 @@ export class Tropipay {
|
|
|
90
90
|
baseURL: config.customTropipayUrl || tpp_env,
|
|
91
91
|
headers: {
|
|
92
92
|
"Content-Type": "application/json",
|
|
93
|
-
Accept: "application/json"
|
|
94
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
93
|
+
Accept: "application/json"
|
|
95
94
|
},
|
|
96
95
|
});
|
|
97
96
|
|
|
98
97
|
// Add request interceptor for Token expired
|
|
99
98
|
this.request.interceptors.request.use(
|
|
100
|
-
async (config
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
async (config) => {
|
|
100
|
+
console.log("Executing interceptor!!");
|
|
101
|
+
const currentTimestamp = Math.floor(Date.now() / 1000);
|
|
102
|
+
|
|
103
103
|
if (Tropipay.expiresIn && Tropipay.expiresIn < currentTimestamp) {
|
|
104
|
-
|
|
104
|
+
console.debug("Token expired, attempting to log in");
|
|
105
105
|
try {
|
|
106
|
-
|
|
106
|
+
await this.login();
|
|
107
107
|
} catch (error) {
|
|
108
|
-
|
|
109
|
-
Tropipay.accessToken = null;
|
|
110
|
-
Tropipay.refreshToken = null;
|
|
111
|
-
throw handleExceptions(error as any);
|
|
108
|
+
throw handleExceptions(error as Error);
|
|
112
109
|
}
|
|
113
|
-
}
|
|
114
|
-
|
|
110
|
+
}
|
|
111
|
+
// Update the Authorization header in the config
|
|
112
|
+
config.headers.Authorization = `Bearer ${Tropipay.accessToken}`;
|
|
115
113
|
return config;
|
|
116
114
|
},
|
|
117
|
-
(error
|
|
115
|
+
(error) => {
|
|
118
116
|
return Promise.reject(error);
|
|
119
117
|
}
|
|
120
118
|
);
|
|
119
|
+
|
|
121
120
|
|
|
122
121
|
this.hooks = new TropipayHooks(this);
|
|
123
122
|
this.paymentCards = new PaymentCard(this);
|
|
@@ -132,25 +131,7 @@ export class Tropipay {
|
|
|
132
131
|
|
|
133
132
|
public async login() {
|
|
134
133
|
try {
|
|
135
|
-
|
|
136
|
-
// const { data } = await this.loginRequest.post<LoginResponse>(
|
|
137
|
-
// "/api/v2/access/token",
|
|
138
|
-
// {
|
|
139
|
-
// client_id: this.clientId,
|
|
140
|
-
// client_secret: this.clientSecret,
|
|
141
|
-
// grant_type: "refresh_token",
|
|
142
|
-
// refresh_token: Tropipay.refreshToken,
|
|
143
|
-
// },
|
|
144
|
-
// {
|
|
145
|
-
// headers: {
|
|
146
|
-
// "Content-Type": "application/json",
|
|
147
|
-
// Accept: "application/json",
|
|
148
|
-
// },
|
|
149
|
-
// }
|
|
150
|
-
// );
|
|
151
|
-
// return data;
|
|
152
|
-
// }
|
|
153
|
-
|
|
134
|
+
|
|
154
135
|
// normal credetials login
|
|
155
136
|
const { data } = await this.loginRequest.post<LoginResponse>(
|
|
156
137
|
"/api/v2/access/token",
|
|
@@ -168,12 +149,13 @@ export class Tropipay {
|
|
|
168
149
|
}
|
|
169
150
|
);
|
|
170
151
|
|
|
152
|
+
|
|
171
153
|
Tropipay.accessToken = data.access_token;
|
|
172
|
-
Tropipay.refreshToken = data.refresh_token;
|
|
173
|
-
Tropipay.expiresIn = data.expires_in;
|
|
174
154
|
Tropipay.expiresIn = data.expires_in;
|
|
175
155
|
return data;
|
|
176
156
|
} catch (error) {
|
|
157
|
+
|
|
158
|
+
console.log("RAW login error ", error)
|
|
177
159
|
Tropipay.accessToken = null;
|
|
178
160
|
Tropipay.refreshToken = null;
|
|
179
161
|
Tropipay.expiresIn = null;
|