cppay-sdk 0.0.2-beta.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 +3 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +116 -0
- package/package.json +34 -0
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={Ethereum:[`USDT`,`USDC`,`ETH`],BSC:[`USDT`,`USDC`,`BNB`],Tron:[`USDT`,`USDC`,`TRX`],Polygon:[`USDT`,`USDC`],Solana:[`USDT`,`USDC`,`SOL`]},t={Ethereum:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png`,BSC:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png`,Tron:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png`,Polygon:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/polygon/info/logo.png`,Solana:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png`},n={USDT:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xdAC17F958D2ee523a2206206994597C13D831ec7/logo.png`,USDC:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png`,ETH:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png`,BNB:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png`,TRX:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png`,SOL:`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png`},r=Object.entries(n).map(([e,t])=>({symbol:e,icon:t})),i=Object.entries(t).map(([e,t])=>({name:e,icon:t}));var a=async(e,t,n)=>{let r=Object.assign({timeout:15e3},n),i=new AbortController;setTimeout(()=>i.abort(),r.timeout);let a=await fetch(e,{signal:i.signal,...t}),o=a.headers.get(`Content-Type`)??``;if(o.includes(`json`))return await a.json();try{return await a.json()}catch{throw Error(`Unsupport Content Type: ${o}`)}},o=0;const s=async e=>{let t=await a(e.url,{method:e.method??`get`,headers:e.headers,body:JSON.stringify(e.data)});if(t.code!==o)throw Error(t.message);return t.data},c=(e=1e3)=>new Promise(t=>setTimeout(t,e));var l=class t{static supportedChains=i;static chainTokens=e;static getSupportedTokens(e){let n=[];return Object.entries(t.chainTokens).some(([t,i])=>{if(t===e)return n=r.filter(e=>i.includes(e.symbol)),!0}),n}apikey;constructor(e){this.apikey=e}get headers(){return{Authorization:this.apikey}}async createPayment(e){let t=await s({url:`/api/payment/create`,method:`post`,data:{chain:e.paymentChain,token:e.paymentToken,order_no:e.orderId,amount:e.amount},headers:this.headers});if(!t)throw Error(`Create Payment Failed`);return{orderId:e.orderId,paymentId:t.payment_id,paymentAmount:t.pay_amount,paymentChain:e.paymentChain,paymentToken:e.paymentToken}}async checkPaymentStatus(e,t=3e3){let n=10;for(;n--;){if((await s({url:`/api/payment/query`,data:{payment_id:e},headers:this.headers}))?.status===`success`)return;await c(t)}throw Error(`Check Payment Status Timeout, PaymentId: ${e}`)}},u=l;exports.CHAINS=i,exports.CHAIN_ICONS=t,exports.CHAIN_TOKENS=e,exports.Cppay=u,exports.TOKENS=r,exports.TOKEN_ICONS=n;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export declare type Chain = "Ethereum" | "BSC" | "Tron" | "Polygon" | "Solana";
|
|
2
|
+
|
|
3
|
+
export declare const CHAIN_ICONS: Record<Chain, string>;
|
|
4
|
+
|
|
5
|
+
export declare const CHAIN_TOKENS: Record<Chain, Token[]>;
|
|
6
|
+
|
|
7
|
+
export declare const CHAINS: {
|
|
8
|
+
name: Chain;
|
|
9
|
+
icon: string;
|
|
10
|
+
}[];
|
|
11
|
+
|
|
12
|
+
export declare class Cppay {
|
|
13
|
+
static readonly supportedChains: {
|
|
14
|
+
name: Chain;
|
|
15
|
+
icon: string;
|
|
16
|
+
}[];
|
|
17
|
+
static readonly chainTokens: Record<Chain, Token[]>;
|
|
18
|
+
static getSupportedTokens(chain: Chain): {
|
|
19
|
+
symbol: Token;
|
|
20
|
+
icon: string;
|
|
21
|
+
}[];
|
|
22
|
+
private apikey;
|
|
23
|
+
constructor(apikey: string);
|
|
24
|
+
get headers(): {
|
|
25
|
+
Authorization: string;
|
|
26
|
+
};
|
|
27
|
+
createPayment(params: {
|
|
28
|
+
paymentChain: Chain;
|
|
29
|
+
paymentToken: Token;
|
|
30
|
+
orderId: string;
|
|
31
|
+
amount: Numberic;
|
|
32
|
+
}): Promise<PaymentInfo>;
|
|
33
|
+
checkPaymentStatus(paymentId: string, interval?: number): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export declare interface CppayResponse<R> {
|
|
37
|
+
code: number;
|
|
38
|
+
message: string;
|
|
39
|
+
data: R extends object ? R | null : R;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export declare type Numberic = `${number}.${number}` | `${number}`;
|
|
43
|
+
|
|
44
|
+
export declare interface PaymentInfo {
|
|
45
|
+
orderId: string;
|
|
46
|
+
paymentId: string;
|
|
47
|
+
paymentAmount: Numberic;
|
|
48
|
+
paymentChain: Chain;
|
|
49
|
+
paymentToken: Token;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export declare type Token = "USDT" | "USDC" | "ETH" | "BNB" | "TRX" | "SOL";
|
|
53
|
+
|
|
54
|
+
export declare const TOKEN_ICONS: Record<Token, string>;
|
|
55
|
+
|
|
56
|
+
export declare const TOKENS: {
|
|
57
|
+
symbol: Token;
|
|
58
|
+
icon: string;
|
|
59
|
+
}[];
|
|
60
|
+
|
|
61
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const CHAIN_TOKENS = {
|
|
2
|
+
Ethereum: [
|
|
3
|
+
"USDT",
|
|
4
|
+
"USDC",
|
|
5
|
+
"ETH"
|
|
6
|
+
],
|
|
7
|
+
BSC: [
|
|
8
|
+
"USDT",
|
|
9
|
+
"USDC",
|
|
10
|
+
"BNB"
|
|
11
|
+
],
|
|
12
|
+
Tron: [
|
|
13
|
+
"USDT",
|
|
14
|
+
"USDC",
|
|
15
|
+
"TRX"
|
|
16
|
+
],
|
|
17
|
+
Polygon: ["USDT", "USDC"],
|
|
18
|
+
Solana: [
|
|
19
|
+
"USDT",
|
|
20
|
+
"USDC",
|
|
21
|
+
"SOL"
|
|
22
|
+
]
|
|
23
|
+
}, CHAIN_ICONS = {
|
|
24
|
+
Ethereum: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png",
|
|
25
|
+
BSC: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png",
|
|
26
|
+
Tron: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png",
|
|
27
|
+
Polygon: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/polygon/info/logo.png",
|
|
28
|
+
Solana: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png"
|
|
29
|
+
}, TOKEN_ICONS = {
|
|
30
|
+
USDT: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xdAC17F958D2ee523a2206206994597C13D831ec7/logo.png",
|
|
31
|
+
USDC: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png",
|
|
32
|
+
ETH: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png",
|
|
33
|
+
BNB: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png",
|
|
34
|
+
TRX: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png",
|
|
35
|
+
SOL: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png"
|
|
36
|
+
}, TOKENS = Object.entries(TOKEN_ICONS).map(([e, i]) => ({
|
|
37
|
+
symbol: e,
|
|
38
|
+
icon: i
|
|
39
|
+
})), CHAINS = Object.entries(CHAIN_ICONS).map(([e, i]) => ({
|
|
40
|
+
name: e,
|
|
41
|
+
icon: i
|
|
42
|
+
}));
|
|
43
|
+
var request = async (e, i, a) => {
|
|
44
|
+
let o = Object.assign({ timeout: 15e3 }, a), s = new AbortController();
|
|
45
|
+
setTimeout(() => s.abort(), o.timeout);
|
|
46
|
+
let c = await fetch(e, {
|
|
47
|
+
signal: s.signal,
|
|
48
|
+
...i
|
|
49
|
+
}), l = c.headers.get("Content-Type") ?? "";
|
|
50
|
+
if (l.includes("json")) return await c.json();
|
|
51
|
+
try {
|
|
52
|
+
return await c.json();
|
|
53
|
+
} catch {
|
|
54
|
+
throw Error(`Unsupport Content Type: ${l}`);
|
|
55
|
+
}
|
|
56
|
+
}, SUCCESS_CODE = 0;
|
|
57
|
+
const cppayRequest = async (e) => {
|
|
58
|
+
let i = await request(e.url, {
|
|
59
|
+
method: e.method ?? "get",
|
|
60
|
+
headers: e.headers,
|
|
61
|
+
body: JSON.stringify(e.data)
|
|
62
|
+
});
|
|
63
|
+
if (i.code !== SUCCESS_CODE) throw Error(i.message);
|
|
64
|
+
return i.data;
|
|
65
|
+
}, sleep = (e = 1e3) => new Promise((i) => setTimeout(i, e));
|
|
66
|
+
var cppay_default = class i {
|
|
67
|
+
static supportedChains = CHAINS;
|
|
68
|
+
static chainTokens = CHAIN_TOKENS;
|
|
69
|
+
static getSupportedTokens(e) {
|
|
70
|
+
let a = [];
|
|
71
|
+
return Object.entries(i.chainTokens).some(([i, s]) => {
|
|
72
|
+
if (i === e) return a = TOKENS.filter((e) => s.includes(e.symbol)), !0;
|
|
73
|
+
}), a;
|
|
74
|
+
}
|
|
75
|
+
apikey;
|
|
76
|
+
constructor(e) {
|
|
77
|
+
this.apikey = e;
|
|
78
|
+
}
|
|
79
|
+
get headers() {
|
|
80
|
+
return { Authorization: this.apikey };
|
|
81
|
+
}
|
|
82
|
+
async createPayment(e) {
|
|
83
|
+
let i = await cppayRequest({
|
|
84
|
+
url: "/api/payment/create",
|
|
85
|
+
method: "post",
|
|
86
|
+
data: {
|
|
87
|
+
chain: e.paymentChain,
|
|
88
|
+
token: e.paymentToken,
|
|
89
|
+
order_no: e.orderId,
|
|
90
|
+
amount: e.amount
|
|
91
|
+
},
|
|
92
|
+
headers: this.headers
|
|
93
|
+
});
|
|
94
|
+
if (!i) throw Error("Create Payment Failed");
|
|
95
|
+
return {
|
|
96
|
+
orderId: e.orderId,
|
|
97
|
+
paymentId: i.payment_id,
|
|
98
|
+
paymentAmount: i.pay_amount,
|
|
99
|
+
paymentChain: e.paymentChain,
|
|
100
|
+
paymentToken: e.paymentToken
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async checkPaymentStatus(e, i = 3e3) {
|
|
104
|
+
let a = 10;
|
|
105
|
+
for (; a--;) {
|
|
106
|
+
if ((await cppayRequest({
|
|
107
|
+
url: "/api/payment/query",
|
|
108
|
+
data: { payment_id: e },
|
|
109
|
+
headers: this.headers
|
|
110
|
+
}))?.status === "success") return;
|
|
111
|
+
await sleep(i);
|
|
112
|
+
}
|
|
113
|
+
throw Error(`Check Payment Status Timeout, PaymentId: ${e}`);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
export { CHAINS, CHAIN_ICONS, CHAIN_TOKENS, cppay_default as Cppay, TOKENS, TOKEN_ICONS };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cppay-sdk",
|
|
3
|
+
"version": "0.0.2-beta.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "vite",
|
|
7
|
+
"build": "vite build",
|
|
8
|
+
"test": "vitest",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^25.0.8",
|
|
20
|
+
"typescript": "~5.9.3",
|
|
21
|
+
"vite": "npm:rolldown-vite@7.2.5",
|
|
22
|
+
"vite-plugin-dts": "^4.5.4",
|
|
23
|
+
"vitest": "^4.0.17"
|
|
24
|
+
},
|
|
25
|
+
"resolutions": {
|
|
26
|
+
"vite": "npm:rolldown-vite@7.2.5"
|
|
27
|
+
},
|
|
28
|
+
"packageManager": "yarn@4.12.0",
|
|
29
|
+
"description": "",
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": "",
|
|
33
|
+
"keywords": []
|
|
34
|
+
}
|