Basically, authorize.net is providing CIM (Customer Information Manager) and AIM (Advanced Integration Method) APIs which we can use in our application to integrate payment gateway. In this tutorial, we will learn how to Integrate Authorize.net API with PHP.
Before going ahead let’s check different between CIM and AIM.
What is CIM in authorize.net?
CIM is the easiest way to implement ongoing subscriptions or multiple charges without redirecting to a page on Authorize.net. We can use CIM to tokenize and store customer’s sensitive payment information on authorize.net’s secure server. By below image, we can easily understand the functionality of CIM.
What is AIM in authorize.net?
AIM is recommended if you are just processing just a single charge. You can not create any subscription or charge a customer multiple times.
Minimum Requirements to integrate authorize.net CIM / AIM API
- The merchant’s website must have server-side scripting or CGI capabilities such as ASP Classic, C#, Cold Fusion, Java, Perl, PHP, or VB.Net
- The merchant must be able to securely store payment gateway account data, such as their API Login ID or Transaction Key.
- The merchant’s website must use HTTPS.
- The merchant must have a valid SSL or TLS certificate and their website must be capable of initiating both client and server side connections.
Note: Authorize.Net plans to disable TLS 1.0 and TLS 1.1 on Sandbox (April 30, 2017) and Production (Sept 18, 2017). So it is recommended to use TLS 1.2.
Steps to Integrate Authorize.net API with PHP
Step 1: Download authorize.net class file
Download authorize.class.php file and include this PHP file using include_once function. Create an object of AuthorizeAPI class as mentioned below.
1 2 |
include_once('authorize.class.php'); $objAuthorizeAPI = new AuthorizeAPI('7mMJ5tx5M', '37U3eM9qR7Td5qEg'); |
Here 7mMJ5tx5M is an API login id and 37U3eM9qR7Td5qEg is a transaction key. Pass third parameter as ‘liveMode’ for a production environment.
Step 2: Set customer’s information
After creating an object of AuthorizeAPI class, Call setCustomerAddress to set customer’s information like the first name, last name, company name, address, phone number and email address.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$arrCustomerInfo = array(); $arrCustomerInfo['firstname'] = 'Prashant'; $arrCustomerInfo['lastname'] = 'Jethwa'; $arrCustomerInfo['company_name'] = 'Code Bucket'; $arrCustomerInfo['ad_street'] = 'Jodhpur Crossroad'; $arrCustomerInfo['ad_city'] = 'Ahmedabad'; $arrCustomerInfo['ad_state'] = 'Gujarat'; $arrCustomerInfo['ad_zip'] = '380015'; $arrCustomerInfo['ad_country'] = 'India'; $arrCustomerInfo['ph_number'] = '123456791'; $arrCustomerInfo['em_email'] = 'codebucket.co@gmail.com'; $objAuthorizeAPI->setCustomerAddress($arrCustomerInfo); |
Step 3: Add Customer Payment Profile (Add credit card / eCheck)
To add credit card / eCheck (customer payment profile) in authorize.net call setCreditCardParameters and addCustomerPaymentProfile methods as mention below:
1 2 3 4 5 6 7 |
/* First: Credit card number Second: Expiration date (YYYY-MM) Third: CVV */ $objAuthorizeAPI->setCreditCardParameters('370000000000002', '2019-12', '123'); $arrCustomerAddCCResponse = $objAuthorizeAPI->addCustomerPaymentProfile(111,'cc'); |
Here 111 is customer id (Unique id) and cc is a parameter which will indicate that we are adding a credit card.
Authorize.net will give a response like below:
1 |
{"success":"1","customerProfileId":"1501067535","customerPaymentProfileId":"1500625311","error":"","paymentFlag":"1","message":""} |
Store customer profile id and customer payment profile id in a table for future reference.
After successfully adding a credit card, we can see customer profile and payment profile in authorize.net account which will look like below:
If we want to add another credit card / eCheck for this same customer then call addCustomerPaymentProfile function as below:
1 2 3 4 5 6 7 |
/* First: Credit card number Second: Expiration date (YYYY-MM) Third: CVV */ $objAuthorizeAPI->setCreditCardParameters(‘4007000000027’, '2019-12', '123'); $arrCustomerAddCCResponse = $objAuthorizeAPI->addCustomerPaymentProfile(111,'cc', true, 1501067535); |
Here we are passing the third parameter as true because this customer is already added once in authorize.net account and we want to add another credit card. Pass customer profile id as a fourth parameter so API will add a new credit card for this customer only.
To add eCheck in authorize.net, call a method as below:
1 2 3 4 5 6 7 8 |
/* First: Routing number Second: Account number Third: Name on account Fourth: Account type (checking, savings, or businessChecking) */ $objAuthorizeAPI->setBankParameters('071921891', '123456789', 'Prashant Jethwa', 'savings'); $arrCustomerAddeCheckResponse = $objAuthorizeAPI->addCustomerPaymentProfile(111,'eCheck',true, 1501067535); |
Here we are passing the second parameter as eCheck to indicate that we are adding eCheck.
That’s it, by above three steps we can add credit card / eCheck details in Authorize.net which are basic steps to Integrate Authorize.net API with PHP.
Some of the useful methods which I created in authorize.class.php file are as below:
Method 1: Charge customer’s credit card / eCheck
1 2 3 4 5 |
/* First: Customer profile id Second: Customer payment profile id Third: Amout */ $arrChargeResponse = $objAuthorizeAPI->chargeCCeCheck(1501067535,1500625311, 50.15); |
Method 2: Get credit card / eCheck Information
1 2 3 4 |
/* First: Customer profile id Second: Customer payment profile id */ $arrCCeCheckInfo = $objAuthorizeAPI->getCCeCheckInfo(1501067535,1500625311); |
Method 3: Refund Amount From Transaction
1 2 3 4 5 6 |
/* First: Customer profile id Second: Customer payment profile id Third: Transaction id Fourth: Amount */ $arrRefundResponse = $objAuthorizeAPI->refundMoneyFromTransaction(1501067535,1500625311, '6547898132', 20.12); |
Method 4: Get Transaction status
1 2 |
// First: Transaction id $arrTransactionStatus = $objAuthorizeAPI->getTransactionStatus('6547898132'); |
Method 5: Delete customer payment profile (Delete credit card / eCheck)
1 2 3 4 |
/* First: Customer profile id Second: Customer payment profile id */ $arrCPPDeleteResponse = $objAuthorizeAPI->deleteCustomerPaymentProfile(1501067535,1500625311); |
Method 6: Delete customer profile (Delete customer from authorize.net account)
1 2 |
// First: Customer profile id $arrCPDeleteResponse = $objAuthorizeAPI->deleteCustomerProfile(1501067535); |
Click on below link to download complete PHP code with working examples.
Download code to Integrate Authorize.net API with PHP
You can also read Authorize.net CIM document for more information.
Comment if you need any help in payment gateway integration. Happy to help 🙂
You may also like:
July 13, 2017 at 8:31 am
Nice Article , wondering if you have code for integration of Authorize.net with Codeigniter, If you have then please share it or share article with code, Much appreciated.
July 14, 2017 at 4:59 pm
Hi Yash,
You can also integrate with codeigniter. I implemented the same using above class file only.
1) Download authorize.class.php file and place it into controller/libs folder.
2) Include this File. Create an object of AuthorizeAPI class and call the functions which you needed as mention in the article
OR
Extend AuthorizeAPI class to your class and call the function which you needed as mention in the article.
Let me know if you are not able to do it, will help you for sure.
May 28, 2018 at 10:17 am
Hi
Nice article,
How we can implement without storing cc info in authorize.net ?
Thanks
Prajosh
August 7, 2018 at 6:05 am
Hi Prajosh,
You can use AIM methods of authorize.net to charge customer without storing cc info.
June 21, 2018 at 5:17 am
Hi,
I already tried your code, But I’m getting plain page(Empty page).
August 7, 2018 at 6:03 am
Hi Dinesh,
Make sure you have correct setup for authorize.net.
1) Your gateway should be in correct mode (test/live)
2) Make sure to allow authorize account to access by API call
3) Check your TLS version
Let me know if you need any help for the same.