public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount) { if (!$this->hmacIsValid()) { $this->_logger->error('HMAC SHA-256 from Monetra client ticket request failed verification.'); throw new LocalizedException(__('Verification of payment information failed.')); } $ticket = $this->getInfoInstance()->getAdditionalInformation('ticket'); try { $monetra = new MonetraInterface($this->getMonetraConfigData()); $response = $monetra->authorize($ticket, $amount); } catch (MonetraException $e) { $this->_logger->critical("Error occurred while attempting Monetra authorization. Details: " . $e->getMessage()); throw new LocalizedException(__($this->_scopeConfig->getValue('payment/monetra_client_ticket/user_facing_error_message'))); } if ($response['code'] !== 'AUTH') { $this->_logger->info(sprintf('Monetra authorization failed for TTID %d. Verbiage: %s', $response['ttid'], $response['verbiage'])); throw new LocalizedException(__($this->_scopeConfig->getValue('payment/monetra_client_ticket/user_facing_deny_message'))); } $payment->setTransactionId($response['ttid']); $payment->setIsTransactionClosed(false); return $this; }
/** * Order Payment * @param \Magento\Payment\Model\InfoInterface $payment * @param float $amount * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function order(\Magento\Payment\Model\InfoInterface $payment, $amount) { $order = $payment->getOrder(); $orderId = ltrim($order->getIncrementId(), '0'); $data = ['transaction_id' => $this->getModuleHelper()->genTransactionId($orderId), 'transaction_types' => $this->getConfigHelper()->getTransactionTypes(), 'order' => ['currency' => $order->getBaseCurrencyCode(), 'language' => $this->getModuleHelper()->getLocale(), 'amount' => $amount, 'usage' => $this->getModuleHelper()->buildOrderUsage(), 'description' => $this->getModuleHelper()->buildOrderDescriptionText($order), 'customer' => ['email' => $this->getCheckoutSession()->getQuote()->getCustomerEmail()], 'billing' => $order->getBillingAddress(), 'shipping' => $order->getShippingAddress()], 'urls' => ['notify' => $this->getModuleHelper()->getNotificationUrl($this->getCode()), 'return_success' => $this->getModuleHelper()->getReturnUrl($this->getCode(), "success"), 'return_cancel' => $this->getModuleHelper()->getReturnUrl($this->getCode(), "cancel"), 'return_failure' => $this->getModuleHelper()->getReturnUrl($this->getCode(), "failure")]]; $this->getConfigHelper()->initGatewayClient(); try { $responseObject = $this->checkout($data); $payment->setTransactionId($responseObject->unique_id); $payment->setIsTransactionPending(true); $payment->setIsTransactionClosed(false); $this->getModuleHelper()->setPaymentTransactionAdditionalInfo($payment, $responseObject); $this->getCheckoutSession()->setEmerchantPayCheckoutRedirectUrl($responseObject->redirect_url); return $this; } catch (\Exception $e) { $this->getLogger()->error($e->getMessage()); $this->getModuleHelper()->maskException($e); } }
/** * Captures specified amount, override the method in Braintree CC method as transaction through PayPal does not * can't be cloned * * @param InfoInterface $payment * @param float $amount * @return $this * @throws LocalizedException */ public function capture(InfoInterface $payment, $amount) { try { /** @var \Magento\Sales\Model\Order\Payment $payment */ if ($payment->getCcTransId()) { $result = $this->braintreeTransaction->submitForSettlement($payment->getCcTransId(), $amount); $this->_debug([$payment->getCcTransId() . ' - ' . $amount]); $this->_debug($this->_convertObjToArray($result)); if ($result->success) { $payment->setIsTransactionClosed(false)->setShouldCloseParentTransaction(true); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } else { $this->braintreeAuthorize($payment, $amount, true); } } catch (\Exception $e) { $this->_logger->critical($e); throw new LocalizedException(__('There was an error capturing the transaction: %1.', $e->getMessage())); } return $this; }
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount) { if (!$this->canAuthorize()) { throw new \Magento\Framework\Exception\LocalizedException(__('The authorize action is not available.')); } $payment->getAdditionalInformation(); $order = $payment->getOrder(); $billing_address = $order->getBillingAddress(); $shipping_address = $order->getShippingAddress(); $params = array(); $params["sellerId"] = $this->getConfigData("merchant_id"); $params["privateKey"] = $this->getConfigData("private_key"); $params["merchantOrderId"] = $order->getRealOrderId(); $params["currency"] = $order->getOrderCurrencyCode(); $params["token"] = $payment->getAdditionalInformation()['token']; $params["total"] = round($order->getGrandTotal(), 2); // Set billing info $params["billingAddr"] = array(); $params["billingAddr"]["name"] = $billing_address->getName(); $params["billingAddr"]["addrLine1"] = $billing_address->getStreet()[0]; if (count($billing_address->getStreet()) > 1) { $params["billingAddr"]["addrLine2"] = $billing_address->getStreet()[1]; } $params["billingAddr"]["city"] = $billing_address->getCity(); $params["billingAddr"]["state"] = $billing_address->getRegion(); $params["billingAddr"]["zipCode"] = $billing_address->getPostcode(); $params["billingAddr"]["country"] = $billing_address->getCountryId(); $params["billingAddr"]["email"] = $order->getCustomerEmail(); $params["billingAddr"]["phoneNumber"] = $billing_address->getTelephone(); if (isset($shipping_address)) { $params["shippingAddress"] = array(); $params["shippingAddress"]["name"] = $shipping_address->getName(); $params["shippingAddress"]["addrLine1"] = $shipping_address->getStreet()[0]; if (count($shipping_address->getStreet()) > 1) { $params["shippingAddress"]["addrLine2"] = $shipping_address->getStreet()[1]; } $params["shippingAddress"]["city"] = $shipping_address->getCity(); $params["shippingAddress"]["state"] = $shipping_address->getRegion(); $params["shippingAddress"]["zipCode"] = $shipping_address->getPostcode(); $params["shippingAddress"]["country"] = $shipping_address->getCountryId(); } $client = $this->_httpClientFactory->create(); $path = $this->getConfigData('merchant_id') . "/rs/authService"; $url = $this->getPaymentApiUrl(); $client->setUri($url . $path); $client->setConfig(['maxredirects' => 0, 'timeout' => 30]); $client->setHeaders(['Accept: application/json', 'Content-Type: application/json']); $client->setMethod(\Zend_Http_Client::POST); $client->setRawData(json_encode($params), 'application/json'); try { $response = $client->request(); $responseBody = json_decode($response->getBody(), true); if (isset($responseBody['exception'])) { throw new \Magento\Framework\Exception\LocalizedException(__($responseBody['exception']['errorMsg'])); } elseif (!isset($responseBody['response'])) { throw new \Magento\Framework\Exception\LocalizedException(__('Error placing transaction.')); } } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); } $payment->setTransactionId($responseBody['response']['transactionId']); $payment->setIsTransactionClosed(0); $payment->setTransactionAdditionalInfo('tco_order_number', $responseBody['response']['orderNumber']); $payment->setAdditionalInformation('tco_order_number', $responseBody['response']['orderNumber']); $payment->setAdditionalInformation('tco_order_status', 'approved'); return $this; }
/** * Refunds specified amount * * @param InfoInterface $payment * @param float $amount * @return $this * @throws LocalizedException */ public function refund(InfoInterface $payment, $amount) { $transactionId = $this->braintreeHelper->clearTransactionId($payment->getRefundTransactionId()); try { $transaction = $this->braintreeTransaction->find($transactionId); $this->_debug([$payment->getCcTransId()]); $this->_debug($this->_convertObjToArray($transaction)); if ($transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { if ($transaction->amount != $amount) { $message = __('This refund is for a partial amount but the Transaction has not settled.')->getText(); $message .= ' '; $message .= __('Please wait 24 hours before trying to issue a partial refund.')->getText(); throw new LocalizedException(__($message)); } } $canVoid = $transaction->status === \Braintree_Transaction::AUTHORIZED || $transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT; $result = $canVoid ? $this->braintreeTransaction->void($transactionId) : $this->braintreeTransaction->refund($transactionId, $amount); $this->_debug($this->_convertObjToArray($result)); if ($result->success) { $payment->setIsTransactionClosed(true); } else { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } catch (\Exception $e) { $message = $e->getMessage(); throw new LocalizedException(__('There was an error refunding the transaction: %1.', $message)); } return $this; }
/** * Fetch transaction details info * * Update transaction info if there is one placing transaction only * * @param \Magento\Payment\Model\InfoInterface $payment * @param string $transactionId * @return array */ public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId) { $transaction = $this->transactionRepository->getByTransactionId($transactionId, $payment->getId(), $payment->getOrder()->getId()); $response = $this->getTransactionResponse($transactionId); if ($response->getXResponseCode() == self::RESPONSE_CODE_APPROVED) { if ($response->getTransactionStatus() == 'voided') { $payment->setIsTransactionDenied(true); $payment->setIsTransactionClosed(true); $transaction->close(); } else { $transaction->setAdditionalInformation(self::TRANSACTION_FRAUD_STATE_KEY, false); $payment->setIsTransactionApproved(true); } } elseif ($response->getXResponseReasonCode() == self::RESPONSE_REASON_CODE_PENDING_REVIEW_DECLINED) { $payment->setIsTransactionDenied(true); } $this->addStatusCommentOnUpdate($payment, $response, $transactionId); return []; }
/** * Process capture request * * @param \Magento\Authorizenet\Model\Directpost\Response $result * @param \Magento\Payment\Model\InfoInterface $payment * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function processCapture($result, $payment) { switch ($result->getXResponseCode()) { case self::RESPONSE_CODE_APPROVED: case self::RESPONSE_CODE_HELD: if (in_array($result->getXResponseReasonCode(), [self::RESPONSE_REASON_CODE_APPROVED, self::RESPONSE_REASON_CODE_PENDING_REVIEW, self::RESPONSE_REASON_CODE_PENDING_REVIEW_AUTHORIZED])) { if (!$payment->getParentTransactionId() || $result->getXTransId() != $payment->getParentTransactionId()) { $payment->setTransactionId($result->getXTransId()); } $payment->setIsTransactionClosed(0)->setTransactionAdditionalInfo(self::REAL_TRANSACTION_ID_KEY, $result->getXTransId()); return $this; } throw new \Magento\Framework\Exception\LocalizedException($this->dataHelper->wrapGatewayError($result->getXResponseReasonText())); case self::RESPONSE_CODE_DECLINED: case self::RESPONSE_CODE_ERROR: throw new \Magento\Framework\Exception\LocalizedException($this->dataHelper->wrapGatewayError($result->getXResponseReasonText())); default: throw new \Magento\Framework\Exception\LocalizedException(__('Payment capturing error.')); } }