3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Core_Payment_AuthorizeNetIPN
extends CRM_Core_Payment_BaseIPN
{
36 function __construct() {
37 parent
::__construct();
40 function main($component = 'contribute') {
42 //we only get invoice num as a key player from payment gateway response.
43 //for ARB we get x_subscription_id and x_subscription_paynum
44 $x_subscription_id = self
::retrieve('x_subscription_id', 'String');
46 if ($x_subscription_id) {
49 $ids = $objects = array();
50 $input['component'] = $component;
52 // load post vars in $input
53 $this->getInput($input, $ids);
55 // load post ids in $ids
56 $this->getIDs($ids, $input);
58 $paymentProcessorID = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
59 'AuthNet', 'id', 'name'
62 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
66 if ($component == 'contribute' && $ids['contributionRecur']) {
67 // check if first contribution is completed, else complete first contribution
69 if ($objects['contribution']->contribution_status_id
== 1) {
72 return $this->recur($input, $ids, $objects, $first);
77 function recur(&$input, &$ids, &$objects, $first) {
78 $recur = &$objects['contributionRecur'];
80 // do a subscription check
81 if ($recur->processor_id
!= $input['subscription_id']) {
82 CRM_Core_Error
::debug_log_message("Unrecognized subscription.");
83 echo "Failure: Unrecognized subscription<p>";
87 // At this point $object has first contribution loaded.
88 // Lets do a check to make sure this payment has the amount same as that of first contribution.
89 if ($objects['contribution']->total_amount
!= $input['amount']) {
90 CRM_Core_Error
::debug_log_message("Subscription amount mismatch.");
91 echo "Failure: Subscription amount mismatch<p>";
95 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
97 $transaction = new CRM_Core_Transaction();
99 $now = date('YmdHis');
101 // fix dates that already exist
102 $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date');
103 foreach ($dates as $name) {
105 $recur->$name = CRM_Utils_Date
::isoToMysql($recur->$name);
109 //load new contribution object if required.
111 // create a contribution and then get it processed
112 $contribution = new CRM_Contribute_BAO_Contribution();
113 $contribution->contact_id
= $ids['contact'];
114 $contribution->financial_type_id
= $objects['contributionType']->id
;
115 $contribution->contribution_page_id
= $ids['contributionPage'];
116 $contribution->contribution_recur_id
= $ids['contributionRecur'];
117 $contribution->receive_date
= $now;
118 $contribution->currency
= $objects['contribution']->currency
;
119 $contribution->payment_instrument_id
= $objects['contribution']->payment_instrument_id
;
120 $contribution->amount_level
= $objects['contribution']->amount_level
;
121 $contribution->address_id
= $objects['contribution']->address_id
;
122 $contribution->honor_contact_id
= $objects['contribution']->honor_contact_id
;
123 $contribution->honor_type_id
= $objects['contribution']->honor_type_id
;
124 $contribution->campaign_id
= $objects['contribution']->campaign_id
;
126 $objects['contribution'] = &$contribution;
128 $objects['contribution']->invoice_id
= md5(uniqid(rand(), TRUE));
129 $objects['contribution']->total_amount
= $input['amount'];
130 $objects['contribution']->trxn_id
= $input['trxn_id'];
132 // since we have processor loaded for sure at this point,
133 // check and validate gateway MD5 response if present
134 $this->checkMD5($ids, $input);
136 $sendNotification = FALSE;
137 if ($input['response_code'] == 1) {
140 $recur->start_date
= $now;
141 $recur->trxn_id
= $recur->processor_id
;
142 $sendNotification = TRUE;
143 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_START
;
145 $statusName = 'In Progress';
146 if (($recur->installments
> 0) &&
147 ($input['subscription_paynum'] >= $recur->installments
)
149 // this is the last payment
150 $statusName = 'Completed';
151 $recur->end_date
= $now;
152 $sendNotification = TRUE;
153 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_END
;
155 $recur->modified_date
= $now;
156 $recur->contribution_status_id
= array_search($statusName, $contributionStatus);
162 $recur->contribution_status_id
= array_search('Failed', $contributionStatus);
163 $recur->cancel_date
= $now;
166 CRM_Core_Error
::debug_log_message("Subscription payment failed - '{$input['response_reason_text']}'");
168 // the recurring contribution has declined a payment or has failed
169 // so we just fix the recurring contribution and not change any of
170 // the existing contribiutions
175 // check if contribution is already completed, if so we ignore this ipn
176 if ($objects['contribution']->contribution_status_id
== 1) {
177 $transaction->commit();
178 CRM_Core_Error
::debug_log_message("returning since contribution has already been handled");
179 echo "Success: Contribution has already been handled<p>";
183 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
185 if ($sendNotification) {
186 $autoRenewMembership = FALSE;
188 isset($ids['membership']) && $ids['membership']
190 $autoRenewMembership = TRUE;
193 //send recurring Notification email for user
194 CRM_Contribute_BAO_ContributionPage
::recurringNotify($subscriptionPaymentStatus,
196 $ids['contributionPage'],
203 function getInput(&$input, &$ids) {
204 $input['amount'] = self
::retrieve('x_amount', 'String');
205 $input['subscription_id'] = self
::retrieve('x_subscription_id', 'Integer');
206 $input['response_code'] = self
::retrieve('x_response_code', 'Integer');
207 $input['MD5_Hash'] = self
::retrieve('x_MD5_Hash', 'String', FALSE, '');
208 $input['fee_amount'] = self
::retrieve('x_fee_amount', 'Money', FALSE, '0.00');
209 $input['net_amount'] = self
::retrieve('x_net_amount', 'Money', FALSE, '0.00');
210 $input['response_reason_code'] = self
::retrieve('x_response_reason_code', 'String', FALSE);
211 $input['response_reason_text'] = self
::retrieve('x_response_reason_text', 'String', FALSE);
212 $input['subscription_paynum'] = self
::retrieve('x_subscription_paynum', 'Integer', FALSE, 0);
213 $input['trxn_id'] = self
::retrieve('x_trans_id', 'String', FALSE);
214 if ($input['trxn_id']) {
215 $input['is_test'] = 0;
218 $input['is_test'] = 1;
219 $input['trxn_id'] = md5(uniqid(rand(), TRUE));
222 if (!$this->getBillingID($ids)) {
225 $billingID = $ids['billing'];
227 'first_name' => 'x_first_name',
228 'last_name' => 'x_last_name',
229 "street_address-{$billingID}" => 'x_address',
230 "city-{$billingID}" => 'x_city',
231 "state-{$billingID}" => 'x_state',
232 "postal_code-{$billingID}" => 'x_zip',
233 "country-{$billingID}" => 'x_country',
234 "email-{$billingID}" => 'x_email',
236 foreach ($params as $civiName => $resName) {
237 $input[$civiName] = self
::retrieve($resName, 'String', FALSE);
241 function getIDs(&$ids, &$input) {
242 $ids['contact'] = self
::retrieve('x_cust_id', 'Integer');
243 $ids['contribution'] = self
::retrieve('x_invoice_num', 'Integer');
245 // joining with contribution table for extra checks
247 SELECT cr.id, cr.contact_id
248 FROM civicrm_contribution_recur cr
249 INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id
250 WHERE cr.processor_id = '{$input['subscription_id']}' AND
251 (cr.contact_id = {$ids['contact']} OR co.id = {$ids['contribution']})
253 $contRecur = CRM_Core_DAO
::executeQuery($sql);
255 $ids['contributionRecur'] = $contRecur->id
;
256 if($ids['contact_id'] != $contRecur->contact_id
){
257 CRM_Core_Error
::debug_log_message("Recurring contribution appears to have been re-assigned from id {$ids['contact_id']} to {$contRecur->contact_id}
258 Continuing with {$contRecur->contact_id}
260 $ids['contact_id'] = $contRecur->contact_id
;
262 if (!$ids['contributionRecur']) {
263 CRM_Core_Error
::debug_log_message("Could not find contributionRecur id: ".print_r($input, TRUE));
264 echo "Failure: Could not find contributionRecur<p>";
268 // get page id based on contribution id
269 $ids['contributionPage'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
270 $ids['contribution'],
271 'contribution_page_id'
274 if ($input['component'] == 'event') {
275 // FIXME: figure out fields for event
278 // get the optional ids
280 // Get membershipId. Join with membership payment table for additional checks
283 FROM civicrm_membership m
284 INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = {$ids['contribution']}
285 WHERE m.contribution_recur_id = {$ids['contributionRecur']}
287 if ($membershipId = CRM_Core_DAO
::singleValueQuery($sql)) {
288 $ids['membership'] = $membershipId;
291 // FIXME: todo related_contact and onBehalfDupeAlert. Check paypalIPN.
295 static function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'POST') {
296 static $store = NULL;
297 $value = CRM_Utils_Request
::retrieve($name, $type, $store,
298 FALSE, $default, $location
300 if ($abort && $value === NULL) {
301 CRM_Core_Error
::debug_log_message("Could not find an entry for $name in $location");
302 CRM_Core_Error
::debug_var('POST', $_POST);
303 CRM_Core_Error
::debug_var('REQUEST', $_REQUEST);
304 echo "Failure: Missing Parameter<p>";
310 function checkMD5($ids, $input) {
311 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ids['paymentProcessor'],
312 $input['is_test'] ?
'test' : 'live'
314 $paymentObject = CRM_Core_Payment
::singleton($input['is_test'] ?
'test' : 'live', $paymentProcessor);
316 if (!$paymentObject->checkMD5($input['MD5_Hash'], $input['trxn_id'], $input['amount'], TRUE)) {
317 CRM_Core_Error
::debug_log_message("MD5 Verification failed.");
318 echo "Failure: Security verification failed<p>";