From 12f9d4fa183a96c5254f911a8c176f405e0bb092 Mon Sep 17 00:00:00 2001 From: webadmin Date: Sat, 21 Jun 2025 16:30:35 +0200 Subject: [PATCH] Update src/mollie/callback.php --- src/mollie/callback.php | 171 +++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 88 deletions(-) diff --git a/src/mollie/callback.php b/src/mollie/callback.php index 5ff9824..a201aa4 100644 --- a/src/mollie/callback.php +++ b/src/mollie/callback.php @@ -1,97 +1,92 @@ load_function('gateway'); $whmcs->load_function('invoice'); -/** - * - * Check parameters - * - */ -if (isset($_POST['id'])) { - - // Get transaction - $transactionQuery = select_query('gateway_mollie', '', array('paymentid' => $_POST['id']), null, null, 1); - - if (mysql_num_rows($transactionQuery) != 1) { - logTransaction('mollieunknown', $_POST, 'Callback - Failure 2 (Transaction not found)'); - - header('HTTP/1.1 500 Transaction not found'); - exit(); - } - - $transaction = mysql_fetch_assoc($transactionQuery); - - $method = $transaction['method']; - - if (empty($method)) { - $method = 'checkout'; - } - - $_GATEWAY = getGatewayVariables('mollie' . $method . '_devapp'); - - if ($transaction['status'] != 'open') { - logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Failure 3 (Transaction not open)'); - - header('HTTP/1.1 500 Transaction not open'); - exit(); - } - - // Get user and transaction currencies - $userCurrency = getCurrency($transaction['userid']); - $transactionCurrency = select_query('tblcurrencies', '', array('id' => $transaction['currencyid'])); - $transactionCurrency = mysql_fetch_assoc($transactionCurrency); - - // Check payment - $mollie = new \Mollie\Api\MollieApiClient(); - $mollie->setApiKey($_GATEWAY['key']); - - $payment = $mollie->payments->get($_POST['id']); - - if ($payment->isPaid()) { - - // Add conversion, when there is need to. WHMCS only supports currencies per user. WHY?! - if ($transactionCurrency['id'] != $userCurrency['id']) { - $transaction['amount'] = convertCurrency($transaction['amount'], $transaction['currencyid'], $userCurrency['id']); - } - - // Check invoice - $invoiceid = checkCbInvoiceID($transaction['invoiceid'], $_GATEWAY['paymentmethod']); - - checkCbTransID($transaction['paymentid']); - - // Add invoice - addInvoicePayment($invoiceid, $transaction['paymentid'], $transaction['amount'], '', $_GATEWAY['paymentmethod']); - - update_query('gateway_mollie', array('status' => 'paid', 'updated' => date('Y-m-d H:i:s', time())), array('id' => $transaction['id'])); - - logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Successful (Paid)'); - - header('HTTP/1.1 200 OK'); - exit(); - } else if ($payment->isOpen() == FALSE) { - update_query('gateway_mollie', array('status' => 'closed', 'updated' => date('Y-m-d H:i:s', time())), array('id' => $transaction['id'])); - - logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Successful (Closed)'); - - header('HTTP/1.1 200 OK'); - exit(); - } else { - logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Failure 1 (Payment not open or paid)'); - - header('HTTP/1.1 500 Payment not open or paid'); - exit(); - } -} else { +if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty($_POST['id'])) { logTransaction('mollieunknown', $_POST, 'Callback - Failure 0 (Arg mismatch)'); - - header('HTTP/1.1 500 Arg mismatch'); - exit(); + http_response_code(500); + exit('Arg mismatch'); } + +// Find transaction by Mollie payment ID +$transaction = Capsule::table('gateway_mollie') + ->where('paymentid', $_POST['id']) + ->first(); + +if (!$transaction) { + logTransaction('mollieunknown', $_POST, 'Callback - Failure 2 (Transaction not found)'); + http_response_code(500); + exit('Transaction not found'); +} + +$transaction = (array) $transaction; +$method = $transaction['method'] ?: 'checkout'; + +// Load gateway configuration +$_GATEWAY = getGatewayVariables('mollie' . $method . '_devapp'); + +if ($transaction['status'] !== 'open') { + logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Failure 3 (Transaction not open)'); + http_response_code(500); + exit('Transaction not open'); +} + +// Load currencies +$userCurrency = getCurrency($transaction['userid']); +$transactionCurrency = Capsule::table('tblcurrencies') + ->where('id', $transaction['currencyid']) + ->first(); + +$transactionCurrency = (array) $transactionCurrency; + +// Init Mollie +$mollie = new MollieApiClient(); +$mollie->setApiKey($_GATEWAY['key']); + +try { + $payment = $mollie->payments->get($_POST['id']); +} catch (Exception $e) { + logTransaction($_GATEWAY['paymentmethod'], $_POST, 'Callback - Failure 4 (API Error): ' . $e->getMessage()); + http_response_code(500); + exit('Mollie API error'); +} + +// Handle payment status +if ($payment->isPaid()) { + // Currency conversion if needed + if ($transactionCurrency['id'] != $userCurrency['id']) { + $transaction['amount'] = convertCurrency($transaction['amount'], $transaction['currencyid'], $userCurrency['id']); + } + + $invoiceid = checkCbInvoiceID($transaction['invoiceid'], $_GATEWAY['paymentmethod']); + checkCbTransID($transaction['paymentid']); + addInvoicePayment($invoiceid, $transaction['paymentid'], $transaction['amount'], '', $_GATEWAY['paymentmethod']); + + Capsule::table('gateway_mollie') + ->where('id', $transaction['id']) + ->update(['status' => 'paid', 'updated' => date('Y-m-d H:i:s')]); + + logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Successful (Paid)'); + http_response_code(200); + exit('OK'); + +} elseif (!$payment->isOpen()) { + Capsule::table('gateway_mollie') + ->where('id', $transaction['id']) + ->update(['status' => 'closed', 'updated' => date('Y-m-d H:i:s')]); + + logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Successful (Closed)'); + http_response_code(200); + exit('Closed'); + +} else { + logTransaction($_GATEWAY['paymentmethod'], array_merge($transaction, $_POST), 'Callback - Failure 1 (Payment not open or paid)'); + http_response_code(500); + exit('Payment not open or paid'); +} \ No newline at end of file