Node.js Integration Guide
index.js – Start Payment
index.js
1const axios = require('axios');
2
3async function requestToken(apiKey, amount, callbackUrl, mobile, email, description) {
4 try {
5 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/token', {
6 api_key: apiKey,
7 amount: amount,
8 callback_url: callbackUrl,
9 mobile: mobile,
10 email: email,
11 description: description,
12 }, {
13 headers: { 'Content-Type': 'application/json' },
14 });
15
16 return response.data;
17 } catch (error) {
18 return { success: false, error: error.response?.data || error.message };
19 }
20}
21
22async function verifyPayment(apiKey, token, amount) {
23 try {
24 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/verify', {
25 api_key: apiKey,
26 token: token,
27 amount: amount,
28 }, {
29 headers: { 'Content-Type': 'application/json' },
30 });
31
32 return response.data;
33 } catch (error) {
34 return { success: false, error: error.response?.data || error.message };
35 }
36}
37
38module.exports = { requestToken, verifyPayment };
/verify route – Verify Transaction
index.js
1const axios = require('axios');
2
3async function requestToken(apiKey, amount, callbackUrl, mobile, email, description) {
4 try {
5 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/token', {
6 api_key: apiKey,
7 amount: amount,
8 callback_url: callbackUrl,
9 mobile: mobile,
10 email: email,
11 description: description,
12 }, {
13 headers: { 'Content-Type': 'application/json' },
14 });
15
16 return response.data;
17 } catch (error) {
18 return { success: false, error: error.response?.data || error.message };
19 }
20}
21
22async function verifyPayment(apiKey, token, amount) {
23 try {
24 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/verify', {
25 api_key: apiKey,
26 token: token,
27 amount: amount,
28 }, {
29 headers: { 'Content-Type': 'application/json' },
30 });
31
32 return response.data;
33 } catch (error) {
34 return { success: false, error: error.response?.data || error.message };
35 }
36}
37
38module.exports = { requestToken, verifyPayment };
functions.js – Helper Functions
index.js
1const axios = require('axios');
2
3async function requestToken(apiKey, amount, callbackUrl, mobile, email, description) {
4 try {
5 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/token', {
6 api_key: apiKey,
7 amount: amount,
8 callback_url: callbackUrl,
9 mobile: mobile,
10 email: email,
11 description: description,
12 }, {
13 headers: { 'Content-Type': 'application/json' },
14 });
15
16 return response.data;
17 } catch (error) {
18 return { success: false, error: error.response?.data || error.message };
19 }
20}
21
22async function verifyPayment(apiKey, token, amount) {
23 try {
24 const response = await axios.post('https://sandbox.coinpay.ir/api/v1/verify', {
25 api_key: apiKey,
26 token: token,
27 amount: amount,
28 }, {
29 headers: { 'Content-Type': 'application/json' },
30 });
31
32 return response.data;
33 } catch (error) {
34 return { success: false, error: error.response?.data || error.message };
35 }
36}
37
38module.exports = { requestToken, verifyPayment };
Sandbox mode API endpoint:
1https://sandbox.coinpay.ir/api/v1
Sandbox mode API endpoint:
1https://coinpay.ir/api/v1
Important Notes
Install required packages.
1npm init -y
2npm install express axios express-session
- Switch API base from https://sandbox.coinpay.ir to https://coinpay.ir in production.
- Use a session store like Redis or MongoDB for better security.