|
| 1 | +const axios = require("axios"); |
| 2 | +const crypto = require('crypto'); |
| 3 | + |
| 4 | +const TOKOPAY_API_URL = "https://api.tokopay.id/v1/order"; |
| 5 | +const MERCHANT_ID = "#"; |
| 6 | +const SECRET = "#"; |
| 7 | + |
| 8 | +function generateSignature(merchantId, secret) { |
| 9 | + const hash = crypto.createHash('md5'); |
| 10 | + hash.update(`${merchantId}:${secret}`); |
| 11 | + return hash.digest('hex'); |
| 12 | +} |
| 13 | + |
| 14 | +function generateSignatureForWithdrawal(merchantId, secret, nominal) { |
| 15 | + const hash = crypto.createHash('md5'); |
| 16 | + hash.update(`${merchantId}:${secret}:${nominal}`); |
| 17 | + return hash.digest('hex'); |
| 18 | +} |
| 19 | + |
| 20 | +async function createTransaction(refId, nominal, metode) { |
| 21 | + const url = `${TOKOPAY_API_URL}?merchant=${MERCHANT_ID}&secret=${SECRET}&ref_id=${refId}&nominal=${nominal}&metode=${metode}`; |
| 22 | + |
| 23 | + try { |
| 24 | + const response = await axios.get(url); |
| 25 | + return response.data; |
| 26 | + } catch (error) { |
| 27 | + throw new Error(`Gagal membuat transaksi: ${error.message}`); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function checkPaymentStatus(refId, nominal, metode) { |
| 32 | + const url = `${TOKOPAY_API_URL}?merchant=${MERCHANT_ID}&secret=${SECRET}&ref_id=${refId}&nominal=${nominal}&metode=${metode}`; |
| 33 | + |
| 34 | + try { |
| 35 | + const response = await axios.get(url); |
| 36 | + return response.data; |
| 37 | + } catch (error) { |
| 38 | + throw new Error(`Gagal memeriksa status pembayaran: ${error.message}`); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function checkAccountBalance() { |
| 43 | + const signature = generateSignature(MERCHANT_ID, SECRET); |
| 44 | + const url = `https://api.tokopay.id/v1/merchant/balance?merchant=${MERCHANT_ID}&signature=${signature}`; |
| 45 | + |
| 46 | + try { |
| 47 | + const response = await axios.get(url); |
| 48 | + const { nama_toko, saldo_tersedia, saldo_tertahan } = response.data.data; |
| 49 | + return { nama_toko, saldo_tersedia, saldo_tertahan }; |
| 50 | + } catch (error) { |
| 51 | + throw new Error(`Gagal memeriksa saldo akun: ${error.message}`); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function tarikSaldo(nominal) { |
| 56 | + const signature = generateSignatureForWithdrawal(MERCHANT_ID, SECRET, nominal); |
| 57 | + const url = `https://api.tokopay.id/v1/tarik-saldo`; |
| 58 | + |
| 59 | + try { |
| 60 | + const response = await axios.post(url, { |
| 61 | + nominal: nominal, |
| 62 | + merchant_id: MERCHANT_ID, |
| 63 | + signature: signature |
| 64 | + }); |
| 65 | + return response.data; |
| 66 | + } catch (error) { |
| 67 | + throw new Error(`Gagal melakukan tarik saldo: ${error.message}`); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +module.exports = { |
| 72 | + createTransaction, |
| 73 | + checkPaymentStatus, |
| 74 | + checkAccountBalance, |
| 75 | + tarikSaldo |
| 76 | +}; |
0 commit comments