Completed
Branch FET/gateways-sometimes-onsite-... (b4b4a7)
by
unknown
10:15 queued 07:55
created
reg_steps/payment_options/EE_SPCO_Reg_Step_Payment_Options.class.php 2 patches
Indentation   +2861 added lines, -2861 removed lines patch added patch discarded remove patch
@@ -12,2865 +12,2865 @@
 block discarded – undo
12 12
 class EE_SPCO_Reg_Step_Payment_Options extends EE_SPCO_Reg_Step
13 13
 {
14 14
 
15
-    /**
16
-     * @access protected
17
-     * @var EE_Line_Item_Display $Line_Item_Display
18
-     */
19
-    protected $line_item_display;
20
-
21
-    /**
22
-     * @access protected
23
-     * @var boolean $handle_IPN_in_this_request
24
-     */
25
-    protected $handle_IPN_in_this_request = false;
26
-
27
-
28
-    /**
29
-     *    set_hooks - for hooking into EE Core, other modules, etc
30
-     *
31
-     * @access    public
32
-     * @return    void
33
-     */
34
-    public static function set_hooks()
35
-    {
36
-        add_filter(
37
-            'FHEE__SPCO__EE_Line_Item_Filter_Collection',
38
-            array('EE_SPCO_Reg_Step_Payment_Options', 'add_spco_line_item_filters')
39
-        );
40
-        add_action(
41
-            'wp_ajax_switch_spco_billing_form',
42
-            array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form')
43
-        );
44
-        add_action(
45
-            'wp_ajax_nopriv_switch_spco_billing_form',
46
-            array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form')
47
-        );
48
-        add_action('wp_ajax_save_payer_details', array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details'));
49
-        add_action(
50
-            'wp_ajax_nopriv_save_payer_details',
51
-            array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details')
52
-        );
53
-        add_action(
54
-            'wp_ajax_get_transaction_details_for_gateways',
55
-            array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details')
56
-        );
57
-        add_action(
58
-            'wp_ajax_nopriv_get_transaction_details_for_gateways',
59
-            array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details')
60
-        );
61
-        add_filter(
62
-            'FHEE__EED_Recaptcha___bypass_recaptcha__bypass_request_params_array',
63
-            array('EE_SPCO_Reg_Step_Payment_Options', 'bypass_recaptcha_for_load_payment_method'),
64
-            10,
65
-            1
66
-        );
67
-    }
68
-
69
-
70
-    /**
71
-     *    ajax switch_spco_billing_form
72
-     *
73
-     * @throws \EE_Error
74
-     */
75
-    public static function switch_spco_billing_form()
76
-    {
77
-        EED_Single_Page_Checkout::process_ajax_request('switch_payment_method');
78
-    }
79
-
80
-
81
-    /**
82
-     *    ajax save_payer_details
83
-     *
84
-     * @throws \EE_Error
85
-     */
86
-    public static function save_payer_details()
87
-    {
88
-        EED_Single_Page_Checkout::process_ajax_request('save_payer_details_via_ajax');
89
-    }
90
-
91
-
92
-    /**
93
-     *    ajax get_transaction_details
94
-     *
95
-     * @throws \EE_Error
96
-     */
97
-    public static function get_transaction_details()
98
-    {
99
-        EED_Single_Page_Checkout::process_ajax_request('get_transaction_details_for_gateways');
100
-    }
101
-
102
-
103
-    /**
104
-     * bypass_recaptcha_for_load_payment_method
105
-     *
106
-     * @access public
107
-     * @return array
108
-     * @throws InvalidArgumentException
109
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
110
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
111
-     */
112
-    public static function bypass_recaptcha_for_load_payment_method()
113
-    {
114
-        return array(
115
-            'EESID'  => EE_Registry::instance()->SSN->id(),
116
-            'step'   => 'payment_options',
117
-            'action' => 'spco_billing_form',
118
-        );
119
-    }
120
-
121
-
122
-    /**
123
-     *    class constructor
124
-     *
125
-     * @access    public
126
-     * @param    EE_Checkout $checkout
127
-     */
128
-    public function __construct(EE_Checkout $checkout)
129
-    {
130
-        $this->_slug = 'payment_options';
131
-        $this->_name = esc_html__('Payment Options', 'event_espresso');
132
-        $this->_template = SPCO_REG_STEPS_PATH . $this->_slug . '/payment_options_main.template.php';
133
-        $this->checkout = $checkout;
134
-        $this->_reset_success_message();
135
-        $this->set_instructions(
136
-            esc_html__(
137
-                'Please select a method of payment and provide any necessary billing information before proceeding.',
138
-                'event_espresso'
139
-            )
140
-        );
141
-    }
142
-
143
-
144
-    /**
145
-     * @return null
146
-     */
147
-    public function line_item_display()
148
-    {
149
-        return $this->line_item_display;
150
-    }
151
-
152
-
153
-    /**
154
-     * @param null $line_item_display
155
-     */
156
-    public function set_line_item_display($line_item_display)
157
-    {
158
-        $this->line_item_display = $line_item_display;
159
-    }
160
-
161
-
162
-    /**
163
-     * @return boolean
164
-     */
165
-    public function handle_IPN_in_this_request()
166
-    {
167
-        return $this->handle_IPN_in_this_request;
168
-    }
169
-
170
-
171
-    /**
172
-     * @param boolean $handle_IPN_in_this_request
173
-     */
174
-    public function set_handle_IPN_in_this_request($handle_IPN_in_this_request)
175
-    {
176
-        $this->handle_IPN_in_this_request = filter_var($handle_IPN_in_this_request, FILTER_VALIDATE_BOOLEAN);
177
-    }
178
-
179
-
180
-    /**
181
-     * translate_js_strings
182
-     *
183
-     * @return void
184
-     */
185
-    public function translate_js_strings()
186
-    {
187
-        EE_Registry::$i18n_js_strings['no_payment_method'] = esc_html__(
188
-            'Please select a method of payment in order to continue.',
189
-            'event_espresso'
190
-        );
191
-        EE_Registry::$i18n_js_strings['invalid_payment_method'] = esc_html__(
192
-            'A valid method of payment could not be determined. Please refresh the page and try again.',
193
-            'event_espresso'
194
-        );
195
-        EE_Registry::$i18n_js_strings['forwarding_to_offsite'] = esc_html__(
196
-            'Forwarding to Secure Payment Provider.',
197
-            'event_espresso'
198
-        );
199
-    }
200
-
201
-
202
-    /**
203
-     * enqueue_styles_and_scripts
204
-     *
205
-     * @return void
206
-     * @throws EE_Error
207
-     * @throws InvalidArgumentException
208
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
209
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
210
-     */
211
-    public function enqueue_styles_and_scripts()
212
-    {
213
-        $transaction = $this->checkout->transaction;
214
-        // if the transaction isn't set or nothing is owed on it, don't enqueue any JS
215
-        if (! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) {
216
-            return;
217
-        }
218
-        foreach (EEM_Payment_Method::instance()->get_all_for_transaction(
219
-            $transaction,
220
-            EEM_Payment_Method::scope_cart
221
-        ) as $payment_method) {
222
-            $type_obj = $payment_method->type_obj();
223
-            if ($type_obj instanceof EE_PMT_Base) {
224
-                $billing_form = $type_obj->generate_new_billing_form($transaction);
225
-                if ($billing_form instanceof EE_Form_Section_Proper) {
226
-                    $billing_form->enqueue_js();
227
-                }
228
-            }
229
-        }
230
-    }
231
-
232
-
233
-    /**
234
-     * initialize_reg_step
235
-     *
236
-     * @return bool
237
-     * @throws EE_Error
238
-     * @throws InvalidArgumentException
239
-     * @throws ReflectionException
240
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
241
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
242
-     */
243
-    public function initialize_reg_step()
244
-    {
245
-        // TODO: if /when we implement donations, then this will need overriding
246
-        if (// don't need payment options for:
247
-            // registrations made via the admin
248
-            // completed transactions
249
-            // overpaid transactions
250
-            // $ 0.00 transactions(no payment required)
251
-            ! $this->checkout->payment_required()
252
-            // but do NOT remove if current action being called belongs to this reg step
253
-            && ! is_callable(array($this, $this->checkout->action))
254
-            && ! $this->completed()
255
-        ) {
256
-            // and if so, then we no longer need the Payment Options step
257
-            if ($this->is_current_step()) {
258
-                $this->checkout->generate_reg_form = false;
259
-            }
260
-            $this->checkout->remove_reg_step($this->_slug);
261
-            // DEBUG LOG
262
-            // $this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
263
-            return false;
264
-        }
265
-        // load EEM_Payment_Method
266
-        EE_Registry::instance()->load_model('Payment_Method');
267
-        // get all active payment methods
268
-        $this->checkout->available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction(
269
-            $this->checkout->transaction,
270
-            EEM_Payment_Method::scope_cart
271
-        );
272
-        return true;
273
-    }
274
-
275
-
276
-    /**
277
-     * @return EE_Form_Section_Proper
278
-     * @throws EE_Error
279
-     * @throws InvalidArgumentException
280
-     * @throws ReflectionException
281
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
282
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
283
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
284
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
285
-     */
286
-    public function generate_reg_form()
287
-    {
288
-        // reset in case someone changes their mind
289
-        $this->_reset_selected_method_of_payment();
290
-        // set some defaults
291
-        $this->checkout->selected_method_of_payment = 'payments_closed';
292
-        $registrations_requiring_payment = array();
293
-        $registrations_for_free_events = array();
294
-        $registrations_requiring_pre_approval = array();
295
-        $sold_out_events = array();
296
-        $insufficient_spaces_available = array();
297
-        $no_payment_required = true;
298
-        // loop thru registrations to gather info
299
-        $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params);
300
-        $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
301
-            $registrations,
302
-            $this->checkout->revisit
303
-        );
304
-        foreach ($registrations as $REG_ID => $registration) {
305
-            /** @var $registration EE_Registration */
306
-            // has this registration lost it's space ?
307
-            if (isset($ejected_registrations[ $REG_ID ])) {
308
-                if ($registration->event()->is_sold_out() || $registration->event()->is_sold_out(true)) {
309
-                    $sold_out_events[ $registration->event()->ID() ] = $registration->event();
310
-                } else {
311
-                    $insufficient_spaces_available[ $registration->event()->ID() ] = $registration->event();
312
-                }
313
-                continue;
314
-            }
315
-            // event requires admin approval
316
-            if ($registration->status_ID() === EEM_Registration::status_id_not_approved) {
317
-                // add event to list of events with pre-approval reg status
318
-                $registrations_requiring_pre_approval[ $REG_ID ] = $registration;
319
-                do_action(
320
-                    'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_pre_approval',
321
-                    $registration->event(),
322
-                    $this
323
-                );
324
-                continue;
325
-            }
326
-            if ($this->checkout->revisit
327
-                && $registration->status_ID() !== EEM_Registration::status_id_approved
328
-                && (
329
-                    $registration->event()->is_sold_out()
330
-                    || $registration->event()->is_sold_out(true)
331
-                )
332
-            ) {
333
-                // add event to list of events that are sold out
334
-                $sold_out_events[ $registration->event()->ID() ] = $registration->event();
335
-                do_action(
336
-                    'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__sold_out_event',
337
-                    $registration->event(),
338
-                    $this
339
-                );
340
-                continue;
341
-            }
342
-            // are they allowed to pay now and is there monies owing?
343
-            if ($registration->owes_monies_and_can_pay()) {
344
-                $registrations_requiring_payment[ $REG_ID ] = $registration;
345
-                do_action(
346
-                    'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_payment',
347
-                    $registration->event(),
348
-                    $this
349
-                );
350
-            } elseif (! $this->checkout->revisit
351
-                      && $registration->status_ID() !== EEM_Registration::status_id_not_approved
352
-                      && $registration->ticket()->is_free()
353
-            ) {
354
-                $registrations_for_free_events[ $registration->ticket()->ID() ] = $registration;
355
-            }
356
-        }
357
-        $subsections = array();
358
-        // now decide which template to load
359
-        if (! empty($sold_out_events)) {
360
-            $subsections['sold_out_events'] = $this->_sold_out_events($sold_out_events);
361
-        }
362
-        if (! empty($insufficient_spaces_available)) {
363
-            $subsections['insufficient_space'] = $this->_insufficient_spaces_available(
364
-                $insufficient_spaces_available
365
-            );
366
-        }
367
-        if (! empty($registrations_requiring_pre_approval)) {
368
-            $subsections['registrations_requiring_pre_approval'] = $this->_registrations_requiring_pre_approval(
369
-                $registrations_requiring_pre_approval
370
-            );
371
-        }
372
-        if (! empty($registrations_for_free_events)) {
373
-            $subsections['no_payment_required'] = $this->_no_payment_required($registrations_for_free_events);
374
-        }
375
-        if (! empty($registrations_requiring_payment)) {
376
-            if ($this->checkout->amount_owing > 0) {
377
-                // autoload Line_Item_Display classes
378
-                EEH_Autoloader::register_line_item_filter_autoloaders();
379
-                $line_item_filter_processor = new EE_Line_Item_Filter_Processor(
380
-                    apply_filters(
381
-                        'FHEE__SPCO__EE_Line_Item_Filter_Collection',
382
-                        new EE_Line_Item_Filter_Collection()
383
-                    ),
384
-                    $this->checkout->cart->get_grand_total()
385
-                );
386
-                /** @var EE_Line_Item $filtered_line_item_tree */
387
-                $filtered_line_item_tree = $line_item_filter_processor->process();
388
-                EEH_Autoloader::register_line_item_display_autoloaders();
389
-                $this->set_line_item_display(new EE_Line_Item_Display('spco'));
390
-                $subsections['payment_options'] = $this->_display_payment_options(
391
-                    $this->line_item_display->display_line_item(
392
-                        $filtered_line_item_tree,
393
-                        array('registrations' => $registrations)
394
-                    )
395
-                );
396
-                $this->checkout->amount_owing = $filtered_line_item_tree->total();
397
-                $this->_apply_registration_payments_to_amount_owing($registrations);
398
-            }
399
-            $no_payment_required = false;
400
-        } else {
401
-            $this->_hide_reg_step_submit_button_if_revisit();
402
-        }
403
-        $this->_save_selected_method_of_payment();
404
-
405
-        $subsections['default_hidden_inputs'] = $this->reg_step_hidden_inputs();
406
-        $subsections['extra_hidden_inputs'] = $this->_extra_hidden_inputs($no_payment_required);
407
-
408
-        return new EE_Form_Section_Proper(
409
-            array(
410
-                'name'            => $this->reg_form_name(),
411
-                'html_id'         => $this->reg_form_name(),
412
-                'subsections'     => $subsections,
413
-                'layout_strategy' => new EE_No_Layout(),
414
-            )
415
-        );
416
-    }
417
-
418
-
419
-    /**
420
-     * add line item filters required for this reg step
421
-     * these filters are applied via this line in EE_SPCO_Reg_Step_Payment_Options::set_hooks():
422
-     *        add_filter( 'FHEE__SPCO__EE_Line_Item_Filter_Collection', array( 'EE_SPCO_Reg_Step_Payment_Options',
423
-     *        'add_spco_line_item_filters' ) ); so any code that wants to use the same set of filters during the
424
-     *        payment options reg step, can apply these filters via the following: apply_filters(
425
-     *        'FHEE__SPCO__EE_Line_Item_Filter_Collection', new EE_Line_Item_Filter_Collection() ) or to an existing
426
-     *        filter collection by passing that instead of instantiating a new collection
427
-     *
428
-     * @param \EE_Line_Item_Filter_Collection $line_item_filter_collection
429
-     * @return EE_Line_Item_Filter_Collection
430
-     * @throws EE_Error
431
-     * @throws InvalidArgumentException
432
-     * @throws ReflectionException
433
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
434
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
435
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
436
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
437
-     */
438
-    public static function add_spco_line_item_filters(EE_Line_Item_Filter_Collection $line_item_filter_collection)
439
-    {
440
-        if (! EE_Registry::instance()->SSN instanceof EE_Session) {
441
-            return $line_item_filter_collection;
442
-        }
443
-        if (! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) {
444
-            return $line_item_filter_collection;
445
-        }
446
-        if (! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) {
447
-            return $line_item_filter_collection;
448
-        }
449
-        $line_item_filter_collection->add(
450
-            new EE_Billable_Line_Item_Filter(
451
-                EE_SPCO_Reg_Step_Payment_Options::remove_ejected_registrations(
452
-                    EE_Registry::instance()->SSN->checkout()->transaction->registrations(
453
-                        EE_Registry::instance()->SSN->checkout()->reg_cache_where_params
454
-                    )
455
-                )
456
-            )
457
-        );
458
-        $line_item_filter_collection->add(new EE_Non_Zero_Line_Item_Filter());
459
-        return $line_item_filter_collection;
460
-    }
461
-
462
-
463
-    /**
464
-     * remove_ejected_registrations
465
-     * if a registrant has lost their potential space at an event due to lack of payment,
466
-     * then this method removes them from the list of registrations being paid for during this request
467
-     *
468
-     * @param \EE_Registration[] $registrations
469
-     * @return EE_Registration[]
470
-     * @throws EE_Error
471
-     * @throws InvalidArgumentException
472
-     * @throws ReflectionException
473
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
474
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
475
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
476
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
477
-     */
478
-    public static function remove_ejected_registrations(array $registrations)
479
-    {
480
-        $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
481
-            $registrations,
482
-            EE_Registry::instance()->SSN->checkout()->revisit
483
-        );
484
-        foreach ($registrations as $REG_ID => $registration) {
485
-            // has this registration lost it's space ?
486
-            if (isset($ejected_registrations[ $REG_ID ])) {
487
-                unset($registrations[ $REG_ID ]);
488
-                continue;
489
-            }
490
-        }
491
-        return $registrations;
492
-    }
493
-
494
-
495
-    /**
496
-     * find_registrations_that_lost_their_space
497
-     * If a registrant chooses an offline payment method like Invoice,
498
-     * then no space is reserved for them at the event until they fully pay fo that site
499
-     * (unless the event's default reg status is set to APPROVED)
500
-     * if a registrant then later returns to pay, but the number of spaces available has been reduced due to sales,
501
-     * then this method will determine which registrations have lost the ability to complete the reg process.
502
-     *
503
-     * @param \EE_Registration[] $registrations
504
-     * @param bool               $revisit
505
-     * @return array
506
-     * @throws EE_Error
507
-     * @throws InvalidArgumentException
508
-     * @throws ReflectionException
509
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
510
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
511
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
512
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
513
-     */
514
-    public static function find_registrations_that_lost_their_space(array $registrations, $revisit = false)
515
-    {
516
-        // registrations per event
517
-        $event_reg_count = array();
518
-        // spaces left per event
519
-        $event_spaces_remaining = array();
520
-        // tickets left sorted by ID
521
-        $tickets_remaining = array();
522
-        // registrations that have lost their space
523
-        $ejected_registrations = array();
524
-        foreach ($registrations as $REG_ID => $registration) {
525
-            if ($registration->status_ID() === EEM_Registration::status_id_approved
526
-                || apply_filters(
527
-                    'FHEE__EE_SPCO_Reg_Step_Payment_Options__find_registrations_that_lost_their_space__allow_reg_payment',
528
-                    false,
529
-                    $registration,
530
-                    $revisit
531
-                )
532
-            ) {
533
-                continue;
534
-            }
535
-            $EVT_ID = $registration->event_ID();
536
-            $ticket = $registration->ticket();
537
-            if (! isset($tickets_remaining[ $ticket->ID() ])) {
538
-                $tickets_remaining[ $ticket->ID() ] = $ticket->remaining();
539
-            }
540
-            if ($tickets_remaining[ $ticket->ID() ] > 0) {
541
-                if (! isset($event_reg_count[ $EVT_ID ])) {
542
-                    $event_reg_count[ $EVT_ID ] = 0;
543
-                }
544
-                $event_reg_count[ $EVT_ID ]++;
545
-                if (! isset($event_spaces_remaining[ $EVT_ID ])) {
546
-                    $event_spaces_remaining[ $EVT_ID ] = $registration->event()->spaces_remaining_for_sale();
547
-                }
548
-            }
549
-            if ($revisit
550
-                && ($tickets_remaining[ $ticket->ID() ] === 0
551
-                    || $event_reg_count[ $EVT_ID ] > $event_spaces_remaining[ $EVT_ID ]
552
-                )
553
-            ) {
554
-                $ejected_registrations[ $REG_ID ] = $registration->event();
555
-                if ($registration->status_ID() !== EEM_Registration::status_id_wait_list) {
556
-                    /** @type EE_Registration_Processor $registration_processor */
557
-                    $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
558
-                    // at this point, we should have enough details about the registrant to consider the registration
559
-                    // NOT incomplete
560
-                    $registration_processor->manually_update_registration_status(
561
-                        $registration,
562
-                        EEM_Registration::status_id_wait_list
563
-                    );
564
-                }
565
-            }
566
-        }
567
-        return $ejected_registrations;
568
-    }
569
-
570
-
571
-    /**
572
-     * _hide_reg_step_submit_button
573
-     * removes the html for the reg step submit button
574
-     * by replacing it with an empty string via filter callback
575
-     *
576
-     * @return void
577
-     */
578
-    protected function _adjust_registration_status_if_event_old_sold()
579
-    {
580
-    }
581
-
582
-
583
-    /**
584
-     * _hide_reg_step_submit_button
585
-     * removes the html for the reg step submit button
586
-     * by replacing it with an empty string via filter callback
587
-     *
588
-     * @return void
589
-     */
590
-    protected function _hide_reg_step_submit_button_if_revisit()
591
-    {
592
-        if ($this->checkout->revisit) {
593
-            add_filter('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', '__return_empty_string');
594
-        }
595
-    }
596
-
597
-
598
-    /**
599
-     * sold_out_events
600
-     * displays notices regarding events that have sold out since hte registrant first signed up
601
-     *
602
-     * @param \EE_Event[] $sold_out_events_array
603
-     * @return \EE_Form_Section_Proper
604
-     * @throws \EE_Error
605
-     */
606
-    private function _sold_out_events($sold_out_events_array = array())
607
-    {
608
-        // set some defaults
609
-        $this->checkout->selected_method_of_payment = 'events_sold_out';
610
-        $sold_out_events = '';
611
-        foreach ($sold_out_events_array as $sold_out_event) {
612
-            $sold_out_events .= EEH_HTML::li(
613
-                EEH_HTML::span(
614
-                    '  ' . $sold_out_event->name(),
615
-                    '',
616
-                    'dashicons dashicons-marker ee-icon-size-16 pink-text'
617
-                )
618
-            );
619
-        }
620
-        return new EE_Form_Section_Proper(
621
-            array(
622
-                'layout_strategy' => new EE_Template_Layout(
623
-                    array(
624
-                        'layout_template_file' => SPCO_REG_STEPS_PATH
625
-                                                  . $this->_slug
626
-                                                  . '/sold_out_events.template.php',
627
-                        'template_args'        => apply_filters(
628
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args',
629
-                            array(
630
-                                'sold_out_events'     => $sold_out_events,
631
-                                'sold_out_events_msg' => apply_filters(
632
-                                    'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__sold_out_events_msg',
633
-                                    sprintf(
634
-                                        esc_html__(
635
-                                            'It appears that the event you were about to make a payment for has sold out since you first registered. If you have already made a partial payment towards this event, please contact the event administrator for a refund.%3$s%3$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%2$s',
636
-                                            'event_espresso'
637
-                                        ),
638
-                                        '<strong>',
639
-                                        '</strong>',
640
-                                        '<br />'
641
-                                    )
642
-                                ),
643
-                            )
644
-                        ),
645
-                    )
646
-                ),
647
-            )
648
-        );
649
-    }
650
-
651
-
652
-    /**
653
-     * _insufficient_spaces_available
654
-     * displays notices regarding events that do not have enough remaining spaces
655
-     * to satisfy the current number of registrations looking to pay
656
-     *
657
-     * @param \EE_Event[] $insufficient_spaces_events_array
658
-     * @return \EE_Form_Section_Proper
659
-     * @throws \EE_Error
660
-     */
661
-    private function _insufficient_spaces_available($insufficient_spaces_events_array = array())
662
-    {
663
-        // set some defaults
664
-        $this->checkout->selected_method_of_payment = 'invoice';
665
-        $insufficient_space_events = '';
666
-        foreach ($insufficient_spaces_events_array as $event) {
667
-            if ($event instanceof EE_Event) {
668
-                $insufficient_space_events .= EEH_HTML::li(
669
-                    EEH_HTML::span(' ' . $event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text')
670
-                );
671
-            }
672
-        }
673
-        return new EE_Form_Section_Proper(
674
-            array(
675
-                'subsections'     => array(
676
-                    'default_hidden_inputs' => $this->reg_step_hidden_inputs(),
677
-                    'extra_hidden_inputs'   => $this->_extra_hidden_inputs(),
678
-                ),
679
-                'layout_strategy' => new EE_Template_Layout(
680
-                    array(
681
-                        'layout_template_file' => SPCO_REG_STEPS_PATH
682
-                                                  . $this->_slug
683
-                                                  . '/sold_out_events.template.php',
684
-                        'template_args'        => apply_filters(
685
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__template_args',
686
-                            array(
687
-                                'sold_out_events'     => $insufficient_space_events,
688
-                                'sold_out_events_msg' => apply_filters(
689
-                                    'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__insufficient_space_msg',
690
-                                    esc_html__(
691
-                                        'It appears that the event you were about to make a payment for has sold additional tickets since you first registered, and there are no longer enough spaces left to accommodate your selections. You may continue to pay and secure the available space(s) remaining, or simply cancel if you no longer wish to purchase. If you have already made a partial payment towards this event, please contact the event administrator for a refund.',
692
-                                        'event_espresso'
693
-                                    )
694
-                                ),
695
-                            )
696
-                        ),
697
-                    )
698
-                ),
699
-            )
700
-        );
701
-    }
702
-
703
-
704
-    /**
705
-     * registrations_requiring_pre_approval
706
-     *
707
-     * @param array $registrations_requiring_pre_approval
708
-     * @return EE_Form_Section_Proper
709
-     * @throws EE_Error
710
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
711
-     */
712
-    private function _registrations_requiring_pre_approval($registrations_requiring_pre_approval = array())
713
-    {
714
-        $events_requiring_pre_approval = array();
715
-        foreach ($registrations_requiring_pre_approval as $registration) {
716
-            if ($registration instanceof EE_Registration && $registration->event() instanceof EE_Event) {
717
-                $events_requiring_pre_approval[ $registration->event()->ID() ] = EEH_HTML::li(
718
-                    EEH_HTML::span(
719
-                        '',
720
-                        '',
721
-                        'dashicons dashicons-marker ee-icon-size-16 orange-text'
722
-                    )
723
-                    . EEH_HTML::span($registration->event()->name(), '', 'orange-text')
724
-                );
725
-            }
726
-        }
727
-        return new EE_Form_Section_Proper(
728
-            array(
729
-                'layout_strategy' => new EE_Template_Layout(
730
-                    array(
731
-                        'layout_template_file' => SPCO_REG_STEPS_PATH
732
-                                                  . $this->_slug
733
-                                                  . '/events_requiring_pre_approval.template.php', // layout_template
734
-                        'template_args'        => apply_filters(
735
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args',
736
-                            array(
737
-                                'events_requiring_pre_approval'     => implode('', $events_requiring_pre_approval),
738
-                                'events_requiring_pre_approval_msg' => apply_filters(
739
-                                    'FHEE__EE_SPCO_Reg_Step_Payment_Options___events_requiring_pre_approval__events_requiring_pre_approval_msg',
740
-                                    esc_html__(
741
-                                        'The following events do not require payment at this time and will not be billed during this transaction. Billing will only occur after the attendee has been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.',
742
-                                        'event_espresso'
743
-                                    )
744
-                                ),
745
-                            )
746
-                        ),
747
-                    )
748
-                ),
749
-            )
750
-        );
751
-    }
752
-
753
-
754
-    /**
755
-     * _no_payment_required
756
-     *
757
-     * @param \EE_Event[] $registrations_for_free_events
758
-     * @return \EE_Form_Section_Proper
759
-     * @throws \EE_Error
760
-     */
761
-    private function _no_payment_required($registrations_for_free_events = array())
762
-    {
763
-        // set some defaults
764
-        $this->checkout->selected_method_of_payment = 'no_payment_required';
765
-        // generate no_payment_required form
766
-        return new EE_Form_Section_Proper(
767
-            array(
768
-                'layout_strategy' => new EE_Template_Layout(
769
-                    array(
770
-                        'layout_template_file' => SPCO_REG_STEPS_PATH
771
-                                                  . $this->_slug
772
-                                                  . '/no_payment_required.template.php', // layout_template
773
-                        'template_args'        => apply_filters(
774
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___no_payment_required__template_args',
775
-                            array(
776
-                                'revisit'                       => $this->checkout->revisit,
777
-                                'registrations'                 => array(),
778
-                                'ticket_count'                  => array(),
779
-                                'registrations_for_free_events' => $registrations_for_free_events,
780
-                                'no_payment_required_msg'       => EEH_HTML::p(
781
-                                    esc_html__('This is a free event, so no billing will occur.', 'event_espresso')
782
-                                ),
783
-                            )
784
-                        ),
785
-                    )
786
-                ),
787
-            )
788
-        );
789
-    }
790
-
791
-
792
-    /**
793
-     * _display_payment_options
794
-     *
795
-     * @param string $transaction_details
796
-     * @return EE_Form_Section_Proper
797
-     * @throws EE_Error
798
-     * @throws InvalidArgumentException
799
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
800
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
801
-     */
802
-    private function _display_payment_options($transaction_details = '')
803
-    {
804
-        // has method_of_payment been set by no-js user?
805
-        $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment();
806
-        // build payment options form
807
-        return apply_filters(
808
-            'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__payment_options_form',
809
-            new EE_Form_Section_Proper(
810
-                array(
811
-                    'subsections'     => array(
812
-                        'before_payment_options' => apply_filters(
813
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__before_payment_options',
814
-                            new EE_Form_Section_Proper(
815
-                                array('layout_strategy' => new EE_Div_Per_Section_Layout())
816
-                            )
817
-                        ),
818
-                        'payment_options'        => $this->_setup_payment_options(),
819
-                        'after_payment_options'  => apply_filters(
820
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__after_payment_options',
821
-                            new EE_Form_Section_Proper(
822
-                                array('layout_strategy' => new EE_Div_Per_Section_Layout())
823
-                            )
824
-                        ),
825
-                    ),
826
-                    'layout_strategy' => new EE_Template_Layout(
827
-                        array(
828
-                            'layout_template_file' => $this->_template,
829
-                            'template_args'        => apply_filters(
830
-                                'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__template_args',
831
-                                array(
832
-                                    'reg_count'                 => $this->line_item_display->total_items(),
833
-                                    'transaction_details'       => $transaction_details,
834
-                                    'available_payment_methods' => array(),
835
-                                )
836
-                            ),
837
-                        )
838
-                    ),
839
-                )
840
-            )
841
-        );
842
-    }
843
-
844
-
845
-    /**
846
-     * _extra_hidden_inputs
847
-     *
848
-     * @param bool $no_payment_required
849
-     * @return \EE_Form_Section_Proper
850
-     * @throws \EE_Error
851
-     */
852
-    private function _extra_hidden_inputs($no_payment_required = true)
853
-    {
854
-        return new EE_Form_Section_Proper(
855
-            array(
856
-                'html_id'         => 'ee-' . $this->slug() . '-extra-hidden-inputs',
857
-                'layout_strategy' => new EE_Div_Per_Section_Layout(),
858
-                'subsections'     => array(
859
-                    'spco_no_payment_required' => new EE_Hidden_Input(
860
-                        array(
861
-                            'normalization_strategy' => new EE_Boolean_Normalization(),
862
-                            'html_name'              => 'spco_no_payment_required',
863
-                            'html_id'                => 'spco-no-payment-required-payment_options',
864
-                            'default'                => $no_payment_required,
865
-                        )
866
-                    ),
867
-                    'spco_transaction_id'      => new EE_Fixed_Hidden_Input(
868
-                        array(
869
-                            'normalization_strategy' => new EE_Int_Normalization(),
870
-                            'html_name'              => 'spco_transaction_id',
871
-                            'html_id'                => 'spco-transaction-id',
872
-                            'default'                => $this->checkout->transaction->ID(),
873
-                        )
874
-                    ),
875
-                ),
876
-            )
877
-        );
878
-    }
879
-
880
-
881
-    /**
882
-     *    _apply_registration_payments_to_amount_owing
883
-     *
884
-     * @access protected
885
-     * @param array $registrations
886
-     * @throws EE_Error
887
-     */
888
-    protected function _apply_registration_payments_to_amount_owing(array $registrations)
889
-    {
890
-        $payments = array();
891
-        foreach ($registrations as $registration) {
892
-            if ($registration instanceof EE_Registration && $registration->owes_monies_and_can_pay()) {
893
-                $payments += $registration->registration_payments();
894
-            }
895
-        }
896
-        if (! empty($payments)) {
897
-            foreach ($payments as $payment) {
898
-                if ($payment instanceof EE_Registration_Payment) {
899
-                    $this->checkout->amount_owing -= $payment->amount();
900
-                }
901
-            }
902
-        }
903
-    }
904
-
905
-
906
-    /**
907
-     *    _reset_selected_method_of_payment
908
-     *
909
-     * @access    private
910
-     * @param    bool $force_reset
911
-     * @return void
912
-     * @throws InvalidArgumentException
913
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
914
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
915
-     */
916
-    private function _reset_selected_method_of_payment($force_reset = false)
917
-    {
918
-        $reset_payment_method = $force_reset
919
-            ? true
920
-            : sanitize_text_field(EE_Registry::instance()->REQ->get('reset_payment_method', false));
921
-        if ($reset_payment_method) {
922
-            $this->checkout->selected_method_of_payment = null;
923
-            $this->checkout->payment_method = null;
924
-            $this->checkout->billing_form = null;
925
-            $this->_save_selected_method_of_payment();
926
-        }
927
-    }
928
-
929
-
930
-    /**
931
-     * _save_selected_method_of_payment
932
-     * stores the selected_method_of_payment in the session
933
-     * so that it's available for all subsequent requests including AJAX
934
-     *
935
-     * @access        private
936
-     * @param string $selected_method_of_payment
937
-     * @return void
938
-     * @throws InvalidArgumentException
939
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
940
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
941
-     */
942
-    private function _save_selected_method_of_payment($selected_method_of_payment = '')
943
-    {
944
-        $selected_method_of_payment = ! empty($selected_method_of_payment)
945
-            ? $selected_method_of_payment
946
-            : $this->checkout->selected_method_of_payment;
947
-        EE_Registry::instance()->SSN->set_session_data(
948
-            array('selected_method_of_payment' => $selected_method_of_payment)
949
-        );
950
-    }
951
-
952
-
953
-    /**
954
-     * _setup_payment_options
955
-     *
956
-     * @return EE_Form_Section_Proper
957
-     * @throws EE_Error
958
-     * @throws InvalidArgumentException
959
-     * @throws ReflectionException
960
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
961
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
962
-     */
963
-    public function _setup_payment_options()
964
-    {
965
-        // load payment method classes
966
-        $this->checkout->available_payment_methods = $this->_get_available_payment_methods();
967
-        if (empty($this->checkout->available_payment_methods)) {
968
-            EE_Error::add_error(
969
-                apply_filters(
970
-                    'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__error_message_no_payment_methods',
971
-                    sprintf(
972
-                        esc_html__(
973
-                            'Sorry, you cannot complete your purchase because a payment method is not active.%1$s Please contact %2$s for assistance and provide a description of the problem.',
974
-                            'event_espresso'
975
-                        ),
976
-                        '<br>',
977
-                        EE_Registry::instance()->CFG->organization->get_pretty('email')
978
-                    )
979
-                ),
980
-                __FILE__,
981
-                __FUNCTION__,
982
-                __LINE__
983
-            );
984
-        }
985
-        // switch up header depending on number of available payment methods
986
-        $payment_method_header = count($this->checkout->available_payment_methods) > 1
987
-            ? apply_filters(
988
-                'FHEE__registration_page_payment_options__method_of_payment_hdr',
989
-                esc_html__('Please Select Your Method of Payment', 'event_espresso')
990
-            )
991
-            : apply_filters(
992
-                'FHEE__registration_page_payment_options__method_of_payment_hdr',
993
-                esc_html__('Method of Payment', 'event_espresso')
994
-            );
995
-        $available_payment_methods = array(
996
-            // display the "Payment Method" header
997
-            'payment_method_header' => new EE_Form_Section_HTML(
998
-                apply_filters(
999
-                    'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__payment_method_header',
1000
-                    EEH_HTML::h4($payment_method_header, 'method-of-payment-hdr'),
1001
-                    $payment_method_header
1002
-                )
1003
-            ),
1004
-        );
1005
-        // the list of actual payment methods ( invoice, paypal, etc ) in a  ( slug => HTML )  format
1006
-        $available_payment_method_options = array();
1007
-        $default_payment_method_option = array();
1008
-        // additional instructions to be displayed and hidden below payment methods (adding a clearing div to start)
1009
-        $payment_methods_billing_info = array(
1010
-            new EE_Form_Section_HTML(
1011
-                EEH_HTML::div('<br />', '', '', 'clear:both;')
1012
-            ),
1013
-        );
1014
-        // loop through payment methods
1015
-        foreach ($this->checkout->available_payment_methods as $payment_method) {
1016
-            if ($payment_method instanceof EE_Payment_Method) {
1017
-                $payment_method_button = EEH_HTML::img(
1018
-                    $payment_method->button_url(),
1019
-                    $payment_method->name(),
1020
-                    'spco-payment-method-' . $payment_method->slug() . '-btn-img',
1021
-                    'spco-payment-method-btn-img'
1022
-                );
1023
-                // check if any payment methods are set as default
1024
-                // if payment method is already selected OR nothing is selected and this payment method should be
1025
-                // open_by_default
1026
-                if (($this->checkout->selected_method_of_payment === $payment_method->slug())
1027
-                    || (! $this->checkout->selected_method_of_payment && $payment_method->open_by_default())
1028
-                ) {
1029
-                    $this->checkout->selected_method_of_payment = $payment_method->slug();
1030
-                    $this->_save_selected_method_of_payment();
1031
-                    $default_payment_method_option[ $payment_method->slug() ] = $payment_method_button;
1032
-                } else {
1033
-                    $available_payment_method_options[ $payment_method->slug() ] = $payment_method_button;
1034
-                }
1035
-                $payment_methods_billing_info[ $payment_method->slug(
1036
-                ) . '-info' ] = $this->_payment_method_billing_info(
1037
-                    $payment_method
1038
-                );
1039
-            }
1040
-        }
1041
-        // prepend available_payment_method_options with default_payment_method_option so that it appears first in list
1042
-        // of PMs
1043
-        $available_payment_method_options = $default_payment_method_option + $available_payment_method_options;
1044
-        // now generate the actual form  inputs
1045
-        $available_payment_methods['available_payment_methods'] = $this->_available_payment_method_inputs(
1046
-            $available_payment_method_options
1047
-        );
1048
-        $available_payment_methods += $payment_methods_billing_info;
1049
-        // build the available payment methods form
1050
-        return new EE_Form_Section_Proper(
1051
-            array(
1052
-                'html_id'         => 'spco-available-methods-of-payment-dv',
1053
-                'subsections'     => $available_payment_methods,
1054
-                'layout_strategy' => new EE_Div_Per_Section_Layout(),
1055
-            )
1056
-        );
1057
-    }
1058
-
1059
-
1060
-    /**
1061
-     * _get_available_payment_methods
1062
-     *
1063
-     * @return EE_Payment_Method[]
1064
-     * @throws EE_Error
1065
-     * @throws InvalidArgumentException
1066
-     * @throws ReflectionException
1067
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1068
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1069
-     */
1070
-    protected function _get_available_payment_methods()
1071
-    {
1072
-        if (! empty($this->checkout->available_payment_methods)) {
1073
-            return $this->checkout->available_payment_methods;
1074
-        }
1075
-        $available_payment_methods = array();
1076
-        // load EEM_Payment_Method
1077
-        EE_Registry::instance()->load_model('Payment_Method');
1078
-        /** @type EEM_Payment_Method $EEM_Payment_Method */
1079
-        $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method;
1080
-        // get all active payment methods
1081
-        $payment_methods = $EEM_Payment_Method->get_all_for_transaction(
1082
-            $this->checkout->transaction,
1083
-            EEM_Payment_Method::scope_cart
1084
-        );
1085
-        foreach ($payment_methods as $payment_method) {
1086
-            if ($payment_method instanceof EE_Payment_Method) {
1087
-                $available_payment_methods[ $payment_method->slug() ] = $payment_method;
1088
-            }
1089
-        }
1090
-        return $available_payment_methods;
1091
-    }
1092
-
1093
-
1094
-    /**
1095
-     *    _available_payment_method_inputs
1096
-     *
1097
-     * @access    private
1098
-     * @param    array $available_payment_method_options
1099
-     * @return    \EE_Form_Section_Proper
1100
-     */
1101
-    private function _available_payment_method_inputs($available_payment_method_options = array())
1102
-    {
1103
-        // generate inputs
1104
-        return new EE_Form_Section_Proper(
1105
-            array(
1106
-                'html_id'         => 'ee-available-payment-method-inputs',
1107
-                'layout_strategy' => new EE_Div_Per_Section_Layout(),
1108
-                'subsections'     => array(
1109
-                    '' => new EE_Radio_Button_Input(
1110
-                        $available_payment_method_options,
1111
-                        array(
1112
-                            'html_name'          => 'selected_method_of_payment',
1113
-                            'html_class'         => 'spco-payment-method',
1114
-                            'default'            => $this->checkout->selected_method_of_payment,
1115
-                            'label_size'         => 11,
1116
-                            'enforce_label_size' => true,
1117
-                        )
1118
-                    ),
1119
-                ),
1120
-            )
1121
-        );
1122
-    }
1123
-
1124
-
1125
-    /**
1126
-     *    _payment_method_billing_info
1127
-     *
1128
-     * @access    private
1129
-     * @param    EE_Payment_Method $payment_method
1130
-     * @return EE_Form_Section_Proper
1131
-     * @throws EE_Error
1132
-     * @throws InvalidArgumentException
1133
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1134
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1135
-     */
1136
-    private function _payment_method_billing_info(EE_Payment_Method $payment_method)
1137
-    {
1138
-        $currently_selected = $this->checkout->selected_method_of_payment === $payment_method->slug()
1139
-            ? true
1140
-            : false;
1141
-        // generate the billing form for payment method
1142
-        $billing_form = $currently_selected
1143
-            ? $this->_get_billing_form_for_payment_method($payment_method)
1144
-            : new EE_Form_Section_HTML();
1145
-        $this->checkout->billing_form = $currently_selected
1146
-            ? $billing_form
1147
-            : $this->checkout->billing_form;
1148
-        // it's all in the details
1149
-        $info_html = EEH_HTML::h3(
1150
-            esc_html__('Important information regarding your payment', 'event_espresso'),
1151
-            '',
1152
-            'spco-payment-method-hdr'
1153
-        );
1154
-        // add some info regarding the step, either from what's saved in the admin,
1155
-        // or a default string depending on whether the PM has a billing form or not
1156
-        if ($payment_method->description()) {
1157
-            $payment_method_info = $payment_method->description();
1158
-        } elseif ($billing_form instanceof EE_Billing_Info_Form) {
1159
-            $payment_method_info = sprintf(
1160
-                esc_html__(
1161
-                    'Please provide the following billing information, then click the "%1$s" button below in order to proceed.',
1162
-                    'event_espresso'
1163
-                ),
1164
-                $this->submit_button_text()
1165
-            );
1166
-        } else {
1167
-            $payment_method_info = sprintf(
1168
-                esc_html__('Please click the "%1$s" button below in order to proceed.', 'event_espresso'),
1169
-                $this->submit_button_text()
1170
-            );
1171
-        }
1172
-        $info_html .= EEH_HTML::div(
1173
-            apply_filters(
1174
-                'FHEE__EE_SPCO_Reg_Step_Payment_Options___payment_method_billing_info__payment_method_info',
1175
-                $payment_method_info
1176
-            ),
1177
-            '',
1178
-            'spco-payment-method-desc ee-attention'
1179
-        );
1180
-        return new EE_Form_Section_Proper(
1181
-            array(
1182
-                'html_id'         => 'spco-payment-method-info-' . $payment_method->slug(),
1183
-                'html_class'      => 'spco-payment-method-info-dv',
1184
-                // only display the selected or default PM
1185
-                'html_style'      => $currently_selected ? '' : 'display:none;',
1186
-                'layout_strategy' => new EE_Div_Per_Section_Layout(),
1187
-                'subsections'     => array(
1188
-                    'info'         => new EE_Form_Section_HTML($info_html),
1189
-                    'billing_form' => $currently_selected ? $billing_form : new EE_Form_Section_HTML(),
1190
-                ),
1191
-            )
1192
-        );
1193
-    }
1194
-
1195
-
1196
-    /**
1197
-     * get_billing_form_html_for_payment_method
1198
-     *
1199
-     * @access public
1200
-     * @return string
1201
-     * @throws EE_Error
1202
-     * @throws InvalidArgumentException
1203
-     * @throws ReflectionException
1204
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1205
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1206
-     */
1207
-    public function get_billing_form_html_for_payment_method()
1208
-    {
1209
-        // how have they chosen to pay?
1210
-        $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
1211
-        $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1212
-        if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1213
-            return false;
1214
-        }
1215
-        if (apply_filters(
1216
-            'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1217
-            false
1218
-        )) {
1219
-            EE_Error::add_success(
1220
-                apply_filters(
1221
-                    'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1222
-                    sprintf(
1223
-                        esc_html__(
1224
-                            'You have selected "%s" as your method of payment. Please note the important payment information below.',
1225
-                            'event_espresso'
1226
-                        ),
1227
-                        $this->checkout->payment_method->name()
1228
-                    )
1229
-                )
1230
-            );
1231
-        }
1232
-        // now generate billing form for selected method of payment
1233
-        $payment_method_billing_form = $this->_get_billing_form_for_payment_method($this->checkout->payment_method);
1234
-        // fill form with attendee info if applicable
1235
-        if ($payment_method_billing_form instanceof EE_Billing_Attendee_Info_Form
1236
-            && $this->checkout->transaction_has_primary_registrant()
1237
-        ) {
1238
-            $payment_method_billing_form->populate_from_attendee(
1239
-                $this->checkout->transaction->primary_registration()->attendee()
1240
-            );
1241
-        }
1242
-        // and debug content
1243
-        if ($payment_method_billing_form instanceof EE_Billing_Info_Form
1244
-            && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base
1245
-        ) {
1246
-            $payment_method_billing_form =
1247
-                $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings(
1248
-                    $payment_method_billing_form
1249
-                );
1250
-        }
1251
-        $billing_info = $payment_method_billing_form instanceof EE_Form_Section_Proper
1252
-            ? $payment_method_billing_form->get_html()
1253
-            : '';
1254
-        $this->checkout->json_response->set_return_data(array('payment_method_info' => $billing_info));
1255
-        // localize validation rules for main form
1256
-        $this->checkout->current_step->reg_form->localize_validation_rules();
1257
-        $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
1258
-        return true;
1259
-    }
1260
-
1261
-
1262
-    /**
1263
-     * _get_billing_form_for_payment_method
1264
-     *
1265
-     * @access private
1266
-     * @param EE_Payment_Method $payment_method
1267
-     * @return EE_Billing_Info_Form|EE_Form_Section_HTML
1268
-     * @throws EE_Error
1269
-     * @throws InvalidArgumentException
1270
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1271
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1272
-     */
1273
-    private function _get_billing_form_for_payment_method(EE_Payment_Method $payment_method)
1274
-    {
1275
-        $billing_form = $payment_method->type_obj()->billing_form(
1276
-            $this->checkout->transaction,
1277
-            array('amount_owing' => $this->checkout->amount_owing)
1278
-        );
1279
-        if ($billing_form instanceof EE_Billing_Info_Form) {
1280
-            if (apply_filters(
1281
-                'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1282
-                false
1283
-            )
1284
-                && EE_Registry::instance()->REQ->is_set('payment_method')
1285
-            ) {
1286
-                EE_Error::add_success(
1287
-                    apply_filters(
1288
-                        'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1289
-                        sprintf(
1290
-                            esc_html__(
1291
-                                'You have selected "%s" as your method of payment. Please note the important payment information below.',
1292
-                                'event_espresso'
1293
-                            ),
1294
-                            $payment_method->name()
1295
-                        )
1296
-                    )
1297
-                );
1298
-            }
1299
-            return apply_filters(
1300
-                'FHEE__EE_SPCO_Reg_Step_Payment_Options___get_billing_form_for_payment_method__billing_form',
1301
-                $billing_form,
1302
-                $payment_method
1303
-            );
1304
-        }
1305
-        // no actual billing form, so return empty HTML form section
1306
-        return new EE_Form_Section_HTML();
1307
-    }
1308
-
1309
-
1310
-    /**
1311
-     * _get_selected_method_of_payment
1312
-     *
1313
-     * @access private
1314
-     * @param boolean $required whether to throw an error if the "selected_method_of_payment"
1315
-     *                          is not found in the incoming request
1316
-     * @param string  $request_param
1317
-     * @return NULL|string
1318
-     * @throws EE_Error
1319
-     * @throws InvalidArgumentException
1320
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1321
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1322
-     */
1323
-    private function _get_selected_method_of_payment(
1324
-        $required = false,
1325
-        $request_param = 'selected_method_of_payment'
1326
-    ) {
1327
-        // is selected_method_of_payment set in the request ?
1328
-        $selected_method_of_payment = EE_Registry::instance()->REQ->get($request_param, false);
1329
-        if ($selected_method_of_payment) {
1330
-            // sanitize it
1331
-            $selected_method_of_payment = is_array($selected_method_of_payment)
1332
-                ? array_shift($selected_method_of_payment)
1333
-                : $selected_method_of_payment;
1334
-            $selected_method_of_payment = sanitize_text_field($selected_method_of_payment);
1335
-            // store it in the session so that it's available for all subsequent requests including AJAX
1336
-            $this->_save_selected_method_of_payment($selected_method_of_payment);
1337
-        } else {
1338
-            // or is is set in the session ?
1339
-            $selected_method_of_payment = EE_Registry::instance()->SSN->get_session_data(
1340
-                'selected_method_of_payment'
1341
-            );
1342
-        }
1343
-        // do ya really really gotta have it?
1344
-        if (empty($selected_method_of_payment) && $required) {
1345
-            EE_Error::add_error(
1346
-                sprintf(
1347
-                    esc_html__(
1348
-                        'The selected method of payment could not be determined.%sPlease ensure that you have selected one before proceeding.%sIf you continue to experience difficulties, then refresh your browser and try again, or contact %s for assistance.',
1349
-                        'event_espresso'
1350
-                    ),
1351
-                    '<br/>',
1352
-                    '<br/>',
1353
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
1354
-                ),
1355
-                __FILE__,
1356
-                __FUNCTION__,
1357
-                __LINE__
1358
-            );
1359
-            return null;
1360
-        }
1361
-        return $selected_method_of_payment;
1362
-    }
1363
-
1364
-
1365
-
1366
-
1367
-
1368
-
1369
-    /********************************************************************************************************/
1370
-    /***********************************  SWITCH PAYMENT METHOD  ************************************/
1371
-    /********************************************************************************************************/
1372
-    /**
1373
-     * switch_payment_method
1374
-     *
1375
-     * @access public
1376
-     * @return string
1377
-     * @throws EE_Error
1378
-     * @throws InvalidArgumentException
1379
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1380
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1381
-     */
1382
-    public function switch_payment_method()
1383
-    {
1384
-        if (! $this->_verify_payment_method_is_set()) {
1385
-            return false;
1386
-        }
1387
-        if (apply_filters(
1388
-            'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1389
-            false
1390
-        )) {
1391
-            EE_Error::add_success(
1392
-                apply_filters(
1393
-                    'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1394
-                    sprintf(
1395
-                        esc_html__(
1396
-                            'You have selected "%s" as your method of payment. Please note the important payment information below.',
1397
-                            'event_espresso'
1398
-                        ),
1399
-                        $this->checkout->payment_method->name()
1400
-                    )
1401
-                )
1402
-            );
1403
-        }
1404
-        // generate billing form for selected method of payment if it hasn't been done already
1405
-        if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1406
-            $this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1407
-                $this->checkout->payment_method
1408
-            );
1409
-        }
1410
-        // fill form with attendee info if applicable
1411
-        if (apply_filters(
1412
-            'FHEE__populate_billing_form_fields_from_attendee',
1413
-            (
1414
-                $this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form
1415
-                && $this->checkout->transaction_has_primary_registrant()
1416
-            ),
1417
-            $this->checkout->billing_form,
1418
-            $this->checkout->transaction
1419
-        )
1420
-        ) {
1421
-            $this->checkout->billing_form->populate_from_attendee(
1422
-                $this->checkout->transaction->primary_registration()->attendee()
1423
-            );
1424
-        }
1425
-        // and debug content
1426
-        if ($this->checkout->billing_form instanceof EE_Billing_Info_Form
1427
-            && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base
1428
-        ) {
1429
-            $this->checkout->billing_form =
1430
-                $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings(
1431
-                    $this->checkout->billing_form
1432
-                );
1433
-        }
1434
-        // get html and validation rules for form
1435
-        if ($this->checkout->billing_form instanceof EE_Form_Section_Proper) {
1436
-            $this->checkout->json_response->set_return_data(
1437
-                array('payment_method_info' => $this->checkout->billing_form->get_html())
1438
-            );
1439
-            // localize validation rules for main form
1440
-            $this->checkout->billing_form->localize_validation_rules(true);
1441
-            $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
1442
-        } else {
1443
-            $this->checkout->json_response->set_return_data(array('payment_method_info' => ''));
1444
-        }
1445
-        // prevents advancement to next step
1446
-        $this->checkout->continue_reg = false;
1447
-        return true;
1448
-    }
1449
-
1450
-
1451
-    /**
1452
-     * _verify_payment_method_is_set
1453
-     *
1454
-     * @return bool
1455
-     * @throws EE_Error
1456
-     * @throws InvalidArgumentException
1457
-     * @throws ReflectionException
1458
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1459
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1460
-     */
1461
-    protected function _verify_payment_method_is_set()
1462
-    {
1463
-        // generate billing form for selected method of payment if it hasn't been done already
1464
-        if (empty($this->checkout->selected_method_of_payment)) {
1465
-            // how have they chosen to pay?
1466
-            $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
1467
-        } else {
1468
-            // choose your own adventure based on method_of_payment
1469
-            switch ($this->checkout->selected_method_of_payment) {
1470
-                case 'events_sold_out':
1471
-                    EE_Error::add_attention(
1472
-                        apply_filters(
1473
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__sold_out_events_msg',
1474
-                            esc_html__(
1475
-                                'It appears that the event you were about to make a payment for has sold out since this form first loaded. Please contact the event administrator if you believe this is an error.',
1476
-                                'event_espresso'
1477
-                            )
1478
-                        ),
1479
-                        __FILE__,
1480
-                        __FUNCTION__,
1481
-                        __LINE__
1482
-                    );
1483
-                    return false;
1484
-                    break;
1485
-                case 'payments_closed':
1486
-                    EE_Error::add_attention(
1487
-                        apply_filters(
1488
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__payments_closed_msg',
1489
-                            esc_html__(
1490
-                                'It appears that the event you were about to make a payment for is not accepting payments at this time. Please contact the event administrator if you believe this is an error.',
1491
-                                'event_espresso'
1492
-                            )
1493
-                        ),
1494
-                        __FILE__,
1495
-                        __FUNCTION__,
1496
-                        __LINE__
1497
-                    );
1498
-                    return false;
1499
-                    break;
1500
-                case 'no_payment_required':
1501
-                    EE_Error::add_attention(
1502
-                        apply_filters(
1503
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__no_payment_required_msg',
1504
-                            esc_html__(
1505
-                                'It appears that the event you were about to make a payment for does not require payment. Please contact the event administrator if you believe this is an error.',
1506
-                                'event_espresso'
1507
-                            )
1508
-                        ),
1509
-                        __FILE__,
1510
-                        __FUNCTION__,
1511
-                        __LINE__
1512
-                    );
1513
-                    return false;
1514
-                    break;
1515
-                default:
1516
-            }
1517
-        }
1518
-        // verify payment method
1519
-        if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1520
-            // get payment method for selected method of payment
1521
-            $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1522
-        }
1523
-        return $this->checkout->payment_method instanceof EE_Payment_Method ? true : false;
1524
-    }
1525
-
1526
-
1527
-
1528
-    /********************************************************************************************************/
1529
-    /***************************************  SAVE PAYER DETAILS  ****************************************/
1530
-    /********************************************************************************************************/
1531
-    /**
1532
-     * save_payer_details_via_ajax
1533
-     *
1534
-     * @return void
1535
-     * @throws EE_Error
1536
-     * @throws InvalidArgumentException
1537
-     * @throws ReflectionException
1538
-     * @throws RuntimeException
1539
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1540
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1541
-     */
1542
-    public function save_payer_details_via_ajax()
1543
-    {
1544
-        if (! $this->_verify_payment_method_is_set()) {
1545
-            return;
1546
-        }
1547
-        // generate billing form for selected method of payment if it hasn't been done already
1548
-        if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1549
-            $this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1550
-                $this->checkout->payment_method
1551
-            );
1552
-        }
1553
-        // generate primary attendee from payer info if applicable
1554
-        if (! $this->checkout->transaction_has_primary_registrant()) {
1555
-            $attendee = $this->_create_attendee_from_request_data();
1556
-            if ($attendee instanceof EE_Attendee) {
1557
-                foreach ($this->checkout->transaction->registrations() as $registration) {
1558
-                    if ($registration->is_primary_registrant()) {
1559
-                        $this->checkout->primary_attendee_obj = $attendee;
1560
-                        $registration->_add_relation_to($attendee, 'Attendee');
1561
-                        $registration->set_attendee_id($attendee->ID());
1562
-                        $registration->update_cache_after_object_save('Attendee', $attendee);
1563
-                    }
1564
-                }
1565
-            }
1566
-        }
1567
-    }
1568
-
1569
-
1570
-    /**
1571
-     * create_attendee_from_request_data
1572
-     * uses info from alternate GET or POST data (such as AJAX) to create a new attendee
1573
-     *
1574
-     * @return EE_Attendee
1575
-     * @throws EE_Error
1576
-     * @throws InvalidArgumentException
1577
-     * @throws ReflectionException
1578
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1579
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1580
-     */
1581
-    protected function _create_attendee_from_request_data()
1582
-    {
1583
-        // get State ID
1584
-        $STA_ID = ! empty($_REQUEST['state']) ? sanitize_text_field($_REQUEST['state']) : '';
1585
-        if (! empty($STA_ID)) {
1586
-            // can we get state object from name ?
1587
-            EE_Registry::instance()->load_model('State');
1588
-            $state = EEM_State::instance()->get_col(array(array('STA_name' => $STA_ID), 'limit' => 1), 'STA_ID');
1589
-            $STA_ID = is_array($state) && ! empty($state) ? reset($state) : $STA_ID;
1590
-        }
1591
-        // get Country ISO
1592
-        $CNT_ISO = ! empty($_REQUEST['country']) ? sanitize_text_field($_REQUEST['country']) : '';
1593
-        if (! empty($CNT_ISO)) {
1594
-            // can we get country object from name ?
1595
-            EE_Registry::instance()->load_model('Country');
1596
-            $country = EEM_Country::instance()->get_col(
1597
-                array(array('CNT_name' => $CNT_ISO), 'limit' => 1),
1598
-                'CNT_ISO'
1599
-            );
1600
-            $CNT_ISO = is_array($country) && ! empty($country) ? reset($country) : $CNT_ISO;
1601
-        }
1602
-        // grab attendee data
1603
-        $attendee_data = array(
1604
-            'ATT_fname'    => ! empty($_REQUEST['first_name']) ? sanitize_text_field($_REQUEST['first_name']) : '',
1605
-            'ATT_lname'    => ! empty($_REQUEST['last_name']) ? sanitize_text_field($_REQUEST['last_name']) : '',
1606
-            'ATT_email'    => ! empty($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '',
1607
-            'ATT_address'  => ! empty($_REQUEST['address']) ? sanitize_text_field($_REQUEST['address']) : '',
1608
-            'ATT_address2' => ! empty($_REQUEST['address2']) ? sanitize_text_field($_REQUEST['address2']) : '',
1609
-            'ATT_city'     => ! empty($_REQUEST['city']) ? sanitize_text_field($_REQUEST['city']) : '',
1610
-            'STA_ID'       => $STA_ID,
1611
-            'CNT_ISO'      => $CNT_ISO,
1612
-            'ATT_zip'      => ! empty($_REQUEST['zip']) ? sanitize_text_field($_REQUEST['zip']) : '',
1613
-            'ATT_phone'    => ! empty($_REQUEST['phone']) ? sanitize_text_field($_REQUEST['phone']) : '',
1614
-        );
1615
-        // validate the email address since it is the most important piece of info
1616
-        if (empty($attendee_data['ATT_email']) || $attendee_data['ATT_email'] !== $_REQUEST['email']) {
1617
-            EE_Error::add_error(
1618
-                esc_html__('An invalid email address was submitted.', 'event_espresso'),
1619
-                __FILE__,
1620
-                __FUNCTION__,
1621
-                __LINE__
1622
-            );
1623
-        }
1624
-        // does this attendee already exist in the db ? we're searching using a combination of first name, last name,
1625
-        // AND email address
1626
-        if (! empty($attendee_data['ATT_fname'])
1627
-            && ! empty($attendee_data['ATT_lname'])
1628
-            && ! empty($attendee_data['ATT_email'])
1629
-        ) {
1630
-            $existing_attendee = EE_Registry::instance()->LIB->EEM_Attendee->find_existing_attendee(
1631
-                array(
1632
-                    'ATT_fname' => $attendee_data['ATT_fname'],
1633
-                    'ATT_lname' => $attendee_data['ATT_lname'],
1634
-                    'ATT_email' => $attendee_data['ATT_email'],
1635
-                )
1636
-            );
1637
-            if ($existing_attendee instanceof EE_Attendee) {
1638
-                return $existing_attendee;
1639
-            }
1640
-        }
1641
-        // no existing attendee? kk let's create a new one
1642
-        // kinda lame, but we need a first and last name to create an attendee, so use the email address if those
1643
-        // don't exist
1644
-        $attendee_data['ATT_fname'] = ! empty($attendee_data['ATT_fname'])
1645
-            ? $attendee_data['ATT_fname']
1646
-            : $attendee_data['ATT_email'];
1647
-        $attendee_data['ATT_lname'] = ! empty($attendee_data['ATT_lname'])
1648
-            ? $attendee_data['ATT_lname']
1649
-            : $attendee_data['ATT_email'];
1650
-        return EE_Attendee::new_instance($attendee_data);
1651
-    }
1652
-
1653
-
1654
-
1655
-    /********************************************************************************************************/
1656
-    /****************************************  PROCESS REG STEP  *****************************************/
1657
-    /********************************************************************************************************/
1658
-    /**
1659
-     * process_reg_step
1660
-     *
1661
-     * @return bool
1662
-     * @throws EE_Error
1663
-     * @throws InvalidArgumentException
1664
-     * @throws ReflectionException
1665
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
1666
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1667
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1668
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
1669
-     */
1670
-    public function process_reg_step()
1671
-    {
1672
-        // how have they chosen to pay?
1673
-        $this->checkout->selected_method_of_payment = $this->checkout->transaction->is_free()
1674
-            ? 'no_payment_required'
1675
-            : $this->_get_selected_method_of_payment(true);
1676
-        // choose your own adventure based on method_of_payment
1677
-        switch ($this->checkout->selected_method_of_payment) {
1678
-            case 'events_sold_out':
1679
-                $this->checkout->redirect = true;
1680
-                $this->checkout->redirect_url = $this->checkout->cancel_page_url;
1681
-                $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url);
1682
-                // mark this reg step as completed
1683
-                $this->set_completed();
1684
-                return false;
1685
-                break;
1686
-
1687
-            case 'payments_closed':
1688
-                if (apply_filters(
1689
-                    'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__payments_closed__display_success',
1690
-                    false
1691
-                )) {
1692
-                    EE_Error::add_success(
1693
-                        esc_html__('no payment required at this time.', 'event_espresso'),
1694
-                        __FILE__,
1695
-                        __FUNCTION__,
1696
-                        __LINE__
1697
-                    );
1698
-                }
1699
-                // mark this reg step as completed
1700
-                $this->set_completed();
1701
-                return true;
1702
-                break;
1703
-
1704
-            case 'no_payment_required':
1705
-                if (apply_filters(
1706
-                    'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__no_payment_required__display_success',
1707
-                    false
1708
-                )) {
1709
-                    EE_Error::add_success(
1710
-                        esc_html__('no payment required.', 'event_espresso'),
1711
-                        __FILE__,
1712
-                        __FUNCTION__,
1713
-                        __LINE__
1714
-                    );
1715
-                }
1716
-                // mark this reg step as completed
1717
-                $this->set_completed();
1718
-                return true;
1719
-                break;
1720
-
1721
-            default:
1722
-                $registrations = EE_Registry::instance()->SSN->checkout()->transaction->registrations(
1723
-                    EE_Registry::instance()->SSN->checkout()->reg_cache_where_params
1724
-                );
1725
-                $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
1726
-                    $registrations,
1727
-                    EE_Registry::instance()->SSN->checkout()->revisit
1728
-                );
1729
-                // calculate difference between the two arrays
1730
-                $registrations = array_diff($registrations, $ejected_registrations);
1731
-                if (empty($registrations)) {
1732
-                    $this->_redirect_because_event_sold_out();
1733
-                    return false;
1734
-                }
1735
-                $payment = $this->_process_payment();
1736
-                if ($payment instanceof EE_Payment) {
1737
-                    $this->checkout->continue_reg = true;
1738
-                    $this->_maybe_set_completed($payment);
1739
-                } else {
1740
-                    $this->checkout->continue_reg = false;
1741
-                }
1742
-                return $payment instanceof EE_Payment;
1743
-        }
1744
-    }
1745
-
1746
-
1747
-    /**
1748
-     * _redirect_because_event_sold_out
1749
-     *
1750
-     * @access protected
1751
-     * @return void
1752
-     */
1753
-    protected function _redirect_because_event_sold_out()
1754
-    {
1755
-        $this->checkout->continue_reg = false;
1756
-        // set redirect URL
1757
-        $this->checkout->redirect_url = add_query_arg(
1758
-            array('e_reg_url_link' => $this->checkout->reg_url_link),
1759
-            $this->checkout->current_step->reg_step_url()
1760
-        );
1761
-        $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url);
1762
-    }
1763
-
1764
-
1765
-    /**
1766
-     * _maybe_set_completed
1767
-     *
1768
-     * @access protected
1769
-     * @param \EE_Payment_Method $payment_method
1770
-     * @return void
1771
-     * @throws \EE_Error
1772
-     */
1773
-    protected function _maybe_set_completed(EE_Payment $payment)
1774
-    {
1775
-        // Do we need to redirect them? If so, there's more work to be done.
1776
-        if (! $payment->redirect_url()){
1777
-            $this->set_completed();
1778
-        }
1779
-    }
1780
-
1781
-
1782
-    /**
1783
-     *    update_reg_step
1784
-     *    this is the final step after a user  revisits the site to retry a payment
1785
-     *
1786
-     * @return bool
1787
-     * @throws EE_Error
1788
-     * @throws InvalidArgumentException
1789
-     * @throws ReflectionException
1790
-     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
1791
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1792
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1793
-     * @throws \EventEspresso\core\exceptions\InvalidStatusException
1794
-     */
1795
-    public function update_reg_step()
1796
-    {
1797
-        $success = true;
1798
-        // if payment required
1799
-        if ($this->checkout->transaction->total() > 0) {
1800
-            do_action(
1801
-                'AHEE__EE_Single_Page_Checkout__process_finalize_registration__before_gateway',
1802
-                $this->checkout->transaction
1803
-            );
1804
-            // attempt payment via payment method
1805
-            $success = $this->process_reg_step();
1806
-        }
1807
-        if ($success && ! $this->checkout->redirect) {
1808
-            $this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn(
1809
-                $this->checkout->transaction->ID()
1810
-            );
1811
-            // set return URL
1812
-            $this->checkout->redirect_url = add_query_arg(
1813
-                array('e_reg_url_link' => $this->checkout->reg_url_link),
1814
-                $this->checkout->thank_you_page_url
1815
-            );
1816
-        }
1817
-        return $success;
1818
-    }
1819
-
1820
-
1821
-    /**
1822
-     *    _process_payment
1823
-     *
1824
-     * @access private
1825
-     * @return EE_Payment|null
1826
-     * @throws EE_Error
1827
-     * @throws InvalidArgumentException
1828
-     * @throws ReflectionException
1829
-     * @throws RuntimeException
1830
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1831
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1832
-     */
1833
-    private function _process_payment()
1834
-    {
1835
-        // basically confirm that the event hasn't sold out since they hit the page
1836
-        if (! $this->_last_second_ticket_verifications()) {
1837
-            return null;
1838
-        }
1839
-        // ya gotta make a choice man
1840
-        if (empty($this->checkout->selected_method_of_payment)) {
1841
-            $this->checkout->json_response->set_plz_select_method_of_payment(
1842
-                esc_html__('Please select a method of payment before proceeding.', 'event_espresso')
1843
-            );
1844
-            return null;
1845
-        }
1846
-        // get EE_Payment_Method object
1847
-        if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
1848
-            return null;
1849
-        }
1850
-        // setup billing form
1851
-        if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1852
-            $this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1853
-                $this->checkout->payment_method
1854
-            );
1855
-            // bad billing form ?
1856
-            if (! $this->_billing_form_is_valid()) {
1857
-                return null;
1858
-            }
1859
-        }
1860
-        // ensure primary registrant has been fully processed
1861
-        if (! $this->_setup_primary_registrant_prior_to_payment()) {
1862
-            return null;
1863
-        }
1864
-        // if session is close to expiring (under 10 minutes by default)
1865
-        if ((time() - EE_Registry::instance()->SSN->expiration()) < EE_Registry::instance()->SSN->extension()) {
1866
-            // add some time to session expiration so that payment can be completed
1867
-            EE_Registry::instance()->SSN->extend_expiration();
1868
-        }
1869
-        /** @type EE_Transaction_Processor $transaction_processor */
1870
-        // $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' );
1871
-        // in case a registrant leaves to an Off-Site Gateway and never returns, we want to approve any registrations
1872
-        // for events with a default reg status of Approved
1873
-        // $transaction_processor->toggle_registration_statuses_for_default_approved_events(
1874
-        //      $this->checkout->transaction, $this->checkout->reg_cache_where_params
1875
-        // );
1876
-        // attempt payment
1877
-        $payment = $this->_attempt_payment($this->checkout->payment_method);
1878
-        // process results
1879
-        $payment = $this->_validate_payment($payment);
1880
-        $payment = $this->_post_payment_processing($payment);
1881
-        // verify payment
1882
-        if ($payment instanceof EE_Payment) {
1883
-            // store that for later
1884
-            $this->checkout->payment = $payment;
1885
-            // we can also consider the TXN to not have been failed, so temporarily upgrade it's status to abandoned
1886
-            $this->checkout->transaction->toggle_failed_transaction_status();
1887
-            $payment_status = $payment->status();
1888
-            if ($payment_status === EEM_Payment::status_id_approved
1889
-                || $payment_status === EEM_Payment::status_id_pending
1890
-            ) {
1891
-                return $payment;
1892
-            } else {
1893
-                return null;
1894
-            }
1895
-        } elseif ($payment === true) {
1896
-            // please note that offline payment methods will NOT make a payment,
1897
-            // but instead just mark themselves as the PMD_ID on the transaction, and return true
1898
-            $this->checkout->payment = $payment;
1899
-            return $payment;
1900
-        }
1901
-        // where's my money?
1902
-        return null;
1903
-    }
1904
-
1905
-
1906
-    /**
1907
-     * _last_second_ticket_verifications
1908
-     *
1909
-     * @access public
1910
-     * @return bool
1911
-     * @throws EE_Error
1912
-     */
1913
-    protected function _last_second_ticket_verifications()
1914
-    {
1915
-        // don't bother re-validating if not a return visit
1916
-        if (! $this->checkout->revisit) {
1917
-            return true;
1918
-        }
1919
-        $registrations = $this->checkout->transaction->registrations();
1920
-        if (empty($registrations)) {
1921
-            return false;
1922
-        }
1923
-        foreach ($registrations as $registration) {
1924
-            if ($registration instanceof EE_Registration && ! $registration->is_approved()) {
1925
-                $event = $registration->event_obj();
1926
-                if ($event instanceof EE_Event && $event->is_sold_out(true)) {
1927
-                    EE_Error::add_error(
1928
-                        apply_filters(
1929
-                            'FHEE__EE_SPCO_Reg_Step_Payment_Options___last_second_ticket_verifications__sold_out_events_msg',
1930
-                            sprintf(
1931
-                                esc_html__(
1932
-                                    'It appears that the %1$s event that you were about to make a payment for has sold out since you first registered and/or arrived at this page. Please refresh the page and try again. If you have already made a partial payment towards this event, please contact the event administrator for a refund.',
1933
-                                    'event_espresso'
1934
-                                ),
1935
-                                $event->name()
1936
-                            )
1937
-                        ),
1938
-                        __FILE__,
1939
-                        __FUNCTION__,
1940
-                        __LINE__
1941
-                    );
1942
-                    return false;
1943
-                }
1944
-            }
1945
-        }
1946
-        return true;
1947
-    }
1948
-
1949
-
1950
-    /**
1951
-     * redirect_form
1952
-     *
1953
-     * @access public
1954
-     * @return bool
1955
-     * @throws EE_Error
1956
-     * @throws InvalidArgumentException
1957
-     * @throws ReflectionException
1958
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1959
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1960
-     */
1961
-    public function redirect_form()
1962
-    {
1963
-        $payment_method_billing_info = $this->_payment_method_billing_info(
1964
-            $this->_get_payment_method_for_selected_method_of_payment()
1965
-        );
1966
-        $html = $payment_method_billing_info->get_html();
1967
-        $html .= $this->checkout->redirect_form;
1968
-        EE_Registry::instance()->REQ->add_output($html);
1969
-        return true;
1970
-    }
1971
-
1972
-
1973
-    /**
1974
-     * _billing_form_is_valid
1975
-     *
1976
-     * @access private
1977
-     * @return bool
1978
-     * @throws \EE_Error
1979
-     */
1980
-    private function _billing_form_is_valid()
1981
-    {
1982
-        if (! $this->checkout->payment_method->type_obj()->has_billing_form()) {
1983
-            return true;
1984
-        }
1985
-        if ($this->checkout->billing_form instanceof EE_Billing_Info_Form) {
1986
-            if ($this->checkout->billing_form->was_submitted()) {
1987
-                $this->checkout->billing_form->receive_form_submission();
1988
-                if ($this->checkout->billing_form->is_valid()) {
1989
-                    return true;
1990
-                }
1991
-                $validation_errors = $this->checkout->billing_form->get_validation_errors_accumulated();
1992
-                $error_strings = array();
1993
-                foreach ($validation_errors as $validation_error) {
1994
-                    if ($validation_error instanceof EE_Validation_Error) {
1995
-                        $form_section = $validation_error->get_form_section();
1996
-                        if ($form_section instanceof EE_Form_Input_Base) {
1997
-                            $label = $form_section->html_label_text();
1998
-                        } elseif ($form_section instanceof EE_Form_Section_Base) {
1999
-                            $label = $form_section->name();
2000
-                        } else {
2001
-                            $label = esc_html__('Validation Error', 'event_espresso');
2002
-                        }
2003
-                        $error_strings[] = sprintf('%1$s: %2$s', $label, $validation_error->getMessage());
2004
-                    }
2005
-                }
2006
-                EE_Error::add_error(
2007
-                    sprintf(
2008
-                        esc_html__(
2009
-                            'One or more billing form inputs are invalid and require correction before proceeding. %1$s %2$s',
2010
-                            'event_espresso'
2011
-                        ),
2012
-                        '<br/>',
2013
-                        implode('<br/>', $error_strings)
2014
-                    ),
2015
-                    __FILE__,
2016
-                    __FUNCTION__,
2017
-                    __LINE__
2018
-                );
2019
-            } else {
2020
-                EE_Error::add_error(
2021
-                    esc_html__(
2022
-                        'The billing form was not submitted or something prevented it\'s submission.',
2023
-                        'event_espresso'
2024
-                    ),
2025
-                    __FILE__,
2026
-                    __FUNCTION__,
2027
-                    __LINE__
2028
-                );
2029
-            }
2030
-        } else {
2031
-            EE_Error::add_error(
2032
-                esc_html__(
2033
-                    'The submitted billing form is invalid possibly due to a technical reason.',
2034
-                    'event_espresso'
2035
-                ),
2036
-                __FILE__,
2037
-                __FUNCTION__,
2038
-                __LINE__
2039
-            );
2040
-        }
2041
-        return false;
2042
-    }
2043
-
2044
-
2045
-    /**
2046
-     * _setup_primary_registrant_prior_to_payment
2047
-     * ensures that the primary registrant has a valid attendee object created with the critical details populated
2048
-     * (first & last name & email) and that both the transaction object and primary registration object have been saved
2049
-     * plz note that any other registrations will NOT be saved at this point (because they may not have any details
2050
-     * yet)
2051
-     *
2052
-     * @access private
2053
-     * @return bool
2054
-     * @throws EE_Error
2055
-     * @throws InvalidArgumentException
2056
-     * @throws ReflectionException
2057
-     * @throws RuntimeException
2058
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2059
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2060
-     */
2061
-    private function _setup_primary_registrant_prior_to_payment()
2062
-    {
2063
-        // check if transaction has a primary registrant and that it has a related Attendee object
2064
-        // if not, then we need to at least gather some primary registrant data before attempting payment
2065
-        if ($this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form
2066
-            && ! $this->checkout->transaction_has_primary_registrant()
2067
-            && ! $this->_capture_primary_registration_data_from_billing_form()
2068
-        ) {
2069
-            return false;
2070
-        }
2071
-        // because saving an object clears it's cache, we need to do the chevy shuffle
2072
-        // grab the primary_registration object
2073
-        $primary_registration = $this->checkout->transaction->primary_registration();
2074
-        // at this point we'll consider a TXN to not have been failed
2075
-        $this->checkout->transaction->toggle_failed_transaction_status();
2076
-        // save the TXN ( which clears cached copy of primary_registration)
2077
-        $this->checkout->transaction->save();
2078
-        // grab TXN ID and save it to the primary_registration
2079
-        $primary_registration->set_transaction_id($this->checkout->transaction->ID());
2080
-        // save what we have so far
2081
-        $primary_registration->save();
2082
-        return true;
2083
-    }
2084
-
2085
-
2086
-    /**
2087
-     * _capture_primary_registration_data_from_billing_form
2088
-     *
2089
-     * @access private
2090
-     * @return bool
2091
-     * @throws EE_Error
2092
-     * @throws InvalidArgumentException
2093
-     * @throws ReflectionException
2094
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2095
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2096
-     */
2097
-    private function _capture_primary_registration_data_from_billing_form()
2098
-    {
2099
-        // convert billing form data into an attendee
2100
-        $this->checkout->primary_attendee_obj = $this->checkout->billing_form->create_attendee_from_billing_form_data();
2101
-        if (! $this->checkout->primary_attendee_obj instanceof EE_Attendee) {
2102
-            EE_Error::add_error(
2103
-                sprintf(
2104
-                    esc_html__(
2105
-                        'The billing form details could not be used for attendee details due to a technical issue.%sPlease try again or contact %s for assistance.',
2106
-                        'event_espresso'
2107
-                    ),
2108
-                    '<br/>',
2109
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2110
-                ),
2111
-                __FILE__,
2112
-                __FUNCTION__,
2113
-                __LINE__
2114
-            );
2115
-            return false;
2116
-        }
2117
-        $primary_registration = $this->checkout->transaction->primary_registration();
2118
-        if (! $primary_registration instanceof EE_Registration) {
2119
-            EE_Error::add_error(
2120
-                sprintf(
2121
-                    esc_html__(
2122
-                        'The primary registrant for this transaction could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2123
-                        'event_espresso'
2124
-                    ),
2125
-                    '<br/>',
2126
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2127
-                ),
2128
-                __FILE__,
2129
-                __FUNCTION__,
2130
-                __LINE__
2131
-            );
2132
-            return false;
2133
-        }
2134
-        if (! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee')
2135
-              instanceof
2136
-              EE_Attendee
2137
-        ) {
2138
-            EE_Error::add_error(
2139
-                sprintf(
2140
-                    esc_html__(
2141
-                        'The primary registrant could not be associated with this transaction due to a technical issue.%sPlease try again or contact %s for assistance.',
2142
-                        'event_espresso'
2143
-                    ),
2144
-                    '<br/>',
2145
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2146
-                ),
2147
-                __FILE__,
2148
-                __FUNCTION__,
2149
-                __LINE__
2150
-            );
2151
-            return false;
2152
-        }
2153
-        /** @type EE_Registration_Processor $registration_processor */
2154
-        $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
2155
-        // at this point, we should have enough details about the registrant to consider the registration NOT incomplete
2156
-        $registration_processor->toggle_incomplete_registration_status_to_default($primary_registration);
2157
-        return true;
2158
-    }
2159
-
2160
-
2161
-    /**
2162
-     * _get_payment_method_for_selected_method_of_payment
2163
-     * retrieves a valid payment method
2164
-     *
2165
-     * @access public
2166
-     * @return EE_Payment_Method
2167
-     * @throws EE_Error
2168
-     * @throws InvalidArgumentException
2169
-     * @throws ReflectionException
2170
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2171
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2172
-     */
2173
-    private function _get_payment_method_for_selected_method_of_payment()
2174
-    {
2175
-        if ($this->checkout->selected_method_of_payment === 'events_sold_out') {
2176
-            $this->_redirect_because_event_sold_out();
2177
-            return null;
2178
-        }
2179
-        // get EE_Payment_Method object
2180
-        if (isset($this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ])) {
2181
-            $payment_method = $this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ];
2182
-        } else {
2183
-            // load EEM_Payment_Method
2184
-            EE_Registry::instance()->load_model('Payment_Method');
2185
-            /** @type EEM_Payment_Method $EEM_Payment_Method */
2186
-            $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method;
2187
-            $payment_method = $EEM_Payment_Method->get_one_by_slug($this->checkout->selected_method_of_payment);
2188
-        }
2189
-        // verify $payment_method
2190
-        if (! $payment_method instanceof EE_Payment_Method) {
2191
-            // not a payment
2192
-            EE_Error::add_error(
2193
-                sprintf(
2194
-                    esc_html__(
2195
-                        'The selected method of payment could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2196
-                        'event_espresso'
2197
-                    ),
2198
-                    '<br/>',
2199
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2200
-                ),
2201
-                __FILE__,
2202
-                __FUNCTION__,
2203
-                __LINE__
2204
-            );
2205
-            return null;
2206
-        }
2207
-        // and verify it has a valid Payment_Method Type object
2208
-        if (! $payment_method->type_obj() instanceof EE_PMT_Base) {
2209
-            // not a payment
2210
-            EE_Error::add_error(
2211
-                sprintf(
2212
-                    esc_html__(
2213
-                        'A valid payment method could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2214
-                        'event_espresso'
2215
-                    ),
2216
-                    '<br/>',
2217
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2218
-                ),
2219
-                __FILE__,
2220
-                __FUNCTION__,
2221
-                __LINE__
2222
-            );
2223
-            return null;
2224
-        }
2225
-        return $payment_method;
2226
-    }
2227
-
2228
-
2229
-    /**
2230
-     *    _attempt_payment
2231
-     *
2232
-     * @access    private
2233
-     * @type    EE_Payment_Method $payment_method
2234
-     * @return mixed EE_Payment | boolean
2235
-     * @throws EE_Error
2236
-     * @throws InvalidArgumentException
2237
-     * @throws ReflectionException
2238
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2239
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2240
-     */
2241
-    private function _attempt_payment(EE_Payment_Method $payment_method)
2242
-    {
2243
-        $payment = null;
2244
-        $this->checkout->transaction->save();
2245
-        $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2246
-        if (! $payment_processor instanceof EE_Payment_Processor) {
2247
-            return false;
2248
-        }
2249
-        try {
2250
-            $payment_processor->set_revisit($this->checkout->revisit);
2251
-            // generate payment object
2252
-            $payment = $payment_processor->process_payment(
2253
-                $payment_method,
2254
-                $this->checkout->transaction,
2255
-                $this->checkout->amount_owing,
2256
-                $this->checkout->billing_form,
2257
-                $this->_get_return_url($payment_method),
2258
-                'CART',
2259
-                $this->checkout->admin_request,
2260
-                true,
2261
-                $this->reg_step_url()
2262
-            );
2263
-        } catch (Exception $e) {
2264
-            $this->_handle_payment_processor_exception($e);
2265
-        }
2266
-        return $payment;
2267
-    }
2268
-
2269
-
2270
-    /**
2271
-     * _handle_payment_processor_exception
2272
-     *
2273
-     * @access protected
2274
-     * @param \Exception $e
2275
-     * @return void
2276
-     * @throws EE_Error
2277
-     * @throws InvalidArgumentException
2278
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2279
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2280
-     */
2281
-    protected function _handle_payment_processor_exception(Exception $e)
2282
-    {
2283
-        EE_Error::add_error(
2284
-            sprintf(
2285
-                esc_html__(
2286
-                    'The payment could not br processed due to a technical issue.%1$sPlease try again or contact %2$s for assistance.||The following Exception was thrown in %4$s on line %5$s:%1$s%3$s',
2287
-                    'event_espresso'
2288
-                ),
2289
-                '<br/>',
2290
-                EE_Registry::instance()->CFG->organization->get_pretty('email'),
2291
-                $e->getMessage(),
2292
-                $e->getFile(),
2293
-                $e->getLine()
2294
-            ),
2295
-            __FILE__,
2296
-            __FUNCTION__,
2297
-            __LINE__
2298
-        );
2299
-    }
2300
-
2301
-
2302
-    /**
2303
-     * _get_return_url
2304
-     *
2305
-     * @access protected
2306
-     * @param \EE_Payment_Method $payment_method
2307
-     * @return string
2308
-     * @throws \EE_Error
2309
-     */
2310
-    protected function _get_return_url(EE_Payment_Method $payment_method)
2311
-    {
2312
-        $return_url = '';
2313
-        switch ($payment_method->type_obj()->payment_occurs()) {
2314
-            case EE_PMT_Base::offsite:
2315
-                $return_url = add_query_arg(
2316
-                    array(
2317
-                        'action'                     => 'process_gateway_response',
2318
-                        'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2319
-                        'spco_txn'                   => $this->checkout->transaction->ID(),
2320
-                    ),
2321
-                    $this->reg_step_url()
2322
-                );
2323
-                break;
2324
-            case EE_PMT_Base::onsite:
2325
-            case EE_PMT_Base::offline:
2326
-                $return_url = $this->checkout->next_step->reg_step_url();
2327
-                break;
2328
-        }
2329
-        return $return_url;
2330
-    }
2331
-
2332
-
2333
-    /**
2334
-     * _validate_payment
2335
-     *
2336
-     * @access private
2337
-     * @param EE_Payment $payment
2338
-     * @return EE_Payment|FALSE
2339
-     * @throws EE_Error
2340
-     * @throws InvalidArgumentException
2341
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2342
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2343
-     */
2344
-    private function _validate_payment($payment = null)
2345
-    {
2346
-        if ($this->checkout->payment_method->is_off_line()) {
2347
-            return true;
2348
-        }
2349
-        // verify payment object
2350
-        if (! $payment instanceof EE_Payment) {
2351
-            // not a payment
2352
-            EE_Error::add_error(
2353
-                sprintf(
2354
-                    esc_html__(
2355
-                        'A valid payment was not generated due to a technical issue.%1$sPlease try again or contact %2$s for assistance.',
2356
-                        'event_espresso'
2357
-                    ),
2358
-                    '<br/>',
2359
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2360
-                ),
2361
-                __FILE__,
2362
-                __FUNCTION__,
2363
-                __LINE__
2364
-            );
2365
-            return false;
2366
-        }
2367
-        return $payment;
2368
-    }
2369
-
2370
-
2371
-    /**
2372
-     * _post_payment_processing
2373
-     *
2374
-     * @access private
2375
-     * @param EE_Payment|bool $payment
2376
-     * @return bool
2377
-     * @throws EE_Error
2378
-     * @throws InvalidArgumentException
2379
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2380
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2381
-     */
2382
-    private function _post_payment_processing($payment = null)
2383
-    {
2384
-        // Off-Line payment?
2385
-        if ($payment === true) {
2386
-            return true;
2387
-        } elseif ($payment instanceof EE_Payment){
2388
-            // Should the user be redirected?
2389
-            if($payment->redirect_url()) {
2390
-                do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->redirect_url(), '$payment->redirect_url()');
2391
-                $this->checkout->redirect = true;
2392
-                $this->checkout->redirect_form = $payment->redirect_form();
2393
-                $this->checkout->redirect_url = $this->reg_step_url('redirect_form');
2394
-                // set JSON response
2395
-                $this->checkout->json_response->set_redirect_form($this->checkout->redirect_form);
2396
-                // and lastly, let's bump the payment status to pending
2397
-                $payment->set_status(EEM_Payment::status_id_pending);
2398
-                $payment->save();
2399
-            } else
2400
-                // User shouldn't be redirected. So let's process it here.
2401
-                if (! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) {
2402
-                // $this->_setup_redirect_for_next_step();
2403
-                $this->checkout->continue_reg = false;
2404
-            }
2405
-        } else {
2406
-            // ummm ya... not Off-Line, not On-Site, not off-Site ????
2407
-            $this->checkout->continue_reg = false;
2408
-            return false;
2409
-        }
2410
-        return $payment;
2411
-    }
2412
-
2413
-
2414
-    /**
2415
-     *    _process_payment_status
2416
-     *
2417
-     * @access private
2418
-     * @type    EE_Payment $payment
2419
-     * @param string       $payment_occurs
2420
-     * @return bool
2421
-     * @throws EE_Error
2422
-     * @throws InvalidArgumentException
2423
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2424
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2425
-     */
2426
-    private function _process_payment_status($payment, $payment_occurs = EE_PMT_Base::offline)
2427
-    {
2428
-        // off-line payment? carry on
2429
-        if ($payment_occurs === EE_PMT_Base::offline) {
2430
-            return true;
2431
-        }
2432
-        // verify payment validity
2433
-        if ($payment instanceof EE_Payment) {
2434
-            do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->status(), '$payment->status()');
2435
-            $msg = $payment->gateway_response();
2436
-            // check results
2437
-            switch ($payment->status()) {
2438
-                // good payment
2439
-                case EEM_Payment::status_id_approved:
2440
-                    EE_Error::add_success(
2441
-                        esc_html__('Your payment was processed successfully.', 'event_espresso'),
2442
-                        __FILE__,
2443
-                        __FUNCTION__,
2444
-                        __LINE__
2445
-                    );
2446
-                    return true;
2447
-                    break;
2448
-                // slow payment
2449
-                case EEM_Payment::status_id_pending:
2450
-                    if (empty($msg)) {
2451
-                        $msg = esc_html__(
2452
-                            'Your payment appears to have been processed successfully, but the Instant Payment Notification has not yet been received. It should arrive shortly.',
2453
-                            'event_espresso'
2454
-                        );
2455
-                    }
2456
-                    EE_Error::add_success($msg, __FILE__, __FUNCTION__, __LINE__);
2457
-                    return true;
2458
-                    break;
2459
-                // don't wanna payment
2460
-                case EEM_Payment::status_id_cancelled:
2461
-                    if (empty($msg)) {
2462
-                        $msg = _n(
2463
-                            'Payment cancelled. Please try again.',
2464
-                            'Payment cancelled. Please try again or select another method of payment.',
2465
-                            count($this->checkout->available_payment_methods),
2466
-                            'event_espresso'
2467
-                        );
2468
-                    }
2469
-                    EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
2470
-                    return false;
2471
-                    break;
2472
-                // not enough payment
2473
-                case EEM_Payment::status_id_declined:
2474
-                    if (empty($msg)) {
2475
-                        $msg = _n(
2476
-                            'We\'re sorry but your payment was declined. Please try again.',
2477
-                            'We\'re sorry but your payment was declined. Please try again or select another method of payment.',
2478
-                            count($this->checkout->available_payment_methods),
2479
-                            'event_espresso'
2480
-                        );
2481
-                    }
2482
-                    EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
2483
-                    return false;
2484
-                    break;
2485
-                // bad payment
2486
-                case EEM_Payment::status_id_failed:
2487
-                    if (! empty($msg)) {
2488
-                        EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2489
-                        return false;
2490
-                    }
2491
-                    // default to error below
2492
-                    break;
2493
-            }
2494
-        }
2495
-        // off-site payment gateway responses are too unreliable, so let's just assume that
2496
-        // the payment processing is just running slower than the registrant's request
2497
-        if ($payment_occurs === EE_PMT_Base::offsite) {
2498
-            return true;
2499
-        }
2500
-        EE_Error::add_error(
2501
-            sprintf(
2502
-                esc_html__(
2503
-                    'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.',
2504
-                    'event_espresso'
2505
-                ),
2506
-                '<br/>',
2507
-                EE_Registry::instance()->CFG->organization->get_pretty('email')
2508
-            ),
2509
-            __FILE__,
2510
-            __FUNCTION__,
2511
-            __LINE__
2512
-        );
2513
-        return false;
2514
-    }
2515
-
2516
-
2517
-
2518
-
2519
-
2520
-
2521
-    /********************************************************************************************************/
2522
-    /**********************************  PROCESS GATEWAY RESPONSE  **********************************/
2523
-    /********************************************************************************************************/
2524
-    /**
2525
-     * process_gateway_response
2526
-     * this is the return point for Off-Site Payment Methods
2527
-     * It will attempt to "handle the IPN" if it appears that this has not already occurred,
2528
-     * otherwise, it will load up the last payment made for the TXN.
2529
-     * If the payment retrieved looks good, it will then either:
2530
-     *    complete the current step and allow advancement to the next reg step
2531
-     *        or present the payment options again
2532
-     *
2533
-     * @access private
2534
-     * @return EE_Payment|FALSE
2535
-     * @throws EE_Error
2536
-     * @throws InvalidArgumentException
2537
-     * @throws ReflectionException
2538
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2539
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2540
-     * @throws \EventEspresso\core\exceptions\InvalidSessionDataException
2541
-     */
2542
-    public function process_gateway_response()
2543
-    {
2544
-        $payment = null;
2545
-        // how have they chosen to pay?
2546
-        $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
2547
-        // get EE_Payment_Method object
2548
-        if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
2549
-            $this->checkout->continue_reg = false;
2550
-            return false;
2551
-        }
2552
-        if (! $this->checkout->payment_method->is_off_site()) {
2553
-            return false;
2554
-        }
2555
-        $this->_validate_offsite_return();
2556
-        // DEBUG LOG
2557
-        // $this->checkout->log(
2558
-        //     __CLASS__,
2559
-        //     __FUNCTION__,
2560
-        //     __LINE__,
2561
-        //     array(
2562
-        //         'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2563
-        //         'payment_method'             => $this->checkout->payment_method,
2564
-        //     ),
2565
-        //     true
2566
-        // );
2567
-        // verify TXN
2568
-        if ($this->checkout->transaction instanceof EE_Transaction) {
2569
-            $gateway = $this->checkout->payment_method->type_obj()->get_gateway();
2570
-            if (! $gateway instanceof EE_Offsite_Gateway) {
2571
-                $this->checkout->continue_reg = false;
2572
-                return false;
2573
-            }
2574
-            $payment = $this->_process_off_site_payment($gateway);
2575
-            $payment = $this->_process_cancelled_payments($payment);
2576
-            $payment = $this->_validate_payment($payment);
2577
-            // if payment was not declined by the payment gateway or cancelled by the registrant
2578
-            if ($this->_process_payment_status($payment, EE_PMT_Base::offsite)) {
2579
-                // $this->_setup_redirect_for_next_step();
2580
-                // store that for later
2581
-                $this->checkout->payment = $payment;
2582
-                // mark this reg step as completed, as long as gateway doesn't use a separate IPN request,
2583
-                // because we will complete this step during the IPN processing then
2584
-                if ($gateway instanceof EE_Offsite_Gateway && ! $this->handle_IPN_in_this_request()) {
2585
-                    $this->set_completed();
2586
-                }
2587
-                return true;
2588
-            }
2589
-        }
2590
-        // DEBUG LOG
2591
-        // $this->checkout->log(
2592
-        //     __CLASS__,
2593
-        //     __FUNCTION__,
2594
-        //     __LINE__,
2595
-        //     array('payment' => $payment)
2596
-        // );
2597
-        $this->checkout->continue_reg = false;
2598
-        return false;
2599
-    }
2600
-
2601
-
2602
-    /**
2603
-     * _validate_return
2604
-     *
2605
-     * @access private
2606
-     * @return void
2607
-     * @throws EE_Error
2608
-     * @throws InvalidArgumentException
2609
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2610
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2611
-     * @throws \EventEspresso\core\exceptions\InvalidSessionDataException
2612
-     */
2613
-    private function _validate_offsite_return()
2614
-    {
2615
-        $TXN_ID = (int) EE_Registry::instance()->REQ->get('spco_txn', 0);
2616
-        if ($TXN_ID !== $this->checkout->transaction->ID()) {
2617
-            // Houston... we might have a problem
2618
-            $invalid_TXN = false;
2619
-            // first gather some info
2620
-            $valid_TXN = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2621
-            $primary_registrant = $valid_TXN instanceof EE_Transaction
2622
-                ? $valid_TXN->primary_registration()
2623
-                : null;
2624
-            // let's start by retrieving the cart for this TXN
2625
-            $cart = $this->checkout->get_cart_for_transaction($this->checkout->transaction);
2626
-            if ($cart instanceof EE_Cart) {
2627
-                // verify that the current cart has tickets
2628
-                $tickets = $cart->get_tickets();
2629
-                if (empty($tickets)) {
2630
-                    $invalid_TXN = true;
2631
-                }
2632
-            } else {
2633
-                $invalid_TXN = true;
2634
-            }
2635
-            $valid_TXN_SID = $primary_registrant instanceof EE_Registration
2636
-                ? $primary_registrant->session_ID()
2637
-                : null;
2638
-            // validate current Session ID and compare against valid TXN session ID
2639
-            if ($invalid_TXN // if this is already true, then skip other checks
2640
-                || EE_Session::instance()->id() === null
2641
-                || (
2642
-                    // WARNING !!!
2643
-                    // this could be PayPal sending back duplicate requests (ya they do that)
2644
-                    // or it **could** mean someone is simply registering AGAIN after having just done so
2645
-                    // so now we need to determine if this current TXN looks valid or not
2646
-                    // and whether this reg step has even been started ?
2647
-                    EE_Session::instance()->id() === $valid_TXN_SID
2648
-                    // really? you're half way through this reg step, but you never started it ?
2649
-                    && $this->checkout->transaction->reg_step_completed($this->slug()) === false
2650
-                )
2651
-            ) {
2652
-                $invalid_TXN = true;
2653
-            }
2654
-            if ($invalid_TXN) {
2655
-                // is the valid TXN completed ?
2656
-                if ($valid_TXN instanceof EE_Transaction) {
2657
-                    // has this step even been started ?
2658
-                    $reg_step_completed = $valid_TXN->reg_step_completed($this->slug());
2659
-                    if ($reg_step_completed !== false && $reg_step_completed !== true) {
2660
-                        // so it **looks** like this is a double request from PayPal
2661
-                        // so let's try to pick up where we left off
2662
-                        $this->checkout->transaction = $valid_TXN;
2663
-                        $this->checkout->refresh_all_entities(true);
2664
-                        return;
2665
-                    }
2666
-                }
2667
-                // you appear to be lost?
2668
-                $this->_redirect_wayward_request($primary_registrant);
2669
-            }
2670
-        }
2671
-    }
2672
-
2673
-
2674
-    /**
2675
-     * _redirect_wayward_request
2676
-     *
2677
-     * @access private
2678
-     * @param \EE_Registration|null $primary_registrant
2679
-     * @return bool
2680
-     * @throws EE_Error
2681
-     * @throws InvalidArgumentException
2682
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2683
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2684
-     */
2685
-    private function _redirect_wayward_request(EE_Registration $primary_registrant)
2686
-    {
2687
-        if (! $primary_registrant instanceof EE_Registration) {
2688
-            // try redirecting based on the current TXN
2689
-            $primary_registrant = $this->checkout->transaction instanceof EE_Transaction
2690
-                ? $this->checkout->transaction->primary_registration()
2691
-                : null;
2692
-        }
2693
-        if (! $primary_registrant instanceof EE_Registration) {
2694
-            EE_Error::add_error(
2695
-                sprintf(
2696
-                    esc_html__(
2697
-                        'Invalid information was received from the Off-Site Payment Processor and your Transaction details could not be retrieved from the database.%1$sPlease try again or contact %2$s for assistance.',
2698
-                        'event_espresso'
2699
-                    ),
2700
-                    '<br/>',
2701
-                    EE_Registry::instance()->CFG->organization->get_pretty('email')
2702
-                ),
2703
-                __FILE__,
2704
-                __FUNCTION__,
2705
-                __LINE__
2706
-            );
2707
-            return false;
2708
-        }
2709
-        // make sure transaction is not locked
2710
-        $this->checkout->transaction->unlock();
2711
-        wp_safe_redirect(
2712
-            add_query_arg(
2713
-                array(
2714
-                    'e_reg_url_link' => $primary_registrant->reg_url_link(),
2715
-                ),
2716
-                $this->checkout->thank_you_page_url
2717
-            )
2718
-        );
2719
-        exit();
2720
-    }
2721
-
2722
-
2723
-    /**
2724
-     * _process_off_site_payment
2725
-     *
2726
-     * @access private
2727
-     * @param \EE_Offsite_Gateway $gateway
2728
-     * @return EE_Payment
2729
-     * @throws EE_Error
2730
-     * @throws InvalidArgumentException
2731
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2732
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2733
-     */
2734
-    private function _process_off_site_payment(EE_Offsite_Gateway $gateway)
2735
-    {
2736
-        try {
2737
-            $request_data = \EE_Registry::instance()->REQ->params();
2738
-            // if gateway uses_separate_IPN_request, then we don't have to process the IPN manually
2739
-            $this->set_handle_IPN_in_this_request(
2740
-                $gateway->handle_IPN_in_this_request($request_data, false)
2741
-            );
2742
-            if ($this->handle_IPN_in_this_request()) {
2743
-                // get payment details and process results
2744
-                /** @type EE_Payment_Processor $payment_processor */
2745
-                $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2746
-                $payment = $payment_processor->process_ipn(
2747
-                    $request_data,
2748
-                    $this->checkout->transaction,
2749
-                    $this->checkout->payment_method,
2750
-                    true,
2751
-                    false
2752
-                );
2753
-                // $payment_source = 'process_ipn';
2754
-            } else {
2755
-                $payment = $this->checkout->transaction->last_payment();
2756
-                // $payment_source = 'last_payment';
2757
-            }
2758
-        } catch (Exception $e) {
2759
-            // let's just eat the exception and try to move on using any previously set payment info
2760
-            $payment = $this->checkout->transaction->last_payment();
2761
-            // $payment_source = 'last_payment after Exception';
2762
-            // but if we STILL don't have a payment object
2763
-            if (! $payment instanceof EE_Payment) {
2764
-                // then we'll object ! ( not object like a thing... but object like what a lawyer says ! )
2765
-                $this->_handle_payment_processor_exception($e);
2766
-            }
2767
-        }
2768
-        // DEBUG LOG
2769
-        // $this->checkout->log(
2770
-        //     __CLASS__,
2771
-        //     __FUNCTION__,
2772
-        //     __LINE__,
2773
-        //     array(
2774
-        //         'process_ipn_payment' => $payment,
2775
-        //         'payment_source'      => $payment_source,
2776
-        //     )
2777
-        // );
2778
-        return $payment;
2779
-    }
2780
-
2781
-
2782
-    /**
2783
-     * _process_cancelled_payments
2784
-     * just makes sure that the payment status gets updated correctly
2785
-     * so tha tan error isn't generated during payment validation
2786
-     *
2787
-     * @access private
2788
-     * @param EE_Payment $payment
2789
-     * @return EE_Payment | FALSE
2790
-     * @throws \EE_Error
2791
-     */
2792
-    private function _process_cancelled_payments($payment = null)
2793
-    {
2794
-        if ($payment instanceof EE_Payment
2795
-            && isset($_REQUEST['ee_cancel_payment'])
2796
-            && $payment->status() === EEM_Payment::status_id_failed
2797
-        ) {
2798
-            $payment->set_status(EEM_Payment::status_id_cancelled);
2799
-        }
2800
-        return $payment;
2801
-    }
2802
-
2803
-
2804
-    /**
2805
-     *    get_transaction_details_for_gateways
2806
-     *
2807
-     * @access    public
2808
-     * @return int
2809
-     * @throws EE_Error
2810
-     * @throws InvalidArgumentException
2811
-     * @throws ReflectionException
2812
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2813
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2814
-     */
2815
-    public function get_transaction_details_for_gateways()
2816
-    {
2817
-        $txn_details = array();
2818
-        // ya gotta make a choice man
2819
-        if (empty($this->checkout->selected_method_of_payment)) {
2820
-            $txn_details = array(
2821
-                'error' => esc_html__('Please select a method of payment before proceeding.', 'event_espresso'),
2822
-            );
2823
-        }
2824
-        // get EE_Payment_Method object
2825
-        if (empty($txn_details)
2826
-            &&
2827
-            ! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()
2828
-        ) {
2829
-            $txn_details = array(
2830
-                'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2831
-                'error'                      => esc_html__(
2832
-                    'A valid Payment Method could not be determined.',
2833
-                    'event_espresso'
2834
-                ),
2835
-            );
2836
-        }
2837
-        if (empty($txn_details) && $this->checkout->transaction instanceof EE_Transaction) {
2838
-            $return_url = $this->_get_return_url($this->checkout->payment_method);
2839
-            $txn_details = array(
2840
-                'TXN_ID'         => $this->checkout->transaction->ID(),
2841
-                'TXN_timestamp'  => $this->checkout->transaction->datetime(),
2842
-                'TXN_total'      => $this->checkout->transaction->total(),
2843
-                'TXN_paid'       => $this->checkout->transaction->paid(),
2844
-                'TXN_reg_steps'  => $this->checkout->transaction->reg_steps(),
2845
-                'STS_ID'         => $this->checkout->transaction->status_ID(),
2846
-                'PMD_ID'         => $this->checkout->transaction->payment_method_ID(),
2847
-                'payment_amount' => $this->checkout->amount_owing,
2848
-                'return_url'     => $return_url,
2849
-                'cancel_url'     => add_query_arg(array('ee_cancel_payment' => true), $return_url),
2850
-                'notify_url'     => EE_Config::instance()->core->txn_page_url(
2851
-                    array(
2852
-                        'e_reg_url_link'    => $this->checkout->transaction->primary_registration()->reg_url_link(),
2853
-                        'ee_payment_method' => $this->checkout->payment_method->slug(),
2854
-                    )
2855
-                ),
2856
-            );
2857
-        }
2858
-        echo wp_json_encode($txn_details);
2859
-        exit();
2860
-    }
2861
-
2862
-
2863
-    /**
2864
-     *    __sleep
2865
-     * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon
2866
-     * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the
2867
-     * reg form, because if needed, it will be regenerated anyways
2868
-     *
2869
-     * @return array
2870
-     */
2871
-    public function __sleep()
2872
-    {
2873
-        // remove the reg form and the checkout
2874
-        return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout', 'line_item_display'));
2875
-    }
15
+	/**
16
+	 * @access protected
17
+	 * @var EE_Line_Item_Display $Line_Item_Display
18
+	 */
19
+	protected $line_item_display;
20
+
21
+	/**
22
+	 * @access protected
23
+	 * @var boolean $handle_IPN_in_this_request
24
+	 */
25
+	protected $handle_IPN_in_this_request = false;
26
+
27
+
28
+	/**
29
+	 *    set_hooks - for hooking into EE Core, other modules, etc
30
+	 *
31
+	 * @access    public
32
+	 * @return    void
33
+	 */
34
+	public static function set_hooks()
35
+	{
36
+		add_filter(
37
+			'FHEE__SPCO__EE_Line_Item_Filter_Collection',
38
+			array('EE_SPCO_Reg_Step_Payment_Options', 'add_spco_line_item_filters')
39
+		);
40
+		add_action(
41
+			'wp_ajax_switch_spco_billing_form',
42
+			array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form')
43
+		);
44
+		add_action(
45
+			'wp_ajax_nopriv_switch_spco_billing_form',
46
+			array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form')
47
+		);
48
+		add_action('wp_ajax_save_payer_details', array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details'));
49
+		add_action(
50
+			'wp_ajax_nopriv_save_payer_details',
51
+			array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details')
52
+		);
53
+		add_action(
54
+			'wp_ajax_get_transaction_details_for_gateways',
55
+			array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details')
56
+		);
57
+		add_action(
58
+			'wp_ajax_nopriv_get_transaction_details_for_gateways',
59
+			array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details')
60
+		);
61
+		add_filter(
62
+			'FHEE__EED_Recaptcha___bypass_recaptcha__bypass_request_params_array',
63
+			array('EE_SPCO_Reg_Step_Payment_Options', 'bypass_recaptcha_for_load_payment_method'),
64
+			10,
65
+			1
66
+		);
67
+	}
68
+
69
+
70
+	/**
71
+	 *    ajax switch_spco_billing_form
72
+	 *
73
+	 * @throws \EE_Error
74
+	 */
75
+	public static function switch_spco_billing_form()
76
+	{
77
+		EED_Single_Page_Checkout::process_ajax_request('switch_payment_method');
78
+	}
79
+
80
+
81
+	/**
82
+	 *    ajax save_payer_details
83
+	 *
84
+	 * @throws \EE_Error
85
+	 */
86
+	public static function save_payer_details()
87
+	{
88
+		EED_Single_Page_Checkout::process_ajax_request('save_payer_details_via_ajax');
89
+	}
90
+
91
+
92
+	/**
93
+	 *    ajax get_transaction_details
94
+	 *
95
+	 * @throws \EE_Error
96
+	 */
97
+	public static function get_transaction_details()
98
+	{
99
+		EED_Single_Page_Checkout::process_ajax_request('get_transaction_details_for_gateways');
100
+	}
101
+
102
+
103
+	/**
104
+	 * bypass_recaptcha_for_load_payment_method
105
+	 *
106
+	 * @access public
107
+	 * @return array
108
+	 * @throws InvalidArgumentException
109
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
110
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
111
+	 */
112
+	public static function bypass_recaptcha_for_load_payment_method()
113
+	{
114
+		return array(
115
+			'EESID'  => EE_Registry::instance()->SSN->id(),
116
+			'step'   => 'payment_options',
117
+			'action' => 'spco_billing_form',
118
+		);
119
+	}
120
+
121
+
122
+	/**
123
+	 *    class constructor
124
+	 *
125
+	 * @access    public
126
+	 * @param    EE_Checkout $checkout
127
+	 */
128
+	public function __construct(EE_Checkout $checkout)
129
+	{
130
+		$this->_slug = 'payment_options';
131
+		$this->_name = esc_html__('Payment Options', 'event_espresso');
132
+		$this->_template = SPCO_REG_STEPS_PATH . $this->_slug . '/payment_options_main.template.php';
133
+		$this->checkout = $checkout;
134
+		$this->_reset_success_message();
135
+		$this->set_instructions(
136
+			esc_html__(
137
+				'Please select a method of payment and provide any necessary billing information before proceeding.',
138
+				'event_espresso'
139
+			)
140
+		);
141
+	}
142
+
143
+
144
+	/**
145
+	 * @return null
146
+	 */
147
+	public function line_item_display()
148
+	{
149
+		return $this->line_item_display;
150
+	}
151
+
152
+
153
+	/**
154
+	 * @param null $line_item_display
155
+	 */
156
+	public function set_line_item_display($line_item_display)
157
+	{
158
+		$this->line_item_display = $line_item_display;
159
+	}
160
+
161
+
162
+	/**
163
+	 * @return boolean
164
+	 */
165
+	public function handle_IPN_in_this_request()
166
+	{
167
+		return $this->handle_IPN_in_this_request;
168
+	}
169
+
170
+
171
+	/**
172
+	 * @param boolean $handle_IPN_in_this_request
173
+	 */
174
+	public function set_handle_IPN_in_this_request($handle_IPN_in_this_request)
175
+	{
176
+		$this->handle_IPN_in_this_request = filter_var($handle_IPN_in_this_request, FILTER_VALIDATE_BOOLEAN);
177
+	}
178
+
179
+
180
+	/**
181
+	 * translate_js_strings
182
+	 *
183
+	 * @return void
184
+	 */
185
+	public function translate_js_strings()
186
+	{
187
+		EE_Registry::$i18n_js_strings['no_payment_method'] = esc_html__(
188
+			'Please select a method of payment in order to continue.',
189
+			'event_espresso'
190
+		);
191
+		EE_Registry::$i18n_js_strings['invalid_payment_method'] = esc_html__(
192
+			'A valid method of payment could not be determined. Please refresh the page and try again.',
193
+			'event_espresso'
194
+		);
195
+		EE_Registry::$i18n_js_strings['forwarding_to_offsite'] = esc_html__(
196
+			'Forwarding to Secure Payment Provider.',
197
+			'event_espresso'
198
+		);
199
+	}
200
+
201
+
202
+	/**
203
+	 * enqueue_styles_and_scripts
204
+	 *
205
+	 * @return void
206
+	 * @throws EE_Error
207
+	 * @throws InvalidArgumentException
208
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
209
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
210
+	 */
211
+	public function enqueue_styles_and_scripts()
212
+	{
213
+		$transaction = $this->checkout->transaction;
214
+		// if the transaction isn't set or nothing is owed on it, don't enqueue any JS
215
+		if (! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) {
216
+			return;
217
+		}
218
+		foreach (EEM_Payment_Method::instance()->get_all_for_transaction(
219
+			$transaction,
220
+			EEM_Payment_Method::scope_cart
221
+		) as $payment_method) {
222
+			$type_obj = $payment_method->type_obj();
223
+			if ($type_obj instanceof EE_PMT_Base) {
224
+				$billing_form = $type_obj->generate_new_billing_form($transaction);
225
+				if ($billing_form instanceof EE_Form_Section_Proper) {
226
+					$billing_form->enqueue_js();
227
+				}
228
+			}
229
+		}
230
+	}
231
+
232
+
233
+	/**
234
+	 * initialize_reg_step
235
+	 *
236
+	 * @return bool
237
+	 * @throws EE_Error
238
+	 * @throws InvalidArgumentException
239
+	 * @throws ReflectionException
240
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
241
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
242
+	 */
243
+	public function initialize_reg_step()
244
+	{
245
+		// TODO: if /when we implement donations, then this will need overriding
246
+		if (// don't need payment options for:
247
+			// registrations made via the admin
248
+			// completed transactions
249
+			// overpaid transactions
250
+			// $ 0.00 transactions(no payment required)
251
+			! $this->checkout->payment_required()
252
+			// but do NOT remove if current action being called belongs to this reg step
253
+			&& ! is_callable(array($this, $this->checkout->action))
254
+			&& ! $this->completed()
255
+		) {
256
+			// and if so, then we no longer need the Payment Options step
257
+			if ($this->is_current_step()) {
258
+				$this->checkout->generate_reg_form = false;
259
+			}
260
+			$this->checkout->remove_reg_step($this->_slug);
261
+			// DEBUG LOG
262
+			// $this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
263
+			return false;
264
+		}
265
+		// load EEM_Payment_Method
266
+		EE_Registry::instance()->load_model('Payment_Method');
267
+		// get all active payment methods
268
+		$this->checkout->available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction(
269
+			$this->checkout->transaction,
270
+			EEM_Payment_Method::scope_cart
271
+		);
272
+		return true;
273
+	}
274
+
275
+
276
+	/**
277
+	 * @return EE_Form_Section_Proper
278
+	 * @throws EE_Error
279
+	 * @throws InvalidArgumentException
280
+	 * @throws ReflectionException
281
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
282
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
283
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
284
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
285
+	 */
286
+	public function generate_reg_form()
287
+	{
288
+		// reset in case someone changes their mind
289
+		$this->_reset_selected_method_of_payment();
290
+		// set some defaults
291
+		$this->checkout->selected_method_of_payment = 'payments_closed';
292
+		$registrations_requiring_payment = array();
293
+		$registrations_for_free_events = array();
294
+		$registrations_requiring_pre_approval = array();
295
+		$sold_out_events = array();
296
+		$insufficient_spaces_available = array();
297
+		$no_payment_required = true;
298
+		// loop thru registrations to gather info
299
+		$registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params);
300
+		$ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
301
+			$registrations,
302
+			$this->checkout->revisit
303
+		);
304
+		foreach ($registrations as $REG_ID => $registration) {
305
+			/** @var $registration EE_Registration */
306
+			// has this registration lost it's space ?
307
+			if (isset($ejected_registrations[ $REG_ID ])) {
308
+				if ($registration->event()->is_sold_out() || $registration->event()->is_sold_out(true)) {
309
+					$sold_out_events[ $registration->event()->ID() ] = $registration->event();
310
+				} else {
311
+					$insufficient_spaces_available[ $registration->event()->ID() ] = $registration->event();
312
+				}
313
+				continue;
314
+			}
315
+			// event requires admin approval
316
+			if ($registration->status_ID() === EEM_Registration::status_id_not_approved) {
317
+				// add event to list of events with pre-approval reg status
318
+				$registrations_requiring_pre_approval[ $REG_ID ] = $registration;
319
+				do_action(
320
+					'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_pre_approval',
321
+					$registration->event(),
322
+					$this
323
+				);
324
+				continue;
325
+			}
326
+			if ($this->checkout->revisit
327
+				&& $registration->status_ID() !== EEM_Registration::status_id_approved
328
+				&& (
329
+					$registration->event()->is_sold_out()
330
+					|| $registration->event()->is_sold_out(true)
331
+				)
332
+			) {
333
+				// add event to list of events that are sold out
334
+				$sold_out_events[ $registration->event()->ID() ] = $registration->event();
335
+				do_action(
336
+					'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__sold_out_event',
337
+					$registration->event(),
338
+					$this
339
+				);
340
+				continue;
341
+			}
342
+			// are they allowed to pay now and is there monies owing?
343
+			if ($registration->owes_monies_and_can_pay()) {
344
+				$registrations_requiring_payment[ $REG_ID ] = $registration;
345
+				do_action(
346
+					'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_payment',
347
+					$registration->event(),
348
+					$this
349
+				);
350
+			} elseif (! $this->checkout->revisit
351
+					  && $registration->status_ID() !== EEM_Registration::status_id_not_approved
352
+					  && $registration->ticket()->is_free()
353
+			) {
354
+				$registrations_for_free_events[ $registration->ticket()->ID() ] = $registration;
355
+			}
356
+		}
357
+		$subsections = array();
358
+		// now decide which template to load
359
+		if (! empty($sold_out_events)) {
360
+			$subsections['sold_out_events'] = $this->_sold_out_events($sold_out_events);
361
+		}
362
+		if (! empty($insufficient_spaces_available)) {
363
+			$subsections['insufficient_space'] = $this->_insufficient_spaces_available(
364
+				$insufficient_spaces_available
365
+			);
366
+		}
367
+		if (! empty($registrations_requiring_pre_approval)) {
368
+			$subsections['registrations_requiring_pre_approval'] = $this->_registrations_requiring_pre_approval(
369
+				$registrations_requiring_pre_approval
370
+			);
371
+		}
372
+		if (! empty($registrations_for_free_events)) {
373
+			$subsections['no_payment_required'] = $this->_no_payment_required($registrations_for_free_events);
374
+		}
375
+		if (! empty($registrations_requiring_payment)) {
376
+			if ($this->checkout->amount_owing > 0) {
377
+				// autoload Line_Item_Display classes
378
+				EEH_Autoloader::register_line_item_filter_autoloaders();
379
+				$line_item_filter_processor = new EE_Line_Item_Filter_Processor(
380
+					apply_filters(
381
+						'FHEE__SPCO__EE_Line_Item_Filter_Collection',
382
+						new EE_Line_Item_Filter_Collection()
383
+					),
384
+					$this->checkout->cart->get_grand_total()
385
+				);
386
+				/** @var EE_Line_Item $filtered_line_item_tree */
387
+				$filtered_line_item_tree = $line_item_filter_processor->process();
388
+				EEH_Autoloader::register_line_item_display_autoloaders();
389
+				$this->set_line_item_display(new EE_Line_Item_Display('spco'));
390
+				$subsections['payment_options'] = $this->_display_payment_options(
391
+					$this->line_item_display->display_line_item(
392
+						$filtered_line_item_tree,
393
+						array('registrations' => $registrations)
394
+					)
395
+				);
396
+				$this->checkout->amount_owing = $filtered_line_item_tree->total();
397
+				$this->_apply_registration_payments_to_amount_owing($registrations);
398
+			}
399
+			$no_payment_required = false;
400
+		} else {
401
+			$this->_hide_reg_step_submit_button_if_revisit();
402
+		}
403
+		$this->_save_selected_method_of_payment();
404
+
405
+		$subsections['default_hidden_inputs'] = $this->reg_step_hidden_inputs();
406
+		$subsections['extra_hidden_inputs'] = $this->_extra_hidden_inputs($no_payment_required);
407
+
408
+		return new EE_Form_Section_Proper(
409
+			array(
410
+				'name'            => $this->reg_form_name(),
411
+				'html_id'         => $this->reg_form_name(),
412
+				'subsections'     => $subsections,
413
+				'layout_strategy' => new EE_No_Layout(),
414
+			)
415
+		);
416
+	}
417
+
418
+
419
+	/**
420
+	 * add line item filters required for this reg step
421
+	 * these filters are applied via this line in EE_SPCO_Reg_Step_Payment_Options::set_hooks():
422
+	 *        add_filter( 'FHEE__SPCO__EE_Line_Item_Filter_Collection', array( 'EE_SPCO_Reg_Step_Payment_Options',
423
+	 *        'add_spco_line_item_filters' ) ); so any code that wants to use the same set of filters during the
424
+	 *        payment options reg step, can apply these filters via the following: apply_filters(
425
+	 *        'FHEE__SPCO__EE_Line_Item_Filter_Collection', new EE_Line_Item_Filter_Collection() ) or to an existing
426
+	 *        filter collection by passing that instead of instantiating a new collection
427
+	 *
428
+	 * @param \EE_Line_Item_Filter_Collection $line_item_filter_collection
429
+	 * @return EE_Line_Item_Filter_Collection
430
+	 * @throws EE_Error
431
+	 * @throws InvalidArgumentException
432
+	 * @throws ReflectionException
433
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
434
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
435
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
436
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
437
+	 */
438
+	public static function add_spco_line_item_filters(EE_Line_Item_Filter_Collection $line_item_filter_collection)
439
+	{
440
+		if (! EE_Registry::instance()->SSN instanceof EE_Session) {
441
+			return $line_item_filter_collection;
442
+		}
443
+		if (! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) {
444
+			return $line_item_filter_collection;
445
+		}
446
+		if (! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) {
447
+			return $line_item_filter_collection;
448
+		}
449
+		$line_item_filter_collection->add(
450
+			new EE_Billable_Line_Item_Filter(
451
+				EE_SPCO_Reg_Step_Payment_Options::remove_ejected_registrations(
452
+					EE_Registry::instance()->SSN->checkout()->transaction->registrations(
453
+						EE_Registry::instance()->SSN->checkout()->reg_cache_where_params
454
+					)
455
+				)
456
+			)
457
+		);
458
+		$line_item_filter_collection->add(new EE_Non_Zero_Line_Item_Filter());
459
+		return $line_item_filter_collection;
460
+	}
461
+
462
+
463
+	/**
464
+	 * remove_ejected_registrations
465
+	 * if a registrant has lost their potential space at an event due to lack of payment,
466
+	 * then this method removes them from the list of registrations being paid for during this request
467
+	 *
468
+	 * @param \EE_Registration[] $registrations
469
+	 * @return EE_Registration[]
470
+	 * @throws EE_Error
471
+	 * @throws InvalidArgumentException
472
+	 * @throws ReflectionException
473
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
474
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
475
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
476
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
477
+	 */
478
+	public static function remove_ejected_registrations(array $registrations)
479
+	{
480
+		$ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
481
+			$registrations,
482
+			EE_Registry::instance()->SSN->checkout()->revisit
483
+		);
484
+		foreach ($registrations as $REG_ID => $registration) {
485
+			// has this registration lost it's space ?
486
+			if (isset($ejected_registrations[ $REG_ID ])) {
487
+				unset($registrations[ $REG_ID ]);
488
+				continue;
489
+			}
490
+		}
491
+		return $registrations;
492
+	}
493
+
494
+
495
+	/**
496
+	 * find_registrations_that_lost_their_space
497
+	 * If a registrant chooses an offline payment method like Invoice,
498
+	 * then no space is reserved for them at the event until they fully pay fo that site
499
+	 * (unless the event's default reg status is set to APPROVED)
500
+	 * if a registrant then later returns to pay, but the number of spaces available has been reduced due to sales,
501
+	 * then this method will determine which registrations have lost the ability to complete the reg process.
502
+	 *
503
+	 * @param \EE_Registration[] $registrations
504
+	 * @param bool               $revisit
505
+	 * @return array
506
+	 * @throws EE_Error
507
+	 * @throws InvalidArgumentException
508
+	 * @throws ReflectionException
509
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
510
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
511
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
512
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
513
+	 */
514
+	public static function find_registrations_that_lost_their_space(array $registrations, $revisit = false)
515
+	{
516
+		// registrations per event
517
+		$event_reg_count = array();
518
+		// spaces left per event
519
+		$event_spaces_remaining = array();
520
+		// tickets left sorted by ID
521
+		$tickets_remaining = array();
522
+		// registrations that have lost their space
523
+		$ejected_registrations = array();
524
+		foreach ($registrations as $REG_ID => $registration) {
525
+			if ($registration->status_ID() === EEM_Registration::status_id_approved
526
+				|| apply_filters(
527
+					'FHEE__EE_SPCO_Reg_Step_Payment_Options__find_registrations_that_lost_their_space__allow_reg_payment',
528
+					false,
529
+					$registration,
530
+					$revisit
531
+				)
532
+			) {
533
+				continue;
534
+			}
535
+			$EVT_ID = $registration->event_ID();
536
+			$ticket = $registration->ticket();
537
+			if (! isset($tickets_remaining[ $ticket->ID() ])) {
538
+				$tickets_remaining[ $ticket->ID() ] = $ticket->remaining();
539
+			}
540
+			if ($tickets_remaining[ $ticket->ID() ] > 0) {
541
+				if (! isset($event_reg_count[ $EVT_ID ])) {
542
+					$event_reg_count[ $EVT_ID ] = 0;
543
+				}
544
+				$event_reg_count[ $EVT_ID ]++;
545
+				if (! isset($event_spaces_remaining[ $EVT_ID ])) {
546
+					$event_spaces_remaining[ $EVT_ID ] = $registration->event()->spaces_remaining_for_sale();
547
+				}
548
+			}
549
+			if ($revisit
550
+				&& ($tickets_remaining[ $ticket->ID() ] === 0
551
+					|| $event_reg_count[ $EVT_ID ] > $event_spaces_remaining[ $EVT_ID ]
552
+				)
553
+			) {
554
+				$ejected_registrations[ $REG_ID ] = $registration->event();
555
+				if ($registration->status_ID() !== EEM_Registration::status_id_wait_list) {
556
+					/** @type EE_Registration_Processor $registration_processor */
557
+					$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
558
+					// at this point, we should have enough details about the registrant to consider the registration
559
+					// NOT incomplete
560
+					$registration_processor->manually_update_registration_status(
561
+						$registration,
562
+						EEM_Registration::status_id_wait_list
563
+					);
564
+				}
565
+			}
566
+		}
567
+		return $ejected_registrations;
568
+	}
569
+
570
+
571
+	/**
572
+	 * _hide_reg_step_submit_button
573
+	 * removes the html for the reg step submit button
574
+	 * by replacing it with an empty string via filter callback
575
+	 *
576
+	 * @return void
577
+	 */
578
+	protected function _adjust_registration_status_if_event_old_sold()
579
+	{
580
+	}
581
+
582
+
583
+	/**
584
+	 * _hide_reg_step_submit_button
585
+	 * removes the html for the reg step submit button
586
+	 * by replacing it with an empty string via filter callback
587
+	 *
588
+	 * @return void
589
+	 */
590
+	protected function _hide_reg_step_submit_button_if_revisit()
591
+	{
592
+		if ($this->checkout->revisit) {
593
+			add_filter('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', '__return_empty_string');
594
+		}
595
+	}
596
+
597
+
598
+	/**
599
+	 * sold_out_events
600
+	 * displays notices regarding events that have sold out since hte registrant first signed up
601
+	 *
602
+	 * @param \EE_Event[] $sold_out_events_array
603
+	 * @return \EE_Form_Section_Proper
604
+	 * @throws \EE_Error
605
+	 */
606
+	private function _sold_out_events($sold_out_events_array = array())
607
+	{
608
+		// set some defaults
609
+		$this->checkout->selected_method_of_payment = 'events_sold_out';
610
+		$sold_out_events = '';
611
+		foreach ($sold_out_events_array as $sold_out_event) {
612
+			$sold_out_events .= EEH_HTML::li(
613
+				EEH_HTML::span(
614
+					'  ' . $sold_out_event->name(),
615
+					'',
616
+					'dashicons dashicons-marker ee-icon-size-16 pink-text'
617
+				)
618
+			);
619
+		}
620
+		return new EE_Form_Section_Proper(
621
+			array(
622
+				'layout_strategy' => new EE_Template_Layout(
623
+					array(
624
+						'layout_template_file' => SPCO_REG_STEPS_PATH
625
+												  . $this->_slug
626
+												  . '/sold_out_events.template.php',
627
+						'template_args'        => apply_filters(
628
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args',
629
+							array(
630
+								'sold_out_events'     => $sold_out_events,
631
+								'sold_out_events_msg' => apply_filters(
632
+									'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__sold_out_events_msg',
633
+									sprintf(
634
+										esc_html__(
635
+											'It appears that the event you were about to make a payment for has sold out since you first registered. If you have already made a partial payment towards this event, please contact the event administrator for a refund.%3$s%3$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%2$s',
636
+											'event_espresso'
637
+										),
638
+										'<strong>',
639
+										'</strong>',
640
+										'<br />'
641
+									)
642
+								),
643
+							)
644
+						),
645
+					)
646
+				),
647
+			)
648
+		);
649
+	}
650
+
651
+
652
+	/**
653
+	 * _insufficient_spaces_available
654
+	 * displays notices regarding events that do not have enough remaining spaces
655
+	 * to satisfy the current number of registrations looking to pay
656
+	 *
657
+	 * @param \EE_Event[] $insufficient_spaces_events_array
658
+	 * @return \EE_Form_Section_Proper
659
+	 * @throws \EE_Error
660
+	 */
661
+	private function _insufficient_spaces_available($insufficient_spaces_events_array = array())
662
+	{
663
+		// set some defaults
664
+		$this->checkout->selected_method_of_payment = 'invoice';
665
+		$insufficient_space_events = '';
666
+		foreach ($insufficient_spaces_events_array as $event) {
667
+			if ($event instanceof EE_Event) {
668
+				$insufficient_space_events .= EEH_HTML::li(
669
+					EEH_HTML::span(' ' . $event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text')
670
+				);
671
+			}
672
+		}
673
+		return new EE_Form_Section_Proper(
674
+			array(
675
+				'subsections'     => array(
676
+					'default_hidden_inputs' => $this->reg_step_hidden_inputs(),
677
+					'extra_hidden_inputs'   => $this->_extra_hidden_inputs(),
678
+				),
679
+				'layout_strategy' => new EE_Template_Layout(
680
+					array(
681
+						'layout_template_file' => SPCO_REG_STEPS_PATH
682
+												  . $this->_slug
683
+												  . '/sold_out_events.template.php',
684
+						'template_args'        => apply_filters(
685
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__template_args',
686
+							array(
687
+								'sold_out_events'     => $insufficient_space_events,
688
+								'sold_out_events_msg' => apply_filters(
689
+									'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__insufficient_space_msg',
690
+									esc_html__(
691
+										'It appears that the event you were about to make a payment for has sold additional tickets since you first registered, and there are no longer enough spaces left to accommodate your selections. You may continue to pay and secure the available space(s) remaining, or simply cancel if you no longer wish to purchase. If you have already made a partial payment towards this event, please contact the event administrator for a refund.',
692
+										'event_espresso'
693
+									)
694
+								),
695
+							)
696
+						),
697
+					)
698
+				),
699
+			)
700
+		);
701
+	}
702
+
703
+
704
+	/**
705
+	 * registrations_requiring_pre_approval
706
+	 *
707
+	 * @param array $registrations_requiring_pre_approval
708
+	 * @return EE_Form_Section_Proper
709
+	 * @throws EE_Error
710
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
711
+	 */
712
+	private function _registrations_requiring_pre_approval($registrations_requiring_pre_approval = array())
713
+	{
714
+		$events_requiring_pre_approval = array();
715
+		foreach ($registrations_requiring_pre_approval as $registration) {
716
+			if ($registration instanceof EE_Registration && $registration->event() instanceof EE_Event) {
717
+				$events_requiring_pre_approval[ $registration->event()->ID() ] = EEH_HTML::li(
718
+					EEH_HTML::span(
719
+						'',
720
+						'',
721
+						'dashicons dashicons-marker ee-icon-size-16 orange-text'
722
+					)
723
+					. EEH_HTML::span($registration->event()->name(), '', 'orange-text')
724
+				);
725
+			}
726
+		}
727
+		return new EE_Form_Section_Proper(
728
+			array(
729
+				'layout_strategy' => new EE_Template_Layout(
730
+					array(
731
+						'layout_template_file' => SPCO_REG_STEPS_PATH
732
+												  . $this->_slug
733
+												  . '/events_requiring_pre_approval.template.php', // layout_template
734
+						'template_args'        => apply_filters(
735
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args',
736
+							array(
737
+								'events_requiring_pre_approval'     => implode('', $events_requiring_pre_approval),
738
+								'events_requiring_pre_approval_msg' => apply_filters(
739
+									'FHEE__EE_SPCO_Reg_Step_Payment_Options___events_requiring_pre_approval__events_requiring_pre_approval_msg',
740
+									esc_html__(
741
+										'The following events do not require payment at this time and will not be billed during this transaction. Billing will only occur after the attendee has been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.',
742
+										'event_espresso'
743
+									)
744
+								),
745
+							)
746
+						),
747
+					)
748
+				),
749
+			)
750
+		);
751
+	}
752
+
753
+
754
+	/**
755
+	 * _no_payment_required
756
+	 *
757
+	 * @param \EE_Event[] $registrations_for_free_events
758
+	 * @return \EE_Form_Section_Proper
759
+	 * @throws \EE_Error
760
+	 */
761
+	private function _no_payment_required($registrations_for_free_events = array())
762
+	{
763
+		// set some defaults
764
+		$this->checkout->selected_method_of_payment = 'no_payment_required';
765
+		// generate no_payment_required form
766
+		return new EE_Form_Section_Proper(
767
+			array(
768
+				'layout_strategy' => new EE_Template_Layout(
769
+					array(
770
+						'layout_template_file' => SPCO_REG_STEPS_PATH
771
+												  . $this->_slug
772
+												  . '/no_payment_required.template.php', // layout_template
773
+						'template_args'        => apply_filters(
774
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___no_payment_required__template_args',
775
+							array(
776
+								'revisit'                       => $this->checkout->revisit,
777
+								'registrations'                 => array(),
778
+								'ticket_count'                  => array(),
779
+								'registrations_for_free_events' => $registrations_for_free_events,
780
+								'no_payment_required_msg'       => EEH_HTML::p(
781
+									esc_html__('This is a free event, so no billing will occur.', 'event_espresso')
782
+								),
783
+							)
784
+						),
785
+					)
786
+				),
787
+			)
788
+		);
789
+	}
790
+
791
+
792
+	/**
793
+	 * _display_payment_options
794
+	 *
795
+	 * @param string $transaction_details
796
+	 * @return EE_Form_Section_Proper
797
+	 * @throws EE_Error
798
+	 * @throws InvalidArgumentException
799
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
800
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
801
+	 */
802
+	private function _display_payment_options($transaction_details = '')
803
+	{
804
+		// has method_of_payment been set by no-js user?
805
+		$this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment();
806
+		// build payment options form
807
+		return apply_filters(
808
+			'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__payment_options_form',
809
+			new EE_Form_Section_Proper(
810
+				array(
811
+					'subsections'     => array(
812
+						'before_payment_options' => apply_filters(
813
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__before_payment_options',
814
+							new EE_Form_Section_Proper(
815
+								array('layout_strategy' => new EE_Div_Per_Section_Layout())
816
+							)
817
+						),
818
+						'payment_options'        => $this->_setup_payment_options(),
819
+						'after_payment_options'  => apply_filters(
820
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__after_payment_options',
821
+							new EE_Form_Section_Proper(
822
+								array('layout_strategy' => new EE_Div_Per_Section_Layout())
823
+							)
824
+						),
825
+					),
826
+					'layout_strategy' => new EE_Template_Layout(
827
+						array(
828
+							'layout_template_file' => $this->_template,
829
+							'template_args'        => apply_filters(
830
+								'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__template_args',
831
+								array(
832
+									'reg_count'                 => $this->line_item_display->total_items(),
833
+									'transaction_details'       => $transaction_details,
834
+									'available_payment_methods' => array(),
835
+								)
836
+							),
837
+						)
838
+					),
839
+				)
840
+			)
841
+		);
842
+	}
843
+
844
+
845
+	/**
846
+	 * _extra_hidden_inputs
847
+	 *
848
+	 * @param bool $no_payment_required
849
+	 * @return \EE_Form_Section_Proper
850
+	 * @throws \EE_Error
851
+	 */
852
+	private function _extra_hidden_inputs($no_payment_required = true)
853
+	{
854
+		return new EE_Form_Section_Proper(
855
+			array(
856
+				'html_id'         => 'ee-' . $this->slug() . '-extra-hidden-inputs',
857
+				'layout_strategy' => new EE_Div_Per_Section_Layout(),
858
+				'subsections'     => array(
859
+					'spco_no_payment_required' => new EE_Hidden_Input(
860
+						array(
861
+							'normalization_strategy' => new EE_Boolean_Normalization(),
862
+							'html_name'              => 'spco_no_payment_required',
863
+							'html_id'                => 'spco-no-payment-required-payment_options',
864
+							'default'                => $no_payment_required,
865
+						)
866
+					),
867
+					'spco_transaction_id'      => new EE_Fixed_Hidden_Input(
868
+						array(
869
+							'normalization_strategy' => new EE_Int_Normalization(),
870
+							'html_name'              => 'spco_transaction_id',
871
+							'html_id'                => 'spco-transaction-id',
872
+							'default'                => $this->checkout->transaction->ID(),
873
+						)
874
+					),
875
+				),
876
+			)
877
+		);
878
+	}
879
+
880
+
881
+	/**
882
+	 *    _apply_registration_payments_to_amount_owing
883
+	 *
884
+	 * @access protected
885
+	 * @param array $registrations
886
+	 * @throws EE_Error
887
+	 */
888
+	protected function _apply_registration_payments_to_amount_owing(array $registrations)
889
+	{
890
+		$payments = array();
891
+		foreach ($registrations as $registration) {
892
+			if ($registration instanceof EE_Registration && $registration->owes_monies_and_can_pay()) {
893
+				$payments += $registration->registration_payments();
894
+			}
895
+		}
896
+		if (! empty($payments)) {
897
+			foreach ($payments as $payment) {
898
+				if ($payment instanceof EE_Registration_Payment) {
899
+					$this->checkout->amount_owing -= $payment->amount();
900
+				}
901
+			}
902
+		}
903
+	}
904
+
905
+
906
+	/**
907
+	 *    _reset_selected_method_of_payment
908
+	 *
909
+	 * @access    private
910
+	 * @param    bool $force_reset
911
+	 * @return void
912
+	 * @throws InvalidArgumentException
913
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
914
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
915
+	 */
916
+	private function _reset_selected_method_of_payment($force_reset = false)
917
+	{
918
+		$reset_payment_method = $force_reset
919
+			? true
920
+			: sanitize_text_field(EE_Registry::instance()->REQ->get('reset_payment_method', false));
921
+		if ($reset_payment_method) {
922
+			$this->checkout->selected_method_of_payment = null;
923
+			$this->checkout->payment_method = null;
924
+			$this->checkout->billing_form = null;
925
+			$this->_save_selected_method_of_payment();
926
+		}
927
+	}
928
+
929
+
930
+	/**
931
+	 * _save_selected_method_of_payment
932
+	 * stores the selected_method_of_payment in the session
933
+	 * so that it's available for all subsequent requests including AJAX
934
+	 *
935
+	 * @access        private
936
+	 * @param string $selected_method_of_payment
937
+	 * @return void
938
+	 * @throws InvalidArgumentException
939
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
940
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
941
+	 */
942
+	private function _save_selected_method_of_payment($selected_method_of_payment = '')
943
+	{
944
+		$selected_method_of_payment = ! empty($selected_method_of_payment)
945
+			? $selected_method_of_payment
946
+			: $this->checkout->selected_method_of_payment;
947
+		EE_Registry::instance()->SSN->set_session_data(
948
+			array('selected_method_of_payment' => $selected_method_of_payment)
949
+		);
950
+	}
951
+
952
+
953
+	/**
954
+	 * _setup_payment_options
955
+	 *
956
+	 * @return EE_Form_Section_Proper
957
+	 * @throws EE_Error
958
+	 * @throws InvalidArgumentException
959
+	 * @throws ReflectionException
960
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
961
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
962
+	 */
963
+	public function _setup_payment_options()
964
+	{
965
+		// load payment method classes
966
+		$this->checkout->available_payment_methods = $this->_get_available_payment_methods();
967
+		if (empty($this->checkout->available_payment_methods)) {
968
+			EE_Error::add_error(
969
+				apply_filters(
970
+					'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__error_message_no_payment_methods',
971
+					sprintf(
972
+						esc_html__(
973
+							'Sorry, you cannot complete your purchase because a payment method is not active.%1$s Please contact %2$s for assistance and provide a description of the problem.',
974
+							'event_espresso'
975
+						),
976
+						'<br>',
977
+						EE_Registry::instance()->CFG->organization->get_pretty('email')
978
+					)
979
+				),
980
+				__FILE__,
981
+				__FUNCTION__,
982
+				__LINE__
983
+			);
984
+		}
985
+		// switch up header depending on number of available payment methods
986
+		$payment_method_header = count($this->checkout->available_payment_methods) > 1
987
+			? apply_filters(
988
+				'FHEE__registration_page_payment_options__method_of_payment_hdr',
989
+				esc_html__('Please Select Your Method of Payment', 'event_espresso')
990
+			)
991
+			: apply_filters(
992
+				'FHEE__registration_page_payment_options__method_of_payment_hdr',
993
+				esc_html__('Method of Payment', 'event_espresso')
994
+			);
995
+		$available_payment_methods = array(
996
+			// display the "Payment Method" header
997
+			'payment_method_header' => new EE_Form_Section_HTML(
998
+				apply_filters(
999
+					'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__payment_method_header',
1000
+					EEH_HTML::h4($payment_method_header, 'method-of-payment-hdr'),
1001
+					$payment_method_header
1002
+				)
1003
+			),
1004
+		);
1005
+		// the list of actual payment methods ( invoice, paypal, etc ) in a  ( slug => HTML )  format
1006
+		$available_payment_method_options = array();
1007
+		$default_payment_method_option = array();
1008
+		// additional instructions to be displayed and hidden below payment methods (adding a clearing div to start)
1009
+		$payment_methods_billing_info = array(
1010
+			new EE_Form_Section_HTML(
1011
+				EEH_HTML::div('<br />', '', '', 'clear:both;')
1012
+			),
1013
+		);
1014
+		// loop through payment methods
1015
+		foreach ($this->checkout->available_payment_methods as $payment_method) {
1016
+			if ($payment_method instanceof EE_Payment_Method) {
1017
+				$payment_method_button = EEH_HTML::img(
1018
+					$payment_method->button_url(),
1019
+					$payment_method->name(),
1020
+					'spco-payment-method-' . $payment_method->slug() . '-btn-img',
1021
+					'spco-payment-method-btn-img'
1022
+				);
1023
+				// check if any payment methods are set as default
1024
+				// if payment method is already selected OR nothing is selected and this payment method should be
1025
+				// open_by_default
1026
+				if (($this->checkout->selected_method_of_payment === $payment_method->slug())
1027
+					|| (! $this->checkout->selected_method_of_payment && $payment_method->open_by_default())
1028
+				) {
1029
+					$this->checkout->selected_method_of_payment = $payment_method->slug();
1030
+					$this->_save_selected_method_of_payment();
1031
+					$default_payment_method_option[ $payment_method->slug() ] = $payment_method_button;
1032
+				} else {
1033
+					$available_payment_method_options[ $payment_method->slug() ] = $payment_method_button;
1034
+				}
1035
+				$payment_methods_billing_info[ $payment_method->slug(
1036
+				) . '-info' ] = $this->_payment_method_billing_info(
1037
+					$payment_method
1038
+				);
1039
+			}
1040
+		}
1041
+		// prepend available_payment_method_options with default_payment_method_option so that it appears first in list
1042
+		// of PMs
1043
+		$available_payment_method_options = $default_payment_method_option + $available_payment_method_options;
1044
+		// now generate the actual form  inputs
1045
+		$available_payment_methods['available_payment_methods'] = $this->_available_payment_method_inputs(
1046
+			$available_payment_method_options
1047
+		);
1048
+		$available_payment_methods += $payment_methods_billing_info;
1049
+		// build the available payment methods form
1050
+		return new EE_Form_Section_Proper(
1051
+			array(
1052
+				'html_id'         => 'spco-available-methods-of-payment-dv',
1053
+				'subsections'     => $available_payment_methods,
1054
+				'layout_strategy' => new EE_Div_Per_Section_Layout(),
1055
+			)
1056
+		);
1057
+	}
1058
+
1059
+
1060
+	/**
1061
+	 * _get_available_payment_methods
1062
+	 *
1063
+	 * @return EE_Payment_Method[]
1064
+	 * @throws EE_Error
1065
+	 * @throws InvalidArgumentException
1066
+	 * @throws ReflectionException
1067
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1068
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1069
+	 */
1070
+	protected function _get_available_payment_methods()
1071
+	{
1072
+		if (! empty($this->checkout->available_payment_methods)) {
1073
+			return $this->checkout->available_payment_methods;
1074
+		}
1075
+		$available_payment_methods = array();
1076
+		// load EEM_Payment_Method
1077
+		EE_Registry::instance()->load_model('Payment_Method');
1078
+		/** @type EEM_Payment_Method $EEM_Payment_Method */
1079
+		$EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method;
1080
+		// get all active payment methods
1081
+		$payment_methods = $EEM_Payment_Method->get_all_for_transaction(
1082
+			$this->checkout->transaction,
1083
+			EEM_Payment_Method::scope_cart
1084
+		);
1085
+		foreach ($payment_methods as $payment_method) {
1086
+			if ($payment_method instanceof EE_Payment_Method) {
1087
+				$available_payment_methods[ $payment_method->slug() ] = $payment_method;
1088
+			}
1089
+		}
1090
+		return $available_payment_methods;
1091
+	}
1092
+
1093
+
1094
+	/**
1095
+	 *    _available_payment_method_inputs
1096
+	 *
1097
+	 * @access    private
1098
+	 * @param    array $available_payment_method_options
1099
+	 * @return    \EE_Form_Section_Proper
1100
+	 */
1101
+	private function _available_payment_method_inputs($available_payment_method_options = array())
1102
+	{
1103
+		// generate inputs
1104
+		return new EE_Form_Section_Proper(
1105
+			array(
1106
+				'html_id'         => 'ee-available-payment-method-inputs',
1107
+				'layout_strategy' => new EE_Div_Per_Section_Layout(),
1108
+				'subsections'     => array(
1109
+					'' => new EE_Radio_Button_Input(
1110
+						$available_payment_method_options,
1111
+						array(
1112
+							'html_name'          => 'selected_method_of_payment',
1113
+							'html_class'         => 'spco-payment-method',
1114
+							'default'            => $this->checkout->selected_method_of_payment,
1115
+							'label_size'         => 11,
1116
+							'enforce_label_size' => true,
1117
+						)
1118
+					),
1119
+				),
1120
+			)
1121
+		);
1122
+	}
1123
+
1124
+
1125
+	/**
1126
+	 *    _payment_method_billing_info
1127
+	 *
1128
+	 * @access    private
1129
+	 * @param    EE_Payment_Method $payment_method
1130
+	 * @return EE_Form_Section_Proper
1131
+	 * @throws EE_Error
1132
+	 * @throws InvalidArgumentException
1133
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1134
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1135
+	 */
1136
+	private function _payment_method_billing_info(EE_Payment_Method $payment_method)
1137
+	{
1138
+		$currently_selected = $this->checkout->selected_method_of_payment === $payment_method->slug()
1139
+			? true
1140
+			: false;
1141
+		// generate the billing form for payment method
1142
+		$billing_form = $currently_selected
1143
+			? $this->_get_billing_form_for_payment_method($payment_method)
1144
+			: new EE_Form_Section_HTML();
1145
+		$this->checkout->billing_form = $currently_selected
1146
+			? $billing_form
1147
+			: $this->checkout->billing_form;
1148
+		// it's all in the details
1149
+		$info_html = EEH_HTML::h3(
1150
+			esc_html__('Important information regarding your payment', 'event_espresso'),
1151
+			'',
1152
+			'spco-payment-method-hdr'
1153
+		);
1154
+		// add some info regarding the step, either from what's saved in the admin,
1155
+		// or a default string depending on whether the PM has a billing form or not
1156
+		if ($payment_method->description()) {
1157
+			$payment_method_info = $payment_method->description();
1158
+		} elseif ($billing_form instanceof EE_Billing_Info_Form) {
1159
+			$payment_method_info = sprintf(
1160
+				esc_html__(
1161
+					'Please provide the following billing information, then click the "%1$s" button below in order to proceed.',
1162
+					'event_espresso'
1163
+				),
1164
+				$this->submit_button_text()
1165
+			);
1166
+		} else {
1167
+			$payment_method_info = sprintf(
1168
+				esc_html__('Please click the "%1$s" button below in order to proceed.', 'event_espresso'),
1169
+				$this->submit_button_text()
1170
+			);
1171
+		}
1172
+		$info_html .= EEH_HTML::div(
1173
+			apply_filters(
1174
+				'FHEE__EE_SPCO_Reg_Step_Payment_Options___payment_method_billing_info__payment_method_info',
1175
+				$payment_method_info
1176
+			),
1177
+			'',
1178
+			'spco-payment-method-desc ee-attention'
1179
+		);
1180
+		return new EE_Form_Section_Proper(
1181
+			array(
1182
+				'html_id'         => 'spco-payment-method-info-' . $payment_method->slug(),
1183
+				'html_class'      => 'spco-payment-method-info-dv',
1184
+				// only display the selected or default PM
1185
+				'html_style'      => $currently_selected ? '' : 'display:none;',
1186
+				'layout_strategy' => new EE_Div_Per_Section_Layout(),
1187
+				'subsections'     => array(
1188
+					'info'         => new EE_Form_Section_HTML($info_html),
1189
+					'billing_form' => $currently_selected ? $billing_form : new EE_Form_Section_HTML(),
1190
+				),
1191
+			)
1192
+		);
1193
+	}
1194
+
1195
+
1196
+	/**
1197
+	 * get_billing_form_html_for_payment_method
1198
+	 *
1199
+	 * @access public
1200
+	 * @return string
1201
+	 * @throws EE_Error
1202
+	 * @throws InvalidArgumentException
1203
+	 * @throws ReflectionException
1204
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1205
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1206
+	 */
1207
+	public function get_billing_form_html_for_payment_method()
1208
+	{
1209
+		// how have they chosen to pay?
1210
+		$this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
1211
+		$this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1212
+		if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1213
+			return false;
1214
+		}
1215
+		if (apply_filters(
1216
+			'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1217
+			false
1218
+		)) {
1219
+			EE_Error::add_success(
1220
+				apply_filters(
1221
+					'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1222
+					sprintf(
1223
+						esc_html__(
1224
+							'You have selected "%s" as your method of payment. Please note the important payment information below.',
1225
+							'event_espresso'
1226
+						),
1227
+						$this->checkout->payment_method->name()
1228
+					)
1229
+				)
1230
+			);
1231
+		}
1232
+		// now generate billing form for selected method of payment
1233
+		$payment_method_billing_form = $this->_get_billing_form_for_payment_method($this->checkout->payment_method);
1234
+		// fill form with attendee info if applicable
1235
+		if ($payment_method_billing_form instanceof EE_Billing_Attendee_Info_Form
1236
+			&& $this->checkout->transaction_has_primary_registrant()
1237
+		) {
1238
+			$payment_method_billing_form->populate_from_attendee(
1239
+				$this->checkout->transaction->primary_registration()->attendee()
1240
+			);
1241
+		}
1242
+		// and debug content
1243
+		if ($payment_method_billing_form instanceof EE_Billing_Info_Form
1244
+			&& $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base
1245
+		) {
1246
+			$payment_method_billing_form =
1247
+				$this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings(
1248
+					$payment_method_billing_form
1249
+				);
1250
+		}
1251
+		$billing_info = $payment_method_billing_form instanceof EE_Form_Section_Proper
1252
+			? $payment_method_billing_form->get_html()
1253
+			: '';
1254
+		$this->checkout->json_response->set_return_data(array('payment_method_info' => $billing_info));
1255
+		// localize validation rules for main form
1256
+		$this->checkout->current_step->reg_form->localize_validation_rules();
1257
+		$this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
1258
+		return true;
1259
+	}
1260
+
1261
+
1262
+	/**
1263
+	 * _get_billing_form_for_payment_method
1264
+	 *
1265
+	 * @access private
1266
+	 * @param EE_Payment_Method $payment_method
1267
+	 * @return EE_Billing_Info_Form|EE_Form_Section_HTML
1268
+	 * @throws EE_Error
1269
+	 * @throws InvalidArgumentException
1270
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1271
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1272
+	 */
1273
+	private function _get_billing_form_for_payment_method(EE_Payment_Method $payment_method)
1274
+	{
1275
+		$billing_form = $payment_method->type_obj()->billing_form(
1276
+			$this->checkout->transaction,
1277
+			array('amount_owing' => $this->checkout->amount_owing)
1278
+		);
1279
+		if ($billing_form instanceof EE_Billing_Info_Form) {
1280
+			if (apply_filters(
1281
+				'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1282
+				false
1283
+			)
1284
+				&& EE_Registry::instance()->REQ->is_set('payment_method')
1285
+			) {
1286
+				EE_Error::add_success(
1287
+					apply_filters(
1288
+						'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1289
+						sprintf(
1290
+							esc_html__(
1291
+								'You have selected "%s" as your method of payment. Please note the important payment information below.',
1292
+								'event_espresso'
1293
+							),
1294
+							$payment_method->name()
1295
+						)
1296
+					)
1297
+				);
1298
+			}
1299
+			return apply_filters(
1300
+				'FHEE__EE_SPCO_Reg_Step_Payment_Options___get_billing_form_for_payment_method__billing_form',
1301
+				$billing_form,
1302
+				$payment_method
1303
+			);
1304
+		}
1305
+		// no actual billing form, so return empty HTML form section
1306
+		return new EE_Form_Section_HTML();
1307
+	}
1308
+
1309
+
1310
+	/**
1311
+	 * _get_selected_method_of_payment
1312
+	 *
1313
+	 * @access private
1314
+	 * @param boolean $required whether to throw an error if the "selected_method_of_payment"
1315
+	 *                          is not found in the incoming request
1316
+	 * @param string  $request_param
1317
+	 * @return NULL|string
1318
+	 * @throws EE_Error
1319
+	 * @throws InvalidArgumentException
1320
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1321
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1322
+	 */
1323
+	private function _get_selected_method_of_payment(
1324
+		$required = false,
1325
+		$request_param = 'selected_method_of_payment'
1326
+	) {
1327
+		// is selected_method_of_payment set in the request ?
1328
+		$selected_method_of_payment = EE_Registry::instance()->REQ->get($request_param, false);
1329
+		if ($selected_method_of_payment) {
1330
+			// sanitize it
1331
+			$selected_method_of_payment = is_array($selected_method_of_payment)
1332
+				? array_shift($selected_method_of_payment)
1333
+				: $selected_method_of_payment;
1334
+			$selected_method_of_payment = sanitize_text_field($selected_method_of_payment);
1335
+			// store it in the session so that it's available for all subsequent requests including AJAX
1336
+			$this->_save_selected_method_of_payment($selected_method_of_payment);
1337
+		} else {
1338
+			// or is is set in the session ?
1339
+			$selected_method_of_payment = EE_Registry::instance()->SSN->get_session_data(
1340
+				'selected_method_of_payment'
1341
+			);
1342
+		}
1343
+		// do ya really really gotta have it?
1344
+		if (empty($selected_method_of_payment) && $required) {
1345
+			EE_Error::add_error(
1346
+				sprintf(
1347
+					esc_html__(
1348
+						'The selected method of payment could not be determined.%sPlease ensure that you have selected one before proceeding.%sIf you continue to experience difficulties, then refresh your browser and try again, or contact %s for assistance.',
1349
+						'event_espresso'
1350
+					),
1351
+					'<br/>',
1352
+					'<br/>',
1353
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
1354
+				),
1355
+				__FILE__,
1356
+				__FUNCTION__,
1357
+				__LINE__
1358
+			);
1359
+			return null;
1360
+		}
1361
+		return $selected_method_of_payment;
1362
+	}
1363
+
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+	/********************************************************************************************************/
1370
+	/***********************************  SWITCH PAYMENT METHOD  ************************************/
1371
+	/********************************************************************************************************/
1372
+	/**
1373
+	 * switch_payment_method
1374
+	 *
1375
+	 * @access public
1376
+	 * @return string
1377
+	 * @throws EE_Error
1378
+	 * @throws InvalidArgumentException
1379
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1380
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1381
+	 */
1382
+	public function switch_payment_method()
1383
+	{
1384
+		if (! $this->_verify_payment_method_is_set()) {
1385
+			return false;
1386
+		}
1387
+		if (apply_filters(
1388
+			'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success',
1389
+			false
1390
+		)) {
1391
+			EE_Error::add_success(
1392
+				apply_filters(
1393
+					'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method',
1394
+					sprintf(
1395
+						esc_html__(
1396
+							'You have selected "%s" as your method of payment. Please note the important payment information below.',
1397
+							'event_espresso'
1398
+						),
1399
+						$this->checkout->payment_method->name()
1400
+					)
1401
+				)
1402
+			);
1403
+		}
1404
+		// generate billing form for selected method of payment if it hasn't been done already
1405
+		if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1406
+			$this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1407
+				$this->checkout->payment_method
1408
+			);
1409
+		}
1410
+		// fill form with attendee info if applicable
1411
+		if (apply_filters(
1412
+			'FHEE__populate_billing_form_fields_from_attendee',
1413
+			(
1414
+				$this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form
1415
+				&& $this->checkout->transaction_has_primary_registrant()
1416
+			),
1417
+			$this->checkout->billing_form,
1418
+			$this->checkout->transaction
1419
+		)
1420
+		) {
1421
+			$this->checkout->billing_form->populate_from_attendee(
1422
+				$this->checkout->transaction->primary_registration()->attendee()
1423
+			);
1424
+		}
1425
+		// and debug content
1426
+		if ($this->checkout->billing_form instanceof EE_Billing_Info_Form
1427
+			&& $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base
1428
+		) {
1429
+			$this->checkout->billing_form =
1430
+				$this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings(
1431
+					$this->checkout->billing_form
1432
+				);
1433
+		}
1434
+		// get html and validation rules for form
1435
+		if ($this->checkout->billing_form instanceof EE_Form_Section_Proper) {
1436
+			$this->checkout->json_response->set_return_data(
1437
+				array('payment_method_info' => $this->checkout->billing_form->get_html())
1438
+			);
1439
+			// localize validation rules for main form
1440
+			$this->checkout->billing_form->localize_validation_rules(true);
1441
+			$this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
1442
+		} else {
1443
+			$this->checkout->json_response->set_return_data(array('payment_method_info' => ''));
1444
+		}
1445
+		// prevents advancement to next step
1446
+		$this->checkout->continue_reg = false;
1447
+		return true;
1448
+	}
1449
+
1450
+
1451
+	/**
1452
+	 * _verify_payment_method_is_set
1453
+	 *
1454
+	 * @return bool
1455
+	 * @throws EE_Error
1456
+	 * @throws InvalidArgumentException
1457
+	 * @throws ReflectionException
1458
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1459
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1460
+	 */
1461
+	protected function _verify_payment_method_is_set()
1462
+	{
1463
+		// generate billing form for selected method of payment if it hasn't been done already
1464
+		if (empty($this->checkout->selected_method_of_payment)) {
1465
+			// how have they chosen to pay?
1466
+			$this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
1467
+		} else {
1468
+			// choose your own adventure based on method_of_payment
1469
+			switch ($this->checkout->selected_method_of_payment) {
1470
+				case 'events_sold_out':
1471
+					EE_Error::add_attention(
1472
+						apply_filters(
1473
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__sold_out_events_msg',
1474
+							esc_html__(
1475
+								'It appears that the event you were about to make a payment for has sold out since this form first loaded. Please contact the event administrator if you believe this is an error.',
1476
+								'event_espresso'
1477
+							)
1478
+						),
1479
+						__FILE__,
1480
+						__FUNCTION__,
1481
+						__LINE__
1482
+					);
1483
+					return false;
1484
+					break;
1485
+				case 'payments_closed':
1486
+					EE_Error::add_attention(
1487
+						apply_filters(
1488
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__payments_closed_msg',
1489
+							esc_html__(
1490
+								'It appears that the event you were about to make a payment for is not accepting payments at this time. Please contact the event administrator if you believe this is an error.',
1491
+								'event_espresso'
1492
+							)
1493
+						),
1494
+						__FILE__,
1495
+						__FUNCTION__,
1496
+						__LINE__
1497
+					);
1498
+					return false;
1499
+					break;
1500
+				case 'no_payment_required':
1501
+					EE_Error::add_attention(
1502
+						apply_filters(
1503
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__no_payment_required_msg',
1504
+							esc_html__(
1505
+								'It appears that the event you were about to make a payment for does not require payment. Please contact the event administrator if you believe this is an error.',
1506
+								'event_espresso'
1507
+							)
1508
+						),
1509
+						__FILE__,
1510
+						__FUNCTION__,
1511
+						__LINE__
1512
+					);
1513
+					return false;
1514
+					break;
1515
+				default:
1516
+			}
1517
+		}
1518
+		// verify payment method
1519
+		if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1520
+			// get payment method for selected method of payment
1521
+			$this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1522
+		}
1523
+		return $this->checkout->payment_method instanceof EE_Payment_Method ? true : false;
1524
+	}
1525
+
1526
+
1527
+
1528
+	/********************************************************************************************************/
1529
+	/***************************************  SAVE PAYER DETAILS  ****************************************/
1530
+	/********************************************************************************************************/
1531
+	/**
1532
+	 * save_payer_details_via_ajax
1533
+	 *
1534
+	 * @return void
1535
+	 * @throws EE_Error
1536
+	 * @throws InvalidArgumentException
1537
+	 * @throws ReflectionException
1538
+	 * @throws RuntimeException
1539
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1540
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1541
+	 */
1542
+	public function save_payer_details_via_ajax()
1543
+	{
1544
+		if (! $this->_verify_payment_method_is_set()) {
1545
+			return;
1546
+		}
1547
+		// generate billing form for selected method of payment if it hasn't been done already
1548
+		if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1549
+			$this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1550
+				$this->checkout->payment_method
1551
+			);
1552
+		}
1553
+		// generate primary attendee from payer info if applicable
1554
+		if (! $this->checkout->transaction_has_primary_registrant()) {
1555
+			$attendee = $this->_create_attendee_from_request_data();
1556
+			if ($attendee instanceof EE_Attendee) {
1557
+				foreach ($this->checkout->transaction->registrations() as $registration) {
1558
+					if ($registration->is_primary_registrant()) {
1559
+						$this->checkout->primary_attendee_obj = $attendee;
1560
+						$registration->_add_relation_to($attendee, 'Attendee');
1561
+						$registration->set_attendee_id($attendee->ID());
1562
+						$registration->update_cache_after_object_save('Attendee', $attendee);
1563
+					}
1564
+				}
1565
+			}
1566
+		}
1567
+	}
1568
+
1569
+
1570
+	/**
1571
+	 * create_attendee_from_request_data
1572
+	 * uses info from alternate GET or POST data (such as AJAX) to create a new attendee
1573
+	 *
1574
+	 * @return EE_Attendee
1575
+	 * @throws EE_Error
1576
+	 * @throws InvalidArgumentException
1577
+	 * @throws ReflectionException
1578
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1579
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1580
+	 */
1581
+	protected function _create_attendee_from_request_data()
1582
+	{
1583
+		// get State ID
1584
+		$STA_ID = ! empty($_REQUEST['state']) ? sanitize_text_field($_REQUEST['state']) : '';
1585
+		if (! empty($STA_ID)) {
1586
+			// can we get state object from name ?
1587
+			EE_Registry::instance()->load_model('State');
1588
+			$state = EEM_State::instance()->get_col(array(array('STA_name' => $STA_ID), 'limit' => 1), 'STA_ID');
1589
+			$STA_ID = is_array($state) && ! empty($state) ? reset($state) : $STA_ID;
1590
+		}
1591
+		// get Country ISO
1592
+		$CNT_ISO = ! empty($_REQUEST['country']) ? sanitize_text_field($_REQUEST['country']) : '';
1593
+		if (! empty($CNT_ISO)) {
1594
+			// can we get country object from name ?
1595
+			EE_Registry::instance()->load_model('Country');
1596
+			$country = EEM_Country::instance()->get_col(
1597
+				array(array('CNT_name' => $CNT_ISO), 'limit' => 1),
1598
+				'CNT_ISO'
1599
+			);
1600
+			$CNT_ISO = is_array($country) && ! empty($country) ? reset($country) : $CNT_ISO;
1601
+		}
1602
+		// grab attendee data
1603
+		$attendee_data = array(
1604
+			'ATT_fname'    => ! empty($_REQUEST['first_name']) ? sanitize_text_field($_REQUEST['first_name']) : '',
1605
+			'ATT_lname'    => ! empty($_REQUEST['last_name']) ? sanitize_text_field($_REQUEST['last_name']) : '',
1606
+			'ATT_email'    => ! empty($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '',
1607
+			'ATT_address'  => ! empty($_REQUEST['address']) ? sanitize_text_field($_REQUEST['address']) : '',
1608
+			'ATT_address2' => ! empty($_REQUEST['address2']) ? sanitize_text_field($_REQUEST['address2']) : '',
1609
+			'ATT_city'     => ! empty($_REQUEST['city']) ? sanitize_text_field($_REQUEST['city']) : '',
1610
+			'STA_ID'       => $STA_ID,
1611
+			'CNT_ISO'      => $CNT_ISO,
1612
+			'ATT_zip'      => ! empty($_REQUEST['zip']) ? sanitize_text_field($_REQUEST['zip']) : '',
1613
+			'ATT_phone'    => ! empty($_REQUEST['phone']) ? sanitize_text_field($_REQUEST['phone']) : '',
1614
+		);
1615
+		// validate the email address since it is the most important piece of info
1616
+		if (empty($attendee_data['ATT_email']) || $attendee_data['ATT_email'] !== $_REQUEST['email']) {
1617
+			EE_Error::add_error(
1618
+				esc_html__('An invalid email address was submitted.', 'event_espresso'),
1619
+				__FILE__,
1620
+				__FUNCTION__,
1621
+				__LINE__
1622
+			);
1623
+		}
1624
+		// does this attendee already exist in the db ? we're searching using a combination of first name, last name,
1625
+		// AND email address
1626
+		if (! empty($attendee_data['ATT_fname'])
1627
+			&& ! empty($attendee_data['ATT_lname'])
1628
+			&& ! empty($attendee_data['ATT_email'])
1629
+		) {
1630
+			$existing_attendee = EE_Registry::instance()->LIB->EEM_Attendee->find_existing_attendee(
1631
+				array(
1632
+					'ATT_fname' => $attendee_data['ATT_fname'],
1633
+					'ATT_lname' => $attendee_data['ATT_lname'],
1634
+					'ATT_email' => $attendee_data['ATT_email'],
1635
+				)
1636
+			);
1637
+			if ($existing_attendee instanceof EE_Attendee) {
1638
+				return $existing_attendee;
1639
+			}
1640
+		}
1641
+		// no existing attendee? kk let's create a new one
1642
+		// kinda lame, but we need a first and last name to create an attendee, so use the email address if those
1643
+		// don't exist
1644
+		$attendee_data['ATT_fname'] = ! empty($attendee_data['ATT_fname'])
1645
+			? $attendee_data['ATT_fname']
1646
+			: $attendee_data['ATT_email'];
1647
+		$attendee_data['ATT_lname'] = ! empty($attendee_data['ATT_lname'])
1648
+			? $attendee_data['ATT_lname']
1649
+			: $attendee_data['ATT_email'];
1650
+		return EE_Attendee::new_instance($attendee_data);
1651
+	}
1652
+
1653
+
1654
+
1655
+	/********************************************************************************************************/
1656
+	/****************************************  PROCESS REG STEP  *****************************************/
1657
+	/********************************************************************************************************/
1658
+	/**
1659
+	 * process_reg_step
1660
+	 *
1661
+	 * @return bool
1662
+	 * @throws EE_Error
1663
+	 * @throws InvalidArgumentException
1664
+	 * @throws ReflectionException
1665
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
1666
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1667
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1668
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
1669
+	 */
1670
+	public function process_reg_step()
1671
+	{
1672
+		// how have they chosen to pay?
1673
+		$this->checkout->selected_method_of_payment = $this->checkout->transaction->is_free()
1674
+			? 'no_payment_required'
1675
+			: $this->_get_selected_method_of_payment(true);
1676
+		// choose your own adventure based on method_of_payment
1677
+		switch ($this->checkout->selected_method_of_payment) {
1678
+			case 'events_sold_out':
1679
+				$this->checkout->redirect = true;
1680
+				$this->checkout->redirect_url = $this->checkout->cancel_page_url;
1681
+				$this->checkout->json_response->set_redirect_url($this->checkout->redirect_url);
1682
+				// mark this reg step as completed
1683
+				$this->set_completed();
1684
+				return false;
1685
+				break;
1686
+
1687
+			case 'payments_closed':
1688
+				if (apply_filters(
1689
+					'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__payments_closed__display_success',
1690
+					false
1691
+				)) {
1692
+					EE_Error::add_success(
1693
+						esc_html__('no payment required at this time.', 'event_espresso'),
1694
+						__FILE__,
1695
+						__FUNCTION__,
1696
+						__LINE__
1697
+					);
1698
+				}
1699
+				// mark this reg step as completed
1700
+				$this->set_completed();
1701
+				return true;
1702
+				break;
1703
+
1704
+			case 'no_payment_required':
1705
+				if (apply_filters(
1706
+					'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__no_payment_required__display_success',
1707
+					false
1708
+				)) {
1709
+					EE_Error::add_success(
1710
+						esc_html__('no payment required.', 'event_espresso'),
1711
+						__FILE__,
1712
+						__FUNCTION__,
1713
+						__LINE__
1714
+					);
1715
+				}
1716
+				// mark this reg step as completed
1717
+				$this->set_completed();
1718
+				return true;
1719
+				break;
1720
+
1721
+			default:
1722
+				$registrations = EE_Registry::instance()->SSN->checkout()->transaction->registrations(
1723
+					EE_Registry::instance()->SSN->checkout()->reg_cache_where_params
1724
+				);
1725
+				$ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space(
1726
+					$registrations,
1727
+					EE_Registry::instance()->SSN->checkout()->revisit
1728
+				);
1729
+				// calculate difference between the two arrays
1730
+				$registrations = array_diff($registrations, $ejected_registrations);
1731
+				if (empty($registrations)) {
1732
+					$this->_redirect_because_event_sold_out();
1733
+					return false;
1734
+				}
1735
+				$payment = $this->_process_payment();
1736
+				if ($payment instanceof EE_Payment) {
1737
+					$this->checkout->continue_reg = true;
1738
+					$this->_maybe_set_completed($payment);
1739
+				} else {
1740
+					$this->checkout->continue_reg = false;
1741
+				}
1742
+				return $payment instanceof EE_Payment;
1743
+		}
1744
+	}
1745
+
1746
+
1747
+	/**
1748
+	 * _redirect_because_event_sold_out
1749
+	 *
1750
+	 * @access protected
1751
+	 * @return void
1752
+	 */
1753
+	protected function _redirect_because_event_sold_out()
1754
+	{
1755
+		$this->checkout->continue_reg = false;
1756
+		// set redirect URL
1757
+		$this->checkout->redirect_url = add_query_arg(
1758
+			array('e_reg_url_link' => $this->checkout->reg_url_link),
1759
+			$this->checkout->current_step->reg_step_url()
1760
+		);
1761
+		$this->checkout->json_response->set_redirect_url($this->checkout->redirect_url);
1762
+	}
1763
+
1764
+
1765
+	/**
1766
+	 * _maybe_set_completed
1767
+	 *
1768
+	 * @access protected
1769
+	 * @param \EE_Payment_Method $payment_method
1770
+	 * @return void
1771
+	 * @throws \EE_Error
1772
+	 */
1773
+	protected function _maybe_set_completed(EE_Payment $payment)
1774
+	{
1775
+		// Do we need to redirect them? If so, there's more work to be done.
1776
+		if (! $payment->redirect_url()){
1777
+			$this->set_completed();
1778
+		}
1779
+	}
1780
+
1781
+
1782
+	/**
1783
+	 *    update_reg_step
1784
+	 *    this is the final step after a user  revisits the site to retry a payment
1785
+	 *
1786
+	 * @return bool
1787
+	 * @throws EE_Error
1788
+	 * @throws InvalidArgumentException
1789
+	 * @throws ReflectionException
1790
+	 * @throws \EventEspresso\core\exceptions\EntityNotFoundException
1791
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1792
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1793
+	 * @throws \EventEspresso\core\exceptions\InvalidStatusException
1794
+	 */
1795
+	public function update_reg_step()
1796
+	{
1797
+		$success = true;
1798
+		// if payment required
1799
+		if ($this->checkout->transaction->total() > 0) {
1800
+			do_action(
1801
+				'AHEE__EE_Single_Page_Checkout__process_finalize_registration__before_gateway',
1802
+				$this->checkout->transaction
1803
+			);
1804
+			// attempt payment via payment method
1805
+			$success = $this->process_reg_step();
1806
+		}
1807
+		if ($success && ! $this->checkout->redirect) {
1808
+			$this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn(
1809
+				$this->checkout->transaction->ID()
1810
+			);
1811
+			// set return URL
1812
+			$this->checkout->redirect_url = add_query_arg(
1813
+				array('e_reg_url_link' => $this->checkout->reg_url_link),
1814
+				$this->checkout->thank_you_page_url
1815
+			);
1816
+		}
1817
+		return $success;
1818
+	}
1819
+
1820
+
1821
+	/**
1822
+	 *    _process_payment
1823
+	 *
1824
+	 * @access private
1825
+	 * @return EE_Payment|null
1826
+	 * @throws EE_Error
1827
+	 * @throws InvalidArgumentException
1828
+	 * @throws ReflectionException
1829
+	 * @throws RuntimeException
1830
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1831
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1832
+	 */
1833
+	private function _process_payment()
1834
+	{
1835
+		// basically confirm that the event hasn't sold out since they hit the page
1836
+		if (! $this->_last_second_ticket_verifications()) {
1837
+			return null;
1838
+		}
1839
+		// ya gotta make a choice man
1840
+		if (empty($this->checkout->selected_method_of_payment)) {
1841
+			$this->checkout->json_response->set_plz_select_method_of_payment(
1842
+				esc_html__('Please select a method of payment before proceeding.', 'event_espresso')
1843
+			);
1844
+			return null;
1845
+		}
1846
+		// get EE_Payment_Method object
1847
+		if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
1848
+			return null;
1849
+		}
1850
+		// setup billing form
1851
+		if ($this->checkout->payment_method->type_obj()->has_billing_form()) {
1852
+			$this->checkout->billing_form = $this->_get_billing_form_for_payment_method(
1853
+				$this->checkout->payment_method
1854
+			);
1855
+			// bad billing form ?
1856
+			if (! $this->_billing_form_is_valid()) {
1857
+				return null;
1858
+			}
1859
+		}
1860
+		// ensure primary registrant has been fully processed
1861
+		if (! $this->_setup_primary_registrant_prior_to_payment()) {
1862
+			return null;
1863
+		}
1864
+		// if session is close to expiring (under 10 minutes by default)
1865
+		if ((time() - EE_Registry::instance()->SSN->expiration()) < EE_Registry::instance()->SSN->extension()) {
1866
+			// add some time to session expiration so that payment can be completed
1867
+			EE_Registry::instance()->SSN->extend_expiration();
1868
+		}
1869
+		/** @type EE_Transaction_Processor $transaction_processor */
1870
+		// $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' );
1871
+		// in case a registrant leaves to an Off-Site Gateway and never returns, we want to approve any registrations
1872
+		// for events with a default reg status of Approved
1873
+		// $transaction_processor->toggle_registration_statuses_for_default_approved_events(
1874
+		//      $this->checkout->transaction, $this->checkout->reg_cache_where_params
1875
+		// );
1876
+		// attempt payment
1877
+		$payment = $this->_attempt_payment($this->checkout->payment_method);
1878
+		// process results
1879
+		$payment = $this->_validate_payment($payment);
1880
+		$payment = $this->_post_payment_processing($payment);
1881
+		// verify payment
1882
+		if ($payment instanceof EE_Payment) {
1883
+			// store that for later
1884
+			$this->checkout->payment = $payment;
1885
+			// we can also consider the TXN to not have been failed, so temporarily upgrade it's status to abandoned
1886
+			$this->checkout->transaction->toggle_failed_transaction_status();
1887
+			$payment_status = $payment->status();
1888
+			if ($payment_status === EEM_Payment::status_id_approved
1889
+				|| $payment_status === EEM_Payment::status_id_pending
1890
+			) {
1891
+				return $payment;
1892
+			} else {
1893
+				return null;
1894
+			}
1895
+		} elseif ($payment === true) {
1896
+			// please note that offline payment methods will NOT make a payment,
1897
+			// but instead just mark themselves as the PMD_ID on the transaction, and return true
1898
+			$this->checkout->payment = $payment;
1899
+			return $payment;
1900
+		}
1901
+		// where's my money?
1902
+		return null;
1903
+	}
1904
+
1905
+
1906
+	/**
1907
+	 * _last_second_ticket_verifications
1908
+	 *
1909
+	 * @access public
1910
+	 * @return bool
1911
+	 * @throws EE_Error
1912
+	 */
1913
+	protected function _last_second_ticket_verifications()
1914
+	{
1915
+		// don't bother re-validating if not a return visit
1916
+		if (! $this->checkout->revisit) {
1917
+			return true;
1918
+		}
1919
+		$registrations = $this->checkout->transaction->registrations();
1920
+		if (empty($registrations)) {
1921
+			return false;
1922
+		}
1923
+		foreach ($registrations as $registration) {
1924
+			if ($registration instanceof EE_Registration && ! $registration->is_approved()) {
1925
+				$event = $registration->event_obj();
1926
+				if ($event instanceof EE_Event && $event->is_sold_out(true)) {
1927
+					EE_Error::add_error(
1928
+						apply_filters(
1929
+							'FHEE__EE_SPCO_Reg_Step_Payment_Options___last_second_ticket_verifications__sold_out_events_msg',
1930
+							sprintf(
1931
+								esc_html__(
1932
+									'It appears that the %1$s event that you were about to make a payment for has sold out since you first registered and/or arrived at this page. Please refresh the page and try again. If you have already made a partial payment towards this event, please contact the event administrator for a refund.',
1933
+									'event_espresso'
1934
+								),
1935
+								$event->name()
1936
+							)
1937
+						),
1938
+						__FILE__,
1939
+						__FUNCTION__,
1940
+						__LINE__
1941
+					);
1942
+					return false;
1943
+				}
1944
+			}
1945
+		}
1946
+		return true;
1947
+	}
1948
+
1949
+
1950
+	/**
1951
+	 * redirect_form
1952
+	 *
1953
+	 * @access public
1954
+	 * @return bool
1955
+	 * @throws EE_Error
1956
+	 * @throws InvalidArgumentException
1957
+	 * @throws ReflectionException
1958
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
1959
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
1960
+	 */
1961
+	public function redirect_form()
1962
+	{
1963
+		$payment_method_billing_info = $this->_payment_method_billing_info(
1964
+			$this->_get_payment_method_for_selected_method_of_payment()
1965
+		);
1966
+		$html = $payment_method_billing_info->get_html();
1967
+		$html .= $this->checkout->redirect_form;
1968
+		EE_Registry::instance()->REQ->add_output($html);
1969
+		return true;
1970
+	}
1971
+
1972
+
1973
+	/**
1974
+	 * _billing_form_is_valid
1975
+	 *
1976
+	 * @access private
1977
+	 * @return bool
1978
+	 * @throws \EE_Error
1979
+	 */
1980
+	private function _billing_form_is_valid()
1981
+	{
1982
+		if (! $this->checkout->payment_method->type_obj()->has_billing_form()) {
1983
+			return true;
1984
+		}
1985
+		if ($this->checkout->billing_form instanceof EE_Billing_Info_Form) {
1986
+			if ($this->checkout->billing_form->was_submitted()) {
1987
+				$this->checkout->billing_form->receive_form_submission();
1988
+				if ($this->checkout->billing_form->is_valid()) {
1989
+					return true;
1990
+				}
1991
+				$validation_errors = $this->checkout->billing_form->get_validation_errors_accumulated();
1992
+				$error_strings = array();
1993
+				foreach ($validation_errors as $validation_error) {
1994
+					if ($validation_error instanceof EE_Validation_Error) {
1995
+						$form_section = $validation_error->get_form_section();
1996
+						if ($form_section instanceof EE_Form_Input_Base) {
1997
+							$label = $form_section->html_label_text();
1998
+						} elseif ($form_section instanceof EE_Form_Section_Base) {
1999
+							$label = $form_section->name();
2000
+						} else {
2001
+							$label = esc_html__('Validation Error', 'event_espresso');
2002
+						}
2003
+						$error_strings[] = sprintf('%1$s: %2$s', $label, $validation_error->getMessage());
2004
+					}
2005
+				}
2006
+				EE_Error::add_error(
2007
+					sprintf(
2008
+						esc_html__(
2009
+							'One or more billing form inputs are invalid and require correction before proceeding. %1$s %2$s',
2010
+							'event_espresso'
2011
+						),
2012
+						'<br/>',
2013
+						implode('<br/>', $error_strings)
2014
+					),
2015
+					__FILE__,
2016
+					__FUNCTION__,
2017
+					__LINE__
2018
+				);
2019
+			} else {
2020
+				EE_Error::add_error(
2021
+					esc_html__(
2022
+						'The billing form was not submitted or something prevented it\'s submission.',
2023
+						'event_espresso'
2024
+					),
2025
+					__FILE__,
2026
+					__FUNCTION__,
2027
+					__LINE__
2028
+				);
2029
+			}
2030
+		} else {
2031
+			EE_Error::add_error(
2032
+				esc_html__(
2033
+					'The submitted billing form is invalid possibly due to a technical reason.',
2034
+					'event_espresso'
2035
+				),
2036
+				__FILE__,
2037
+				__FUNCTION__,
2038
+				__LINE__
2039
+			);
2040
+		}
2041
+		return false;
2042
+	}
2043
+
2044
+
2045
+	/**
2046
+	 * _setup_primary_registrant_prior_to_payment
2047
+	 * ensures that the primary registrant has a valid attendee object created with the critical details populated
2048
+	 * (first & last name & email) and that both the transaction object and primary registration object have been saved
2049
+	 * plz note that any other registrations will NOT be saved at this point (because they may not have any details
2050
+	 * yet)
2051
+	 *
2052
+	 * @access private
2053
+	 * @return bool
2054
+	 * @throws EE_Error
2055
+	 * @throws InvalidArgumentException
2056
+	 * @throws ReflectionException
2057
+	 * @throws RuntimeException
2058
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2059
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2060
+	 */
2061
+	private function _setup_primary_registrant_prior_to_payment()
2062
+	{
2063
+		// check if transaction has a primary registrant and that it has a related Attendee object
2064
+		// if not, then we need to at least gather some primary registrant data before attempting payment
2065
+		if ($this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form
2066
+			&& ! $this->checkout->transaction_has_primary_registrant()
2067
+			&& ! $this->_capture_primary_registration_data_from_billing_form()
2068
+		) {
2069
+			return false;
2070
+		}
2071
+		// because saving an object clears it's cache, we need to do the chevy shuffle
2072
+		// grab the primary_registration object
2073
+		$primary_registration = $this->checkout->transaction->primary_registration();
2074
+		// at this point we'll consider a TXN to not have been failed
2075
+		$this->checkout->transaction->toggle_failed_transaction_status();
2076
+		// save the TXN ( which clears cached copy of primary_registration)
2077
+		$this->checkout->transaction->save();
2078
+		// grab TXN ID and save it to the primary_registration
2079
+		$primary_registration->set_transaction_id($this->checkout->transaction->ID());
2080
+		// save what we have so far
2081
+		$primary_registration->save();
2082
+		return true;
2083
+	}
2084
+
2085
+
2086
+	/**
2087
+	 * _capture_primary_registration_data_from_billing_form
2088
+	 *
2089
+	 * @access private
2090
+	 * @return bool
2091
+	 * @throws EE_Error
2092
+	 * @throws InvalidArgumentException
2093
+	 * @throws ReflectionException
2094
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2095
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2096
+	 */
2097
+	private function _capture_primary_registration_data_from_billing_form()
2098
+	{
2099
+		// convert billing form data into an attendee
2100
+		$this->checkout->primary_attendee_obj = $this->checkout->billing_form->create_attendee_from_billing_form_data();
2101
+		if (! $this->checkout->primary_attendee_obj instanceof EE_Attendee) {
2102
+			EE_Error::add_error(
2103
+				sprintf(
2104
+					esc_html__(
2105
+						'The billing form details could not be used for attendee details due to a technical issue.%sPlease try again or contact %s for assistance.',
2106
+						'event_espresso'
2107
+					),
2108
+					'<br/>',
2109
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2110
+				),
2111
+				__FILE__,
2112
+				__FUNCTION__,
2113
+				__LINE__
2114
+			);
2115
+			return false;
2116
+		}
2117
+		$primary_registration = $this->checkout->transaction->primary_registration();
2118
+		if (! $primary_registration instanceof EE_Registration) {
2119
+			EE_Error::add_error(
2120
+				sprintf(
2121
+					esc_html__(
2122
+						'The primary registrant for this transaction could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2123
+						'event_espresso'
2124
+					),
2125
+					'<br/>',
2126
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2127
+				),
2128
+				__FILE__,
2129
+				__FUNCTION__,
2130
+				__LINE__
2131
+			);
2132
+			return false;
2133
+		}
2134
+		if (! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee')
2135
+			  instanceof
2136
+			  EE_Attendee
2137
+		) {
2138
+			EE_Error::add_error(
2139
+				sprintf(
2140
+					esc_html__(
2141
+						'The primary registrant could not be associated with this transaction due to a technical issue.%sPlease try again or contact %s for assistance.',
2142
+						'event_espresso'
2143
+					),
2144
+					'<br/>',
2145
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2146
+				),
2147
+				__FILE__,
2148
+				__FUNCTION__,
2149
+				__LINE__
2150
+			);
2151
+			return false;
2152
+		}
2153
+		/** @type EE_Registration_Processor $registration_processor */
2154
+		$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
2155
+		// at this point, we should have enough details about the registrant to consider the registration NOT incomplete
2156
+		$registration_processor->toggle_incomplete_registration_status_to_default($primary_registration);
2157
+		return true;
2158
+	}
2159
+
2160
+
2161
+	/**
2162
+	 * _get_payment_method_for_selected_method_of_payment
2163
+	 * retrieves a valid payment method
2164
+	 *
2165
+	 * @access public
2166
+	 * @return EE_Payment_Method
2167
+	 * @throws EE_Error
2168
+	 * @throws InvalidArgumentException
2169
+	 * @throws ReflectionException
2170
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2171
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2172
+	 */
2173
+	private function _get_payment_method_for_selected_method_of_payment()
2174
+	{
2175
+		if ($this->checkout->selected_method_of_payment === 'events_sold_out') {
2176
+			$this->_redirect_because_event_sold_out();
2177
+			return null;
2178
+		}
2179
+		// get EE_Payment_Method object
2180
+		if (isset($this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ])) {
2181
+			$payment_method = $this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ];
2182
+		} else {
2183
+			// load EEM_Payment_Method
2184
+			EE_Registry::instance()->load_model('Payment_Method');
2185
+			/** @type EEM_Payment_Method $EEM_Payment_Method */
2186
+			$EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method;
2187
+			$payment_method = $EEM_Payment_Method->get_one_by_slug($this->checkout->selected_method_of_payment);
2188
+		}
2189
+		// verify $payment_method
2190
+		if (! $payment_method instanceof EE_Payment_Method) {
2191
+			// not a payment
2192
+			EE_Error::add_error(
2193
+				sprintf(
2194
+					esc_html__(
2195
+						'The selected method of payment could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2196
+						'event_espresso'
2197
+					),
2198
+					'<br/>',
2199
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2200
+				),
2201
+				__FILE__,
2202
+				__FUNCTION__,
2203
+				__LINE__
2204
+			);
2205
+			return null;
2206
+		}
2207
+		// and verify it has a valid Payment_Method Type object
2208
+		if (! $payment_method->type_obj() instanceof EE_PMT_Base) {
2209
+			// not a payment
2210
+			EE_Error::add_error(
2211
+				sprintf(
2212
+					esc_html__(
2213
+						'A valid payment method could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.',
2214
+						'event_espresso'
2215
+					),
2216
+					'<br/>',
2217
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2218
+				),
2219
+				__FILE__,
2220
+				__FUNCTION__,
2221
+				__LINE__
2222
+			);
2223
+			return null;
2224
+		}
2225
+		return $payment_method;
2226
+	}
2227
+
2228
+
2229
+	/**
2230
+	 *    _attempt_payment
2231
+	 *
2232
+	 * @access    private
2233
+	 * @type    EE_Payment_Method $payment_method
2234
+	 * @return mixed EE_Payment | boolean
2235
+	 * @throws EE_Error
2236
+	 * @throws InvalidArgumentException
2237
+	 * @throws ReflectionException
2238
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2239
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2240
+	 */
2241
+	private function _attempt_payment(EE_Payment_Method $payment_method)
2242
+	{
2243
+		$payment = null;
2244
+		$this->checkout->transaction->save();
2245
+		$payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2246
+		if (! $payment_processor instanceof EE_Payment_Processor) {
2247
+			return false;
2248
+		}
2249
+		try {
2250
+			$payment_processor->set_revisit($this->checkout->revisit);
2251
+			// generate payment object
2252
+			$payment = $payment_processor->process_payment(
2253
+				$payment_method,
2254
+				$this->checkout->transaction,
2255
+				$this->checkout->amount_owing,
2256
+				$this->checkout->billing_form,
2257
+				$this->_get_return_url($payment_method),
2258
+				'CART',
2259
+				$this->checkout->admin_request,
2260
+				true,
2261
+				$this->reg_step_url()
2262
+			);
2263
+		} catch (Exception $e) {
2264
+			$this->_handle_payment_processor_exception($e);
2265
+		}
2266
+		return $payment;
2267
+	}
2268
+
2269
+
2270
+	/**
2271
+	 * _handle_payment_processor_exception
2272
+	 *
2273
+	 * @access protected
2274
+	 * @param \Exception $e
2275
+	 * @return void
2276
+	 * @throws EE_Error
2277
+	 * @throws InvalidArgumentException
2278
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2279
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2280
+	 */
2281
+	protected function _handle_payment_processor_exception(Exception $e)
2282
+	{
2283
+		EE_Error::add_error(
2284
+			sprintf(
2285
+				esc_html__(
2286
+					'The payment could not br processed due to a technical issue.%1$sPlease try again or contact %2$s for assistance.||The following Exception was thrown in %4$s on line %5$s:%1$s%3$s',
2287
+					'event_espresso'
2288
+				),
2289
+				'<br/>',
2290
+				EE_Registry::instance()->CFG->organization->get_pretty('email'),
2291
+				$e->getMessage(),
2292
+				$e->getFile(),
2293
+				$e->getLine()
2294
+			),
2295
+			__FILE__,
2296
+			__FUNCTION__,
2297
+			__LINE__
2298
+		);
2299
+	}
2300
+
2301
+
2302
+	/**
2303
+	 * _get_return_url
2304
+	 *
2305
+	 * @access protected
2306
+	 * @param \EE_Payment_Method $payment_method
2307
+	 * @return string
2308
+	 * @throws \EE_Error
2309
+	 */
2310
+	protected function _get_return_url(EE_Payment_Method $payment_method)
2311
+	{
2312
+		$return_url = '';
2313
+		switch ($payment_method->type_obj()->payment_occurs()) {
2314
+			case EE_PMT_Base::offsite:
2315
+				$return_url = add_query_arg(
2316
+					array(
2317
+						'action'                     => 'process_gateway_response',
2318
+						'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2319
+						'spco_txn'                   => $this->checkout->transaction->ID(),
2320
+					),
2321
+					$this->reg_step_url()
2322
+				);
2323
+				break;
2324
+			case EE_PMT_Base::onsite:
2325
+			case EE_PMT_Base::offline:
2326
+				$return_url = $this->checkout->next_step->reg_step_url();
2327
+				break;
2328
+		}
2329
+		return $return_url;
2330
+	}
2331
+
2332
+
2333
+	/**
2334
+	 * _validate_payment
2335
+	 *
2336
+	 * @access private
2337
+	 * @param EE_Payment $payment
2338
+	 * @return EE_Payment|FALSE
2339
+	 * @throws EE_Error
2340
+	 * @throws InvalidArgumentException
2341
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2342
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2343
+	 */
2344
+	private function _validate_payment($payment = null)
2345
+	{
2346
+		if ($this->checkout->payment_method->is_off_line()) {
2347
+			return true;
2348
+		}
2349
+		// verify payment object
2350
+		if (! $payment instanceof EE_Payment) {
2351
+			// not a payment
2352
+			EE_Error::add_error(
2353
+				sprintf(
2354
+					esc_html__(
2355
+						'A valid payment was not generated due to a technical issue.%1$sPlease try again or contact %2$s for assistance.',
2356
+						'event_espresso'
2357
+					),
2358
+					'<br/>',
2359
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2360
+				),
2361
+				__FILE__,
2362
+				__FUNCTION__,
2363
+				__LINE__
2364
+			);
2365
+			return false;
2366
+		}
2367
+		return $payment;
2368
+	}
2369
+
2370
+
2371
+	/**
2372
+	 * _post_payment_processing
2373
+	 *
2374
+	 * @access private
2375
+	 * @param EE_Payment|bool $payment
2376
+	 * @return bool
2377
+	 * @throws EE_Error
2378
+	 * @throws InvalidArgumentException
2379
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2380
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2381
+	 */
2382
+	private function _post_payment_processing($payment = null)
2383
+	{
2384
+		// Off-Line payment?
2385
+		if ($payment === true) {
2386
+			return true;
2387
+		} elseif ($payment instanceof EE_Payment){
2388
+			// Should the user be redirected?
2389
+			if($payment->redirect_url()) {
2390
+				do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->redirect_url(), '$payment->redirect_url()');
2391
+				$this->checkout->redirect = true;
2392
+				$this->checkout->redirect_form = $payment->redirect_form();
2393
+				$this->checkout->redirect_url = $this->reg_step_url('redirect_form');
2394
+				// set JSON response
2395
+				$this->checkout->json_response->set_redirect_form($this->checkout->redirect_form);
2396
+				// and lastly, let's bump the payment status to pending
2397
+				$payment->set_status(EEM_Payment::status_id_pending);
2398
+				$payment->save();
2399
+			} else
2400
+				// User shouldn't be redirected. So let's process it here.
2401
+				if (! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) {
2402
+				// $this->_setup_redirect_for_next_step();
2403
+				$this->checkout->continue_reg = false;
2404
+			}
2405
+		} else {
2406
+			// ummm ya... not Off-Line, not On-Site, not off-Site ????
2407
+			$this->checkout->continue_reg = false;
2408
+			return false;
2409
+		}
2410
+		return $payment;
2411
+	}
2412
+
2413
+
2414
+	/**
2415
+	 *    _process_payment_status
2416
+	 *
2417
+	 * @access private
2418
+	 * @type    EE_Payment $payment
2419
+	 * @param string       $payment_occurs
2420
+	 * @return bool
2421
+	 * @throws EE_Error
2422
+	 * @throws InvalidArgumentException
2423
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2424
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2425
+	 */
2426
+	private function _process_payment_status($payment, $payment_occurs = EE_PMT_Base::offline)
2427
+	{
2428
+		// off-line payment? carry on
2429
+		if ($payment_occurs === EE_PMT_Base::offline) {
2430
+			return true;
2431
+		}
2432
+		// verify payment validity
2433
+		if ($payment instanceof EE_Payment) {
2434
+			do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->status(), '$payment->status()');
2435
+			$msg = $payment->gateway_response();
2436
+			// check results
2437
+			switch ($payment->status()) {
2438
+				// good payment
2439
+				case EEM_Payment::status_id_approved:
2440
+					EE_Error::add_success(
2441
+						esc_html__('Your payment was processed successfully.', 'event_espresso'),
2442
+						__FILE__,
2443
+						__FUNCTION__,
2444
+						__LINE__
2445
+					);
2446
+					return true;
2447
+					break;
2448
+				// slow payment
2449
+				case EEM_Payment::status_id_pending:
2450
+					if (empty($msg)) {
2451
+						$msg = esc_html__(
2452
+							'Your payment appears to have been processed successfully, but the Instant Payment Notification has not yet been received. It should arrive shortly.',
2453
+							'event_espresso'
2454
+						);
2455
+					}
2456
+					EE_Error::add_success($msg, __FILE__, __FUNCTION__, __LINE__);
2457
+					return true;
2458
+					break;
2459
+				// don't wanna payment
2460
+				case EEM_Payment::status_id_cancelled:
2461
+					if (empty($msg)) {
2462
+						$msg = _n(
2463
+							'Payment cancelled. Please try again.',
2464
+							'Payment cancelled. Please try again or select another method of payment.',
2465
+							count($this->checkout->available_payment_methods),
2466
+							'event_espresso'
2467
+						);
2468
+					}
2469
+					EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
2470
+					return false;
2471
+					break;
2472
+				// not enough payment
2473
+				case EEM_Payment::status_id_declined:
2474
+					if (empty($msg)) {
2475
+						$msg = _n(
2476
+							'We\'re sorry but your payment was declined. Please try again.',
2477
+							'We\'re sorry but your payment was declined. Please try again or select another method of payment.',
2478
+							count($this->checkout->available_payment_methods),
2479
+							'event_espresso'
2480
+						);
2481
+					}
2482
+					EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__);
2483
+					return false;
2484
+					break;
2485
+				// bad payment
2486
+				case EEM_Payment::status_id_failed:
2487
+					if (! empty($msg)) {
2488
+						EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2489
+						return false;
2490
+					}
2491
+					// default to error below
2492
+					break;
2493
+			}
2494
+		}
2495
+		// off-site payment gateway responses are too unreliable, so let's just assume that
2496
+		// the payment processing is just running slower than the registrant's request
2497
+		if ($payment_occurs === EE_PMT_Base::offsite) {
2498
+			return true;
2499
+		}
2500
+		EE_Error::add_error(
2501
+			sprintf(
2502
+				esc_html__(
2503
+					'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.',
2504
+					'event_espresso'
2505
+				),
2506
+				'<br/>',
2507
+				EE_Registry::instance()->CFG->organization->get_pretty('email')
2508
+			),
2509
+			__FILE__,
2510
+			__FUNCTION__,
2511
+			__LINE__
2512
+		);
2513
+		return false;
2514
+	}
2515
+
2516
+
2517
+
2518
+
2519
+
2520
+
2521
+	/********************************************************************************************************/
2522
+	/**********************************  PROCESS GATEWAY RESPONSE  **********************************/
2523
+	/********************************************************************************************************/
2524
+	/**
2525
+	 * process_gateway_response
2526
+	 * this is the return point for Off-Site Payment Methods
2527
+	 * It will attempt to "handle the IPN" if it appears that this has not already occurred,
2528
+	 * otherwise, it will load up the last payment made for the TXN.
2529
+	 * If the payment retrieved looks good, it will then either:
2530
+	 *    complete the current step and allow advancement to the next reg step
2531
+	 *        or present the payment options again
2532
+	 *
2533
+	 * @access private
2534
+	 * @return EE_Payment|FALSE
2535
+	 * @throws EE_Error
2536
+	 * @throws InvalidArgumentException
2537
+	 * @throws ReflectionException
2538
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2539
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2540
+	 * @throws \EventEspresso\core\exceptions\InvalidSessionDataException
2541
+	 */
2542
+	public function process_gateway_response()
2543
+	{
2544
+		$payment = null;
2545
+		// how have they chosen to pay?
2546
+		$this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
2547
+		// get EE_Payment_Method object
2548
+		if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
2549
+			$this->checkout->continue_reg = false;
2550
+			return false;
2551
+		}
2552
+		if (! $this->checkout->payment_method->is_off_site()) {
2553
+			return false;
2554
+		}
2555
+		$this->_validate_offsite_return();
2556
+		// DEBUG LOG
2557
+		// $this->checkout->log(
2558
+		//     __CLASS__,
2559
+		//     __FUNCTION__,
2560
+		//     __LINE__,
2561
+		//     array(
2562
+		//         'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2563
+		//         'payment_method'             => $this->checkout->payment_method,
2564
+		//     ),
2565
+		//     true
2566
+		// );
2567
+		// verify TXN
2568
+		if ($this->checkout->transaction instanceof EE_Transaction) {
2569
+			$gateway = $this->checkout->payment_method->type_obj()->get_gateway();
2570
+			if (! $gateway instanceof EE_Offsite_Gateway) {
2571
+				$this->checkout->continue_reg = false;
2572
+				return false;
2573
+			}
2574
+			$payment = $this->_process_off_site_payment($gateway);
2575
+			$payment = $this->_process_cancelled_payments($payment);
2576
+			$payment = $this->_validate_payment($payment);
2577
+			// if payment was not declined by the payment gateway or cancelled by the registrant
2578
+			if ($this->_process_payment_status($payment, EE_PMT_Base::offsite)) {
2579
+				// $this->_setup_redirect_for_next_step();
2580
+				// store that for later
2581
+				$this->checkout->payment = $payment;
2582
+				// mark this reg step as completed, as long as gateway doesn't use a separate IPN request,
2583
+				// because we will complete this step during the IPN processing then
2584
+				if ($gateway instanceof EE_Offsite_Gateway && ! $this->handle_IPN_in_this_request()) {
2585
+					$this->set_completed();
2586
+				}
2587
+				return true;
2588
+			}
2589
+		}
2590
+		// DEBUG LOG
2591
+		// $this->checkout->log(
2592
+		//     __CLASS__,
2593
+		//     __FUNCTION__,
2594
+		//     __LINE__,
2595
+		//     array('payment' => $payment)
2596
+		// );
2597
+		$this->checkout->continue_reg = false;
2598
+		return false;
2599
+	}
2600
+
2601
+
2602
+	/**
2603
+	 * _validate_return
2604
+	 *
2605
+	 * @access private
2606
+	 * @return void
2607
+	 * @throws EE_Error
2608
+	 * @throws InvalidArgumentException
2609
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2610
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2611
+	 * @throws \EventEspresso\core\exceptions\InvalidSessionDataException
2612
+	 */
2613
+	private function _validate_offsite_return()
2614
+	{
2615
+		$TXN_ID = (int) EE_Registry::instance()->REQ->get('spco_txn', 0);
2616
+		if ($TXN_ID !== $this->checkout->transaction->ID()) {
2617
+			// Houston... we might have a problem
2618
+			$invalid_TXN = false;
2619
+			// first gather some info
2620
+			$valid_TXN = EEM_Transaction::instance()->get_one_by_ID($TXN_ID);
2621
+			$primary_registrant = $valid_TXN instanceof EE_Transaction
2622
+				? $valid_TXN->primary_registration()
2623
+				: null;
2624
+			// let's start by retrieving the cart for this TXN
2625
+			$cart = $this->checkout->get_cart_for_transaction($this->checkout->transaction);
2626
+			if ($cart instanceof EE_Cart) {
2627
+				// verify that the current cart has tickets
2628
+				$tickets = $cart->get_tickets();
2629
+				if (empty($tickets)) {
2630
+					$invalid_TXN = true;
2631
+				}
2632
+			} else {
2633
+				$invalid_TXN = true;
2634
+			}
2635
+			$valid_TXN_SID = $primary_registrant instanceof EE_Registration
2636
+				? $primary_registrant->session_ID()
2637
+				: null;
2638
+			// validate current Session ID and compare against valid TXN session ID
2639
+			if ($invalid_TXN // if this is already true, then skip other checks
2640
+				|| EE_Session::instance()->id() === null
2641
+				|| (
2642
+					// WARNING !!!
2643
+					// this could be PayPal sending back duplicate requests (ya they do that)
2644
+					// or it **could** mean someone is simply registering AGAIN after having just done so
2645
+					// so now we need to determine if this current TXN looks valid or not
2646
+					// and whether this reg step has even been started ?
2647
+					EE_Session::instance()->id() === $valid_TXN_SID
2648
+					// really? you're half way through this reg step, but you never started it ?
2649
+					&& $this->checkout->transaction->reg_step_completed($this->slug()) === false
2650
+				)
2651
+			) {
2652
+				$invalid_TXN = true;
2653
+			}
2654
+			if ($invalid_TXN) {
2655
+				// is the valid TXN completed ?
2656
+				if ($valid_TXN instanceof EE_Transaction) {
2657
+					// has this step even been started ?
2658
+					$reg_step_completed = $valid_TXN->reg_step_completed($this->slug());
2659
+					if ($reg_step_completed !== false && $reg_step_completed !== true) {
2660
+						// so it **looks** like this is a double request from PayPal
2661
+						// so let's try to pick up where we left off
2662
+						$this->checkout->transaction = $valid_TXN;
2663
+						$this->checkout->refresh_all_entities(true);
2664
+						return;
2665
+					}
2666
+				}
2667
+				// you appear to be lost?
2668
+				$this->_redirect_wayward_request($primary_registrant);
2669
+			}
2670
+		}
2671
+	}
2672
+
2673
+
2674
+	/**
2675
+	 * _redirect_wayward_request
2676
+	 *
2677
+	 * @access private
2678
+	 * @param \EE_Registration|null $primary_registrant
2679
+	 * @return bool
2680
+	 * @throws EE_Error
2681
+	 * @throws InvalidArgumentException
2682
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2683
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2684
+	 */
2685
+	private function _redirect_wayward_request(EE_Registration $primary_registrant)
2686
+	{
2687
+		if (! $primary_registrant instanceof EE_Registration) {
2688
+			// try redirecting based on the current TXN
2689
+			$primary_registrant = $this->checkout->transaction instanceof EE_Transaction
2690
+				? $this->checkout->transaction->primary_registration()
2691
+				: null;
2692
+		}
2693
+		if (! $primary_registrant instanceof EE_Registration) {
2694
+			EE_Error::add_error(
2695
+				sprintf(
2696
+					esc_html__(
2697
+						'Invalid information was received from the Off-Site Payment Processor and your Transaction details could not be retrieved from the database.%1$sPlease try again or contact %2$s for assistance.',
2698
+						'event_espresso'
2699
+					),
2700
+					'<br/>',
2701
+					EE_Registry::instance()->CFG->organization->get_pretty('email')
2702
+				),
2703
+				__FILE__,
2704
+				__FUNCTION__,
2705
+				__LINE__
2706
+			);
2707
+			return false;
2708
+		}
2709
+		// make sure transaction is not locked
2710
+		$this->checkout->transaction->unlock();
2711
+		wp_safe_redirect(
2712
+			add_query_arg(
2713
+				array(
2714
+					'e_reg_url_link' => $primary_registrant->reg_url_link(),
2715
+				),
2716
+				$this->checkout->thank_you_page_url
2717
+			)
2718
+		);
2719
+		exit();
2720
+	}
2721
+
2722
+
2723
+	/**
2724
+	 * _process_off_site_payment
2725
+	 *
2726
+	 * @access private
2727
+	 * @param \EE_Offsite_Gateway $gateway
2728
+	 * @return EE_Payment
2729
+	 * @throws EE_Error
2730
+	 * @throws InvalidArgumentException
2731
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2732
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2733
+	 */
2734
+	private function _process_off_site_payment(EE_Offsite_Gateway $gateway)
2735
+	{
2736
+		try {
2737
+			$request_data = \EE_Registry::instance()->REQ->params();
2738
+			// if gateway uses_separate_IPN_request, then we don't have to process the IPN manually
2739
+			$this->set_handle_IPN_in_this_request(
2740
+				$gateway->handle_IPN_in_this_request($request_data, false)
2741
+			);
2742
+			if ($this->handle_IPN_in_this_request()) {
2743
+				// get payment details and process results
2744
+				/** @type EE_Payment_Processor $payment_processor */
2745
+				$payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2746
+				$payment = $payment_processor->process_ipn(
2747
+					$request_data,
2748
+					$this->checkout->transaction,
2749
+					$this->checkout->payment_method,
2750
+					true,
2751
+					false
2752
+				);
2753
+				// $payment_source = 'process_ipn';
2754
+			} else {
2755
+				$payment = $this->checkout->transaction->last_payment();
2756
+				// $payment_source = 'last_payment';
2757
+			}
2758
+		} catch (Exception $e) {
2759
+			// let's just eat the exception and try to move on using any previously set payment info
2760
+			$payment = $this->checkout->transaction->last_payment();
2761
+			// $payment_source = 'last_payment after Exception';
2762
+			// but if we STILL don't have a payment object
2763
+			if (! $payment instanceof EE_Payment) {
2764
+				// then we'll object ! ( not object like a thing... but object like what a lawyer says ! )
2765
+				$this->_handle_payment_processor_exception($e);
2766
+			}
2767
+		}
2768
+		// DEBUG LOG
2769
+		// $this->checkout->log(
2770
+		//     __CLASS__,
2771
+		//     __FUNCTION__,
2772
+		//     __LINE__,
2773
+		//     array(
2774
+		//         'process_ipn_payment' => $payment,
2775
+		//         'payment_source'      => $payment_source,
2776
+		//     )
2777
+		// );
2778
+		return $payment;
2779
+	}
2780
+
2781
+
2782
+	/**
2783
+	 * _process_cancelled_payments
2784
+	 * just makes sure that the payment status gets updated correctly
2785
+	 * so tha tan error isn't generated during payment validation
2786
+	 *
2787
+	 * @access private
2788
+	 * @param EE_Payment $payment
2789
+	 * @return EE_Payment | FALSE
2790
+	 * @throws \EE_Error
2791
+	 */
2792
+	private function _process_cancelled_payments($payment = null)
2793
+	{
2794
+		if ($payment instanceof EE_Payment
2795
+			&& isset($_REQUEST['ee_cancel_payment'])
2796
+			&& $payment->status() === EEM_Payment::status_id_failed
2797
+		) {
2798
+			$payment->set_status(EEM_Payment::status_id_cancelled);
2799
+		}
2800
+		return $payment;
2801
+	}
2802
+
2803
+
2804
+	/**
2805
+	 *    get_transaction_details_for_gateways
2806
+	 *
2807
+	 * @access    public
2808
+	 * @return int
2809
+	 * @throws EE_Error
2810
+	 * @throws InvalidArgumentException
2811
+	 * @throws ReflectionException
2812
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
2813
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
2814
+	 */
2815
+	public function get_transaction_details_for_gateways()
2816
+	{
2817
+		$txn_details = array();
2818
+		// ya gotta make a choice man
2819
+		if (empty($this->checkout->selected_method_of_payment)) {
2820
+			$txn_details = array(
2821
+				'error' => esc_html__('Please select a method of payment before proceeding.', 'event_espresso'),
2822
+			);
2823
+		}
2824
+		// get EE_Payment_Method object
2825
+		if (empty($txn_details)
2826
+			&&
2827
+			! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()
2828
+		) {
2829
+			$txn_details = array(
2830
+				'selected_method_of_payment' => $this->checkout->selected_method_of_payment,
2831
+				'error'                      => esc_html__(
2832
+					'A valid Payment Method could not be determined.',
2833
+					'event_espresso'
2834
+				),
2835
+			);
2836
+		}
2837
+		if (empty($txn_details) && $this->checkout->transaction instanceof EE_Transaction) {
2838
+			$return_url = $this->_get_return_url($this->checkout->payment_method);
2839
+			$txn_details = array(
2840
+				'TXN_ID'         => $this->checkout->transaction->ID(),
2841
+				'TXN_timestamp'  => $this->checkout->transaction->datetime(),
2842
+				'TXN_total'      => $this->checkout->transaction->total(),
2843
+				'TXN_paid'       => $this->checkout->transaction->paid(),
2844
+				'TXN_reg_steps'  => $this->checkout->transaction->reg_steps(),
2845
+				'STS_ID'         => $this->checkout->transaction->status_ID(),
2846
+				'PMD_ID'         => $this->checkout->transaction->payment_method_ID(),
2847
+				'payment_amount' => $this->checkout->amount_owing,
2848
+				'return_url'     => $return_url,
2849
+				'cancel_url'     => add_query_arg(array('ee_cancel_payment' => true), $return_url),
2850
+				'notify_url'     => EE_Config::instance()->core->txn_page_url(
2851
+					array(
2852
+						'e_reg_url_link'    => $this->checkout->transaction->primary_registration()->reg_url_link(),
2853
+						'ee_payment_method' => $this->checkout->payment_method->slug(),
2854
+					)
2855
+				),
2856
+			);
2857
+		}
2858
+		echo wp_json_encode($txn_details);
2859
+		exit();
2860
+	}
2861
+
2862
+
2863
+	/**
2864
+	 *    __sleep
2865
+	 * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon
2866
+	 * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the
2867
+	 * reg form, because if needed, it will be regenerated anyways
2868
+	 *
2869
+	 * @return array
2870
+	 */
2871
+	public function __sleep()
2872
+	{
2873
+		// remove the reg form and the checkout
2874
+		return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout', 'line_item_display'));
2875
+	}
2876 2876
 }
Please login to merge, or discard this patch.
Spacing   +79 added lines, -79 removed lines patch added patch discarded remove patch
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
     {
130 130
         $this->_slug = 'payment_options';
131 131
         $this->_name = esc_html__('Payment Options', 'event_espresso');
132
-        $this->_template = SPCO_REG_STEPS_PATH . $this->_slug . '/payment_options_main.template.php';
132
+        $this->_template = SPCO_REG_STEPS_PATH.$this->_slug.'/payment_options_main.template.php';
133 133
         $this->checkout = $checkout;
134 134
         $this->_reset_success_message();
135 135
         $this->set_instructions(
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
     {
213 213
         $transaction = $this->checkout->transaction;
214 214
         // if the transaction isn't set or nothing is owed on it, don't enqueue any JS
215
-        if (! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) {
215
+        if ( ! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) {
216 216
             return;
217 217
         }
218 218
         foreach (EEM_Payment_Method::instance()->get_all_for_transaction(
@@ -304,18 +304,18 @@  discard block
 block discarded – undo
304 304
         foreach ($registrations as $REG_ID => $registration) {
305 305
             /** @var $registration EE_Registration */
306 306
             // has this registration lost it's space ?
307
-            if (isset($ejected_registrations[ $REG_ID ])) {
307
+            if (isset($ejected_registrations[$REG_ID])) {
308 308
                 if ($registration->event()->is_sold_out() || $registration->event()->is_sold_out(true)) {
309
-                    $sold_out_events[ $registration->event()->ID() ] = $registration->event();
309
+                    $sold_out_events[$registration->event()->ID()] = $registration->event();
310 310
                 } else {
311
-                    $insufficient_spaces_available[ $registration->event()->ID() ] = $registration->event();
311
+                    $insufficient_spaces_available[$registration->event()->ID()] = $registration->event();
312 312
                 }
313 313
                 continue;
314 314
             }
315 315
             // event requires admin approval
316 316
             if ($registration->status_ID() === EEM_Registration::status_id_not_approved) {
317 317
                 // add event to list of events with pre-approval reg status
318
-                $registrations_requiring_pre_approval[ $REG_ID ] = $registration;
318
+                $registrations_requiring_pre_approval[$REG_ID] = $registration;
319 319
                 do_action(
320 320
                     'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_pre_approval',
321 321
                     $registration->event(),
@@ -331,7 +331,7 @@  discard block
 block discarded – undo
331 331
                 )
332 332
             ) {
333 333
                 // add event to list of events that are sold out
334
-                $sold_out_events[ $registration->event()->ID() ] = $registration->event();
334
+                $sold_out_events[$registration->event()->ID()] = $registration->event();
335 335
                 do_action(
336 336
                     'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__sold_out_event',
337 337
                     $registration->event(),
@@ -341,38 +341,38 @@  discard block
 block discarded – undo
341 341
             }
342 342
             // are they allowed to pay now and is there monies owing?
343 343
             if ($registration->owes_monies_and_can_pay()) {
344
-                $registrations_requiring_payment[ $REG_ID ] = $registration;
344
+                $registrations_requiring_payment[$REG_ID] = $registration;
345 345
                 do_action(
346 346
                     'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_payment',
347 347
                     $registration->event(),
348 348
                     $this
349 349
                 );
350
-            } elseif (! $this->checkout->revisit
350
+            } elseif ( ! $this->checkout->revisit
351 351
                       && $registration->status_ID() !== EEM_Registration::status_id_not_approved
352 352
                       && $registration->ticket()->is_free()
353 353
             ) {
354
-                $registrations_for_free_events[ $registration->ticket()->ID() ] = $registration;
354
+                $registrations_for_free_events[$registration->ticket()->ID()] = $registration;
355 355
             }
356 356
         }
357 357
         $subsections = array();
358 358
         // now decide which template to load
359
-        if (! empty($sold_out_events)) {
359
+        if ( ! empty($sold_out_events)) {
360 360
             $subsections['sold_out_events'] = $this->_sold_out_events($sold_out_events);
361 361
         }
362
-        if (! empty($insufficient_spaces_available)) {
362
+        if ( ! empty($insufficient_spaces_available)) {
363 363
             $subsections['insufficient_space'] = $this->_insufficient_spaces_available(
364 364
                 $insufficient_spaces_available
365 365
             );
366 366
         }
367
-        if (! empty($registrations_requiring_pre_approval)) {
367
+        if ( ! empty($registrations_requiring_pre_approval)) {
368 368
             $subsections['registrations_requiring_pre_approval'] = $this->_registrations_requiring_pre_approval(
369 369
                 $registrations_requiring_pre_approval
370 370
             );
371 371
         }
372
-        if (! empty($registrations_for_free_events)) {
372
+        if ( ! empty($registrations_for_free_events)) {
373 373
             $subsections['no_payment_required'] = $this->_no_payment_required($registrations_for_free_events);
374 374
         }
375
-        if (! empty($registrations_requiring_payment)) {
375
+        if ( ! empty($registrations_requiring_payment)) {
376 376
             if ($this->checkout->amount_owing > 0) {
377 377
                 // autoload Line_Item_Display classes
378 378
                 EEH_Autoloader::register_line_item_filter_autoloaders();
@@ -437,13 +437,13 @@  discard block
 block discarded – undo
437 437
      */
438 438
     public static function add_spco_line_item_filters(EE_Line_Item_Filter_Collection $line_item_filter_collection)
439 439
     {
440
-        if (! EE_Registry::instance()->SSN instanceof EE_Session) {
440
+        if ( ! EE_Registry::instance()->SSN instanceof EE_Session) {
441 441
             return $line_item_filter_collection;
442 442
         }
443
-        if (! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) {
443
+        if ( ! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) {
444 444
             return $line_item_filter_collection;
445 445
         }
446
-        if (! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) {
446
+        if ( ! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) {
447 447
             return $line_item_filter_collection;
448 448
         }
449 449
         $line_item_filter_collection->add(
@@ -483,8 +483,8 @@  discard block
 block discarded – undo
483 483
         );
484 484
         foreach ($registrations as $REG_ID => $registration) {
485 485
             // has this registration lost it's space ?
486
-            if (isset($ejected_registrations[ $REG_ID ])) {
487
-                unset($registrations[ $REG_ID ]);
486
+            if (isset($ejected_registrations[$REG_ID])) {
487
+                unset($registrations[$REG_ID]);
488 488
                 continue;
489 489
             }
490 490
         }
@@ -534,24 +534,24 @@  discard block
 block discarded – undo
534 534
             }
535 535
             $EVT_ID = $registration->event_ID();
536 536
             $ticket = $registration->ticket();
537
-            if (! isset($tickets_remaining[ $ticket->ID() ])) {
538
-                $tickets_remaining[ $ticket->ID() ] = $ticket->remaining();
537
+            if ( ! isset($tickets_remaining[$ticket->ID()])) {
538
+                $tickets_remaining[$ticket->ID()] = $ticket->remaining();
539 539
             }
540
-            if ($tickets_remaining[ $ticket->ID() ] > 0) {
541
-                if (! isset($event_reg_count[ $EVT_ID ])) {
542
-                    $event_reg_count[ $EVT_ID ] = 0;
540
+            if ($tickets_remaining[$ticket->ID()] > 0) {
541
+                if ( ! isset($event_reg_count[$EVT_ID])) {
542
+                    $event_reg_count[$EVT_ID] = 0;
543 543
                 }
544
-                $event_reg_count[ $EVT_ID ]++;
545
-                if (! isset($event_spaces_remaining[ $EVT_ID ])) {
546
-                    $event_spaces_remaining[ $EVT_ID ] = $registration->event()->spaces_remaining_for_sale();
544
+                $event_reg_count[$EVT_ID]++;
545
+                if ( ! isset($event_spaces_remaining[$EVT_ID])) {
546
+                    $event_spaces_remaining[$EVT_ID] = $registration->event()->spaces_remaining_for_sale();
547 547
                 }
548 548
             }
549 549
             if ($revisit
550
-                && ($tickets_remaining[ $ticket->ID() ] === 0
551
-                    || $event_reg_count[ $EVT_ID ] > $event_spaces_remaining[ $EVT_ID ]
550
+                && ($tickets_remaining[$ticket->ID()] === 0
551
+                    || $event_reg_count[$EVT_ID] > $event_spaces_remaining[$EVT_ID]
552 552
                 )
553 553
             ) {
554
-                $ejected_registrations[ $REG_ID ] = $registration->event();
554
+                $ejected_registrations[$REG_ID] = $registration->event();
555 555
                 if ($registration->status_ID() !== EEM_Registration::status_id_wait_list) {
556 556
                     /** @type EE_Registration_Processor $registration_processor */
557 557
                     $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
@@ -611,7 +611,7 @@  discard block
 block discarded – undo
611 611
         foreach ($sold_out_events_array as $sold_out_event) {
612 612
             $sold_out_events .= EEH_HTML::li(
613 613
                 EEH_HTML::span(
614
-                    '  ' . $sold_out_event->name(),
614
+                    '  '.$sold_out_event->name(),
615 615
                     '',
616 616
                     'dashicons dashicons-marker ee-icon-size-16 pink-text'
617 617
                 )
@@ -666,7 +666,7 @@  discard block
 block discarded – undo
666 666
         foreach ($insufficient_spaces_events_array as $event) {
667 667
             if ($event instanceof EE_Event) {
668 668
                 $insufficient_space_events .= EEH_HTML::li(
669
-                    EEH_HTML::span(' ' . $event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text')
669
+                    EEH_HTML::span(' '.$event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text')
670 670
                 );
671 671
             }
672 672
         }
@@ -714,7 +714,7 @@  discard block
 block discarded – undo
714 714
         $events_requiring_pre_approval = array();
715 715
         foreach ($registrations_requiring_pre_approval as $registration) {
716 716
             if ($registration instanceof EE_Registration && $registration->event() instanceof EE_Event) {
717
-                $events_requiring_pre_approval[ $registration->event()->ID() ] = EEH_HTML::li(
717
+                $events_requiring_pre_approval[$registration->event()->ID()] = EEH_HTML::li(
718 718
                     EEH_HTML::span(
719 719
                         '',
720 720
                         '',
@@ -853,7 +853,7 @@  discard block
 block discarded – undo
853 853
     {
854 854
         return new EE_Form_Section_Proper(
855 855
             array(
856
-                'html_id'         => 'ee-' . $this->slug() . '-extra-hidden-inputs',
856
+                'html_id'         => 'ee-'.$this->slug().'-extra-hidden-inputs',
857 857
                 'layout_strategy' => new EE_Div_Per_Section_Layout(),
858 858
                 'subsections'     => array(
859 859
                     'spco_no_payment_required' => new EE_Hidden_Input(
@@ -893,7 +893,7 @@  discard block
 block discarded – undo
893 893
                 $payments += $registration->registration_payments();
894 894
             }
895 895
         }
896
-        if (! empty($payments)) {
896
+        if ( ! empty($payments)) {
897 897
             foreach ($payments as $payment) {
898 898
                 if ($payment instanceof EE_Registration_Payment) {
899 899
                     $this->checkout->amount_owing -= $payment->amount();
@@ -1017,23 +1017,23 @@  discard block
 block discarded – undo
1017 1017
                 $payment_method_button = EEH_HTML::img(
1018 1018
                     $payment_method->button_url(),
1019 1019
                     $payment_method->name(),
1020
-                    'spco-payment-method-' . $payment_method->slug() . '-btn-img',
1020
+                    'spco-payment-method-'.$payment_method->slug().'-btn-img',
1021 1021
                     'spco-payment-method-btn-img'
1022 1022
                 );
1023 1023
                 // check if any payment methods are set as default
1024 1024
                 // if payment method is already selected OR nothing is selected and this payment method should be
1025 1025
                 // open_by_default
1026 1026
                 if (($this->checkout->selected_method_of_payment === $payment_method->slug())
1027
-                    || (! $this->checkout->selected_method_of_payment && $payment_method->open_by_default())
1027
+                    || ( ! $this->checkout->selected_method_of_payment && $payment_method->open_by_default())
1028 1028
                 ) {
1029 1029
                     $this->checkout->selected_method_of_payment = $payment_method->slug();
1030 1030
                     $this->_save_selected_method_of_payment();
1031
-                    $default_payment_method_option[ $payment_method->slug() ] = $payment_method_button;
1031
+                    $default_payment_method_option[$payment_method->slug()] = $payment_method_button;
1032 1032
                 } else {
1033
-                    $available_payment_method_options[ $payment_method->slug() ] = $payment_method_button;
1033
+                    $available_payment_method_options[$payment_method->slug()] = $payment_method_button;
1034 1034
                 }
1035
-                $payment_methods_billing_info[ $payment_method->slug(
1036
-                ) . '-info' ] = $this->_payment_method_billing_info(
1035
+                $payment_methods_billing_info[$payment_method->slug(
1036
+                ).'-info'] = $this->_payment_method_billing_info(
1037 1037
                     $payment_method
1038 1038
                 );
1039 1039
             }
@@ -1069,7 +1069,7 @@  discard block
 block discarded – undo
1069 1069
      */
1070 1070
     protected function _get_available_payment_methods()
1071 1071
     {
1072
-        if (! empty($this->checkout->available_payment_methods)) {
1072
+        if ( ! empty($this->checkout->available_payment_methods)) {
1073 1073
             return $this->checkout->available_payment_methods;
1074 1074
         }
1075 1075
         $available_payment_methods = array();
@@ -1084,7 +1084,7 @@  discard block
 block discarded – undo
1084 1084
         );
1085 1085
         foreach ($payment_methods as $payment_method) {
1086 1086
             if ($payment_method instanceof EE_Payment_Method) {
1087
-                $available_payment_methods[ $payment_method->slug() ] = $payment_method;
1087
+                $available_payment_methods[$payment_method->slug()] = $payment_method;
1088 1088
             }
1089 1089
         }
1090 1090
         return $available_payment_methods;
@@ -1179,7 +1179,7 @@  discard block
 block discarded – undo
1179 1179
         );
1180 1180
         return new EE_Form_Section_Proper(
1181 1181
             array(
1182
-                'html_id'         => 'spco-payment-method-info-' . $payment_method->slug(),
1182
+                'html_id'         => 'spco-payment-method-info-'.$payment_method->slug(),
1183 1183
                 'html_class'      => 'spco-payment-method-info-dv',
1184 1184
                 // only display the selected or default PM
1185 1185
                 'html_style'      => $currently_selected ? '' : 'display:none;',
@@ -1209,7 +1209,7 @@  discard block
 block discarded – undo
1209 1209
         // how have they chosen to pay?
1210 1210
         $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
1211 1211
         $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1212
-        if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1212
+        if ( ! $this->checkout->payment_method instanceof EE_Payment_Method) {
1213 1213
             return false;
1214 1214
         }
1215 1215
         if (apply_filters(
@@ -1381,7 +1381,7 @@  discard block
 block discarded – undo
1381 1381
      */
1382 1382
     public function switch_payment_method()
1383 1383
     {
1384
-        if (! $this->_verify_payment_method_is_set()) {
1384
+        if ( ! $this->_verify_payment_method_is_set()) {
1385 1385
             return false;
1386 1386
         }
1387 1387
         if (apply_filters(
@@ -1516,7 +1516,7 @@  discard block
 block discarded – undo
1516 1516
             }
1517 1517
         }
1518 1518
         // verify payment method
1519
-        if (! $this->checkout->payment_method instanceof EE_Payment_Method) {
1519
+        if ( ! $this->checkout->payment_method instanceof EE_Payment_Method) {
1520 1520
             // get payment method for selected method of payment
1521 1521
             $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment();
1522 1522
         }
@@ -1541,7 +1541,7 @@  discard block
 block discarded – undo
1541 1541
      */
1542 1542
     public function save_payer_details_via_ajax()
1543 1543
     {
1544
-        if (! $this->_verify_payment_method_is_set()) {
1544
+        if ( ! $this->_verify_payment_method_is_set()) {
1545 1545
             return;
1546 1546
         }
1547 1547
         // generate billing form for selected method of payment if it hasn't been done already
@@ -1551,7 +1551,7 @@  discard block
 block discarded – undo
1551 1551
             );
1552 1552
         }
1553 1553
         // generate primary attendee from payer info if applicable
1554
-        if (! $this->checkout->transaction_has_primary_registrant()) {
1554
+        if ( ! $this->checkout->transaction_has_primary_registrant()) {
1555 1555
             $attendee = $this->_create_attendee_from_request_data();
1556 1556
             if ($attendee instanceof EE_Attendee) {
1557 1557
                 foreach ($this->checkout->transaction->registrations() as $registration) {
@@ -1582,7 +1582,7 @@  discard block
 block discarded – undo
1582 1582
     {
1583 1583
         // get State ID
1584 1584
         $STA_ID = ! empty($_REQUEST['state']) ? sanitize_text_field($_REQUEST['state']) : '';
1585
-        if (! empty($STA_ID)) {
1585
+        if ( ! empty($STA_ID)) {
1586 1586
             // can we get state object from name ?
1587 1587
             EE_Registry::instance()->load_model('State');
1588 1588
             $state = EEM_State::instance()->get_col(array(array('STA_name' => $STA_ID), 'limit' => 1), 'STA_ID');
@@ -1590,7 +1590,7 @@  discard block
 block discarded – undo
1590 1590
         }
1591 1591
         // get Country ISO
1592 1592
         $CNT_ISO = ! empty($_REQUEST['country']) ? sanitize_text_field($_REQUEST['country']) : '';
1593
-        if (! empty($CNT_ISO)) {
1593
+        if ( ! empty($CNT_ISO)) {
1594 1594
             // can we get country object from name ?
1595 1595
             EE_Registry::instance()->load_model('Country');
1596 1596
             $country = EEM_Country::instance()->get_col(
@@ -1623,7 +1623,7 @@  discard block
 block discarded – undo
1623 1623
         }
1624 1624
         // does this attendee already exist in the db ? we're searching using a combination of first name, last name,
1625 1625
         // AND email address
1626
-        if (! empty($attendee_data['ATT_fname'])
1626
+        if ( ! empty($attendee_data['ATT_fname'])
1627 1627
             && ! empty($attendee_data['ATT_lname'])
1628 1628
             && ! empty($attendee_data['ATT_email'])
1629 1629
         ) {
@@ -1773,7 +1773,7 @@  discard block
 block discarded – undo
1773 1773
     protected function _maybe_set_completed(EE_Payment $payment)
1774 1774
     {
1775 1775
         // Do we need to redirect them? If so, there's more work to be done.
1776
-        if (! $payment->redirect_url()){
1776
+        if ( ! $payment->redirect_url()) {
1777 1777
             $this->set_completed();
1778 1778
         }
1779 1779
     }
@@ -1833,7 +1833,7 @@  discard block
 block discarded – undo
1833 1833
     private function _process_payment()
1834 1834
     {
1835 1835
         // basically confirm that the event hasn't sold out since they hit the page
1836
-        if (! $this->_last_second_ticket_verifications()) {
1836
+        if ( ! $this->_last_second_ticket_verifications()) {
1837 1837
             return null;
1838 1838
         }
1839 1839
         // ya gotta make a choice man
@@ -1844,7 +1844,7 @@  discard block
 block discarded – undo
1844 1844
             return null;
1845 1845
         }
1846 1846
         // get EE_Payment_Method object
1847
-        if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
1847
+        if ( ! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
1848 1848
             return null;
1849 1849
         }
1850 1850
         // setup billing form
@@ -1853,12 +1853,12 @@  discard block
 block discarded – undo
1853 1853
                 $this->checkout->payment_method
1854 1854
             );
1855 1855
             // bad billing form ?
1856
-            if (! $this->_billing_form_is_valid()) {
1856
+            if ( ! $this->_billing_form_is_valid()) {
1857 1857
                 return null;
1858 1858
             }
1859 1859
         }
1860 1860
         // ensure primary registrant has been fully processed
1861
-        if (! $this->_setup_primary_registrant_prior_to_payment()) {
1861
+        if ( ! $this->_setup_primary_registrant_prior_to_payment()) {
1862 1862
             return null;
1863 1863
         }
1864 1864
         // if session is close to expiring (under 10 minutes by default)
@@ -1913,7 +1913,7 @@  discard block
 block discarded – undo
1913 1913
     protected function _last_second_ticket_verifications()
1914 1914
     {
1915 1915
         // don't bother re-validating if not a return visit
1916
-        if (! $this->checkout->revisit) {
1916
+        if ( ! $this->checkout->revisit) {
1917 1917
             return true;
1918 1918
         }
1919 1919
         $registrations = $this->checkout->transaction->registrations();
@@ -1979,7 +1979,7 @@  discard block
 block discarded – undo
1979 1979
      */
1980 1980
     private function _billing_form_is_valid()
1981 1981
     {
1982
-        if (! $this->checkout->payment_method->type_obj()->has_billing_form()) {
1982
+        if ( ! $this->checkout->payment_method->type_obj()->has_billing_form()) {
1983 1983
             return true;
1984 1984
         }
1985 1985
         if ($this->checkout->billing_form instanceof EE_Billing_Info_Form) {
@@ -2098,7 +2098,7 @@  discard block
 block discarded – undo
2098 2098
     {
2099 2099
         // convert billing form data into an attendee
2100 2100
         $this->checkout->primary_attendee_obj = $this->checkout->billing_form->create_attendee_from_billing_form_data();
2101
-        if (! $this->checkout->primary_attendee_obj instanceof EE_Attendee) {
2101
+        if ( ! $this->checkout->primary_attendee_obj instanceof EE_Attendee) {
2102 2102
             EE_Error::add_error(
2103 2103
                 sprintf(
2104 2104
                     esc_html__(
@@ -2115,7 +2115,7 @@  discard block
 block discarded – undo
2115 2115
             return false;
2116 2116
         }
2117 2117
         $primary_registration = $this->checkout->transaction->primary_registration();
2118
-        if (! $primary_registration instanceof EE_Registration) {
2118
+        if ( ! $primary_registration instanceof EE_Registration) {
2119 2119
             EE_Error::add_error(
2120 2120
                 sprintf(
2121 2121
                     esc_html__(
@@ -2131,7 +2131,7 @@  discard block
 block discarded – undo
2131 2131
             );
2132 2132
             return false;
2133 2133
         }
2134
-        if (! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee')
2134
+        if ( ! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee')
2135 2135
               instanceof
2136 2136
               EE_Attendee
2137 2137
         ) {
@@ -2177,8 +2177,8 @@  discard block
 block discarded – undo
2177 2177
             return null;
2178 2178
         }
2179 2179
         // get EE_Payment_Method object
2180
-        if (isset($this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ])) {
2181
-            $payment_method = $this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ];
2180
+        if (isset($this->checkout->available_payment_methods[$this->checkout->selected_method_of_payment])) {
2181
+            $payment_method = $this->checkout->available_payment_methods[$this->checkout->selected_method_of_payment];
2182 2182
         } else {
2183 2183
             // load EEM_Payment_Method
2184 2184
             EE_Registry::instance()->load_model('Payment_Method');
@@ -2187,7 +2187,7 @@  discard block
 block discarded – undo
2187 2187
             $payment_method = $EEM_Payment_Method->get_one_by_slug($this->checkout->selected_method_of_payment);
2188 2188
         }
2189 2189
         // verify $payment_method
2190
-        if (! $payment_method instanceof EE_Payment_Method) {
2190
+        if ( ! $payment_method instanceof EE_Payment_Method) {
2191 2191
             // not a payment
2192 2192
             EE_Error::add_error(
2193 2193
                 sprintf(
@@ -2205,7 +2205,7 @@  discard block
 block discarded – undo
2205 2205
             return null;
2206 2206
         }
2207 2207
         // and verify it has a valid Payment_Method Type object
2208
-        if (! $payment_method->type_obj() instanceof EE_PMT_Base) {
2208
+        if ( ! $payment_method->type_obj() instanceof EE_PMT_Base) {
2209 2209
             // not a payment
2210 2210
             EE_Error::add_error(
2211 2211
                 sprintf(
@@ -2243,7 +2243,7 @@  discard block
 block discarded – undo
2243 2243
         $payment = null;
2244 2244
         $this->checkout->transaction->save();
2245 2245
         $payment_processor = EE_Registry::instance()->load_core('Payment_Processor');
2246
-        if (! $payment_processor instanceof EE_Payment_Processor) {
2246
+        if ( ! $payment_processor instanceof EE_Payment_Processor) {
2247 2247
             return false;
2248 2248
         }
2249 2249
         try {
@@ -2347,7 +2347,7 @@  discard block
 block discarded – undo
2347 2347
             return true;
2348 2348
         }
2349 2349
         // verify payment object
2350
-        if (! $payment instanceof EE_Payment) {
2350
+        if ( ! $payment instanceof EE_Payment) {
2351 2351
             // not a payment
2352 2352
             EE_Error::add_error(
2353 2353
                 sprintf(
@@ -2384,9 +2384,9 @@  discard block
 block discarded – undo
2384 2384
         // Off-Line payment?
2385 2385
         if ($payment === true) {
2386 2386
             return true;
2387
-        } elseif ($payment instanceof EE_Payment){
2387
+        } elseif ($payment instanceof EE_Payment) {
2388 2388
             // Should the user be redirected?
2389
-            if($payment->redirect_url()) {
2389
+            if ($payment->redirect_url()) {
2390 2390
                 do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->redirect_url(), '$payment->redirect_url()');
2391 2391
                 $this->checkout->redirect = true;
2392 2392
                 $this->checkout->redirect_form = $payment->redirect_form();
@@ -2398,7 +2398,7 @@  discard block
 block discarded – undo
2398 2398
                 $payment->save();
2399 2399
             } else
2400 2400
                 // User shouldn't be redirected. So let's process it here.
2401
-                if (! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) {
2401
+                if ( ! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) {
2402 2402
                 // $this->_setup_redirect_for_next_step();
2403 2403
                 $this->checkout->continue_reg = false;
2404 2404
             }
@@ -2484,7 +2484,7 @@  discard block
 block discarded – undo
2484 2484
                     break;
2485 2485
                 // bad payment
2486 2486
                 case EEM_Payment::status_id_failed:
2487
-                    if (! empty($msg)) {
2487
+                    if ( ! empty($msg)) {
2488 2488
                         EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2489 2489
                         return false;
2490 2490
                     }
@@ -2545,11 +2545,11 @@  discard block
 block discarded – undo
2545 2545
         // how have they chosen to pay?
2546 2546
         $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true);
2547 2547
         // get EE_Payment_Method object
2548
-        if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
2548
+        if ( ! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) {
2549 2549
             $this->checkout->continue_reg = false;
2550 2550
             return false;
2551 2551
         }
2552
-        if (! $this->checkout->payment_method->is_off_site()) {
2552
+        if ( ! $this->checkout->payment_method->is_off_site()) {
2553 2553
             return false;
2554 2554
         }
2555 2555
         $this->_validate_offsite_return();
@@ -2567,7 +2567,7 @@  discard block
 block discarded – undo
2567 2567
         // verify TXN
2568 2568
         if ($this->checkout->transaction instanceof EE_Transaction) {
2569 2569
             $gateway = $this->checkout->payment_method->type_obj()->get_gateway();
2570
-            if (! $gateway instanceof EE_Offsite_Gateway) {
2570
+            if ( ! $gateway instanceof EE_Offsite_Gateway) {
2571 2571
                 $this->checkout->continue_reg = false;
2572 2572
                 return false;
2573 2573
             }
@@ -2684,13 +2684,13 @@  discard block
 block discarded – undo
2684 2684
      */
2685 2685
     private function _redirect_wayward_request(EE_Registration $primary_registrant)
2686 2686
     {
2687
-        if (! $primary_registrant instanceof EE_Registration) {
2687
+        if ( ! $primary_registrant instanceof EE_Registration) {
2688 2688
             // try redirecting based on the current TXN
2689 2689
             $primary_registrant = $this->checkout->transaction instanceof EE_Transaction
2690 2690
                 ? $this->checkout->transaction->primary_registration()
2691 2691
                 : null;
2692 2692
         }
2693
-        if (! $primary_registrant instanceof EE_Registration) {
2693
+        if ( ! $primary_registrant instanceof EE_Registration) {
2694 2694
             EE_Error::add_error(
2695 2695
                 sprintf(
2696 2696
                     esc_html__(
@@ -2760,7 +2760,7 @@  discard block
 block discarded – undo
2760 2760
             $payment = $this->checkout->transaction->last_payment();
2761 2761
             // $payment_source = 'last_payment after Exception';
2762 2762
             // but if we STILL don't have a payment object
2763
-            if (! $payment instanceof EE_Payment) {
2763
+            if ( ! $payment instanceof EE_Payment) {
2764 2764
                 // then we'll object ! ( not object like a thing... but object like what a lawyer says ! )
2765 2765
                 $this->_handle_payment_processor_exception($e);
2766 2766
             }
Please login to merge, or discard this patch.
core/domain/services/custom_post_types/RewriteRules.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -13,35 +13,35 @@
 block discarded – undo
13 13
 class RewriteRules
14 14
 {
15 15
 
16
-    const OPTION_KEY_FLUSH_REWRITE_RULES = 'ee_flush_rewrite_rules';
16
+	const OPTION_KEY_FLUSH_REWRITE_RULES = 'ee_flush_rewrite_rules';
17 17
 
18 18
 
19
-    /**
20
-     * This will flush rewrite rules on demand.  This actually gets called around wp init priority level 100.
21
-     *
22
-     * @return void
23
-     */
24
-    public function flush()
25
-    {
26
-        update_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, true);
27
-    }
19
+	/**
20
+	 * This will flush rewrite rules on demand.  This actually gets called around wp init priority level 100.
21
+	 *
22
+	 * @return void
23
+	 */
24
+	public function flush()
25
+	{
26
+		update_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, true);
27
+	}
28 28
 
29 29
 
30
-    /**
31
-     * This will flush rewrite rules on demand.  This actually gets called around wp init priority level 100.
32
-     *
33
-     * @return void
34
-     */
35
-    public function flushRewriteRules()
36
-    {
37
-        if (get_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, true)) {
38
-            add_action(
39
-                'shutdown',
40
-                static function () {
41
-                    flush_rewrite_rules();
42
-                    update_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, false);
43
-                }
44
-            );
45
-        }
46
-    }
30
+	/**
31
+	 * This will flush rewrite rules on demand.  This actually gets called around wp init priority level 100.
32
+	 *
33
+	 * @return void
34
+	 */
35
+	public function flushRewriteRules()
36
+	{
37
+		if (get_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, true)) {
38
+			add_action(
39
+				'shutdown',
40
+				static function () {
41
+					flush_rewrite_rules();
42
+					update_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, false);
43
+				}
44
+			);
45
+		}
46
+	}
47 47
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -37,7 +37,7 @@
 block discarded – undo
37 37
         if (get_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, true)) {
38 38
             add_action(
39 39
                 'shutdown',
40
-                static function () {
40
+                static function() {
41 41
                     flush_rewrite_rules();
42 42
                     update_option(RewriteRules::OPTION_KEY_FLUSH_REWRITE_RULES, false);
43 43
                 }
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
38 38
  * @since           4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 } else {
64
-    define('EE_MIN_PHP_VER_REQUIRED', '5.6.2');
65
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
-        /**
67
-         * espresso_minimum_php_version_error
68
-         *
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
64
+	define('EE_MIN_PHP_VER_REQUIRED', '5.6.2');
65
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
+		/**
67
+		 * espresso_minimum_php_version_error
68
+		 *
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.10.13.rc.006');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.10.13.rc.006');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118 118
 
119
-        require_once __DIR__ . '/core/bootstrap_espresso.php';
120
-        bootstrap_espresso();
121
-    }
119
+		require_once __DIR__ . '/core/bootstrap_espresso.php';
120
+		bootstrap_espresso();
121
+	}
122 122
 }
123 123
 if (! function_exists('espresso_deactivate_plugin')) {
124
-    /**
125
-     *    deactivate_plugin
126
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
-     *
128
-     * @access public
129
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
-     * @return    void
131
-     */
132
-    function espresso_deactivate_plugin($plugin_basename = '')
133
-    {
134
-        if (! function_exists('deactivate_plugins')) {
135
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
-        }
137
-        unset($_GET['activate'], $_REQUEST['activate']);
138
-        deactivate_plugins($plugin_basename);
139
-    }
124
+	/**
125
+	 *    deactivate_plugin
126
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
+	 *
128
+	 * @access public
129
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
+	 * @return    void
131
+	 */
132
+	function espresso_deactivate_plugin($plugin_basename = '')
133
+	{
134
+		if (! function_exists('deactivate_plugins')) {
135
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
+		}
137
+		unset($_GET['activate'], $_REQUEST['activate']);
138
+		deactivate_plugins($plugin_basename);
139
+	}
140 140
 }
Please login to merge, or discard this patch.