Skip to content

Commit b4d3d09

Browse files
committed
Consolidate common functionality between AIM and CIM
1 parent f4fc378 commit b4d3d09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+757
-649
lines changed

src/CIMGateway.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
class CIMGateway extends AIMGateway
1111
{
12-
public function getDefaultParameters()
13-
{
14-
$params = parent::getDefaultParameters();
15-
$params['forceCardUpdate'] = false;
16-
$params['defaultBillTo'] = array(array());
17-
return $params;
18-
}
19-
2012
public function getName()
2113
{
2214
return 'Authorize.Net CIM';

src/Message/AIMAbstractRequest.php

Lines changed: 102 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Omnipay\AuthorizeNet\Message;
44

5+
use Omnipay\AuthorizeNet\Model\CardReference;
6+
use Omnipay\AuthorizeNet\Model\TransactionReference;
57
use Omnipay\Common\CreditCard;
68
use Omnipay\Common\Exception\InvalidRequestException;
79
use Omnipay\Common\Message\AbstractRequest;
@@ -11,6 +13,7 @@
1113
*/
1214
abstract class AIMAbstractRequest extends AbstractRequest
1315
{
16+
protected $requestType = 'createTransactionRequest';
1417
protected $action = null;
1518

1619
public function getApiLoginId()
@@ -72,16 +75,84 @@ private function getDuplicateWindow()
7275
return $this->getParameter('duplicateWindow'); // Maps x_duplicate_window
7376
}
7477

75-
protected function addExtraOptions(\SimpleXMLElement $data)
78+
public function getLiveEndpoint()
7679
{
77-
if (!is_null($this->getDuplicateWindow())) {
78-
$extraOptions = $data->addChild('extraOptions');
79-
$node = dom_import_simplexml($extraOptions);
80-
$nodeOwner = $node->ownerDocument;
81-
$duplicateWindowStr = sprintf("x_duplicate_window=%s", $this->getDuplicateWindow());
82-
$node->appendChild($nodeOwner->createCDATASection($duplicateWindowStr));
80+
return $this->getParameter('liveEndpoint');
81+
}
82+
83+
public function setLiveEndpoint($value)
84+
{
85+
return $this->setParameter('liveEndpoint', $value);
86+
}
87+
88+
public function getDeveloperEndpoint()
89+
{
90+
return $this->getParameter('developerEndpoint');
91+
}
92+
93+
public function setDeveloperEndpoint($value)
94+
{
95+
return $this->setParameter('developerEndpoint', $value);
96+
}
97+
98+
public function getEndpoint()
99+
{
100+
return $this->getDeveloperMode() ? $this->getDeveloperEndpoint() : $this->getLiveEndpoint();
101+
}
102+
103+
/**
104+
* @return TransactionReference
105+
*/
106+
public function getTransactionReference()
107+
{
108+
return $this->getParameter('transactionReference');
109+
}
110+
111+
public function setTransactionReference($value)
112+
{
113+
if (substr($value, 0, 1) === '{') {
114+
// Value is a complex key containing the transaction ID and other properties
115+
$transactionRef = new TransactionReference($value);
116+
} else {
117+
// Value just contains the transaction ID
118+
$transactionRef = new TransactionReference();
119+
$transactionRef->setTransId($value);
83120
}
84-
return $data;
121+
return $this->setParameter('transactionReference', $transactionRef);
122+
}
123+
124+
/**
125+
* @param string|CardReference $value
126+
* @return AbstractRequest
127+
*/
128+
public function setCardReference($value)
129+
{
130+
if (!($value instanceof CardReference)) {
131+
$value = new CardReference($value);
132+
}
133+
return parent::setCardReference($value);
134+
}
135+
136+
/**
137+
* @param bool $serialize Determines whether the return value will be a string or object
138+
* @return string|CardReference
139+
*/
140+
public function getCardReference($serialize = true)
141+
{
142+
$value = parent::getCardReference();
143+
if ($serialize) {
144+
$value = (string)$value;
145+
}
146+
return $value;
147+
}
148+
149+
public function sendData($data)
150+
{
151+
$headers = array('Content-Type' => 'text/xml; charset=utf-8');
152+
$data = $data->saveXml();
153+
$httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $data)->send();
154+
155+
return $this->response = new AIMResponse($this, $httpResponse->getBody());
85156
}
86157

87158
/**
@@ -90,27 +161,35 @@ protected function addExtraOptions(\SimpleXMLElement $data)
90161
*/
91162
public function getBaseData()
92163
{
93-
$data = new \SimpleXMLElement('<createTransactionRequest/>');
164+
$data = new \SimpleXMLElement('<' . $this->requestType . '/>');
94165
$data->addAttribute('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');
166+
$this->addAuthentication($data);
167+
$this->addReferenceId($data);
168+
$this->addTransactionType($data);
169+
return $data;
170+
}
95171

96-
// Credentials
172+
protected function addAuthentication(\SimpleXMLElement $data)
173+
{
97174
$data->merchantAuthentication->name = $this->getApiLoginId();
98175
$data->merchantAuthentication->transactionKey = $this->getTransactionKey();
176+
}
99177

100-
// User-assigned transaction ID
178+
protected function addReferenceId(\SimpleXMLElement $data)
179+
{
101180
$txnId = $this->getTransactionId();
102181
if (!empty($txnId)) {
103182
$data->refId = $this->getTransactionId();
104183
}
184+
}
105185

106-
// Transaction type
186+
protected function addTransactionType(\SimpleXMLElement $data)
187+
{
107188
if (!$this->action) {
108189
// The extending class probably hasn't specified an "action"
109190
throw new InvalidRequestException();
110191
}
111192
$data->transactionRequest->transactionType = $this->action;
112-
113-
return $data;
114193
}
115194

116195
/**
@@ -165,37 +244,15 @@ protected function addTestModeSetting(\SimpleXMLElement $data)
165244
return $data;
166245
}
167246

168-
public function sendData($data)
169-
{
170-
$headers = array('Content-Type' => 'text/xml; charset=utf-8');
171-
$data = $data->saveXml();
172-
$httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $data)->send();
173-
174-
return $this->response = new AIMResponse($this, $httpResponse->getBody());
175-
}
176-
177-
public function getLiveEndpoint()
178-
{
179-
return $this->getParameter('liveEndpoint');
180-
}
181-
182-
public function setLiveEndpoint($value)
183-
{
184-
return $this->setParameter('liveEndpoint', $value);
185-
}
186-
187-
public function getDeveloperEndpoint()
188-
{
189-
return $this->getParameter('developerEndpoint');
190-
}
191-
192-
public function setDeveloperEndpoint($value)
193-
{
194-
return $this->setParameter('developerEndpoint', $value);
195-
}
196-
197-
public function getEndpoint()
247+
protected function addExtraOptions(\SimpleXMLElement $data)
198248
{
199-
return $this->getDeveloperMode() ? $this->getDeveloperEndpoint() : $this->getLiveEndpoint();
249+
if (!is_null($this->getDuplicateWindow())) {
250+
$extraOptions = $data->addChild('extraOptions');
251+
$node = dom_import_simplexml($extraOptions);
252+
$nodeOwner = $node->ownerDocument;
253+
$duplicateWindowStr = sprintf("x_duplicate_window=%s", $this->getDuplicateWindow());
254+
$node->appendChild($nodeOwner->createCDATASection($duplicateWindowStr));
255+
}
256+
return $data;
200257
}
201258
}

src/Message/AIMAuthorizeRequest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,34 @@ class AIMAuthorizeRequest extends AIMAbstractRequest
1313

1414
public function getData()
1515
{
16-
$this->validate('amount', 'card');
16+
$this->validate('amount');
17+
$data = $this->getBaseData();
18+
$data->transactionRequest->amount = $this->getAmount();
19+
$this->addPayment($data);
20+
$this->addCustomerIP($data);
21+
$this->addBillingData($data);
22+
$this->addTestModeSetting($data);
23+
$this->addExtraOptions($data);
1724

25+
return $data;
26+
}
27+
28+
protected function addPayment(\SimpleXMLElement $data)
29+
{
30+
$this->validate('card');
1831
/** @var CreditCard $card */
1932
$card = $this->getCard();
2033
$card->validate();
21-
22-
$data = $this->getBaseData();
23-
$data->transactionRequest->amount = $this->getAmount();
2434
$data->transactionRequest->payment->creditCard->cardNumber = $card->getNumber();
2535
$data->transactionRequest->payment->creditCard->expirationDate = $card->getExpiryDate('my');
2636
$data->transactionRequest->payment->creditCard->cardCode = $card->getCvv();
37+
}
38+
39+
protected function addCustomerIP(\SimpleXMLElement $data)
40+
{
2741
$ip = $this->getClientIp();
2842
if (!empty($ip)) {
2943
$data->transactionRequest->customerIP = $ip;
3044
}
31-
32-
$this->addBillingData($data);
33-
$this->addTestModeSetting($data);
34-
$this->addExtraOptions($data);
35-
36-
return $data;
3745
}
3846
}

src/Message/AIMCaptureRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function getData()
1515

1616
$data = $this->getBaseData();
1717
$data->transactionRequest->amount = $this->getAmount();
18-
$data->transactionRequest->refTransId = $this->getTransactionReference();
18+
$data->transactionRequest->refTransId = $this->getTransactionReference()->getTransId();
1919
$this->addTestModeSetting($data);
2020

2121
return $data;

src/Message/AIMRefundRequest.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,65 @@
22

33
namespace Omnipay\AuthorizeNet\Message;
44

5-
use Omnipay\Common\CreditCard;
6-
75
/**
86
* Authorize.net AIM Refund Request
97
*/
108
class AIMRefundRequest extends AIMAbstractRequest
119
{
1210
protected $action = 'refundTransaction';
1311

14-
public function getData()
12+
public function shouldVoidIfRefundFails()
13+
{
14+
return !!$this->getParameter('voidIfRefundFails');
15+
}
16+
17+
public function setVoidIfRefundFails($value)
1518
{
16-
$this->validate('transactionReference', 'amount', 'card');
19+
$this->setParameter('voidIfRefundFails', $value);
20+
}
1721

18-
/** @var CreditCard $card */
19-
$card = $this->getCard();
22+
public function getData()
23+
{
24+
$this->validate('transactionReference', 'amount');
2025

2126
$data = $this->getBaseData();
2227
$data->transactionRequest->amount = $this->getParameter('amount');
23-
$data->transactionRequest->payment->creditCard->cardNumber = $card->getNumber();
24-
$data->transactionRequest->payment->creditCard->expirationDate = $card->getExpiryDate('my');
25-
$data->transactionRequest->refTransId = $this->getTransactionReference();
28+
29+
$transactionRef = $this->getTransactionReference();
30+
if ($card = $transactionRef->getCard()) {
31+
$data->transactionRequest->payment->creditCard->cardNumber = $card->number;
32+
$data->transactionRequest->payment->creditCard->expirationDate = $card->expiry;
33+
} elseif ($cardReference = $transactionRef->getCardReference()) {
34+
$data->transactionRequest->profile->customerProfileId = $cardReference->getCustomerProfileId();
35+
$data->transactionRequest->profile->paymentProfile->paymentProfileId = $cardReference->getPaymentProfileId();
36+
} else {
37+
// Transaction reference only contains the transaction ID, so a card is required
38+
$this->validate('card');
39+
$card = $this->getCard();
40+
$data->transactionRequest->payment->creditCard->cardNumber = $card->getNumberLastFour();
41+
$data->transactionRequest->payment->creditCard->expirationDate = $card->getExpiryDate('my');
42+
}
43+
$data->transactionRequest->refTransId = $transactionRef->getTransId();
44+
2645
$this->addTestModeSetting($data);
2746

2847
return $data;
2948
}
49+
50+
public function send()
51+
{
52+
/** @var AIMResponse $response */
53+
$response = parent::send();
54+
55+
if (!$response->isSuccessful() && $this->shouldVoidIfRefundFails() &&
56+
$response->getReasonCode() == AIMResponse::ERROR_RESPONSE_CODE_CANNOT_ISSUE_CREDIT
57+
) {
58+
// This transaction has not yet been settled, hence cannot be refunded. But a void is possible.
59+
$voidRequest = new CIMVoidRequest($this->httpClient, $this->httpRequest);
60+
$voidRequest->initialize($this->getParameters());
61+
$response = $voidRequest->send();
62+
}
63+
64+
return $response;
65+
}
3066
}

0 commit comments

Comments
 (0)