first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
const transactionService = require('../services/transaction.service');
|
||||
const responseHelper = require('../../../helpers/response.helper');
|
||||
|
||||
class TransactionController {
|
||||
static async getStatus(req, res) {
|
||||
try {
|
||||
const { order_id } = req.params;
|
||||
const result = await transactionService.getStatus(order_id);
|
||||
return responseHelper.success(res, "Status transaksi berhasil diambil", result);
|
||||
} catch (error) {
|
||||
return responseHelper.error(res, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async refund(req, res) {
|
||||
try {
|
||||
const payload = req.body;
|
||||
if (!payload.order_id || !payload.amount) {
|
||||
return responseHelper.error(res, "order_id dan amount wajib diisi untuk refund", 400);
|
||||
}
|
||||
const result = await transactionService.refund(payload);
|
||||
return responseHelper.success(res, "Permintaan refund berhasil diproses", result);
|
||||
} catch (error) {
|
||||
return responseHelper.error(res, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async cancel(req, res) {
|
||||
try {
|
||||
const { order_id } = req.params;
|
||||
const result = await transactionService.cancel(order_id);
|
||||
return responseHelper.success(res, "Transaksi berhasil dibatalkan", result);
|
||||
} catch (error) {
|
||||
return responseHelper.error(res, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TransactionController;
|
||||
10
app/modules/transaction/routes/transaction.route.js
Normal file
10
app/modules/transaction/routes/transaction.route.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const transactionController = require('../controllers/transaction.controller');
|
||||
|
||||
|
||||
router.get('/status/:order_id', transactionController.getStatus);
|
||||
router.post('/refund', transactionController.refund);
|
||||
router.post('/cancel/:order_id', transactionController.cancel);
|
||||
|
||||
module.exports = router;
|
||||
40
app/modules/transaction/services/transaction.service.js
Normal file
40
app/modules/transaction/services/transaction.service.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const midtransCore = require('../../../core/midtrans.service');
|
||||
|
||||
class TransactionService {
|
||||
async getStatus(orderId) {
|
||||
try {
|
||||
const response = await midtransCore.getCoreApi().transaction.status(orderId);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`Gagal mengambil status transaksi: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async refund(payload) {
|
||||
const { order_id, amount, reason, refund_key } = payload;
|
||||
|
||||
const parameter = {
|
||||
refund_key: refund_key || `ref-${order_id}-${Date.now()}`,
|
||||
amount: amount,
|
||||
reason: reason || "Customer request"
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await midtransCore.getCoreApi().transaction.refund(order_id, parameter);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`Gagal melakukan refund: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async cancel(orderId) {
|
||||
try {
|
||||
const response = await midtransCore.getCoreApi().transaction.cancel(orderId);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`Gagal membatalkan transaksi: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new TransactionService();
|
||||
Reference in New Issue
Block a user