UNPKG

wahdx-api

Version:

Package untuk generate QRIS dan cek payment status secara realtime dengan API OrderKuota dari https://api.wahdx.co

92 lines (81 loc) 3.47 kB
const axios = require("axios"); const moment = require("moment-timezone"); class PaymentChecker { constructor(config) { if (!config.tokenKey || !config.auth_username || !config.auth_token) { throw new Error('tokenKey, auth_username, dan auth_token harus diisi'); } this.config = { tokenKey: config.tokenKey, auth_username: config.auth_username, auth_token: config.auth_token }; } async checkPaymentStatus(reference, amount) { try { if (!reference || !amount || amount <= 0) { throw new Error('Reference dan amount harus diisi dengan benar'); } const response = await axios.post( 'https://api.wahdx.co/api/mutasi-orkut-v2', { username_orkut: this.config.auth_username, token_orkut: this.config.auth_token }, { headers: { 'tokenKey': this.config.tokenKey, 'Content-Type': 'application/json' } } ); if (!response.data || !response.data.status || !response.data.data) { throw new Error('Response tidak valid dari server'); } const transactions = response.data.data; const matchingTransactions = transactions.filter(tx => { const txAmount = parseInt(tx.amount); // Parse tanggal dengan timezone Jakarta const txDate = moment.tz(tx.date, 'YYYY-MM-DD HH:mm', 'Asia/Jakarta'); const now = moment().tz("Asia/Jakarta"); const timeDiff = now.diff(txDate, 'milliseconds'); return txAmount === amount && tx.qris === "static" && tx.type === "CR" && timeDiff <= 5 * 60 * 1000; }); if (matchingTransactions.length > 0) { const latestTransaction = matchingTransactions.reduce((latest, current) => { const currentDate = moment.tz(current.date, 'YYYY-MM-DD HH:mm', 'Asia/Jakarta'); const latestDate = moment.tz(latest.date, 'YYYY-MM-DD HH:mm', 'Asia/Jakarta'); return currentDate.isAfter(latestDate) ? current : latest; }); return { success: true, data: { status: 'PAID', amount: parseInt(latestTransaction.amount), reference: latestTransaction.issuer_reff, date: latestTransaction.date, brand_name: latestTransaction.brand_name, buyer_reff: latestTransaction.buyer_reff } }; } return { success: true, data: { status: 'UNPAID', amount: amount, reference: reference } }; } catch (error) { return { success: false, error: 'Gagal cek status pembayaran: ' + error.message }; } } } module.exports = PaymentChecker;