-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptomus.js
More file actions
91 lines (79 loc) · 2.75 KB
/
cryptomus.js
File metadata and controls
91 lines (79 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const crypto = require('crypto');
const axios = require('axios');
class Cryptomus {
constructor(merchantId, paymentKey) {
this.merchantId = merchantId;
this.paymentKey = paymentKey;
this.apiUrl = 'https://api.cryptomus.com/v1';
}
createSignature(payload) {
const data = Buffer.from(JSON.stringify(payload)).toString('base64');
return crypto.createHash('md5').update(data + this.paymentKey).digest('hex');
}
async createPayment(options) {
const payload = {
merchant_id: this.merchantId,
order_id: options.orderId,
amount: options.amount,
currency: options.currency || 'USD',
network: options.network || 'ETH',
url_callback: options.callbackUrl,
url_return: options.returnUrl,
is_payment_multiple: false,
lifetime: options.lifetime || 3600,
to_currency: options.toCurrency || 'ETH'
};
const sign = this.createSignature(payload);
try {
const response = await axios.post(`${this.apiUrl}/payment`, payload, {
headers: {
'merchant': this.merchantId,
'sign': sign
}
});
return response.data;
} catch (error) {
return error.response.data;
}
}
async getPaymentStatus(orderId) {
const payload = {
merchant_id: this.merchantId,
order_id: orderId
};
const sign = this.createSignature(payload);
try {
const response = await axios.post(`${this.apiUrl}/payment/status`, payload, {
headers: {
'merchant': this.merchantId,
'sign': sign
}
});
return response.data;
} catch (error) {
throw new Error(`Payment status query error: ${error.message}`);
}
}
async testWebhook(payload) {
const sign = this.createSignature(payload);
try {
const response = await axios.post(`${this.apiUrl}/test-webhook/payment`, payload, {
headers: {
'merchant': this.merchantId,
'sign': sign
}
});
return response.data;
}
catch (error) {
throw new Error(`Test webhook error: ${error.message}`);
}
}
verifyWebhook(payload, signature) {
if (payload?.sign) delete payload.sign;
const data = Buffer.from(JSON.stringify(payload)).toString('base64');
const hash = crypto.createHash('md5').update(data + this.paymentKey).digest('hex');
return hash === signature;
}
}
module.exports = Cryptomus;