Search

Change Language

Sunday, July 17, 2011

Implementing Moneybookers Payment Gateway with PHP

Today we will integrate a Payment gateway in our website. We are going to implement Moneybookers Payment Gateway. Moneybookers provide an easily customizable payment gateway that can be implemented without much headache. You just need to create two accounts on Moneybookers, one for the Merchant and other for the Buyer. Then send a request to Moneybookers to convert these accounts into test accounts (for testing purpose). This is a manual process as Moneybookers provide no sandbox account like PayPal for the developers. After your account has been approved as test account, you can start using it for transactions.
Moneybookers payment gateway gives you two options for the payment, either you can transfer the buyer to Moneybookers payment page or you can embed the iframe into your own page which looks more elegant. Here we will be following the second one i.e the iframe.

Database structure:
Create table 'payment_details' as:
_________________________________________________
CREATE TABLE `payment_status` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT,
  `transaction_id` varchar(255) DEFAULT NULL,
  `amount` float(10,2) DEFAULT NULL,
  `status` int(1) DEFAULT NULL,
  `payment_type` varchar(20) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `username` varbinary(255) DEFAULT NULL,
  PRIMARY KEY (`order_id`)
)
_________________________________________________



Create the payment page:
We will include our payment form inthis page using an iframe. This is simple and doesn't need anything special. We will pass the necessary information with the URL of the iframe as:
<iframe src="http://yoursite.com/payment-form?email=<?php echo $email ?>&fname=<?php echo $FirstName ?>&lname=<?php echo $LastName ?>&address=<?php echo $Address ?>&address2=<?php echo $Address2 ?>&city=<?php echo $City ?>&postal_code=<?php echo $PostalCode ?>&country=<?php echo $CountryId ?>&state=<?php echo $State ?>" name="payment" height="600" width="610" border="0">
 </iframe>

Create the payment form:
Payment page starts with a form with a number of hidden fields for the payment process. We will autosubnit this form to proceed to the next step of payment.
<html>
<head>
<title>MoneyBookers by WebSpeaks.in</title>
</head>
<body>
<?php
$email = $_GET['email'];
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$address = $_GET['address'];
$address = $_GET['address'];
$address2 = $_GET['address2'];
$city = $_GET['city'];
$postal_code = $_GET['postal_code'];
$country = $_GET['country'];
$state = $_GET['state'];
 
$price = 10;
?>
<form action="https://www.moneybookers.com/app/payment.pl" target="_self" name="payment_form">
 
 <input type="hidden" name="pay_to_email" value="your_merchant_email"> 
 <input type="hidden" name="pay_from_email" value="<?php echo $email ?>">
 <input type="hidden" name="return_url" value="http://yoursite.com/payment_success.php"> <!-- URL to redirect after payment success -->
 <input type="hidden" name="cancel_url" value="http://yoursite.com/payment_cancel.php">  <!-- URL to redirect after payment cancel -->
 <input type="hidden" name="status_url" value="http://yoursite.com/payment_status.php"> <!-- URL to get the payment response (not visible to user, called on backend) -->
 <input type="hidden" name="language" value="EN"> <!-- Language of payment -->
 
 <input type="hidden" name="hide_login" value="1">  <!-- Whether to show the tiny login form with the payment form, no in our case -->
 
 <!-- Specifies a target in which the return_url value will be called upon successful payment from customer.  -->
 <!-- 1 = '_top', 2 = '_parent', 3 = '_self', 4= '_blank' -->
 <input type="hidden" name="return_url_target" value="1">  
 <input type="hidden" name="cancel_url_target" value="1"> 
 
 <!-- Custom fields for your own needs -->
 <input type="hidden" name="merchant_fields" value="name, username, order_amount, user_id"> <!-- List all your custom fields here (comma separated, max 5)-->
 <input type="hidden" name="name" value="<?php echo $fname. ' '.$lname ?>"> <!-- Value of Custom 'name' -->
 <input type="hidden" name="username" value="<?php echo $username ?>">  <!-- Value of Custom 'username' -->
 <input type="hidden" name="order_amount" value="<?php echo $price ?>"> <!-- Value of Custom 'order_amount' -->
 <input type="hidden" name="user_id" value="<?php echo $user_id ?>">  <!-- Value of Custom 'user_id' -->
 
 <input type="hidden" name="amount_description" value="Testing Amount">  <!-- Description of the amount -->
 <input type="hidden" name="amount" value="<?php echo $price ?>">  <!-- Amount to be charged -->
 <input type="hidden" name="currency" value="GBP">   <!-- Currency of payment -->
 <input type="hidden" name="firstname" value="<?php echo $fname ?>">   <!-- Firstname of buyer, need for autofilling -->
 <input type="hidden" name="lastname" value="<?php echo $lname ?>">    <!-- Lastname of buyer, need for autofilling -->
 <input type="hidden" name="email" value="<?php echo $email ?>">    <!-- Email of buyer, need for autofilling -->
 <input type="hidden" name="address" value="<?php echo $address ?>">     <!-- Address of buyer, need for autofilling -->
 <input type="hidden" name="city" value="<?php echo $city ?>">     <!-- City of buyer, need for autofilling -->
 <input type="hidden" name="state" value="<?php echo $state ?>">     <!-- State of buyer, need for autofilling -->
 <input type="hidden" name="postal_code" value="<?php echo $postal_code ?>">     <!-- Postal_code of buyer, need for autofilling -->
 <input type="hidden" name="country" value="<?php echo $country ?>">     <!-- Country code of buyer, need for autofilling -->
 <input type="hidden" name="detail1_description" value="New Product">      <!-- Description heading of the payyment, shown after payment has been made -->
 <input type="hidden" name="detail1_text" value="<?php echo $product ?>">      <!-- Detailed description of the payment, shown after payment has been made -->
 <input type="hidden" name="confirmation_note" value="Thanks for purchasing Voodoo Video!"> <!-- Confirmation message to be shown after payment has been made --> 
 
 <p>
  <input type="submit" class="submitBtn">

Receive Payment Status:

In the payment form we have specified the 'status_url' field. This field specifies the URL on which the Moneybookers sends the status of the payment. It is URL to which the transaction details will be posted after the payment process is complete. Alternatively, you may specify an email address to which you would like to receive the results. If the status_url is omitted, no transaction details will be sent to the Merchant. 
Detailed status description 
‘2’ Processed – This status is sent when the transaction is processed and the funds have been received on your Moneybookers account. 
‘0’ Pending – This status is sent when the customers pays via the pending bank transfer option. Such transactions will auto-process IF the bank transfer is received by Moneybookers. We strongly recommend that you do NOT process the order/transaction in your system upon receipt of a pending status from Moneybookers. 
‘-1’ Cancelled – Pending transactions can either be cancelled manually by the sender in their online account history or they will auto-cancel after 14 days if still pending. ‘-2’ Failed – This status is sent when the customer tries to pay via Credit Card or Direct debit but our provider declines the transaction. If you do not accept Credit Card or Direct debit payments via Moneybookers (see page 16) then you will never receive the failed status. 
‘-3’ Chargeback – This status could be received only if your account is configured to receive chargebacks. If this is the case, whenever a chargeback is received by Moneybookers, a -3 status will be posted on the status_url for the reversed transaction. 

Create payment_status.php as:

$status = $_POST['status']/*Status of transaction*/
$username = $_POST['username'];/*Custom field*/
$user_id = $_POST['user_id'];/*Custom field*/
$amount = $_POST['amount'];/*Amount of transaction*/
$payment_type = $_POST['payment_type'];/*Type of Payment*/
$mb_transaction_id = $_POST['mb_transaction_id'];/*Moneybookers transaction id*/
 
/*save transaction details in database*/
mysql_query("INSERT INTO `payment_details` (`transaction_id`, `amount`, `status`, `payment_type`, `user_id`, `username`) VALUES ('".$mb_transaction_id."', '".$amount."', '".$status."', '".$payment_type."', '".$user_id."', '".$username."') "

Payment Success Page:
<?php
echo "Your payment has been received. Thanks for purchasing our product.";
?>

Payment Cancel Page:

<?php
echo "Your payment has been cancelled. Please try again.";
?>