purchase / checkout flow

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-08-31 17:57:29 +08:00
parent 7413a296fb
commit 79203b1654
6 changed files with 40 additions and 19 deletions
@@ -76,13 +76,17 @@ exports.captureCourseOrder = async (req, res) => {
const { order_id } = req.body;
if (!order_id) return R.error(res, 'order_id is required.', 400);
const purchase = await mdl_CoursePurchase.findOne({
// Matched on provider_payload.order_id, not just "most recent pending" —
// see tiers.controller.js#captureOrder for why picking by recency alone
// can miss an older order that PayPal legitimately approved.
const pendingPurchases = await mdl_CoursePurchase.findAll({
where: { user_id: req.user.user_id, status: 'pending' },
include: [{ model: mdl_Product, as: 'product' }],
order: [['createdAt', 'DESC']],
});
const purchase = pendingPurchases.find((p) => p.provider_payload?.order_id === order_id);
if (!purchase || purchase.provider_payload?.order_id !== order_id)
if (!purchase)
return R.error(res, 'Pending purchase not found.', 404);
let captureData;
@@ -144,12 +148,14 @@ exports.cancelCourseOrder = async (req, res) => {
const { order_id } = req.body;
if (!order_id) return R.error(res, 'order_id is required.', 400);
const purchase = await mdl_CoursePurchase.findOne({
// See captureCourseOrder above for why this matches on order_id instead of recency.
const pendingPurchases = await mdl_CoursePurchase.findAll({
where: { user_id: req.user.user_id, status: 'pending' },
order: [['createdAt', 'DESC']],
});
const purchase = pendingPurchases.find((p) => p.provider_payload?.order_id === order_id);
if (!purchase || purchase.provider_payload?.order_id !== order_id)
if (!purchase)
return R.error(res, 'Pending purchase not found.', 404);
await purchase.update({