@yosle/tropipayjs
Advanced tools
Comparing version 0.1.20 to 0.1.21
@@ -16,5 +16,6 @@ /** | ||
readonly clientSecret: string; | ||
readonly scopes: String[]; | ||
request: Axios; | ||
static accessToken: string | undefined; | ||
static refreshToken: string | undefined; | ||
static accessToken: string | null; | ||
static refreshToken: string | null; | ||
serverMode: ServerMode; | ||
@@ -24,2 +25,7 @@ hooks: TropipayHooks; | ||
depositAccounts: DepositAccounts; | ||
/** | ||
* Initializes a new instance of the Tropipay class. | ||
* | ||
* @param {TropipayConfig} config - The configuration object. | ||
*/ | ||
constructor(config: TropipayConfig); | ||
@@ -26,0 +32,0 @@ login(): Promise<LoginResponse>; |
@@ -28,3 +28,3 @@ import { HookEventType, UserHookSubscribed } from "../interfaces"; | ||
/** | ||
* Get the sucbcribed hook info by his event type. | ||
* Get the subscribed hook info by his event type. | ||
* If no event type is passed, it will return | ||
@@ -31,0 +31,0 @@ * all subscribed hooks or empty Array if none exist. |
@@ -14,2 +14,3 @@ import { Axios } from 'axios'; | ||
clientSecret: string; | ||
scopes?: string[]; | ||
serverMode?: ServerMode$1; | ||
@@ -192,3 +193,3 @@ } | ||
/** | ||
* Get the sucbcribed hook info by his event type. | ||
* Get the subscribed hook info by his event type. | ||
* If no event type is passed, it will return | ||
@@ -299,5 +300,6 @@ * all subscribed hooks or empty Array if none exist. | ||
readonly clientSecret: string; | ||
readonly scopes: String[]; | ||
request: Axios; | ||
static accessToken: string | undefined; | ||
static refreshToken: string | undefined; | ||
static accessToken: string | null; | ||
static refreshToken: string | null; | ||
serverMode: ServerMode; | ||
@@ -307,2 +309,7 @@ hooks: TropipayHooks; | ||
depositAccounts: DepositAccounts; | ||
/** | ||
* Initializes a new instance of the Tropipay class. | ||
* | ||
* @param {TropipayConfig} config - The configuration object. | ||
*/ | ||
constructor(config: TropipayConfig); | ||
@@ -309,0 +316,0 @@ login(): Promise<LoginResponse>; |
53
index.js
@@ -45,4 +45,4 @@ 'use strict'; | ||
return new TropipayJSException(errorMessage, axiosResponse.status, axiosResponse.data.error); | ||
// case 429: | ||
// return new TooManyRequestsException(errorMessage); | ||
case 429: | ||
return new TropipayJSException(`${errorMessage} Too many request or rate limited`, axiosResponse.status, axiosResponse.data.error); | ||
default: | ||
@@ -62,3 +62,3 @@ return new TropipayJSException(errorMessage, axiosResponse.status, axiosResponse.data.error); | ||
else { | ||
return new TropipayJSException(`jsbfvbsfvbf`, 500, null); | ||
return new TropipayJSException("An Unknown error occurred", 500, null); | ||
} | ||
@@ -125,3 +125,3 @@ } | ||
/** | ||
* Get the sucbcribed hook info by his event type. | ||
* Get the subscribed hook info by his event type. | ||
* If no event type is passed, it will return | ||
@@ -414,2 +414,3 @@ * all subscribed hooks or empty Array if none exist. | ||
clientSecret; | ||
scopes; | ||
request; | ||
@@ -422,3 +423,28 @@ static accessToken; | ||
depositAccounts; | ||
/** | ||
* Initializes a new instance of the Tropipay class. | ||
* | ||
* @param {TropipayConfig} config - The configuration object. | ||
*/ | ||
constructor(config) { | ||
// use all scopes if none is passed | ||
if (!config?.scopes) { | ||
this.scopes = [ | ||
"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", | ||
]; | ||
} | ||
else { | ||
this.scopes = config.scopes; | ||
} | ||
if (!config.clientId || !config.clientSecret) { | ||
throw new TropipayJSException(`You must pass clientId and clientSecret in Tropipay constructor`, 400, null); | ||
} | ||
this.clientId = config.clientId; | ||
@@ -443,2 +469,17 @@ this.clientSecret = config.clientSecret; | ||
try { | ||
if (Tropipay.refreshToken) { | ||
const { data } = await this.request.post("/api/v2/access/token", { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "refresh_token", | ||
refresh_token: Tropipay.refreshToken, | ||
}, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
}); | ||
return data; | ||
} | ||
// normal credetials login | ||
const { data } = await this.request.post("/api/v2/access/token", { | ||
@@ -448,3 +489,3 @@ client_id: this.clientId, | ||
grant_type: "client_credentials", | ||
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", | ||
scope: this.scopes.join(" "), // "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", | ||
}, { | ||
@@ -461,2 +502,4 @@ headers: { | ||
catch (error) { | ||
Tropipay.accessToken = null; | ||
Tropipay.refreshToken = null; | ||
throw handleExceptions(error); | ||
@@ -463,0 +506,0 @@ } |
@@ -10,2 +10,3 @@ import { ServerMode } from "../constants/TropipayConstants"; | ||
clientSecret: string; | ||
scopes?: string[]; | ||
serverMode?: ServerMode; | ||
@@ -12,0 +13,0 @@ } |
{ | ||
"name": "@yosle/tropipayjs", | ||
"version": "0.1.20", | ||
"version": "0.1.21", | ||
"description": "Javascript / Typescript SDK for the Tropipay API", | ||
@@ -39,3 +39,4 @@ "main": "index.js", | ||
"typescript": "^5.0.4", | ||
"vitest": "^1.2.2" | ||
"vitest": "^1.2.2", | ||
"@types/node": "^20.12.7" | ||
}, | ||
@@ -42,0 +43,0 @@ "funding": { |
@@ -50,3 +50,3 @@ # TropipayJS - Typescript/Javascript SDK for Tropipay Payments API | ||
![alt](https://yosle.github.io/tropipayjs-docs/images/cred1.png) | ||
![alt](https://yosle.github.io/tropipayjs-docs/images/cred2.png) | ||
![alt](https://yosle.github.io/tropipayjs-docs/images/cred3.png) | ||
@@ -53,0 +53,0 @@ |
@@ -23,9 +23,10 @@ /** | ||
import { DepositAccounts } from "../depositAccount/depositAccounts"; | ||
import { handleExceptions } from "../utils/errors"; | ||
import { TropipayJSException, handleExceptions } from "../utils/errors"; | ||
export class Tropipay { | ||
readonly clientId: string; | ||
readonly clientSecret: string; | ||
readonly scopes: String[]; | ||
public request: Axios; | ||
public static accessToken: string | undefined; | ||
public static refreshToken: string | undefined; | ||
public static accessToken: string | null; | ||
public static refreshToken: string | null; | ||
public serverMode: ServerMode; | ||
@@ -36,3 +37,34 @@ public hooks: TropipayHooks; | ||
/** | ||
* Initializes a new instance of the Tropipay class. | ||
* | ||
* @param {TropipayConfig} config - The configuration object. | ||
*/ | ||
constructor(config: TropipayConfig) { | ||
// use all scopes if none is passed | ||
if (!config?.scopes) { | ||
this.scopes = [ | ||
"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", | ||
]; | ||
} else { | ||
this.scopes = config.scopes; | ||
} | ||
if (!config.clientId || !config.clientSecret) { | ||
throw new TropipayJSException( | ||
`You must pass clientId and clientSecret in Tropipay constructor`, | ||
400, | ||
null | ||
); | ||
} | ||
this.clientId = config.clientId; | ||
@@ -59,2 +91,22 @@ this.clientSecret = config.clientSecret; | ||
try { | ||
if (Tropipay.refreshToken) { | ||
const { data } = await this.request.post<LoginResponse>( | ||
"/api/v2/access/token", | ||
{ | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "refresh_token", | ||
refresh_token: Tropipay.refreshToken, | ||
}, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
} | ||
); | ||
return data; | ||
} | ||
// normal credetials login | ||
const { data } = await this.request.post<LoginResponse>( | ||
@@ -66,4 +118,3 @@ "/api/v2/access/token", | ||
grant_type: "client_credentials", | ||
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", | ||
scope: this.scopes.join(" "), // "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", | ||
}, | ||
@@ -82,2 +133,4 @@ { | ||
} catch (error) { | ||
Tropipay.accessToken = null; | ||
Tropipay.refreshToken = null; | ||
throw handleExceptions(error as any); | ||
@@ -84,0 +137,0 @@ } |
@@ -60,3 +60,3 @@ import { HookEventType, UserHookSubscribed } from "../interfaces"; | ||
/** | ||
* Get the sucbcribed hook info by his event type. | ||
* Get the subscribed hook info by his event type. | ||
* If no event type is passed, it will return | ||
@@ -63,0 +63,0 @@ * all subscribed hooks or empty Array if none exist. |
@@ -12,2 +12,3 @@ import { ServerMode } from "../constants/TropipayConstants"; | ||
clientSecret: string; | ||
scopes?: string[]; | ||
serverMode?: ServerMode; | ||
@@ -14,0 +15,0 @@ } |
@@ -60,4 +60,8 @@ import { AxiosError, AxiosResponse } from "axios"; | ||
); | ||
// case 429: | ||
// return new TooManyRequestsException(errorMessage); | ||
case 429: | ||
return new TropipayJSException( | ||
`${errorMessage} Too many request or rate limited`, | ||
axiosResponse.status, | ||
axiosResponse.data.error | ||
); | ||
default: | ||
@@ -82,3 +86,3 @@ return new TropipayJSException( | ||
} else { | ||
return new TropipayJSException(`jsbfvbsfvbf`, 500, null); | ||
return new TropipayJSException("An Unknown error occurred", 500, null); | ||
} | ||
@@ -85,0 +89,0 @@ } |
119365
3304
9