PrestaShop / PrestaShop

PrestaShop is the universal open-source software platform to build your e-commerce solution.
https://www.prestashop-project.org/
Other
8.11k stars 4.79k forks source link

Prestashop payment module controller loging out automatically sometimes #21130

Closed fkomaralp closed 4 years ago

fkomaralp commented 4 years ago

Hi, I'm writing a prestashop offline payment module for version 1.7 > . Module is using 4 front controllers.

First payment.php is making a creadit card form for the customer. validation.php Is making requests for a bank. Working with payment.php controller file. There is two controller is triggering by the bank. OnError.php and OnSuccess.php. My problem is on the OnSuccess.php

OnSuccess.php file is like;

<?php

use PrestaTurk\Module\KuveytturkPaymentModule\Entity\KpmInstallmentRate;
use PrestaTurk\Module\KuveytturkPaymentModule\Entity\KpmOrders;
use PrestaTurk\Module\KuveytturkPaymentModule\Model\AdditionalData;
use PrestaTurk\Module\KuveytturkPaymentModule\Model\KuveytturkThreeDModelProvisionGateRequest;
use PrestaTurk\Module\KuveytturkPaymentModule\Model\KuveytturkThreeDModelPayGateResponse;
use PrestaTurk\Module\KuveytturkPaymentModule\Model\KuveytTurkVPosAdditionalData;
use PrestaTurk\Module\KuveytturkPaymentModule\Serializer\KpmNameConverter;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use PrestaTurk\Module\KuveytturkPaymentModule\Model\KuveytturkThreeDModelProvisionGateResponse;

class kuveytturkpaymentmoduleOnSuccessModuleFrontController extends ModuleFrontController
{
    public function postProcess()
    {
        // Ödeme
        $live_mode = (bool)Configuration::get("KUVEYTTURKPAYMENTMODULE_LIVE_MODE");
        $mercant_id = Configuration::get("KUVEYTTURKPAYMENTMODULE_MERCANT_ID");
        $username = Configuration::get("KUVEYTTURKPAYMENTMODULE_USERNAME");
        $password = Configuration::get("KUVEYTTURKPAYMENTMODULE_PASSWORD");
        $customer_id = Configuration::get("KUVEYTTURKPAYMENTMODULE_CUSTOMER_ID");

        $bank_url = "https://boatest.kuveytturk.com.tr/boa.virtualpos.services/Home/ThreeDModelProvisionGate";

        if(!$live_mode){
            $bank_url = "https://boatest.kuveytturk.com.tr/boa.virtualpos.services/Home/ThreeDModelProvisionGate";
        } else {
            $bank_url = "https://boa.kuveytturk.com.tr/sanalposservice/Home/ThreeDModelProvisionGate";
        }

        $password_hashed = base64_encode(sha1($password,"ISO-8859-9"));

        $xml = urldecode(Tools::getValue("AuthenticationResponse"));

        $xml_encoder = new XmlEncoder();

        $object_normalizer = new ObjectNormalizer(null, new KpmNameConverter());

        $encoders = [$xml_encoder];

        $normalizers = [$object_normalizer];

        $serializer = new Serializer($normalizers, $encoders);
        /** @var KuveytturkThreeDModelPayGateResponse $kuveytturk_on_success */
        $kuveytturk_on_success = $serializer->deserialize($xml, KuveytturkThreeDModelPayGateResponse::class,'xml');

        $ad = new AdditionalData();
        $ad->setData($kuveytturk_on_success->getMD());
        $ad->setKey("MD");

        $kvpad = new KuveytTurkVPosAdditionalData();
        $kvpad->setAdditionalData($ad);

        $vpm = $kuveytturk_on_success->getVPosMessage();

        $vpm["OrderId"] = 123456789;

        $HashData = base64_encode(sha1($mercant_id.$vpm["OrderId"].$vpm["Amount"].$username.$password_hashed , "ISO-8859-9"));

        $ktdmpgr = new KuveytturkThreeDModelProvisionGateRequest();

        $ktdmpgr->setAPIVersion("1.0.0");
        $ktdmpgr->setHashData($HashData);
        $ktdmpgr->setMerchantId($mercant_id);
        $ktdmpgr->setCustomerId($customer_id);
        $ktdmpgr->setUserName($username);
        $ktdmpgr->setTransactionType("Sale");
        $ktdmpgr->setInstallmentCount($vpm["InstallmentCount"]);
        $ktdmpgr->setAmount($vpm["Amount"]);
        $ktdmpgr->setMerchantOrderId($vpm["OrderId"]);
        $ktdmpgr->setTransactionSecurity($vpm["TransactionSecurity"]);
        $ktdmpgr->setKuveytTurkVPosAdditionalData($kvpad);

        $xml_encoder = new XmlEncoder();
        $xml_encoder->setRootNodeName($ktdmpgr->getRootName());

        $object_normalizer = new ObjectNormalizer(null, new KpmNameConverter());

        $object_normalizer->setIgnoredAttributes(["RootName"]);

        $encoders = [$xml_encoder];

        $normalizers = [$object_normalizer];

        $serializer = new Serializer($normalizers, $encoders);

        $ktdmpgr_xml = $serializer->serialize($ktdmpgr,'xml');

        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml', 'Content-length: '. strlen($ktdmpgr_xml)) );
            curl_setopt($ch, CURLOPT_POST, true); //POST Metodu kullanarak verileri gönder
            curl_setopt($ch, CURLOPT_HEADER, false); //Serverdan gelen Header bilgilerini önemseme.
            curl_setopt($ch, CURLOPT_URL, $bank_url); //Baglanacagi URL
            curl_setopt($ch, CURLOPT_POSTFIELDS, $ktdmpgr_xml);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Transfer sonuçlarini al.
            $data = curl_exec($ch);
            curl_close($ch);
        } catch (\Exception $e){
            // TODO: Exception
        }

        $xml_encoder = new XmlEncoder();

        $object_normalizer = new ObjectNormalizer(null, new KpmNameConverter());

        $object_normalizer->setIgnoredAttributes(["RootName"]);

        $encoders = [$xml_encoder];

        $normalizers = [$object_normalizer];

        $serializer = new Serializer($normalizers, $encoders);

        /** @var KuveytturkThreeDModelProvisionGateResponse $provision_gate_response */
        $provision_gate_response = $serializer->deserialize($data,KuveytturkThreeDModelProvisionGateResponse::class, 'xml');
        // Ödeme
        if($provision_gate_response->getResponseCode() == "00") {
            $cart = $this->context->cart;
            $currency_id = (int) Context::getContext()->currency->id;
            $customer = new Customer((int)$cart->id_customer);
            $order_id = Order::getOrderByCartId((int) $cart->id);

            $entity_manager = $this->get("doctrine.orm.entity_manager");
            $result = $entity_manager->getRepository(KpmInstallmentRate::class)
            ->findByInstallment($vpm["InstallmentCount"]);

            $kpm_order = new KpmOrders();
            $kpm_order->setInstallment($vpm["InstallmentCount"]);
            if(count($result) > 0){
                /** @var KpmInstallmentRate $installment */
                $installment = $result[0];

                $kpm_order->setRate($installment->getRate());
            } else {
                $kpm_order->setRate(0);
            }

            $kpm_order->setRefferance($cart->id);

            $entity_manager->persist($kpm_order);
            $entity_manager->flush();

            $this->module->validateOrder($cart->id,  _PS_OS_PAYMENT_, $cart->getOrderTotal(), $this->module->displayName.' - Kuveyttürk', NULL, [], $currency_id, false, $customer->secure_key);
            Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.$order_id.'&key='.$customer->secure_key);
        } else {
            $home_url = _PS_BASE_URL_ . __PS_BASE_URI__;

            $payment_url = $this->context->link->getModuleLink($this->module->name, 'payment', [], true);

            $this->context->smarty->assign(array(
                'result' => $provision_gate_response->getResponseMessage(),
                'home_url' => $home_url,
                "payment_url" => $payment_url,
                'error_icon' => __PS_BASE_URI__ . "/modules/" . $this->module->name . "/views/img/error.png",
            ));

            $this->setTemplate('module:kuveytturkpaymentmodule/views/templates/front/on_success_on_error.tpl');

        }
    }

    public function setMedia()
    {
        parent::setMedia();
        $this->registerStylesheet(
            'kuveytturkpaymentmodule-error-css',
            'modules/'.$this->module->name.'/views/css/front/error.css',
            [
                'priority' => 200,
                'attribute' => 'async',
            ]
        );

        $this->registerStylesheet(
            'kuveytturkpaymentmodule-front-css',
            'modules/'.$this->module->name.'/views/css/front.css',
            [
                'priority' => 200,
                'attribute' => 'async',
            ]
        );
    }
}

If bank is returns with different result is like "14" (not 00), I want to display to customer an error message. Everything is normal. But sometimes OnSuccess controller is deleting customer's cart and loging out automaticallly. This is a bug or am I wrong on my code? Bank is making a POST request for this page for giving an payment result answer.

prestashop-issue-bot[bot] commented 4 years ago

Thanks for opening this issue! We will help you to keep its state consistent

SimonGrn commented 4 years ago

Hello,

We use GitHub issues only to discuss about bugs and new features in the PrestaShop project. If you have questions about using PrestaShop or third-party modules, or if you need help with your shop, please consider one of our support plans.

Alternatively, you can also ask for help in the community forums or in the public Slack channel.

Thank you