17 June 2010

PHP Payment Library for Paypal, Authorize.net and 2Checkout

If you are like me, whenever you need to work with a 3rd party API or a gateway, you’d first search in Google for a possible wrapper for it in PHP. When it comes to supporting payment gateways, you get bunch of libraries in the search results who are fundamentally different. Some of them are old PHP 3/4 ones, some are new, some may need PEAR, etc.
As they were not required together in one single project, I used them whenever needed. But in one project, I needed them all. I thoughts it’s a chance and decided to stop using them and wrote my own ones where I can use the same methods for all the gateways

So, here is an abstract PaymentGateway library which is being extended to be used for three popular payment gateways (Paypal, Authorize.net, and 2Checkout) in order to provide you with a similar way of using them. Note that the libraries are for basic usage only and do not contain options for recurring payments. Without much babble, let’s see a few examples of how you can use them.

Paypal
In order to process payments using Paypal, you’ll need to follow these steps:

  1. Send the required information to Paypal (snippet 1). Be sure to specify your Paypal email where you 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 you will not need in real scenario.
  2. Create a payment success page where Paypal will send your customer after payment.
  3. Create a payment failure page where Paypal will send your customer after failed payment.
  4. Create a IPN page where Paypal will send payment notification in the background. Make sure you use/remove the test mode in conjunction with step 1. (snippet 2)

Snippet 1


// Include the paypal library
include_once ('Paypal.php');
// Create an instance of the paypal library
$myPaypal = new Paypal();
// Specify your paypal email
$myPaypal->addField('business', 'YOUR_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://YOUR_HOST/payment/paypal_success.php');
$myPaypal->addField('cancel_return', 'http://YOUR_HOST/payment/paypal_failure.php');
// Specify the url where paypal will send the IPN
$myPaypal->addField('notify_url', 'http://YOUR_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 2


// 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);
    }
}
Authorize.net
In order to process payments using Authorize.net, you’ll need to follow these steps:
  1. Send the required information to Authorize.net(snippet 3). Be sure to specify your Authorize.net login and secret key, the success/failure pages, the IPN page, and the product information. The example has the test mode ON, which you will not need in real scenario.
  2. Create a payment success/failure page where Authorize.net will send your customer after payment.
  3. Create a IPN page where Authorize.net will send payment notification in the background. Make sure you use/remove the test mode in conjunction with step 1. (snippet 4)
  4. In order to set the secret key, log into your authorize.net merchant account. Go to “MD5 Hash” menu and set a secret word to desired values and use that in the “setUserInfo” function showed in the example.

Snippet 3


// Include the paypal library
include_once ('Authorize.php');
// Create an instance of the authorize.net library
$myAuthorize = new Authorize();
// Specify your authorize.net login and secret
$myAuthorize->setUserInfo('YOUR_LOGIN', 'YOUR_SECRET_KEY');
// Specify the url where authorize.net will send the user on success/failure
$myAuthorize->addField('x_Receipt_Link_URL', 'http://YOUR_HOST/payment/authorize_success.php');
// Specify the url where authorize.net will send the IPN
$myAuthorize->addField('x_Relay_URL', 'http://YOUR_HOST/payment/authorize_ipn.php');
// Specify the product information
$myAuthorize->addField('x_Description', 'T-Shirt');
$myAuthorize->addField('x_Amount', '9.99');
$myAuthorize->addField('x_Invoice_num', rand(1, 100));
$myAuthorize->addField('x_Cust_ID', 'muri-khao');
// Enable test mode if needed
$myAuthorize->enableTestMode();
// Let's start the train!
$myAuthorize->submitPayment();

Snippet 4


// Include the paypal library
include_once ('Authorize.php');
// Create an instance of the authorize.net library
$myAuthorize = new Authorize();
// Log the IPN results
$myAuthorize->ipnLog = TRUE;
// Specify your authorize login and secret
$myAuthorize->setUserInfo('YOUR_LOGIN', 'YOUR_SECRET_KEY');
// Enable test mode if needed
$myAuthorize->enableTestMode();
// Check validity and write down it
if ($myAuthorize->validateIpn())
{
    file_put_contents('authorize.txt', 'SUCCESS');
}
else
{
    file_put_contents('authorize.txt', "FAILURE\n\n" . $myPaypal->ipnData);
}
2Checkout
In order to process payments using 2Checkout, you’ll need to follow these steps:
  1. Send the required information to 2Checkout(snippet 5). Be sure to specify your 2Checkout vendor id, the return page, and the product information. Please note that 2Checkout does not send IPN in the background, so you will need to handle the payment data in the return page. The example has the test mode ON, which you will not need in real scenario.
  2. Create a return page where 2Checkout will send your customer after payment. This is also where you will need to retrieve and use the payment data. Make sure you use/remove the test mode in conjunction with step 1. (snippet 6)
  3. In order to set the secret key, log into your 2checkout.com account and go to “Look and Feel” section. At the bottom enter the “Secret Word” and use it in the IPN verification process as shown in the example.

Snippet 5


// Include the paypal library
include_once ('TwoCo.php');
// Create an instance of the authorize.net library
$my2CO = new TwoCo();
// Specify your 2CheckOut vendor id
$my2CO->addField('sid', 'YOUR_VENDOR_ID');
// Specify the order information
$my2CO->addField('cart_order_id', rand(1, 100));
$my2CO->addField('total', '9.99');
// Specify the url where authorize.net will send the IPN
$my2CO->addField('x_Receipt_Link_URL', 'http://YOUR_HOST/payment/twoco_ipn.php');
$my2CO->addField('tco_currency', 'USD');
$my2CO->addField('custom', 'muri-khao');
// Enable test mode if needed
$my2CO->enableTestMode();
// Let's start the train!
$my2CO->submitPayment()

Snippet 6


// Include the paypal library
include_once ('TwoCo.php');
// Create an instance of the authorize.net library
$my2CO = new TwoCo();
// Log the IPN results
$my2CO->ipnLog = TRUE;
// Specify your authorize login and secret
$my2CO->setSecret('YOUR_SECRET_KEY');
// Enable test mode if needed
$my2CO->enableTestMode();
// Check validity and write down it
if ($my2CO->validateIpn())
{
    file_put_contents('2co.txt', 'SUCCESS');
}
else
{
    file_put_contents('2co.txt', "FAILURE\n\n" . $my2CO->ipnData);
}
Hope this will help you integrate the payment gateways in an easy manner. If you have any questions, or find any bug, have a suggestion, feel free to post them as comment here. Btw, this library is released under the MIT license.

Download

payment.zip
PHP Payment Library for Paypal, Authorize.net and 2Checkout

13 June 2010

bad word filter script in php

This is a simple file based bad word filter PHP function. The function offers a variety of options. You can choose how many letters of the bad word you wish to keep, which character you want to use as the badword "bleeper" as well as a rating for the bad word.

The bad words file would be formatted like this:

  • 9   badword
  • 2   anther badword
  • 7   A bad word Phrase
  • 7   YetAnotherWord
The first character number is the bad word "rating" the higher the number the worse the word. This can be useful in say, a forum situation where a user can specify their bad word tolerance. So if I pass the function a bad word tolerance of 5, the function will only remove words with a tolerance rating higher than 5.

A sample function call would look like this:

$myString = "This is a sentence with some badwords, It is an example based on the Yetanotherword file above.";

echo filterBadWords($myString,"badwords.txt","*",1,3);
If our above badwords file was used for this string the output would result in:

This is a sentence with some b******s, It is an example based on the Y************* file above.
Here is the function code:

function filterBadWords($str,$badWordsFile,$replaceChar="*",$showLetters=0,$rating=0){
  // check for the badwords file
  if (!is_file($badWordsFile)) {
    echo 'ERROR: Could not find badwords file "'.$badWordsFile.'"';
    exit;
  }
  else {
    //open or file as resource $handle
    $handle =  fopen($badWordsFile,"r");
  }
  // while we're not at eof (End Of File) do this
  while(!feof($handle)){
    $badword = trim(fgets($handle)); // get the word from the file
    $word_rating = substr($badword,0,1); // get the badword "rating", word is in format 5myBadWord
    $badword = substr($badword,1);  //take the rating off of the badword so we have just the badword left
    //if my word rating is greater than my exceptable level bleep it out
    if($word_rating>$rating){
      // look for and take out our bad word
      $str = eregi_replace($badword, substr($badword,0,$showLetters).sprintf("%'".$replaceChar.(strlen($badword)-$showLetters)."s", NULL), $str);
    }
  }
  //return our formatted string
  return $str;
}