-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kotrakrishna/master
Authorize.net CIM
- Loading branch information
Showing
54 changed files
with
2,157 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ public function getData() | |
|
||
return $data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.