Basic 2checkout processing script

We do not have a rant today, nor we do have a review. Today I am going to go over something that a lot of people ask me – how to create a very basic 2checkout processing script. I am going to say now that this guide is simply how to do the processing side of it, it will not cover anything like the database side of things so you can create something like automatic downloads – we may do that as another tutorial.

2checkout HTML Form.

The first thing we need to do is the HTML form for 2checkout. We are going to be using the single page checkout for 2checkout as I prefer that one. Replace sid value with your own and setup the bits below to what you want it to be called. If you want to add a second product to the checkout form, add things like c_name_2, c_price_2 etc. As stated either, I am not going into the database side of things in this tutorial but you can always use a programming language to prefill all in this and use cart_order_id as a method to know what order someone is ordering.

The return URL you want to set up as the same as the URL for the second file which is listed after the HTML form.

2checkout.com/checkout/spurchase” method=”post”>
<input type=”hidden” name=”sid” value=”123456″/>
<input type=”hidden” name=”cart_order_id” value=”XXX”/>
<input type=”hidden” name=”total” value=”XXX”/>
<input type=”hidden” name=”c_name_1″ value=”XXX”/>
<input type=”hidden” name=”c_description_1″ value=”XXX”/>
<input type=”hidden” name=”c_price_1″ value=”XXX”/>
<input type=”hidden” name=”fixed” value=”Y”/>
<input type=”hidden” name=”return_url” value=”XXX” />
<input type=”hidden” name=”merchant_order_id” value=”XXX” />
<input type=”hidden” name=”x_receipt_link_url” value=”XXX”/>
<input type=”hidden” name=”id_type” value=”1″/>
<input type=”hidden” name=”c_tangible_1″ value=”N”/>
<input type=”hidden” name=”lang” value=”en”/>
<input type=”hidden” name=”pay_method” value=”CC”/>
<input type=”submit” value=”Purchase”/>
</form>
If you want PayPal to be the preferred selection, change pay_method to PPI. So now we need to do the page where they go after they have completed the payment.
2checkout PHP Form
Now this bit is very simple, eveyrthing is passed on directly to us via GET or POST (depending on your 2checkout settings). The only part we actually need to do is make sure you check the hash value given from 2checkout return so that you can confirm the person actually ordered and not just sent a GET request via their own browser.
So how we do make the hash. Well the hash is simply – md5 (secret word + vendor number + order number + total). So such as, we have.
<php
//Lets check the hash!
$OurKey = “snatty”;
$MOrderID = $_GET[‘merchant_order_id’];
$VendorID = $_GET[‘sid’];
$OrderID = $_GET[‘cart_order_id’];
$Total = $_GET[‘total’];
$CheckoutHash = $_GET[‘hash’];
$OurHash = md5($OurKey . $VendorID . $MOrderID . $Total);
if($CheckoutHash == “1”)
{
//This was a demo order – do what you want for a demo order.
}
if($OurHash == $CheckoutHash)
{
//Do all your PHP stuff here such as updating a database etc.
}
else
{
//This is highly like a unlegit order, or fraud or something. Deal with it accordingly.
}
?>

And there you have it, just a very basic 2checkout ordering system. It isn’t the best but what do you expect for me spending just a few minutes writing this. 2checkout also though does have a setting to send everything by POST so may need to take that into account, While the above should work, II ain’t making promises anything that you can just drop this into your code and it will work. I have also not tested the PHP side of it, but it should work but regardless this tutorial serves as a nice guide to get you started.