Tweak PayPal Standard for Non-Supported Base Currency

Last modified by rexwal on Fri, May 6, 2011 20:50
Source|Old Revisions  

This is an old revision of the document!


The Issue

As of July 2009, PayPal supports these currencies.

  • Canadian Dollars
  • Euros
  • British Pounds
  • U.S. Dollars
  • Yen
  • Australian Dollars
  • New Zealand Dollars
  • Swiss Francs
  • Hong Kong Dollars
  • Singapore Dollars
  • Swedish Kroner
  • Danish Kroner
  • Polish Zloty
  • Norwegian Kroner
  • Hungarian Forints
  • Czech Koruny
  • Israeli Shekels
  • Mexican Pesos

If you have set your base currency to one not listed above and have enable the PayPal Standard in Admin>System>Configuration, PayPal Standard payment method will not be listed in the onepage checkout after version 1.3.0.

The tweak describe here enable you to get around this issue by changing two core files:

  1. app/code/core/Mage/Payment/Block/Form/Container.php
  2. app/code/core/Mage/Paypal/Model/Standard.php

Download

To make things easy, you can download the zip file. Unzip the file in app/code/local/ so that Magento updates will not overwrite the files. You should now have

  1. app/code/local/Mage/Payment/Block/Form/Container.php
  2. app/code/local/Mage/Paypal/Model/Standard.php

Setup PayPal

To setup PayPal Standard with IPN, refer this wiki Paypal Standard Payments Setup with IPN

To configure PayPal in Magento:

  1. Setup the allowed currencies in Admin>System>Configuration>Currency Setup
  2. Setup PayPal Accounts in Admin>System>Configuration>PayPal Accounts pp_setup.jpg
  3. Configure Payment Methods in Admin>System>Configuration>Payment Methods pp_config.jpg

Modify Container.php

Locate the file app/code/core/mage/payment/block/form/Container.php Find function _canUseMethod, line 54 and change to this:

  1.     protected function _canUseMethod($method)
  2.     {
  3.         if (!$method->canUseForCountry($this->getQuote()->getBillingAddress()->getCountry())) {
  4.             return false;
  5.         }
  6.  
  7.         //if (!$method->canUseForCurrency(Mage::app()->getStore()->getBaseCurrencyCode())) {
  8.         if (!$method->canUseForCurrency(Mage::app()->getStore()->getCurrentCurrencyCode())) {
  9.             return false;
  10.         }
  11.  
  12.         /**
  13.          * Checking for min/max order total for assigned payment method
  14.          */
  15.         $total = $this->getQuote()->getBaseGrandTotal();
  16.         $minTotal = $method->getConfigData('min_order_total');
  17.         $maxTotal = $method->getConfigData('max_order_total');
  18.  
  19.         if((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
  20.             return false;
  21.         }
  22.         return true;
  23.     }

Modify Standard.php

Locate app/code/core/mage/paypal/model/Standard.php. Find function validate, line 127 and edit:

  1. public function validate()
  2.     {
  3.         parent::validate();
  4.         //$currency_code = $this->getQuote()->getBaseCurrencyCode();
  5.         $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
  6.         if (!in_array($currency_code,$this->_allowCurrencyCode)) {
  7.             Mage::throwException(Mage::helper('paypal')->__('Selected currency code ('.$currency_code.') is not compatible with PayPal'));
  8.         }
  9.         return $this;
  10.     }

Find function getStandardCheckoutFormFields, line 158 and edit:

  1.     public function getStandardCheckoutFormFields()
  2.     {
  3.         if ($this->getQuote()->getIsVirtual()) {
  4.             $a = $this->getQuote()->getBillingAddress();
  5.             $b = $this->getQuote()->getShippingAddress();
  6.         } else {
  7.             $a = $this->getQuote()->getShippingAddress();
  8.             $b = $this->getQuote()->getBillingAddress();
  9.         }
  10.         /*tweak version
  11.           init currency conversion
  12.           if currency is not the same as base currency, set convert boolean to true 
  13.         */
  14.         $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
  15.  
  16.  
  17.         $storeCurrency = Mage::getSingleton('directory/currency')
  18.                 ->load($this->getQuote()->getStoreCurrencyCode());
  19.        
  20.         $bConvert = $currency_code != $this->getQuote()->getBaseCurrencyCode();
  21.  
  22.  
  23.  
  24.         $sArr = array(
  25.             'charset'           => self::DATA_CHARSET,
  26.             'business'          => Mage::getStoreConfig('paypal/wps/business_account'),
  27.             'return'            => Mage::getUrl('paypal/standard/success',array('_secure' => true)),
  28.             'cancel_return'     => Mage::getUrl('paypal/standard/cancel',array('_secure' => false)),
  29.             'notify_url'        => Mage::getUrl('paypal/standard/ipn'),
  30.             'invoice'           => $this->getCheckout()->getLastRealOrderId(),
  31.             'currency_code'     => $currency_code,
  32.             'address_override'  => 1,
  33.             'first_name'        => $a->getFirstname(),
  34.             'last_name'         => $a->getLastname(),
  35.             'address1'          => $a->getStreet(1),
  36.             'address2'          => $a->getStreet(2),
  37.             'city'              => $a->getCity(),
  38.             'state'             => $a->getRegionCode(),
  39.             'country'           => $a->getCountry(),
  40.             'zip'               => $a->getPostcode(),
  41.             'bn'                => 'Varien_Cart_WPS_US'
  42.         );
  43.  
  44.         $logoUrl = Mage::getStoreConfig('paypal/wps/logo_url');
  45.         if($logoUrl){
  46.              $sArr = array_merge($sArr, array(
  47.                   'cpp_header_image' => $logoUrl
  48.              ));
  49.         }
  50.  
  51.         if($this->getConfigData('payment_action')==self::PAYMENT_TYPE_AUTH){
  52.              $sArr = array_merge($sArr, array(
  53.                   'paymentaction' => 'authorization'
  54.              ));
  55.         }
  56.  
  57.         $transaciton_type = $this->getConfigData('transaction_type');
  58.         /*
  59.         O=aggregate cart amount to paypal
  60.         I=individual items to paypal
  61.         */
  62.         if ($transaciton_type=='O') {
  63.             $businessName = Mage::getStoreConfig('paypal/wps/business_name');
  64.             $storeName = Mage::getStoreConfig('store/system/name');
  65.             $amount = ($a->getBaseSubtotal()+$b->getBaseSubtotal())-($a->getBaseDiscountAmount()+$b->getBaseDiscountAmount());
  66.             //convert the amount to the current currency
  67.             if ($bConvert) {
  68.                 $amount = $storeCurrency->convert($amount, $currency_code);
  69.             }
  70.             $sArr = array_merge($sArr, array(
  71.                     'cmd'           => '_ext-enter',
  72.                     'redirect_cmd'  => '_xclick',
  73.                     'item_name'     => $businessName ? $businessName : $storeName,
  74.                     'amount'        => sprintf('%.2f', $amount),
  75.                 ));
  76.             $_shippingTax = $this->getQuote()->getShippingAddress()->getBaseTaxAmount();
  77.             $_billingTax = $this->getQuote()->getBillingAddress()->getBaseTaxAmount();
  78.             $tax = $_shippingTax + $_billingTax;
  79.             //convert the amount to the current currency
  80.             if ($bConvert) {
  81.                 $tax = $storeCurrency->convert($tax, $currency_code);
  82.             }
  83.             $tax = sprintf('%.2f', $tax);
  84.             if ($tax>0) {
  85.                   $sArr = array_merge($sArr, array(
  86.                         'tax' => $tax
  87.                   ));
  88.             }
  89.  
  90.         } else {
  91.             $sArr = array_merge($sArr, array(
  92.                 'cmd'       => '_cart',
  93.                 'upload'       => '1',
  94.             ));
  95.             $items = $this->getQuote()->getAllItems();
  96.             if ($items) {
  97.                 $i = 1;
  98.                 foreach($items as $item){
  99.                     if ($item->getParentItem()) {
  100.                         continue;
  101.                     }
  102.                     //echo "<pre>"; print_r($item->getData()); echo"</pre>";
  103.                     $amount = ($item->getBaseCalculationPrice() - $item->getBaseDiscountAmount());
  104.                     if ($bConvert) {
  105.                         $amount = $storeCurrency->convert($amount, $currency_code);
  106.                     }
  107.                     $sArr = array_merge($sArr, array(
  108.                         'item_name_'.$i      => $item->getName(),
  109.                         'item_number_'.$i      => $item->getSku(),
  110.                         'quantity_'.$i      => $item->getQty(),
  111.                         'amount_'.$i      => sprintf('%.2f', $amount),
  112.                     ));
  113.                     $tax = $item->getBaseTaxAmount();
  114.                     if($tax>0){
  115.                         //convert the amount to the current currency
  116.                         if ($bConvert) {
  117.                             $tax = $storeCurrency->convert($tax, $currency_code);
  118.                         }
  119.                         $sArr = array_merge($sArr, array(
  120.                         'tax_'.$i      => sprintf('%.2f',$tax/$item->getQty()),
  121.                         ));
  122.                     }
  123.                     $i++;
  124.                 }
  125.            }
  126.         }
  127.  
  128.         $totalArr = $a->getTotals();
  129.         $shipping = $this->getQuote()->getShippingAddress()->getBaseShippingAmount();
  130.         if ($shipping>0 && !$this->getQuote()->getIsVirtual()) {
  131.           //convert the amount to the current currency
  132.           if ($bConvert) {
  133.               $shipping = $storeCurrency->convert($shipping, $currency_code);
  134.           }
  135.           $shipping = sprintf('%.2f', $shipping);
  136.          
  137.           if ($transaciton_type=='O') {
  138.               $sArr = array_merge($sArr, array(
  139.                     'shipping' => $shipping
  140.               ));
  141.           } else {
  142.               $shippingTax = $this->getQuote()->getShippingAddress()->getBaseShippingTaxAmount();
  143.               //convert the amount to the current currency
  144.               if ($bConvert) {
  145.                   $shippingTax = $storeCurrency->convert($shippingTax, $currency_code);
  146.               }
  147.               $sArr = array_merge($sArr, array(
  148.                     'item_name_'.$i   => $totalArr['shipping']->getTitle(),
  149.                     'quantity_'.$i    => 1,
  150.                     'amount_'.$i      => sprintf('%.2f',$shipping),
  151.                     'tax_'.$i         => sprintf('%.2f',$shippingTax),
  152.               ));
  153.               $i++;
  154.           }
  155.         }
  156.  
  157.         $sReq = '';
  158.         $sReqDebug = '';
  159.         $rArr = array();
  160.  
  161.  
  162.         foreach ($sArr as $k=>$v) {
  163.             /*
  164.             replacing & char with and. otherwise it will break the post
  165.             */
  166.             $valuestr_replace("&","and",$v);
  167.             $rArr[$k]$value;
  168.             $sReq .= '&'.$k.'='.$value;
  169.             $sReqDebug .= '&'.$k.'=';
  170.             if (in_array($k, $this->_debugReplacePrivateDataKeys)) {
  171.                 $sReqDebug .= '***';
  172.             } else  {
  173.                 $sReqDebug .= $value;
  174.             }
  175.            
  176.         }
  177.  
  178.         if ($this->getDebug() && $sReq) {
  179.             $sReq = substr($sReq, 1);
  180.             $debug = Mage::getModel('paypal/api_debug')
  181.                     ->setApiEndpoint($this->getPaypalUrl())
  182.                     ->setRequestBody($sReq)
  183.                     ->save();
  184.         }
  185.         return $rArr;
  186.     }

Alternative Solution

Create the file Config.php in /app/code/local/Mage/Paypal/Model/Config.php

Step 1

Copy the file from /app/code/core/Mage/Paypal/Model/Config.php to /app/code/local/Mage/Paypal/Model/Config.php

Step 2

Look for the $_supportedCurrencyCodes

protected $_supportedCurrencyCodes = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN',
        'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD', 'TWD', 'THB');

and add your currency that needs supporting to the end of the array

protected $_supportedCurrencyCodes = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN',
        'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD', 'TWD', 'THB', 'PHP');



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs