6

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);
    }

}

4 Answers 4

0

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

6
  • Here is the code snippet (added to the main question) where PayTM controls the redirects. I don't see any place where PayTM is setting the status to success. (I could be wrong though.. )
    – Sanjay
    Feb 21, 2017 at 11:07
  • Also, is there a way I can see the process flow (or redirects)?
    – Sanjay
    Feb 21, 2017 at 11:11
  • so you have any other files at Controller?
    – Amit Bera
    Feb 21, 2017 at 11:41
  • Actually, this order is in "PAYMENT REVIEW" state which makes sense. but now the issue becomes 1. how should I send a link to the customer to complete the payment 2. Instead of showing order successful on screen, I want to show payment failed. Do I need to code this? This looks pretty basic logic and requirement, so maybe Magento already provides it.
    – Sanjay
    Feb 22, 2017 at 13:46
  • @Sanjay have you resolved the issue??? Jun 29, 2017 at 13:18
0

By default Magento 2 provides processing as default status for rediretion payment gateway.

You need to put

$order = $this->getOrderById($orderId); //load your order
$order->addStatusToHistory(\Magento\Sales\Model\Order::STATE_NEW,__('Customer was redirected to PayTM'));
$order->save();

This will set your order in pending status once you redirected to gateway. Your success and failure action will change the status upon payment success ot failure.

Thanks and let me know if you have any query.

6
  • Need to put this where?
    – Sanjay
    Feb 22, 2017 at 6:10
  • In your redirect controller action. Feb 22, 2017 at 6:13
  • PayTM controller?
    – Sanjay
    Feb 22, 2017 at 6:29
  • Please share how many controller actions are there? or please share all controller code or extension code. Feb 22, 2017 at 9:52
  • Here is the link for PayTM extension. github.com/Paytm-Payments/Paytm_Magento_v2.x_Kit It has the entire code including controllers..
    – Sanjay
    Feb 22, 2017 at 10:58
0

you need to replace in Redirect.php file

$order->setState("pending_payment")->setStatus("pending_payment");
            $order->addStatusToHistory($order->getStatus(), "Customer was redirected to paytm.");
$order->save();

By:-

$order->setState("pending_payment")->setStatus("pending_payment");
            $order->addStatusToHistory($order->getStatus(), "Customer was redirected to paytm.");
            //$order->save();

it's working for me.

0

For stopping mail for failed order, you have to write observer.

name : checkout_onepage_controller_success_action

if($order->getStatus() == 'confirmed'){
                $this->orderSender->send($order, true);

            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.