/** * Method is invoked before save * * @param Varien_Object $object * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract */ public function beforeSave($object) { if ($object->getCreditmemo()) { $object->setParentId($object->getCreditmemo()->getId()); } return parent::beforeSave($object); }
/** * Getting all available childs for Invoice, Shipmen or Creditmemo item * * @param Varien_Object $item * @return array */ public function getChilds($item) { $_itemsArray = array(); if ($item instanceof Mage_Sales_Model_Order_Invoice_Item) { $_items = $item->getInvoice()->getAllItems(); } else { if ($item instanceof Mage_Sales_Model_Order_Shipment_Item) { $_items = $item->getShipment()->getAllItems(); } else { if ($item instanceof Mage_Sales_Model_Order_Creditmemo_Item) { $_items = $item->getCreditmemo()->getAllItems(); } } } if ($_items) { foreach ($_items as $_item) { $parentItem = $_item->getOrderItem()->getParentItem(); if ($parentItem) { $_itemsArray[$parentItem->getId()][$_item->getOrderItemId()] = $_item; } else { $_itemsArray[$_item->getOrderItem()->getId()][$_item->getOrderItemId()] = $_item; } } } if (isset($_itemsArray[$item->getOrderItem()->getId()])) { return $_itemsArray[$item->getOrderItem()->getId()]; } else { return null; } }
public function issue_creditmemo_refund(Varien_Object $payment) { $refund = Mage::getStoreConfig('payment/twocheckout/refund'); if ($refund == '1') { $order = $payment->getCreditmemo()->getOrder(); $creditmemo = $payment->getCreditmemo()->getOrder()->getData(); $creditmemo_amount = $payment->getCreditmemo()->getData(); $creditmemo_comment = $payment->getCreditmemo()->getCommentsCollection()->toArray(); if (isset($creditmemo_comment['items'][0]['comment'])) { $comment = $creditmemo_comment['items'][0]['comment']; } else { $comment = 'Refund issued by seller'; } $username = Mage::getStoreConfig('payment/twocheckout/username'); $password = Mage::getStoreConfig('payment/twocheckout/password'); $sandbox = Mage::getStoreConfig('payment/twocheckout/demo'); Twocheckout::username($username); Twocheckout::password($password); if ($sandbox == "1") { Twocheckout::sandbox(true); } else { Twocheckout::sandbox(false); } $data = array(); $data['invoice_id'] = $creditmemo['ext_order_id']; $data['comment'] = $comment; $data['category'] = '5'; $data['amount'] = $creditmemo_amount['grand_total']; $data['currency'] = 'vendor'; try { $response = Twocheckout_Sale::refund($data); $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save(); $order->addStatusHistoryComment($response["response_message"]); $order->save(); } catch (Twocheckout_Error $e) { Mage::throwException(Mage::helper('core')->__($e->getMessage())); } } }
public function issue_creditmemo_refund(Varien_Object $payment) { $refund = Mage::getStoreConfig('payment/Chinmay/refund'); if ($refund == '1') { $order = $payment->getCreditmemo()->getOrder(); $creditmemo = $payment->getCreditmemo()->getOrder()->getData(); $creditmemo_amount = $payment->getCreditmemo()->getData(); $creditmemo_comment = $payment->getCreditmemo()->getCommentsCollection()->toArray(); if (isset($creditmemo_comment['items'][0]['comment'])) { $comment = $creditmemo_comment['items'][0]['comment']; } else { $comment = 'Refund issued by seller'; } $username = Mage::getStoreConfig('payment/Chinmay/username'); $password = Mage::getStoreConfig('payment/Chinmay/password'); $auth = 'Basic ' . base64_encode($username . ':' . $password); $data = array(); $data['sale_id'] = $creditmemo['ext_order_id']; $data['comment'] = $comment; $data['category'] = '5'; $data['amount'] = $creditmemo_amount['grand_total']; $data['currency'] = 'vendor'; $headers = array('Authorization: ' . $auth, 'Accept: application/json'); $url = 'https://www.2checkout.com/api/sales/refund_invoice'; $config = array('timeout' => 30); try { $http = new Varien_Http_Adapter_Curl(); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $url, '1.1', $headers, $data); $response = $http->read(); $order->addStatusHistoryComment($response); $order->save(); } catch (Exception $e) { Mage::throwException(Mage::helper('core')->__($e->getMessage())); } } }
/** * @param Varien_Object $payment * @param float $amount * * @return $this|Mage_Payment_Model_Abstract */ public function refund(Varien_Object $payment, $amount) { //RY - fix for shop currency transactions being refunded in base currency amount $transactionCurrencyMode = (string) $this->getConfigData('trncurrency'); if ($transactionCurrencyMode == 'store' or $transactionCurrencyMode == 'switcher') { $creditmemo = Mage::registry('current_creditmemo'); $amount = (string) Mage::app()->getStore()->roundPrice($creditmemo->getGrandTotal()); } $order = $payment->getOrder(); $trn = $this->getTransactionDetails($order->getId()); if (!$trn->getId()) { $msg = $this->_getCoreHelper()->__('Transaction does not exist, order id -> %s', $order->getId()); self::log($msg); Mage::throwException($msg); } /** * If we are REFUNDing a FORM transaction we HAVE to retrieve the * SecurityKey from the "Admin & Access API" */ if (!$trn->getSecurityKey() && strtoupper($trn->getIntegration()) == 'FORM') { $this->_addSecurityKey($trn); } $paymentId = Mage::app()->getRequest()->getParam('sagepaysuiterefundtrn'); if (!is_null($paymentId)) { $action = Mage::getModel('sagepaysuite2/sagepaysuite_action')->load((int) $paymentId); if ($action->getVpsTxId() && $action->getSecurityKey() && $action->getTxAuthNo()) { $trn->setVpsTxId($action->getVpsTxId())->setSecurityKey($action->getSecurityKey())->setTxAuthNo($action->getTxAuthNo())->setVendorTxCode($action->getVendorTxCode()); } } //@TODO: If it is PAYPAL dont VOID //VOID payment if refunding full amount if ($this->floatsEqual($order->getGrandTotal(), $payment->getCreditmemo()->getGrandTotal())) { try { $this->voidPayment($trn); return $this; } catch (Exception $e) { Sage_Log::logException($e); } } $data = array(); $data['VPSProtocol'] = $trn->getVpsProtocol(); $data['TxType'] = self::REQUEST_TYPE_REFUND; $data['ReferrerID'] = $this->getConfigData('referrer_id'); $data['Vendor'] = $trn->getVendorname(); $data['VendorTxCode'] = substr(time(), 0, 30) . substr($trn->getVendorTxCode(), 0, 10); $data['Amount'] = $this->formatAmount($amount, $trn->getTrnCurrency()); $data['Currency'] = $trn->getTrnCurrency(); $data['Description'] = '.'; $data['RelatedVPSTxId'] = $trn->getVpsTxId(); $data['RelatedVendorTxCode'] = $trn->getVendorTxCode(); $data['RelatedSecurityKey'] = $trn->getSecurityKey(); $data['RelatedTxAuthNo'] = $trn->getTxAuthNo(); //$data['RelatedSecurityKey'] = (isset($formSecKey) ? $formSecKey : $trn->getSecurityKey()); if (strtoupper($trn->getTxType()) == self::REQUEST_TYPE_AUTHENTICATE && is_null($paymentId)) { $lastAuthorise = Mage::getModel('sagepaysuite2/sagepaysuite_action')->getLastAuthorise($order->getId()); if (is_null($lastAuthorise->getId())) { Mage::throwException('AUTHORISE transaction not found. Refund online cannot be completed.'); } $data['RelatedVPSTxId'] = $lastAuthorise->getVpsTxId(); $data['RelatedVendorTxCode'] = $lastAuthorise->getVendorTxCode(); $data['RelatedSecurityKey'] = $lastAuthorise->getSecurityKey(); $data['RelatedTxAuthNo'] = $lastAuthorise->getTxAuthNo(); } $result = $this->requestPost($this->getUrl('refund', false, $this->_getIntegrationCode($trn->getIntegration()), $trn->getMode()), $data); if ($result['Status'] != 'OK') { Sage_log::log($result['StatusDetail']); Mage::throwException(Mage::helper('sagepaysuite')->__($result['StatusDetail'])); } //For saving purposes $result['Amount'] = $data['Amount']; $this->saveAction($order->getId(), $data, $result); return $this; }
/** * Refund money * * @param Varien_Object $payment * @param float $amount * @return Mage_Payment_Model_Abstract */ public function refund(Varien_Object $payment, $amount) { //parent::refund($payment, $amount); $captureTxnId = $payment->getParentTransactionId(); if ($captureTxnId) { $order = $payment->getOrder(); $request = $this->_getApiRequest(); $request->setData('transaction_id', $captureTxnId)->setData('amount', $amount)->setData('currency_code', $order->getBaseCurrencyCode())->setData('cc_number', $payment->getCcLast4()); $canRefundMore = $order->canCreditmemo(); $allRefunds = (double) $amount + (double) $order->getBaseTotalOnlineRefunded() + (double) $order->getBaseTotalOfflineRefunded(); $isFullRefund = !$canRefundMore && 0.0001 > (double) $order->getBaseGrandTotal() - $allRefunds; $request->setData('is_full_refund', (int) $isFullRefund); // whether to close capture transaction $invoiceCanRefundMore = $payment->getCreditmemo()->getInvoice()->canRefund(); $payment->setShouldCloseParentTransaction($invoiceCanRefundMore ? 0 : 1); $payment->setIsTransactionClosed(1); $api = $this->_getApi()->doRefund($request); $this->_importResultToPayment($payment, $api->getResponse()); return $api->getResponse(); } else { Mage::throwException(Mage::helper('enterprise_pbridge')->__('Impossible to issue a refund transaction, because capture transaction does not exist.')); } }
/** * Refund specified amount for payment * * @param \Varien_Object $payment * @param float $amount * * @return $this * @throws \Mage_Core_Exception */ public function refund(Varien_Object $payment, $amount) { try { // Attempt to load the invoice /* @var $invoice Mage_Sales_Model_Order_Invoice */ $invoice = $payment->getCreditmemo()->getInvoice(); if (!$invoice) { Mage::throwException('Unable to load invoice from credit memo.'); } // Init the environment $this->_getWrapper()->init($payment->getOrder()->getStoreId()); // Convert the refund amount $refundAmount = $this->_getWrapper()->getCaptureAmount($payment->getOrder(), $amount); // Retrieve the transaction ID $transactionId = $this->_getWrapper()->getCleanTransactionId($invoice->getTransactionId()); // Load the transaction from Braintree $transaction = Braintree_Transaction::find($transactionId); // If the transaction hasn't yet settled we can't do partial refunds if ($transaction->status === Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { // If we're doing a partial refund and it's not settled it's a no go if ($transaction->amount != $refundAmount) { Mage::throwException($this->_getHelper()->__('This transaction has not yet settled, please wait until the transaction has settled to process a partial refund.')); } } // Swap between refund and void $result = $transaction->status === Braintree_Transaction::SETTLED || $transaction->status == Braintree_Transaction::SETTLING || isset($transaction->paypal) && isset($transaction->paypal['paymentId']) && !empty($transaction->paypal['paymentId']) ? Braintree_Transaction::refund($transactionId, $refundAmount) : Braintree_Transaction::void($transactionId); // If it's a success close the transaction if ($result->success) { // Pass over the transaction ID $payment->getCreditmemo()->setRefundTransactionId($result->transaction->id); // Only close the transaction once the if ($transaction->amount == $refundAmount) { $payment->setIsTransactionClosed(1); // Mark the invoice as canceled if the invoice was completely refunded $invoice->setState(Mage_Sales_Model_Order_Invoice::STATE_CANCELED); } } else { if ($result->errors->deepSize() > 0) { Mage::throwException($this->_getWrapper()->parseErrors($result->errors->deepAll())); } else { Mage::throwException('An unknown error has occurred whilst trying to process the transaction'); } } } catch (Exception $e) { Mage::throwException($this->_getHelper()->__('An error occurred whilst trying to process the refund: ') . $e->getMessage()); } return $this; }