PHP Payment Library for Paypal, Authorize.net and 2Checkout (2CO)
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
Snippet 2
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
Snippet 4
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
Snippet 6
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.