Multi-currency store: wrong currency on PayPal checkout

VirtueMart is great when you want to set up a store which works with multiple currencies. You can set up your own prices for each currencies in your shop, or you can use one of available currency converters. The core functionality supports the use of European Central Bank's live currency rates, but there are plugins supporting other rates too.

But you can run into trouble where you'd expect to not have any surprises: on PayPal checkout page.

The basic problem is, that if you have multiple currencies set up - let's say Swiss Franks United States Dollars, you might experiencing something like this: If you confirm order after the checkout without changing currency on the fly, emails display prices into CHF currency (ex: 40 CHF) but Paypal displays the same amount in USD (40 USD).

If you use the currency converter before to confirm order, the PayPal currency is as expected. (CHF in my example). The fix is relatively easy, you don't need to hack anything in fact, yous to replace the default payment extra info in the VirtueMart's Payment Methods interface.

So, go to VirtueMart Payment methods>PayPal>The second tab with Virtue Mart Payment extra info box. CopyPaste the PHP code below to replace what you have there, and you are set.

<?php
$db1 
= new ps_DB();
$q "SELECT country_2_code FROM #__vm_country WHERE country_3_code='".$user->country."' ORDER BY country_2_code ASC";
$db1->query($q);
if(isset($_SESSION['product_currency']) && $_SESSION['product_currency']!=''){
$currency $_SESSION['product_currency'];
}else{
$currency $_SESSION['vendor_currency'];
}
$vendor_currency $_SESSION['vendor_currency'];
if(
$currency!=$vendor_currency){
        
$shipping_temp sprintf("%.2f"$db->f("order_shipping"));
        
$shipping =sprintf("%.2f"$GLOBALS['CURRENCY']->convert$shipping_temp,$currency,$vendor_currency));
}else{
        
$shipping =sprintf("%.2f"$db->f("order_shipping"));
}

$url "https://www.paypal.com/cgi-bin/webscr";
$tax_total $db->f("order_tax") + $db->f("order_shipping_tax");
$discount_total $db->f("coupon_discount") + $db->f("order_discount");
$post_variables = Array(
"cmd" => "_ext-enter",
"redirect_cmd" => "_xclick",
"upload" => "1",
"business" => PAYPAL_EMAIL,
"receiver_email" => PAYPAL_EMAIL,
"item_name" => $VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER').": "$db->f("order_id"),
"order_id" => $db->f("order_id"),
"invoice" => $db->f("order_number"),
"amount" => round$db->f("order_subtotal")+$tax_total-$discount_total2),
"shipping" => $shipping,
"currency_code" => $currency ,
"address_override" => "1",
"first_name" => $dbbt->f('first_name'),
"last_name" => $dbbt->f('last_name'),
"address1" => $dbbt->f('address_1'),
"address2" => $dbbt->f('address_2'),
"zip" => $dbbt->f('zip'),
"city" => $dbbt->f('city'),
"state" => $dbbt->f('state'),
"country" => $db1->f('country_2_code'),
"email" => $dbbt->f('user_email'),
"night_phone_b" => $dbbt->f('phone_1'),
"cpp_header_image" => $vendor_image_url,
"return" => SECUREURL ."index.php?option=com_virtuemart&page=checkout.result&order_id=".$db->f("order_id"),
"notify_url" => SECUREURL ."administrator/components/com_virtuemart/notify.php",
"cancel_return" => SECUREURL ."index.php",
"undefined_quantity" => "0",
"test_ipn" => PAYPAL_DEBUG,
"pal" => "NRUBJXESJTY24",
"no_shipping" => "1",
"no_note" => "1"
);
if( $page == "checkout.thankyou" ) {
$query_string "?";
foreach( 
$post_variables as $name => $value ) {
$query_string .= $name"=" urlencode($value) ."&";
}
vmRedirect$url $query_string );
} else {
echo 
'<form action="'.$url.'" method="post" target="_blank">';
echo 
'<input type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but6.gif" alt="Click to pay with PayPal - it is fast, free and secure!" />';
foreach( $post_variables as $name => $value ) {
echo 
'<input type="hidden" name="'.$name.'" value="'.htmlspecialchars($value).'" />';
}
echo 
'</form>';
}
?>

Originally published on forum.virtuemart.net