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 | 20 | /** |
c490a46a | 21 | * @param array $params |
0cf587a7 EM |
22 | * @param $participant |
23 | * @param $event | |
24 | * | |
25 | * @return mixed | |
26 | */ | |
00be9182 | 27 | public function registerParticipant($params, &$participant, $event) { |
6a488035 TO |
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 | ||
00be9182 | 131 | public function buildPaymentFields() { |
6a488035 TO |
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 | ||
dfc68e82 | 163 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, FALSE); |
6a488035 TO |
164 | |
165 | if ($can_pay_later || self::is_administrator()) { | |
166 | $this->addElement('checkbox', 'is_pay_later', | |
167 | $pay_later_text | |
168 | ); | |
169 | $this->addElement('checkbox', 'payment_completed', | |
170 | ts('Payment Completed') | |
171 | ); | |
172 | $this->assign('pay_later_instructions', $this->pay_later_receipt); | |
173 | } | |
174 | } | |
175 | ||
00be9182 | 176 | public function buildQuickForm() { |
6a488035 | 177 | |
353ffa53 TO |
178 | $this->line_items = array(); |
179 | $this->sub_total = 0; | |
6a488035 TO |
180 | $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices'); |
181 | ||
182 | // iterate over each event in cart | |
183 | foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) { | |
184 | $this->process_event_line_item($event_in_cart); | |
185 | foreach ($this->cart->get_events_in_carts_by_main_event_id($event_in_cart->event_id) as $subevent) { | |
186 | $this->process_event_line_item($subevent, 'subevent'); | |
187 | } | |
188 | } | |
189 | ||
190 | $this->total = $this->sub_total; | |
191 | $this->payment_required = ($this->total > 0); | |
192 | $this->assign('payment_required', $this->payment_required); | |
193 | $this->assign('line_items', $this->line_items); | |
194 | $this->assign('sub_total', $this->sub_total); | |
195 | $this->assign('total', $this->total); | |
196 | $buttons = array(); | |
197 | $buttons[] = array( | |
f212d37d | 198 | 'name' => ts('Go Back'), |
6a488035 TO |
199 | 'spacing' => '  ', |
200 | 'type' => 'back', | |
201 | ); | |
202 | $buttons[] = array( | |
203 | 'isDefault' => TRUE, | |
f212d37d | 204 | 'name' => ts('Complete Transaction'), |
6a488035 TO |
205 | 'spacing' => ' ', |
206 | 'type' => 'next', | |
207 | ); | |
208 | ||
209 | if ($this->total) { | |
210 | $this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE); | |
211 | $this->assign('collect_billing_email', TRUE); | |
212 | } | |
213 | if (self::is_administrator()) { | |
214 | $this->add('textarea', 'note', 'Note'); | |
215 | $this->add('text', 'source', 'Source', array('size' => 80)); | |
216 | $instruments = array(); | |
217 | CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE); | |
218 | $options = array(); | |
219 | foreach ($instruments as $type) { | |
220 | $options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']); | |
221 | } | |
222 | $this->addGroup($options, 'payment_type', ts("Alternative Payment Type")); | |
223 | $this->add('text', 'check_number', ts('Check No.'), array('size' => 20)); | |
224 | $this->addElement('checkbox', 'is_pending', ts('Create a pending registration')); | |
225 | ||
226 | $this->assign('administrator', TRUE); | |
227 | } | |
228 | $this->addButtons($buttons); | |
229 | ||
230 | $this->addFormRule(array('CRM_Event_Cart_Form_Checkout_Payment', 'formRule'), $this); | |
231 | ||
232 | if ($this->payment_required) { | |
233 | $this->buildPaymentFields(); | |
234 | } | |
235 | } | |
236 | ||
0cf587a7 EM |
237 | /** |
238 | * @param $event_in_cart | |
239 | * @param null $class | |
240 | */ | |
00be9182 | 241 | public function process_event_line_item(&$event_in_cart, $class = NULL) { |
353ffa53 | 242 | $cost = 0; |
9da8dc8c | 243 | $price_set_id = CRM_Price_BAO_PriceSet::getFor("civicrm_event", $event_in_cart->event_id); |
6a488035 TO |
244 | $amount_level = NULL; |
245 | if ($price_set_id) { | |
246 | $event_price_values = array(); | |
247 | foreach ($this->_price_values as $key => $value) { | |
248 | if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) { | |
249 | $event_price_values[$matches[1]] = $value; | |
250 | } | |
251 | } | |
353ffa53 TO |
252 | $price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE); |
253 | $price_set = $price_sets[$price_set_id]; | |
6a488035 | 254 | $price_set_amount = array(); |
9da8dc8c | 255 | CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount); |
353ffa53 TO |
256 | $cost = $event_price_values['amount']; |
257 | $amount_level = $event_price_values['amount_level']; | |
2b8d92de | 258 | $price_details[$price_set_id] = $price_set_amount; |
6a488035 TO |
259 | } |
260 | ||
261 | // iterate over each participant in event | |
262 | foreach ($event_in_cart->participants as & $participant) { | |
263 | $participant->cost = $cost; | |
264 | $participant->fee_level = $amount_level; | |
2b8d92de | 265 | $participant->price_details = $price_details; |
6a488035 TO |
266 | } |
267 | ||
268 | $this->add_line_item($event_in_cart, $class); | |
269 | } | |
270 | ||
0cf587a7 EM |
271 | /** |
272 | * @param $event_in_cart | |
273 | * @param null $class | |
274 | */ | |
00be9182 | 275 | public function add_line_item($event_in_cart, $class = NULL) { |
6a488035 TO |
276 | $amount = 0; |
277 | $cost = 0; | |
278 | $not_waiting_participants = array(); | |
279 | foreach ($event_in_cart->not_waiting_participants() as $participant) { | |
280 | $amount += $participant->cost; | |
281 | $cost = max($cost, $participant->cost); | |
282 | $not_waiting_participants[] = array( | |
283 | 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id), | |
284 | ); | |
285 | } | |
286 | $waiting_participants = array(); | |
287 | foreach ($event_in_cart->waiting_participants() as $participant) { | |
288 | $waiting_participants[] = array( | |
289 | 'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id), | |
290 | ); | |
291 | } | |
292 | $this->line_items[] = array( | |
293 | 'amount' => $amount, | |
294 | 'cost' => $cost, | |
295 | 'event' => $event_in_cart->event, | |
296 | 'participants' => $not_waiting_participants, | |
297 | 'num_participants' => count($not_waiting_participants), | |
298 | 'num_waiting_participants' => count($waiting_participants), | |
299 | 'waiting_participants' => $waiting_participants, | |
300 | 'class' => $class, | |
301 | ); | |
302 | ||
303 | $this->sub_total += $amount; | |
304 | } | |
305 | ||
0cf587a7 EM |
306 | /** |
307 | * @return mixed | |
308 | */ | |
00be9182 | 309 | public function getDefaultFrom() { |
6a488035 TO |
310 | $values = CRM_Core_OptionGroup::values('from_email_address'); |
311 | return $values[1]; | |
312 | } | |
313 | ||
0cf587a7 EM |
314 | /** |
315 | * @param $events_in_cart | |
c490a46a | 316 | * @param array $params |
0cf587a7 | 317 | */ |
00be9182 | 318 | public function emailReceipt($events_in_cart, $params) { |
353ffa53 TO |
319 | $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id); |
320 | $state_province = new CRM_Core_DAO_StateProvince(); | |
6a488035 TO |
321 | $state_province->id = $params["billing_state_province_id-{$this->_bltID}"]; |
322 | $state_province->find(); | |
323 | $state_province->fetch(); | |
324 | $country = new CRM_Core_DAO_Country(); | |
325 | $country->id = $params["billing_country_id-{$this->_bltID}"]; | |
326 | $country->find(); | |
327 | $country->fetch(); | |
328 | foreach ($this->line_items as & $line_item) { | |
329 | $location_params = array('entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event'); | |
330 | $line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE); | |
331 | CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]); | |
332 | } | |
333 | $send_template_params = array( | |
334 | 'table' => 'civicrm_msg_template', | |
335 | 'contactId' => $this->payer_contact_id, | |
336 | 'from' => $this->getDefaultFrom(), | |
337 | 'groupName' => 'msg_tpl_workflow_event', | |
338 | 'isTest' => FALSE, | |
339 | 'toEmail' => $contact_details[1], | |
340 | 'toName' => $contact_details[0], | |
0479b4c8 | 341 | 'tplParams' => array( |
6a488035 TO |
342 | 'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}", |
343 | 'billing_city' => $params["billing_city-{$this->_bltID}"], | |
344 | 'billing_country' => $country->name, | |
345 | 'billing_postal_code' => $params["billing_postal_code-{$this->_bltID}"], | |
346 | 'billing_state' => $state_province->abbreviation, | |
347 | 'billing_street_address' => $params["billing_street_address-{$this->_bltID}"], | |
348 | 'credit_card_exp_date' => $params['credit_card_exp_date'], | |
349 | 'credit_card_type' => $params['credit_card_type'], | |
350 | 'credit_card_number' => "************" . substr($params['credit_card_number'], -4, 4), | |
351 | // XXX cart->get_discounts | |
352 | 'discounts' => $this->discounts, | |
353 | 'email' => $contact_details[1], | |
354 | 'events_in_cart' => $events_in_cart, | |
355 | 'line_items' => $this->line_items, | |
356 | 'name' => $contact_details[0], | |
357 | 'transaction_id' => $params['trxn_id'], | |
358 | 'transaction_date' => $params['trxn_date'], | |
359 | 'is_pay_later' => $this->is_pay_later, | |
360 | 'pay_later_receipt' => $this->pay_later_receipt, | |
361 | ), | |
362 | 'valueName' => 'event_registration_receipt', | |
92fcb95f | 363 | 'PDFFilename' => ts('confirmation') . '.pdf', |
6a488035 TO |
364 | ); |
365 | $template_params_to_copy = array( | |
366 | 'billing_name', | |
367 | 'billing_city', | |
368 | 'billing_country', | |
369 | 'billing_postal_code', | |
370 | 'billing_state', | |
371 | 'billing_street_address', | |
372 | 'credit_card_exp_date', | |
373 | 'credit_card_type', | |
374 | 'credit_card_number', | |
375 | ); | |
376 | foreach ($template_params_to_copy as $template_param_to_copy) { | |
377 | $this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]); | |
378 | } | |
379 | ||
c6327d7d | 380 | CRM_Core_BAO_MessageTemplate::sendTemplate($send_template_params); |
6a488035 TO |
381 | } |
382 | ||
0cf587a7 EM |
383 | /** |
384 | * @param $fields | |
385 | * @param $files | |
386 | * @param $self | |
387 | * | |
388 | * @return array|bool | |
389 | */ | |
00be9182 | 390 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
391 | $errors = array(); |
392 | ||
8cc574cf | 393 | if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) { |
7cb3d4f0 | 394 | CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); |
6a488035 | 395 | |
a479fe60 | 396 | // validate payment instrument values (e.g. credit card number) |
397 | CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, $self); | |
6a488035 TO |
398 | } |
399 | ||
400 | return empty($errors) ? TRUE : $errors; | |
401 | } | |
402 | ||
0cf587a7 EM |
403 | /** |
404 | * @return bool | |
405 | */ | |
00be9182 | 406 | public function validate() { |
6a488035 TO |
407 | if ($this->is_pay_later) { |
408 | $this->_fields['credit_card_number']['is_required'] = FALSE; | |
409 | $this->_fields['cvv2']['is_required'] = FALSE; | |
410 | $this->_fields['credit_card_exp_date']['is_required'] = FALSE; | |
411 | $this->_fields['credit_card_type']['is_required'] = FALSE; | |
412 | } | |
413 | return parent::validate(); | |
414 | } | |
415 | ||
00be9182 | 416 | public function preProcess() { |
6a488035 TO |
417 | $params = $this->_submitValues; |
418 | $this->is_pay_later = CRM_Utils_Array::value('is_pay_later', $params, FALSE) && !CRM_Utils_Array::value('payment_completed', $params); | |
419 | ||
420 | parent::preProcess(); | |
421 | } | |
422 | ||
00be9182 | 423 | public function postProcess() { |
6a488035 TO |
424 | |
425 | $transaction = new CRM_Core_Transaction(); | |
2b8d92de | 426 | $trxnDetails = NULL; |
353ffa53 | 427 | $params = $this->_submitValues; |
6a488035 TO |
428 | |
429 | $main_participants = $this->cart->get_main_event_participants(); | |
430 | foreach ($main_participants as $participant) { | |
353ffa53 TO |
431 | $defaults = array(); |
432 | $ids = array('contact_id' => $participant->contact_id); | |
433 | $contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults); | |
6a488035 TO |
434 | $contact->is_deleted = 0; |
435 | $contact->save(); | |
436 | } | |
437 | ||
438 | $trxn_prefix = 'VR'; | |
439 | if (array_key_exists('billing_contact_email', $params)) { | |
440 | $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), array( | |
353ffa53 TO |
441 | 'email' => $params['billing_contact_email'], |
442 | 'first_name' => $params['billing_first_name'], | |
443 | 'last_name' => $params['billing_last_name'], | |
444 | 'is_deleted' => FALSE, | |
445 | )); | |
6a488035 TO |
446 | |
447 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
448 | $this->payer_contact_id, | |
449 | 'contact_type' | |
450 | ); | |
451 | $billing_fields = array( | |
452 | "billing_first_name" => 1, | |
453 | "billing_middle_name" => 1, | |
454 | "billing_last_name" => 1, | |
455 | "billing_street_address-{$this->_bltID}" => 1, | |
456 | "billing_city-{$this->_bltID}" => 1, | |
457 | "billing_state_province_id-{$this->_bltID}" => 1, | |
458 | "billing_postal_code-{$this->_bltID}" => 1, | |
459 | "billing_country_id-{$this->_bltID}" => 1, | |
460 | "address_name-{$this->_bltID}" => 1, | |
461 | "email-{$this->_bltID}" => 1, | |
462 | ); | |
463 | ||
464 | $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); | |
465 | ||
466 | $params["email-{$this->_bltID}"] = $params['billing_contact_email']; | |
467 | CRM_Contact_BAO_Contact::createProfileContact( | |
468 | $params, | |
469 | $billing_fields, | |
470 | $this->payer_contact_id, | |
471 | NULL, | |
472 | NULL, | |
473 | $ctype, | |
474 | TRUE | |
475 | ); | |
476 | } | |
477 | ||
478 | $params['now'] = date('YmdHis'); | |
479 | $params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
480 | $params['amount'] = $this->total; | |
2b8d92de | 481 | $params['financial_type_id'] = $this->financial_type_id; |
8cc574cf | 482 | if ($this->payment_required && empty($params['is_pay_later'])) { |
2b8d92de AN |
483 | $trxnDetails = $this->make_payment($params); |
484 | $params['trxn_id'] = $trxnDetails['trxn_id']; | |
485 | $params['trxn_date'] = $trxnDetails['trxn_date']; | |
486 | $params['currencyID'] = $trxnDetails['currency']; | |
6a488035 TO |
487 | } |
488 | $this->cart->completed = TRUE; | |
489 | $this->cart->save(); | |
490 | $this->set('last_event_cart_id', $this->cart->id); | |
491 | ||
492 | $contribution_statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
493 | $params['payment_instrument_id'] = NULL; | |
a7488080 | 494 | if (!empty($params['is_pay_later'])) { |
6a488035 TO |
495 | $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name'); |
496 | $trxn_prefix = 'CK'; | |
497 | } | |
498 | else { | |
499 | $params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Credit Card', 'name'); | |
500 | } | |
8cc574cf | 501 | if ($this->is_pay_later && empty($params['payment_completed'])) { |
6a488035 TO |
502 | $params['contribution_status_id'] = array_search('Pending', $contribution_statuses); |
503 | } | |
504 | else { | |
505 | $params['contribution_status_id'] = array_search('Completed', $contribution_statuses); | |
506 | $params['participant_status'] = 'Registered'; | |
507 | $params['is_pay_later'] = 0; | |
508 | } | |
2b8d92de | 509 | if ($trxnDetails == NULL) { |
6a488035 TO |
510 | $params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S"); |
511 | $params['trxn_date'] = $params['now']; | |
512 | } | |
513 | ||
514 | if ($this->payment_required) { | |
515 | $this->emailReceipt($this->cart->events_in_carts, $params); | |
516 | } | |
517 | ||
518 | // n.b. we need to process the subparticipants before main event | |
519 | // participants so that session attendance can be included in the email | |
520 | $main_participants = $this->cart->get_main_event_participants(); | |
521 | $this->all_participants = array(); | |
522 | foreach ($main_participants as $main_participant) { | |
523 | $this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant)); | |
524 | } | |
525 | $this->all_participants = array_merge($this->all_participants, $main_participants); | |
526 | ||
527 | $this->sub_trxn_index = 0; | |
528 | foreach ($this->all_participants as $mer_participant) { | |
529 | $event_in_cart = $this->cart->get_event_in_cart_by_event_id($mer_participant->event_id); | |
530 | ||
531 | $this->sub_trxn_index += 1; | |
532 | ||
533 | unset($params['contributionID']); | |
534 | if ($mer_participant->must_wait) { | |
535 | $this->registerParticipant($params, $mer_participant, $event_in_cart->event); | |
536 | } | |
537 | else { | |
6a488035 TO |
538 | $params['amount'] = $mer_participant->cost - $mer_participant->discount_amount; |
539 | ||
2b8d92de AN |
540 | if ($event_in_cart->event->financial_type_id && $mer_participant->cost) { |
541 | $params['financial_type_id'] = $event_in_cart->event->financial_type_id; | |
6a488035 | 542 | $params['participant_contact_id'] = $mer_participant->contact_id; |
481a74f4 | 543 | $contribution = $this->record_contribution($mer_participant, $params, $event_in_cart->event); |
2b8d92de AN |
544 | // Record civicrm_line_item |
545 | CRM_Price_BAO_LineItem::processPriceSet($mer_participant->id, $mer_participant->price_details, $contribution, $entity_table = 'civicrm_participant'); | |
6a488035 TO |
546 | } |
547 | $this->registerParticipant($params, $mer_participant, $event_in_cart->event); | |
548 | } | |
549 | } | |
550 | $this->trxn_id = $params['trxn_id']; | |
551 | $this->trxn_date = $params['trxn_date']; | |
552 | $this->saveDataToSession(); | |
553 | $transaction->commit(); | |
554 | } | |
555 | ||
0cf587a7 | 556 | /** |
c490a46a | 557 | * @param array $params |
0cf587a7 | 558 | * |
971d41b1 | 559 | * @return array|void |
0cf587a7 EM |
560 | * @throws Exception |
561 | */ | |
00be9182 | 562 | public function make_payment(&$params) { |
6a488035 TO |
563 | $config = CRM_Core_Config::singleton(); |
564 | if (isset($params["billing_state_province_id-{$this->_bltID}"]) && $params["billing_state_province_id-{$this->_bltID}"]) { | |
565 | $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]); | |
566 | } | |
567 | ||
568 | if (isset($params["billing_country_id-{$this->_bltID}"]) && $params["billing_country_id-{$this->_bltID}"]) { | |
569 | $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]); | |
570 | } | |
571 | $params['ip_address'] = CRM_Utils_System::ipAddress(); | |
572 | $params['currencyID'] = $config->defaultCurrency; | |
6a488035 | 573 | |
077017db | 574 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); |
6a488035 TO |
575 | CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE); |
576 | $params['month'] = $params['credit_card_exp_date']['M']; | |
353ffa53 | 577 | $params['year'] = $params['credit_card_exp_date']['Y']; |
248141b9 EM |
578 | try { |
579 | $result = $payment->doPayment($params); | |
580 | } | |
581 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { | |
6a488035 TO |
582 | CRM_Core_Error::displaySessionError($result); |
583 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE)); | |
6a488035 | 584 | } |
248141b9 | 585 | |
2b8d92de | 586 | $trxnDetails = array( |
6a488035 | 587 | 'trxn_id' => $result['trxn_id'], |
2b8d92de AN |
588 | 'trxn_date' => $result['now'], |
589 | 'currency' => CRM_Utils_Array::value('currencyID', $result), | |
6a488035 | 590 | ); |
2b8d92de | 591 | return $trxnDetails; |
6a488035 TO |
592 | } |
593 | ||
0cf587a7 EM |
594 | /** |
595 | * @param $mer_participant | |
c490a46a | 596 | * @param array $params |
0cf587a7 EM |
597 | * @param $event |
598 | * | |
599 | * @return object | |
600 | * @throws Exception | |
601 | */ | |
00be9182 | 602 | public function record_contribution(&$mer_participant, &$params, $event) { |
8cc574cf | 603 | if (self::is_administrator() && !empty($params['payment_type'])) { |
6a488035 TO |
604 | $params['payment_instrument_id'] = $params['payment_type']; |
605 | } | |
606 | ||
607 | if ($this->payer_contact_id) { | |
608 | $payer = $this->payer_contact_id; | |
609 | } | |
610 | elseif (self::getContactID()) { | |
611 | $payer = self::getContactID(); | |
612 | } | |
613 | else { | |
614 | $payer = $params['participant_contact_id']; | |
615 | } | |
616 | ||
617 | $contribParams = array( | |
618 | 'contact_id' => $payer, | |
619 | 'financial_type_id' => $params['financial_type_id'], | |
620 | 'receive_date' => $params['now'], | |
621 | 'total_amount' => $params['amount'], | |
622 | 'amount_level' => $mer_participant->fee_level, | |
6a488035 TO |
623 | 'net_amount' => $params['amount'], |
624 | 'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}", | |
625 | 'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}", | |
626 | 'currency' => CRM_Utils_Array::value('currencyID', $params), | |
627 | 'source' => $event->title, | |
628 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), | |
629 | 'contribution_status_id' => $params['contribution_status_id'], | |
630 | 'payment_instrument_id' => $params['payment_instrument_id'], | |
631 | 'check_number' => CRM_Utils_Array::value('check_number', $params), | |
2b8d92de | 632 | 'skipLineItem' => 1, |
6a488035 TO |
633 | ); |
634 | ||
2b8d92de AN |
635 | if (is_array($this->_paymentProcessor)) { |
636 | $contribParams['payment_processor'] = $this->_paymentProcessor['id']; | |
637 | } | |
638 | ||
639 | $contribution = &CRM_Contribute_BAO_Contribution::add($contribParams); | |
6a488035 | 640 | if (is_a($contribution, 'CRM_Core_Error')) { |
782f39bf | 641 | 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 |
642 | } |
643 | $mer_participant->contribution_id = $contribution->id; | |
644 | $params['contributionID'] = $contribution->id; | |
2b8d92de AN |
645 | |
646 | return $contribution; | |
6a488035 TO |
647 | } |
648 | ||
00be9182 | 649 | public function saveDataToSession() { |
6a488035 TO |
650 | $session_line_items = array(); |
651 | foreach ($this->line_items as $line_item) { | |
652 | $session_line_item = array(); | |
653 | $session_line_item['amount'] = $line_item['amount']; | |
654 | $session_line_item['cost'] = $line_item['cost']; | |
655 | $session_line_item['event_id'] = $line_item['event']->id; | |
656 | $session_line_items[] = $session_line_item; | |
657 | } | |
658 | $this->set('line_items', $session_line_items); | |
659 | $this->set('payment_required', $this->payment_required); | |
660 | $this->set('is_pay_later', $this->is_pay_later); | |
661 | $this->set('pay_later_receipt', $this->pay_later_receipt); | |
662 | $this->set('trxn_id', $this->trxn_id); | |
663 | $this->set('trxn_date', $this->trxn_date); | |
664 | $this->set('total', $this->total); | |
665 | } | |
666 | ||
0cf587a7 EM |
667 | /** |
668 | * @return array | |
669 | */ | |
00be9182 | 670 | public function setDefaultValues() { |
6a488035 TO |
671 | |
672 | $defaults = parent::setDefaultValues(); | |
673 | ||
674 | $config = CRM_Core_Config::singleton(); | |
675 | $default_country = new CRM_Core_DAO_Country(); | |
676 | $default_country->iso_code = $config->defaultContactCountry(); | |
677 | $default_country->find(TRUE); | |
678 | $defaults["billing_country_id-{$this->_bltID}"] = $default_country->id; | |
679 | ||
680 | if (self::getContactID() && !self::is_administrator()) { | |
681 | $params = array('id' => self::getContactID()); | |
682 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults); | |
683 | ||
684 | foreach ($contact->email as $email) { | |
685 | if ($email['is_billing']) { | |
686 | $defaults["billing_contact_email"] = $email['email']; | |
687 | } | |
688 | } | |
a7488080 | 689 | if (empty($defaults['billing_contact_email'])) { |
6a488035 TO |
690 | foreach ($contact->email as $email) { |
691 | if ($email['is_primary']) { | |
692 | $defaults["billing_contact_email"] = $email['email']; | |
693 | } | |
694 | } | |
695 | } | |
696 | ||
697 | $defaults["billing_first_name"] = $contact->first_name; | |
698 | $defaults["billing_middle_name"] = $contact->middle_name; | |
699 | $defaults["billing_last_name"] = $contact->last_name; | |
700 | ||
701 | $billing_address = CRM_Event_Cart_BAO_MerParticipant::billing_address_from_contact($contact); | |
702 | ||
703 | if ($billing_address != NULL) { | |
704 | $defaults["billing_street_address-{$this->_bltID}"] = $billing_address['street_address']; | |
705 | $defaults["billing_city-{$this->_bltID}"] = $billing_address['city']; | |
706 | $defaults["billing_postal_code-{$this->_bltID}"] = $billing_address['postal_code']; | |
707 | $defaults["billing_state_province_id-{$this->_bltID}"] = $billing_address['state_province_id']; | |
708 | $defaults["billing_country_id-{$this->_bltID}"] = $billing_address['country_id']; | |
709 | } | |
710 | } | |
711 | ||
712 | $defaults["source"] = $this->description; | |
713 | ||
714 | return $defaults; | |
715 | } | |
96025800 | 716 | |
6a488035 | 717 | } |