Skip to content

Commit 2d0b1de

Browse files
authored
Merge pull request #79 from eileenmcnaughton/anet_query
Add actions for querying Authorize.net - subscription plans, batches, and transactions.
2 parents ff9dc9d + 0388163 commit 2d0b1de

17 files changed

+1032
-0
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# CiviCRM editor configuration normalization
2+
# @see http://editorconfig.org/
3+
4+
# This is the top-most .editorconfig file; do not search in parent directories.
5+
root = true
6+
7+
# All files.
8+
[*]
9+
end_of_line = LF
10+
indent_style = space
11+
indent_size = 4
12+
charset = utf-8
13+
trim_trailing_whitespace = true
14+
insert_final_newline = true

src/AIMGateway.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Omnipay\AuthorizeNet\Message\AIMAuthorizeRequest;
66
use Omnipay\AuthorizeNet\Message\AIMCaptureRequest;
7+
use Omnipay\AuthorizeNet\Message\AIMPaymentPlanQueryResponse;
78
use Omnipay\AuthorizeNet\Message\AIMPurchaseRequest;
9+
use Omnipay\AuthorizeNet\Message\QueryRequest;
810
use Omnipay\AuthorizeNet\Message\AIMRefundRequest;
911
use Omnipay\AuthorizeNet\Message\AIMVoidRequest;
1012
use Omnipay\Common\AbstractGateway;
@@ -152,4 +154,58 @@ public function refund(array $parameters = array())
152154
{
153155
return $this->createRequest('\Omnipay\AuthorizeNet\Message\AIMRefundRequest', $parameters);
154156
}
157+
158+
/**
159+
* @param array $parameters
160+
* @return AIMPaymentPlansQueryRequest
161+
*/
162+
public function paymentPlansQuery(array $parameters = array())
163+
{
164+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\Query\AIMPaymentPlansQueryRequest', $parameters);
165+
}
166+
167+
/**
168+
* @param array $parameters
169+
* @return AIMPaymentPlanQueryResponse
170+
*/
171+
public function paymentPlanQuery(array $parameters = array())
172+
{
173+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\Query\AIMPaymentPlanQueryRequest', $parameters);
174+
}
175+
176+
/**
177+
* @param array $parameters
178+
* @return QueryResponse
179+
*/
180+
public function query(array $parameters = array())
181+
{
182+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryRequest', $parameters);
183+
}
184+
185+
/**
186+
* @param array $parameters
187+
* @return QueryBatchResponse
188+
*/
189+
public function queryBatch(array $parameters = array())
190+
{
191+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryBatchRequest', $parameters);
192+
}
193+
194+
/**
195+
* @param array $parameters
196+
* @return QueryBatchDetailResponse
197+
*/
198+
public function queryBatchDetail(array $parameters = array())
199+
{
200+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryBatchDetailRequest', $parameters);
201+
}
202+
203+
/**
204+
* @param array $parameters
205+
* @return QueryDetailResponse
206+
*/
207+
public function queryDetail(array $parameters = array())
208+
{
209+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryDetailRequest', $parameters);
210+
}
155211
}

src/Message/AIMAbstractRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function getHashSecret()
6060
{
6161
return $this->getParameter('hashSecret');
6262
}
63+
6364
public function setHashSecret($value)
6465
{
6566
return $this->setParameter('hashSecret', $value);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Omnipay\AuthorizeNet\Message\Query;
4+
5+
/**
6+
* Authorize.Net AIM Abstract Request
7+
*/
8+
abstract class AIMAbstractQueryRequest extends AIMAbstractRequest
9+
{
10+
protected $limit = 1000;
11+
protected $offset = 1;
12+
13+
/**
14+
* Get Limit.
15+
*
16+
* @return int
17+
*/
18+
public function getLimit()
19+
{
20+
return $this->limit;
21+
}
22+
23+
/**
24+
* Set Limit.
25+
*
26+
* @param int $limit
27+
*/
28+
public function setLimit($limit)
29+
{
30+
$this->limit = $limit;
31+
}
32+
33+
/**
34+
* Get offset.
35+
*
36+
* @return int
37+
*/
38+
public function getOffset()
39+
{
40+
return $this->offset;
41+
}
42+
43+
/**
44+
* Set offset.
45+
*
46+
* @param int $offset
47+
*/
48+
public function setOffset($offset)
49+
{
50+
$this->offset = $offset;
51+
}
52+
53+
/**
54+
* Get data to send.
55+
*/
56+
public function getData()
57+
{
58+
$data = $this->getBaseData();
59+
return $data;
60+
}
61+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Omnipay\AuthorizeNet\Message\Query;
4+
5+
use Omnipay\Common\CreditCard;
6+
7+
/**
8+
* Authorize.Net AIM Authorize Request
9+
*/
10+
class AIMPaymentPlanQueryRequest extends AIMAbstractQueryRequest
11+
{
12+
protected $action = '';
13+
protected $requestType = 'ARBGetSubscriptionRequest';
14+
protected $recurringReference;
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getRecurringReference()
20+
{
21+
return $this->recurringReference;
22+
}
23+
24+
/**
25+
* @param string $recurringReference
26+
*/
27+
public function setRecurringReference($recurringReference)
28+
{
29+
$this->recurringReference = $recurringReference;
30+
}
31+
32+
/**
33+
* Get data to send.
34+
*/
35+
public function getData()
36+
{
37+
$data = $this->getBaseData();
38+
$data->subscriptionId = $this->getRecurringReference();
39+
return $data;
40+
}
41+
42+
protected function addTransactionType(\SimpleXMLElement $data)
43+
{
44+
}
45+
46+
public function sendData($data)
47+
{
48+
$headers = array('Content-Type' => 'text/xml; charset=utf-8');
49+
$data = $data->saveXml();
50+
$httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $data)->send();
51+
52+
return $this->response = new AIMPaymentPlanQueryResponse($this, $httpResponse->getBody());
53+
}
54+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Omnipay\AuthorizeNet\Message\Query;
4+
5+
use Omnipay\AuthorizeNet\Model\CardReference;
6+
use Omnipay\AuthorizeNet\Model\TransactionReference;
7+
use Omnipay\Common\Exception\InvalidResponseException;
8+
use Omnipay\Common\Message\AbstractRequest;
9+
use Omnipay\Common\Message\AbstractResponse;
10+
use Omnipay\Omnipay;
11+
12+
/**
13+
* Authorize.Net AIM Response
14+
*/
15+
class AIMPaymentPlanQueryResponse extends AbstractQueryResponse
16+
{
17+
protected $subscription;
18+
protected $profile;
19+
20+
public function __construct(AbstractRequest $request, $data)
21+
{
22+
// Strip out the xmlns junk so that PHP can parse the XML
23+
$xml = preg_replace('/<ARBGetSubscriptionRequest[^>]+>/', '<ARBGetSubscriptionRequest>', (string)$data);
24+
try {
25+
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOWARNING);
26+
} catch (\Exception $e) {
27+
throw new InvalidResponseException();
28+
}
29+
30+
if (!$xml) {
31+
throw new InvalidResponseException();
32+
}
33+
34+
parent::__construct($request, $xml);
35+
$result = $this->xml2array($this->data->subscription, true);
36+
$this->subscription = $result['subscription'][0];
37+
}
38+
39+
public function isSuccessful()
40+
{
41+
return 1 === $this->getResultCode();
42+
}
43+
44+
public function getData()
45+
{
46+
return $this->subscription;
47+
}
48+
49+
public function getRecurStartDate()
50+
{
51+
return $this->subscription['paymentSchedule']['interval']['startDate'];
52+
}
53+
54+
public function getRecurInstallmentLimit()
55+
{
56+
return $this->subscription['paymentSchedule']['interval']['totalOccurrences'];
57+
}
58+
59+
public function getRecurrenceInterval()
60+
{
61+
return $this->subscription['paymentSchedule']['interval'][0]['length'];
62+
}
63+
64+
public function getRecurAmount()
65+
{
66+
return $this->subscription['amount'];
67+
}
68+
69+
public function getRecurReference()
70+
{
71+
return $this->subscription;
72+
}
73+
74+
public function getContactReference()
75+
{
76+
$profileID = $this->subscription['profile'][0]['customerProfileId'];
77+
$gateway = $gateway = Omnipay::create('AuthorizeNet_CIM');
78+
$gateway->setApiLoginId($this->request->getApiLoginId());
79+
$gateway->setHashSecret($this->request->getHashSecret());
80+
$gateway->setTransactionKey($this->request->getTransactionKey());
81+
$data = array(
82+
'customerProfileId' => $profileID,
83+
'customerPaymentProfileId' =>
84+
$this->subscription['profile'][0]['paymentProfile'][0]['customerPaymentProfileId'],
85+
);
86+
$dataResponse = $gateway->getProfile($data)->send();
87+
return $dataResponse->getCustomerId();
88+
}
89+
90+
/**
91+
* @todo formalise options.
92+
*
93+
* @return mixed
94+
*/
95+
public function getRecurStatus()
96+
{
97+
return $this->subscription['paymentSchedule']['interval'][0]['status'];
98+
}
99+
100+
public function getRecurrenceUnit()
101+
{
102+
$interval = $this->subscription['paymentSchedule']['interval'][0]['unit'];
103+
$options = array(
104+
'months' => 'month',
105+
);
106+
return $options[$interval];
107+
}
108+
109+
/**
110+
* http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/
111+
*
112+
* Convert a simpleXMLElement in to an array
113+
*
114+
* @todo this is duplicated from CIMAbstractResponse. Put somewhere shared.
115+
*
116+
* @param \SimpleXMLElement $xml
117+
*
118+
* @return array
119+
*/
120+
public function xml2array(\SimpleXMLElement $xml)
121+
{
122+
$arr = array();
123+
foreach ($xml as $element) {
124+
$tag = $element->getName();
125+
$e = get_object_vars($element);
126+
if (!empty($e)) {
127+
$arr[$tag][] = $element instanceof \SimpleXMLElement ? $this->xml2array($element) : $e;
128+
} else {
129+
$arr[$tag] = trim($element);
130+
}
131+
}
132+
133+
return $arr;
134+
}
135+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Omnipay\AuthorizeNet\Message\Query;
4+
5+
use Omnipay\Common\CreditCard;
6+
7+
/**
8+
* Authorize.Net AIM Authorize Request
9+
*/
10+
class AIMPaymentPlansQueryRequest extends AIMAbstractRequest
11+
{
12+
protected $action = '';
13+
protected $requestType = 'ARBGetSubscriptionListRequest';
14+
protected $limit = 1000;
15+
protected $offset = 1;
16+
17+
/**
18+
* Get Limit.
19+
*
20+
* @return int
21+
*/
22+
public function getLimit()
23+
{
24+
return $this->limit;
25+
}
26+
27+
/**
28+
* Get data to send.
29+
*/
30+
public function getData()
31+
{
32+
$data = $this->getBaseData();
33+
$data->searchType = 'subscriptionActive';
34+
$data->sorting->orderBy = 'id';
35+
$data->sorting->orderDescending = true;
36+
$data->paging->limit = $this->getLimit();
37+
$data->paging->offset = $this->getOffset();
38+
return $data;
39+
}
40+
41+
protected function addTransactionType(\SimpleXMLElement $data)
42+
{
43+
}
44+
45+
public function sendData($data)
46+
{
47+
$headers = array('Content-Type' => 'text/xml; charset=utf-8');
48+
$data = $data->saveXml();
49+
$httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $data)->send();
50+
51+
return $this->response = new AIMPaymentPlansQueryResponse($this, $httpResponse->getBody());
52+
}
53+
}

0 commit comments

Comments
 (0)