News:

Support the VirtueMart project and become a member

Main Menu

Currency conflicts with 2checkout

Started by Ruppert, September 28, 2008, 02:01:43 AM

Previous topic - Next topic

Ruppert


If you're having problems with 2Checkout's default currency then read on.

In 2CO, you have to set a default currency. The orders sent by VM to 2CO must therefore be in that 2CO's default currency, otherwise 2CO interprets them as being in that currency no matter what you submit your order in.

The Paypal module solves this by sending a currency code with the order:

"currency_code" => $_SESSION['vendor_currency'],

2CO doesn't support currency codes, so here's how to fix this:

1/ Go to VM / Store / List Payment Methods / 2Checkout / Configuration / Payment Extra Info

2/ Replace

$formdata['x_amount'] = number_format($db->f("order_total"), 2, '.', '');

with :


    //Convert the currency
    $my_2co_default_currency = 'USD';
    $order_total = $db->f("order_total");
    if ($db->f('order_currency') != $my_2co_default_currency ) {
      $order_total = $GLOBALS['CURRENCY']->convert( $db->f("order_total") , $db->f('order_currency'), $my_2co_default_currency );
    }
    $formdata['x_amount'] = number_format($order_total, 2, '.', '');


3/ Replace the value of $my_2co_default_currency with that set in 2CO Admin. In my case, I set it to 'USD' because it's set to US Dollar.


Here is a copy of my complete 2Checkout file which also fixes the redirecting issue and the non-forwarded billing address:


<?php

       
// Get ship_to information
    
if( $db->f("user_info_id") != $dbbt->f("user_info_id")) {
         
$q2  "SELECT * FROM #__vm_user_info WHERE user_info_id='".$db->f("user_info_id")."'"
    
$dbst = new ps_DB;
   
$dbst->setQuery($q2);
       
$dbst->query();
      
$dbst->next_record();
      }
     else  {
         
$dbst $dbbt;
    } 
              
      
//Authnet vars to send
        
$formdata = array (
   
'x_login' => TWOCO_LOGIN,
   
'x_email_merchant' => ((TWOCO_MERCHANT_EMAIL == 'True') ? 'TRUE' 'FALSE'),
                  
      
// Customer Name and Billing Address
  
'x_first_name' => $dbbt->f("first_name"),
        
'x_last_name' => $dbbt->f("last_name"),
  
'x_company' => $dbbt->f("company"),
      
'x_address' => $dbbt->f("address_1"),
    
'x_city' => $dbbt->f("city"),
    
'x_state' => $dbbt->f("state"),
  
'x_zip' => $dbbt->f("zip"),
      
'x_country' => $dbbt->f("country"),
      
'x_phone' => $dbbt->f("phone_1"),
        
'x_fax' => $dbbt->f("fax"),
      
'x_email' => $dbbt->f("email"),
 
       
// Customer Shipping Address
  
'x_ship_to_first_name' => $dbst->f("first_name"),
        
'x_ship_to_last_name' => $dbst->f("last_name"),
  
'x_ship_to_company' => $dbst->f("company"),
      
'x_ship_to_address' => $dbst->f("address_1"),
    
'x_ship_to_city' => $dbst->f("city"),
    
'x_ship_to_state' => $dbst->f("state"),
  
'x_ship_to_zip' => $dbst->f("zip"),
      
'x_ship_to_country' => $dbst->f("country"),
     
       
'x_invoice_num' => $db->f("order_number"),
       
'x_receipt_link_url' => SECUREURL."2checkout_notify.php",
       
'product_description' => $VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER').": "$db->f("order_id") ,
       
'sh_cost' => sprintf("%.2f"$db->f("order_shipping"))
  );
    
     if( 
TWOCO_TESTMODE == "Y" )
   
$formdata['demo'] = "Y";
       
       
$version "2";
    
$url "https://www2.2checkout.com/2co/buyer/purchase";
    
    
//Convert the currency 
    
$my_2co_default_currency 'USD';
    
$order_total $db->f("order_total");
    if (
$db->f('order_currency') != $my_2co_default_currency ) {
      
$order_total $GLOBALS['CURRENCY']->convert$db->f("order_total") , $db->f('order_currency'), $my_2co_default_currency );
    }
    
$formdata['x_amount'] = number_format($order_total2'.''');
       
    
if( 
$page == "checkout.thankyou" ) {
    
$query_string "?";
    foreach( 
$formdata as $name => $value ) {
    
$query_string .= $name"=" urlencode($value) ."&";
    }
    
vmRedirect$url $query_string );
}
else {

//build the post string
       
$poststring '';
       
   foreach(
$formdata AS $key => $val){
          
$poststring .= "<input type='hidden' name='$key' value='$val' />
          "
;
    } 
   
    
//old buy logo: https://www.2checkout.com/images/buy_logo.gif
?>

    <form id="cho" action="<?php echo $url ?>" method="post" target="_blank">
       <?php echo $poststring ?>
    <p><font size=+1>Click on the Image below to pay...</font></p>
     <input type="image" name="submit" src="https://www2.2checkout.com/static/checkout/CheckoutButton2COCards.gif" border="0" alt="Make payments with 2Checkout, it's fast and secure!" title="Pay your Order with 2Checkout, it's fast and secure!" />
      </form>

   <!-- Uncomment if you want to try a javascript redirect no matter what -->
   <!--script language="JavaScript" type="text/javascript">
   document.getElementById('cho').submit();
   </script-->
   
<?php 
}
?>



Let me know if that works for you.

Ruppert.


Ruppert.

________________________
VM 1.1.3
Joomla 1.5.9

jmwaniki

Thanks. This uses the ecb currency converter which does not have many currencies supported.Is it possible to use xe.com converter? What can i change.


Ruppert

If Xe.com has an currency XML feed, then yes.

You'd need to implement your own currency conversion class. You could either update the currency conversion class in VirtueMart or this example class to use the XE.com feed.

But you'll need some programming skills.
Ruppert.

________________________
VM 1.1.3
Joomla 1.5.9

crashtackle

Hi, I used the code above but the conversion rate is way out - by about 28%...

Any ideas on using a more accurate conversion?

Ruppert

Quote from: crashtackle on November 13, 2009, 14:17:58 PM
Hi, I used the code above but the conversion rate is way out - by about 28%...

Any ideas on using a more accurate conversion?

Be aware that 2Checkout.com charges much higher fees for currency conversions than those displayed on www.xe.com for example. Like 10% more sometimes. That might be the reason.

I would advise your customers to pay in USD and let their credit card issuer do the currency conversion, as they will pay a lot less than if 2Checkout does it.

This is a bit of sneaky way for 2Checkout to make more profit off of international customers...



Ruppert.

________________________
VM 1.1.3
Joomla 1.5.9

Ruppert

To fix the difference between the 2Checkout foreign exchange rates and those of the ECB (used by default in virtuemart), then I suggest the following hack:

replace the line in /administrator/components/com_virtuemart/classes/currency/convertECB.php


   $val = $amountA * $valB / $valA;


with


//modification to fix 2CO and ECB currency exchange difference
//compensated in VM / Store / List Payment Methods / 2Checkout / Configuration / Payment Extra Info
$val = ( $amountA * $valB / $valA ) * 1.05;


I have found that a multiplier of 1.05 (i.e. +5%) roughly matches the 2Checkout - probably spam - margin.

(Note that this change will affect all currency conversions in Virtuemart. So if you support several payment systems other than 2Checkout, you will need to apply similar the modifications to them than I am doing below.)

Next, you will also need to modify my previous modification to VM / Store / List Payment Methods / 2Checkout / Configuration / Payment Extra Info


//Convert the currency
    $my_2co_default_currency = 'USD';
    $order_total = $db->f("order_total");
    if ($db->f('order_currency') != $my_2co_default_currency ) {
      $order_total = $GLOBALS['CURRENCY']->convert( $db->f("order_total") , $db->f('order_currency'), $my_2co_default_currency );

      //to compensate for 2CO's high currency xchange rates, values are multiplied by 1.05 when in another currency
      //need to remove that surplus before sending to 2CO a USD value which will have 2CO current xchange multiplier applied
      // error_log("Foreign currency selected, so changing total from  : ". $order_total, 0);
       $order_total = floatval($order_total) * (1/1.05)* (1/1.05); //need to apply it twice as 2 conversions were applied to it: USD->FOREIGN(1.05)->USD(1.05) so need to rectify that twice
      // error_log("to  : ". $order_total, 0);

   }
    $formdata['x_amount'] = number_format($order_total, 2, '.', '');


So basically this code returns the original order total back to its normal value without the additional +5% conversions. It then sends that to 2Checkout, which in turns adds its 5% onto it. If we didn't subtract the value, we'd have the additional +5% margin applied multiple times.

Hope this helps.


Ruppert.

________________________
VM 1.1.3
Joomla 1.5.9

nebojsa

I also have same issue with XE converter.
Your solution doesn't work for me.
My site is live and I cannot accept orders.
Please help ...