Skip to content

Commit

Permalink
Merge pull request #1 from kotrakrishna/master
Browse files Browse the repository at this point in the history
Authorize.net CIM
  • Loading branch information
rushi committed Nov 13, 2014
2 parents 4426ff1 + 808e673 commit 5084452
Show file tree
Hide file tree
Showing 54 changed files with 2,157 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"name": "omnipay/authorizenet",
"type": "library",
"description": "Authorize.Net gateway for the Omnipay payment processing library",
Expand Down
2 changes: 1 addition & 1 deletion src/AIMGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Omnipay\AuthorizeNet\Message\AIMAuthorizeRequest;
use Omnipay\AuthorizeNet\Message\AIMCaptureRequest;
use Omnipay\AuthorizeNet\Message\AIMPurchaseRequest;
use Omnipay\AuthorizeNet\Message\AIMRefundRequest;
use Omnipay\AuthorizeNet\Message\AIMVoidRequest;
use Omnipay\Common\AbstractGateway;

Expand Down Expand Up @@ -102,5 +103,4 @@ public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\AIMRefundRequest', $parameters);
}

}
46 changes: 46 additions & 0 deletions src/CIMGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Omnipay\AuthorizeNet;

use Omnipay\AuthorizeNet\Message\CIMCreateCardRequest;

/**
* Authorize.Net CIM Class
*/
class CIMGateway extends AIMGateway
{
public function getName()
{
return 'Authorize.Net CIM';
}

/**
* @param array $parameters
*
* @return CIMCreateCardRequest
*/
public function createCard(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMCreateCardRequest', $parameters);
}

public function authorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMAuthorizeRequest', $parameters);
}

public function capture(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMCaptureRequest', $parameters);
}

public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMPurchaseRequest', $parameters);
}

public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMRefundRequest', $parameters);
}
}
2 changes: 1 addition & 1 deletion src/Message/AIMRefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public function getData()

return $data;
}
}
}
87 changes: 87 additions & 0 deletions src/Message/CIMAbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Omnipay\AuthorizeNet\Message;

use Omnipay\Common\Exception\InvalidRequestException;

/**
* Authorize.Net CIM Abstract Request
*/
abstract class CIMAbstractRequest extends AIMAbstractRequest
{
protected $xmlRootElement = null;

// Need the below setters and getters for accessing this data within createCardRequest.send
public function setEmail($value)
{
return $this->setParameter('email', $value);
}

public function getEmail()
{
return $this->getParameter('email');
}

public function setName($value)
{
return $this->setParameter('name', $value);
}

public function getName()
{
return $this->getParameter('name');
}

public function setCustomerProfileId($value)
{
return $this->setParameter('customerProfileId', $value);
}

public function getCustomerProfileId()
{
return $this->getParameter('customerProfileId');
}

public function setCustomerPaymentProfileId($value)
{
return $this->setParameter('customerPaymentProfileId', $value);
}

public function getCustomerPaymentProfileId()
{
return $this->getParameter('customerPaymentProfileId');
}

/**
* Flag to force update consumer payment profile if duplicate is found
*
* @param $value
*
* @return $this
*/
public function setForceCardUpdate($value)
{
return $this->setParameter('forceCardUpdate', $value);
}

public function getForceCardUpdate()
{
return $this->getParameter('forceCardUpdate');
}

/**
* @throws InvalidRequestException
* @return mixed|\SimpleXMLElement
*/
public function getBaseData()
{
$data = new \SimpleXMLElement("<" . $this->xmlRootElement . "/>");
$data->addAttribute('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');

// Credentials
$data->merchantAuthentication->name = $this->getApiLoginId();
$data->merchantAuthentication->transactionKey = $this->getTransactionKey();

return $data;
}
}
169 changes: 169 additions & 0 deletions src/Message/CIMAbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Omnipay\AuthorizeNet\Message;

use Omnipay\Common\CreditCard;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RequestInterface;

/**
* Authorize.Net CIM Response
*/
class CIMAbstractResponse extends AbstractResponse
{
protected $xmlRootElement = null;

public function __construct(RequestInterface $request, $data)
{
$this->request = $request;

// Check if this is an error response
$isError = strpos((string)$data, '<ErrorResponse');

$xmlRootElement = $isError !== false ? 'ErrorResponse' : $this->xmlRootElement;
// Strip out the xmlns junk so that PHP can parse the XML
$xml = preg_replace('/<' . $xmlRootElement . '[^>]+>/', '<' . $xmlRootElement . '>', (string)$data);

try {
$xml = simplexml_load_string($xml);
} catch(\Exception $e) {
throw new InvalidResponseException();
}

if (!$xml) {
throw new InvalidResponseException();
}

$this->data = $this->xml2array($xml);
}

public function isSuccessful()
{
return 1 === $this->getResultCode();
}

/**
* Overall status of the transaction. This field is also known as "Response Code" in Authorize.NET terminology.
*
* @return int 1 = Approved, 2 = Declined, 3 = Error, 4 = Held for Review
*/
public function getResultCode()
{
$result = (string)$this->data['messages'][0]['resultCode'][0];
switch ($result) {
case 'Ok':
return 1;
case 'Error':
return 3;
default:
return null;

}
}

/**
* A more detailed version of the Result/Response code.
*
* @return int|null
*/
public function getReasonCode()
{
$code = null;

if (isset($this->data['messages'])) {
// In case of a successful transaction, a "messages" element is present
$code = (string)$this->data['messages'][0]['message'][0]['code'][0];

}

return $code;
}

/**
* Text description of the status.
*
* @return string|null
*/
public function getMessage()
{
$message = null;

if (isset($this->data['messages'])) {
// In case of a successful transaction, a "messages" element is present
$message = (string)$this->data['messages'][0]['message'][0]['text'][0];

}

return $message;
}

public function getCardReference()
{
$cardRef = null;
if ($this->isSuccessful()) {
$data['customerProfileId'] = $this->getCustomerProfileId();
$data['customerPaymentProfileId'] = $this->getCustomerPaymentProfileId();
if (!empty($data['customerProfileId']) && !empty($data['customerPaymentProfileId'])) {
// For card reference both profileId and payment profileId should exist
$cardRef = json_encode($data);
}
}
return $cardRef;
}

/**
* http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/
*
* Convert a simpleXMLElement in to an array
*
* @param \SimpleXMLElement $xml
*
* @return array
*/
public function xml2array(\SimpleXMLElement $xml)
{
$arr = array();
foreach ($xml as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag][] = $element instanceof \SimpleXMLElement ? $this->xml2array($element) : $e;
} else {
$arr[$tag][] = trim($element);
}
}

return $arr;
}

public function getCustomerProfileId()
{
return null;
}

public function getCustomerPaymentProfileId()
{
return null;
}

/**
* Authorize net does not provide finger print and brand of the card hence we build the parameters from the
* requested card data
*
*/
public function augmentResponse()
{
if ($this->isSuccessful()) {
/** @var CreditCard $card */
$card = $this->request->getCard();
if ($card) {
$ccString = $card->getNumber() . $card->getExpiryMonth() . $card->getExpiryYear();
$this->data['hash'] = md5($ccString);
$this->data['brand'] = $card->getBrand();
$this->data['expiryYear'] = $card->getExpiryYear();
$this->data['expiryMonth'] = $card->getExpiryMonth();
}
}
}
}
61 changes: 61 additions & 0 deletions src/Message/CIMAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Omnipay\AuthorizeNet\Message;

/**
* Creates a Authorize only transaction request for the specified card
*/
class CIMAuthorizeRequest extends CIMAbstractRequest
{
protected $xmlRootElement = 'createCustomerProfileTransactionRequest';

protected $action = "profileTransAuthOnly";

public function getData()
{
$this->validate('cardReference', 'amount');

$data = $this->getBaseData();

$this->addTransactionData($data);
return $data;
}

/**
* Adds transaction data
*
* @param \SimpleXMLElement $data
*
* @return \SimpleXMLElement
*/
protected function addTransactionData(\SimpleXMLElement $data)
{
$transaction = $data->addChild('transaction');
$action = $transaction->addChild($this->action);
$action->amount = number_format($this->getAmount(), 2);

$cardRef = json_decode($this->getCardReference(), true);
$action->customerProfileId = $cardRef['customerProfileId'];
$action->customerPaymentProfileId = $cardRef['customerPaymentProfileId'];
if (!empty($cardRef['customerShippingAddressId'])) {
$action->customerShippingAddressId = $cardRef['customerShippingAddressId'];
}

$desc = $this->getDescription();
if (!empty($desc)) {
$action->order->description = $desc;
}

return $data;
}

public function sendData($data)
{
$headers = array('Content-Type' => 'text/xml; charset=utf-8');
$data = $data->saveXml();
$httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $data)->send();

return $this->response = new CIMResponse($this, $httpResponse->getBody());
}

}
Loading

0 comments on commit 5084452

Please sign in to comment.