Hi there,
I have taken this from another thread (
http://forum.virtuemart.net/index.php?topic=55302.15) and placed it here in hopes of helping other poor souls who are struggling with this payment method. I am not guaranteeing it as a solution, but offering it as a possible solution and/or the basis for making a better solution.
*** Here We Go! ***
The Code to be placed in you 2CheckOut configuration (found in Payment Methods in your VM admin):
<?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;
}
$q3 = "SELECT * FROM #__vm_order_item WHERE user_info_id='".$db->f("user_info_id")."'";
$dbcprod = new ps_DB;
$dbcprod->setQuery($q3);
$dbcprod->query();
$dbcprod->next_record();
//sort out user orders
for($i = 0; $i < count($dbcprod->record); $i++){ $orders[$i] = $dbcprod->record[$i]->order_id; }
arsort($orders);
$this_order_id = current($orders); //sets most recent order number for user
//gets all product info entries for this order number
for($i = 0; $i < count($dbcprod->record); $i++)
{
if($dbcprod->record[$i]->order_id == $this_order_id) { $this_order[$i] = $dbcprod->record[$i]; }
}
sort($this_order);
//2co c_prods to send
//creates 2co c_prod parameters
for($i = 0; $i < count($this_order); $i++)
{
$twoco_prod_info["c_prod_$i"] = $this_order[$i]->order_item_sku .",". $this_order[$i]->product_quantity;
$twoco_prod_info["c_name_$i"] = $this_order[$i]->order_item_name;
$twoco_prod_info["c_price_$i"] = $this_order[$i]->product_final_price;
$twoco_prod_info["c_description_$i"] = $this_order[$i]->order_item_name .",". $this_order[$i]->product_attribute;
}
//Authnet vars to send
$formdata = array (
'x_login' => TWOCO_LOGIN,
'x_email_merchant' => ((TWOCO_MERCHANT_EMAIL == 'True') ? 'TRUE' : 'FALSE'),
'id_type' => 1, //added for c_prod compliance
// 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_id"),
'merchant_order_id' => $db->f("order_number"),
'x_receipt_link_url' => SECUREURL."2checkout_notify.php"
);
$formdata = array_merge($twoco_prod_info, $formdata); //combine formdata with twoco_prod_info so c_prod info gets sent to 2co
if( TWOCO_TESTMODE == "Y" )
$formdata['demo'] = "Y";
$version = "2";
$url = "https://www.2checkout.com/checkout/spurchase";
//Convert the currency
$my_2co_default_currency = 'EUR';
$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, '.', '');
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>
<?php
}
?>
Ensure you have entered your 2CO Vendor ID and Secret Word as they appear in your 2CO account into the settings.
Set "Order Status for successful transactions" to "Confirmed"
Set "Order Status for failed transactions" to "Canceled"
Save these settings.
Note: You do NOT need to add you "Approved URL" to your site management settings in the 2CO Vendor Admin area. This is included in the code above.
Ensure you have moved/copied 2checkout_notify.php from ROOT/administrator/components/com_virtuemart to your site root (i.e. if you were to type in the URL to this file it would appear like this:
www.yoursite.com/2checkout_notify.php)
Now make a test order.
The code, as created on the original thread, passes through the product name and description (one for each item ordered in the cart) to 2CO (so your customer will see his/her ordered items there as well, not some meaningless session number), sends the full and correct customer info to 2CO (no more parameter errors), converts the currency if needed (make sure you have set it correctly using the currency identifiers supplied by 2CO), and removes the need to click on the 2CO image to go through to payment - you are immediately redirected to 2CO.
I have used the 2CO Single Page payment method in the code above. If you wish to use the standard multi-page 2CO payment chain, change the following line from the code:
https://www.2checkout.com/checkout/spurchase
To:
https://www.2checkout.com/checkout/purchase
I'd like to thank all of the contributors to the original thread for their sterling work - they saved me many hours, although I ended up spending almost as many hours trying to find out how to get the order confirmation to work! But we're a little bit closer now.
If this works, or if you find cleaner/better methods of getting this code into action, please post them - 2CO & VM users really need to stick together!
Silent Dave