/** * Payment gateway postback: make sure everything checks out and complete transaction * * @return void */ public function postbackTask() { $test = false; // TESTING *********************** if ($test) { $postBackTransactionId = 331; } $params = Component::params(Request::getVar('option')); if (empty($_POST) && !$test) { throw new Exception(Lang::txt('Page Not Found'), 404); } // Initialize logger $logger = new CartMessenger('Payment Postback'); // Get payment provider if (!$test) { $paymentGatewayProivder = $params->get('paymentProvider'); include_once JPATH_COMPONENT . DS . 'lib' . DS . 'payment' . DS . 'PaymentDispatcher.php'; $paymentDispatcher = new PaymentDispatcher($paymentGatewayProivder); $pay = $paymentDispatcher->getPaymentProvider(); // Extract the transaction id from postback information $postBackTransactionId = $pay->setPostBack($_POST); if (!$postBackTransactionId) { // Transaction id couldn't be extracted $error = 'Post back did not have the valid transaction ID '; $logger->setMessage($error); $logger->setPostback($_POST); $logger->log(LoggingLevel::ERROR); return false; } } else { include_once JPATH_COMPONENT . DS . 'lib' . DS . 'payment' . DS . 'PaymentDispatcher.php'; $paymentDispatcher = new PaymentDispatcher('DUMMY AUTO PAYMENT'); $pay = $paymentDispatcher->getPaymentProvider(); } // Get transaction info $tInfo = CartModelCart::getTransactionFacts($postBackTransactionId); //print_r($tInfo); die; // Check if it exists if (!$tInfo) { // Transaction doesn't exist, log error $error = 'Incoming payment for the transaction that does not exist: ' . $postBackTransactionId; $logger->setMessage($error); $logger->setPostback($_POST); $logger->log(LoggingLevel::ERROR); return false; } // Check if the transaction can be processed (it can only be processed if the transaction is awaiting payment) if ($tInfo->info->tStatus != 'awaiting payment') { // Transaction cannot be processed, log error $error = 'Transaction cannot be processed: ' . $postBackTransactionId . '. Current transaction status is "' . $tInfo->info->tStatus . '"'; $logger->setMessage($error); $logger->setPostback($_POST); $logger->log(LoggingLevel::ERROR); return false; } // Get the action. Post back will normally be triggered on payment success, but can also be the cancel post back $postBackAction = $pay->getPostBackAction(); if ($postBackAction == 'payment' || $test) { // verify payment if (!$test && !$pay->verifyPayment($tInfo)) { // Payment has not been verified, get verification error $error = $pay->getError()->msg; $error .= ' Transaction ID: ' . $postBackTransactionId; // Log error $logger->setMessage($error); $logger->setPostback($_POST); $logger->log(LoggingLevel::ERROR); // Handle error CartModelCart::handleTransactionError($postBackTransactionId, $error); return false; } // No error $message = 'Transaction completed. '; $message .= 'Transaction ID: ' . $postBackTransactionId; // Log info if (!$test) { $logger->setMessage($message); $logger->setPostback($_POST); $logger->log(LoggingLevel::INFO); } // Finalize order -- whatever needs to be done $this->completeOrder($tInfo); } elseif ($postBackAction == 'cancel') { // Cancel transaction $message = 'Transaction cancelled. '; $message .= 'Transaction ID: ' . $postBackTransactionId; // Log info if (!$test) { $logger->setMessage($message); $logger->setPostback($_POST); $logger->log(LoggingLevel::INFO); } // Release the transaction CartModelCart::releaseTransaction($postBackTransactionId); } else { // No supported action, log error $error = 'Post back action is invalid: ' . $postBackAction; $logger->setMessage($error); $logger->setPostback($_POST); $logger->log(LoggingLevel::ERROR); return false; } }
/** * Confirm step of the checkout. Should be a pass-through page for JS-enabled browsers, requires a form submission to the payment gateway * * @return void */ public function confirmTask() { require_once PATH_CORE . DS . 'components' . DS . 'com_cart' . DS . 'models' . DS . 'CurrentCart.php'; $cart = new CartModelCurrentCart(); $transaction = $cart->liftTransaction(); if (!$transaction) { $cart->redirect('home'); } // Get security token $transaction->token = $cart->getToken(); // Check if there are any steps missing. Redirect if needed $nextStep = $cart->getNextCheckoutStep(); if ($nextStep != 'summary') { $cart->redirect($nextStep); } // Final step here before payment CartModelCart::updateTransactionStatus('awaiting payment', $transaction->info->tId); // Generate payment code $params = Component::params(Request::getVar('option')); $paymentGatewayProivder = $params->get('paymentProvider'); include_once JPATH_COMPONENT . DS . 'lib' . DS . 'payment' . DS . 'PaymentDispatcher.php'; $paymentDispatcher = new PaymentDispatcher($paymentGatewayProivder); $pay = $paymentDispatcher->getPaymentProvider(); $pay->setTransactionDetails($transaction); $error = false; try { $paymentCode = $pay->getPaymentCode(); $this->view->paymentCode = $paymentCode; } catch (Exception $e) { $error = $e->getMessage(); } if (!empty($error)) { $this->view->setError($error); } $this->view->display(); }