class-wc-gateway-stripe.php
41.9 KB
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Gateway_Stripe class.
*
* @extends WC_Payment_Gateway
*/
class WC_Gateway_Stripe extends WC_Stripe_Payment_Gateway {
const ID = 'stripe';
/**
* Should we capture Credit cards
*
* @var bool
*/
public $capture;
/**
* Alternate credit card statement name
*
* @var bool
*/
public $statement_descriptor;
/**
* Should we store the users credit cards?
*
* @var bool
*/
public $saved_cards;
/**
* API access secret key
*
* @var string
*/
public $secret_key;
/**
* Api access publishable key
*
* @var string
*/
public $publishable_key;
/**
* Do we accept Payment Request?
*
* @var bool
*/
public $payment_request;
/**
* Is test mode active?
*
* @var bool
*/
public $testmode;
/**
* Inline CC form styling
*
* @var string
*/
public $inline_cc_form;
/**
* Constructor
*/
public function __construct() {
$this->id = self::ID;
$this->method_title = __( 'Stripe', 'woocommerce-gateway-stripe' );
/* translators: 1) link to Stripe register page 2) link to Stripe api keys page */
$this->method_description = __( 'Stripe works by adding payment fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' );
$this->has_fields = true;
$this->supports = [
'products',
'refunds',
'tokenization',
'add_payment_method',
];
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Check if subscriptions are enabled and add support for them.
$this->maybe_init_subscriptions();
// Check if pre-orders are enabled and add support for them.
$this->maybe_init_pre_orders();
// Get setting values.
$this->title = $this->get_validated_option( 'title' );
$this->description = $this->get_validated_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = 'yes' === $this->get_option( 'testmode' );
$this->inline_cc_form = 'yes' === $this->get_option( 'inline_cc_form' );
$this->capture = 'yes' === $this->get_option( 'capture', 'yes' );
$this->statement_descriptor = WC_Stripe_Helper::clean_statement_descriptor( $this->get_validated_option( 'statement_descriptor' ) );
$this->saved_cards = 'yes' === $this->get_option( 'saved_cards' );
$this->secret_key = $this->testmode ? $this->get_validated_option( 'test_secret_key' ) : $this->get_validated_option( 'secret_key' );
$this->publishable_key = $this->testmode ? $this->get_validated_option( 'test_publishable_key' ) : $this->get_validated_option( 'publishable_key' );
$this->payment_request = 'yes' === $this->get_option( 'payment_request', 'yes' );
WC_Stripe_API::set_secret_key( $this->secret_key );
// Hooks.
add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
add_action( 'woocommerce_admin_order_totals_after_total', [ $this, 'display_order_fee' ] );
add_action( 'woocommerce_admin_order_totals_after_total', [ $this, 'display_order_payout' ], 20 );
add_action( 'woocommerce_customer_save_address', [ $this, 'show_update_card_notice' ], 10, 2 );
add_filter( 'woocommerce_available_payment_gateways', [ $this, 'prepare_order_pay_page' ] );
add_action( 'woocommerce_account_view-order_endpoint', [ $this, 'check_intent_status_on_order_page' ], 1 );
add_filter( 'woocommerce_payment_successful_result', [ $this, 'modify_successful_payment_result' ], 99999, 2 );
add_action( 'set_logged_in_cookie', [ $this, 'set_cookie_on_current_request' ] );
add_filter( 'woocommerce_get_checkout_payment_url', [ $this, 'get_checkout_payment_url' ], 10, 2 );
add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, [ $this, 'settings_api_sanitized_fields' ] );
add_filter( 'woocommerce_gateway_' . $this->id . '_settings_values', [ $this, 'update_onboarding_settings' ] );
// Note: display error is in the parent class.
add_action( 'admin_notices', [ $this, 'display_errors' ], 9999 );
}
/**
* Checks if gateway should be available to use.
*
* @since 4.0.2
*/
public function is_available() {
if ( is_add_payment_method_page() && ! $this->saved_cards ) {
return false;
}
return parent::is_available();
}
/**
* Adds a notice for customer when they update their billing address.
*
* @since 4.1.0
* @param int $user_id The ID of the current user.
* @param string $load_address The address to load.
*/
public function show_update_card_notice( $user_id, $load_address ) {
if ( ! $this->saved_cards || ! WC_Stripe_Payment_Tokens::customer_has_saved_methods( $user_id ) || 'billing' !== $load_address ) {
return;
}
/* translators: 1) Opening anchor tag 2) closing anchor tag */
wc_add_notice( sprintf( __( 'If your billing address has been changed for saved payment methods, be sure to remove any %1$ssaved payment methods%2$s on file and re-add them.', 'woocommerce-gateway-stripe' ), '<a href="' . esc_url( wc_get_endpoint_url( 'payment-methods' ) ) . '" class="wc-stripe-update-card-notice" style="text-decoration:underline;">', '</a>' ), 'notice' );
}
/**
* Get_icon function.
*
* @since 1.0.0
* @version 5.6.2
* @return string|null
*/
public function get_icon() {
return apply_filters( 'woocommerce_gateway_icon', null, $this->id );
}
/**
* Initialise Gateway Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = require dirname( __FILE__ ) . '/admin/stripe-settings.php';
unset( $this->form_fields['title_upe'] );
}
/**
* Payment form on checkout page
*/
public function payment_fields() {
global $wp;
$user = wp_get_current_user();
$display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards;
$total = WC()->cart->total;
$user_email = '';
$description = $this->get_description();
$description = ! empty( $description ) ? $description : '';
$firstname = '';
$lastname = '';
// If paying from order, we need to get total from order not cart.
if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) { // wpcs: csrf ok.
$order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); // wpcs: csrf ok, sanitization ok.
$total = $order->get_total();
$user_email = $order->get_billing_email();
} else {
if ( $user->ID ) {
$user_email = get_user_meta( $user->ID, 'billing_email', true );
$user_email = $user_email ? $user_email : $user->user_email;
}
}
if ( is_add_payment_method_page() ) {
$firstname = $user->user_firstname;
$lastname = $user->user_lastname;
}
ob_start();
echo '<div
id="stripe-payment-data"
data-email="' . esc_attr( $user_email ) . '"
data-full-name="' . esc_attr( $firstname . ' ' . $lastname ) . '"
data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '"
>';
if ( $this->testmode ) {
/* translators: link to Stripe testing page */
$description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the <a href="%s" target="_blank">Testing Stripe documentation</a> for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' );
}
$description = trim( $description );
echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id ); // wpcs: xss ok.
if ( $display_tokenization ) {
$this->tokenization_script();
$this->saved_payment_methods();
}
$this->elements_form();
if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) { // wpcs: csrf ok.
$this->save_payment_method_checkbox();
}
do_action( 'wc_stripe_payment_fields_stripe', $this->id );
echo '</div>';
ob_end_flush();
}
/**
* Renders the Stripe elements form.
*
* @since 4.0.0
* @version 4.0.0
*/
public function elements_form() {
?>
<fieldset id="wc-<?php echo esc_attr( $this->id ); ?>-cc-form" class="wc-credit-card-form wc-payment-form" style="background:transparent;">
<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
<?php if ( $this->inline_cc_form ) { ?>
<label for="card-element">
<?php esc_html_e( 'Credit or debit card', 'woocommerce-gateway-stripe' ); ?>
</label>
<div id="stripe-card-element" class="wc-stripe-elements-field">
<!-- a Stripe Element will be inserted here. -->
</div>
<?php } else { ?>
<div class="form-row form-row-wide">
<label for="stripe-card-element"><?php esc_html_e( 'Card Number', 'woocommerce-gateway-stripe' ); ?> <span class="required">*</span></label>
<div class="stripe-card-group">
<div id="stripe-card-element" class="wc-stripe-elements-field">
<!-- a Stripe Element will be inserted here. -->
</div>
<i class="stripe-credit-card-brand stripe-card-brand" alt="Credit Card"></i>
</div>
</div>
<div class="form-row form-row-first">
<label for="stripe-exp-element"><?php esc_html_e( 'Expiry Date', 'woocommerce-gateway-stripe' ); ?> <span class="required">*</span></label>
<div id="stripe-exp-element" class="wc-stripe-elements-field">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
<div class="form-row form-row-last">
<label for="stripe-cvc-element"><?php esc_html_e( 'Card Code (CVC)', 'woocommerce-gateway-stripe' ); ?> <span class="required">*</span></label>
<div id="stripe-cvc-element" class="wc-stripe-elements-field">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
<div class="clear"></div>
<?php } ?>
<!-- Used to display form errors -->
<div class="stripe-source-errors" role="alert"></div>
<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
<div class="clear"></div>
</fieldset>
<?php
}
/**
* Override the parent admin_options method.
*/
public function admin_options() {
do_action( 'wc_stripe_gateway_admin_options_wrapper', $this );
}
/**
* Completes an order without a positive value.
*
* @since 4.2.0
* @param WC_Order $order The order to complete.
* @param WC_Order $prepared_source Payment source and customer data.
* @param boolean $force_save_source Whether the payment source must be saved, like when dealing with a Subscription setup.
* @return array Redirection data for `process_payment`.
*/
public function complete_free_order( $order, $prepared_source, $force_save_source ) {
if ( $force_save_source ) {
$intent_secret = $this->setup_intent( $order, $prepared_source );
if ( ! empty( $intent_secret ) ) {
// `get_return_url()` must be called immediately before returning a value.
return [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
'setup_intent_secret' => $intent_secret,
];
}
}
// Remove cart.
WC()->cart->empty_cart();
$order->payment_complete();
// Return thank you page redirect.
return [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
];
}
/**
* Process the payment
*
* @since 1.0.0
* @since 4.1.0 Add 4th parameter to track previous error.
* @version 5.6.0
*
* @param int $order_id Reference.
* @param bool $retry Should we retry on fail.
* @param bool $force_save_source Force save the payment source.
* @param mix $previous_error Any error message from previous request.
* @param bool $use_order_source Whether to use the source, which should already be attached to the order.
*
* @throws Exception If payment will not be accepted.
* @return array|void
*/
public function process_payment( $order_id, $retry = true, $force_save_source = false, $previous_error = false, $use_order_source = false ) {
try {
$order = wc_get_order( $order_id );
if ( $this->has_subscription( $order_id ) ) {
$force_save_source = true;
}
if ( $this->maybe_change_subscription_payment_method( $order_id ) ) {
return $this->process_change_subscription_payment_method( $order_id );
}
if ( $this->maybe_process_pre_orders( $order_id ) ) {
return $this->process_pre_order( $order_id );
}
// Check whether there is an existing intent.
$intent = $this->get_intent_from_order( $order );
if ( isset( $intent->object ) && 'setup_intent' === $intent->object ) {
$intent = false; // This function can only deal with *payment* intents
}
$stripe_customer_id = null;
if ( $intent && ! empty( $intent->customer ) ) {
$stripe_customer_id = $intent->customer;
}
// For some payments the source should already be present in the order.
if ( $use_order_source ) {
$prepared_source = $this->prepare_order_source( $order );
} else {
$prepared_source = $this->prepare_source( get_current_user_id(), $force_save_source, $stripe_customer_id );
}
// If we are using a saved payment method that is PaymentMethod (pm_) and not a Source (src_) we need to use
// the process_payment() from the UPE gateway which uses the PaymentMethods API instead of Sources API.
// This happens when using a saved payment method that was added with the UPE gateway.
if ( $this->is_using_saved_payment_method() && ! empty( $prepared_source->source ) && substr( $prepared_source->source, 0, 3 ) === 'pm_' ) {
$upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
return $upe_gateway->process_payment_with_saved_payment_method( $order_id );
}
$this->maybe_disallow_prepaid_card( $prepared_source->source_object );
$this->check_source( $prepared_source );
$this->save_source_to_order( $order, $prepared_source );
if ( 0 >= $order->get_total() ) {
return $this->complete_free_order( $order, $prepared_source, $force_save_source );
}
// This will throw exception if not valid.
$this->validate_minimum_order_amount( $order );
WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" );
if ( $intent ) {
$intent = $this->update_existing_intent( $intent, $order, $prepared_source );
} else {
$intent = $this->create_intent( $order, $prepared_source );
}
// Confirm the intent after locking the order to make sure webhooks will not interfere.
if ( empty( $intent->error ) ) {
$this->lock_order_payment( $order, $intent );
$intent = $this->confirm_intent( $intent, $order, $prepared_source );
}
$force_save_source_value = apply_filters( 'wc_stripe_force_save_source', $force_save_source, $prepared_source->source );
if ( ! empty( $intent->error ) ) {
$this->maybe_remove_non_existent_customer( $intent->error, $order );
// We want to retry.
if ( $this->is_retryable_error( $intent->error ) ) {
return $this->retry_after_error( $intent, $order, $retry, $force_save_source, $previous_error, $use_order_source );
}
$this->unlock_order_payment( $order );
$this->throw_localized_message( $intent, $order );
}
if ( 'succeeded' === $intent->status && ! $this->is_using_saved_payment_method() && ( $this->save_payment_method_requested() || $force_save_source_value ) ) {
$this->save_payment_method( $prepared_source->source_object );
}
if ( ! empty( $intent ) ) {
// Use the last charge within the intent to proceed.
$response = end( $intent->charges->data );
// If the intent requires a 3DS flow, redirect to it.
if ( 'requires_action' === $intent->status ) {
$this->unlock_order_payment( $order );
if ( is_wc_endpoint_url( 'order-pay' ) ) {
$redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) );
return [
'result' => 'success',
'redirect' => $redirect_url,
];
} else {
/**
* This URL contains only a hash, which will be sent to `checkout.js` where it will be set like this:
* `window.location = result.redirect`
* Once this redirect is sent to JS, the `onHashChange` function will execute `handleCardPayment`.
*/
return [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
'payment_intent_secret' => $intent->client_secret,
'save_payment_method' => $this->save_payment_method_requested(),
];
}
}
}
// Process valid response.
$this->process_response( $response, $order );
// Remove cart.
if ( isset( WC()->cart ) ) {
WC()->cart->empty_cart();
}
// Unlock the order.
$this->unlock_order_payment( $order );
// Return thank you page redirect.
return [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
];
} catch ( WC_Stripe_Exception $e ) {
wc_add_notice( $e->getLocalizedMessage(), 'error' );
WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
/* translators: error message */
$order->update_status( 'failed' );
return [
'result' => 'fail',
'redirect' => '',
];
}
}
/**
* Saves payment method
*
* @param object $source_object
* @throws WC_Stripe_Exception
*/
public function save_payment_method( $source_object ) {
$user_id = get_current_user_id();
$customer = new WC_Stripe_Customer( $user_id );
if ( ( $user_id && 'reusable' === $source_object->usage ) ) {
$response = $customer->add_source( $source_object->id );
if ( ! empty( $response->error ) ) {
throw new WC_Stripe_Exception( print_r( $response, true ), $this->get_localized_error_message_from_response( $response ) );
}
if ( is_wp_error( $response ) ) {
throw new WC_Stripe_Exception( $response->get_error_message(), $response->get_error_message() );
}
}
}
/**
* Displays the Stripe fee
*
* @since 4.1.0
*
* @param int $order_id The ID of the order.
*/
public function display_order_fee( $order_id ) {
if ( apply_filters( 'wc_stripe_hide_display_order_fee', false, $order_id ) ) {
return;
}
$order = wc_get_order( $order_id );
$fee = WC_Stripe_Helper::get_stripe_fee( $order );
$currency = WC_Stripe_Helper::get_stripe_currency( $order );
if ( ! $fee || ! $currency ) {
return;
}
?>
<tr>
<td class="label stripe-fee">
<?php echo wc_help_tip( __( 'This represents the fee Stripe collects for the transaction.', 'woocommerce-gateway-stripe' ) ); // wpcs: xss ok. ?>
<?php esc_html_e( 'Stripe Fee:', 'woocommerce-gateway-stripe' ); ?>
</td>
<td width="1%"></td>
<td class="total">
-<?php echo wc_price( $fee, [ 'currency' => $currency ] ); // wpcs: xss ok. ?>
</td>
</tr>
<?php
}
/**
* Displays the net total of the transaction without the charges of Stripe.
*
* @since 4.1.0
*
* @param int $order_id The ID of the order.
*/
public function display_order_payout( $order_id ) {
if ( apply_filters( 'wc_stripe_hide_display_order_payout', false, $order_id ) ) {
return;
}
$order = wc_get_order( $order_id );
$net = WC_Stripe_Helper::get_stripe_net( $order );
$currency = WC_Stripe_Helper::get_stripe_currency( $order );
if ( ! $net || ! $currency ) {
return;
}
?>
<tr>
<td class="label stripe-payout">
<?php echo wc_help_tip( __( 'This represents the net total that will be credited to your Stripe bank account. This may be in the currency that is set in your Stripe account.', 'woocommerce-gateway-stripe' ) ); // wpcs: xss ok. ?>
<?php esc_html_e( 'Stripe Payout:', 'woocommerce-gateway-stripe' ); ?>
</td>
<td width="1%"></td>
<td class="total">
<?php echo wc_price( $net, [ 'currency' => $currency ] ); // wpcs: xss ok. ?>
</td>
</tr>
<?php
}
/**
* Retries the payment process once an error occured.
*
* @since 4.2.0
* @param object $response The response from the Stripe API.
* @param WC_Order $order An order that is being paid for.
* @param bool $retry A flag that indicates whether another retry should be attempted.
* @param bool $force_save_source Force save the payment source.
* @param mixed $previous_error Any error message from previous request.
* @param bool $use_order_source Whether to use the source, which should already be attached to the order.
* @throws WC_Stripe_Exception If the payment is not accepted.
* @return array|void
*/
public function retry_after_error( $response, $order, $retry, $force_save_source, $previous_error, $use_order_source ) {
if ( ! $retry ) {
$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
$order->add_order_note( $localized_message );
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.
}
// Don't do anymore retries after this.
if ( 5 <= $this->retry_interval ) {
return $this->process_payment( $order->get_id(), false, $force_save_source, $response->error, $previous_error );
}
sleep( $this->retry_interval );
$this->retry_interval++;
return $this->process_payment( $order->get_id(), true, $force_save_source, $response->error, $previous_error, $use_order_source );
}
/**
* Adds the necessary hooks to modify the "Pay for order" page in order to clean
* it up and prepare it for the Stripe PaymentIntents modal to confirm a payment.
*
* @since 4.2
* @param WC_Payment_Gateway[] $gateways A list of all available gateways.
* @return WC_Payment_Gateway[] Either the same list or an empty one in the right conditions.
*/
public function prepare_order_pay_page( $gateways ) {
if ( ! is_wc_endpoint_url( 'order-pay' ) || ! isset( $_GET['wc-stripe-confirmation'] ) ) { // wpcs: csrf ok.
return $gateways;
}
try {
$this->prepare_intent_for_order_pay_page();
} catch ( WC_Stripe_Exception $e ) {
// Just show the full order pay page if there was a problem preparing the Payment Intent
return $gateways;
}
add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
add_filter( 'woocommerce_pay_order_button_html', '__return_false' );
add_filter( 'woocommerce_available_payment_gateways', '__return_empty_array' );
add_filter( 'woocommerce_no_available_payment_methods_message', [ $this, 'change_no_available_methods_message' ] );
add_action( 'woocommerce_pay_order_after_submit', [ $this, 'render_payment_intent_inputs' ] );
return [];
}
/**
* Changes the text of the "No available methods" message to one that indicates
* the need for a PaymentIntent to be confirmed.
*
* @since 4.2
* @return string the new message.
*/
public function change_no_available_methods_message() {
return wpautop( __( "Almost there!\n\nYour order has already been created, the only thing that still needs to be done is for you to authorize the payment with your bank.", 'woocommerce-gateway-stripe' ) );
}
/**
* Prepares the Payment Intent for it to be completed in the "Pay for Order" page.
*
* @param WC_Order|null $order Order object, or null to get the order from the "order-pay" URL parameter
*
* @throws WC_Stripe_Exception
* @since 4.3
*/
public function prepare_intent_for_order_pay_page( $order = null ) {
if ( ! isset( $order ) || empty( $order ) ) {
$order = wc_get_order( absint( get_query_var( 'order-pay' ) ) );
}
$intent = $this->get_intent_from_order( $order );
if ( ! $intent ) {
throw new WC_Stripe_Exception(
'Payment Intent not found',
sprintf(
/* translators: %s is the order Id */
__( 'Payment Intent not found for order #%s', 'woocommerce-gateway-stripe' ),
$order->get_id()
)
);
}
if ( 'requires_payment_method' === $intent->status && isset( $intent->last_payment_error )
&& 'authentication_required' === $intent->last_payment_error->code ) {
$level3_data = $this->get_level3_data_from_order( $order );
$intent = WC_Stripe_API::request_with_level3_data(
[
'payment_method' => $intent->last_payment_error->source->id,
],
'payment_intents/' . $intent->id . '/confirm',
$level3_data,
$order
);
if ( isset( $intent->error ) ) {
throw new WC_Stripe_Exception( print_r( $intent, true ), $intent->error->message );
}
}
$this->order_pay_intent = $intent;
}
/**
* Renders hidden inputs on the "Pay for Order" page in order to let Stripe handle PaymentIntents.
*
* @param WC_Order|null $order Order object, or null to get the order from the "order-pay" URL parameter
*
* @throws WC_Stripe_Exception
* @since 4.2
*/
public function render_payment_intent_inputs( $order = null ) {
if ( ! isset( $order ) || empty( $order ) ) {
$order = wc_get_order( absint( get_query_var( 'order-pay' ) ) );
}
if ( ! isset( $this->order_pay_intent ) ) {
$this->prepare_intent_for_order_pay_page( $order );
}
$verification_url = add_query_arg(
[
'order' => $order->get_id(),
'nonce' => wp_create_nonce( 'wc_stripe_confirm_pi' ),
'redirect_to' => rawurlencode( $this->get_return_url( $order ) ),
'is_pay_for_order' => true,
],
WC_AJAX::get_endpoint( 'wc_stripe_verify_intent' )
);
echo '<input type="hidden" id="stripe-intent-id" value="' . esc_attr( $this->order_pay_intent->client_secret ) . '" />';
echo '<input type="hidden" id="stripe-intent-return" value="' . esc_attr( $verification_url ) . '" />';
}
/**
* Adds an error message wrapper to each saved method.
*
* @since 4.2.0
* @param WC_Payment_Token $token Payment Token.
* @return string Generated payment method HTML
*/
public function get_saved_payment_method_option_html( $token ) {
$html = parent::get_saved_payment_method_option_html( $token );
$error_wrapper = '<div class="stripe-source-errors" role="alert"></div>';
return preg_replace( '~</(\w+)>\s*$~', "$error_wrapper</$1>", $html );
}
/**
* Attempt to manually complete the payment process for orders, which are still pending
* before displaying the View Order page. This is useful in case webhooks have not been set up.
*
* @since 4.2.0
* @param int $order_id The ID that will be used for the thank you page.
*/
public function check_intent_status_on_order_page( $order_id ) {
if ( empty( $order_id ) || absint( $order_id ) <= 0 ) {
return;
}
$order = wc_get_order( absint( $order_id ) );
if ( ! $order ) {
return;
}
$this->verify_intent_after_checkout( $order );
}
/**
* Attached to `woocommerce_payment_successful_result` with a late priority,
* this method will combine the "naturally" generated redirect URL from
* WooCommerce and a payment/setup intent secret into a hash, which contains both
* the secret, and a proper URL, which will confirm whether the intent succeeded.
*
* @since 4.2.0
* @param array $result The result from `process_payment`.
* @param int $order_id The ID of the order which is being paid for.
* @return array
*/
public function modify_successful_payment_result( $result, $order_id ) {
if ( ! isset( $result['payment_intent_secret'] ) && ! isset( $result['setup_intent_secret'] ) ) {
// Only redirects with intents need to be modified.
return $result;
}
// Put the final thank you page redirect into the verification URL.
$query_params = [
'order' => $order_id,
'nonce' => wp_create_nonce( 'wc_stripe_confirm_pi' ),
'redirect_to' => rawurlencode( $result['redirect'] ),
];
$force_save_source_value = apply_filters( 'wc_stripe_force_save_source', false );
if ( $this->save_payment_method_requested() || $force_save_source_value ) {
$query_params['save_payment_method'] = true;
}
$verification_url = add_query_arg( $query_params, WC_AJAX::get_endpoint( 'wc_stripe_verify_intent' ) );
if ( isset( $result['payment_intent_secret'] ) ) {
$redirect = sprintf( '#confirm-pi-%s:%s', $result['payment_intent_secret'], rawurlencode( $verification_url ) );
} elseif ( isset( $result['setup_intent_secret'] ) ) {
$redirect = sprintf( '#confirm-si-%s:%s', $result['setup_intent_secret'], rawurlencode( $verification_url ) );
}
return [
'result' => 'success',
'redirect' => $redirect,
];
}
/**
* Proceed with current request using new login session (to ensure consistent nonce).
*/
public function set_cookie_on_current_request( $cookie ) {
$_COOKIE[ LOGGED_IN_COOKIE ] = $cookie;
}
/**
* Executed between the "Checkout" and "Thank you" pages, this
* method updates orders based on the status of associated PaymentIntents.
*
* @since 4.2.0
* @param WC_Order $order The order which is in a transitional state.
*/
public function verify_intent_after_checkout( $order ) {
$payment_method = $order->get_payment_method();
if ( $payment_method !== $this->id ) {
// If this is not the payment method, an intent would not be available.
return;
}
$intent = $this->get_intent_from_order( $order );
if ( ! $intent ) {
// No intent, redirect to the order received page for further actions.
return;
}
// A webhook might have modified or locked the order while the intent was retreived. This ensures we are reading the right status.
clean_post_cache( $order->get_id() );
$order = wc_get_order( $order->get_id() );
if ( ! $order->has_status(
apply_filters(
'wc_stripe_allowed_payment_processing_statuses',
[ 'pending', 'failed' ],
$order
)
) ) {
// If payment has already been completed, this function is redundant.
return;
}
if ( $this->lock_order_payment( $order, $intent ) ) {
return;
}
if ( 'setup_intent' === $intent->object && 'succeeded' === $intent->status ) {
WC()->cart->empty_cart();
if ( $this->has_pre_order( $order ) ) {
$this->mark_order_as_pre_ordered( $order );
} else {
$order->payment_complete();
}
} elseif ( 'succeeded' === $intent->status || 'requires_capture' === $intent->status ) {
// Proceed with the payment completion.
$this->handle_intent_verification_success( $order, $intent );
} elseif ( 'requires_payment_method' === $intent->status ) {
// `requires_payment_method` means that SCA got denied for the current payment method.
$this->handle_intent_verification_failure( $order, $intent );
}
$this->unlock_order_payment( $order );
}
/**
* Called after an intent verification succeeds, this allows
* specific APNs or children of this class to modify its behavior.
*
* @param WC_Order $order The order whose verification succeeded.
* @param stdClass $intent The Payment Intent object.
*/
protected function handle_intent_verification_success( $order, $intent ) {
$this->process_response( end( $intent->charges->data ), $order );
$this->maybe_process_subscription_early_renewal_success( $order, $intent );
}
/**
* Called after an intent verification fails, this allows
* specific APNs or children of this class to modify its behavior.
*
* @param WC_Order $order The order whose verification failed.
* @param stdClass $intent The Payment Intent object.
*/
protected function handle_intent_verification_failure( $order, $intent ) {
$this->failed_sca_auth( $order, $intent );
$this->maybe_process_subscription_early_renewal_failure( $order, $intent );
}
/**
* Checks if the payment intent associated with an order failed and records the event.
*
* @since 4.2.0
* @param WC_Order $order The order which should be checked.
* @param object $intent The intent, associated with the order.
*/
public function failed_sca_auth( $order, $intent ) {
// If the order has already failed, do not repeat the same message.
if ( $order->has_status( 'failed' ) ) {
return;
}
// Load the right message and update the status.
$status_message = isset( $intent->last_payment_error )
/* translators: 1) The error message that was received from Stripe. */
? sprintf( __( 'Stripe SCA authentication failed. Reason: %s', 'woocommerce-gateway-stripe' ), $intent->last_payment_error->message )
: __( 'Stripe SCA authentication failed.', 'woocommerce-gateway-stripe' );
$order->update_status( 'failed', $status_message );
}
/**
* Preserves the "wc-stripe-confirmation" URL parameter so the user can complete the SCA authentication after logging in.
*
* @param string $pay_url Current computed checkout URL for the given order.
* @param WC_Order $order Order object.
*
* @return string Checkout URL for the given order.
*/
public function get_checkout_payment_url( $pay_url, $order ) {
global $wp;
if ( isset( $_GET['wc-stripe-confirmation'] ) && isset( $wp->query_vars['order-pay'] ) && $wp->query_vars['order-pay'] == $order->get_id() ) {
$pay_url = add_query_arg( 'wc-stripe-confirmation', 1, $pay_url );
}
return $pay_url;
}
/**
* Checks whether new keys are being entered when saving options.
*/
public function process_admin_options() {
// Load all old values before the new settings get saved.
$old_publishable_key = $this->get_option( 'publishable_key' );
$old_secret_key = $this->get_option( 'secret_key' );
$old_test_publishable_key = $this->get_option( 'test_publishable_key' );
$old_test_secret_key = $this->get_option( 'test_secret_key' );
parent::process_admin_options();
// Load all old values after the new settings have been saved.
$new_publishable_key = $this->get_option( 'publishable_key' );
$new_secret_key = $this->get_option( 'secret_key' );
$new_test_publishable_key = $this->get_option( 'test_publishable_key' );
$new_test_secret_key = $this->get_option( 'test_secret_key' );
// Checks whether a value has transitioned from a non-empty value to a new one.
$has_changed = function( $old_value, $new_value ) {
return ! empty( $old_value ) && ( $old_value !== $new_value );
};
// Look for updates.
if (
$has_changed( $old_publishable_key, $new_publishable_key )
|| $has_changed( $old_secret_key, $new_secret_key )
|| $has_changed( $old_test_publishable_key, $new_test_publishable_key )
|| $has_changed( $old_test_secret_key, $new_test_secret_key )
) {
update_option( 'wc_stripe_show_changed_keys_notice', 'yes' );
}
}
public function validate_publishable_key_field( $key, $value ) {
$value = $this->validate_text_field( $key, $value );
if ( ! empty( $value ) && ! preg_match( '/^pk_live_/', $value ) ) {
return '';
}
return $value;
}
public function validate_secret_key_field( $key, $value ) {
$value = $this->validate_text_field( $key, $value );
if ( ! empty( $value ) && ! preg_match( '/^[rs]k_live_/', $value ) ) {
return '';
}
return $value;
}
public function validate_test_publishable_key_field( $key, $value ) {
$value = $this->validate_text_field( $key, $value );
if ( ! empty( $value ) && ! preg_match( '/^pk_test_/', $value ) ) {
return '';
}
return $value;
}
public function validate_test_secret_key_field( $key, $value ) {
$value = $this->validate_text_field( $key, $value );
if ( ! empty( $value ) && ! preg_match( '/^[rs]k_test_/', $value ) ) {
return '';
}
return $value;
}
/**
* Ensures the statement descriptor about to be saved to options does not contain any invalid characters.
*
* @since 4.8.0
* @param $settings WC_Settings_API settings to be filtered
* @return Filtered settings
*/
public function settings_api_sanitized_fields( $settings ) {
if ( is_array( $settings ) ) {
if ( array_key_exists( 'statement_descriptor', $settings ) ) {
$settings['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $settings['statement_descriptor'] );
}
}
return $settings;
}
/**
* Checks whether the gateway is enabled.
*
* @return bool The result.
*/
public function is_enabled() {
return 'yes' === $this->get_option( 'enabled' );
}
/**
* Disables gateway.
*/
public function disable() {
$this->update_option( 'enabled', 'no' );
}
/**
* Enables gateway.
*/
public function enable() {
$this->update_option( 'enabled', 'yes' );
}
/**
* Returns whether test_mode is active for the gateway.
*
* @return boolean Test mode enabled if true, disabled if false.
*/
public function is_in_test_mode() {
return 'yes' === $this->get_option( 'testmode' );
}
/**
* Determines whether the "automatic" or "manual" capture setting is enabled.
*
* @return bool
*/
public function is_automatic_capture_enabled() {
return empty( $this->get_option( 'capture' ) ) || $this->get_option( 'capture' ) === 'yes';
}
/**
* Validates statement descriptor value
*
* @param string $param Param name.
* @param string $value Posted Value.
* @param int $max_length Maximum statement length.
*
* @return string Sanitized statement descriptor.
* @throws InvalidArgumentException When statement descriptor is invalid.
*/
public function validate_account_statement_descriptor_field( $param, $value, $max_length ) {
// Since the value is escaped, and we are saving in a place that does not require escaping, apply stripslashes.
$value = trim( stripslashes( $value ) );
$field = __( 'customer bank statement', 'woocommerce-gateway-stripe' );
if ( 'short_statement_descriptor' === $param ) {
$field = __( 'shortened customer bank statement', 'woocommerce-gateway-stripe' );
}
// Validation can be done with a single regex but splitting into multiple for better readability.
$valid_length = '/^.{5,' . $max_length . '}$/';
$has_one_letter = '/^.*[a-zA-Z]+/';
$no_specials = '/^[^*"\'<>]*$/';
if (
! preg_match( $valid_length, $value ) ||
! preg_match( $has_one_letter, $value ) ||
! preg_match( $no_specials, $value )
) {
throw new InvalidArgumentException(
sprintf(
/* translators: %1 field name, %2 Number of the maximum characters allowed */
__( 'The %1$s is invalid. The bank statement must contain only Latin characters, be between 5 and %2$u characters, contain at least one letter, and not contain any of the special characters: \' " * < >', 'woocommerce-gateway-stripe' ),
$field,
$max_length
)
);
}
return $value;
}
/**
* Get required setting keys for setup.
*
* @return array Array of setting keys used for setup.
*/
public function get_required_settings_keys() {
return [ 'publishable_key', 'secret_key' ];
}
/**
* Get the connection URL.
*
* @return string Connection URL.
*/
public function get_connection_url( $return_url = '' ) {
$api = new WC_Stripe_Connect_API();
$connect = new WC_Stripe_Connect( $api );
$url = $connect->get_oauth_url( $return_url );
return is_wp_error( $url ) ? null : $url;
}
/**
* Get help text to display during quick setup.
*
* @return string
*/
public function get_setup_help_text() {
return sprintf(
/* translators: %1$s Link to Stripe API details, %2$s Link to register a Stripe account */
__( 'Your API details can be obtained from your <a href="%1$s">Stripe account</a>. Don’t have a Stripe account? <a href="%2$s">Create one.</a>', 'woocommerce-gateway-stripe' ),
'https://dashboard.stripe.com/apikeys',
'https://dashboard.stripe.com/register'
);
}
/**
* Determine if the gateway still requires setup.
*
* @return bool
*/
public function needs_setup() {
return ! $this->get_option( 'publishable_key' ) || ! $this->get_option( 'secret_key' );
}
/**
* Updates the test mode based on keys provided when setting up the gateway via onboarding.
*
* @return array
*/
public function update_onboarding_settings( $settings ) {
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) {
return;
}
parse_str( wp_parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY ), $queries ); // phpcs:ignore sanitization ok.
// Determine if merchant is onboarding (page='wc-admin' and task='payments').
if (
! isset( $queries ) ||
! isset( $queries['page'] ) ||
! isset( $queries['task'] ) ||
'wc-admin' !== $queries['page'] ||
'payments' !== $queries['task']
) {
return;
}
if ( ! empty( $settings['publishable_key'] ) && ! empty( $settings['secret_key'] ) ) {
if ( strpos( $settings['publishable_key'], 'pk_test_' ) === 0 || strpos( $settings['secret_key'], 'sk_test_' ) === 0 ) {
$settings['test_publishable_key'] = $settings['publishable_key'];
$settings['test_secret_key'] = $settings['secret_key'];
unset( $settings['publishable_key'] );
unset( $settings['secret_key'] );
$settings['testmode'] = 'yes';
} else {
$settings['testmode'] = 'no';
}
}
return $settings;
}
/**
* Validates a field value before updating.
*
* @param string $field_key the form field key.
* @param string $field_value the form field value.
*
* @return bool True if the value was updated, false otherwise.
*/
public function update_validated_option( $field_key, $field_value ) {
$validated_field_value = $this->validate_field( $field_key, $field_value );
return $this->update_option( $field_key, $validated_field_value );
}
/**
* Retrieves validated field value.
*
* @param string $field_key the form field key.
* @param mixed $empty_value fallback value.
*
* @return string validated field value.
*/
public function get_validated_option( $field_key, $empty_value = null ) {
$value = parent::get_option( $field_key, $empty_value );
return $this->validate_field( $field_key, $value );
}
/**
* Ensures validated field values.
*
* @param string $field_key the form field key.
* @param string $field_value the form field value.
*
* @return string validated field value.
*/
private function validate_field( $field_key, $field_value ) {
if ( is_callable( [ $this, 'validate_' . $field_key . '_field' ] ) ) {
return $this->{'validate_' . $field_key . '_field'}( $field_key, $field_value );
}
if ( empty( $this->form_fields ) ) {
$this->init_form_fields();
}
if ( key_exists( $field_key, $this->form_fields ) ) {
$field_type = $this->form_fields[ $field_key ]['type'];
if ( is_callable( [ $this, 'validate_' . $field_type . '_field' ] ) ) {
return $this->{'validate_' . $field_type . '_field'}( $field_key, $field_value );
}
}
return $this->validate_text_field( $field_key, $field_value );
}
}