Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Hostedpro.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Paypal\Model;
7 
13 
18 class Hostedpro extends Direct
19 {
23  const BM_BUTTON_CODE = 'TOKEN';
24 
28  const BM_BUTTON_TYPE = 'PAYMENT';
29 
33  const BM_BUTTON_METHOD = 'BMCreateButton';
34 
41 
45  protected $_formBlockType = \Magento\Paypal\Block\Hosted\Pro\Form::class;
46 
50  protected $_infoBlockType = \Magento\Paypal\Block\Hosted\Pro\Info::class;
51 
57  protected $_canUseInternal = false;
58 
64  protected $_canSaveCc = false;
65 
71  protected $_isInitializeNeeded = true;
72 
77 
99  public function __construct(
100  \Magento\Framework\Model\Context $context,
101  \Magento\Framework\Registry $registry,
102  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
103  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
104  \Magento\Payment\Helper\Data $paymentData,
105  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
106  \Magento\Payment\Model\Method\Logger $logger,
107  \Magento\Framework\Module\ModuleListInterface $moduleList,
108  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
109  \Magento\Paypal\Model\ProFactory $proFactory,
111  \Magento\Framework\UrlInterface $urlBuilder,
112  \Magento\Framework\App\RequestInterface $requestHttp,
113  \Magento\Paypal\Model\CartFactory $cartFactory,
114  \Magento\Paypal\Model\Hostedpro\RequestFactory $hostedproRequestFactory,
115  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
116  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
117  array $data = []
118  ) {
119  $this->hostedproRequestFactory = $hostedproRequestFactory;
120  parent::__construct(
121  $context,
122  $registry,
123  $extensionFactory,
124  $customAttributeFactory,
125  $paymentData,
126  $scopeConfig,
127  $logger,
128  $moduleList,
129  $localeDate,
130  $proFactory,
132  $urlBuilder,
133  $requestHttp,
134  $cartFactory,
135  $resource,
136  $resourceCollection,
137  $data
138  );
139  }
140 
147  public function getAllowedCcTypes()
148  {
149  return true;
150  }
151 
158  public function getMerchantCountry()
159  {
160  return $this->_pro->getConfig()->getMerchantCountry();
161  }
162 
168  public function validate()
169  {
170  return true;
171  }
172 
180  public function initialize($paymentAction, $stateObject)
181  {
182  switch ($paymentAction) {
185  $payment = $this->getInfoInstance();
187  $order = $payment->getOrder();
188  $order->setCanSendNewEmailFlag(false);
189  $payment->setAmountAuthorized($order->getTotalDue());
190  $payment->setBaseAmountAuthorized($order->getBaseTotalDue());
191 
192  $this->setPaymentFormUrl($payment);
193 
194  $stateObject->setState(Order::STATE_PENDING_PAYMENT);
195  $stateObject->setStatus(Order::STATE_PENDING_PAYMENT);
196  $stateObject->setIsNotified(false);
197  break;
198  default:
199  break;
200  }
201  }
202 
211  {
212  $request = $this->buildFormUrlRequest($payment);
214  if ($response) {
215  $payment->setAdditionalInformation('secure_form_url', $response);
216  } else {
217  throw new LocalizedException(__('Cannot get secure form URL from PayPal'));
218  }
219  }
220 
228  {
229  $order = $payment->getOrder();
230  $request = $this->buildBasicRequest()->setOrder($order)->setPaymentMethod($this)->setAmount($order);
231 
232  return $request;
233  }
234 
241  protected function sendFormUrlRequest(Request $request)
242  {
243  $api = $this->_pro->getApi();
244  $response = $api->call(self::BM_BUTTON_METHOD, $request->getRequestData());
245 
246  if (!isset($response['EMAILLINK'])) {
247  return false;
248  }
249  return $response['EMAILLINK'];
250  }
251 
257  protected function buildBasicRequest()
258  {
259  $request = $this->hostedproRequestFactory->create()->setData(
260  [
261  'METHOD' => self::BM_BUTTON_METHOD,
262  'BUTTONCODE' => self::BM_BUTTON_CODE,
263  'BUTTONTYPE' => self::BM_BUTTON_TYPE,
264  ]
265  );
266  return $request;
267  }
268 
275  public function getReturnUrl($storeId = null)
276  {
277  return $this->getUrl('paypal/hostedpro/return', $storeId);
278  }
279 
286  public function getNotifyUrl($storeId = null)
287  {
288  return $this->getUrl('paypal/ipn', $storeId, false);
289  }
290 
297  public function getCancelUrl($storeId = null)
298  {
299  return $this->getUrl('paypal/hostedpro/cancel', $storeId);
300  }
301 
310  protected function getUrl($path, $storeId, $secure = null)
311  {
312  $store = $this->_storeManager->getStore($storeId);
313  return $this->_urlBuilder->getUrl(
314  $path,
315  ["_secure" => $secure === null ? $store->isCurrentlySecure() : $secure]
316  );
317  }
318 }
$response
Definition: 404.php:11
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Payment\Helper\Data $paymentData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Payment\Model\Method\Logger $logger, \Magento\Framework\Module\ModuleListInterface $moduleList, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Paypal\Model\ProFactory $proFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\App\RequestInterface $requestHttp, \Magento\Paypal\Model\CartFactory $cartFactory, \Magento\Paypal\Model\Hostedpro\RequestFactory $hostedproRequestFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Hostedpro.php:99
buildFormUrlRequest(InfoInterface $payment)
Definition: Hostedpro.php:227
setAdditionalInformation($key, $value=null)
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
getUrl($path, $storeId, $secure=null)
Definition: Hostedpro.php:310
$logger
setPaymentFormUrl(InfoInterface $payment)
Definition: Hostedpro.php:210
$payment
Definition: order.php:17
sendFormUrlRequest(Request $request)
Definition: Hostedpro.php:241