/** * Payment refund * * @param \Magento\Payment\Model\InfoInterface $payment * @param float $amount * @return $this * @throws \Magento\Framework\Validator\Exception */ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) { $transactionId = $payment->getParentTransactionId(); try { \Stripe\Charge::retrieve($transactionId)->refund(); } catch (\Exception $e) { $this->debugData(['transaction_id' => $transactionId, 'exception' => $e->getMessage()]); $this->_logger->error(__('Payment refunding error.')); throw new \Magento\Framework\Validator\Exception(__('Payment refunding error.')); } $payment->setTransactionId($transactionId . '-' . \Magento\Sales\Model\Order\Payment\Transaction::TYPE_REFUND)->setParentTransactionId($transactionId)->setIsTransactionClosed(1)->setShouldCloseParentTransaction(1); return $this; }
public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) { if ($amount <= 0) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid amount for refund.')); } if (!$payment->getParentTransactionId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid transaction ID.')); } $orderNumber = $payment->getAdditionalInformation('tco_order_number'); $args = array('sale_id' => $orderNumber, 'category' => 5, 'comment' => 'Refund issued by merchant.', 'amount' => $amount, 'currency' => 'vendor'); $client = $this->_httpClientFactory->create(); $path = 'sales/refund_invoice'; $url = $this->getApiUrl(); $client->setUri($url . $path); $client->setConfig(['maxredirects' => 0, 'timeout' => 30]); $client->setAuth($this->getConfigData('api_user'), $this->getConfigData('api_pass')); $client->setHeaders(['Accept: application/json']); $client->setParameterPost($args); $client->setMethod(\Zend_Http_Client::POST); try { $response = $client->request(); $responseBody = json_decode($response->getBody(), true); if (isset($responseBody['errors'])) { $this->_logger->critical(sprintf('Error Refunding Invoice: "%s"', $responseBody['errors'][0]['message'])); throw new \Magento\Framework\Exception\LocalizedException(__($responseBody['errors'][0]['message'])); } elseif (!isset($responseBody['response_code']) || !isset($responseBody['response_message'])) { throw new \Magento\Framework\Exception\LocalizedException(__('Error refunding transaction.')); } elseif ($responseBody['response_code'] != 'OK') { throw new \Magento\Framework\Exception\LocalizedException(__($responseBody['response_message'])); } } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); } return $this; }
/** * Refund capture * * @param InfoInterface|Payment|Object $payment * @param float $amount * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\State\InvalidTransitionException */ public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) { $request = $this->buildBasicRequest(); $request->setTrxtype(self::TRXTYPE_CREDIT); $request->setOrigid($payment->getParentTransactionId()); $request->setAmt(round($amount, 2)); $response = $this->postRequest($request, $this->getConfig()); $this->processErrors($response); if ($response->getResultCode() == self::RESPONSE_CODE_APPROVED) { $payment->setTransactionId($response->getPnref())->setIsTransactionClosed(true); } return $this; }
/** * Void the payment through gateway * * @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface $payment * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function void(\Magento\Payment\Model\InfoInterface $payment) { if (!$payment->getParentTransactionId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid transaction ID.')); } $payment->setAnetTransType(self::REQUEST_TYPE_VOID); $payment->setXTransId($this->getRealParentTransactionId($payment)); $request = $this->buildRequest($payment); $result = $this->postRequest($request); switch ($result->getXResponseCode()) { case self::RESPONSE_CODE_APPROVED: if ($result->getXResponseReasonCode() == self::RESPONSE_REASON_CODE_APPROVED) { if ($result->getXTransId() != $payment->getParentTransactionId()) { $payment->setTransactionId($result->getXTransId()); } $payment->setIsTransactionClosed(1)->setShouldCloseParentTransaction(1)->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 voiding error.')); } }
public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) { $ttid = $payment->getParentTransactionId(); try { $monetra = new MonetraInterface($this->getMonetraConfigData()); $response = $monetra->refund($ttid, $amount); } catch (MonetraException $e) { $this->_logger->critical("Error occurred while attempting Monetra refund. 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 refund failed for TTID %d. Verbiage: %s', $response['ttid'], $response['verbiage'])); throw new LocalizedException(__('Refund request failed. Details: ' . $response['verbiage'])); } return $this; }