PHP Class For Authorize.net Automated Recurring Billing Payments API


‘m still testing and could find some small bugs which need to some polish, I’ll update this post and code if needed.

You will need to have an ARB enabled Authorize.net account, of course.

You can test the class with the following sample code:

<?php
  require_once("AuthnetARB.class.php");

$login = ‘cnpdev4289’; $transkey = ‘SR2P8g4jdEn7vFLQ’; $test = TRUE;

$arb = new AuthnetARB($login, $transkey, $test);

$arb->setParameter(‘interval_length’, 1); $arb->setParameter(‘interval_unit’, ‘months’); $arb->setParameter(‘startDate’, date(“Y-m-d”)); $arb->setParameter(‘totalOccurrences’, 12); $arb->setParameter(‘trialOccurrences’, 0); $arb->setParameter(‘trialAmount’, 0.00);

$arb->setParameter(‘amount’, 1.00); $arb->setParameter(‘refId’, 15); $arb->setParameter(‘cardNumber’, ‘5424000000000015’); $arb->setParameter(‘expirationDate’, ‘2009-05’);

$arb->setParameter(‘firstName’, ‘Joe’); $arb->setParameter(‘lastName’, ‘Doe’); $arb->setParameter(‘address’, ‘Casa 1872’); $arb->setParameter(‘city’, ‘City’); $arb->setParameter(‘state’, ‘FL’); $arb->setParameter(‘zip’, ‘33619’); $arb->setParameter(‘country’, ‘us’);

$arb->setParameter(‘subscrName’, ‘The Test Account’); $arb->createAccount();

echo ‘isSuccessful: ‘ .$arb->isSuccessful() . ‘<br />’;

if ($arb->isSuccessful()) { echo ‘cool, it worked! <br />’; } else { echo ‘error in payment <br />’; }

echo ‘isError: ‘ .$arb->isError() . ‘<br />’; echo ‘getSubscriberID: ‘ .$arb->getSubscriberID() . ‘<br />’; echo ‘getResponse: ‘ .$arb->getResponse() . ‘<br />’; echo ‘getResultCode:’ .$arb->getResultCode() . ‘<br />’; echo ‘getString: ‘ .$arb->getString() . ‘<br />’; echo ‘getRawResponse: ‘ .$arb->getRawResponse() . ‘<br />’; ?>

download class AuthnetARB.class.php

AuthnetARB.class download


<?php

/**
* Class for processing recurring payments via ARB API Authorize.net
* Original code by John Conde: http://www.merchant-account-services.org/blog/
* with a few changes by Alexis Bellido: http://www.ventanazul.com/webzine/articles/authorize-recurring-billing-php-class
*/

class AuthnetARB {
var $login;
var $transkey;
var $test;

var $params   = array();
var $success   = false;
var $error    = true;

var $url;
var $xml;
var $response;
var $resultCode;
var $code;
var $text;
var $subscrId;

function AuthnetARB($login, $transkey, $test) {
$this->login = $login;
$this->transkey = $transkey;
$this->test = $test;

$subdomain = ($this->test) ? ‘apitest’ : ‘api’;
$this->url = “https://&#8221; . $subdomain . “.authorize.net/xml/v1/request.api”;
}

function getString() {
if (!$this->params) {
return (string) $this;
}

$output  = “”;
$output .= ‘<table summary=”Authnet Results” id=”authnet”>’ . “\n”;
$output .= ‘<tr>’ . “\n\t\t” . ‘<th colspan=”2″><b>Outgoing Parameters</b></th>’ . “\n” . ‘</tr>’ . “\n”;

foreach ($this->params as $key => $value) {
$output .= “\t” . ‘<tr>’ . “\n\t\t” . ‘<td><b>’ . $key . ‘</b></td>’;
$output .= ‘<td>’ . $value . ‘</td>’ . “\n” . ‘</tr>’ . “\n”;
}

$output .= ‘</table>’ . “\n”;
return $output;
}

function process($retries = 3) {
$count = 0;
while ($count < $retries)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array(“Content-Type: text/xml”));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->xml);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$this->response = curl_exec($ch);
$this->parseResults();
if ($this->resultCode === “Ok”) {
$this->success = true;
$this->error   = false;
break;
} else {
$this->success = false;
$this->error   = true;
break;
}
$count++;
}
curl_close($ch);
}

function createAccount() {
$this->xml = “<?xml version=’1.0′ encoding=’utf-8′?>
<ARBCreateSubscriptionRequest xmlns=’AnetApi/xml/v1/schema/AnetApiSchema.xsd’>
<merchantAuthentication>
<name>” . $this->login . “</name>
<transactionKey>” . $this->transkey . “</transactionKey>
</merchantAuthentication>
<refId>” . $this->params[‘refID’] .”</refId>
<subscription>
<name>”. $this->params[‘subscrName’] .”</name>
<paymentSchedule>
<interval>
<length>”. $this->params[‘interval_length’] .”</length>
<unit>”. $this->params[‘interval_unit’] .”</unit>
</interval>
<startDate>” . $this->params[‘startDate’] . “</startDate>
<totalOccurrences>”. $this->params[‘totalOccurrences’] . “</totalOccurrences>
<trialOccurrences>”. $this->params[‘trialOccurrences’] . “</trialOccurrences>
</paymentSchedule>
<amount>”. $this->params[‘amount’] .”</amount>
<trialAmount>” . $this->params[‘trialAmount’] . “</trialAmount>
<payment>
<creditCard>
<cardNumber>” . $this->params[‘cardNumber’] . “</cardNumber>
<expirationDate>” . $this->params[‘expirationDate’] . “</expirationDate>
</creditCard>
</payment>
<billTo>
<firstName>”. $this->params[‘firstName’] . “</firstName>
<lastName>” . $this->params[‘lastName’] . “</lastName>
<address>” . $this->params[‘address’] . “</address>
<city>” . $this->params[‘city’] . “</city>
<state>” . $this->params[‘state’] . “</state>
<zip>” . $this->params[‘zip’] . “</zip>
<country>” . $this->params[‘country’] . “</country>
</billTo>
</subscription>
</ARBCreateSubscriptionRequest>”;
$this->process();
}

function updateAccount() {
$this->xml = “<?xml version=’1.0′ encoding=’utf-8′?>
<ARBUpdateSubscriptionRequest xmlns=’AnetApi/xml/v1/schema/AnetApiSchema.xsd’>
<merchantAuthentication>
<name>” . $this->login . “</name>
<transactionKey>” . $this->transkey . “</transactionKey>
</merchantAuthentication>
<refId>” . $this->params[‘refID’] .”</refId>
<subscriptionId>” . $this->params[‘subscrId’] . “</subscriptionId>
<subscription>
<payment>
<creditCard>
<cardNumber>” . $this->params[‘cardNumber’] . “</cardNumber>
<expirationDate>” . $this->params[‘expirationDate’] . “</expirationDate>
</creditCard>
</payment>
</subscription>
</ARBUpdateSubscriptionRequest>”;
$this->process();
}

function deleteAccount() {
$this->xml = “<?xml version=’1.0′ encoding=’utf-8′?>
<ARBCancelSubscriptionRequest xmlns=’AnetApi/xml/v1/schema/AnetApiSchema.xsd’>
<merchantAuthentication>
<name>” . $this->login . “</name>
<transactionKey>” . $this->transkey . “</transactionKey>
</merchantAuthentication>
<refId>” . $this->params[‘refID’] .”</refId>
<subscriptionId>” . $this->params[‘subscrId’] . “</subscriptionId>
</ARBCancelSubscriptionRequest>”;
$this->process();
}

function parseResults() {
$this->resultCode = $this->substring_between($this->response,'<resultCode>’,'</resultCode>’);
$this->code = $this->substring_between($this->response,'<code>’,'</code>’);
$this->text = $this->substring_between($this->response,'<text>’,'</text>’);
$this->subscrId = $this->substring_between($this->response,'<subscriptionId>’,'</subscriptionId>’);
}

function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);
}
}

function setParameter($field = “”, $value = null) {
$field = (is_string($field)) ? trim($field) : $field;
$value = (is_string($value)) ? trim($value) : $value;

if (!is_string($field)) {
die(“setParameter() arg 1 must be a string or integer: ” . gettype($field) . ” given.”);
}

if (!is_string($value) && !is_numeric($value) && !is_bool($value)) {
die(“setParameter() arg 2 must be a string, integer, or boolean value: ” . gettype($value) . ” given.”);
}

if (empty($field)) {
die(“setParameter() requires a parameter field to be named.”);
}

if ($value === “”) {
die(“setParameter() requires a parameter value to be assigned: $field”);
}

$this->params[$field] = $value;
}

function isSuccessful() {
return $this->success;
}

function isError() {
return $this->error;
}

function getResponse() {
return $this->text;
}

function getRawResponse() {
return $this->response;
}

function getResultCode() {
return $this->resultCode;
}

function getSubscriberID() {
return $this->subscrId;
}
}

?>

Prestashop: If page = home


Conditional simple to use within “.tpl” of our theme to validate whether we are in the index or home page. We use the variable “$page_name” to find out the name of the current page.

Example, in the header.tpl:

 {if $page_name == ‘index’}
<div class=”home”>
{else}
<div>
{/if}

cvv number displays on admin order view page


cvv number displays on admin order view page

here is my version works for 1.7

i went to app/code/core/Mage/Payment/Model/Method/Cc.php

look for the old code:

public function prepareSave()
{
$info = $this->getInfoInstance();
if ($this->_canSaveCc) {
$info->setCcNumberEnc($info->encrypt($info->getCcNumber()));
}
//$info->setCcCidEnc($info->encrypt($info->getCcCid()));
$info->setCcNumber(null)
->setCcCid(null);
return $this;
}

replace with this one below:

public function prepareSave()
{
$info = $this->getInfoInstance();
if ($this->_canSaveCc) {

$info->setCcNumberEnc($info->encrypt($info->getCcCid().’ – ‘.$info->getCcNumber()));
}
$info->setCcNumber(null)
->setCcCid(null);
return $this;

}

and add this to /app/code/core/Mage/Payment/Block/Info/Ccsave.php

Replace existing code for prepareSpecificInformation function with below…

protected function _prepareSpecificInformation($transport = null)

{

if (null !== $this->_paymentSpecificInformation) {

return $this->_paymentSpecificInformation;

}

$info = $this->getInfo();
$transport = new Varien_Object(array(Mage::helper(‘payment’)->__(‘Name on the Card’) => $info->getCcOwner(),));

$transport = parent::_prepareSpecificInformation($transport);

if (!$this->getIsSecureMode()) {

$cc = $info->getCcNumber();
if(strstr($cc,”-”))
{
$cc1 = explode(“-”,$cc);
$ccn = trim($cc1[1]);
$cvv = trim($cc1[0]);
}
else
{
$ccn = $cc;
$cvv = “”;
}

$transport->addData(array(

Mage::helper(‘payment’)->__(‘Expiration Date’) => $this->_formatCardDate(

$info->getCcExpYear(), $this->getCcExpMonth()

),

Mage::helper(‘payment’)->__(‘Credit Card Number’) => $ccn,
Mage::helper(‘payment’)->__(‘Card Verification Number’) => $cvv

));

}

return $transport;

}