Improved configuration of the VirtueMart 1.1 Paypal payment module - Joomla & VirtueMart Tips & How To's

GJC Web Design UK Joomla & VirtueMart how to, tips, tricks, coding examples, html, php, tutorial, 101, useful hints, help

A collection of help articles and coding examples for Joomla & Virtuemart

Belgium, VirtueMart & Joomla Web Design England UK Britain & Germany

Joomla, VirtueMart, how to, tips, tricks, coding examples, html, php, tutorial, 101, useful hints, help How to configure the VirtueMart 1.1. payment module to be much more customer friendly.

Home Joomla - VirtueMart Joomla & VM How To's Improved configuration of the VirtueMart 1.1 Paypal payment module

Tue

28

Apr

Improved configuration of the VirtueMart 1.1 Paypal payment module
  • Joomla 1.0 + 1.5
  • VirtueMart 1.1
  • PayPal payment module

The configuration for the Paypal payment module shipped with VirtueMart 1.1 is extremely basic, providing very little information for the customer and even misleading information e.g. no matter how many products the customer buys the Paypal interface shows Quantity : 1.

With a few changes the Paypal payment method can be much more informative.

Update 01.01.2010:
Seems the Paypal API has changed since this was written. The country variable is changed from "address_country" to "country".

Also as thehoffr pointed out below the logic for the shipping/billing address doesn't work and in fact is unnecessary.
The shipping address, if used is always added after the billing address in the vm_order_user_info table so if present will be used.
Have commented out the superfluous section and tested extensively. If it causes any problems please leave a comment.

Have also noticed that Paypal puts the country code in front of the telephone number from the Paypal account holders country (in my case +32 for Belgium) unless the number is formatted correctly i.e. +44 1234 567 for GB etc.
The Paypal API states "The three-digit prefix for U.S. phone numbers, or the entire phone number for phone numbers outside the U.S., excluding country code. This will pre-populate the payer’s home phone number." so it should work but doesn't.
This won't affect you if your Vendor Paypal account is in the same country as your customers, otherwise if you don't want that info passed comment out that line.

Changes made below and tested working 01.01.2010.
Update 23.08.2010:
Changes by Matteo Beretta added, thanks

With these changes we can pass and display the following information to Paypal

  • Order Number
  • SKU Number
  • Product Name
  • Product Attributes
  • Quantity of individual products
  • Unit price of individual products
  • Total number of items
  • Product subtotals
VirtueMart Paypal Module Open your Paypal payment module configuration VirtueMart-->Store-->List Payment Methods.
Choose the Paypal module and when it opens the "Configuration" Tab.
Scroll down to the "Payment Extra Info:" text field.

For safety copy/paste the existing code somewhere safe in case any thing goes wrong with your updating. If so you can simply paste the old code straight back in.

The code basically gets various parameters from your database, manipulates some of them and then passes them to Paypal where they are displayed in the Paypal payment dialogue.

In the updated code we will make a couple of new database queries and pass this information to Paypal.
It is possible just to copy and paste all this code directly into the "Payment Extra Info:" text field, the only adjustment needed will be the image at the end of the code, setting this to your own language/country.
<?php
 
$url = "https://www.paypal.com/cgi-bin/webscr";
 
$order_id = $db->f("order_id");
 
$tax_total = $db->f("order_tax") + $db->f("order_shipping_tax");
 
$discount_total = $db->f("coupon_discount") + $db->f("order_discount");  
 
 
 
// Query for Order Items
 
$dboi = new ps_DB;
 
$q_oi = "SELECT * FROM #__vm_order_item ";
 
$q_oi .= "WHERE #__vm_order_item.order_id='$order_id'";
 
$dboi->query($q_oi);
 
 
 
$row_num = $dboi->num_rows();
 
 
 
//Getting Cart Items
 
$auth = $_SESSION['auth'];
 
$cart = $_SESSION['cart'];
 
$t_quantity = 0;
 
$disc_perItem = 0;
 
$i=1;
 
 
 
// Query to get User Info
 
$dbb = new ps_DB;
 
$q = "SELECT * FROM #__vm_user_info ";
 
$q .= "WHERE user_id ='".$my->id."' ";
 
$dbb->setQuery($q);
 
$dbb->query();
 
 
 
//logic for applying discounts to multiple items
 
$discount_totalCP = $db->f("coupon_discount") + $db->f("order_discount");
 
 
 
while($dboi->next_record()) {
 
  $t_quantity = $t_quantity + intval($dboi->f("product_quantity"));
 
}
 
 
 
$dboi = null;
 
$dboi = new ps_DB;
 
$dboi->query($q_oi);
 
 
 
if($t_quantity > 0)
 
{
 
  if($discount_totalCP > 0) {
 
    $disc_perItem = round($discount_totalCP / $t_quantity, 2);
 
  }
 
  else {
 
    $disc_perItem = 0;
 
  }
 
}
 
else {
 
  $disc_perItem = 0;
 
}
 
//query to optain product attributes
 
while($dboi->next_record()) {
 
 
 
  $prod_attrib = $dboi->f("product_attribute");
 
  $supp_var['item_name_' . $i] = strip_tags($VM_LANG->_('PHPSHOP_ORDER_LIST_ID') . " " . $db->f("order_id").": ". $dboi->f("order_item_name"));
 
 
 
  if($prod_attrib > ''){
 
    $attributes_array = explode('
',$prod_attrib);
 
    $v = 0;
 
    $z = 1;
 
    foreach ( $attributes_array as $attributes_value){
 
      $attrib_name = trim(substr($attributes_value, 0, strpos($attributes_value, ':')), " ");
 
      $supp_var['on' . $z . '_' . $i] = $attrib_name;
 
      $attrib_sel = trim(substr_replace(substr_replace($attributes_value, "", 0, strrpos($attributes_value, ':')), "", 0, 2));
 
      $supp_var['os' . $z . '_' . $i] = $attrib_sel;
 
      $v++;
 
      $z++;
 
      unset($attributes_value);
 
    }
 
  }
 
 
 
  $supp_var['item_number_' . $i] = $dboi->f("order_item_sku");
 
  $supp_var['quantity_' . $i] = $dboi->f("product_quantity");
 
  $supp_var['amount_' . $i] = round(($dboi->f("product_item_price") - $disc_perItem),2);
 
  $i++;
 
}
 
 
 
 
 
//Query used to find whether to use Bill Address or Ship to address
 
$dboui = new ps_DB;
 
$q_oui = "SELECT * FROM #__vm_order_user_info ";
 
$q_oui .= "WHERE #__vm_order_user_info.order_id='$order_id' ORDER BY #__vm_order_user_info.order_info_id DESC";
 
$dboui->query($q_oui);
 
 
 
 
 
  $first_name = $dboui->f("first_name");
 
  $last_name = $dboui->f("last_name");
 
  $address1 = $dboui->f("address_1");
 
  $address2 = $dboui->f("address_2");
 
  $city = $dboui->f("city");
 
  $state = $dboui->f("state");
 
  $address_country = $dboui->f("country_2_code");
 
  $zip = $dboui->f("zip");
 
  $H_PhoneNumber = $dboui->f("phone_1");
 
 
 
 
 
// Builds array for the form
 
$post_variables = Array(
 
"charset" => "utf8",
 
"cmd" => "_cart",
 
"upload" => "1",
 
"page_style" => "paypal",
 
"business" => PAYPAL_EMAIL,
 
"currency_code" => $_SESSION['vendor_currency'],
 
"amount" => round( $db->f("order_subtotal")+$tax_total-$discount_total, 2),
 
"handling_cart" => sprintf("%.2f", $db->f("order_shipping")),
 
"tax" => $tax_total,
 
"tax_cart" => $tax_total,
 
"invoice" => $db->f("order_number"),
 
"image_url" => $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",
 
"no_shipping" => "1",
 
"no_note" => "1",
 
"email" => $dbb->f("user_email"),
 
"address_override" => "1",//change this to 0 if you have - Paypal does not allow your country of residence to ship to the country you wish to - errors
 
"first_name" => $first_name,
 
"last_name" => $last_name,
 
"address1" => $address1,
 
"address2" => $address2,
 
"city" => $city,
 
"state" => $state,
 
"country" => $address_country,
 
"zip" => $zip,
 
"night_phone_b" => $H_PhoneNumber
 
);
 
//add and send the new variables
 
if( $page == "checkout.thankyou" ) {
 
  $query_string = "?";
 
 
 
  foreach( $post_variables as $name => $value ) {
 
    $query_string .= $name. "=" . urlencode($value) ."&";
 
  }
 
 
 
  if(is_array($supp_var) && count($supp_var)) {
 
    foreach($supp_var as $name => $value) {
 
      $query_string .= $name. "=" . urlencode($value) ."&";
 
    }
 
  }
 
 
 
  vmRedirect( $url . $query_string );
 
} 
 
else {
 
  echo '<form action="'.$url.'" method="post" target="_blank">';
 
 
 
  foreach( $post_variables as $name => $value ) {
 
    echo '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
 
  }
 
 
 
  if(is_array($supp_var) && count($supp_var)) {
 
    foreach($supp_var as $name => $value) {
 
      echo '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
 
    }
 
  }
 
  echo '<input type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but6.gif" border="0" alt="Make payments with PayPal, it is fast, free, and secure!">';
 
//Change the above image url for different languages and countries
 
  echo '</form>';
 
}
 
?>
 
The result is a huge improvement on the original VirtueMart core distribution of the Paypal payment module. (see below)
VirtueMart Paypal Module core
Thanks to the all the contributors at the VirtueMart Forums for sorting all this.
Last Updated on Monday, 06 September 2010 09:01
 

Latest Articles

VirtueMart
Your Cart is currently empty.

Paypal Donation

If you use this module on your site please donate a small amount.
Any donation amount appreciated.

Latest Joomla & VirtueMart Tips

See all tips & examples

VM Live Product Search

GJC Web Design UK Joomla & VirtueMart how to, tips, tricks, coding examples, html, php, tutorial, 101, useful hints, help

A collection of help articles and coding examples for Joomla & Virtuemart

Belgium, VirtueMart & Joomla Web Design England UK Britain & Germany

Joomla, VirtueMart, how to, tips, tricks, coding examples, html, php, tutorial, 101, useful hints, help



Improved configuration of the VirtueMart 1.1 Paypal payment module|Joomla & VirtueMart Tips & How To's



How to configure the VirtueMart 1.1. payment module to be much more customer friendly.


order, paypal, address, country, dboi, supp, var, query, value, dboui, info, attributes, code, quantity, payment, discount, number, user, product, tax, total, attrib, virtuemart, item, cart, array, phone, peritem, foreach, url, disc, echo, shipping, module, city, variables, state, items, image, zip


guru

We use cookies to improve our website and your experience when using it. Cookies used for the essential operation of the site have already been set.
To find out more about the cookies we use and how to delete them, see our privacy policy.

I accept cookies from this site.