/** * Refund * * @param \XLite\Model\Payment\BackendTransaction $transaction Backend transaction * * @return boolean */ protected function doRefund(\XLite\Model\Payment\BackendTransaction $transaction) { $this->includeVelocityLibrary(); $backendTransactionStatus = $transaction::STATUS_FAILED; $errorData = ''; if ($this->getSetting('mode') == 'test') { $isTestAccount = true; } else { $isTestAccount = false; } try { $velocityProcessor = new \VelocityProcessor(self::$applicationprofileid, self::$merchantprofileid, self::$workflowid, $isTestAccount, self::$identitytoken); } catch (Exception $e) { $transaction->setDataCell('error_message', $e->getMessage(), 'Velocity error message'); $errorData .= $e->getMessage(); } $refund_amount = $transaction->getValue(); $txnid = $transaction->getPaymentTransaction()->getDataCell('velocity_payment_id')->getValue(); try { // request for refund $response = $velocityProcessor->returnById(array('amount' => $refund_amount, 'TransactionId' => $txnid)); $xml = \VelocityXmlCreator::returnByIdXML($refund_amount, $txnid); // got ReturnById xml object. $req = $xml->saveXML(); $obj_req = serialize($req); if (is_array($response) && !empty($response) && isset($response['Status']) && $response['Status'] == 'Successful') { $backendTransactionStatus = $transaction::STATUS_SUCCESS; $transaction->setDataCell('velocity_refund_id', $response['TransactionId'], 'Velocity Refund ID'); $transaction->setDataCell('approval_code', $response['ApprovalCode'], 'Velocity Approval Code'); $transaction->setDataCell('request_refund_object', $obj_req, 'Velocity Request Refund Object'); $transaction->setDataCell('response_refund_object', serialize($response), 'Velocity Response Refund Object'); $transaction->setDataCell('refund_status', $response['TransactionState'], 'Refund Transaction Status'); $transaction->setStatus($backendTransactionStatus); \XLite\Core\Database::getEM()->flush(); } else { if (is_array($response) && !empty($response)) { $transaction->setDataCell('error_message', $response['StatusMessage'], 'Velocity error message'); $errorData .= $response['StatusMessage']; } else { if (is_string($response)) { $transaction->setDataCell('error_message', $response, 'Velocity error message'); $errorData .= $response; } else { $transaction->setDataCell('error_message', 'Unknown Error please contact the site admin', 'Velocity error message'); $errorData .= 'Unknown Error please contact the site admin'; } } } } catch (Exception $e) { $transaction->setDataCell('error_message', $e->getMessage(), 'Velocity error message'); $errorData .= $e->getMessage(); } if (\XLite\Model\Payment\BackendTransaction::STATUS_SUCCESS == $backendTransactionStatus) { $order = $transaction->getPaymentTransaction()->getOrder(); $paymentTransactionSums = $order->getRawPaymentTransactionSums(); $refunded = $paymentTransactionSums['refunded']; $status = $refunded < $transaction->getPaymentTransaction()->getValue() ? \XLite\Model\Order\Status\Payment::STATUS_PART_PAID : \XLite\Model\Order\Status\Payment::STATUS_REFUNDED; $order->setPaymentStatus($status); \XLite\Core\TopMessage::getInstance()->addInfo('Payment has been refunded successfully'); } else { $msg = 'Transaction failure'; if (!empty($errorData)) { $msg .= '-' . $errorData; } \XLite\Core\TopMessage::getInstance()->addError($msg); } return \XLite\Model\Payment\BackendTransaction::STATUS_SUCCESS == $backendTransactionStatus; }
/** * Refund * * @param \XLite\Model\Payment\BackendTransaction $transaction Backend transaction * @param boolean $isDoVoid Is void action OPTIONAL * * @return boolean */ protected function doRefund(\XLite\Model\Payment\BackendTransaction $transaction, $isDoVoid = false) { $this->includeStripeLibrary(); $backendTransactionStatus = \XLite\Model\Payment\BackendTransaction::STATUS_FAILED; try { $payment = \Stripe_Charge::retrieve($transaction->getPaymentTransaction()->getDataCell('stripe_id')->getValue()); if ($transaction->getValue() != $transaction->getPaymentTransaction()->getValue()) { $payment->refund(array('id' => $transaction->getPaymentTransaction()->getDataCell('stripe_id')->getValue(), 'amount' => $this->formatCurrency($transaction->getValue()))); $refundTransaction = null; foreach ($payment->refunds as $r) { if (!$this->isRefundTransactionRegistered($r)) { $refundTransaction = $r; break; } } } else { $payment->refund(); $refundTransaction = reset($payment->refunds); } if ($refundTransaction) { $backendTransactionStatus = \XLite\Model\Payment\BackendTransaction::STATUS_SUCCESS; $transaction->setDataCell('stripe_date', $refundTransaction->created); if ($refundTransaction->balance_transaction) { $transaction->setDataCell('stripe_b_txntid', $refundTransaction->balance_transaction); } } } catch (\Exception $e) { $transaction->setDataCell('errorMessage', $e->getMessage()); \XLite\Logger::getInstance()->log($e->getMessage(), LOG_ERR); \XLite\Core\TopMessage::addError($e->getMessage()); } $transaction->setStatus($backendTransactionStatus); return \XLite\Model\Payment\BackendTransaction::STATUS_SUCCESS == $backendTransactionStatus; }
/** * Convert order to array for RefundTransaction * * @param \XLite\Model\Payment\BackendTransaction $transaction Transaction * @param string $transactionId Transaction id * * @return array * @see https://developer.paypal.com/docs/classic/api/merchant/DoVoid_API_Operation_NVP/ */ public function convertRefundTransactionParams($transaction, $transactionId) { /** @var \XLite\Model\Order $order */ $order = $transaction->getPaymentTransaction()->getOrder(); /** @var \XLite\Model\Currency $currency */ $currency = $order->getCurrency(); $amount = $currency->roundValue($transaction->getValue()); return array('TRANSACTIONID' => $transactionId, 'AMT' => $amount); }
/** * {@inheritDoc} */ public function getValue() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getValue', array()); return parent::getValue(); }