0

I'm making a payment method where the customer is redirected to an external cash page and then back to the site once the payment is completed. With a logged in user everything works correctly. When I try to checkout as a guest I find the following screen: enter image description here

The function that creates the url for redirection is as follows:

public function getUrlXPay() {

    /* $_quote = $this->checkoutSession->getQuote();
      $_quote->collectTotals();
      $_quote->reserveOrderId(); */ //->save();

    $orderIncrementId = $this->checkoutSession->getLastRealOrderId();
    $order = $this->_orderFactory->create()->loadByIncrementId($orderIncrementId);

    /*
     * Se lingua pagina di cassa AUTOMATICA rilevo locale sito altrimenti metto quella scelta dal Merchant in configurazione
     */
    if ($this->getConfigData('language') == "AUTO") {
        /*
         * Converto local in codice pagina di cassa X-Pay, di default lascio INGLESE
         */
        $locale = $this->getLocaleCode();
        $aLang = array('it_IT' => 'ITA', 'it_CH' => 'ITA',
            'en_AU' => 'ENG', 'en_CA' => 'ENG', 'en_IE' => 'ENG', 'en_NZ' => 'ENG', 'en_GB' => 'ENG', 'en_US' => 'ENG',
            'es_AR' => 'SPA', 'es_CL' => 'SPA', 'es_CO' => 'SPA', 'es_CR' => 'SPA', 'es_MX' => 'SPA', 'es_PA' => 'SPA', 'es_PE' => 'SPA', 'es_ES' => 'SPA', 'es_VE' => 'SPA',
            'fr_CA' => 'FRA', 'fr_FR' => 'FRA',
            'de_AT' => 'GER', 'de_DE' => 'GER', 'de_CH' => 'GER',
            'ja_JP' => 'JPN');
        if (isset($aLang[$locale])) {
            $languageId = $aLang[$locale];
        } else {
            $languageId = 'ENG';
        }
    } else {
        $languageId = $this->getConfigData('language');
    }

    /*
     * Prendo Moneta corrente di magento e imposto EUR per CartaSì (valore fisso)
     */
    //$currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
    $currencyCode = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
    $divisa = 'EUR';

    $quoteId = $this->checkoutSession->getQuoteId();

    /*
     * Setto Importo, Url, Alias e String per Calcolo MAC in base al settaggio del modulo tra produzione e test
     */
    $importo = $order->getGrandTotal();
    if ($currencyCode != $divisa) {
        throw new Magento\Framework\Exception\LocalizedException('Importo non in euro.');
        //$importo = Mage::helper('directory')->currencyConvert($importo, $currencyCode, $divisa);
    }

    $importo = strtr(round($importo * 100), array('.' => '', ',' => ''));
    if ($this->getConfigData('testmode') == 1) {
        /*
         * Dati di configurazione per effettuare i test
         */
        $alias = $this->getConfigData('alias_test');
        $stringaSegreta = $this->getConfigData('mac_test');
        $url = 'https://int-ecommerce.cartasi.it/ecomm/ecomm/DispatcherServlet';
    } else {
        /*
         * Se moneta diversa, converto in EUR prima di continuare
         */
        $alias = $this->getConfigData('alias');
        $stringaSegreta = $this->getConfigData('mac');
        $url = 'https://ecommerce.cartasi.it/ecomm/ecomm/DispatcherServlet';
    }

    /*
     * Prendo l'id della Sessione per il ritorno sul sito e lo uso anche per calcolare il Codice univoco della Transazione
     */
    /* $session_id = $_quote->getReservedOrderId();
      $codTrans = substr($quoteId . '-' . time(), 0, 30); */
    $session_id = $orderIncrementId;
    $codTrans = substr($orderIncrementId . '-' . time(), 0, 30);


    $sUrl = $url . '?';
    $sUrl .= 'alias=' . trim($alias);
    $sUrl .= '&codTrans=' . trim($codTrans);
    $sUrl .= '&importo=' . trim($importo);
    $sUrl .= '&divisa=' . trim($divisa);
    $sUrl .= '&languageId=' . trim($languageId);
    $sUrl .= '&session_id=' . trim($session_id);
    $sUrl .= '&urlpost=' . trim(urlencode($this->urlBuilder->getUrl('cartasixpay/payment/notificas2s', array('_secure' => true))));
    $sUrl .= '&url=' . trim(urlencode($this->urlBuilder->getUrl('cartasixpay/payment/conferma', array('_secure' => true)))); //'checkout/onepage/success'
    $sUrl .= '&url_back=' . trim(urlencode($this->urlBuilder->getUrl('cartasixpay/payment/annullo', array('_secure' => true))));
    $sUrl .= '&mail=' . trim($order->getCustomerEmail());
    $sUrl .= '&mac=' . sha1('codTrans=' . trim($codTrans) . 'divisa=' . trim($divisa) . 'importo=' . trim($importo) . $stringaSegreta);
    $sUrl .= '&descrizione=Ordine: ' . $orderIncrementId;
    $sUrl .= '&Note1=magento';
    $sUrl .= '&Note2=2.x';

    return $sUrl;
}

I also tried to comment on the line in which I get the email to be inserted in the url but the problem persists. Can someone help me? I'm using Magento 2.1.3 for test.

0

You can not get email of guest

$order->getCustomerEmail()

May be you have to use

$order->getBillingAddress()->getEmail()

Saying based on this line of magento

->getBillingAddress()->setEmail($email)
  • Thanks for the answer, I tried to follow your instructions but the problem persists. I do not even know if it's the fault of my form, because the function that returns the email, even if it does not go back I will not throw exceptions. The problem may be that the server is not enabled to send mail? In system.log i find main.ERROR: Unable to send mail. – Filippo Dell'Oca Aug 11 '17 at 9:52
  • Than Please try simple mail sending script of php. – padhiyar gaurang Aug 11 '17 at 9:56
  • Script to send email using simple php : $to = 'xx@x.com'; $subject = 'the subject from mail'; $message = 'hello'; $headers = 'From: abc@abc.com' . "\r\n"; $mail = mail($to, $subject, $message, $headers); $msg = "First line of text\nSecond line of text"; // use wordwrap() if lines are longer than 70 characters $msg = wordwrap($msg,70); // send email mail($to,"My subject",$msg); if($mail = 1){ echo "send"; } else{ echo "Something Wrong"; } – padhiyar gaurang Aug 11 '17 at 9:58
  • I confirm that sending mail from php is disabled on the server for security issues. I read that you can not use a smtp with native magento2, I have to install a plugin. However, I do not understand how it can affect the plugin, even because with the registered user it works. – Filippo Dell'Oca Aug 11 '17 at 10:01
  • So you want to say that , mails are working for orders, register customers (e.g like forgot password), any custom module emails etc... Are therse all emails working? – padhiyar gaurang Aug 11 '17 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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