responseValidator = $responseValidator; $this->paymentTokenFactory = $paymentTokenFactory; $this->paymentExtensionFactory = $paymentExtensionFactory; } /** * @return ResponseValidator */ public function getResponceValidator() { return $this->responseValidator; } /** * Do not validate payment form using server methods * * @return bool */ public function validate() { return true; } /** * Performs authorize transaction * * @param InfoInterface|Object $payment * @param float $amount * @return $this * @throws InvalidTransitionException * @throws LocalizedException */ public function authorize(InfoInterface $payment, $amount) { /** @var Payment $payment */ $request = $this->buildBasicRequest(); /** @var \Magento\Sales\Model\Order $order */ $order = $payment->getOrder(); $this->addRequestOrderInfo($request, $order); $request = $this->fillCustomerContacts($order, $request); $token = $payment->getAdditionalInformation(self::PNREF); $request->setData('trxtype', self::TRXTYPE_AUTH_ONLY); $request->setData('origid', $token); $request->setData('amt', $this->formatPrice($amount)); $request->setData('currency', $order->getBaseCurrencyCode()); $request->setData('taxamt', $this->formatPrice($order->getBaseTaxAmount())); $request->setData('freightamt', $this->formatPrice($order->getBaseShippingAmount())); $response = $this->postRequest($request, $this->getConfig()); $this->processErrors($response); try { $this->responseValidator->validate($response, $this); } catch (LocalizedException $exception) { $payment->setParentTransactionId($response->getData(self::PNREF)); $this->void($payment); throw new LocalizedException(__("The payment couldn't be processed at this time. Please try again later.")); } $this->setTransStatus($payment, $response); $this->createPaymentToken($payment, $token); $payment->unsAdditionalInformation(self::CC_DETAILS); $payment->unsAdditionalInformation(self::PNREF); return $this; } /** * {inheritdoc} */ public function getConfigInterface() { return parent::getConfig(); } /** * @param Payment $payment * @param string $token * @throws LocalizedException * @return void */ protected function createPaymentToken(Payment $payment, $token) { /** @var PaymentTokenInterface $paymentToken */ $paymentToken = $this->paymentTokenFactory->create(); $paymentToken->setGatewayToken($token); $paymentToken->setTokenDetails( json_encode($payment->getAdditionalInformation(Transparent::CC_DETAILS)) ); $paymentToken->setExpiresAt( $this->getExpirationDate($payment) ); $this->getPaymentExtensionAttributes($payment)->setVaultPaymentToken($paymentToken); } /** * @param Payment $payment * @return string */ private function getExpirationDate(Payment $payment) { $expDate = new \DateTime( $payment->getCcExpYear() . '-' . $payment->getCcExpMonth() . '-' . '01' . ' ' . '00:00:00', new \DateTimeZone('UTC') ); $expDate->add(new \DateInterval('P1M')); return $expDate->format('Y-m-d 00:00:00'); } /** * @param Payment $payment * @return \Magento\Sales\Api\Data\OrderPaymentExtensionInterface */ private function getPaymentExtensionAttributes(Payment $payment) { $extensionAttributes = $payment->getExtensionAttributes(); if ($extensionAttributes === null) { $extensionAttributes = $this->paymentExtensionFactory->create(); $payment->setExtensionAttributes($extensionAttributes); } return $extensionAttributes; } /** * Capture payment * * @param InfoInterface|Payment $payment * @param float $amount * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\State\InvalidTransitionException */ public function capture(InfoInterface $payment, $amount) { /** @var Payment $payment */ $token = $payment->getAdditionalInformation(self::PNREF); parent::capture($payment, $amount); if ($token && !$payment->getAuthorizationTransaction()) { $this->createPaymentToken($payment, $token); } return $this; } }