Integrate Authorize.net payment gateway with SocialEngine

Integrate Authorize.net payment gateway with SocialEngine

Ecommerce and Social networking are trending aspects of today. It seem that these trends shall soon become an essential part of the  our daily routine. Eventually everyone shall use social networking sites to connect with friends and shop online to buy the goods.

One of our client had socialcommerce site built on SocialEngine. Since SocialEngine supports Paypal and 2Checkout payment gateways by default, it was not possible to do the transactions through credit card , debit card or net banking. Our client wants to support these payment modes so that customer has to register payment details once in the system and recurring payment should be done automatically. We did extensive search for payment gateway which support recurring billing with minimum customer interaction and found Authorize.net. In this article, we are going to demonstrate how we integrated SocialEngine with Authorize.Net.

Integrate Socialengine and Authorize.Net Payment Gateway

For integration, we need authorize.net php library which you can get from PHP sdk. We created new module Authorize in Socialengine and add library in Api folder of the module. By default Api folder is not present in newly created module, so we have to add Api folder  first and then add library folder. We add subscription function inside  Indexcontroller controller of the Authorize module. You should add api_login_id and transation_key provide by your authorize.net account, in constructor method of AuthorizeNetRequest class which is available in lib/shared/AuthorizeNetRequest.php file.

<?php

require 'Authorize/Api/lib/shared/AuthorizeNetTypes.php';
require 'Authorize/Api/lib/AuthorizeNetARB.php';
class Authorize_IndexController{
  
  public function subscriberAction(){
  	
  	
  	/*** demo user info type ***/
		  	$subscriptionRow = null;
		  	$userInfo = array();
		  	$street =  "street";
		  	$city = "city";
		  	$state = "state";
		  	$country = "country";
		  	$phone1 = "phone1";
		  	$phone2 = "phone2";
		  	$postalcode = "111122";
		  	
		  	$userInfo['customerPhoneNumber'] = $phone1;
		  	$userInfo['billToAddress'] = $street;
		        $userInfo['billToCity'] = $city;
		  	$userInfo['billToCountry'] = $country;
		  	$userInfo['billToFirstName'] = $userInfo['fname'];
		  	$userInfo['billToLastName'] = $userInfo['lname'];
		  	$userInfo['billToState'] = $state;
		  	$userInfo['billToZip'] = $postalcode;
		  	
	  /*** end demo user info ***/
		  	
  	
  	/*** Get post and payment values ***/
  	
	  	$values = $this->getRequest()->getPost();
	  	$paymentType = $values['payment_type'];
	  	
  	/*** End payment values ***/
  	
	
  	/*** Create Authorize subscription object ***/
		  	$subscription = new AuthorizeNet_Subscription;
		  	$subscription->name = 'package_title';
		  	$subscription->totalOccurrences = "9999";
		  	if($package->recurrence_type == 'month'){
		  		$subscription->intervalLength          = '1';
		  		$subscription->intervalUnit            = "months";
		  	}
		  	elseif($package->recurrence_type == 'year'){
		  		$subscription->intervalLength          = '12';
		  		$subscription->intervalUnit            = "months";
		  	}
		  	elseif($package->recurrence_type == 'week'){
		  		$subscription->intervalLength          = '7';
		  		$subscription->intervalUnit            = "days";
		  	}
		  	elseif($package->recurrence_type == 'forever'){
		  		$subscription->intervalLength          = '1';
		  		$subscription->intervalUnit            = "months";
		  		$subscription->totalOccurrences        = "1";
		  	}
		  	
		  	$subscription->startDate               = $todayDate->format('Y-m-d');
		  	$subscription->amount                  = '29';
		  	
		  	$billAddress = "";
		  	$billCity = "";
		  	$billState = "";
		  	$billZip = "";
		  	$billCountry = "";
		  	/*** Credit card details ***/
		  	if($paymentType == 'credit'){
		  		$expiryDateArray = explode('/',$values['expirationDate']);
		  		$expiryDate = '20'.$expiryDateArray[1].'-'.$expiryDateArray[0];
			  	$subscription->creditCardCardNumber    = $values["cardNumber"];
			  	$subscription->creditCardExpirationDate= $expiryDate;
			  	$subscription->creditCardCardCode      = $values['cardCode'];
			  	$subscription->billToFirstName         = $values["firstName"];
			  	$subscription->billToLastName          = $values["lastName"];
			  	
			  	$billAddress = $values['billToAddress'];
			  	$billCity = $values['billToCity'];
			  	$billCountry = $values['billToCountry'];
			  	$billState = $values['billToState'];
			  	$billZip = $values['billToZip'];
		  	}
		  	/*** End credit card details ***/
		  	
		  	/*** Bank account details ***/
			  	if($paymentType == 'bank'){
				  	$subscription->bankAccountAccountType   = 'account_demo_type;
				  	$subscription->bankAccountRoutingNumber = 'routingNumberdemo';
				  	$subscription->bankAccountAccountNumber = 'accountNumberdemo';
				  	$subscription->bankAccountNameOnAccount = 'nameOnAccountdemo';
				  	$subscription->bankAccountEcheckType    = 'WEB';
				  	$subscription->bankAccountBankName      = 'bankNamedemo';
				  	
				  	$billAddress = 'demoebillAddress';
				  	$billCity = 'demoebillToCity';
				  	$billCountry = 'ebillCountrydemo';
				  	$billState = 'ebillStatedemo';
				  	$billZip = 'ebillToZipdemo';
			  	}  	
		  	/*** end bank account details***/
			  	
			  	
	      /*** Customer Information ***/
                 $subscription->customerEmail = '[email protected]';
                 $subscription->customerId   = '121342';
                 $subscription->customerPhoneNumber = $phone1;
                 $subscription->billToAddress = $billAddress;
                 $subscription->billToCity = $billCity;
                 $subscription->billToCountry = $billCountry;
                 $subscription->billToFirstName = $userInfo['f_name'];
                 $subscription->billToLastName = $userInfo['l_name'];
                 $subscription->billToState = $billState;
                 $subscription->billToZip = $billZip;
                 $subscription->orderInvoiceNumber = time();
              /*** End Customer Information  ***/  	
	/*** End authorize subscription object creation ***/		  	
  	
	/*** Create the subscription. ***/
	  	$request = new AuthorizeNetARB;
	  	$response = $request->createSubscription($subscription);
	  	$subscription_id = $response->getSubscriptionId();
	/*** End subscription create ***/  	
  
	if($response->getResultCode() == "Ok"){
  		$status = 1;
  	}
  	else{
  		$status = 0;
  	}
  	
  }
}
?>  
  	

We need two library files AuthorizeNetTypes.php and AuthorizeNetARB.php, which you can found in PHP sdk folder. We added this files at top of our class. Then we create a class AuthorizeTest which have a function Subcription. In subcription function we used demo info for user data. We assume that payment info will be getting through Post Action, so we save post values in $values variable.

We check payment type and payment recurrence type and set values accordingly. Then we create request object of AuhtorizeNetARB and using this object we call create subscription method. We save the response in a variable and check whether result code is OK or not. There are many types of result code supported by authorize and you can get details available in Api. We get subscription id in response that can be save in database for further interaction with authorize.net e.g. subscription update, cancel subscription.

Conclusion

Hence by using above method we can integrate with authorize.net and can create, update or cancel subscription at authorize.net server using api functions. Feel free to contact us in case you have any queries.

Integrate Authorize.net payment gateway with SocialEngine was last modified: April 11th, 2016 by Dinesh Kumar