Monday, December 13, 2010

2Checkout payment Gateway integration using PHP

Installation and Usage
========================================
In order to use the CC Processor Gateway Interface, you
must have an account with
2Checkout http://www.2checkout.com/
Open the processors.php script and set the variable as
indicated at the top of the script. Upload the script to
your server and then follow the instructions below.


HOW TO USE - 2CHECKOUT
========================================
Using the 2Checkout functionality takes a bit of work, at
least if you want to be truly automated. For a basic 2Checkout
transaction, place the commandrequire_once("processors.php");
at the top of your existing script (for example, the script
that your order form posts to). Call the twocheckout() function
from your script like so:
twocheckout($amount [,$invoice]);
The only parameter you must pass to the twocheckout function is
the dollar amount of the transaction. The customer will
be redirected to a payment form at 2Checkout's site which
lets them make their payment. If you do not supply an invoice
number, one will be randomly generated for you.Using the twocheckout() function in this basic manner, you will
receive the standard email notification from 2Checkout when
a customer makes a payment, and then you'll have to proceed
manually to provide the customer with his service or product.
This is recommended for non-advanced users.
Now comes the tricky part. If you want to _automate_ the
process of validating 2Checkout transactions and performing
actions based upon the transaction status, you will need
to do two things:
1. Login to 2Checkout and edit your preferences so that 2Checkout's postback URL is set to your installed copy
of 2checkout_ipn.php.
2. Edit 2checkout_ipn.php. You will need to insert your own
PHP code for each of the possible outcomes of a 2Checkout
transaction. 2checkout_ipn.php contains more detailed

instructions.
Note that step 2 requires writing your own PHP code. (If
you don't know PHP, contact PHPSelect.com for a quote on
custom coding.)


2checkout_ipn.php


<?php

  # FILE: 2checkout_ipn.php

  # ----

  # Description: This script handles postback notifications from

# 2Checkout's gateway.
################################################################

  # After someone makes a 2Checkout payment through your site,   #

  # 2Checkout will access this script to instantly notify you    #

  # about the status of the transaction.                         #

  #                                                              #

  # By default, this script will send a "thank you" email to the #

  # payer when a 2Checkout transaction is successfully completed.#

  # Assuming you want to perform some automatic functions when a #

  # payment is completed (like creating a new user's account),   #

  # you MUST insert your own code into the areas marked below in #

  # the body of this script. Each block corresponds to one of the#

  # potential outcomes of a new payment: transaction approved    #

  # or transaction declined.                                     #

  #                                                              #

  # For example, if you're selling a software package on your    #

  # site, you would want to put code in the "success" block that #

  # emails the payer a copy of the software. 2Checkout will post #

  # a number of variables to this script, which you may find     #

  # useful. Some of these variables are:                         #

  #                                                              #

  #   $_POST[order_number]                                       #

  #   $_POST[card_holder_name]                                   #

  #   $_POST[street_address]                                     #

  #   $_POST[city]                                               #

  #   $_POST[state]                                              # 

  #   $_POST[zip]                                                #

  #   $_POST[country]                                            #

  #   $_POST[email]                                              #

  #   $_POST[phone]                                              #

  #   $_POST[cart_order_id]                                      #

  #   $_POST[cart_id]                                            #

  #   $_POST[credit_card_processed]                              #

  #   $_POST[total]                                              #

  #   $_POST[ship_name]                                          #

  #   $_POST[ship_street_address]                                #

  #   $_POST[ship_city]                                          #

  #   $_POST[ship_state]                                         #

  #   $_POST[ship_zip]                                           #

  #   $_POST[ship_country]                                       #

  #                                                              #

  # See the 2Checkout admin area for more information.           #

  ################################################################
# Get the posted data

  $params = "cmd=_notify-validate";
while(list($key, $val) = each($_POST)){

  $val = urlencode(stripslashes($val));

  $params .= "&$key=$val";

  }
if($_POST[credit_card_processed] == "Y"){

  #payment was successful
############################################################

  # INSERT YOUR OWN CODE HERE TO BE PERFORMED WHEN A PAYMENT #

  # WAS SUCCESSFULLY RECEIVED                                #

  ############################################################
#### Placeholder code to send "thank you" email

  $to = $_POST[email];

  $from = "test@example.com";

  $message = "Thanks for your 2Checkout payment of $$_POST[total]!";

  mail($to, "Thank you!", $message, "From: $from");

  #### End placeholder code
}
else{

  #transaction was declined or did not go through
############################################################

  # INSERT YOUR OWN CODE HERE TO BE PERFORMED WHEN A PAYMENT #

  # WAS DECLINED OR DID NOT GO THROUGH                       #

  ############################################################

  }


?>

Processors.php
<?php

  # FILE: processors.php

  # ----

  # Description: This file contains the function used to post

# transactions 2Checkout.
################################################################

  # You MUST set the following variable before using the script  #

  ################################################################
# $merchant2cuser is your merchant ID for 2Checkout. 
$merchant2cuser = "test";
################################################################

  # End of configuration - No need to edit below this line       #

  ################################################################
#Process a transaction via 2Checkout

  #Returns: None. Transaction status will be posted to 

  #2checkout_ipn.php by 2Checkout.
function twocheckout($amount, $invoice=0){
global $_SERVER, $merchant2cuser;
$target = "https://www.2checkout.com/cgi-bin/sbuyers/cartpurchase.2c?";

  $amount = sprintf("%.02f", $amount);
if(!$invoice)

  $invoice = sprintf("%06d", rand(111111, 999999));
$params = "sid=$merchant2cuser&total=$amount&cart_order_id=$invoice";
$params = str_replace(" ", "%20", $params);
header("Location: $target$params");
}
?>

By PHP with 14 comments

14 comments:

i want to integrate 2checkout in php , so could pls give me proper code ??

hello nisha singh
you display on this link not valid url so try to correct url display

These vulnerabilities are mostly due to the fact that the payment information is flowing through browser. The risk can be highly reduced by having another server to server(mutual auth) communication between merchant site and PG in parallel to browser redirect. That can act as a source for validation. Further, both PG and merchant site can use this server to server communication to update the state of transaction rather than relying on browser redirect data. i feel that can be a Grade: 10/10.

aws training in chennai

Hello, Any idea to integrate Net Banking with 2checkout

Hi, i have one issue in INS receiving. INS generated but all are goest to failed list. why? Is there any mistatke in may code?

nice article very easy to use i like it read my blog for other integrations
www.gajabwap.blogspot.in

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

rpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa courses in bangalore

Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
AWS Training in Bangalore

Post a Comment

    • Popular
    • Categories
    • Archives