Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Transparent.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Payment\Helper\Formatter;
12 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
17 use Magento\Payment\Model\Method\ConfigInterfaceFactory;
23 
30 {
31  use Formatter;
32 
33  const CC_DETAILS = 'cc_details';
34 
35  const CC_VAULT_CODE = 'payflowpro_cc_vault';
36 
40  protected $_formBlockType = \Magento\Payment\Block\Transparent\Info::class;
41 
45  protected $_infoBlockType = \Magento\Paypal\Block\Payment\Info::class;
46 
50  private $responseValidator;
51 
55  private $paymentTokenFactory;
56 
60  private $paymentExtensionFactory;
61 
84  public function __construct(
85  \Magento\Framework\Model\Context $context,
86  \Magento\Framework\Registry $registry,
87  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
88  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
89  \Magento\Payment\Helper\Data $paymentData,
90  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
91  \Magento\Payment\Model\Method\Logger $logger,
92  \Magento\Framework\Module\ModuleListInterface $moduleList,
93  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
95  ConfigInterfaceFactory $configFactory,
96  Gateway $gateway,
97  HandlerInterface $errorHandler,
98  ResponseValidator $responseValidator,
99  PaymentTokenInterfaceFactory $paymentTokenFactory,
100  OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
101  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
102  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
103  array $data = []
104  ) {
105  parent::__construct(
106  $context,
107  $registry,
108  $extensionFactory,
109  $customAttributeFactory,
110  $paymentData,
111  $scopeConfig,
112  $logger,
113  $moduleList,
114  $localeDate,
117  $gateway,
118  $errorHandler,
119  $resource,
120  $resourceCollection,
121  $data
122  );
123  $this->responseValidator = $responseValidator;
124  $this->paymentTokenFactory = $paymentTokenFactory;
125  $this->paymentExtensionFactory = $paymentExtensionFactory;
126  }
127 
131  public function getResponceValidator()
132  {
133  return $this->responseValidator;
134  }
135 
141  public function validate()
142  {
143  return true;
144  }
145 
155  public function authorize(InfoInterface $payment, $amount)
156  {
158  $request = $this->buildBasicRequest();
159 
161  $order = $payment->getOrder();
164 
165  $token = $payment->getAdditionalInformation(self::PNREF);
166  $request->setData('trxtype', self::TRXTYPE_AUTH_ONLY);
167  $request->setData('origid', $token);
168  $request->setData('amt', $this->formatPrice($amount));
169  $request->setData('currency', $order->getBaseCurrencyCode());
170  $request->setData('taxamt', $this->formatPrice($order->getBaseTaxAmount()));
171  $request->setData('freightamt', $this->formatPrice($order->getBaseShippingAmount()));
172 
173  $response = $this->postRequest($request, $this->getConfig());
174  $this->processErrors($response);
175 
176  try {
177  $this->responseValidator->validate($response, $this);
178  } catch (LocalizedException $exception) {
179  $payment->setParentTransactionId($response->getData(self::PNREF));
180  $this->void($payment);
181  throw new LocalizedException(__("The payment couldn't be processed at this time. Please try again later."));
182  }
183 
184  $this->setTransStatus($payment, $response);
185 
186  $this->createPaymentToken($payment, $token);
187 
188  $payment->unsAdditionalInformation(self::CC_DETAILS);
189  $payment->unsAdditionalInformation(self::PNREF);
190 
191  return $this;
192  }
193 
197  public function getConfigInterface()
198  {
199  return parent::getConfig();
200  }
201 
208  protected function createPaymentToken(Payment $payment, $token)
209  {
211  $paymentToken = $this->paymentTokenFactory->create();
212 
213  $paymentToken->setGatewayToken($token);
214  $paymentToken->setTokenDetails(
215  json_encode($payment->getAdditionalInformation(Transparent::CC_DETAILS))
216  );
217  $paymentToken->setExpiresAt(
218  $this->getExpirationDate($payment)
219  );
220 
221  $this->getPaymentExtensionAttributes($payment)->setVaultPaymentToken($paymentToken);
222  }
223 
228  private function getExpirationDate(Payment $payment)
229  {
230  $expDate = new \DateTime(
231  $payment->getCcExpYear()
232  . '-'
233  . $payment->getCcExpMonth()
234  . '-'
235  . '01'
236  . ' '
237  . '00:00:00',
238  new \DateTimeZone('UTC')
239  );
240  $expDate->add(new \DateInterval('P1M'));
241  return $expDate->format('Y-m-d 00:00:00');
242  }
243 
248  private function getPaymentExtensionAttributes(Payment $payment)
249  {
250  $extensionAttributes = $payment->getExtensionAttributes();
251  if ($extensionAttributes === null) {
252  $extensionAttributes = $this->paymentExtensionFactory->create();
253  $payment->setExtensionAttributes($extensionAttributes);
254  }
255 
256  return $extensionAttributes;
257  }
258 
268  public function capture(InfoInterface $payment, $amount)
269  {
271  $token = $payment->getAdditionalInformation(self::PNREF);
272  parent::capture($payment, $amount);
273 
274  if ($token && !$payment->getAuthorizationTransaction()) {
275  $this->createPaymentToken($payment, $token);
276  }
277 
278  return $this;
279  }
280 }
$response
Definition: 404.php:11
capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Payflowpro.php:415
$order
Definition: order.php:55
__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\Store\Model\StoreManagerInterface $storeManager, ConfigInterfaceFactory $configFactory, Gateway $gateway, HandlerInterface $errorHandler, ResponseValidator $responseValidator, PaymentTokenInterfaceFactory $paymentTokenFactory, OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Transparent.php:84
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$logger
fillCustomerContacts(DataObject $order, DataObject $request)
Definition: Payflowpro.php:855
authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Payflowpro.php:381
$amount
Definition: order.php:14
$payment
Definition: order.php:17
addRequestOrderInfo(DataObject $request, Order $order)
Definition: Payflowpro.php:876
void(\Magento\Payment\Model\InfoInterface $payment)
Definition: Payflowpro.php:452
$extensionAttributes
Definition: payment.php:22
processErrors(DataObject $response)
Definition: Payflowpro.php:653
postRequest(DataObject $request, ConfigInterface $config)
Definition: Payflowpro.php:589
$paymentExtensionFactory
Definition: payment.php:21
setTransStatus($payment, $response)
Definition: Payflowpro.php:824