cppay-sdk 0.0.2-beta.1 → 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 +5 -5
- package/dist/index.js +43 -65
- 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,9 +1,9 @@
|
|
|
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 const CHAIN_TOKENS: Record<Chain, Token[]>;
|
|
6
|
-
|
|
7
7
|
export declare interface ChainInfo {
|
|
8
8
|
icon?: string;
|
|
9
9
|
chain: Chain;
|
|
@@ -45,10 +45,10 @@ export declare const TOKEN_ICONS: Record<Token, string>;
|
|
|
45
45
|
|
|
46
46
|
export declare interface TokenInfo {
|
|
47
47
|
icon?: string;
|
|
48
|
-
symbol:
|
|
48
|
+
symbol: Token;
|
|
49
49
|
decimals: number;
|
|
50
|
-
address:
|
|
51
|
-
receiveAddresses:
|
|
50
|
+
address: Address | "";
|
|
51
|
+
receiveAddresses: Address[];
|
|
52
52
|
}
|
|
53
53
|
|
|
54
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",
|
|
@@ -34,89 +12,89 @@ const CHAIN_TOKENS = {
|
|
|
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
14
|
};
|
|
37
|
-
var BASE_URL = "http://192.168.0.162:8000", request = async (e,
|
|
38
|
-
let
|
|
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);
|
|
39
17
|
try {
|
|
40
18
|
let i = await fetch(new URL(e, BASE_URL), {
|
|
41
|
-
signal:
|
|
42
|
-
...
|
|
19
|
+
signal: o.signal,
|
|
20
|
+
...n
|
|
43
21
|
});
|
|
44
|
-
clearTimeout(
|
|
45
|
-
let
|
|
46
|
-
if (
|
|
22
|
+
clearTimeout(s);
|
|
23
|
+
let a = i.headers.get("Content-Type") ?? "";
|
|
24
|
+
if (a.includes("json")) return await i.json();
|
|
47
25
|
try {
|
|
48
26
|
return await i.json();
|
|
49
27
|
} catch {
|
|
50
|
-
throw Error(`URL: ${e}, Unsupport Content Type: ${
|
|
28
|
+
throw Error(`URL: ${e}, Unsupport Content Type: ${a}`);
|
|
51
29
|
}
|
|
52
|
-
} catch (
|
|
53
|
-
throw clearTimeout(
|
|
30
|
+
} catch (n) {
|
|
31
|
+
throw clearTimeout(s), n instanceof DOMException && n.name === "AbortError" ? Error(`URL: ${e}, Request timeout after ${a.timeout}ms`) : n;
|
|
54
32
|
}
|
|
55
33
|
}, SUCCESS_CODE = 0;
|
|
56
|
-
const cppayRequest = async (e,
|
|
57
|
-
let
|
|
58
|
-
method:
|
|
34
|
+
const cppayRequest = async (e, n) => {
|
|
35
|
+
let r = await request(n.url, {
|
|
36
|
+
method: n.method ?? "get",
|
|
59
37
|
headers: {
|
|
60
38
|
Authorization: e,
|
|
61
|
-
...
|
|
39
|
+
...n.headers
|
|
62
40
|
},
|
|
63
|
-
body: JSON.stringify(
|
|
41
|
+
body: JSON.stringify(n.data)
|
|
64
42
|
});
|
|
65
|
-
if (
|
|
66
|
-
return
|
|
67
|
-
}, sleep = (e = 1e3) => new Promise((
|
|
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));
|
|
68
46
|
var cppay_default = class {
|
|
69
47
|
apikey;
|
|
70
48
|
constructor(e) {
|
|
71
49
|
this.apikey = e;
|
|
72
50
|
}
|
|
73
51
|
async getSupportedChains() {
|
|
74
|
-
let
|
|
75
|
-
let
|
|
76
|
-
chain:
|
|
52
|
+
let r = (await cppayRequest(this.apikey, { url: "/api/payment/token" })).reduce((r, i) => {
|
|
53
|
+
let a = r[i.chain] ?? {
|
|
54
|
+
chain: i.chain,
|
|
77
55
|
tokens: [],
|
|
78
|
-
icon: CHAIN_ICONS[
|
|
56
|
+
icon: CHAIN_ICONS[i.chain]
|
|
79
57
|
};
|
|
80
|
-
return
|
|
81
|
-
symbol:
|
|
82
|
-
decimals:
|
|
83
|
-
address:
|
|
84
|
-
receiveAddresses:
|
|
85
|
-
icon: TOKEN_ICONS[
|
|
86
|
-
}),
|
|
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;
|
|
87
65
|
}, {});
|
|
88
|
-
return Object.values(
|
|
66
|
+
return Object.values(r);
|
|
89
67
|
}
|
|
90
68
|
async createPayment(e) {
|
|
91
|
-
let
|
|
69
|
+
let n = {
|
|
92
70
|
chain: e.paymentChain,
|
|
93
71
|
token: e.paymentToken,
|
|
94
72
|
order_no: e.orderId,
|
|
95
73
|
amount: e.amount
|
|
96
|
-
},
|
|
74
|
+
}, r = await cppayRequest(this.apikey, {
|
|
97
75
|
url: "/api/payment/create",
|
|
98
76
|
method: "post",
|
|
99
|
-
data:
|
|
77
|
+
data: n
|
|
100
78
|
});
|
|
101
79
|
return {
|
|
102
80
|
orderId: e.orderId,
|
|
103
|
-
paymentId:
|
|
104
|
-
paymentAmount:
|
|
81
|
+
paymentId: r.payment_id,
|
|
82
|
+
paymentAmount: r.pay_amount,
|
|
105
83
|
paymentChain: e.paymentChain,
|
|
106
84
|
paymentToken: e.paymentToken
|
|
107
85
|
};
|
|
108
86
|
}
|
|
109
|
-
async checkPaymentStatus(e,
|
|
110
|
-
let
|
|
111
|
-
for (;
|
|
112
|
-
let
|
|
87
|
+
async checkPaymentStatus(e, n = 1e3) {
|
|
88
|
+
let r = 15;
|
|
89
|
+
for (; r--;) {
|
|
90
|
+
let r = { payment_id: e };
|
|
113
91
|
if ((await cppayRequest(this.apikey, {
|
|
114
92
|
url: "/api/payment/query",
|
|
115
|
-
data:
|
|
93
|
+
data: r
|
|
116
94
|
})).status === "success") return;
|
|
117
|
-
await sleep(
|
|
95
|
+
await sleep(n);
|
|
118
96
|
}
|
|
119
97
|
throw Error(`Check Payment Status Timeout, PaymentId: ${e}`);
|
|
120
98
|
}
|
|
121
99
|
};
|
|
122
|
-
export { CHAIN_ICONS,
|
|
100
|
+
export { CHAIN_ICONS, cppay_default as Cppay, TOKEN_ICONS };
|