Improve logic in processWebhookRequest to handle more cases

This commit is contained in:
cnohall 2024-09-15 19:58:18 +09:00
parent 749b58500e
commit 3cfffa09c4

View File

@ -111,37 +111,46 @@ class BlockonomicsPaymentDriver extends BaseDriver
public function processWebhookRequest()
{
// TODO: Include logic to create new payment with pending status in case user sends payment to the address after navigating away from the payment page
$url_callback_secret = $_GET['secret'];
$db_callback_secret = $this->getCallbackSecret();
if ($url_callback_secret != $db_callback_secret) {
throw new PaymentFailed('Secret does not match');
return;
}
$txid = $_GET['txid'];
$value = $_GET['value'];
$status = $_GET['status'];
$addr = $_GET['addr'];
$payment = $this->getPaymentByTxid($txid);
$callbackSecret = $this->getCallbackSecret();
//Match secret for security
if ($_GET['secret'] != $callbackSecret) {
throw new PaymentFailed('Secret does not match');
return;
}
// Only accept confirmed transactions
if ($status != 2) {
throw new PaymentFailed('Transaction not confirmed');
if (!$payment) {
// TODO: Implement logic to create new payment in case user sends payment to the address after closing the payment page
}
switch ($status) {
case 0:
$statusId = Payment::STATUS_PENDING;
break;
case 1:
$statusId = Payment::STATUS_PENDING;
break;
case 2:
$statusId = Payment::STATUS_COMPLETED;
break;
}
// Save the updated payment status
if ($payment->status_id != $statusId) {
if($payment->status_id == $statusId) {
header('HTTP/1.1 200 OK');
echo "No change in payment status";
} else {
$payment->status_id = $statusId;
$payment->save();
}
header('HTTP/1.1 200 OK');
echo "Success";
return;
echo "Payment status updated successfully";
}
}