Magento - Online refund with PayPal

前提是你 提交于 2019-12-02 10:49:56

问题


Currently we have a Magento ver. 1.8.1.0 with installed PayPal Website Payments Standard option enabled. However when I want to do an online refund it doesn't show an 'Refund' button but only 'Offline refund'.

Is it actually possible to create an online refund with PayPal Standard?


回答1:


Impossible with Paypal standard, you must overload the paypal standard model to implement the refund method, or you can install a module.




回答2:


As luigifab said, that functionality isn't part of the Paypal module - I've implemented a small add-on for it that can be seen here:

https://gist.github.com/bubach/ed86611c634b401e5d66392cf32c2f6e

The most important part being this class:

<?php
class Namespace_Modulename_Model_Paypal extends Mage_Paypal_Model_Standard
{

    protected $_canRefund               = true;
    protected $_canRefundInvoicePartial = true;
    protected $_canVoid                 = true;

    /**
     * https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/RefundTransaction_API_Operation_NVP/
     */
    public function tryRefund(Varien_Object $payment, $amount)
    {
        $transactionId = $payment->getLastTransId();

        if ($transactionId) {
            $order          = $payment->getOrder();
            $storeId        = $order->getStoreId();
            $refundType     = "Partial";
            $invoiceFee = $payment->getMethodInstance()->getInfoInstance()->getAdditionalInformation('invoice_fee');
            $remaining  = $order->getTotalInvoiced() - ($order->getTotalOfflineRefunded() + $order->getTotalOnlineRefunded()) - $invoiceFee;

            if (abs($remaining - $amount) < 0.00001) {
                $refundType = "Full";
            }

            $currencyCode   = $order->getBaseCurrencyCode();
            $invoiceId      = $order->getIncrementId();

            $params = array(
                "METHOD"        => "RefundTransaction",
                "VERSION"       => "72.0",
                "TRANSACTIONID" => $transactionId,
                "INVOICEID"     => $invoiceId,
                "REFUNDTYPE"    => $refundType,
                "AMT"           => $amount,
                "CURRENCYCODE"  => $currencyCode,
                "USER"          => Mage::getStoreConfig('paypal/wpp/api_username', $storeId),
                "PWD"           => Mage::getStoreConfig('paypal/wpp/api_password', $storeId),
                "SIGNATURE"     => Mage::getStoreConfig('paypal/wpp/api_signature', $storeId)
            );

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, "https://api-3t.paypal.com/nvp");
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS,  http_build_query($params));

            $response = curl_exec($ch);

            if (curl_errno($ch)) {
                curl_close($ch);
                throw new Mage_Core_Exception('Impossible to issue a refund transaction because of cURL error.');
            } else  {
                curl_close($ch);

                $responseArray = array();
                parse_str($response, $responseArray); // Break the NVP string to an array

                if ($responseArray['ACK'] == "Success") {
                    return array(0, "Paypal refunded successfully");
                } else {
                    return array(-1, "Paypal refund failed!");
                }
            }
        } else {
            Mage::throwException(Mage::helper('paypal')->__('Impossible to issue a refund transaction because the capture transaction does not exist.'));
        }
    }
}


来源:https://stackoverflow.com/questions/30375154/magento-online-refund-with-paypal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!