3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Core_Payment_AuthorizeNetIPN
extends CRM_Core_Payment_BaseIPN
{
38 * Constructor function.
40 * @param array $inputData
41 * contents of HTTP REQUEST.
43 * @throws CRM_Core_Exception
45 public function __construct($inputData) {
46 $this->setInputParameters($inputData);
47 parent
::__construct();
51 * @param string $component
55 public function main($component = 'contribute') {
57 //we only get invoice num as a key player from payment gateway response.
58 //for ARB we get x_subscription_id and x_subscription_paynum
59 $x_subscription_id = $this->retrieve('x_subscription_id', 'String');
60 $ids = $objects = $input = array();
62 if ($x_subscription_id) {
65 $input['component'] = $component;
67 // load post vars in $input
68 $this->getInput($input, $ids);
70 // load post ids in $ids
71 $this->getIDs($ids, $input);
73 $paymentProcessorID = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
74 'AuthNet', 'id', 'name'
77 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
81 if ($component == 'contribute' && $ids['contributionRecur']) {
82 // check if first contribution is completed, else complete first contribution
84 if ($objects['contribution']->contribution_status_id
== 1) {
87 return $this->recur($input, $ids, $objects, $first);
96 * @param array $objects
101 public function recur(&$input, &$ids, &$objects, $first) {
102 $this->_isRecurring
= TRUE;
103 $recur = &$objects['contributionRecur'];
105 // do a subscription check
106 if ($recur->processor_id
!= $input['subscription_id']) {
107 CRM_Core_Error
::debug_log_message("Unrecognized subscription.");
108 echo "Failure: Unrecognized subscription<p>";
112 // At this point $object has first contribution loaded.
113 // Lets do a check to make sure this payment has the amount same as that of first contribution.
114 if ($objects['contribution']->total_amount
!= $input['amount']) {
115 CRM_Core_Error
::debug_log_message("Subscription amount mismatch.");
116 echo "Failure: Subscription amount mismatch<p>";
120 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
122 $transaction = new CRM_Core_Transaction();
124 $now = date('YmdHis');
126 // fix dates that already exist
127 $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date');
128 foreach ($dates as $name) {
130 $recur->$name = CRM_Utils_Date
::isoToMysql($recur->$name);
134 //load new contribution object if required.
136 // create a contribution and then get it processed
137 $contribution = new CRM_Contribute_BAO_Contribution();
138 $contribution->contact_id
= $ids['contact'];
139 $contribution->financial_type_id
= $objects['contributionType']->id
;
140 $contribution->contribution_page_id
= $ids['contributionPage'];
141 $contribution->contribution_recur_id
= $ids['contributionRecur'];
142 $contribution->receive_date
= $now;
143 $contribution->currency
= $objects['contribution']->currency
;
144 $contribution->payment_instrument_id
= $objects['contribution']->payment_instrument_id
;
145 $contribution->amount_level
= $objects['contribution']->amount_level
;
146 $contribution->address_id
= $objects['contribution']->address_id
;
147 $contribution->campaign_id
= $objects['contribution']->campaign_id
;
149 $objects['contribution'] = &$contribution;
151 $objects['contribution']->invoice_id
= md5(uniqid(rand(), TRUE));
152 $objects['contribution']->total_amount
= $input['amount'];
153 $objects['contribution']->trxn_id
= $input['trxn_id'];
155 // since we have processor loaded for sure at this point,
156 // check and validate gateway MD5 response if present
157 $this->checkMD5($ids, $input);
159 if ($input['response_code'] == 1) {
162 $recur->start_date
= $now;
163 $recur->trxn_id
= $recur->processor_id
;
164 $this->_isFirstOrLastRecurringPayment
= CRM_Core_Payment
::RECURRING_PAYMENT_START
;
166 $statusName = 'In Progress';
167 if (($recur->installments
> 0) &&
168 ($input['subscription_paynum'] >= $recur->installments
)
170 // this is the last payment
171 $statusName = 'Completed';
172 $recur->end_date
= $now;
173 $this->_isFirstOrLastRecurringPayment
= CRM_Core_Payment
::RECURRING_PAYMENT_END
;
175 $recur->modified_date
= $now;
176 $recur->contribution_status_id
= array_search($statusName, $contributionStatus);
182 $recur->contribution_status_id
= array_search('Failed', $contributionStatus);
183 $recur->cancel_date
= $now;
186 $message = ts("Subscription payment failed - %1", array(1 => htmlspecialchars($input['response_reason_text'])));
187 CRM_Core_Error
::debug_log_message($message);
189 // the recurring contribution has declined a payment or has failed
190 // so we just fix the recurring contribution and not change any of
191 // the existing contributions
196 // check if contribution is already completed, if so we ignore this ipn
197 if ($objects['contribution']->contribution_status_id
== 1) {
198 $transaction->commit();
199 CRM_Core_Error
::debug_log_message("Returning since contribution has already been handled.");
200 echo "Success: Contribution has already been handled<p>";
204 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
213 public function getInput(&$input, &$ids) {
214 $input['amount'] = $this->retrieve('x_amount', 'String');
215 $input['subscription_id'] = $this->retrieve('x_subscription_id', 'Integer');
216 $input['response_code'] = $this->retrieve('x_response_code', 'Integer');
217 $input['MD5_Hash'] = $this->retrieve('x_MD5_Hash', 'String', FALSE, '');
218 $input['response_reason_code'] = $this->retrieve('x_response_reason_code', 'String', FALSE);
219 $input['response_reason_text'] = $this->retrieve('x_response_reason_text', 'String', FALSE);
220 $input['subscription_paynum'] = $this->retrieve('x_subscription_paynum', 'Integer', FALSE, 0);
221 $input['trxn_id'] = $this->retrieve('x_trans_id', 'String', FALSE);
223 if ($input['trxn_id']) {
224 $input['is_test'] = 0;
226 // Only assume trxn_id 'should' have been returned for success.
227 // Per CRM-17611 it would also not be passed back for a decline.
228 elseif ($input['response_code'] == 1) {
229 $input['is_test'] = 1;
230 $input['trxn_id'] = md5(uniqid(rand(), TRUE));
233 if (!$this->getBillingID($ids)) {
236 $billingID = $ids['billing'];
238 'first_name' => 'x_first_name',
239 'last_name' => 'x_last_name',
240 "street_address-{$billingID}" => 'x_address',
241 "city-{$billingID}" => 'x_city',
242 "state-{$billingID}" => 'x_state',
243 "postal_code-{$billingID}" => 'x_zip',
244 "country-{$billingID}" => 'x_country',
245 "email-{$billingID}" => 'x_email',
247 foreach ($params as $civiName => $resName) {
248 $input[$civiName] = $this->retrieve($resName, 'String', FALSE);
256 public function getIDs(&$ids, &$input) {
257 $ids['contact'] = $this->retrieve('x_cust_id', 'Integer', FALSE, 0);
258 $ids['contribution'] = $this->retrieve('x_invoice_num', 'Integer');
260 // joining with contribution table for extra checks
262 SELECT cr.id, cr.contact_id
263 FROM civicrm_contribution_recur cr
264 INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id
265 WHERE cr.processor_id = '{$input['subscription_id']}' AND
266 (cr.contact_id = {$ids['contact']} OR co.id = {$ids['contribution']})
268 $contRecur = CRM_Core_DAO
::executeQuery($sql);
270 $ids['contributionRecur'] = $contRecur->id
;
271 if ($ids['contact'] != $contRecur->contact_id
) {
272 $message = ts("Recurring contribution appears to have been re-assigned from id %1 to %2, continuing with %2.", array(1 => $ids['contact'], 2 => $contRecur->contact_id
));
273 CRM_Core_Error
::debug_log_message($message);
274 $ids['contact'] = $contRecur->contact_id
;
276 if (!$ids['contributionRecur']) {
277 $message = ts("Could not find contributionRecur id");
278 $log = new CRM_Utils_SystemLogger();
279 $log->error('payment_notification', array('message' => $message, 'ids' => $ids, 'input' => $input));
280 throw new CRM_Core_Exception($message);
283 // get page id based on contribution id
284 $ids['contributionPage'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
285 $ids['contribution'],
286 'contribution_page_id'
289 if ($input['component'] == 'event') {
290 // FIXME: figure out fields for event
293 // get the optional ids
295 // Get membershipId. Join with membership payment table for additional checks
298 FROM civicrm_membership m
299 INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = {$ids['contribution']}
300 WHERE m.contribution_recur_id = {$ids['contributionRecur']}
302 if ($membershipId = CRM_Core_DAO
::singleValueQuery($sql)) {
303 $ids['membership'] = $membershipId;
306 // FIXME: todo related_contact and onBehalfDupeAlert. Check paypalIPN.
311 * @param string $name
313 * @param string $type
316 * Abort if not present.
317 * @param null $default
320 * @throws CRM_Core_Exception
323 public function retrieve($name, $type, $abort = TRUE, $default = NULL) {
324 $value = CRM_Utils_Type
::validate(
325 empty($this->_inputParameters
[$name]) ?
$default : $this->_inputParameters
[$name],
329 if ($abort && $value === NULL) {
330 throw new CRM_Core_Exception("Could not find an entry for $name");
336 * Check that the MDs is valid.
338 * Note that this only checks if it is provided.
341 * @param array $input
343 * @throws CRM_Core_Exception
345 public function checkMD5($ids, $input) {
346 if (empty($input['trxn_id'])) {
347 // For decline we have nothing to check against.
350 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ids['paymentProcessor'],
351 $input['is_test'] ?
'test' : 'live'
353 $paymentObject = CRM_Core_Payment
::singleton($input['is_test'] ?
'test' : 'live', $paymentProcessor);
355 if (!$paymentObject->checkMD5($input['MD5_Hash'], $input['trxn_id'], $input['amount'], TRUE)) {
356 $message = "Failure: Security verification failed";
357 $log = new CRM_Utils_SystemLogger();
358 $log->error('payment_notification', array('message' => $message, 'ids' => $ids, 'input' => $input));
359 throw new CRM_Core_Exception($message);