Skip to content

Commit b01a2c0

Browse files
Towards audit functionality.
This stuff is gonna be the start of a long upstreaming journey so, it works, but I don't want to commit to keeping it working
1 parent 16cabe0 commit b01a2c0

18 files changed

+828
-101
lines changed

CRM/Core/Payment/OmnipayMultiProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ public function processPaymentNotification($params) {
857857

858858
public function queryPaymentPlans($params) {
859859
$this->createGateway($this->_paymentProcessor['id']);
860-
$response = $this->gateway->paymentPlanQuery($params)->send();
860+
$response = $this->gateway->paymentPlansQuery($params)->send();
861861
return $response->getPlanData();
862862
}
863863

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/**
4+
* Query Payment Processor recurring details.
5+
*
6+
* @param array $params
7+
*
8+
* @throws API_Exception
9+
* @throws CiviCRM_API3_Exception
10+
*
11+
* @return array
12+
* API Result array
13+
*/
14+
function civicrm_api3_payment_processor_getmissing($params) {
15+
$result = civicrm_api3('PaymentProcessor', 'query', $params);
16+
$missing = array();
17+
$contributionStatuses = civicrm_api3('Contribution', 'getoptions', array('field' => 'contribution_status_id'));
18+
$contributionStatusFilter = empty($params['contribution_status_id']) ? NULL : $contributionStatuses['values'][$params['contribution_status_id']];
19+
20+
foreach ($result['values'] as $payment) {
21+
if ($contributionStatusFilter && $payment['contribution_status_id'] != $contributionStatusFilter) {
22+
continue;
23+
}
24+
if (isset($params['is_recur'])) {
25+
if ($params['is_recur'] && empty($payment['recur_processor_reference'])) {
26+
continue;
27+
}
28+
if (($params['is_recur'] === FALSE || $params['is_recur'] === 0) && !empty($payment['recur_processor_reference'])) {
29+
continue;
30+
}
31+
}
32+
try {
33+
civicrm_api3('Contribution', 'getsingle', array('trxn_id' => $payment['trxn_id']));
34+
}
35+
catch (Exception $e) {
36+
$missing[$payment['trxn_id']] = $payment;
37+
}
38+
}
39+
return civicrm_api3_create_success($missing, $params);
40+
}
41+
42+
/**
43+
* Define metadata for payment_processor.tokenquery.
44+
*
45+
* @param array $params
46+
*/
47+
function _civicrm_api3_payment_processor_getmissing_spec(&$params) {
48+
$params['contribution_recur_id'] = array(
49+
'title' => 'Contribution Recur ID',
50+
'type' => CRM_Utils_Type::T_INT,
51+
);
52+
$params['contribution_status_id'] = array(
53+
'title' => 'Contribution Status ID',
54+
'type' => CRM_Utils_Type::T_INT,
55+
'pseudoconstant' => array(
56+
'optionGroupName' => 'contributionStatus',
57+
'keyColumn' => 'name',
58+
),
59+
);
60+
$params['is_recur'] = array(
61+
'title' => 'Is recurring?',
62+
'type' => CRM_Utils_Type::T_BOOLEAN,
63+
);
64+
}

api/v3/PaymentProcessor/Query.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,46 @@
1414
function civicrm_api3_payment_processor_query($params) {
1515
$processor = civicrm_api3('payment_processor', 'getsingle', array('id' => $params['payment_processor_id']));
1616
$responder = new CRM_Core_Payment_OmnipayMultiProcessor('live', $processor);
17-
$result = $responder->query($params);
18-
$count = 0;
19-
foreach ($result as $id => $row) {
20-
try {
21-
civicrm_api3('ContributionRecur', 'getsingle', array('processor_id' => $row['id'],
22-
));
17+
$gatewayParams = array();
18+
if (!empty($params['start_date_time'])) {
19+
$gatewayParams['startTimestamp'] = strtotime($params['start_date_time']);
20+
}
21+
if (!empty($params['end_date_time'])) {
22+
$gatewayParams['endTimestamp'] = strtotime($params['end_date_time']);
23+
}
24+
$result = $responder->query($gatewayParams);
25+
$payments = array();
26+
foreach ($result as $id => /*@var \Omnipay\Common\Message\QueryDetailResponse $response*/ $response) {
27+
$payment = array(
28+
'first_name' => $response->getFirstName(),
29+
'last_name' => $response->getLastName(),
30+
'invoice_id' => $response->getTransactionId(),
31+
'trxn_id' => $response->getTransactionReference(),
32+
'street_address' => $response->getBillingAddress1(),
33+
'postal_code' => $response->getBillingPostcode(),
34+
'state_province_id' => $response->getBillingState(),
35+
'country_id' => $response->getBillingCountry(),
36+
'city' => $response->getBillingCity(),
37+
'total_amount' => $response->getAmount(),
38+
'email' => $response->getEmail(),
39+
'contribution_source' => $response->getDescription(),
40+
'receive_date' => date('Y-m-d H:i:s', strtotime($response->getTransactionDate())),
41+
'settled_date' => date('Y-m-d H:i:s', strtotime($response->getSettlementDate())),
42+
);
43+
if ($response->isSuccessful()) {
44+
$payment['contribution_status_id'] = 'Completed';
2345
}
24-
catch (Exception $e) {
25-
$count++;
26-
echo $count . " " . ($id+1) . " " . $row['id'] . " " . $row['firstName'] . " " . $row['lastName'] . "\n";
46+
elseif ($response->getResultCode() == 2) {
47+
$payment['contribution_status_id'] = 'Failed';
2748
}
49+
if ($response->isRecurring()) {
50+
$payment['gateway_contact_reference'] = $response->getCustomerReference();
51+
$payment['recur_processor_reference'] = $response->getRecurringReference();
52+
}
53+
$payments[$payment['trxn_id']] = $payment;
2854
}
2955

30-
//return civicrm_api3_create_success($result, $params);
56+
return civicrm_api3_create_success($payments, $params);
3157
}
3258

3359
/**
@@ -40,4 +66,8 @@ function _civicrm_api3_payment_processor_query_spec(&$params) {
4066
'title' => 'Contribution Recur ID',
4167
'type' => CRM_Utils_Type::T_INT,
4268
);
69+
$params['is_recur'] = array(
70+
'title' => 'Is recurring?',
71+
'type' => CRM_Utils_Type::T_BOOLEAN,
72+
);
4373
}

vendor/omnipay/authorizenet/src/AIMGateway.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ public function refund(array $parameters = array())
153153
return $this->createRequest('\Omnipay\AuthorizeNet\Message\AIMRefundRequest', $parameters);
154154
}
155155

156+
/**
157+
* @param array $parameters
158+
* @return AIMPaymentPlansQueryResponse
159+
*/
160+
public function paymentPlansQuery(array $parameters = array())
161+
{
162+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\AIMPaymentPlansQueryRequest', $parameters);
163+
}
156164

157165
/**
158166
* @param array $parameters
@@ -180,4 +188,22 @@ public function queryBatch(array $parameters = array())
180188
{
181189
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryBatchRequest', $parameters);
182190
}
191+
192+
/**
193+
* @param array $parameters
194+
* @return QueryBatchDetailResponse
195+
*/
196+
public function queryBatchDetail(array $parameters = array())
197+
{
198+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryBatchDetailRequest', $parameters);
199+
}
200+
201+
/**
202+
* @param array $parameters
203+
* @return QueryDetailResponse
204+
*/
205+
public function queryDetail(array $parameters = array())
206+
{
207+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\QueryDetailRequest', $parameters);
208+
}
183209
}

vendor/omnipay/authorizenet/src/CIMGateway.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function getPaymentProfile(array $parameters = array())
5151
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMGetPaymentProfileRequest', $parameters);
5252
}
5353

54+
55+
public function getProfile(array $parameters = array())
56+
{
57+
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMGetProfileRequest', $parameters);
58+
}
59+
5460
public function deleteCard(array $parameters = array())
5561
{
5662
return $this->createRequest('\Omnipay\AuthorizeNet\Message\CIMDeletePaymentProfileRequest', $parameters);

vendor/omnipay/authorizenet/src/Message/AIMPaymentPlanQueryRequest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@
1010
class AIMPaymentPlanQueryRequest extends AIMAbstractRequest
1111
{
1212
protected $action = '';
13-
protected $requestType = 'ARBGetSubscriptionListRequest';
14-
protected $limit = 1000;
15-
protected $offset = 1;
13+
protected $requestType = 'ARBGetSubscriptionRequest';
14+
protected $recurringReference;
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getRecurringReference() {
20+
return $this->recurringReference;
21+
}
22+
23+
/**
24+
* @param string $recurringReference
25+
*/
26+
public function setRecurringReference($recurringReference) {
27+
$this->recurringReference = $recurringReference;
28+
}
1629

1730
/**
1831
* Get Limit.
@@ -56,11 +69,7 @@ public function setOffset($offset) {
5669
public function getData()
5770
{
5871
$data = $this->getBaseData();
59-
$data->searchType = 'subscriptionActive';
60-
$data->sorting->orderBy = 'id';
61-
$data->sorting->orderDescending = true;
62-
$data->paging->limit = $this->getLimit();
63-
$data->paging->offset = $this->getOffset();
72+
$data->subscriptionId = $this->getRecurringReference();
6473
return $data;
6574
}
6675

vendor/omnipay/authorizenet/src/Message/AIMPaymentPlanQueryResponse.php

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
use Omnipay\Common\Exception\InvalidResponseException;
88
use Omnipay\Common\Message\AbstractRequest;
99
use Omnipay\Common\Message\AbstractResponse;
10+
use Omnipay\Omnipay;
1011

1112
/**
1213
* Authorize.Net AIM Response
1314
*/
1415
class AIMPaymentPlanQueryResponse extends AbstractResponse
1516
{
16-
/**
17-
* For Error codes: @see https://developer.authorize.net/api/reference/responseCodes.html
18-
*/
19-
const ERROR_RESPONSE_CODE_CANNOT_ISSUE_CREDIT = 54;
17+
protected $subscription;
18+
protected $profile;
2019

2120
public function __construct(AbstractRequest $request, $data)
2221
{
2322
// Strip out the xmlns junk so that PHP can parse the XML
24-
$xml = preg_replace('/<ARBGetSubscriptionListRequest[^>]+>/', '<ARBGetSubscriptionListRequest>', (string)$data);
25-
23+
$xml = preg_replace('/<ARBGetSubscriptionRequest[^>]+>/', '<ARBGetSubscriptionRequest>', (string)$data);
2624
try {
2725
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOWARNING);
2826
} catch (\Exception $e) {
@@ -34,16 +32,69 @@ public function __construct(AbstractRequest $request, $data)
3432
}
3533

3634
parent::__construct($request, $xml);
35+
$result = $this->xml2array($this->data->subscription, TRUE);
36+
$this->subscription = $result['subscription'][0];
3737
}
3838

3939
public function isSuccessful()
4040
{
4141
return 1 === $this->getResultCode();
4242
}
4343

44-
public function getPlanData() {
45-
$result = $this->xml2array($this->data->subscriptionDetails, TRUE);
46-
return $result['subscriptionDetails'][0]['subscriptionDetail'];
44+
public function getData() {
45+
return $this->subscription;
46+
}
47+
48+
public function getRecurStartDate() {
49+
return $this->subscription['paymentSchedule']['interval']['startDate'];
50+
}
51+
52+
public function getRecurInstallmentLimit() {
53+
return $this->subscription['paymentSchedule']['interval']['totalOccurrences'];
54+
}
55+
56+
public function getRecurrenceInterval() {
57+
return $this->subscription['paymentSchedule']['interval'][0]['length'];
58+
}
59+
60+
public function getRecurAmount() {
61+
return $this->subscription['amount'];
62+
}
63+
64+
public function getRecurReference() {
65+
echo "he";
66+
print_r($this->subscription);
67+
}
68+
69+
public function getContactReference() {
70+
$profileID = $this->subscription['profile'][0]['customerProfileId'];
71+
$gateway = $gateway = Omnipay::create('AuthorizeNet_CIM');
72+
$gateway->setApiLoginId($this->request->getApiLoginId());
73+
$gateway->setHashSecret($this->request->getHashSecret());
74+
$gateway->setTransactionKey($this->request->getTransactionKey());
75+
$data = array(
76+
'customerProfileId' => $profileID,
77+
'customerPaymentProfileId' => $this->subscription['profile'][0]['paymentProfile'][0]['customerPaymentProfileId'],
78+
);
79+
$dataResponse = $gateway->getProfile($data)->send();
80+
return $dataResponse->getCustomerId();
81+
}
82+
83+
/**
84+
* @todo formalise options.
85+
*
86+
* @return mixed
87+
*/
88+
public function getRecurStatus() {
89+
return $this->subscription['paymentSchedule']['interval'][0]['status'];
90+
}
91+
92+
public function getRecurrenceUnit() {
93+
$interval = $this->subscription['paymentSchedule']['interval'][0]['unit'];
94+
$options = array(
95+
'months' => 'month',
96+
);
97+
return $options[$interval];
4798
}
4899

49100
/**

0 commit comments

Comments
 (0)