Form.php 4.45 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Payment\Block\Transparent;

use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Model\Method\Adapter;
use Magento\Payment\Model\Method\TransparentInterface;
use Magento\Checkout\Model\Session;
use Magento\Payment\Model\Config;
use Magento\Framework\View\Element\Template\Context;

/**
 * Transparent form block
 *
 * @api
 * @since 100.0.2
 */
class Form extends \Magento\Payment\Block\Form\Cc
{
    /**
     * @var Session
     */
    private $checkoutSession;

    /**
     * @var string
     */
    protected $_template = 'Magento_Payment::transparent/form.phtml';

    /**
     * @param Context $context
     * @param Config $paymentConfig
     * @param Session $checkoutSession
     * @param array $data
     */
    public function __construct(
        Context $context,
        Config $paymentConfig,
        Session $checkoutSession,
        array $data = []
    ) {
        parent::__construct($context, $paymentConfig, $data);
        $this->checkoutSession = $checkoutSession;
    }

    /**
     * {inheritdoc}
     */
    protected function _toHtml()
    {
        if ($this->shouldRender()) {
            return $this->processHtml();
        }

        return '';
    }

    /**
     * Checks whether block should be rendered
     * basing on TransparentInterface presence in checkout session
     *
     * @return bool
     */
    protected function shouldRender()
    {
        $quote = $this->checkoutSession->getQuote();
        if ($quote) {
            $payment = $quote->getPayment();
            return $payment && $payment->getMethodInstance() instanceof TransparentInterface;
        }

        return false;
    }

    /**
     * Initializes method
     *
     * @return void
     */
    protected function initializeMethod()
    {
        $this->setData(
            'method',
            $this->checkoutSession
                ->getQuote()
                ->getPayment()
                ->getMethodInstance()
        );
    }

    /**
     * Parent rendering wrapper
     *
     * @return string
     */
    protected function processHtml()
    {
        $this->initializeMethod();
        return parent::_toHtml();
    }

    /**
     * Get type of request
     *
     * @return bool
     */
    public function isAjaxRequest()
    {
        return $this->getRequest()->getParam('isAjax');
    }

    /**
     * Get delimiter for date
     *
     * @return string
     */
    public function getDateDelim()
    {
        return $this->getMethodConfigData('date_delim');
    }

    /**
     * Get map of cc_code, cc_num, cc_expdate for gateway
     * Returns json formatted string
     *
     * @return string
     */
    public function getCardFieldsMap()
    {
        $keys = ['cccvv', 'ccexpdate', 'ccnum'];
        $ccfields = array_combine($keys, explode(',', $this->getMethodConfigData('ccfields')));
        return json_encode($ccfields);
    }

    /**
     * Retrieve place order url on front
     *
     * @return string
     */
    public function getOrderUrl()
    {
        return $this->_urlBuilder->getUrl(
            $this->getMethodConfigData('place_order_url'),
            [
                '_secure' => $this->getRequest()->isSecure()
            ]
        );
    }

    /**
     * Retrieve gateway url
     *
     * @return string
     */
    public function getCgiUrl()
    {
        return (bool)$this->getMethodConfigData('sandbox_flag')
            ? $this->getMethodConfigData('cgi_url_test_mode')
            : $this->getMethodConfigData('cgi_url');
    }

    /**
     * Retrieve config data value by field name
     *
     * @param string $fieldName
     * @return mixed
     */
    public function getMethodConfigData($fieldName)
    {
        $method = $this->getMethod();
        if ($method instanceof TransparentInterface) {
            return $method->getConfigInterface()->getValue($fieldName);
        }
        return $method->getConfigData($fieldName);
    }

    /**
     * Returns transparent method service
     *
     * @return TransparentInterface
     * @throws LocalizedException
     */
    public function getMethod()
    {
        $method = parent::getMethod();

        if (!$method instanceof TransparentInterface && !$method instanceof Adapter) {
            throw new LocalizedException(
                __('We cannot retrieve the transparent payment method model object.')
            );
        }
        return $method;
    }
}