将 Magento 中不接受的货币转换为用PayPal进行的美元付款


Convert to USD payments made with PayPal for unaccepted currencies in Magento

概要: 如果您的基础货币不是此处列出的货币,Magento PayPal支付模块不会将货币转换为美元或任何PayPal接受的货币 https://www.paypal.com/us/webapps/helpcenter/helphub/article/?solutionId=FAQ2390

我有一个 Magento 安装,安装了 3 种货币。

我使用PayPal标准付款作为付款方式,但问题是向用户收取相同的金额,但以美元为单位。例如,如果我有 100 罗马尼亚列伊并选择按PayPal付款,我将被收取 100 美元的费用。

我检查了使重定向可用的PayPal模块

应用程序/代码/核心/法师/PayPal/块/标准/重定向.php

并且与货币和金额相关的变量已正确设置并发送到PayPal。

我不知道如何解决这个问题,这非常令人沮丧,因为我想Magento中的标准PayPal模块是由PayPal设计的(需要确认),并针对这种情况进行了验证。

信息#1:PayPal不接受某些货币(例如罗马尼亚列伊,恰好是这家商店的基础货币)进行交易(允许的货币 https://www.paypal.com/us/webapps/helpcenter/helphub/article/?solutionId=FAQ2390 在此处列出),因此当您单击提交时,它们将仅转换货币符号(到$)而不是金额。您必须自己更改的数量,但是Magento(v 1.5)中的默认PayPal模块没有这样做,这就是我打开这个问题的原因。

编辑 #1

我尝试了下面提出的解决方案,但它不起作用,因为实际上它替换了一些变量,但没有以所需的方式。

我在这里看到两个选项:

选项

#1:"查找并替换"选项是我找到最终形式中的所有浮点值并将其替换为转换为美元的值的地方。但是,这不是一个可行的选择,因为没有正确转换值并且可能会发生错误。

选项#2:

我找到了在表单中弹出值的函数,它位于 spp/code/core/Mage/PayPal/Model/Api/Abstract.php

protected function _exportLineItems(array &$request, $i = 0)
    {
        if (!$this->_cart) {
            return;
        }
        // always add cart totals, even if line items are not requested
        if ($this->_lineItemTotalExportMap) {
            foreach ($this->_cart->getTotals() as $key => $total) {
                if (isset($this->_lineItemTotalExportMap[$key])) { // !empty($total)
                    $privateKey = $this->_lineItemTotalExportMap[$key];
                    $request[$privateKey] = $this->_filterAmount($total);
                }
            }
        }
        // add cart line items
        $items = $this->_cart->getItems();
        if (empty($items) || !$this->getIsLineItemsEnabled()) {
            return;
        }
        $result = null;
        foreach ($items as $item) {
            foreach ($this->_lineItemExportItemsFormat as $publicKey => $privateFormat) {
                $result = true;
                $value = $item->getDataUsingMethod($publicKey);
                if (isset($this->_lineItemExportItemsFilters[$publicKey])) {
                    $callback   = $this->_lineItemExportItemsFilters[$publicKey];
                    $value = call_user_func(array($this, $callback), $value);
                }
                if (is_float($value)) {
                    $value = $this->_filterAmount($value);
                }
                $request[sprintf($privateFormat, $i)] = $value;
            }
            $i++;
        }
        return $result;
    }

这 2 行:

$request[$privateKey] = $this->_filterAmount($total);
$value = $this->_filterAmount($value);

打印出变量列表中的金额,以便我编写了以下函数,而不是函数_filterAmount,该函数应根据后端定义的汇率将金额从任何基础货币转换为美元:

protected function _convertAmounttoUSD($value)
    {
        $baseCode = Mage::app()->getBaseCurrencyCode();
        $fromCur = Mage::app()->getStore()->getCurrentCurrencyCode();
        $toCur = 'USD';
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
        $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCode, array_values($allowedCurrencies));
        $output = ( $value * $rates[$toCur] ) / $rates[$fromCur];
        return sprintf('%.2F', $output);
    }

我已将上面的行替换为以下内容:

$request[$privateKey] = $this->_convertAmounttoUSD($total);
$value = $this->_convertAmounttoUSD($value);

问题是值没有被转换。

在 magento 中,当用户被重定向到PayPal它 magento 发送商店货币来收费,但PayPal使用PayPal帐户中关联的货币,因此我们需要将其转换为PayPal帐户货币。

它将收取100罗马尼亚列伊,但将其转换为美元货币。

您需要使用以下功能在以下文件中进行更改:app''code''core''Mage''PayPal''Model''Standard.php将 getStandardCheckoutFormFields 函数替换为以下函数:

public function getStandardCheckoutFormFields()
{
    $orderIncrementId = $this->getCheckout()->getLastRealOrderId();
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    $api = Mage::getModel('paypal/api_standard')->setConfigObject($this->getConfig());
    $api->setOrderId($orderIncrementId)
        ->setCurrencyCode($order->getBaseCurrencyCode())
        //->setPaymentAction()
        ->setOrder($order)
        ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
        ->setReturnUrl(Mage::getUrl('paypal/standard/success'))
        ->setCancelUrl(Mage::getUrl('paypal/standard/cancel'));
    // export address
    $isOrderVirtual = $order->getIsVirtual();
    $address = $isOrderVirtual ? $order->getBillingAddress() : $order->getShippingAddress();
    if ($isOrderVirtual) {
        $api->setNoShipping(true);
    } elseif ($address->validate()) {
        $api->setAddress($address);
    }
    // add cart totals and line items
    $api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
        ->setIsLineItemsEnabled($this->_config->lineItemsEnabled)
    ;
    $api->setCartSummary($this->_getAggregatedCartSummary());

    $result = $api->getStandardCheckoutRequest();
    $baseCode = Mage::app()->getBaseCurrencyCode();
    $fromCur = Mage::app()->getStore()->getCurrentCurrencyCode();
    $toCur = 'USD';
    $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
    $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCode, array_values($allowedCurrencies));
    $result['amount'] = round((($order->getGrandTotal() * $rates[$toCur])/$rates[$fromCur]),2);
    $result['currency_code'] = $toCur;
    $j = 0;
    $items = $order->getAllItems();
    foreach ($items as $itemId => $item)
    {
        if ($item->getParentItem()) {
            continue;
        }
        $j ++;
        $result['amount_'.$j] = round((($item->getPrice() * $rates[$toCur])/$rates[$fromCur]),2);
    }
    $j++;
    $result['country']          = $order->getBillingAddress()->getCountryId();
    $shippingSpo            = $order->getBaseShippingAmount();
    $result['shipping']         = round((($shippingSpo * $rates[$toCur])/$rates[$fromCur]),2);
    $result['discount_amount']  = -1*round((($order->getDiscountAmount() * $rates[$toCur])/$rates[$fromCur]),2);
    $result['discount_amount_cart'] = $result['discount_amount'];
    $result['amount_'.$j] = $result['shipping'];
    unset($result['discount_amount']);
    unset($result['shipping']);
    unset($result['discount_amount_cart']);
    unset($result['amount_'.$j]);
    return $result;
}