How to integrate Authorize.net & PayPal payment gateway
Do you find it difficult to integrate a payment gateway? Do you need a simplest and fastest way of payment gateway integration? If yes then this article is for you.
The payment gateway systems are there for a long time now, they have come a long way and so does their APIs. They are much sophisticated and complex systems now.
All majority payment gateway systems are not offering REST API endpoints with different implementation to capture the payment. And they generally require SDKs to make your work easy.
These SDKs are large and complex to learn or understand them on the go. I confess that it’s not a rocket science but it’s definitely something you may need to learn extra.
If your requirement is simple and it doesn’t include advance payment systems like split payment, paralel payment or authorizing user and capturing payment(s) later in the system then there is a very simple way persist.
If you’re a designer or a novice programmer who wants a way to quickly attach the payment gateway without much of hassel and learning here is the procedure to integrate Authorize.net and PayPal payment gateway.
Integrate Authorize.net payment gateway:
Authorize.net is one of the mainstram payment gateway provider in the indsutry and it offers many various way to integrate payment system.
We are more interested with the SIM (Simple Integration Model) method. It takes couple of minute to integrate the payment system.
You will need a developer sandbox account in order to test the code and transaction while developmental. If you don’t have an account, create a authorize.net sandbox account here.
Keep the loginID and TransactionKey handy we will need them in the later part of this project.
Let’s start our one file minimalist code of SIM payment integration method.
Setting up the essential variables:
$loginID = “YOUR-LOGIN-ID”;
$transactionKey = “YOUR-TRANSACTION-KEY”;
$amount = “19.99”;
$description = “Sample Transaction”;
$label = “Submit Payment”; // The is the label on the ‘submit’ button
The variable setup is pretty self explanatory. The loginID and transactionKey help us identify our merchant account on payment gateway server.
Amount variable contains the total order amount. You can set anything here or you can set value dynamically based on user item selection or customization if you are offering.
Description is the transaction description that will be reflected on payment page as well as the payment gateway generated payment receipt.
Label is simple containing the text we want to show on payment button. Pressing the button user will be redirected to payment page.
Payment processing variables:
$testMode = “true”;
$url = “https://test.authorize.net/gateway/transact.dll”;
Setting testMode true helps to code use sandbox (mock) transaction and keeping it false will execute the real transaction.
URL can hold either of the two endpoints urls. Each url points to the sandbox payment processing or real transaction processing respectively.The one we shown above is used payment testing or in other words sandbox processing.
Transaction related variables:
$invoice = date(‘YmdHis’);
$sequence = rand(1, 1000);
$timeStamp = time();
$fingerprint = hash_hmac(“md5”, $loginID . “^” . $sequence . “^” . $timeStamp . “^” . $amount . “^”, $transactionKey);
If you are using custom invoice numbers you can put that code here. If not we will be using the date and time to generate a unique invoice number for the transaction.
The fingerprint variable contains a hashed value with a combination of loginID, Sequence, timeStamp, amount and transactionKey.
We need this variable because the payment server uses same set of merchant information to decrypt the transaction fingerprint and authenticate the transaction.
<form method=’post’ action='<?php echo $url; ?>’ >
<!– Additional fields can be added here as outlined in the SIM integration
guide at: http://developer.authorize.net –>
<input type=’hidden’ name=’x_login’ value='<?php echo $loginID; ?>’ />
<input type=’hidden’ name=’x_amount’ value='<?php echo $amount; ?>’ />
<input type=’hidden’ name=’x_description’ value='<?php echo $description; ?>’ />
<input type=’hidden’ name=’x_invoice_num’ value='<?php echo $invoice; ?>’ />
<input type=’hidden’ name=’x_fp_sequence’ value='<?php echo $sequence; ?>’ />
<input type=’hidden’ name=’x_fp_timestamp’ value='<?php echo $timeStamp; ?>’ />
<input type=’hidden’ name=’x_fp_hash’ value='<?php echo $fingerprint; ?>’ />
<input type=’hidden’ name=’x_test_request’ value='<?php echo $testMode; ?>’ />
<input type=’hidden’ name=’x_show_form’ value=’PAYMENT_FORM’ />
<input type=’submit’ value='<?php echo $label; ?>’ />
We now have everything in place and setup to process a transaction. Compile the code into a single file by replacing with your loginId and transactionKey. Once done hit the Pay button.
You can use the mock card number to process the payment flow. Consider the testing guide which gives the card number and status code references.
You see the x_ variables these are called as the fieldnames in payment gateway and they have specific meaning. Refer the fieldnames and description guide to learn more.
Integrate PayPal payment gateway:
Well you need no introduction to this payment gateway. You should have heard or worked with PayPal payment system many times in your life.
PayPal is pioneer in electronic payment processing system. It offers variety of payment solutions. Its undoubtedly the large and complex payment system.
To make this easier they have released the API documentation for each of the payment types with example. Additionally they are also offering payment SDKs for various languages or frameworks to make your work easy.
We will be talking about the exploring the easy gem called PayPal NVP solution. The NVP stands for Name Value Pair solution.
Let’s start the PayPal integration process:
As we are creating and testing the solution you may need the developer or sandbox account. If you don’t have a developer account, create the PayPal developer sandbox account here.
Setting up the user accounts:
Once you are done with the sandbox account you will need to create “Merchant” and the “Buyer” types of users.
Let’s proceed with the Merchant user. Login to your developer account and now click on the accounts tabs from the left navigation panel. Choose the Business(Merchant) as user type while creation the user.
The same applies to the Buyer account. Go to the accounts tab and select the Buyer as user type while creating the user account.
The payment processing code:
The code we are covering is fairly easy and self explanatory as well. The solution shown here will help you create a payment solution for single and/or multiple products in single transaction.
<form method=”post” action=”https://www.sandbox.paypal.com/cgi-bin/webscr”>
<input type=”hidden” name=”cmd” value=”_cart”>
<input type=”hidden” name=”upload” value=”1″>
<input type=”hidden” name=”business” value=”MERCHANT EMAIL”>
<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”item_name_1″ value=”Item One”>
<input type=”hidden” name=”amount_1″ value=”150.00″>
<input type=”hidden” name=”item_name_2″ value=”Item Two”>
<input type=”hidden” name=”amount_2″ value=”4250.00″>
<input type=”image” src=”http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif” border=”0″ name=”submit”>
The _cart variable is used to specify that the transaction we are making will be a cart purchase.
Additionally, the _cart variable helps us specify what kind of cart shopping we are making. The upload is used to specify the third party shopping cart(not the paypal shopping cart) solution.
The business variable is used to specify the merchant email address. The email address which will get the payment.
The currency_code variable helps you specify the the transaction currency. We used USD for this example. You can see the supported currency code on the documentation.
The variables item_name_x and amount_x pairs are used to specify multiple product and its amount in the shopping cart.
Finally we are using the PayPal pay image on our submit button. Hitting the button will navigate the user to the payment page, which shows each of the items and total of transaction amount.
That is it folks! If you loved this easy way of integrating PayPal and Authorize.net gateway, let us know in the comment below. If you find something confusing writing down in the comments, we’ll try answer it.
No matter how complex payment systems are, there is an easy way integrating it if your needs are real simple. The article will help you integrate your prefered PayPal or Authorize.net payment gateway easily.