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