Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { | |
36 | function __construct() { | |
37 | parent::__construct(); | |
38 | } | |
39 | ||
40 | function main($component = 'contribute') { | |
41 | ||
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'); | |
45 | ||
46 | if ($x_subscription_id) { | |
47 | //Approved | |
48 | ||
49 | $ids = $objects = array(); | |
50 | $input['component'] = $component; | |
51 | ||
52 | // load post vars in $input | |
53 | $this->getInput($input, $ids); | |
54 | ||
55 | // load post ids in $ids | |
56 | $this->getIDs($ids, $input); | |
57 | ||
58 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
59 | 'AuthNet', 'id', 'name' | |
60 | ); | |
61 | ||
62 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
63 | return FALSE; | |
64 | } | |
65 | ||
66 | if ($component == 'contribute' && $ids['contributionRecur']) { | |
67 | // check if first contribution is completed, else complete first contribution | |
68 | $first = TRUE; | |
69 | if ($objects['contribution']->contribution_status_id == 1) { | |
70 | $first = FALSE; | |
71 | } | |
72 | return $this->recur($input, $ids, $objects, $first); | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | function recur(&$input, &$ids, &$objects, $first) { | |
78 | $recur = &$objects['contributionRecur']; | |
79 | ||
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>"; | |
84 | return FALSE; | |
85 | } | |
86 | ||
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>"; | |
92 | return FALSE; | |
93 | } | |
94 | ||
95 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
96 | ||
97 | $transaction = new CRM_Core_Transaction(); | |
98 | ||
99 | $now = date('YmdHis'); | |
100 | ||
101 | // fix dates that already exist | |
102 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
103 | foreach ($dates as $name) { | |
104 | if ($recur->$name) { | |
105 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
106 | } | |
107 | } | |
108 | ||
109 | //load new contribution object if required. | |
110 | if (!$first) { | |
111 | // create a contribution and then get it processed | |
112 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
113 | $contribution->contact_id = $ids['contact']; | |
94d1bc8d | 114 | $contribution->financial_type_id = $objects['contributionType']->id; |
6a488035 TO |
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; | |
94d1bc8d PJ |
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; | |
125 | ||
6a488035 TO |
126 | $objects['contribution'] = &$contribution; |
127 | } | |
128 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); | |
129 | $objects['contribution']->total_amount = $input['amount']; | |
130 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
131 | ||
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); | |
135 | ||
136 | $sendNotification = FALSE; | |
137 | if ($input['response_code'] == 1) { | |
138 | // Approved | |
139 | if ($first) { | |
140 | $recur->start_date = $now; | |
141 | $recur->trxn_id = $recur->processor_id; | |
142 | $sendNotification = TRUE; | |
143 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
144 | } | |
145 | $statusName = 'In Progress'; | |
146 | if (($recur->installments > 0) && | |
147 | ($input['subscription_paynum'] >= $recur->installments) | |
148 | ) { | |
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; | |
154 | } | |
155 | $recur->modified_date = $now; | |
156 | $recur->contribution_status_id = array_search($statusName, $contributionStatus); | |
157 | $recur->save(); | |
158 | } | |
159 | else { | |
160 | // Declined | |
161 | // failed status | |
162 | $recur->contribution_status_id = array_search('Failed', $contributionStatus); | |
163 | $recur->cancel_date = $now; | |
164 | $recur->save(); | |
165 | ||
166 | CRM_Core_Error::debug_log_message("Subscription payment failed - '{$input['response_reason_text']}'"); | |
167 | ||
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 | |
171 | // CRM-9036 | |
172 | return TRUE; | |
173 | } | |
174 | ||
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>"; | |
180 | return TRUE; | |
181 | } | |
182 | ||
183 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
184 | ||
185 | if ($sendNotification) { | |
186 | $autoRenewMembership = FALSE; | |
187 | if ($recur->id && | |
188 | isset($ids['membership']) && $ids['membership'] | |
189 | ) { | |
190 | $autoRenewMembership = TRUE; | |
191 | } | |
192 | ||
193 | //send recurring Notification email for user | |
194 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
195 | $ids['contact'], | |
196 | $ids['contributionPage'], | |
197 | $recur, | |
198 | $autoRenewMembership | |
199 | ); | |
200 | } | |
201 | } | |
202 | ||
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; | |
216 | } | |
217 | else { | |
218 | $input['is_test'] = 1; | |
219 | $input['trxn_id'] = md5(uniqid(rand(), TRUE)); | |
220 | } | |
221 | ||
222 | if (!$this->getBillingID($ids)) { | |
223 | return FALSE; | |
224 | } | |
225 | $billingID = $ids['billing']; | |
226 | $params = array( | |
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', | |
235 | ); | |
236 | foreach ($params as $civiName => $resName) { | |
237 | $input[$civiName] = self::retrieve($resName, 'String', FALSE); | |
238 | } | |
239 | } | |
240 | ||
241 | function getIDs(&$ids, &$input) { | |
242 | $ids['contact'] = self::retrieve('x_cust_id', 'Integer'); | |
243 | $ids['contribution'] = self::retrieve('x_invoice_num', 'Integer'); | |
244 | ||
245 | // joining with contribution table for extra checks | |
246 | $sql = " | |
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']}) | |
252 | LIMIT 1"; | |
253 | $contRecur = CRM_Core_DAO::executeQuery($sql); | |
254 | $contRecur->fetch(); | |
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} | |
259 | "); | |
260 | $ids['contact_id'] = $contRecur->contact_id; | |
261 | } | |
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>"; | |
265 | exit(); | |
266 | } | |
267 | ||
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' | |
272 | ); | |
273 | ||
274 | if ($input['component'] == 'event') { | |
275 | // FIXME: figure out fields for event | |
276 | } | |
277 | else { | |
278 | // get the optional ids | |
279 | ||
280 | // Get membershipId. Join with membership payment table for additional checks | |
281 | $sql = " | |
282 | SELECT m.id | |
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']} | |
286 | LIMIT 1"; | |
287 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { | |
288 | $ids['membership'] = $membershipId; | |
289 | } | |
290 | ||
291 | // FIXME: todo related_contact and onBehalfDupeAlert. Check paypalIPN. | |
292 | } | |
293 | } | |
294 | ||
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 | |
299 | ); | |
300 | if ($abort && $value === NULL) { | |
301 | CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location"); | |
4b7d50ce | 302 | CRM_Core_Error::debug_var('POST', $_POST); |
303 | CRM_Core_Error::debug_var('REQUEST', $_REQUEST); | |
6a488035 TO |
304 | echo "Failure: Missing Parameter<p>"; |
305 | exit(); | |
306 | } | |
307 | return $value; | |
308 | } | |
309 | ||
310 | function checkMD5($ids, $input) { | |
311 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ids['paymentProcessor'], | |
312 | $input['is_test'] ? 'test' : 'live' | |
313 | ); | |
314 | $paymentObject = CRM_Core_Payment::singleton($input['is_test'] ? 'test' : 'live', $paymentProcessor); | |
315 | ||
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>"; | |
319 | exit(); | |
320 | } | |
321 | return TRUE; | |
322 | } | |
323 | } | |
324 |