hostedproRequestFactory = $hostedproRequestFactory; parent::__construct( $context, $registry, $extensionFactory, $customAttributeFactory, $paymentData, $scopeConfig, $logger, $moduleList, $localeDate, $proFactory, $storeManager, $urlBuilder, $requestHttp, $cartFactory, $resource, $resourceCollection, $data ); } /** * Return available CC types for gateway based on merchant country. * We do not have to check the availability of card types. * * @return true */ public function getAllowedCcTypes() { return true; } /** * Return merchant country code from config, * use default country if it not specified in General settings * * @return string */ public function getMerchantCountry() { return $this->_pro->getConfig()->getMerchantCountry(); } /** * Do not validate payment form using server methods * * @return true */ public function validate() { return true; } /** * Instantiate state and set it to state object * * @param string $paymentAction * @param \Magento\Framework\DataObject $stateObject * @return void */ public function initialize($paymentAction, $stateObject) { switch ($paymentAction) { case Config::PAYMENT_ACTION_AUTH: case Config::PAYMENT_ACTION_SALE: $payment = $this->getInfoInstance(); /** @var \Magento\Sales\Model\Order $order */ $order = $payment->getOrder(); $order->setCanSendNewEmailFlag(false); $payment->setAmountAuthorized($order->getTotalDue()); $payment->setBaseAmountAuthorized($order->getBaseTotalDue()); $this->setPaymentFormUrl($payment); $stateObject->setState(Order::STATE_PENDING_PAYMENT); $stateObject->setStatus(Order::STATE_PENDING_PAYMENT); $stateObject->setIsNotified(false); break; default: break; } } /** * Sends API request to PayPal to get form URL, then sets this URL to $payment object. * * @param \Magento\Payment\Model\InfoInterface $payment * @return void * @throws \Magento\Framework\Exception\LocalizedException */ protected function setPaymentFormUrl(InfoInterface $payment) { $request = $this->buildFormUrlRequest($payment); $response = $this->sendFormUrlRequest($request); if ($response) { $payment->setAdditionalInformation('secure_form_url', $response); } else { throw new LocalizedException(__('Cannot get secure form URL from PayPal')); } } /** * Returns request object with needed data for API request to PayPal to get form URL. * * @param \Magento\Payment\Model\InfoInterface $payment * @return \Magento\Paypal\Model\Hostedpro\Request */ protected function buildFormUrlRequest(InfoInterface $payment) { $order = $payment->getOrder(); $request = $this->buildBasicRequest()->setOrder($order)->setPaymentMethod($this)->setAmount($order); return $request; } /** * Returns form URL from request to PayPal. * * @param \Magento\Paypal\Model\Hostedpro\Request $request * @return string|false */ protected function sendFormUrlRequest(Request $request) { $api = $this->_pro->getApi(); $response = $api->call(self::BM_BUTTON_METHOD, $request->getRequestData()); if (!isset($response['EMAILLINK'])) { return false; } return $response['EMAILLINK']; } /** * Return request object with basic information * * @return \Magento\Paypal\Model\Hostedpro\Request */ protected function buildBasicRequest() { $request = $this->hostedproRequestFactory->create()->setData( [ 'METHOD' => self::BM_BUTTON_METHOD, 'BUTTONCODE' => self::BM_BUTTON_CODE, 'BUTTONTYPE' => self::BM_BUTTON_TYPE, ] ); return $request; } /** * Get return URL * * @param int|null $storeId * @return string */ public function getReturnUrl($storeId = null) { return $this->getUrl('paypal/hostedpro/return', $storeId); } /** * Get notify (IPN) URL * * @param int|null $storeId * @return string */ public function getNotifyUrl($storeId = null) { return $this->getUrl('paypal/ipn', $storeId, false); } /** * Get cancel URL * * @param int|null $storeId * @return string */ public function getCancelUrl($storeId = null) { return $this->getUrl('paypal/hostedpro/cancel', $storeId); } /** * Build URL for store * * @param string $path * @param int $storeId * @param bool|null $secure * @return string */ protected function getUrl($path, $storeId, $secure = null) { $store = $this->_storeManager->getStore($storeId); return $this->_urlBuilder->getUrl( $path, ["_secure" => $secure === null ? $store->isCurrentlySecure() : $secure] ); } }