/
checkout.php
841 lines (728 loc) · 29.2 KB
/
checkout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
<?php
global $post, $gateway, $wpdb, $besecure, $discount_code, $discount_code_id, $pmpro_level, $pmpro_msg, $pmpro_msgt, $pmpro_review, $skip_account_fields, $pmpro_paypal_token, $pmpro_show_discount_code, $pmpro_error_fields, $pmpro_required_billing_fields, $pmpro_required_user_fields, $wp_version, $current_user, $pmpro_checkout_level_ids;
// we are on the checkout page
add_filter( 'pmpro_is_checkout', '__return_true' );
//this var stores fields with errors so we can make them red on the frontend
$pmpro_error_fields = array();
//blank array for required fields, set below
$pmpro_required_billing_fields = array();
$pmpro_required_user_fields = array();
//was a gateway passed?
if ( ! empty( $_REQUEST['gateway'] ) ) {
$gateway = sanitize_text_field($_REQUEST['gateway']);
} elseif ( ! empty( $_REQUEST['review'] ) ) {
$gateway = "paypalexpress";
} else {
$gateway = get_option( "pmpro_gateway" );
}
//set valid gateways - the active gateway in the settings and any gateway added through the filter will be allowed
if ( get_option( "pmpro_gateway" ) == "paypal" ) {
$valid_gateways = apply_filters( "pmpro_valid_gateways", array( "paypal", "paypalexpress" ) );
} else {
$valid_gateways = apply_filters( "pmpro_valid_gateways", array( get_option( "pmpro_gateway" ) ) );
}
//let's add an error now, if an invalid gateway is set
if ( ! in_array( $gateway, $valid_gateways ) ) {
$pmpro_msg = __( "Invalid gateway.", 'paid-memberships-pro' );
$pmpro_msgt = "pmpro_error";
}
/**
* Action to run extra preheader code before setting checkout level.
*
* @since 2.0.5
*/
do_action( 'pmpro_checkout_preheader_before_get_level_at_checkout' );
//what level are they purchasing? (discount code passed)
$pmpro_level = pmpro_getLevelAtCheckout();
/**
* Action to run extra preheader code after setting checkout level.
*
* @since 2.0.5
* //TODO update docblock
*/
do_action( 'pmpro_checkout_preheader_after_get_level_at_checkout', $pmpro_level );
if ( empty( $pmpro_level->id ) ) {
wp_redirect( pmpro_url( "levels" ) );
exit( 0 );
}
//enqueue some scripts
wp_enqueue_script( 'jquery.creditCardValidator', plugins_url( '/js/jquery.creditCardValidator.js', dirname( __FILE__ ) ), array( 'jquery' ) );
global $wpdb, $current_user, $pmpro_requirebilling;
//unless we're submitting a form, let's try to figure out if https should be used
if ( ! pmpro_isLevelFree( $pmpro_level ) ) {
//require billing and ssl
$pagetitle = __( "Checkout: Payment Information", 'paid-memberships-pro' );
$pmpro_requirebilling = true;
$besecure = get_option( "pmpro_use_ssl" );
} else {
//no payment so we don't need ssl
$pagetitle = __( "Set Up Your Account", 'paid-memberships-pro' );
$pmpro_requirebilling = false;
$besecure = false;
}
// Allow for filters.
// TODO: docblock.
$pmpro_requirebilling = apply_filters( 'pmpro_require_billing', $pmpro_requirebilling, $pmpro_level );
//in case a discount code was used or something else made the level free, but we're already over ssl
if ( ! $besecure && ! empty( $_REQUEST['submit-checkout'] ) && is_ssl() ) {
$besecure = true;
} //be secure anyway since we're already checking out
//action to run extra code for gateways/etc
do_action( 'pmpro_checkout_preheader' );
// We set a global var for add-ons that are expecting it.
$pmpro_show_discount_code = pmpro_show_discount_code();
//by default we show the account fields if the user isn't logged in
if ( $current_user->ID ) {
$skip_account_fields = true;
} else {
$skip_account_fields = false;
}
//in case people want to have an account created automatically
$skip_account_fields = apply_filters( "pmpro_skip_account_fields", $skip_account_fields, $current_user );
//some options
global $tospage;
$tospage = get_option( "pmpro_tospage" );
if ( $tospage ) {
$tospage = get_post( $tospage );
}
//load em up (other fields)
global $username, $password, $password2, $bfirstname, $blastname, $baddress1, $baddress2, $bcity, $bstate, $bzipcode, $bcountry, $bphone, $bemail, $bconfirmemail, $CardType, $AccountNumber, $ExpirationMonth, $ExpirationYear;
if ( isset( $_REQUEST['order_id'] ) ) {
$order_id = intval( $_REQUEST['order_id'] );
} else {
$order_id = "";
}
if ( isset( $_REQUEST['bfirstname'] ) ) {
$bfirstname = stripslashes( sanitize_text_field( $_REQUEST['bfirstname'] ) );
} else {
$bfirstname = "";
}
if ( isset( $_REQUEST['blastname'] ) ) {
$blastname = stripslashes( sanitize_text_field( $_REQUEST['blastname'] ) );
} else {
$blastname = "";
}
if ( isset( $_REQUEST['fullname'] ) ) {
$fullname = sanitize_text_field( $_REQUEST['fullname'] );
} //honeypot for spammers
if ( isset( $_REQUEST['baddress1'] ) ) {
$baddress1 = stripslashes( sanitize_text_field( $_REQUEST['baddress1'] ) );
} else {
$baddress1 = "";
}
if ( isset( $_REQUEST['baddress2'] ) ) {
$baddress2 = stripslashes( sanitize_text_field( $_REQUEST['baddress2'] ) );
} else {
$baddress2 = "";
}
if ( isset( $_REQUEST['bcity'] ) ) {
$bcity = stripslashes( sanitize_text_field( $_REQUEST['bcity'] ) );
} else {
$bcity = "";
}
if ( isset( $_REQUEST['bstate'] ) ) {
$bstate = stripslashes( sanitize_text_field( $_REQUEST['bstate'] ) );
} else {
$bstate = "";
}
//convert long state names to abbreviations
if ( ! empty( $bstate ) ) {
global $pmpro_states;
foreach ( $pmpro_states as $abbr => $state ) {
if ( $bstate == $state ) {
$bstate = $abbr;
break;
}
}
}
if ( isset( $_REQUEST['bzipcode'] ) ) {
$bzipcode = stripslashes( sanitize_text_field( $_REQUEST['bzipcode'] ) );
} else {
$bzipcode = "";
}
if ( isset( $_REQUEST['bcountry'] ) ) {
$bcountry = stripslashes( sanitize_text_field( $_REQUEST['bcountry'] ) );
} else {
$bcountry = "";
}
if ( isset( $_REQUEST['bphone'] ) ) {
$bphone = stripslashes( sanitize_text_field( $_REQUEST['bphone'] ) );
} else {
$bphone = "";
}
if ( isset ( $_REQUEST['bemail'] ) ) {
$bemail = stripslashes( sanitize_email( $_REQUEST['bemail'] ) );
} elseif ( is_user_logged_in() ) {
$bemail = $current_user->user_email;
} else {
$bemail = "";
}
if ( isset( $_REQUEST['bconfirmemail_copy'] ) ) {
$bconfirmemail = $bemail;
} elseif ( isset( $_REQUEST['bconfirmemail'] ) ) {
$bconfirmemail = stripslashes( sanitize_email( $_REQUEST['bconfirmemail'] ) );
} elseif ( is_user_logged_in() ) {
$bconfirmemail = $current_user->user_email;
} else {
$bconfirmemail = "";
}
if ( isset( $_REQUEST['CardType'] ) && ! empty( $_REQUEST['AccountNumber'] ) ) {
$CardType = sanitize_text_field( $_REQUEST['CardType'] );
} else {
$CardType = "";
}
if ( isset( $_REQUEST['AccountNumber'] ) ) {
$AccountNumber = sanitize_text_field( $_REQUEST['AccountNumber'] );
} else {
$AccountNumber = "";
}
if ( isset( $_REQUEST['ExpirationMonth'] ) ) {
$ExpirationMonth = sanitize_text_field( $_REQUEST['ExpirationMonth'] );
} else {
$ExpirationMonth = "";
}
if ( isset( $_REQUEST['ExpirationYear'] ) ) {
$ExpirationYear = sanitize_text_field( $_REQUEST['ExpirationYear'] );
} else {
$ExpirationYear = "";
}
if ( isset( $_REQUEST['CVV'] ) ) {
$CVV = sanitize_text_field( $_REQUEST['CVV'] );
} else {
$CVV = "";
}
if ( ! empty( $pmpro_level->discount_code ) ) {
$discount_code = preg_replace( "/[^A-Za-z0-9\-]/", "", sanitize_text_field( $pmpro_level->discount_code ) );
} else {
$discount_code = "";
}
if ( isset( $_REQUEST['username'] ) ) {
$username = sanitize_user( $_REQUEST['username'] , true);
} else {
$username = "";
}
// Note: We can't sanitize the passwords. They get hashed when saved.
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_REQUEST['password'] ) ) {
$password = $_REQUEST['password'];
} else {
$password = "";
}
if ( isset( $_REQUEST['password2_copy'] ) ) {
$password2 = $password;
} elseif ( isset( $_REQUEST['password2'] ) ) {
$password2 = $_REQUEST['password2'];
} else {
$password2 = "";
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_REQUEST['tos'] ) ) {
$tos = intval( $_REQUEST['tos'] );
} else {
$tos = "";
}
$submit = pmpro_was_checkout_form_submitted();
/**
* Hook to run actions after the parameters are set on the checkout page.
* @since 2.1
*/
do_action( 'pmpro_checkout_after_parameters_set' );
//require fields
$pmpro_required_billing_fields = array(
"bfirstname" => $bfirstname,
"blastname" => $blastname,
"baddress1" => $baddress1,
"bcity" => $bcity,
"bstate" => $bstate,
"bzipcode" => $bzipcode,
"bphone" => $bphone,
"bemail" => $bemail,
"bcountry" => $bcountry,
"CardType" => $CardType,
"AccountNumber" => $AccountNumber,
"ExpirationMonth" => $ExpirationMonth,
"ExpirationYear" => $ExpirationYear,
"CVV" => $CVV
);
$pmpro_required_billing_fields = apply_filters( "pmpro_required_billing_fields", $pmpro_required_billing_fields );
$pmpro_required_user_fields = array(
"username" => $username,
"password" => $password,
"password2" => $password2,
"bemail" => $bemail,
"bconfirmemail" => $bconfirmemail
);
$pmpro_required_user_fields = apply_filters( "pmpro_required_user_fields", $pmpro_required_user_fields );
//pmpro_confirmed is set to true later if payment goes through
$pmpro_confirmed = false;
//check their fields if they clicked continue
if ( $submit && $pmpro_msgt != "pmpro_error" ) {
// Check the nonce.
if ( empty( $_REQUEST['pmpro_checkout_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['pmpro_checkout_nonce'] ), 'pmpro_checkout_nonce' ) ) {
// Nonce is not valid, but a nonce was only added in the 3.0 checkout template. We only want to show an error if the checkout template is 3.0 or later.
$loaded_path = pmpro_get_template_path_to_load( 'checkout' );
$loaded_version = pmpro_get_version_for_page_template_at_path( $loaded_path );
if ( ! empty( $loaded_version ) && version_compare( $loaded_version, '3.0', '>=' ) ) {
// Nonce is not valid. Show an error.
pmpro_setMessage( __( "Nonce security check failed.", 'paid-memberships-pro' ), 'pmpro_error' );
}
}
//make sure javascript is ok
if ( apply_filters( "pmpro_require_javascript_for_checkout", true ) && ! empty( $_REQUEST['checkjavascript'] ) && empty( $_REQUEST['javascriptok'] ) ) {
pmpro_setMessage( __( "There are JavaScript errors on the page. Please contact the webmaster.", 'paid-memberships-pro' ), "pmpro_error" );
}
// If we're skipping the account fields and there is no user, we need to create a username and password.
if ( $skip_account_fields && ! $current_user->ID ) {
// Generate the username using the first name, last name and/or email address.
$username = pmpro_generateUsername( $bfirstname, $blastname, $bemail );
// Generate the password.
$password = wp_generate_password();
// Set the password confirmation to the generated password.
$password2 = $password;
}
//check billing fields
if ( $pmpro_requirebilling ) {
//filter
foreach ( $pmpro_required_billing_fields as $key => $field ) {
if ( ! $field ) {
$pmpro_error_fields[] = $key;
}
}
}
//check user fields
if ( empty( $current_user->ID ) ) {
foreach ( $pmpro_required_user_fields as $key => $field ) {
if ( ! $field ) {
$pmpro_error_fields[] = $key;
}
}
}
if ( ! empty( $pmpro_error_fields ) ) {
pmpro_setMessage( __( "Please complete all required fields.", 'paid-memberships-pro' ), "pmpro_error" );
}
if ( ! empty( $password ) && $password != $password2 ) {
pmpro_setMessage( __( "Your passwords do not match. Please try again.", 'paid-memberships-pro' ), "pmpro_error" );
$pmpro_error_fields[] = "password";
$pmpro_error_fields[] = "password2";
}
if ( strcasecmp($bemail, $bconfirmemail) !== 0 ) {
pmpro_setMessage( __( "Your email addresses do not match. Please try again.", 'paid-memberships-pro' ), "pmpro_error" );
$pmpro_error_fields[] = "bemail";
$pmpro_error_fields[] = "bconfirmemail";
}
if ( ! empty( $bemail ) && ! is_email( $bemail ) ) {
pmpro_setMessage( __( "The email address entered is in an invalid format. Please try again.", 'paid-memberships-pro' ), "pmpro_error" );
$pmpro_error_fields[] = "bemail";
$pmpro_error_fields[] = "bconfirmemail";
}
if ( ! empty( $tospage ) && empty( $tos ) ) {
pmpro_setMessage( sprintf( __( "Please check the box to agree to the %s.", 'paid-memberships-pro' ), $tospage->post_title ), "pmpro_error" );
$pmpro_error_fields[] = "tospage";
}
if ( ! in_array( $gateway, $valid_gateways ) ) {
pmpro_setMessage( __( "Invalid gateway.", 'paid-memberships-pro' ), "pmpro_error" );
}
if ( ! empty( $fullname ) ) {
pmpro_setMessage( __( "Are you a spammer?", 'paid-memberships-pro' ), "pmpro_error" );
}
if ( $pmpro_msgt == "pmpro_error" ) {
$pmpro_continue_registration = false;
} else {
$pmpro_continue_registration = true;
}
$pmpro_continue_registration = apply_filters( "pmpro_registration_checks", $pmpro_continue_registration );
if ( $pmpro_continue_registration ) {
//if creating a new user, check that the email and username are available
if ( empty( $current_user->ID ) ) {
$ouser = get_user_by( 'login', $username );
$oldem_user = get_user_by( 'email', $bemail );
//this hook can be used to allow multiple accounts with the same email address
$oldemail = apply_filters( "pmpro_checkout_oldemail", ( false !== $oldem_user ? $oldem_user->user_email : null ) );
}
if ( ! empty( $ouser->user_login ) ) {
pmpro_setMessage( __( "That username is already taken. Please try another.", 'paid-memberships-pro' ), "pmpro_error" );
$pmpro_error_fields[] = "username";
}
if ( ! empty( $oldemail ) ) {
pmpro_setMessage( __( "That email address is already in use. Please log in, or use a different email address.", 'paid-memberships-pro' ), "pmpro_error" );
$pmpro_error_fields[] = "bemail";
$pmpro_error_fields[] = "bconfirmemail";
}
//only continue if there are no other errors yet
if ( $pmpro_msgt != "pmpro_error" ) {
//check recaptcha first
$recaptcha = get_option( "pmpro_recaptcha");
if ( $recaptcha == 2 || ( $recaptcha == 1 && pmpro_isLevelFree( $pmpro_level ) ) ) {
$recaptcha_validated = pmpro_recaptcha_is_validated(); // Returns true if validated, string error message if not.
if ( is_string( $recaptcha_validated ) ) {
$pmpro_msg = sprintf( __( "reCAPTCHA failed. (%s) Please try again.", 'paid-memberships-pro' ), $recaptcha_validated );
$pmpro_msgt = "pmpro_error";
}
}
}
// Only continue if there are no other errors yet
if ( $pmpro_msgt != "pmpro_error" ) {
// Do we need to create a user account?
if ( ! $current_user->ID ) {
// Yes, create user.
//first name
if ( ! empty( $_REQUEST['first_name'] ) ) {
$first_name = sanitize_text_field( $_REQUEST['first_name'] );
} else {
$first_name = $bfirstname;
}
//last name
if ( ! empty( $_REQUEST['last_name'] ) ) {
$last_name = sanitize_text_field( $_REQUEST['last_name'] );
} else {
$last_name = $blastname;
}
//insert user
$new_user_array = apply_filters( 'pmpro_checkout_new_user_array', array(
"user_login" => $username,
"user_pass" => $password,
"user_email" => $bemail,
"first_name" => $first_name,
"last_name" => $last_name
)
);
$user_id = apply_filters( 'pmpro_new_user', '', $new_user_array );
if ( empty( $user_id ) ) {
$user_id = wp_insert_user( $new_user_array );
}
if ( empty( $user_id ) || is_wp_error( $user_id ) ) {
$e_msg = '';
if ( is_wp_error( $user_id ) ) {
$e_msg = $user_id->get_error_message();
}
$pmpro_msg = __( "There was an error setting up your account. Please contact us.", 'paid-memberships-pro' ) . sprintf( " %s", $e_msg ); // Dirty 'don't break translation hack.
$pmpro_msgt = "pmpro_error";
} elseif ( apply_filters( 'pmpro_setup_new_user', true, $user_id, $new_user_array, $pmpro_level ) ) {
pmpro_maybe_send_wp_new_user_notification( $user_id, $pmpro_level->id );
$wpuser = get_userdata( $user_id );
$wpuser->set_role( get_option( 'default_role', 'subscriber' ) );
/**
* Allow hooking before the user authentication process when setting up new user.
*
* @since 2.5.10
*
* @param int $user_id The user ID that is being setting up.
*/
do_action( 'pmpro_checkout_before_user_auth', $user_id );
//okay, log them in to WP
$creds = array();
$creds['user_login'] = $new_user_array['user_login'];
$creds['user_password'] = $new_user_array['user_pass'];
$creds['remember'] = true;
$user = wp_signon( $creds, false );
//setting some cookies
wp_set_current_user( $user_id, $username );
wp_set_auth_cookie( $user_id, true, apply_filters( 'pmpro_checkout_signon_secure', force_ssl_admin() ) );
// Update nonce value to be for this new user when we load the checkout page.
add_filter( 'pmpro_update_nonce_at_checkout', '__return_true' );
// Skip the account fields since we just created an account.
$skip_account_fields = true;
}
} else {
$user_id = $current_user->ID;
}
//no errors yet
if ( $pmpro_msgt != "pmpro_error" ) {
do_action( 'pmpro_checkout_before_processing' );
//process checkout if required
if ( $pmpro_requirebilling ) {
$morder = pmpro_build_order_for_checkout();
$pmpro_processed = $morder->process();
if ( ! empty( $pmpro_processed ) ) {
$pmpro_msg = __( "Payment accepted.", 'paid-memberships-pro' );
$pmpro_msgt = "pmpro_success";
$pmpro_confirmed = true;
} else {
/**
* Allow running code when processing fails.
*
* @since 2.7
* @param MemberOrder $morder The order object used at checkout.
*/
do_action( 'pmpro_checkout_processing_failed', $morder );
// Make sure we have an error message.
$pmpro_msg = !empty( $morder->error ) ? $morder->error : null;
if ( empty( $pmpro_msg ) ) {
$pmpro_msg = __( "Unknown error generating account. Please contact us to set up your membership.", 'paid-memberships-pro' );
}
if ( ! empty( $morder->error_type ) ) {
$pmpro_msgt = $morder->error_type;
} else {
$pmpro_msgt = "pmpro_error";
}
}
} else // !$pmpro_requirebilling
{
//must have been a free membership, continue
$pmpro_confirmed = true;
}
}
}
} //endif ($pmpro_continue_registration)
}
//make sure we have at least an empty morder here to avoid a warning
if ( empty( $morder ) ) {
$morder = false;
}
// Make sure that we have a User ID to avoid a warning.
if ( empty( $user_id ) && ! empty( $current_user->ID ) ) {
$user_id = $current_user->ID;
}
//Hook to check payment confirmation or replace it. If we get an array back, pull the values (morder) out
$pmpro_confirmed_data = apply_filters( 'pmpro_checkout_confirmed', $pmpro_confirmed, $morder );
/**
* @todo Refactor this to avoid using extract.
*/
if ( is_array( $pmpro_confirmed_data ) ) {
extract( $pmpro_confirmed_data );
} else {
$pmpro_confirmed = $pmpro_confirmed_data;
}
//if payment was confirmed create/update the user.
if ( ! empty( $pmpro_confirmed ) ) {
//just in case this hasn't been set yet
$submit = true;
do_action( 'pmpro_checkout_before_change_membership_level', $user_id, $morder );
if ( 'pmpro_error' !== $pmpro_msgt ) {
//start date is NOW() but filterable below
$startdate = current_time( "mysql" );
/**
* Filter the start date for the membership/subscription.
*
* @since 1.8.9
*
* @param string $startdate , datetime formatsted for MySQL (NOW() or YYYY-MM-DD)
* @param int $user_id , ID of the user checking out
* @param object $pmpro_level , object of level being checked out for
*/
$startdate = apply_filters( "pmpro_checkout_start_date", $startdate, $user_id, $pmpro_level );
//calculate the end date
if ( ! empty( $pmpro_level->expiration_number ) ) {
if( $pmpro_level->expiration_period == 'Hour' ){
$enddate = date( "Y-m-d H:i:s", strtotime( "+ " . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, current_time( "timestamp" ) ) );
} else {
$enddate = date( "Y-m-d 23:59:59", strtotime( "+ " . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, current_time( "timestamp" ) ) );
}
} else {
$enddate = "NULL";
}
/**
* Filter the end date for the membership/subscription.
*
* @since 1.8.9
*
* @param string $enddate , datetime formatsted for MySQL (YYYY-MM-DD)
* @param int $user_id , ID of the user checking out
* @param object $pmpro_level , object of level being checked out for
* @param string $startdate , startdate calculated above
*/
$enddate = apply_filters( "pmpro_checkout_end_date", $enddate, $user_id, $pmpro_level, $startdate );
//check code before adding it to the order
if ( ! empty( $discount_code ) ) {
if ( isset( $pmpro_checkout_level_ids ) ) {
$code_check = pmpro_checkDiscountCode( $discount_code, $pmpro_checkout_level_ids, true );
} else {
$code_check = pmpro_checkDiscountCode( $discount_code, $pmpro_level->id, true );
}
if ( $code_check[0] == false ) {
//error
$pmpro_msg = $code_check[1];
$pmpro_msgt = "pmpro_error";
//don't use this code
$use_discount_code = false;
} else {
//all okay
$use_discount_code = true;
}
//update membership_user table.
if ( ! empty( $discount_code ) && ! empty( $use_discount_code ) ) {
$discount_code_id = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql( $discount_code ) . "' LIMIT 1" );
} else {
$discount_code_id = "";
}
} else {
$discount_code_id = "";
}
$custom_level = array(
'user_id' => $user_id,
'membership_id' => $pmpro_level->id,
'code_id' => $discount_code_id,
'initial_payment' => pmpro_round_price( $pmpro_level->initial_payment ),
'billing_amount' => pmpro_round_price( $pmpro_level->billing_amount ),
'cycle_number' => $pmpro_level->cycle_number,
'cycle_period' => $pmpro_level->cycle_period,
'billing_limit' => $pmpro_level->billing_limit,
'trial_amount' => pmpro_round_price( $pmpro_level->trial_amount ),
'trial_limit' => $pmpro_level->trial_limit,
'startdate' => $startdate,
'enddate' => $enddate
);
if ( pmpro_changeMembershipLevel( $custom_level, $user_id, 'changed' ) ) {
//we're good
//blank order for free levels
if ( empty( $morder ) ) {
$morder = new MemberOrder();
$morder->InitialPayment = 0;
$morder->Email = $bemail;
$morder->gateway = 'free';
$morder->status = 'success';
$morder = apply_filters( "pmpro_checkout_order_free", $morder );
}
//add an item to the history table, cancel old subscriptions
if ( ! empty( $morder ) ) {
$morder->user_id = $user_id;
$morder->membership_id = $pmpro_level->id;
$morder->saveOrder();
}
//update the current user
global $current_user;
if ( ! $current_user->ID && $user->ID ) {
$current_user = $user;
} //in case the user just signed up
pmpro_set_current_user();
//add discount code use
if ( $discount_code && $use_discount_code ) {
if ( ! empty( $morder->id ) ) {
$code_order_id = $morder->id;
} else {
$code_order_id = "";
}
$wpdb->query( $wpdb->prepare(
"INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES(%d, %d, %d, %s)",
$discount_code_id,
$user_id,
$code_order_id,
current_time( "mysql" )
) );
do_action( 'pmpro_discount_code_used', $discount_code_id, $user_id, $code_order_id );
}
//save billing info etc, as user meta
$meta_keys = array();
$meta_values = array();
// Check if firstname and last name fields are set.
if ( ! empty( $bfirstname ) || ! empty( $blastname ) ) {
$meta_keys = array_merge( $meta_keys, array(
"pmpro_bfirstname",
"pmpro_blastname",
) );
$meta_values = array_merge( $meta_values, array(
$bfirstname,
$blastname,
) );
}
// Check if billing details are available, if not adjust the arrays.
if ( ! empty( $baddress1 ) ) {
$meta_keys = array_merge( $meta_keys, array(
"pmpro_baddress1",
"pmpro_baddress2",
"pmpro_bcity",
"pmpro_bstate",
"pmpro_bzipcode",
"pmpro_bcountry",
"pmpro_bphone",
"pmpro_bemail",
) );
$meta_values = array_merge( $meta_values, array(
$baddress1,
$baddress2,
$bcity,
$bstate,
$bzipcode,
$bcountry,
$bphone,
$bemail,
) );
}
pmpro_replaceUserMeta( $user_id, $meta_keys, $meta_values );
//save first and last name fields
if ( ! empty( $bfirstname ) ) {
$old_firstname = get_user_meta( $user_id, "first_name", true );
if ( empty( $old_firstname ) ) {
update_user_meta( $user_id, "first_name", $bfirstname );
}
}
if ( ! empty( $blastname ) ) {
$old_lastname = get_user_meta( $user_id, "last_name", true );
if ( empty( $old_lastname ) ) {
update_user_meta( $user_id, "last_name", $blastname );
}
}
if( $pmpro_level->expiration_period == 'Hour' ){
update_user_meta( $user_id, 'pmpro_disable_notifications', true );
}
//show the confirmation
$ordersaved = true;
//hook
do_action( "pmpro_after_checkout", $user_id, $morder ); //added $morder param in v2.0
$sendemails = apply_filters( "pmpro_send_checkout_emails", true);
if($sendemails) { // Send the emails only if the flag is set to true
//setup some values for the emails
if ( ! empty( $morder ) ) {
$invoice = new MemberOrder( $morder->id );
} else {
$invoice = null;
}
$current_user->membership_level = $pmpro_level; //make sure they have the right level info
//send email to member
$pmproemail = new PMProEmail();
$pmproemail->sendCheckoutEmail( $current_user, $invoice );
//send email to admin
$pmproemail = new PMProEmail();
$pmproemail->sendCheckoutAdminEmail( $current_user, $invoice );
}
//redirect to confirmation
$rurl = pmpro_url( "confirmation", "?pmpro_level=" . $pmpro_level->id );
$rurl = apply_filters( "pmpro_confirmation_url", $rurl, $user_id, $pmpro_level );
wp_redirect( $rurl );
exit;
} else {
//uh oh. we charged them then the membership creation failed
// test that the order object contains data
$test = (array) $morder;
if ( ! empty( $test ) && $morder->cancel() ) {
$pmpro_msg = __( "IMPORTANT: Something went wrong during membership creation. Your credit card authorized, but we cancelled the order immediately. You should not try to submit this form again. Please contact the site owner to fix this issue.", 'paid-memberships-pro' );
$morder = null;
} else {
$pmpro_msg = __( "IMPORTANT: Something went wrong during membership creation. Your credit card was charged, but we couldn't assign your membership. You should not submit this form again. Please contact the site owner to fix this issue.", 'paid-memberships-pro' );
}
}
}
}
//default values
if ( empty( $submit ) ) {
//show message if the payment gateway is not setup yet
if ( $pmpro_requirebilling && ! get_option( "pmpro_gateway" ) ) {
if ( pmpro_isAdmin() ) {
$pmpro_msg = sprintf( __( 'You must <a href="%s">set up a Payment Gateway</a> before any payments will be processed.', 'paid-memberships-pro' ), admin_url( 'admin.php?page=pmpro-paymentsettings' ) );
} else {
$pmpro_msg = __( "A Payment Gateway must be set up before any payments will be processed.", 'paid-memberships-pro' );
}
$pmpro_msgt = "";
}
//default values from DB
if ( ! empty( $current_user->ID ) ) {
$bfirstname = get_user_meta( $current_user->ID, "pmpro_bfirstname", true );
$blastname = get_user_meta( $current_user->ID, "pmpro_blastname", true );
$baddress1 = get_user_meta( $current_user->ID, "pmpro_baddress1", true );
$baddress2 = get_user_meta( $current_user->ID, "pmpro_baddress2", true );
$bcity = get_user_meta( $current_user->ID, "pmpro_bcity", true );
$bstate = get_user_meta( $current_user->ID, "pmpro_bstate", true );
$bzipcode = get_user_meta( $current_user->ID, "pmpro_bzipcode", true );
$bcountry = get_user_meta( $current_user->ID, "pmpro_bcountry", true );
$bphone = get_user_meta( $current_user->ID, "pmpro_bphone", true );
$bemail = get_user_meta( $current_user->ID, "pmpro_bemail", true );
$bconfirmemail = $bemail; //as of 1.7.5, just setting to bemail
}
}
// Preventing conflicts with old checkout templates that depend on the $pmpro_level global being set.
pmpro_getAllLevels();
/**
* Hook to run actions after the checkout preheader is loaded.
* @since 2.1
*/
do_action( 'pmpro_after_checkout_preheader', $morder );