autoft-qris
Version:
Package untuk generate QRIS dengan 2 tema (Biru & Hijau) dan cek payment status secara realtime dengan API OrderKuota
138 lines (124 loc) • 4.65 kB
JavaScript
const axios = require('axios');
const moment = require('moment');
class PaymentChecker {
constructor(config) {
if (!config.auth_token || !config.auth_username) {
throw new Error('auth_token dan auth_username harus diisi');
}
this.config = {
auth_token: config.auth_token,
auth_username: config.auth_username
};
}
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://orkut.ftvpn.me/api/mutasi',
{
auth_token: this.config.auth_token,
auth_username: this.config.auth_username
},
{
headers: {
'Content-Type': 'application/json'
}
}
);
if (
!response.data ||
response.data.status !== true ||
!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);
const txDate = moment(tx.date, 'YYYY-MM-DD HH:mm').toDate();
const now = new Date();
const timeDiff = now - txDate;
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(current.date, 'YYYY-MM-DD HH:mm').toDate();
const latestDate = moment(latest.date, 'YYYY-MM-DD HH:mm').toDate();
return currentDate > 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
};
}
}
async checkSaldo() {
try {
const response = await axios.post(
'https://orkut.ftvpn.me/api/saldo',
{
auth_token: this.config.auth_token,
auth_username: this.config.auth_username
},
{
headers: {
'Content-Type': 'application/json'
},
timeout: this.config.axiosTimeoutMs
}
);
if (!response || !response.data || response.data.status !== true) {
throw new Error('Response tidak valid dari server');
}
const payload = response.data.data;
let saldo = null;
if (payload && (typeof payload.saldo === 'number' || typeof payload.saldo === 'string')) {
const parsed = typeof payload.saldo === 'string' ? parseInt(payload.saldo.replace(/[^0-9-]/g, ''), 10) : payload.saldo;
if (Number.isFinite(parsed)) {
saldo = parsed;
}
}
return {
success: true,
data: {
saldo: saldo,
raw: payload
}
};
} catch (error) {
return {
success: false,
error: 'Gagal cek saldo: ' + error.message
};
}
}
}
module.exports = PaymentChecker;