Skip to content

Commit

Permalink
update webhook service
Browse files Browse the repository at this point in the history
  • Loading branch information
zylcom committed Dec 25, 2023
1 parent 1b3b27b commit 24b0458
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/controllers/webhook-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const webhook = async (req, res, next) => {

res.status(200).json({ received: true });
} catch (error) {
console.log(error);

next(error);
}
};
Expand Down
111 changes: 58 additions & 53 deletions src/services/webhook-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,58 @@ const webhook = async (notificationJson) => {
}
*/

await snap.transaction.notification(notificationJson).then(async (statusResponse) => {
console.log(statusResponse);
await snap.transaction
.notification(notificationJson)
.then(async (statusResponse) => {
console.log(statusResponse);

console.log(
`Transaction notification received.
console.log(
`Transaction notification received.
Order ID: ${statusResponse.order_id}.
Transaction status: ${statusResponse.transaction_status}.
Fraud status: ${statusResponse.fraud_status}`
);
);

if (statusResponse.transaction_status == "capture") {
// capture only applies to card transaction, which you need to check for the fraudStatus
if (fraudStatus == "challenge") {
// TODO set transaction status on your databaase to 'challenge'
await prismaClient.order.update({
where: { id: statusResponse.order_id },
data: {
status: "challenge",
payment: {
update: {
amount: statusResponse.gross_amount,
method: notificationJson.payment_type,
status: "challenge",
signatureKey: statusResponse.signature_key,
store: statusResponse.store,
currency: statusResponse.currency,
if (statusResponse.transaction_status == "capture") {
// capture only applies to card transaction, which you need to check for the fraudStatus
if (fraudStatus == "challenge") {
// TODO set transaction status on your databaase to 'challenge'
await prismaClient.order.update({
where: { id: statusResponse.order_id },
data: {
status: "challenge",
payment: {
update: {
amount: statusResponse.gross_amount,
method: notificationJson.payment_type,
status: "challenge",
signatureKey: statusResponse.signature_key,
store: statusResponse.store,
currency: statusResponse.currency,
},
},
},
},
});
} else if (fraudStatus == "accept") {
});
} else if (fraudStatus == "accept") {
// TODO set transaction status on your databaase to 'success'
await prismaClient.order.update({
where: { id: statusResponse.order_id },
data: {
status: "success",
payment: {
update: {
amount: statusResponse.gross_amount,
method: notificationJson.payment_type,
status: "paid",
signatureKey: statusResponse.signature_key,
store: statusResponse.store,
currency: statusResponse.currency,
},
},
},
});
}
} else if (statusResponse.transaction_status == "settlement") {
// TODO set transaction status on your databaase to 'success'
await prismaClient.order.update({
where: { id: statusResponse.order_id },
Expand All @@ -70,36 +91,20 @@ const webhook = async (notificationJson) => {
},
},
});
} else if (statusResponse.transaction_status == "deny") {
// TODO you can ignore 'deny', because most of the time it allows payment retries
// and later can become success
} else if (statusResponse.transaction_status == "cancel" || statusResponse.transaction_status == "expire") {
// TODO set transaction status on your databaase to 'failure'
await prismaClient.order.update({ where: { id: statusResponse.order_id }, data: { status: "failure" } });
} else if (statusResponse.transaction_status == "pending") {
// TODO set transaction status on your databaase to 'pending' / waiting payment
await prismaClient.order.update({ where: { id: statusResponse.order_id }, data: { status: "pending" } });
}
} else if (statusResponse.transaction_status == "settlement") {
// TODO set transaction status on your databaase to 'success'
await prismaClient.order.update({
where: { id: statusResponse.order_id },
data: {
status: "success",
payment: {
update: {
amount: statusResponse.gross_amount,
method: notificationJson.payment_type,
status: "paid",
signatureKey: statusResponse.signature_key,
store: statusResponse.store,
currency: statusResponse.currency,
},
},
},
});
} else if (statusResponse.transaction_status == "deny") {
// TODO you can ignore 'deny', because most of the time it allows payment retries
// and later can become success
} else if (statusResponse.transaction_status == "cancel" || statusResponse.transaction_status == "expire") {
// TODO set transaction status on your databaase to 'failure'
await prismaClient.order.update({ where: { id: statusResponse.order_id }, data: { status: "failure" } });
} else if (statusResponse.transaction_status == "pending") {
// TODO set transaction status on your databaase to 'pending' / waiting payment
await prismaClient.order.update({ where: { id: statusResponse.order_id }, data: { status: "pending" } });
}
});
})
.catch((error) => {
console.log(error);
});
};

export default { webhook };

0 comments on commit 24b0458

Please sign in to comment.