@yosle/tropipayjs 0.2.2 → 0.3.0
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/README.md +69 -9
- package/index.d.ts +358 -75
- package/index.js +427 -355
- package/index.js.map +1 -0
- package/package.json +18 -11
- package/.nvmrc +0 -1
- package/.vscode/launch.json +0 -18
- package/api/TropipayAPI.d.ts +0 -133
- package/config/TropipayConfig.d.ts +0 -2
- package/constants/TropipayConstants.d.ts +0 -2
- package/depositAccount/depositAccounts.d.ts +0 -36
- package/docs/readme.md +0 -3
- package/globals.d.ts +0 -75
- package/hooks/TropipayHooks.d.ts +0 -38
- package/interfaces/countries/index.d.ts +0 -1
- package/interfaces/depositaccounts/index.d.ts +0 -68
- package/interfaces/index.d.ts +0 -176
- package/interfaces/mediation/index.d.ts +0 -22
- package/interfaces/paymentlink/index.d.ts +0 -87
- package/jest.config.js +0 -5
- package/mediationPaymentCard/MediationPaymentCard.d.ts +0 -17
- package/paymentcard/PaymentCard.d.ts +0 -37
- package/paymentcard/paymentcard.spec.d.ts +0 -1
- package/rollup.config.js +0 -24
- package/src/api/TropipayAPI.ts +0 -459
- package/src/config/TropipayConfig.ts +0 -2
- package/src/constants/TropipayConstants.ts +0 -2
- package/src/depositAccount/depositAccounts.ts +0 -139
- package/src/hooks/TropipayHooks.ts +0 -154
- package/src/index.ts +0 -29
- package/src/interfaces/depositaccounts/index.ts +0 -70
- package/src/interfaces/index.js +0 -2
- package/src/interfaces/index.ts +0 -212
- package/src/interfaces/mediation/index.ts +0 -23
- package/src/interfaces/paymentlink/index.d.ts +0 -36
- package/src/interfaces/paymentlink/index.js +0 -3
- package/src/interfaces/paymentlink/index.ts +0 -91
- package/src/mediationPaymentCard/MediationPaymentCard.ts +0 -42
- package/src/mocks/tropipay.ts +0 -0
- package/src/paymentcard/PaymentCard.ts +0 -123
- package/src/paymentcard/paymentcard.spec.ts +0 -79
- package/src/utils/TropipayUtils.ts +0 -126
- package/src/utils/errors.ts +0 -92
- package/tsconfig.json +0 -96
- package/utils/TropipayUtils.d.ts +0 -45
- package/utils/errors.d.ts +0 -16
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { Tropipay } from "../api/TropipayAPI";
|
|
2
|
-
import { PaymentLink } from "../interfaces";
|
|
3
|
-
import { PaymentLinkPayload } from "../interfaces";
|
|
4
|
-
import { handleExceptions } from "../utils/errors";
|
|
5
|
-
export default class PaymentCard {
|
|
6
|
-
private tropipay: Tropipay;
|
|
7
|
-
constructor(tropipayInstance: Tropipay) {
|
|
8
|
-
this.tropipay = tropipayInstance;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Create a paymentLink with the specified options.
|
|
13
|
-
* @param payload PaymentLinkPayload Object.
|
|
14
|
-
* @returns Promise<PaymentLink> or throws an Exception.
|
|
15
|
-
* @see https://tpp.stoplight.io/docs/tropipay-api-doc/b3A6ODgyNTM3OQ-create-a-new-pay-link-charge
|
|
16
|
-
*/
|
|
17
|
-
async create(payload: PaymentLinkPayload): Promise<PaymentLink> {
|
|
18
|
-
if (!Tropipay.accessToken) {
|
|
19
|
-
await this.tropipay.login();
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
const paylink = await this.tropipay.request.post(
|
|
23
|
-
"/api/v2/paymentcards",
|
|
24
|
-
payload,
|
|
25
|
-
{
|
|
26
|
-
headers: {
|
|
27
|
-
"Content-Type": "application/json",
|
|
28
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
29
|
-
Accept: "application/json",
|
|
30
|
-
},
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
return paylink.data as PaymentLink;
|
|
34
|
-
} catch (error) {
|
|
35
|
-
throw handleExceptions(error as unknown as any);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Shows a list of stored paymentcards created by user.
|
|
40
|
-
* This list includes active and closed paylinks
|
|
41
|
-
* @returns Array of paymentlinks
|
|
42
|
-
*/
|
|
43
|
-
public async list() {
|
|
44
|
-
if (!Tropipay.accessToken) {
|
|
45
|
-
await this.tropipay.login();
|
|
46
|
-
}
|
|
47
|
-
try {
|
|
48
|
-
const paymentcards = await this.tropipay.request.get(
|
|
49
|
-
`/api/v2/paymentcards`,
|
|
50
|
-
{
|
|
51
|
-
headers: {
|
|
52
|
-
"Content-Type": "application/json",
|
|
53
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
54
|
-
Accept: "application/json",
|
|
55
|
-
},
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
return paymentcards.data;
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw new Error(`Could not retrieve PaymenCards list`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Retrieves a payment card with the specified ID.
|
|
66
|
-
*
|
|
67
|
-
* @param {string} id - The ID of the payment card to retrieve.
|
|
68
|
-
* @return {Promise<any>} A Promise that resolves to the payment card data.
|
|
69
|
-
* @throws {Error} If an error occurs while retrieving the payment card.
|
|
70
|
-
*/
|
|
71
|
-
public async get(id: string) {
|
|
72
|
-
if (!Tropipay.accessToken) {
|
|
73
|
-
await this.tropipay.login();
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
const paymentcard = await this.tropipay.request.get(
|
|
77
|
-
`/api/v2/paymentcards/${id}`,
|
|
78
|
-
{
|
|
79
|
-
headers: {
|
|
80
|
-
"Content-Type": "application/json",
|
|
81
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
82
|
-
Accept: "application/json",
|
|
83
|
-
},
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
return paymentcard.data;
|
|
87
|
-
} catch (error) {
|
|
88
|
-
throw handleExceptions(error as unknown as any);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Deletes a payment card with the specified ID. Its a LOGIC delete
|
|
94
|
-
* so this will delete the paymentcard from paymentcard list and
|
|
95
|
-
* disable shortUrl but not paymentUrl
|
|
96
|
-
* @param {string} id - The ID of the payment card to delete.
|
|
97
|
-
* @return {Promise<any>} A Promise that resolves to the deleted payment card data.
|
|
98
|
-
* @throws {Error} If an error occurs while deleting the payment card.
|
|
99
|
-
*/
|
|
100
|
-
public async delete(id: string) {
|
|
101
|
-
if (!Tropipay.accessToken) {
|
|
102
|
-
await this.tropipay.login();
|
|
103
|
-
}
|
|
104
|
-
try {
|
|
105
|
-
const paymentcard = await this.tropipay.request.delete(
|
|
106
|
-
`/api/v2/paymentcards/`,
|
|
107
|
-
{
|
|
108
|
-
headers: {
|
|
109
|
-
"Content-Type": "application/json",
|
|
110
|
-
Authorization: `Bearer ${Tropipay.accessToken}`,
|
|
111
|
-
Accept: "application/json",
|
|
112
|
-
},
|
|
113
|
-
data: {
|
|
114
|
-
cardId: id,
|
|
115
|
-
},
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
return paymentcard.data;
|
|
119
|
-
} catch (error) {
|
|
120
|
-
throw handleExceptions(error as unknown as any);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { Tropipay } from "../../src/api/TropipayAPI";
|
|
2
|
-
import { PaymentLinkPayload } from "../interfaces";
|
|
3
|
-
import { TropipayJSException } from "../utils/errors";
|
|
4
|
-
import PaymentCard from "./PaymentCard";
|
|
5
|
-
|
|
6
|
-
jest.mock("axios");
|
|
7
|
-
jest.mock("../../src/api/TropipayAPI");
|
|
8
|
-
|
|
9
|
-
describe("Create PaymentCard", () => {
|
|
10
|
-
describe("create", () => {
|
|
11
|
-
it("should create a payment link", async () => {
|
|
12
|
-
const payload: PaymentLinkPayload = {
|
|
13
|
-
amount: 200,
|
|
14
|
-
client: null,
|
|
15
|
-
concept: "Concept",
|
|
16
|
-
currency: "EUR",
|
|
17
|
-
description: "Description",
|
|
18
|
-
directPayment: true,
|
|
19
|
-
expirationDays: 30,
|
|
20
|
-
favorite: false,
|
|
21
|
-
reasonId: 45,
|
|
22
|
-
reference: "Reference",
|
|
23
|
-
serviceDate: "2022-01-01",
|
|
24
|
-
urlFailed: "urlFailed",
|
|
25
|
-
urlNotification: "urlNotification",
|
|
26
|
-
urlSuccess: "urlSuccess",
|
|
27
|
-
singleUse: false,
|
|
28
|
-
paymentMethods: ["EXT"],
|
|
29
|
-
lang: "es",
|
|
30
|
-
};
|
|
31
|
-
const expectedPaymentLink = {
|
|
32
|
-
id: "1f8b0600-0b62-11ef-b879-9d16dab40e58",
|
|
33
|
-
reference: "08025834659",
|
|
34
|
-
concept: "NFT Purchase",
|
|
35
|
-
description: "Golden Monkey NFT character",
|
|
36
|
-
amount: 500,
|
|
37
|
-
currency: "EUR",
|
|
38
|
-
singleUse: true,
|
|
39
|
-
favorite: false,
|
|
40
|
-
reasonId: 4,
|
|
41
|
-
reasonDes: null,
|
|
42
|
-
expirationDays: 0,
|
|
43
|
-
userId: "a9546500-65c0-11ed-97e6-2ffd2c53074d",
|
|
44
|
-
lang: "es",
|
|
45
|
-
imageBase: null,
|
|
46
|
-
state: 1,
|
|
47
|
-
urlSuccess: null,
|
|
48
|
-
urlFailed: null,
|
|
49
|
-
urlNotification: null,
|
|
50
|
-
expirationDate: null,
|
|
51
|
-
serviceDate: null,
|
|
52
|
-
hasClient: false,
|
|
53
|
-
credentialId: 129710,
|
|
54
|
-
force3ds: false,
|
|
55
|
-
saveToken: true,
|
|
56
|
-
origin: 2,
|
|
57
|
-
paymentcardType: 1,
|
|
58
|
-
updatedAt: "2024-05-06T04:35:54.352Z",
|
|
59
|
-
createdAt: "2024-05-06T04:35:53.568Z",
|
|
60
|
-
qrImage:
|
|
61
|
-
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAACkCAYAAAAZtYVBAAAAAklEQVR4AewaftIAAAcPSURBVO3B0ald3QqA0S+yexKsxfKsRbCq3LxdnyZM1ton5sc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cVdFlBZ+o8kRV0pk6XFdzICk5MnZOs4CQr+JuEtQYR1hpEWGuQD1+WFXxTVtCZOidZwYmp02UFJ6bOialzYup0WcGJqXOSFbzJ1Omygs7U6bKCJ4S1BhHWGkRYa5APLzN11v9lBZ2pc8PU6bKCJ0ydk6ygywpOsoI3CWsNIqw1iLDWIL9+/8FaQwhrDSKsNYiw1iDCWoMIaw0irDWIsNYgwlqDCGsNIqw1iLDWIMJagwhrDSKsNYiw1iD/Ayc0u8My6tdVAAAAAElFTkSuQmCC",
|
|
62
|
-
shortUrl: "https://tppay.me/lvugy80i",
|
|
63
|
-
paymentUrl: null,
|
|
64
|
-
rawUrlPayment: null,
|
|
65
|
-
giftcard: null,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const tropipayInstance = new Tropipay({
|
|
69
|
-
clientId: "clientId",
|
|
70
|
-
clientSecret: "clientSecret",
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const result = await tropipayInstance.paymentCards.create(payload);
|
|
74
|
-
|
|
75
|
-
// expect(result).toEqual(expectedPaymentLink);
|
|
76
|
-
expect(true).toBeDefined();
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import * as crypto from "crypto";
|
|
2
|
-
import { Tropipay } from "../api/TropipayAPI";
|
|
3
|
-
|
|
4
|
-
import axios from "axios";
|
|
5
|
-
import fs from "fs/promises";
|
|
6
|
-
import { MAX_IMAGE_SIZE_MB } from "../constants/TropipayConstants";
|
|
7
|
-
export class ServerSideUtils {
|
|
8
|
-
private tropipay: Tropipay;
|
|
9
|
-
constructor(tropipayInstance: Tropipay) {
|
|
10
|
-
this.tropipay = tropipayInstance;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Verify Topipay's signature on webhooks.
|
|
15
|
-
* @param credentials Credential object or Tropipay instance
|
|
16
|
-
* @param {String} originalCurrencyAmount
|
|
17
|
-
* @param bankOrderCode
|
|
18
|
-
* @param signature
|
|
19
|
-
* @returns {Boolean}
|
|
20
|
-
*/
|
|
21
|
-
public static verifySignature(
|
|
22
|
-
credentials:
|
|
23
|
-
| {
|
|
24
|
-
clientId: string;
|
|
25
|
-
clientSecret: string;
|
|
26
|
-
}
|
|
27
|
-
| Tropipay,
|
|
28
|
-
originalCurrencyAmount: string,
|
|
29
|
-
bankOrderCode: string,
|
|
30
|
-
signature: string
|
|
31
|
-
): boolean {
|
|
32
|
-
const localSignature = crypto
|
|
33
|
-
.createHash("sha256")
|
|
34
|
-
.update(
|
|
35
|
-
bankOrderCode +
|
|
36
|
-
credentials.clientId +
|
|
37
|
-
credentials.clientSecret +
|
|
38
|
-
originalCurrencyAmount
|
|
39
|
-
)
|
|
40
|
-
.digest("hex");
|
|
41
|
-
return localSignature === signature;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Checks if the provided base64 string represents a square image.
|
|
45
|
-
*
|
|
46
|
-
* @param {string} base64String - The base64 string of the image
|
|
47
|
-
* @return {Promise<boolean>} A Promise that resolves to a boolean indicating whether the image is square
|
|
48
|
-
*/
|
|
49
|
-
public static async isBase64ImageSquare(
|
|
50
|
-
base64String: string
|
|
51
|
-
): Promise<boolean> {
|
|
52
|
-
return new Promise<boolean>((resolve, reject) => {
|
|
53
|
-
const img = new Image();
|
|
54
|
-
|
|
55
|
-
img.onload = () => {
|
|
56
|
-
// Check if the image has a 1:1 aspect ratio
|
|
57
|
-
const isSquare = img.width === img.height;
|
|
58
|
-
resolve(isSquare);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
img.onerror = (error) => {
|
|
62
|
-
reject(error);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
img.src = base64String;
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Takes a local file path and returns a base64 representation of the file content.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} filepath - the path of the file to be converted to base64
|
|
73
|
-
* @return {Promise<string>} a Promise that resolves to the base64 representation of the file content
|
|
74
|
-
*/
|
|
75
|
-
public static async fileToBase64(filepath: string): Promise<string> {
|
|
76
|
-
const contents = await fs.readFile(filepath);
|
|
77
|
-
let base64content = contents.toString("base64");
|
|
78
|
-
const ext = filepath.split(".").pop();
|
|
79
|
-
return `data:image/${ext};base64,` + base64content;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Get the base64 representation of a remote file from the given URL.
|
|
84
|
-
*
|
|
85
|
-
* @param {string} url - the URL of the file
|
|
86
|
-
* @return {Promise<string>} the base64 representation of the file
|
|
87
|
-
*/
|
|
88
|
-
public static async getBase64FromFileUrl(url: string): Promise<string> {
|
|
89
|
-
const response = await axios.get(url, {
|
|
90
|
-
responseType: "arraybuffer",
|
|
91
|
-
maxRedirects: 5,
|
|
92
|
-
});
|
|
93
|
-
return Buffer.from(response.data, "binary").toString("base64");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Check if the base64 string represents a valid image and has a valid size
|
|
98
|
-
*
|
|
99
|
-
* @param {string} base64Image - the base64 image to be validated
|
|
100
|
-
* @return {Promise<string>} the valid base64 image
|
|
101
|
-
*/
|
|
102
|
-
public static isValidImage(base64Image: string): boolean {
|
|
103
|
-
// Check if the base64 string represents a valid image and has a valid size
|
|
104
|
-
try {
|
|
105
|
-
const isBase64Image = base64Image.startsWith("data:image/");
|
|
106
|
-
if (!isBase64Image) {
|
|
107
|
-
console.error("Not a valid base64 image");
|
|
108
|
-
throw new Error("Not a valid base64 image");
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// TODO: use constants intead of magic numbers here
|
|
112
|
-
const isSizeLessThan2MB =
|
|
113
|
-
base64Image.length * 0.75 <= MAX_IMAGE_SIZE_MB * 1024 * 1024;
|
|
114
|
-
|
|
115
|
-
if (!isSizeLessThan2MB) {
|
|
116
|
-
console.error(`The image should be less than 2mb`);
|
|
117
|
-
throw new Error(`The image should be less than 2mb`);
|
|
118
|
-
}
|
|
119
|
-
// all is fine
|
|
120
|
-
return true;
|
|
121
|
-
} catch (error) {
|
|
122
|
-
console.error("Error checking base64 image:", error);
|
|
123
|
-
throw new Error(`Error checking base64 image`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
package/src/utils/errors.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { AxiosError, AxiosResponse } from "axios";
|
|
2
|
-
|
|
3
|
-
const ErrorType = {
|
|
4
|
-
VALIDATION_ERROR: "VALIDATION_ERROR", // 400
|
|
5
|
-
UNAUTHORIZED_ERROR: "UNAUTHORIZED_ERROR", // 401 (Not valid credentials)
|
|
6
|
-
FORBIDDEN_ERROR: "FORBIDDEN_ERROR", // 403 (Valid credentials but insufficient to grant access)
|
|
7
|
-
NOT_FOUND: "NOT_FOUND", // 404
|
|
8
|
-
RATE_LIMIT_ERROR: "RATE_LIMIT_ERROR", // 429 (Used only in cloudflare for now)
|
|
9
|
-
OPERATION_ERROR: "OPERATION_ERROR", // 500
|
|
10
|
-
} as const;
|
|
11
|
-
|
|
12
|
-
const ErrorCode = {
|
|
13
|
-
VALIDATION_ERROR: "VALIDATION_ERROR",
|
|
14
|
-
UNAUTHORIZED_ERROR: "UNAUTHORIZED_ERROR",
|
|
15
|
-
FORBIDDEN_ERROR: "FORBIDDEN_ERROR",
|
|
16
|
-
NOT_FOUND: "NOT_FOUND",
|
|
17
|
-
RATE_LIMIT_ERROR: "RATE_LIMIT_ERROR",
|
|
18
|
-
OPERATION_ERROR: "OPERATION_ERROR",
|
|
19
|
-
SYNTAX_ERROR: "SYNTAX_ERROR",
|
|
20
|
-
INVALID_PARAM: "INVALID_PARAM",
|
|
21
|
-
UNHANDLED_ERROR: "SERVER_ERROR",
|
|
22
|
-
} as const;
|
|
23
|
-
|
|
24
|
-
export interface ErrorDetail {
|
|
25
|
-
param: string;
|
|
26
|
-
value: string | number | null;
|
|
27
|
-
msg: string;
|
|
28
|
-
code: string;
|
|
29
|
-
location: string | null;
|
|
30
|
-
meta: unknown;
|
|
31
|
-
}
|
|
32
|
-
export function handleExceptions(
|
|
33
|
-
error: AxiosError | Error
|
|
34
|
-
): TropipayJSException {
|
|
35
|
-
if (error instanceof AxiosError) {
|
|
36
|
-
if (error.response) {
|
|
37
|
-
const axiosResponse: AxiosResponse = error.response;
|
|
38
|
-
const errorMessage =
|
|
39
|
-
axiosResponse.data?.error?.message || "An error occurred";
|
|
40
|
-
|
|
41
|
-
switch (axiosResponse.status) {
|
|
42
|
-
case 400:
|
|
43
|
-
case 401:
|
|
44
|
-
case 403:
|
|
45
|
-
case 404:
|
|
46
|
-
return new TropipayJSException(
|
|
47
|
-
errorMessage,
|
|
48
|
-
axiosResponse.status,
|
|
49
|
-
axiosResponse.data.error
|
|
50
|
-
);
|
|
51
|
-
case 429:
|
|
52
|
-
return new TropipayJSException(
|
|
53
|
-
`${errorMessage} Too many request or rate limited`,
|
|
54
|
-
axiosResponse.status,
|
|
55
|
-
axiosResponse.data.error
|
|
56
|
-
);
|
|
57
|
-
default:
|
|
58
|
-
return new TropipayJSException(
|
|
59
|
-
errorMessage,
|
|
60
|
-
axiosResponse.status,
|
|
61
|
-
axiosResponse.data.error
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
} else if (error.request) {
|
|
65
|
-
// Axios request was made but no response received (e.g., network error)
|
|
66
|
-
return new TropipayJSException(
|
|
67
|
-
"Request failed: No response received",
|
|
68
|
-
500,
|
|
69
|
-
null
|
|
70
|
-
);
|
|
71
|
-
} else {
|
|
72
|
-
// Something else went wrong
|
|
73
|
-
return new TropipayJSException("An error occurred", 500, null);
|
|
74
|
-
}
|
|
75
|
-
} else {
|
|
76
|
-
return new TropipayJSException("An Unknown error occurred", 500, null);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export class TropipayJSException extends Error {
|
|
81
|
-
code: number; // Status code
|
|
82
|
-
error: unknown;
|
|
83
|
-
message: string;
|
|
84
|
-
constructor(message: string, code: number, data: unknown) {
|
|
85
|
-
super(message);
|
|
86
|
-
// Set the prototype explicitly.
|
|
87
|
-
Object.setPrototypeOf(this, TropipayJSException.prototype);
|
|
88
|
-
this.code = code;
|
|
89
|
-
this.error = data;
|
|
90
|
-
this.message = message;
|
|
91
|
-
}
|
|
92
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
-
/* Projects */
|
|
5
|
-
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
6
|
-
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
7
|
-
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
8
|
-
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
9
|
-
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
10
|
-
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
11
|
-
/* Language and Environment */
|
|
12
|
-
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
13
|
-
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
14
|
-
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
15
|
-
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
16
|
-
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
17
|
-
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
18
|
-
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
19
|
-
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
20
|
-
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
21
|
-
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
22
|
-
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
23
|
-
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
24
|
-
/* Modules */
|
|
25
|
-
"module": "esnext" /* Specify what module code is generated. */,
|
|
26
|
-
"rootDir": "./src" /* Specify the root folder within your source files. */,
|
|
27
|
-
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
|
28
|
-
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
29
|
-
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
30
|
-
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
31
|
-
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
32
|
-
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
33
|
-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
34
|
-
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
35
|
-
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
36
|
-
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
37
|
-
/* JavaScript Support */
|
|
38
|
-
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
39
|
-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
40
|
-
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
41
|
-
/* Emit */
|
|
42
|
-
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
|
43
|
-
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
44
|
-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
45
|
-
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
|
|
46
|
-
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
47
|
-
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
|
48
|
-
"removeComments": false /* Disable emitting comments. */,
|
|
49
|
-
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
50
|
-
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
51
|
-
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
52
|
-
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
53
|
-
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
54
|
-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
55
|
-
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
56
|
-
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
57
|
-
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
58
|
-
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
59
|
-
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
60
|
-
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
61
|
-
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
62
|
-
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
63
|
-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
64
|
-
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
65
|
-
/* Interop Constraints */
|
|
66
|
-
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
67
|
-
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
68
|
-
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
69
|
-
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
70
|
-
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
71
|
-
/* Type Checking */
|
|
72
|
-
"strict": true /* Enable all strict type-checking options. */,
|
|
73
|
-
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
74
|
-
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
75
|
-
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
76
|
-
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
77
|
-
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
78
|
-
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
79
|
-
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
80
|
-
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
81
|
-
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
82
|
-
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
83
|
-
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
84
|
-
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
85
|
-
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
86
|
-
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
87
|
-
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
88
|
-
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
89
|
-
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
90
|
-
"allowUnreachableCode": false /* Disable error reporting for unreachable code. */,
|
|
91
|
-
/* Completeness */
|
|
92
|
-
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
93
|
-
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
94
|
-
},
|
|
95
|
-
"exclude": ["node_modules/**", "**/*.spec.ts", "./dist/**/*"]
|
|
96
|
-
}
|
package/utils/TropipayUtils.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Tropipay } from "../api/TropipayAPI";
|
|
2
|
-
export declare class ServerSideUtils {
|
|
3
|
-
private tropipay;
|
|
4
|
-
constructor(tropipayInstance: Tropipay);
|
|
5
|
-
/**
|
|
6
|
-
* Verify Topipay's signature on webhooks.
|
|
7
|
-
* @param credentials Credential object or Tropipay instance
|
|
8
|
-
* @param {String} originalCurrencyAmount
|
|
9
|
-
* @param bankOrderCode
|
|
10
|
-
* @param signature
|
|
11
|
-
* @returns {Boolean}
|
|
12
|
-
*/
|
|
13
|
-
static verifySignature(credentials: {
|
|
14
|
-
clientId: string;
|
|
15
|
-
clientSecret: string;
|
|
16
|
-
} | Tropipay, originalCurrencyAmount: string, bankOrderCode: string, signature: string): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Checks if the provided base64 string represents a square image.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} base64String - The base64 string of the image
|
|
21
|
-
* @return {Promise<boolean>} A Promise that resolves to a boolean indicating whether the image is square
|
|
22
|
-
*/
|
|
23
|
-
static isBase64ImageSquare(base64String: string): Promise<boolean>;
|
|
24
|
-
/**
|
|
25
|
-
* Takes a local file path and returns a base64 representation of the file content.
|
|
26
|
-
*
|
|
27
|
-
* @param {string} filepath - the path of the file to be converted to base64
|
|
28
|
-
* @return {Promise<string>} a Promise that resolves to the base64 representation of the file content
|
|
29
|
-
*/
|
|
30
|
-
static fileToBase64(filepath: string): Promise<string>;
|
|
31
|
-
/**
|
|
32
|
-
* Get the base64 representation of a remote file from the given URL.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} url - the URL of the file
|
|
35
|
-
* @return {Promise<string>} the base64 representation of the file
|
|
36
|
-
*/
|
|
37
|
-
static getBase64FromFileUrl(url: string): Promise<string>;
|
|
38
|
-
/**
|
|
39
|
-
* Check if the base64 string represents a valid image and has a valid size
|
|
40
|
-
*
|
|
41
|
-
* @param {string} base64Image - the base64 image to be validated
|
|
42
|
-
* @return {Promise<string>} the valid base64 image
|
|
43
|
-
*/
|
|
44
|
-
static isValidImage(base64Image: string): boolean;
|
|
45
|
-
}
|
package/utils/errors.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { AxiosError } from "axios";
|
|
2
|
-
export interface ErrorDetail {
|
|
3
|
-
param: string;
|
|
4
|
-
value: string | number | null;
|
|
5
|
-
msg: string;
|
|
6
|
-
code: string;
|
|
7
|
-
location: string | null;
|
|
8
|
-
meta: unknown;
|
|
9
|
-
}
|
|
10
|
-
export declare function handleExceptions(error: AxiosError | Error): TropipayJSException;
|
|
11
|
-
export declare class TropipayJSException extends Error {
|
|
12
|
-
code: number;
|
|
13
|
-
error: unknown;
|
|
14
|
-
message: string;
|
|
15
|
-
constructor(message: string, code: number, data: unknown);
|
|
16
|
-
}
|