cppay-sdk 0.0.2-beta.0 → 0.0.2-beta.2
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/dist/index.cjs +1 -1
- package/dist/index.d.ts +15 -22
- package/dist/index.js +64 -80
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e={Ethereum
|
|
1
|
+
const e={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`},t={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`};var n=`http://192.168.0.162:8000`,r=async(e,t,r)=>{let i=Object.assign({timeout:15e3},r),a=new AbortController,o=setTimeout(()=>a.abort(),i.timeout);try{let r=await fetch(new URL(e,n),{signal:a.signal,...t});clearTimeout(o);let i=r.headers.get(`Content-Type`)??``;if(i.includes(`json`))return await r.json();try{return await r.json()}catch{throw Error(`URL: ${e}, Unsupport Content Type: ${i}`)}}catch(t){throw clearTimeout(o),t instanceof DOMException&&t.name===`AbortError`?Error(`URL: ${e}, Request timeout after ${i.timeout}ms`):t}},i=0;const a=async(e,t)=>{let n=await r(t.url,{method:t.method??`get`,headers:{Authorization:e,...t.headers},body:JSON.stringify(t.data)});if(n.code!==i)throw Error(`Request ${t.url} Failed. ${n.message}`);return n.data},o=(e=1e3)=>new Promise(t=>setTimeout(t,e));var s=class{apikey;constructor(e){this.apikey=e}async getSupportedChains(){let n=(await a(this.apikey,{url:`/api/payment/token`})).reduce((n,r)=>{let i=n[r.chain]??{chain:r.chain,tokens:[],icon:e[r.chain]};return i.tokens.push({symbol:r.token_symbol,decimals:r.token_decimals,address:r.token_address,receiveAddresses:r.receive_addresses,icon:t[r.token_symbol]}),n[r.chain]=i,n},{});return Object.values(n)}async createPayment(e){let t={chain:e.paymentChain,token:e.paymentToken,order_no:e.orderId,amount:e.amount},n=await a(this.apikey,{url:`/api/payment/create`,method:`post`,data:t});return{orderId:e.orderId,paymentId:n.payment_id,paymentAmount:n.pay_amount,paymentChain:e.paymentChain,paymentToken:e.paymentToken}}async checkPaymentStatus(e,t=1e3){let n=15;for(;n--;){let n={payment_id:e};if((await a(this.apikey,{url:`/api/payment/query`,data:n})).status===`success`)return;await o(t)}throw Error(`Check Payment Status Timeout, PaymentId: ${e}`)}},c=s;exports.CHAIN_ICONS=e,exports.Cppay=c,exports.TOKEN_ICONS=t;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,19 @@
|
|
|
1
|
+
export declare type Address = `0x${string}`;
|
|
2
|
+
|
|
1
3
|
export declare type Chain = "Ethereum" | "BSC" | "Tron" | "Polygon" | "Solana";
|
|
2
4
|
|
|
3
5
|
export declare const CHAIN_ICONS: Record<Chain, string>;
|
|
4
6
|
|
|
5
|
-
export declare
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}[];
|
|
7
|
+
export declare interface ChainInfo {
|
|
8
|
+
icon?: string;
|
|
9
|
+
chain: Chain;
|
|
10
|
+
tokens: TokenInfo[];
|
|
11
|
+
}
|
|
11
12
|
|
|
12
13
|
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
14
|
private apikey;
|
|
23
15
|
constructor(apikey: string);
|
|
24
|
-
|
|
25
|
-
Authorization: string;
|
|
26
|
-
};
|
|
16
|
+
getSupportedChains(): Promise<ChainInfo[]>;
|
|
27
17
|
createPayment(params: {
|
|
28
18
|
paymentChain: Chain;
|
|
29
19
|
paymentToken: Token;
|
|
@@ -36,7 +26,7 @@ export declare class Cppay {
|
|
|
36
26
|
export declare interface CppayResponse<R> {
|
|
37
27
|
code: number;
|
|
38
28
|
message: string;
|
|
39
|
-
data: R
|
|
29
|
+
data: R;
|
|
40
30
|
}
|
|
41
31
|
|
|
42
32
|
export declare type Numberic = `${number}.${number}` | `${number}`;
|
|
@@ -53,9 +43,12 @@ export declare type Token = "USDT" | "USDC" | "ETH" | "BNB" | "TRX" | "SOL";
|
|
|
53
43
|
|
|
54
44
|
export declare const TOKEN_ICONS: Record<Token, string>;
|
|
55
45
|
|
|
56
|
-
export declare
|
|
46
|
+
export declare interface TokenInfo {
|
|
47
|
+
icon?: string;
|
|
57
48
|
symbol: Token;
|
|
58
|
-
|
|
59
|
-
|
|
49
|
+
decimals: number;
|
|
50
|
+
address: Address | "";
|
|
51
|
+
receiveAddresses: Address[];
|
|
52
|
+
}
|
|
60
53
|
|
|
61
54
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,4 @@
|
|
|
1
|
-
const
|
|
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 = {
|
|
1
|
+
const CHAIN_ICONS = {
|
|
24
2
|
Ethereum: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png",
|
|
25
3
|
BSC: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png",
|
|
26
4
|
Tron: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png",
|
|
@@ -33,84 +11,90 @@ const CHAIN_TOKENS = {
|
|
|
33
11
|
BNB: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png",
|
|
34
12
|
TRX: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/tron/info/logo.png",
|
|
35
13
|
SOL: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png"
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
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();
|
|
14
|
+
};
|
|
15
|
+
var BASE_URL = "http://192.168.0.162:8000", request = async (e, n, i) => {
|
|
16
|
+
let a = Object.assign({ timeout: 15e3 }, i), o = new AbortController(), s = setTimeout(() => o.abort(), a.timeout);
|
|
51
17
|
try {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
18
|
+
let i = await fetch(new URL(e, BASE_URL), {
|
|
19
|
+
signal: o.signal,
|
|
20
|
+
...n
|
|
21
|
+
});
|
|
22
|
+
clearTimeout(s);
|
|
23
|
+
let a = i.headers.get("Content-Type") ?? "";
|
|
24
|
+
if (a.includes("json")) return await i.json();
|
|
25
|
+
try {
|
|
26
|
+
return await i.json();
|
|
27
|
+
} catch {
|
|
28
|
+
throw Error(`URL: ${e}, Unsupport Content Type: ${a}`);
|
|
29
|
+
}
|
|
30
|
+
} catch (n) {
|
|
31
|
+
throw clearTimeout(s), n instanceof DOMException && n.name === "AbortError" ? Error(`URL: ${e}, Request timeout after ${a.timeout}ms`) : n;
|
|
55
32
|
}
|
|
56
33
|
}, SUCCESS_CODE = 0;
|
|
57
|
-
const cppayRequest = async (e) => {
|
|
58
|
-
let
|
|
59
|
-
method:
|
|
60
|
-
headers:
|
|
61
|
-
|
|
34
|
+
const cppayRequest = async (e, n) => {
|
|
35
|
+
let r = await request(n.url, {
|
|
36
|
+
method: n.method ?? "get",
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: e,
|
|
39
|
+
...n.headers
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify(n.data)
|
|
62
42
|
});
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
65
|
-
}, sleep = (e = 1e3) => new Promise((
|
|
66
|
-
var cppay_default = class
|
|
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
|
-
}
|
|
43
|
+
if (r.code !== SUCCESS_CODE) throw Error(`Request ${n.url} Failed. ${r.message}`);
|
|
44
|
+
return r.data;
|
|
45
|
+
}, sleep = (e = 1e3) => new Promise((n) => setTimeout(n, e));
|
|
46
|
+
var cppay_default = class {
|
|
75
47
|
apikey;
|
|
76
48
|
constructor(e) {
|
|
77
49
|
this.apikey = e;
|
|
78
50
|
}
|
|
79
|
-
|
|
80
|
-
|
|
51
|
+
async getSupportedChains() {
|
|
52
|
+
let r = (await cppayRequest(this.apikey, { url: "/api/payment/token" })).reduce((r, i) => {
|
|
53
|
+
let a = r[i.chain] ?? {
|
|
54
|
+
chain: i.chain,
|
|
55
|
+
tokens: [],
|
|
56
|
+
icon: CHAIN_ICONS[i.chain]
|
|
57
|
+
};
|
|
58
|
+
return a.tokens.push({
|
|
59
|
+
symbol: i.token_symbol,
|
|
60
|
+
decimals: i.token_decimals,
|
|
61
|
+
address: i.token_address,
|
|
62
|
+
receiveAddresses: i.receive_addresses,
|
|
63
|
+
icon: TOKEN_ICONS[i.token_symbol]
|
|
64
|
+
}), r[i.chain] = a, r;
|
|
65
|
+
}, {});
|
|
66
|
+
return Object.values(r);
|
|
81
67
|
}
|
|
82
68
|
async createPayment(e) {
|
|
83
|
-
let
|
|
69
|
+
let n = {
|
|
70
|
+
chain: e.paymentChain,
|
|
71
|
+
token: e.paymentToken,
|
|
72
|
+
order_no: e.orderId,
|
|
73
|
+
amount: e.amount
|
|
74
|
+
}, r = await cppayRequest(this.apikey, {
|
|
84
75
|
url: "/api/payment/create",
|
|
85
76
|
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
|
|
77
|
+
data: n
|
|
93
78
|
});
|
|
94
|
-
if (!i) throw Error("Create Payment Failed");
|
|
95
79
|
return {
|
|
96
80
|
orderId: e.orderId,
|
|
97
|
-
paymentId:
|
|
98
|
-
paymentAmount:
|
|
81
|
+
paymentId: r.payment_id,
|
|
82
|
+
paymentAmount: r.pay_amount,
|
|
99
83
|
paymentChain: e.paymentChain,
|
|
100
84
|
paymentToken: e.paymentToken
|
|
101
85
|
};
|
|
102
86
|
}
|
|
103
|
-
async checkPaymentStatus(e,
|
|
104
|
-
let
|
|
105
|
-
for (;
|
|
106
|
-
|
|
87
|
+
async checkPaymentStatus(e, n = 1e3) {
|
|
88
|
+
let r = 15;
|
|
89
|
+
for (; r--;) {
|
|
90
|
+
let r = { payment_id: e };
|
|
91
|
+
if ((await cppayRequest(this.apikey, {
|
|
107
92
|
url: "/api/payment/query",
|
|
108
|
-
data:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
await sleep(i);
|
|
93
|
+
data: r
|
|
94
|
+
})).status === "success") return;
|
|
95
|
+
await sleep(n);
|
|
112
96
|
}
|
|
113
97
|
throw Error(`Check Payment Status Timeout, PaymentId: ${e}`);
|
|
114
98
|
}
|
|
115
99
|
};
|
|
116
|
-
export {
|
|
100
|
+
export { CHAIN_ICONS, cppay_default as Cppay, TOKEN_ICONS };
|