Vous êtes sur la page 1sur 6

<?

php
class AuthnetARBException extends Exception {}
class AuthnetARB
{
const USE_PRODUCTION_SERVER = 0;
const USE_DEVELOPMENT_SERVER = 1;
const EXCEPTION_CURL = 10;
private $login;
private $transkey;
private $test;
private $params = array();
private $success = false;
private $error = true;
private $ch;
private $xml;
private $response;
private $resultCode;
private $code;
private $text;
private $subscrId;
public function __construct($login, $transkey, $test = self::USE_PRODUCTION_
SERVER)
{
$login = trim($login);
$transkey = trim($transkey);
if (empty($login) || empty($transkey))
{
throw new AuthnetARBException('You have not configured your ' . __CL
ASS__ . '() login credentials properly.');
}
$this->login = trim($login);
$this->transkey = trim($transkey);
$this->test = (bool) $test;
$subdomain = ($this->test) ? 'apitest' : 'api';
$this->url = 'https://' . $subdomain . '.authorize.net/xml/v1/request.ap
i';
}
public function __destruct()
{
if (isset($this->ch))
{
curl_close($this->ch);
}
}
public function __toString()
{
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";
if (!empty($this->xml))
{
$output .= 'XML: ';
$output .= htmlentities($this->xml);
}
return $output;
}
private function process()
{
$myFile = "testFile.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData =$this->xml;
fwrite($fh, $stringData);
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $this->url);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array('Content-Type: text/xml
'));
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->xml);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
$this->response = curl_exec($this->ch);
if($this->response)
{
$this->parseResults();
if ($this->resultCode === 'Ok')
{
$this->success = true;
$this->error = false;
}
else
{
$this->success = false;
$this->error = true;
}
curl_close($this->ch);
unset($this->ch);
return;
}
throw new AuthnetARBException('Connection error: ' . curl_error($this->c
h) . ' (' . curl_errno($this->ch) . ')', self::EXCEPTION_CURL);
}
public function createAccount($echeck = false)
{
$this->xml = "<?xml version='1.0' encoding='utf-8'?>
<ARBCreateSubscriptionRequest xm
lns='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
>
<interva
l>
<length>". $this->params['interval_length'] ."</length>
<unit>". $this->params['interval_unit'] ."</unit>
</interv
al>
<startDa
te>" . $this->params['startDate'] . "</startDate>
<totalOc
currences>". $this->params['totalOccurrences'] . "</totalOccurrences>
</paymentSchedul
e>
<amount>". $this
->params['amount'] ."</amount>";
if ($echeck)
{
$this->xml .= "
<payment>
<bankAcc
ount>
<accountType>". $this->params['accountType'] ."</accountType>
<routingNumber>". $this->params['routingNumber'] ."</routingNumber>
<accountNumber>". $this->params['accountNumber'] ."</accountNumber>
<nameOnAccount>". $this->params['nameOnAccount'] ."</nameOnAccount>
<bankName>". $this->params['bankName'] ."</bankName>
</bankAc
count>
</payment>";
}
else
{
$this->xml .= "
<payment>
<creditC
ard>
<cardNumber>" . $this->params['cardNumber'] . "</cardNumber>
<expirationDate>" . $this->params['expirationDate'] . "</expirationDate>
<cardCode>".$this->params['cardCode']."</cardCode>
</credit
Card>
</payment>";
}
$this->xml .= " <order>
<invoice
Number>" . $this->params['orderInvoiceNumber'] . "</invoiceNumber>
<descrip
tion>" . $this->params['orderDescription'] . "</description>
</order>
<customer>
<id>" .
$this->params['customerId'] . "</id>
<email>"
. $this->params['customerEmail'] . "</email>
<phoneNu
mber>" . $this->params['customerPhoneNumber'] . "</phoneNumber>
<faxNumb
er>" . $this->params['customerFaxNumber'] . "</faxNumber>
</customer>
<billTo>
<firstNa
me>". $this->params['firstName'] . "</firstName>
<lastNam
e>" . $this->params['lastName'] . "</lastName>
<company
>" . $this->params['company'] . "</company>
<address
>" . $this->params['address'] . "</address>
<city>"
. $this->params['city'] . "</city>
<state>"
. $this->params['state'] . "</state>
<zip>" .
$this->params['zip'] . "</zip>
</billTo>
</subscription>
</ARBCreateSubscriptionRequest>";
$this->process();
}
public 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 . "</transacti
onKey>
</merchantAuthentication>
<refId>" . $this->params['refID'] ."</refId>
<subscriptionId>" . $this->params['subscrId'] . "</sub
scriptionId>
<subscription>
<name>". $this->params['subscrName'] ."</name>
<amount>". $this->params['amount'] ."</amount>
<trialAmount>" . $this->params['trialAmount'] . "<
/trialAmount>
<payment>
<creditCard>
<cardNumber>" . $this->params['cardNumber'
] . "</cardNumber>
<expirationDate>" . $this->params['expirat
ionDate'] . "</expirationDate>
</creditCard>
</payment>
<billTo>
<firstName>". $this->params['firstName'] . "</
firstName>
<lastName>" . $this->params['lastName'] . "</l
astName>
<address>" . $this->params['address'] . "</add
ress>
<city>" . $this->params['city'] . "</city>
<state>" . $this->params['state'] . "</state>
<zip>" . $this->params['zip'] . "</zip>
</billTo>
</subscription>
</ARBUpdateSubscriptionRequest>";
$this->process();
}
public 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 . "</transacti
onKey>
</merchantAuthentication>
<refId>" . $this->params['refID'] ."</refId>
<subscriptionId>" . $this->params['subscrId'] . "</sub
scriptionId>
</ARBCancelSubscriptionRequest>";
$this->process();
}
private function parseResults()
{
$response = str_replace('xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"
', '', $this->response);
$xml = new SimpleXMLElement($response);
$this->refId = (string) $xml->refId;
$this->resultCode = (string) $xml->messages->resultCode;
$this->code = (string) $xml->messages->message->code;
$this->text = (string) $xml->messages->message->text;
$this->subscrId = (string) $xml->subscriptionId;
}
public function setParameter($field = '', $value = null)
{
$field = (is_string($field)) ? trim($field) : $field;
$value = (is_string($value)) ? trim($value) : $value;
if (!is_string($field))
{
throw new AuthnetARBException('setParameter() arg 1 must be a string
or integer: ' . gettype($field) . ' given.');
}
if (!is_string($value) && !is_numeric($value) && !is_bool($value))
{
throw new AuthnetARBException('setParameter() arg 2 must be a string
, integer, or boolean value: ' . gettype($value) . ' given.');
}
if (empty($field))
{
throw new AuthnetARBException('setParameter() requires a parameter f
ield to be named.');
}
if ($value === '')
{
throw new AuthnetARBException('setParameter() requires a parameter v
alue to be assigned: $field');
}
$this->params[$field] = $value;
}
public function isSuccessful()
{
return $this->success;
}
public function isError()
{
return $this->error;
}
public function getResponse()
{
return strip_tags($this->text);
}
public function getResponseCode()
{
return $this->code;
}
public function getSubscriberID()
{
return $this->subscrId;
}
public function getrefId()
{
return $this->refId;
}
}
?>

Vous aimerez peut-être aussi