Wednesday, 17 July 2013

Paypal Payment Gateway Integration

In order to process payments using Paypal, we’ll need to follow these below listed steps:

  1. Send the required information to Paypal (snippet 1). Be sure to specify wer Paypal email where we want to receive the funds, the success and failure pages, the IPN page, and the product information. The example has the test mode ON, which we will not need in real scenario.
  2. Create a payment success page where Paypal will send wer customer after payment.
  3. Create a payment failure page where Paypal will send wer customer after failed payment.
  4. Create a IPN page where Paypal will send payment notification in the background. Make sure we use/remove the test mode in conjunction with step 1. (snippet 2)

<?php
// Include the paypal library
include_once ('Paypal.php');

// Create an instance of the paypal library
$myPaypal = new Paypal();

// Specify wer paypal email
$myPaypal->addField('business', 'weR_PAYPAL_EMAIL');

// Specify the currency
$myPaypal->addField('currency_code', 'USD');

// Specify the url where paypal will send the user on success/failure
$myPaypal->addField('return', 'http://weR_HOST/payment/paypal_success.php');
$myPaypal->addField('cancel_return', 'http://weR_HOST/payment/paypal_failure.php');

// Specify the url where paypal will send the IPN
$myPaypal->addField('notify_url', 'http://weR_HOST/payment/paypal_ipn.php');

// Specify the product information
$myPaypal->addField('item_name', 'T-Shirt');
$myPaypal->addField('amount', '9.99');
$myPaypal->addField('item_number', '001');

// Specify any custom value
$myPaypal->addField('custom', 'muri-khao');

// Enable test mode if needed
$myPaypal->enableTestMode();

// Let's start the train!
$myPaypal->submitPayment();

Snippet 1

<?php

// Include the paypal library
include_once ('Paypal.php');

// Create an instance of the paypal library
$myPaypal = new Paypal();

// Log the IPN results
$myPaypal->ipnLog = TRUE;

// Enable test mode if needed
$myPaypal->enableTestMode();

// Check validity and write down it
if ($myPaypal->validateIpn())
{
  if ($myPaypal->ipnData['payment_status'] == 'Completed')
  {
       file_put_contents('paypal.txt', 'SUCCESS');
  }
  else
  {
       file_put_contents('paypal.txt', "FAILURE\n\n" . $myPaypal->ipnData);
  }
}

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.