Transparent.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Payflow;
  7. use Magento\Payment\Helper\Formatter;
  8. use Magento\Payment\Model\InfoInterface;
  9. use Magento\Paypal\Model\Payflowpro;
  10. use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
  11. use Magento\Sales\Model\Order\Payment;
  12. use Magento\Paypal\Model\Payflow\Service\Gateway;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Payment\Model\Method\TransparentInterface;
  15. use Magento\Payment\Model\Method\ConfigInterfaceFactory;
  16. use Magento\Framework\Exception\State\InvalidTransitionException;
  17. use Magento\Paypal\Model\Payflow\Service\Response\Handler\HandlerInterface;
  18. use Magento\Paypal\Model\Payflow\Service\Response\Validator\ResponseValidator;
  19. use Magento\Vault\Api\Data\PaymentTokenInterface;
  20. use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory;
  21. /**
  22. * Payflow Pro payment gateway model
  23. *
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class Transparent extends Payflowpro implements TransparentInterface
  27. {
  28. use Formatter;
  29. const CC_DETAILS = 'cc_details';
  30. const CC_VAULT_CODE = 'payflowpro_cc_vault';
  31. /**
  32. * @var string
  33. */
  34. protected $_formBlockType = \Magento\Payment\Block\Transparent\Info::class;
  35. /**
  36. * @var string
  37. */
  38. protected $_infoBlockType = \Magento\Paypal\Block\Payment\Info::class;
  39. /**
  40. * @var ResponseValidator
  41. */
  42. private $responseValidator;
  43. /**
  44. * @var PaymentTokenInterfaceFactory
  45. */
  46. private $paymentTokenFactory;
  47. /**
  48. * @var OrderPaymentExtensionInterfaceFactory
  49. */
  50. private $paymentExtensionFactory;
  51. /**
  52. * @param \Magento\Framework\Model\Context $context
  53. * @param \Magento\Framework\Registry $registry
  54. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  55. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  56. * @param \Magento\Payment\Helper\Data $paymentData
  57. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  58. * @param \Magento\Payment\Model\Method\Logger $logger
  59. * @param \Magento\Framework\Module\ModuleListInterface $moduleList
  60. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  61. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  62. * @param ConfigInterfaceFactory $configFactory
  63. * @param Gateway $gateway
  64. * @param HandlerInterface $errorHandler
  65. * @param ResponseValidator $responseValidator
  66. * @param PaymentTokenInterfaceFactory $paymentTokenFactory
  67. * @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
  68. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  69. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  70. * @param array $data
  71. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  72. */
  73. public function __construct(
  74. \Magento\Framework\Model\Context $context,
  75. \Magento\Framework\Registry $registry,
  76. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  77. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  78. \Magento\Payment\Helper\Data $paymentData,
  79. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  80. \Magento\Payment\Model\Method\Logger $logger,
  81. \Magento\Framework\Module\ModuleListInterface $moduleList,
  82. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  83. \Magento\Store\Model\StoreManagerInterface $storeManager,
  84. ConfigInterfaceFactory $configFactory,
  85. Gateway $gateway,
  86. HandlerInterface $errorHandler,
  87. ResponseValidator $responseValidator,
  88. PaymentTokenInterfaceFactory $paymentTokenFactory,
  89. OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
  90. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  91. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  92. array $data = []
  93. ) {
  94. parent::__construct(
  95. $context,
  96. $registry,
  97. $extensionFactory,
  98. $customAttributeFactory,
  99. $paymentData,
  100. $scopeConfig,
  101. $logger,
  102. $moduleList,
  103. $localeDate,
  104. $storeManager,
  105. $configFactory,
  106. $gateway,
  107. $errorHandler,
  108. $resource,
  109. $resourceCollection,
  110. $data
  111. );
  112. $this->responseValidator = $responseValidator;
  113. $this->paymentTokenFactory = $paymentTokenFactory;
  114. $this->paymentExtensionFactory = $paymentExtensionFactory;
  115. }
  116. /**
  117. * @return ResponseValidator
  118. */
  119. public function getResponceValidator()
  120. {
  121. return $this->responseValidator;
  122. }
  123. /**
  124. * Do not validate payment form using server methods
  125. *
  126. * @return bool
  127. */
  128. public function validate()
  129. {
  130. return true;
  131. }
  132. /**
  133. * Performs authorize transaction
  134. *
  135. * @param InfoInterface|Object $payment
  136. * @param float $amount
  137. * @return $this
  138. * @throws InvalidTransitionException
  139. * @throws LocalizedException
  140. */
  141. public function authorize(InfoInterface $payment, $amount)
  142. {
  143. /** @var Payment $payment */
  144. $request = $this->buildBasicRequest();
  145. /** @var \Magento\Sales\Model\Order $order */
  146. $order = $payment->getOrder();
  147. $this->addRequestOrderInfo($request, $order);
  148. $request = $this->fillCustomerContacts($order, $request);
  149. $token = $payment->getAdditionalInformation(self::PNREF);
  150. $request->setData('trxtype', self::TRXTYPE_AUTH_ONLY);
  151. $request->setData('origid', $token);
  152. $request->setData('amt', $this->formatPrice($amount));
  153. $request->setData('currency', $order->getBaseCurrencyCode());
  154. $request->setData('taxamt', $this->formatPrice($order->getBaseTaxAmount()));
  155. $request->setData('freightamt', $this->formatPrice($order->getBaseShippingAmount()));
  156. $response = $this->postRequest($request, $this->getConfig());
  157. $this->processErrors($response);
  158. try {
  159. $this->responseValidator->validate($response, $this);
  160. } catch (LocalizedException $exception) {
  161. $payment->setParentTransactionId($response->getData(self::PNREF));
  162. $this->void($payment);
  163. throw new LocalizedException(__("The payment couldn't be processed at this time. Please try again later."));
  164. }
  165. $this->setTransStatus($payment, $response);
  166. $this->createPaymentToken($payment, $token);
  167. $payment->unsAdditionalInformation(self::CC_DETAILS);
  168. $payment->unsAdditionalInformation(self::PNREF);
  169. return $this;
  170. }
  171. /**
  172. * {inheritdoc}
  173. */
  174. public function getConfigInterface()
  175. {
  176. return parent::getConfig();
  177. }
  178. /**
  179. * @param Payment $payment
  180. * @param string $token
  181. * @throws LocalizedException
  182. * @return void
  183. */
  184. protected function createPaymentToken(Payment $payment, $token)
  185. {
  186. /** @var PaymentTokenInterface $paymentToken */
  187. $paymentToken = $this->paymentTokenFactory->create();
  188. $paymentToken->setGatewayToken($token);
  189. $paymentToken->setTokenDetails(
  190. json_encode($payment->getAdditionalInformation(Transparent::CC_DETAILS))
  191. );
  192. $paymentToken->setExpiresAt(
  193. $this->getExpirationDate($payment)
  194. );
  195. $this->getPaymentExtensionAttributes($payment)->setVaultPaymentToken($paymentToken);
  196. }
  197. /**
  198. * @param Payment $payment
  199. * @return string
  200. */
  201. private function getExpirationDate(Payment $payment)
  202. {
  203. $expDate = new \DateTime(
  204. $payment->getCcExpYear()
  205. . '-'
  206. . $payment->getCcExpMonth()
  207. . '-'
  208. . '01'
  209. . ' '
  210. . '00:00:00',
  211. new \DateTimeZone('UTC')
  212. );
  213. $expDate->add(new \DateInterval('P1M'));
  214. return $expDate->format('Y-m-d 00:00:00');
  215. }
  216. /**
  217. * @param Payment $payment
  218. * @return \Magento\Sales\Api\Data\OrderPaymentExtensionInterface
  219. */
  220. private function getPaymentExtensionAttributes(Payment $payment)
  221. {
  222. $extensionAttributes = $payment->getExtensionAttributes();
  223. if ($extensionAttributes === null) {
  224. $extensionAttributes = $this->paymentExtensionFactory->create();
  225. $payment->setExtensionAttributes($extensionAttributes);
  226. }
  227. return $extensionAttributes;
  228. }
  229. /**
  230. * Capture payment
  231. *
  232. * @param InfoInterface|Payment $payment
  233. * @param float $amount
  234. * @return $this
  235. * @throws \Magento\Framework\Exception\LocalizedException
  236. * @throws \Magento\Framework\Exception\State\InvalidTransitionException
  237. */
  238. public function capture(InfoInterface $payment, $amount)
  239. {
  240. /** @var Payment $payment */
  241. $token = $payment->getAdditionalInformation(self::PNREF);
  242. parent::capture($payment, $amount);
  243. if ($token && !$payment->getAuthorizationTransaction()) {
  244. $this->createPaymentToken($payment, $token);
  245. }
  246. return $this;
  247. }
  248. }