-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6be5714
commit 4b2c3f3
Showing
6 changed files
with
70 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import midtransSnap from "@/lib/payment/midtrans"; | ||
import { MidtransNotification, Order, ordersCollection } from "@/lib/db/order_collection"; | ||
import { ObjectId } from "mongodb"; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const body = await req.json(); | ||
|
||
const response = await midtransSnap.transaction.notification(body); | ||
const { order_id: orderId, transaction_status: transactionStatus, fraud_status: fraudStatus } = response; | ||
|
||
console.log(`Transaction notification received. Order ID: ${orderId}. Transaction status: ${transactionStatus}. Fraud status: ${fraudStatus}`); | ||
|
||
const orderStatus = getOrderStatus(transactionStatus, fraudStatus); | ||
|
||
await updateOrderStatus(orderId, orderStatus, body); | ||
|
||
return NextResponse.json({ message: 'OK' }, { status: 200 }); | ||
} catch (error) { | ||
console.error('Error processing payment notification:', error); | ||
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 }); | ||
} | ||
} | ||
|
||
function getOrderStatus(transactionStatus: string, fraudStatus: string): Order['status'] { | ||
if ((transactionStatus === 'capture' && fraudStatus === 'accept') || transactionStatus === 'settlement') { | ||
return 'paid'; | ||
} else if (['cancel', 'deny', 'expire'].includes(transactionStatus)) { | ||
return 'cancelled'; | ||
} else if (transactionStatus === 'pending') { | ||
return 'pending'; | ||
} | ||
|
||
throw new Error(`Unknown transaction status: ${transactionStatus}`); | ||
} | ||
|
||
async function updateOrderStatus(orderId: string, status: Order['status'], notificationBody: MidtransNotification) { | ||
const result = await ordersCollection.updateOne( | ||
{ _id: new ObjectId(orderId) }, | ||
{ | ||
$set: { | ||
status, | ||
"payment.notification": notificationBody | ||
} | ||
} | ||
); | ||
|
||
if (result.matchedCount === 0) { | ||
throw new Error(`Order not found: ${orderId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import midtransClient from "midtrans-client"; | ||
|
||
// Prepare Midtrans payment request | ||
const midtransSnap = new midtransClient.Snap({ | ||
isProduction: process.env.NODE_ENV === "production", | ||
serverKey: process.env.MIDTRANS_SERVER_KEY, | ||
clientKey: process.env.MIDTRANS_CLIENT_KEY | ||
}); | ||
|
||
export default midtransSnap |