first commit
This commit is contained in:
76
app/helpers/payment_logger.helper.js
Normal file
76
app/helpers/payment_logger.helper.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const db = require('../../models/migration');
|
||||
|
||||
class PaymentLoggerHelper {
|
||||
async log(midtransResponse, requestPayload = null, type = 'PAYMENT') {
|
||||
const orderIdInternal = requestPayload ? requestPayload.order_id : null;
|
||||
const orderIdExternal = midtransResponse.order_id || (requestPayload ? requestPayload.external_id : null);
|
||||
|
||||
const grossAmount = midtransResponse.gross_amount || (requestPayload ? requestPayload.amount : null);
|
||||
const transactionStatus = midtransResponse.transaction_status || 'pending';
|
||||
const paymentType = midtransResponse.payment_type || (requestPayload ? 'snap' : null);
|
||||
const transactionId = midtransResponse.transaction_id || null;
|
||||
|
||||
if (!orderIdInternal && !orderIdExternal) {
|
||||
console.warn(`[PaymentLogger] Tidak ada ID (internal/external) ditemukan. Lewati pencatatan.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
let order = null;
|
||||
|
||||
if (orderIdInternal) {
|
||||
order = await db.Order.findByPk(orderIdInternal);
|
||||
}
|
||||
|
||||
if (!order && orderIdExternal) {
|
||||
order = await db.Order.findOne({
|
||||
where: { orderID: orderIdExternal }
|
||||
});
|
||||
}
|
||||
|
||||
if (!order) {
|
||||
console.warn(`[PaymentLogger] Order tidak ditemukan untuk Internal ID: ${orderIdInternal} atau External ID: ${orderIdExternal}.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
let mappedStatus = 'PENDING';
|
||||
if (['settlement', 'capture'].includes(transactionStatus)) {
|
||||
mappedStatus = 'SUCCESS';
|
||||
} else if (['deny', 'cancel', 'expire'].includes(transactionStatus)) {
|
||||
mappedStatus = 'FAILED';
|
||||
}
|
||||
|
||||
const payment = await db.Payment.create({
|
||||
order_id: order.id,
|
||||
user_id: order.user_id || null,
|
||||
amount: order.total || grossAmount,
|
||||
response_data: JSON.stringify(midtransResponse),
|
||||
currency: 'IDR',
|
||||
description: midtransResponse.status_message || `Payment for order ${orderIdExternal || order.id}`,
|
||||
type: type,
|
||||
channel_code: paymentType,
|
||||
channel_name: paymentType,
|
||||
channel_type: this._getChannelType(paymentType),
|
||||
status: mappedStatus,
|
||||
third_party: 'MIDTRANS',
|
||||
external_id: orderIdExternal || transactionId
|
||||
});
|
||||
|
||||
console.log(`[PaymentLogger] Berhasil mencatat pembayaran untuk Order: ${order.id} (External: ${orderIdExternal})`);
|
||||
return payment;
|
||||
} catch (error) {
|
||||
console.error(`[PaymentLogger] Gagal mencatat pembayaran: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
_getChannelType(paymentType) {
|
||||
if (!paymentType) return null;
|
||||
if (paymentType.includes('va') || paymentType.includes('bank') || paymentType.includes('echannel')) return 'BANK';
|
||||
if (['gopay', 'shopeepay', 'qris'].includes(paymentType)) return 'E_WALLET';
|
||||
if (paymentType.includes('credit_card')) return 'CREDIT_CARD';
|
||||
return 'QRIS';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PaymentLoggerHelper();
|
||||
24
app/helpers/response.helper.js
Normal file
24
app/helpers/response.helper.js
Normal file
@@ -0,0 +1,24 @@
|
||||
class ResponseHelper {
|
||||
static success(res, message, data = {}, status = 200) {
|
||||
return res.status(status).json({
|
||||
success: true,
|
||||
message,
|
||||
data
|
||||
});
|
||||
}
|
||||
|
||||
static error(res, message, status = 500, errors = null) {
|
||||
const response = {
|
||||
success: false,
|
||||
message
|
||||
};
|
||||
|
||||
if (errors) {
|
||||
response.errors = errors;
|
||||
}
|
||||
|
||||
return res.status(status).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ResponseHelper;
|
||||
Reference in New Issue
Block a user