Magento – Magento 2 : Successful Order with Failed Payment

magento2orderspayment

I am using an Indian payment gateway (PayTM), and payment gateway is working fine (in terms of successful payment and failed payment URLs). However, The payment succeed or failed, Magento still :

  • creates an order with status processing,
  • Send a confirmation email to the customer

How can I solve this issue? Basically I don't want order to be generated (or rather processed) if payment is failed. Or anything else other than sending confirmation email to customer.

Any help would be appreciated.

payTM RESPONSE code:

class Response extends \One97\Paytm\Controller\Paytm
{

    public function execute()
    {
        $comment = "";
        $request = $_POST;
        if(!empty($_POST)){
            foreach($_POST as $key => $val){
                if($key != "CHECKSUMHASH"){
                    $comment .= $key  . "=" . $val . ", \n <br />";
                }
            }
        }
        $errorMsg = '';
        $successFlag = false;
        $resMessage = $_POST['RESPMSG'];
        $orderId = $this->getRequest()->getParam('ORDERID');
        $order = $this->getOrderById($orderId);
        $orderTotal = round($order->getGrandTotal(), 2);
        $orderStatus = $this->getRequest()->getParam('STATUS');
        $resCode = $this->getRequest()->getParam('RESPCODE');
        $orderTxnAmount = $this->getRequest()->getParam('TXNAMOUNT');
        //print_r($request);
        if($this->getPaytmModel()->validateResponse($request, $orderId))
        {
            if($orderStatus == "TXN_SUCCESS" && $orderTotal == $orderTxnAmount){                
                // Create an array having all required parameters for status query.             
                $requestParamList = array("MID" => $_POST['MID'] , "ORDERID" => $orderId);

                // Call the PG's getTxnStatus() function for verifying the transaction status.
                $check_status_url = $this->getPaytmModel()->getStatusQueryUrl();                
                $responseParamList = $this->getPaytmHelper()->callAPI($check_status_url, $requestParamList);
                if($responseParamList['STATUS']=='TXN_SUCCESS' && $responseParamList['TXNAMOUNT']==$_POST['TXNAMOUNT'])
                {
                    $successFlag = true;
                    $comment .=  "Success ";
                    $order->setStatus($order::STATE_PROCESSING);
                    $order->setExtOrderId($orderId);
                    $returnUrl = $this->getPaytmHelper()->getUrl('checkout/onepage/success');
                }
                else{
                    $errorMsg = 'Paytm Transaction Failed ! Fraud has been detected';
                    $comment .=  "Fraud Detucted";
                    $order->setStatus($order::STATUS_FRAUD);
                    $returnUrl = $this->getPaytmHelper()->getUrl('checkout/onepage/failure');
                }
            }else{
                if($resCode == "141" || $resCode == "8102" || $resCode == "8103" || $resCode == "14112"){
                    $errorMsg = 'Paytm Transaction Failed ! Transaction was cancelled.';
                    $comment .=  "Payment cancelled by user";
                    $order->setStatus($order::STATE_CANCELED);
                    $this->_cancelPayment("Payment cancelled by user");
                    //$order->save();
                    $returnUrl = $this->getPaytmHelper()->getUrl('checkout/cart');
                }else{
                    $errorMsg = 'Paytm Transaction Failed ! '.$resMessage;
                    $comment .=  "Failed";

                    $order->setStatus($order::STATE_PAYMENT_REVIEW);
                    $returnUrl = $this->getPaytmHelper()->getUrl('checkout/onepage/failure');
                }
            }            
        }
        else
        {
            $errorMsg = 'Paytm Transaction Failed ! Fraud has been detected';
            $comment .=  "Fraud Detucted";
            $order->setStatus($order::STATUS_FRAUD);
            $returnUrl = $this->getPaytmHelper()->getUrl('checkout/onepage/failure');
        }
        $this->addOrderHistory($order,$comment);
        $order->save();
        if($successFlag){
            $this->messageManager->addSuccess( __('Paytm transaction has been successful.') );
        }else{
            $this->messageManager->addError( __($errorMsg) );
        }
        $this->getResponse()->setRedirect($returnUrl);
    }

}

Best Answer

Sanjay,

Issue may be at logic of Paytm module.

There are must be 3 frontend urls in this module

Redirection url means the page where customer is landed after order place and before goes to Paytm site

Success url: where paytm is redirect to the customer after successfully payment done at paytm

And another failure url: where paytm is redirect to the customer after failure in payment at paytm

So, may at Redirection action. Forcefully order status has been changed to Processing.

So, you need stop to run this code and also. Change the status success action