Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0cf587a7 EM |
2 | |
3 | /** | |
4 | * Class CRM_Event_Cart_Form_Checkout_Payment | |
5 | */ | |
6a488035 TO |
6 | class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { |
7 | public $all_participants; | |
8 | public $financial_type_id; | |
9 | public $description; | |
10 | public $line_items; | |
11 | public $_fields = array(); | |
12 | public $_paymentProcessor; | |
13 | public $total; | |
14 | public $sub_total; | |
15 | public $payment_required = TRUE; | |
16 | public $payer_contact_id; | |
17 | public $is_pay_later = FALSE; | |
18 | public $pay_later_receipt; | |
19 | ||
0cf587a7 EM |
20 | /** |
21 | * @param $params | |
22 | * @param $participant | |
23 | * @param $event | |
24 | * | |
25 | * @return mixed | |
26 | */ | |
6a488035 TO |
27 | function registerParticipant($params, &$participant, $event) { |
28 | $transaction = new CRM_Core_Transaction(); | |
29 | ||
30 | // handle register date CRM-4320 | |
31 | $registerDate = date('YmdHis'); | |
32 | $participantParams = array( | |
33 | 'id' => $participant->id, | |
34 | 'event_id' => $event->id, | |
35 | 'register_date' => $registerDate, | |
36 | 'source' => CRM_Utils_Array::value('participant_source', $params, $this->description), | |
37 | //'fee_level' => $participant->fee_level, | |
38 | 'is_pay_later' => $this->is_pay_later, | |
39 | 'fee_amount' => CRM_Utils_Array::value('amount', $params, 0), | |
40 | //XXX why is this a ref to participant and not contact?: | |
41 | //'registered_by_id' => $this->payer_contact_id, | |
42 | 'fee_currency' => CRM_Utils_Array::value('currencyID', $params), | |
43 | ); | |
44 | ||
45 | if ($participant->must_wait) { | |
46 | $participant_status = 'On waitlist'; | |
47 | } | |
48 | elseif (CRM_Utils_Array::value('is_pay_later', $params, FALSE)) { | |
49 | $participant_status = 'Pending from pay later'; | |
50 | } | |
51 | else { | |
52 | $participant_status = 'Registered'; | |
53 | } | |
54 | $participant_statuses = CRM_Event_PseudoConstant::participantStatus(); | |
55 | $participantParams['status_id'] = array_search($participant_status, $participant_statuses); | |
56 | $participant_status_label = CRM_Utils_Array::value($participantParams['status_id'], CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label')); | |
57 | $participantParams['participant_status'] = $participant_status_label; | |
58 | ||
59 | $this->assign('isOnWaitlist', $participant->must_wait); | |
60 | ||
61 | if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { | |
62 | $participantParams['is_test'] = 1; | |
63 | } | |
64 | else { | |
65 | $participantParams['is_test'] = 0; | |
66 | } | |
67 | ||
68 | if (self::is_administrator()) { | |
a7488080 | 69 | if (!empty($params['note'])) { |
6a488035 TO |
70 | $note_params = array( |
71 | 'participant_id' => $participant->id, | |
72 | 'contact_id' => self::getContactID(), | |
73 | 'note' => $params['note'], | |
74 | ); | |
75 | CRM_Event_BAO_Participant::update_note($note_params); | |
76 | } | |
77 | } | |
78 | ||
79 | $participant->copyValues($participantParams); | |
80 | $participant->save(); | |
81 | ||
a7488080 | 82 | if (!empty($params['contributionID'])) { |
6a488035 TO |
83 | $payment_params = array( |
84 | 'participant_id' => $participant->id, | |
85 | 'contribution_id' => $params['contributionID'], | |
86 | ); | |
87 | $ids = array(); | |
88 | $paymentParticpant = CRM_Event_BAO_ParticipantPayment::create($payment_params, $ids); | |
89 | } | |
90 | ||
91 | $transaction->commit(); | |
92 | ||
93 | $event_values = array(); | |
94 | CRM_Core_DAO::storeValues($event, $event_values); | |
95 | ||
96 | $location = array(); | |
97 | if (CRM_Utils_Array::value('is_show_location', $event_values) == 1) { | |
98 | $locationParams = array( | |
99 | 'entity_id' => $participant->event_id, | |
100 | 'entity_table' => 'civicrm_event', | |
101 | ); | |
102 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
103 | CRM_Core_BAO_Address::fixAddress($location['address'][1]); | |
104 | } | |
105 | ||
106 | list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant::get_profile_groups($participant->event_id); | |
107 | $payer_values = array( | |
108 | 'email' => '', | |
109 | 'name' => '', | |
110 | ); | |
111 | if ($this->payer_contact_id) { | |
112 | $payer_contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id); | |
113 | $payer_values = array( | |
114 | 'email' => $payer_contact_details[1], | |
115 | 'name' => $payer_contact_details[0], | |
116 | ); | |
117 | } | |
118 | $values = array( | |
119 | 'params' => array($participant->id => $participantParams), | |
120 | 'event' => $event_values, | |
121 | 'location' => $location, | |
122 | 'custom_pre_id' => $pre_id, | |
123 | 'custom_post_id' => $post_id, | |
124 | 'payer' => $payer_values, | |
125 | ); | |
126 | CRM_Event_BAO_Event::sendMail($participant->contact_id, $values, $participant->id); | |
127 | ||
128 | return $participant; | |
129 | } | |
130 | ||
131 | function buildPaymentFields() { | |
132 | $payment_processor_id = NULL; | |
133 | $can_pay_later = TRUE; | |
134 | $pay_later_text = ""; | |
135 | $this->pay_later_receipt = ""; | |
136 | foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) { | |
137 | if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) { | |
138 | $payment_processor_id = $event_in_cart->event->payment_processor; | |
2b8d92de | 139 | $this->financial_type_id = $event_in_cart->event->financial_type_id; |
6a488035 TO |
140 | } |
141 | else { | |
142 | if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) { | |
143 | CRM_Core_Error::statusBounce(ts('When registering for multiple events all events must use the same payment processor. ')); | |
144 | } | |
145 | } | |
146 | if (!$event_in_cart->event->is_pay_later) { | |
147 | $can_pay_later = FALSE; | |
148 | } | |
149 | else { | |
150 | //XXX | |
151 | $pay_later_text = $event_in_cart->event->pay_later_text; | |
152 | $this->pay_later_receipt = $event_in_cart->event->pay_later_receipt; | |
153 | } | |
154 | } | |
155 | ||
156 | if ($payment_processor_id == NULL) { | |
157 | CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).')); | |
158 | } | |
159 | ||
160 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, $this->_mode); | |
161 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
162 | ||
163 | CRM_Core_Payment_Form::setCreditCardFields($this); | |
164 | CRM_Core_Payment_Form::buildCreditCard($this); | |
165 | ||
166 | if ($can_pay_later || self::is_administrator()) { | |
167 | $this->addElement('checkbox', 'is_pay_later', | |
168 | $pay_later_text | |
169 | ); | |
170 | $this->addElement('checkbox', 'payment_completed', | |
171 | ts('Payment Completed') | |
172 | ); | |
173 | $this->assign('pay_later_instructions', $this->pay_later_receipt); | |
174 | } | |
175 | } | |
176 | ||
177 | function buildQuickForm() { | |
178 | ||
179 | $this->line_items = array(); | |
180 | $this->sub_total = 0; | |
181 | $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices'); | |
182 | ||
183 | // iterate over each event in cart | |
184 | foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) { | |
185 | $this->process_event_line_item($event_in_cart); | |
186 | foreach ($this->cart->get_events_in_carts_by_main_event_id($event_in_cart->event_id) as $subevent) { | |
187 | $this->process_event_line_item($subevent, 'subevent'); | |
188 | } | |
189 | } | |
190 | ||
191 | $this->total = $this->sub_total; | |
192 | $this->payment_required = ($this->total > 0); | |
193 | $this->assign('payment_required', $this->payment_required); | |
194 | $this->assign('line_items', $this->line_items); | |
195 | $this->assign('sub_total', $this->sub_total); | |
196 | $this->assign('total', $this->total); | |
197 | $buttons = array(); | |
198 | $buttons[] = array( | |
199 | 'name' => ts('<< Go Back'), | |
200 | 'spacing' => '  ', | |
201 | 'type' => 'back', | |
202 | ); | |
203 | $buttons[] = array( | |
204 | 'isDefault' => TRUE, | |
205 | 'name' => ts('Complete Transaction >>'), | |
206 | 'spacing' => ' ', | |
207 | 'type' => 'next', | |
208 | ); | |
209 | ||
210 | if ($this->total) { | |
211 | $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE); | |
212 | $this->assign('collect_billing_email', TRUE); | |
213 | } | |
214 | if (self::is_administrator()) { | |
215 | $this->add('textarea', 'note', 'Note'); | |
216 | $this->add('text', 'source', 'Source', array('size' => 80)); | |
217 | $instruments = array(); | |
218 | CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE); | |
219 | $options = array(); | |
220 | foreach ($instruments as $type) { | |
221 | $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']); | |
222 | } | |
223 | $this->addGroup($options, 'payment_type', ts("Alternative Payment Type")); | |
224 | $this->add('text', 'check_number', ts('Check No.'), array('size' => 20)); | |
225 | $this->addElement('checkbox', 'is_pending', ts('Create a pending registration')); | |
226 | ||
227 | $this->assign('administrator', TRUE); | |
228 | } | |
229 | $this->addButtons($buttons); | |
230 | ||
231 | $this->addFormRule(array('CRM_Event_Cart_Form_Checkout_Payment', 'formRule'), $this); | |
232 | ||
233 | if ($this->payment_required) { | |
234 | $this->buildPaymentFields(); | |
235 | } | |
236 | } | |
237 | ||
0cf587a7 EM |
238 | /** |
239 | * @param $event_in_cart | |
240 | * @param null $class | |
241 | */ | |
6a488035 TO |
242 | function process_event_line_item(&$event_in_cart, $class = NULL) { |
243 | $cost = 0; | |
9da8dc8c | 244 | $price_set_id = CRM_Price_BAO_PriceSet::getFor("civicrm_event", $event_in_cart->event_id); |
6a488035 TO |
245 | $amount_level = NULL; |
246 | if ($price_set_id) { | |
247 | $event_price_values = array(); | |
248 | foreach ($this->_price_values as $key => $value) { | |
249 | if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) { | |
250 | $event_price_values[$matches[1]] = $value; | |
251 | } | |
252 | } | |
9da8dc8c | 253 | $price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE); |
6a488035 TO |
254 | $price_set = $price_sets[$price_set_id]; |
255 | $price_set_amount = array(); | |
9da8dc8c | 256 | CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount); |
6a488035 TO |
257 | $cost = $event_price_values['amount']; |
258 | $amount_level = $event_price_values['amount_level']; | |
2b8d92de | 259 | $price_details[$price_set_id] = $price_set_amount; |
6a488035 TO |
260 | } |
261 | ||
262 | // iterate over each participant in event | |
263 | foreach ($event_in_cart->participants as & $participant) { | |
264 | $participant->cost = $cost; | |
265 | $participant->fee_level = $amount_level; | |
2b8d92de | 266 | $participant->price_details = $price_details; |
6a488035 TO |
267 | } |
268 | ||
269 | $this->add_line_item($event_in_cart, $class); | |
270 | } | |
271 | ||
0cf587a7 EM |
272 | /** |
273 | * @param $event_in_cart | |
274 | * @param null $class | |
275 | */ | |
6a488035 TO |
276 | function add_line_item($event_in_cart, $class = NULL) { |
277 | $amount = 0; | |
278 | $cost = 0; | |
279 | $not_waiting_participants = array(); | |
280 | foreach ($event_in_cart->not_waiting_participants() as $participant) { | |
281 | $amount += $participant->cost; | |
282 | $cost = max($cost, $participant->cost); | |
283 | $not_waiting_participants[] = array( | |
284 | 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id), | |
285 | ); | |
286 | } | |
287 | $waiting_participants = array(); | |
288 | foreach ($event_in_cart->waiting_participants() as $participant) { | |
289 | $waiting_participants[] = array( | |
290 | 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id), | |
291 | ); | |
292 | } | |
293 | $this->line_items[] = array( | |
294 | 'amount' => $amount, | |
295 | 'cost' => $cost, | |
296 | 'event' => $event_in_cart->event, | |
297 | 'participants' => $not_waiting_participants, | |
298 | 'num_participants' => count($not_waiting_participants), | |
299 | 'num_waiting_participants' => count($waiting_participants), | |
300 | 'waiting_participants' => $waiting_participants, | |
301 | 'class' => $class, | |
302 | ); | |
303 | ||
304 | $this->sub_total += $amount; | |
305 | } | |
306 | ||
0cf587a7 EM |
307 | /** |
308 | * @return mixed | |
309 | */ | |
6a488035 TO |
310 | function getDefaultFrom() { |
311 | $values = CRM_Core_OptionGroup::values('from_email_address'); | |
312 | return $values[1]; | |
313 | } | |
314 | ||
0cf587a7 EM |
315 | /** |
316 | * @param $events_in_cart | |
317 | * @param $params | |
318 | */ | |
6a488035 TO |
319 | function emailReceipt($events_in_cart, $params) { |
320 | $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id); | |
321 | $state_province = new CRM_Core_DAO_StateProvince(); | |
322 | $state_province->id = $params["billing_state_province_id-{$this->_bltID}"]; | |
323 | $state_province->find(); | |
324 | $state_province->fetch(); | |
325 | $country = new CRM_Core_DAO_Country(); | |
326 | $country->id = $params["billing_country_id-{$this->_bltID}"]; | |
327 | $country->find(); | |
328 | $country->fetch(); | |
329 | foreach ($this->line_items as & $line_item) { | |
330 | $location_params = array('entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event'); | |
331 | $line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE); | |
332 | CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]); | |
333 | } | |
334 | $send_template_params = array( | |
335 | 'table' => 'civicrm_msg_template', | |
336 | 'contactId' => $this->payer_contact_id, | |
337 | 'from' => $this->getDefaultFrom(), | |
338 | 'groupName' => 'msg_tpl_workflow_event', | |
339 | 'isTest' => FALSE, | |
340 | 'toEmail' => $contact_details[1], | |
341 | 'toName' => $contact_details[0], | |
342 | 'tplParams' => array | |
343 | ( | |
344 | 'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}", | |
345 | 'billing_city' => $params["billing_city-{$this->_bltID}"], | |
346 | 'billing_country' => $country->name, | |
347 | 'billing_postal_code' => $params["billing_postal_code-{$this->_bltID}"], | |
348 | 'billing_state' => $state_province->abbreviation, | |
349 | 'billing_street_address' => $params["billing_street_address-{$this->_bltID}"], | |
350 | 'credit_card_exp_date' => $params['credit_card_exp_date'], | |
351 | 'credit_card_type' => $params['credit_card_type'], | |
352 | 'credit_card_number' => "************" . substr($params['credit_card_number'], -4, 4), | |
353 | // XXX cart->get_discounts | |
354 | 'discounts' => $this->discounts, | |
355 | 'email' => $contact_details[1], | |
356 | 'events_in_cart' => $events_in_cart, | |
357 | 'line_items' => $this->line_items, | |
358 | 'name' => $contact_details[0], | |
359 | 'transaction_id' => $params['trxn_id'], | |
360 | 'transaction_date' => $params['trxn_date'], | |
361 | 'is_pay_later' => $this->is_pay_later, | |
362 | 'pay_later_receipt' => $this->pay_later_receipt, | |
363 | ), | |
364 | 'valueName' => 'event_registration_receipt', | |
fed86eca | 365 | 'PDFFilename' => ts('confirmation').'.pdf', |
6a488035 TO |
366 | ); |
367 | $template_params_to_copy = array( | |
368 | 'billing_name', | |
369 | 'billing_city', | |
370 | 'billing_country', | |
371 | 'billing_postal_code', | |
372 | 'billing_state', | |
373 | 'billing_street_address', | |
374 | 'credit_card_exp_date', | |
375 | 'credit_card_type', | |
376 | 'credit_card_number', | |
377 | ); | |
378 | foreach ($template_params_to_copy as $template_param_to_copy) { | |
379 | $this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]); | |
380 | } | |
381 | ||
c6327d7d | 382 | CRM_Core_BAO_MessageTemplate::sendTemplate($send_template_params); |
6a488035 TO |
383 | } |
384 | ||
0cf587a7 EM |
385 | /** |
386 | * @param $fields | |
387 | * @param $files | |
388 | * @param $self | |
389 | * | |
390 | * @return array|bool | |
391 | */ | |
6a488035 TO |
392 | static function formRule($fields, $files, $self) { |
393 | $errors = array(); | |
394 | ||
8cc574cf | 395 | if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) { |
6a488035 TO |
396 | $payment = &CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this); |
397 | $error = $payment->checkConfig($self->_mode); | |
398 | if ($error) { | |
399 | $errors['_qf_default'] = $error; | |
400 | } | |
7cb3d4f0 | 401 | CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); |
6a488035 | 402 | |
7cb3d4f0 CW |
403 | // make sure that credit card number and cvv are valid |
404 | CRM_Core_Payment_Form::validateCreditCard($fields, $errors); | |
6a488035 TO |
405 | } |
406 | ||
407 | return empty($errors) ? TRUE : $errors; | |
408 | } | |
409 | ||
0cf587a7 EM |
410 | /** |
411 | * @return bool | |
412 | */ | |
6a488035 TO |
413 | function validate() { |
414 | if ($this->is_pay_later) { | |
415 | $this->_fields['credit_card_number']['is_required'] = FALSE; | |
416 | $this->_fields['cvv2']['is_required'] = FALSE; | |
417 | $this->_fields['credit_card_exp_date']['is_required'] = FALSE; | |
418 | $this->_fields['credit_card_type']['is_required'] = FALSE; | |
419 | } | |
420 | return parent::validate(); | |
421 | } | |
422 | ||
423 | function preProcess() { | |
424 | $params = $this->_submitValues; | |
425 | $this->is_pay_later = CRM_Utils_Array::value('is_pay_later', $params, FALSE) && !CRM_Utils_Array::value('payment_completed', $params); | |
426 | ||
427 | parent::preProcess(); | |
428 | } | |
429 | ||
430 | function postProcess() { | |
431 | ||
432 | $transaction = new CRM_Core_Transaction(); | |
2b8d92de | 433 | $trxnDetails = NULL; |
6a488035 TO |
434 | $params = $this->_submitValues; |
435 | ||
436 | $main_participants = $this->cart->get_main_event_participants(); | |
437 | foreach ($main_participants as $participant) { | |
438 | $defaults = array(); | |
439 | $ids = array('contact_id' => $participant->contact_id); | |
440 | $contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults); | |
441 | $contact->is_deleted = 0; | |
442 | $contact->save(); | |
443 | } | |
444 | ||
445 | $trxn_prefix = 'VR'; | |
446 | if (array_key_exists('billing_contact_email', $params)) { | |
447 | $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), array( | |
448 | 'email' => $params['billing_contact_email'], | |
449 | 'first_name' => $params['billing_first_name'], | |
450 | 'last_name' => $params['billing_last_name'], | |
451 | 'is_deleted' => FALSE, | |
452 | )); | |
453 | ||
454 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
455 | $this->payer_contact_id, | |
456 | 'contact_type' | |
457 | ); | |
458 | $billing_fields = array( | |
459 | "billing_first_name" => 1, | |
460 | "billing_middle_name" => 1, | |
461 | "billing_last_name" => 1, | |
462 | "billing_street_address-{$this->_bltID}" => 1, | |
463 | "billing_city-{$this->_bltID}" => 1, | |
464 | "billing_state_province_id-{$this->_bltID}" => 1, | |
465 | "billing_postal_code-{$this->_bltID}" => 1, | |
466 | "billing_country_id-{$this->_bltID}" => 1, | |
467 | "address_name-{$this->_bltID}" => 1, | |
468 | "email-{$this->_bltID}" => 1, | |
469 | ); | |
470 | ||
471 | $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params); | |
472 | ||
473 | $params["email-{$this->_bltID}"] = $params['billing_contact_email']; | |
474 | CRM_Contact_BAO_Contact::createProfileContact( | |
475 | $params, | |
476 | $billing_fields, | |
477 | $this->payer_contact_id, | |
478 | NULL, | |
479 | NULL, | |
480 | $ctype, | |
481 | TRUE | |
482 | ); | |
483 | } | |
484 | ||
485 | $params['now'] = date('YmdHis'); | |
486 | $params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
487 | $params['amount'] = $this->total; | |
2b8d92de | 488 | $params['financial_type_id'] = $this->financial_type_id; |
8cc574cf | 489 | if ($this->payment_required && empty($params['is_pay_later'])) { |
2b8d92de AN |
490 | $trxnDetails = $this->make_payment($params); |
491 | $params['trxn_id'] = $trxnDetails['trxn_id']; | |
492 | $params['trxn_date'] = $trxnDetails['trxn_date']; | |
493 | $params['currencyID'] = $trxnDetails['currency']; | |
6a488035 TO |
494 | } |
495 | $this->cart->completed = TRUE; | |
496 | $this->cart->save(); | |
497 | $this->set('last_event_cart_id', $this->cart->id); | |
498 | ||
499 | $contribution_statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
500 | $params['payment_instrument_id'] = NULL; | |
a7488080 | 501 | if (!empty($params['is_pay_later'])) { |
6a488035 TO |
502 | $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name'); |
503 | $trxn_prefix = 'CK'; | |
504 | } | |
505 | else { | |
506 | $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Credit Card', 'name'); | |
507 | } | |
8cc574cf | 508 | if ($this->is_pay_later && empty($params['payment_completed'])) { |
6a488035 TO |
509 | $params['contribution_status_id'] = array_search('Pending', $contribution_statuses); |
510 | } | |
511 | else { | |
512 | $params['contribution_status_id'] = array_search('Completed', $contribution_statuses); | |
513 | $params['participant_status'] = 'Registered'; | |
514 | $params['is_pay_later'] = 0; | |
515 | } | |
2b8d92de | 516 | if ($trxnDetails == NULL) { |
6a488035 TO |
517 | $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S"); |
518 | $params['trxn_date'] = $params['now']; | |
519 | } | |
520 | ||
521 | if ($this->payment_required) { | |
522 | $this->emailReceipt($this->cart->events_in_carts, $params); | |
523 | } | |
524 | ||
525 | // n.b. we need to process the subparticipants before main event | |
526 | // participants so that session attendance can be included in the email | |
527 | $main_participants = $this->cart->get_main_event_participants(); | |
528 | $this->all_participants = array(); | |
529 | foreach ($main_participants as $main_participant) { | |
530 | $this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant)); | |
531 | } | |
532 | $this->all_participants = array_merge($this->all_participants, $main_participants); | |
533 | ||
534 | $this->sub_trxn_index = 0; | |
535 | foreach ($this->all_participants as $mer_participant) { | |
536 | $event_in_cart = $this->cart->get_event_in_cart_by_event_id($mer_participant->event_id); | |
537 | ||
538 | $this->sub_trxn_index += 1; | |
539 | ||
540 | unset($params['contributionID']); | |
541 | if ($mer_participant->must_wait) { | |
542 | $this->registerParticipant($params, $mer_participant, $event_in_cart->event); | |
543 | } | |
544 | else { | |
6a488035 TO |
545 | $params['amount'] = $mer_participant->cost - $mer_participant->discount_amount; |
546 | ||
2b8d92de AN |
547 | if ($event_in_cart->event->financial_type_id && $mer_participant->cost) { |
548 | $params['financial_type_id'] = $event_in_cart->event->financial_type_id; | |
6a488035 | 549 | $params['participant_contact_id'] = $mer_participant->contact_id; |
2b8d92de AN |
550 | $contribution = $this->record_contribution($mer_participant, $params, $event_in_cart->event ); |
551 | // Record civicrm_line_item | |
552 | CRM_Price_BAO_LineItem::processPriceSet($mer_participant->id, $mer_participant->price_details, $contribution, $entity_table = 'civicrm_participant'); | |
6a488035 TO |
553 | } |
554 | $this->registerParticipant($params, $mer_participant, $event_in_cart->event); | |
555 | } | |
556 | } | |
557 | $this->trxn_id = $params['trxn_id']; | |
558 | $this->trxn_date = $params['trxn_date']; | |
559 | $this->saveDataToSession(); | |
560 | $transaction->commit(); | |
561 | } | |
562 | ||
0cf587a7 EM |
563 | /** |
564 | * @param $params | |
565 | * | |
566 | * @return array | |
567 | * @throws Exception | |
568 | */ | |
6a488035 TO |
569 | function make_payment(&$params) { |
570 | $config = CRM_Core_Config::singleton(); | |
571 | if (isset($params["billing_state_province_id-{$this->_bltID}"]) && $params["billing_state_province_id-{$this->_bltID}"]) { | |
572 | $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]); | |
573 | } | |
574 | ||
575 | if (isset($params["billing_country_id-{$this->_bltID}"]) && $params["billing_country_id-{$this->_bltID}"]) { | |
576 | $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]); | |
577 | } | |
578 | $params['ip_address'] = CRM_Utils_System::ipAddress(); | |
579 | $params['currencyID'] = $config->defaultCurrency; | |
580 | $params['payment_action'] = 'Sale'; | |
581 | ||
582 | $payment = &CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
583 | CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE); | |
584 | $params['month'] = $params['credit_card_exp_date']['M']; | |
585 | $params['year'] = $params['credit_card_exp_date']['Y']; | |
586 | $result = &$payment->doDirectPayment($params); | |
587 | if (is_a($result, 'CRM_Core_Error')) { | |
588 | CRM_Core_Error::displaySessionError($result); | |
589 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE)); | |
590 | return; | |
591 | } | |
592 | elseif (!$result['trxn_id']) { | |
593 | CRM_Core_Error::fatal(ts("Financial institution didn't return a transaction id.")); | |
594 | } | |
2b8d92de | 595 | $trxnDetails = array( |
6a488035 | 596 | 'trxn_id' => $result['trxn_id'], |
2b8d92de AN |
597 | 'trxn_date' => $result['now'], |
598 | 'currency' => CRM_Utils_Array::value('currencyID', $result), | |
6a488035 | 599 | ); |
2b8d92de | 600 | return $trxnDetails; |
6a488035 TO |
601 | } |
602 | ||
0cf587a7 EM |
603 | /** |
604 | * @param $mer_participant | |
605 | * @param $params | |
606 | * @param $event | |
607 | * | |
608 | * @return object | |
609 | * @throws Exception | |
610 | */ | |
6a488035 | 611 | function record_contribution(&$mer_participant, &$params, $event) { |
8cc574cf | 612 | if (self::is_administrator() && !empty($params['payment_type'])) { |
6a488035 TO |
613 | $params['payment_instrument_id'] = $params['payment_type']; |
614 | } | |
615 | ||
616 | if ($this->payer_contact_id) { | |
617 | $payer = $this->payer_contact_id; | |
618 | } | |
619 | elseif (self::getContactID()) { | |
620 | $payer = self::getContactID(); | |
621 | } | |
622 | else { | |
623 | $payer = $params['participant_contact_id']; | |
624 | } | |
625 | ||
626 | $contribParams = array( | |
627 | 'contact_id' => $payer, | |
628 | 'financial_type_id' => $params['financial_type_id'], | |
629 | 'receive_date' => $params['now'], | |
630 | 'total_amount' => $params['amount'], | |
631 | 'amount_level' => $mer_participant->fee_level, | |
6a488035 TO |
632 | 'net_amount' => $params['amount'], |
633 | 'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}", | |
634 | 'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}", | |
635 | 'currency' => CRM_Utils_Array::value('currencyID', $params), | |
636 | 'source' => $event->title, | |
637 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), | |
638 | 'contribution_status_id' => $params['contribution_status_id'], | |
639 | 'payment_instrument_id' => $params['payment_instrument_id'], | |
640 | 'check_number' => CRM_Utils_Array::value('check_number', $params), | |
2b8d92de | 641 | 'skipLineItem' => 1, |
6a488035 TO |
642 | ); |
643 | ||
2b8d92de AN |
644 | if (is_array($this->_paymentProcessor)) { |
645 | $contribParams['payment_processor'] = $this->_paymentProcessor['id']; | |
646 | } | |
647 | ||
648 | $contribution = &CRM_Contribute_BAO_Contribution::add($contribParams); | |
6a488035 | 649 | if (is_a($contribution, 'CRM_Core_Error')) { |
782f39bf | 650 | CRM_Core_Error::fatal(ts("There was an error creating a contribution record for your event. Please report this error to the webmaster. Details: %1", array(1 => $contribution->getMessages($contribution)))); |
6a488035 TO |
651 | } |
652 | $mer_participant->contribution_id = $contribution->id; | |
653 | $params['contributionID'] = $contribution->id; | |
2b8d92de AN |
654 | |
655 | return $contribution; | |
6a488035 TO |
656 | } |
657 | ||
658 | function saveDataToSession() { | |
659 | $session_line_items = array(); | |
660 | foreach ($this->line_items as $line_item) { | |
661 | $session_line_item = array(); | |
662 | $session_line_item['amount'] = $line_item['amount']; | |
663 | $session_line_item['cost'] = $line_item['cost']; | |
664 | $session_line_item['event_id'] = $line_item['event']->id; | |
665 | $session_line_items[] = $session_line_item; | |
666 | } | |
667 | $this->set('line_items', $session_line_items); | |
668 | $this->set('payment_required', $this->payment_required); | |
669 | $this->set('is_pay_later', $this->is_pay_later); | |
670 | $this->set('pay_later_receipt', $this->pay_later_receipt); | |
671 | $this->set('trxn_id', $this->trxn_id); | |
672 | $this->set('trxn_date', $this->trxn_date); | |
673 | $this->set('total', $this->total); | |
674 | } | |
675 | ||
0cf587a7 EM |
676 | /** |
677 | * @return array | |
678 | */ | |
6a488035 TO |
679 | function setDefaultValues() { |
680 | ||
681 | $defaults = parent::setDefaultValues(); | |
682 | ||
683 | $config = CRM_Core_Config::singleton(); | |
684 | $default_country = new CRM_Core_DAO_Country(); | |
685 | $default_country->iso_code = $config->defaultContactCountry(); | |
686 | $default_country->find(TRUE); | |
687 | $defaults["billing_country_id-{$this->_bltID}"] = $default_country->id; | |
688 | ||
689 | if (self::getContactID() && !self::is_administrator()) { | |
690 | $params = array('id' => self::getContactID()); | |
691 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults); | |
692 | ||
693 | foreach ($contact->email as $email) { | |
694 | if ($email['is_billing']) { | |
695 | $defaults["billing_contact_email"] = $email['email']; | |
696 | } | |
697 | } | |
a7488080 | 698 | if (empty($defaults['billing_contact_email'])) { |
6a488035 TO |
699 | foreach ($contact->email as $email) { |
700 | if ($email['is_primary']) { | |
701 | $defaults["billing_contact_email"] = $email['email']; | |
702 | } | |
703 | } | |
704 | } | |
705 | ||
706 | $defaults["billing_first_name"] = $contact->first_name; | |
707 | $defaults["billing_middle_name"] = $contact->middle_name; | |
708 | $defaults["billing_last_name"] = $contact->last_name; | |
709 | ||
710 | $billing_address = CRM_Event_Cart_BAO_MerParticipant::billing_address_from_contact($contact); | |
711 | ||
712 | if ($billing_address != NULL) { | |
713 | $defaults["billing_street_address-{$this->_bltID}"] = $billing_address['street_address']; | |
714 | $defaults["billing_city-{$this->_bltID}"] = $billing_address['city']; | |
715 | $defaults["billing_postal_code-{$this->_bltID}"] = $billing_address['postal_code']; | |
716 | $defaults["billing_state_province_id-{$this->_bltID}"] = $billing_address['state_province_id']; | |
717 | $defaults["billing_country_id-{$this->_bltID}"] = $billing_address['country_id']; | |
718 | } | |
719 | } | |
720 | ||
721 | $defaults["source"] = $this->description; | |
722 | ||
723 | return $defaults; | |
724 | } | |
725 | } | |
726 |