PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-paypal/Model/Direct.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 511 lines | 225 code | 49 blank | 237 comment | 15 complexity | 88af8106e3d4891a1dcfff62a8ea9fe5 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Quote\Api\Data\PaymentInterface;
  8. use Magento\Sales\Model\Order\Payment;
  9. /**
  10. * PayPal Direct Module
  11. * @SuppressWarnings(PHPMD.TooManyFields)
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Direct extends \Magento\Payment\Model\Method\Cc
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $_code = \Magento\Paypal\Model\Config::METHOD_WPP_DIRECT;
  20. /**
  21. * @var string
  22. */
  23. protected $_infoBlockType = 'Magento\Paypal\Block\Payment\Info';
  24. /**
  25. * Availability option
  26. *
  27. * @var bool
  28. */
  29. protected $_isGateway = true;
  30. /**
  31. * Availability option
  32. *
  33. * @var bool
  34. */
  35. protected $_canAuthorize = true;
  36. /**
  37. * Availability option
  38. *
  39. * @var bool
  40. */
  41. protected $_canCapture = true;
  42. /**
  43. * Availability option
  44. *
  45. * @var bool
  46. */
  47. protected $_canCapturePartial = true;
  48. /**
  49. * Availability option
  50. *
  51. * @var bool
  52. */
  53. protected $_canRefund = true;
  54. /**
  55. * Availability option
  56. *
  57. * @var bool
  58. */
  59. protected $_canRefundInvoicePartial = true;
  60. /**
  61. * Availability option
  62. *
  63. * @var bool
  64. */
  65. protected $_canVoid = true;
  66. /**
  67. * Availability option
  68. *
  69. * @var bool
  70. */
  71. protected $_canUseInternal = true;
  72. /**
  73. * Availability option
  74. *
  75. * @var bool
  76. */
  77. protected $_canUseCheckout = true;
  78. /**
  79. * Availability option
  80. *
  81. * @var bool
  82. */
  83. protected $_canSaveCc = false;
  84. /**
  85. * @var bool
  86. */
  87. protected $_canFetchTransactionInfo = true;
  88. /**
  89. * Availability option
  90. *
  91. * @var bool
  92. */
  93. protected $_canReviewPayment = true;
  94. /**
  95. * Website Payments Pro instance
  96. *
  97. * @var \Magento\Paypal\Model\Pro
  98. */
  99. protected $_pro;
  100. /**
  101. * @var \Magento\Store\Model\StoreManagerInterface
  102. */
  103. protected $_storeManager;
  104. /**
  105. * @var \Magento\Framework\UrlInterface
  106. */
  107. protected $_urlBuilder;
  108. /**
  109. * @var \Magento\Framework\App\RequestInterface
  110. */
  111. protected $_requestHttp;
  112. /**
  113. * @var \Magento\Paypal\Model\CartFactory
  114. */
  115. protected $_cartFactory;
  116. /**
  117. * @param \Magento\Framework\Model\Context $context
  118. * @param \Magento\Framework\Registry $registry
  119. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  120. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  121. * @param \Magento\Payment\Helper\Data $paymentData
  122. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  123. * @param \Magento\Payment\Model\Method\Logger $logger
  124. * @param \Magento\Framework\Module\ModuleListInterface $moduleList
  125. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  126. * @param ProFactory $proFactory
  127. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  128. * @param \Magento\Framework\UrlInterface $urlBuilder
  129. * @param \Magento\Framework\App\RequestInterface $requestHttp
  130. * @param CartFactory $cartFactory
  131. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  132. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  133. * @param array $data
  134. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  135. */
  136. public function __construct(
  137. \Magento\Framework\Model\Context $context,
  138. \Magento\Framework\Registry $registry,
  139. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  140. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  141. \Magento\Payment\Helper\Data $paymentData,
  142. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  143. \Magento\Payment\Model\Method\Logger $logger,
  144. \Magento\Framework\Module\ModuleListInterface $moduleList,
  145. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  146. \Magento\Paypal\Model\ProFactory $proFactory,
  147. \Magento\Store\Model\StoreManagerInterface $storeManager,
  148. \Magento\Framework\UrlInterface $urlBuilder,
  149. \Magento\Framework\App\RequestInterface $requestHttp,
  150. \Magento\Paypal\Model\CartFactory $cartFactory,
  151. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  152. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  153. array $data = []
  154. ) {
  155. parent::__construct(
  156. $context,
  157. $registry,
  158. $extensionFactory,
  159. $customAttributeFactory,
  160. $paymentData,
  161. $scopeConfig,
  162. $logger,
  163. $moduleList,
  164. $localeDate,
  165. $resource,
  166. $resourceCollection,
  167. $data
  168. );
  169. $this->_storeManager = $storeManager;
  170. $this->_urlBuilder = $urlBuilder;
  171. $this->_requestHttp = $requestHttp;
  172. $this->_cartFactory = $cartFactory;
  173. $proInstance = array_shift($data);
  174. if ($proInstance && $proInstance instanceof \Magento\Paypal\Model\Pro) {
  175. $this->_pro = $proInstance;
  176. } else {
  177. $this->_pro = $proFactory->create();
  178. }
  179. $this->_pro->setMethod($this->_code);
  180. }
  181. /**
  182. * Store setter
  183. * Also updates store ID in config object
  184. *
  185. * @param \Magento\Store\Model\Store|int $store
  186. * @return $this
  187. */
  188. public function setStore($store)
  189. {
  190. $this->setData('store', $store);
  191. if (null === $store) {
  192. $store = $this->_storeManager->getStore()->getId();
  193. }
  194. $this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
  195. return $this;
  196. }
  197. /**
  198. * Whether method is available for specified currency
  199. *
  200. * @param string $currencyCode
  201. * @return bool
  202. */
  203. public function canUseForCurrency($currencyCode)
  204. {
  205. return $this->_pro->getConfig()->isCurrencyCodeSupported($currencyCode);
  206. }
  207. /**
  208. * Payment action getter compatible with payment model
  209. *
  210. * @see \Magento\Sales\Model\Payment::place()
  211. * @return string
  212. */
  213. public function getConfigPaymentAction()
  214. {
  215. return $this->_pro->getConfig()->getPaymentAction();
  216. }
  217. /**
  218. * Return available CC types for gateway based on merchant country
  219. *
  220. * @return string
  221. */
  222. public function getAllowedCcTypes()
  223. {
  224. $ccTypes = explode(',', $this->_pro->getConfig()->getValue('cctypes'));
  225. $country = $this->_pro->getConfig()->getMerchantCountry();
  226. if ($country == 'GB') {
  227. $ccTypes = array_intersect(['SM', 'SO', 'MC', 'DI', 'VI'], $ccTypes);
  228. } elseif ($country == 'CA') {
  229. $ccTypes = array_intersect(['MC', 'VI'], $ccTypes);
  230. }
  231. return implode(',', $ccTypes);
  232. }
  233. /**
  234. * Check whether payment method can be used
  235. *
  236. * @param \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote|null $quote
  237. * @return bool
  238. */
  239. public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
  240. {
  241. return parent::isAvailable($quote) && $this->_pro->getConfig()->isMethodAvailable();
  242. }
  243. /**
  244. * Custom getter for payment configuration
  245. *
  246. * @param string $field
  247. * @param int|null $storeId
  248. * @return null|string
  249. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  250. */
  251. public function getConfigData($field, $storeId = null)
  252. {
  253. $value = null;
  254. switch ($field) {
  255. case 'cctypes':
  256. $value = $this->getAllowedCcTypes();
  257. break;
  258. case 'order_place_redirect_url':
  259. $value = $this->getOrderPlaceRedirectUrl();
  260. break;
  261. default:
  262. $value = $this->_pro->getConfig()->getValue($field);
  263. }
  264. return $value;
  265. }
  266. /**
  267. * Authorize payment
  268. *
  269. * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
  270. * @param float $amount
  271. * @return $this
  272. */
  273. public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
  274. {
  275. return $this->_placeOrder($payment, $amount);
  276. }
  277. /**
  278. * Void payment
  279. *
  280. * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
  281. * @return $this
  282. * @throws \Magento\Framework\Exception\LocalizedException
  283. */
  284. public function void(\Magento\Payment\Model\InfoInterface $payment)
  285. {
  286. $this->_pro->void($payment);
  287. return $this;
  288. }
  289. /**
  290. * Capture payment
  291. *
  292. * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
  293. * @param float $amount
  294. * @return $this
  295. */
  296. public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
  297. {
  298. if (false === $this->_pro->capture($payment, $amount)) {
  299. $this->_placeOrder($payment, $amount);
  300. }
  301. return $this;
  302. }
  303. /**
  304. * Refund capture
  305. *
  306. * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
  307. * @param float $amount
  308. * @return $this
  309. * @throws \Magento\Framework\Exception\LocalizedException
  310. */
  311. public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
  312. {
  313. $this->_pro->refund($payment, $amount);
  314. return $this;
  315. }
  316. /**
  317. * Cancel payment
  318. *
  319. * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
  320. * @return $this
  321. */
  322. public function cancel(\Magento\Payment\Model\InfoInterface $payment)
  323. {
  324. $this->void($payment);
  325. return $this;
  326. }
  327. /**
  328. * Whether payment can be reviewed
  329. * @return bool
  330. */
  331. public function canReviewPayment()
  332. {
  333. return parent::canReviewPayment() && $this->_pro->canReviewPayment($this->getInfoInstance());
  334. }
  335. /**
  336. * Attempt to accept a pending payment
  337. *
  338. * @param \Magento\Payment\Model\Info|Payment $payment
  339. * @return bool
  340. */
  341. public function acceptPayment(\Magento\Payment\Model\InfoInterface $payment)
  342. {
  343. parent::acceptPayment($payment);
  344. return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_ACCEPT);
  345. }
  346. /**
  347. * Attempt to deny a pending payment
  348. *
  349. * @param \Magento\Payment\Model\Info|PaymentInterface $payment
  350. * @return bool
  351. */
  352. public function denyPayment(\Magento\Payment\Model\InfoInterface $payment)
  353. {
  354. parent::denyPayment($payment);
  355. return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_DENY);
  356. }
  357. /**
  358. * Fetch transaction details info
  359. *
  360. * @param \Magento\Payment\Model\InfoInterface $payment
  361. * @param string $transactionId
  362. * @return array
  363. */
  364. public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
  365. {
  366. return $this->_pro->fetchTransactionInfo($payment, $transactionId);
  367. }
  368. /**
  369. * Place an order with authorization or capture action
  370. *
  371. * @param Payment $payment
  372. * @param float $amount
  373. * @return $this
  374. */
  375. protected function _placeOrder(Payment $payment, $amount)
  376. {
  377. $order = $payment->getOrder();
  378. $api = $this->_pro->getApi()->setPaymentAction(
  379. $this->_pro->getConfig()->getValue('paymentAction')
  380. )->setIpAddress(
  381. $this->_requestHttp->getClientIp(false)
  382. )->setAmount(
  383. $amount
  384. )->setCurrencyCode(
  385. $order->getBaseCurrencyCode()
  386. )->setInvNum(
  387. $order->getIncrementId()
  388. )->setEmail(
  389. $order->getCustomerEmail()
  390. )->setNotifyUrl(
  391. $this->_urlBuilder->getUrl('paypal/ipn/')
  392. )->setCreditCardType(
  393. $payment->getCcType()
  394. )->setCreditCardNumber(
  395. $payment->getCcNumber()
  396. )->setCreditCardExpirationDate(
  397. $this->_getFormattedCcExpirationDate($payment->getCcExpMonth(), $payment->getCcExpYear())
  398. )->setCreditCardCvv2(
  399. $payment->getCcCid()
  400. )->setMaestroSoloIssueNumber(
  401. $payment->getCcSsIssue()
  402. );
  403. if ($payment->getCcSsStartMonth() && $payment->getCcSsStartYear()) {
  404. $year = sprintf('%02d', substr($payment->getCcSsStartYear(), -2, 2));
  405. $api->setMaestroSoloIssueDate($this->_getFormattedCcExpirationDate($payment->getCcSsStartMonth(), $year));
  406. }
  407. // add shipping and billing addresses
  408. if ($order->getIsVirtual()) {
  409. $api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
  410. } else {
  411. $api->setAddress($order->getShippingAddress());
  412. $api->setBillingAddress($order->getBillingAddress());
  413. }
  414. // add line items
  415. $cart = $this->_cartFactory->create(['salesModel' => $order]);
  416. $api->setPaypalCart($cart)->setIsLineItemsEnabled($this->_pro->getConfig()->getValue('lineItemsEnabled'));
  417. // call api and import transaction and other payment information
  418. $api->callDoDirectPayment();
  419. $this->_importResultToPayment($api, $payment);
  420. try {
  421. $api->callGetTransactionDetails();
  422. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  423. // if we receive errors, but DoDirectPayment response is Success, then set Pending status for transaction
  424. $payment->setIsTransactionPending(true);
  425. }
  426. $this->_importResultToPayment($api, $payment);
  427. return $this;
  428. }
  429. /**
  430. * Format credit card expiration date based on month and year values
  431. * Format: mmyyyy
  432. *
  433. * @param string|int $month
  434. * @param string|int $year
  435. * @return string
  436. */
  437. protected function _getFormattedCcExpirationDate($month, $year)
  438. {
  439. return sprintf('%02d%02d', $month, $year);
  440. }
  441. /**
  442. * Import direct payment results to payment
  443. *
  444. * @param \Magento\Paypal\Model\Api\Nvp $api
  445. * @param Payment $payment
  446. * @return void
  447. */
  448. protected function _importResultToPayment($api, $payment)
  449. {
  450. $payment->setTransactionId($api->getTransactionId())->setIsTransactionClosed(0);
  451. $this->_pro->importPaymentInfo($api, $payment);
  452. }
  453. /**
  454. * Check void availability
  455. * @return bool
  456. * @internal param \Magento\Framework\DataObject $payment
  457. */
  458. public function canVoid()
  459. {
  460. return $this->_canVoid;
  461. }
  462. }