Hostedpro.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Payment\Model\InfoInterface;
  9. use Magento\Paypal\Model\Config;
  10. use Magento\Paypal\Model\Hostedpro\Request;
  11. use Magento\Sales\Model\Order;
  12. /**
  13. * Website Payments Pro Hosted Solution payment gateway model
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Hostedpro extends Direct
  17. {
  18. /**
  19. * Button code
  20. */
  21. const BM_BUTTON_CODE = 'TOKEN';
  22. /**
  23. * Button type
  24. */
  25. const BM_BUTTON_TYPE = 'PAYMENT';
  26. /**
  27. * Paypal API method name for button creation
  28. */
  29. const BM_BUTTON_METHOD = 'BMCreateButton';
  30. /**
  31. * Payment method code
  32. *
  33. * @var string
  34. */
  35. protected $_code = Config::METHOD_HOSTEDPRO;
  36. /**
  37. * @var string
  38. */
  39. protected $_formBlockType = \Magento\Paypal\Block\Hosted\Pro\Form::class;
  40. /**
  41. * @var string
  42. */
  43. protected $_infoBlockType = \Magento\Paypal\Block\Hosted\Pro\Info::class;
  44. /**
  45. * Availability option
  46. *
  47. * @var bool
  48. */
  49. protected $_canUseInternal = false;
  50. /**
  51. * Availability option
  52. *
  53. * @var bool
  54. */
  55. protected $_canSaveCc = false;
  56. /**
  57. * Availability option
  58. *
  59. * @var bool
  60. */
  61. protected $_isInitializeNeeded = true;
  62. /**
  63. * @var \Magento\Paypal\Model\Hostedpro\RequestFactory
  64. */
  65. protected $hostedproRequestFactory;
  66. /**
  67. * @param \Magento\Framework\Model\Context $context
  68. * @param \Magento\Framework\Registry $registry
  69. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  70. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  71. * @param \Magento\Payment\Helper\Data $paymentData
  72. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  73. * @param \Magento\Payment\Model\Method\Logger $logger
  74. * @param \Magento\Framework\Module\ModuleListInterface $moduleList
  75. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  76. * @param ProFactory $proFactory
  77. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  78. * @param \Magento\Framework\UrlInterface $urlBuilder
  79. * @param \Magento\Framework\App\RequestInterface $requestHttp
  80. * @param CartFactory $cartFactory
  81. * @param Hostedpro\RequestFactory $hostedproRequestFactory
  82. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  83. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  84. * @param array $data
  85. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  86. */
  87. public function __construct(
  88. \Magento\Framework\Model\Context $context,
  89. \Magento\Framework\Registry $registry,
  90. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  91. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  92. \Magento\Payment\Helper\Data $paymentData,
  93. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  94. \Magento\Payment\Model\Method\Logger $logger,
  95. \Magento\Framework\Module\ModuleListInterface $moduleList,
  96. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  97. \Magento\Paypal\Model\ProFactory $proFactory,
  98. \Magento\Store\Model\StoreManagerInterface $storeManager,
  99. \Magento\Framework\UrlInterface $urlBuilder,
  100. \Magento\Framework\App\RequestInterface $requestHttp,
  101. \Magento\Paypal\Model\CartFactory $cartFactory,
  102. \Magento\Paypal\Model\Hostedpro\RequestFactory $hostedproRequestFactory,
  103. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  104. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  105. array $data = []
  106. ) {
  107. $this->hostedproRequestFactory = $hostedproRequestFactory;
  108. parent::__construct(
  109. $context,
  110. $registry,
  111. $extensionFactory,
  112. $customAttributeFactory,
  113. $paymentData,
  114. $scopeConfig,
  115. $logger,
  116. $moduleList,
  117. $localeDate,
  118. $proFactory,
  119. $storeManager,
  120. $urlBuilder,
  121. $requestHttp,
  122. $cartFactory,
  123. $resource,
  124. $resourceCollection,
  125. $data
  126. );
  127. }
  128. /**
  129. * Return available CC types for gateway based on merchant country.
  130. * We do not have to check the availability of card types.
  131. *
  132. * @return true
  133. */
  134. public function getAllowedCcTypes()
  135. {
  136. return true;
  137. }
  138. /**
  139. * Return merchant country code from config,
  140. * use default country if it not specified in General settings
  141. *
  142. * @return string
  143. */
  144. public function getMerchantCountry()
  145. {
  146. return $this->_pro->getConfig()->getMerchantCountry();
  147. }
  148. /**
  149. * Do not validate payment form using server methods
  150. *
  151. * @return true
  152. */
  153. public function validate()
  154. {
  155. return true;
  156. }
  157. /**
  158. * Instantiate state and set it to state object
  159. *
  160. * @param string $paymentAction
  161. * @param \Magento\Framework\DataObject $stateObject
  162. * @return void
  163. */
  164. public function initialize($paymentAction, $stateObject)
  165. {
  166. switch ($paymentAction) {
  167. case Config::PAYMENT_ACTION_AUTH:
  168. case Config::PAYMENT_ACTION_SALE:
  169. $payment = $this->getInfoInstance();
  170. /** @var \Magento\Sales\Model\Order $order */
  171. $order = $payment->getOrder();
  172. $order->setCanSendNewEmailFlag(false);
  173. $payment->setAmountAuthorized($order->getTotalDue());
  174. $payment->setBaseAmountAuthorized($order->getBaseTotalDue());
  175. $this->setPaymentFormUrl($payment);
  176. $stateObject->setState(Order::STATE_PENDING_PAYMENT);
  177. $stateObject->setStatus(Order::STATE_PENDING_PAYMENT);
  178. $stateObject->setIsNotified(false);
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. /**
  185. * Sends API request to PayPal to get form URL, then sets this URL to $payment object.
  186. *
  187. * @param \Magento\Payment\Model\InfoInterface $payment
  188. * @return void
  189. * @throws \Magento\Framework\Exception\LocalizedException
  190. */
  191. protected function setPaymentFormUrl(InfoInterface $payment)
  192. {
  193. $request = $this->buildFormUrlRequest($payment);
  194. $response = $this->sendFormUrlRequest($request);
  195. if ($response) {
  196. $payment->setAdditionalInformation('secure_form_url', $response);
  197. } else {
  198. throw new LocalizedException(__('Cannot get secure form URL from PayPal'));
  199. }
  200. }
  201. /**
  202. * Returns request object with needed data for API request to PayPal to get form URL.
  203. *
  204. * @param \Magento\Payment\Model\InfoInterface $payment
  205. * @return \Magento\Paypal\Model\Hostedpro\Request
  206. */
  207. protected function buildFormUrlRequest(InfoInterface $payment)
  208. {
  209. $order = $payment->getOrder();
  210. $request = $this->buildBasicRequest()->setOrder($order)->setPaymentMethod($this)->setAmount($order);
  211. return $request;
  212. }
  213. /**
  214. * Returns form URL from request to PayPal.
  215. *
  216. * @param \Magento\Paypal\Model\Hostedpro\Request $request
  217. * @return string|false
  218. */
  219. protected function sendFormUrlRequest(Request $request)
  220. {
  221. $api = $this->_pro->getApi();
  222. $response = $api->call(self::BM_BUTTON_METHOD, $request->getRequestData());
  223. if (!isset($response['EMAILLINK'])) {
  224. return false;
  225. }
  226. return $response['EMAILLINK'];
  227. }
  228. /**
  229. * Return request object with basic information
  230. *
  231. * @return \Magento\Paypal\Model\Hostedpro\Request
  232. */
  233. protected function buildBasicRequest()
  234. {
  235. $request = $this->hostedproRequestFactory->create()->setData(
  236. [
  237. 'METHOD' => self::BM_BUTTON_METHOD,
  238. 'BUTTONCODE' => self::BM_BUTTON_CODE,
  239. 'BUTTONTYPE' => self::BM_BUTTON_TYPE,
  240. ]
  241. );
  242. return $request;
  243. }
  244. /**
  245. * Get return URL
  246. *
  247. * @param int|null $storeId
  248. * @return string
  249. */
  250. public function getReturnUrl($storeId = null)
  251. {
  252. return $this->getUrl('paypal/hostedpro/return', $storeId);
  253. }
  254. /**
  255. * Get notify (IPN) URL
  256. *
  257. * @param int|null $storeId
  258. * @return string
  259. */
  260. public function getNotifyUrl($storeId = null)
  261. {
  262. return $this->getUrl('paypal/ipn', $storeId, false);
  263. }
  264. /**
  265. * Get cancel URL
  266. *
  267. * @param int|null $storeId
  268. * @return string
  269. */
  270. public function getCancelUrl($storeId = null)
  271. {
  272. return $this->getUrl('paypal/hostedpro/cancel', $storeId);
  273. }
  274. /**
  275. * Build URL for store
  276. *
  277. * @param string $path
  278. * @param int $storeId
  279. * @param bool|null $secure
  280. * @return string
  281. */
  282. protected function getUrl($path, $storeId, $secure = null)
  283. {
  284. $store = $this->_storeManager->getStore($storeId);
  285. return $this->_urlBuilder->getUrl(
  286. $path,
  287. ["_secure" => $secure === null ? $store->isCurrentlySecure() : $secure]
  288. );
  289. }
  290. }