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
|
<?php /** * Checkout Shortcode * * Used on the checkout page, the checkout shortcode displays the checkout process. * * @package WooCommerce\Shortcodes\Checkout * @version 2.0.0 */
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Enums\OrderStatus; use Automattic\WooCommerce\Internal\Utilities\Users;
/** * Shortcode checkout class. */ class WC_Shortcode_Checkout {
/** * Get the shortcode content. * * @param array $atts Shortcode attributes. * @return string */ public static function get( $atts ) { return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); }
/** * Output the shortcode. * * @param array $atts Shortcode attributes. */ public static function output( $atts ) { global $wp;
// Check cart class is loaded or abort. if ( is_null( WC()->cart ) ) { return; }
// Backwards compatibility with old pay and thanks link arguments. if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) { // WPCS: input var ok, CSRF ok. wc_deprecated_argument( __CLASS__ . '->' . __FUNCTION__, '2.1', '"order" is no longer used to pass an order ID. Use the order-pay or order-received endpoint instead.' );
// Get the order to work out what we are showing. $order_id = absint( $_GET['order'] ); // WPCS: input var ok. $order = wc_get_order( $order_id );
if ( $order && $order->has_status( OrderStatus::PENDING ) ) { $wp->query_vars['order-pay'] = absint( $_GET['order'] ); // WPCS: input var ok. } else { $wp->query_vars['order-received'] = absint( $_GET['order'] ); // WPCS: input var ok. } }
// Handle checkout actions. if ( ! empty( $wp->query_vars['order-pay'] ) ) {
self::order_pay( $wp->query_vars['order-pay'] );
} elseif ( isset( $wp->query_vars['order-received'] ) ) {
self::order_received( $wp->query_vars['order-received'] );
} else {
self::checkout();
} }
/** * Show the pay page. * * @throws Exception When validate fails. * @param int $order_id Order ID. */ private static function order_pay( $order_id ) {
do_action( 'before_woocommerce_pay' );
$order_id = absint( $order_id );
// Pay for existing order. if ( isset( $_GET['pay_for_order'], $_GET['key'] ) && $order_id ) { // WPCS: input var ok, CSRF ok. try { $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok. $order = wc_get_order( $order_id );
// Order or payment link is invalid. if ( ! $order || $order->get_id() !== $order_id || ! hash_equals( $order->get_order_key(), $order_key ) ) { throw new Exception( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ) ); }
// Logged out customer does not have permission to pay for this order. if ( ! current_user_can( 'pay_for_order', $order_id ) && ! is_user_logged_in() ) { wc_print_notice( esc_html__( 'Please log in to your account below to continue to the payment form.', 'woocommerce' ), 'notice' ); woocommerce_login_form( array( 'redirect' => $order->get_checkout_payment_url(), ) ); return; }
// Add notice if logged in customer is trying to pay for guest order. if ( ! $order->get_user_id() && is_user_logged_in() ) { // If order has does not have same billing email then current logged in user then show warning. if ( $order->get_billing_email() !== wp_get_current_user()->user_email ) { wc_print_notice( __( 'You are paying for a guest order. Please continue with payment only if you recognize this order.', 'woocommerce' ), 'error' ); } }
// Logged in customer trying to pay for someone else's order. if ( ! current_user_can( 'pay_for_order', $order_id ) ) { throw new Exception( __( 'This order cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ) ); }
// Does not need payment. if ( ! $order->needs_payment() ) { /* translators: %s: order status */ throw new Exception( sprintf( __( 'This order’s status is “%s”—it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), wc_get_order_status_name( $order->get_status() ) ) ); }
// Ensure order items are still stocked if paying for a failed order. Pending orders do not need this check because stock is held. if ( ! $order->has_status( wc_get_is_pending_statuses() ) ) { $quantities = array();
foreach ( $order->get_items() as $item_key => $item ) { if ( $item && is_callable( array( $item, 'get_product' ) ) ) { $product = $item->get_product();
if ( ! $product ) { continue; }
$quantities[ $product->get_stock_managed_by_id() ] = isset( $quantities[ $product->get_stock_managed_by_id() ] ) ? $quantities[ $product->get_stock_managed_by_id() ] + $item->get_quantity() : $item->get_quantity(); } }
// Stock levels may already have been adjusted for this order (in which case we don't need to worry about checking for low stock). if ( ! $order->get_data_store()->get_stock_reduced( $order->get_id() ) ) { foreach ( $order->get_items() as $item_key => $item ) { if ( $item && is_callable( array( $item, 'get_product' ) ) ) { $product = $item->get_product();
if ( ! $product ) { continue; }
if ( ! apply_filters( 'woocommerce_pay_order_product_in_stock', $product->is_in_stock(), $product, $order ) ) { /* translators: %s: product name */ throw new Exception( sprintf( __( 'Sorry, "%s" is no longer in stock so this order cannot be paid for. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name() ) ); }
// We only need to check products managing stock, with a limited stock qty. if ( ! $product->managing_stock() || $product->backorders_allowed() ) { continue; }
// Check stock based on all items in the cart and consider any held stock within pending orders. $held_stock = wc_get_held_stock_quantity( $product, $order->get_id() ); $required_stock = $quantities[ $product->get_stock_managed_by_id() ];
if ( ! apply_filters( 'woocommerce_pay_order_product_has_enough_stock', ( $product->get_stock_quantity() >= ( $held_stock + $required_stock ) ), $product, $order ) ) { /* translators: 1: product name 2: quantity in stock */ throw new Exception( sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ) ); } } } } }
// If we cannot match the order with the current user, ask that they verify their email address. if ( self::guest_should_verify_email( $order, 'order-pay' ) ) { wc_get_template( 'checkout/form-verify-email.php', array( 'failed_submission' => ! empty( $_POST['email'] ), // phpcs:ignore WordPress.Security.NonceVerification.Missing 'verify_url' => $order->get_checkout_payment_url(), ) ); return; }
WC()->customer->set_props( array( 'billing_country' => $order->get_billing_country() ? $order->get_billing_country() : null, 'billing_state' => $order->get_billing_state() ? $order->get_billing_state() : null, 'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null, ) ); WC()->customer->save();
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( count( $available_gateways ) ) { current( $available_gateways )->set_current(); }
/** * Allows the text of the submit button on the Pay for Order page to be changed. * * @param string $text The text of the button. * * @since 3.0.2 */ $order_button_text = apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce' ) );
/** * Triggered right before the Pay for Order form, after validation of the order and customer. * * @param WC_Order $order The order that is being paid for. * @param string $order_button_text The text for the submit button. * @param array $available_gateways All available gateways. * * @since 6.6 */ do_action( 'before_woocommerce_pay_form', $order, $order_button_text, $available_gateways );
wc_get_template( 'checkout/form-pay.php', array( 'order' => $order, 'available_gateways' => $available_gateways, 'order_button_text' => $order_button_text, ) );
} catch ( Exception $e ) { wc_print_notice( $e->getMessage(), 'error' ); } } elseif ( $order_id ) {
// Pay for order after checkout step. $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok. $order = wc_get_order( $order_id );
if ( $order && $order->get_id() === $order_id && hash_equals( $order->get_order_key(), $order_key ) ) {
if ( $order->needs_payment() ) {
wc_get_template( 'checkout/order-receipt.php', array( 'order' => $order ) );
} else { /* translators: %s: order status */ wc_print_notice( sprintf( __( 'This order’s status is “%s”—it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), wc_get_order_status_name( $order->get_status() ) ), 'error' ); } } else { wc_print_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' ); } } else { wc_print_notice( __( 'Invalid order.', 'woocommerce' ), 'error' ); }
do_action( 'after_woocommerce_pay' ); }
/** * Show the thanks page. * * @param int $order_id Order ID. */ private static function order_received( $order_id = 0 ) { $order = false;
// Get the order. $order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) ); $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( wp_unslash( $_GET['key'] ) ) ); // WPCS: input var ok, CSRF ok.
if ( $order_id > 0 ) { $order = wc_get_order( $order_id );
if ( ( ! $order instanceof WC_Order ) || ! hash_equals( $order->get_order_key(), $order_key ) ) { $order = false; } }
// Empty awaiting payment session. unset( WC()->session->order_awaiting_payment );
// In case order is created from admin, but paid by the actual customer, store the ip address of the payer // when they visit the payment confirmation page. if ( $order && $order->is_created_via( 'admin' ) ) { $order->set_customer_ip_address( WC_Geolocation::get_ip_address() ); $order->save(); }
// Empty current cart. wc_empty_cart();
// If the specified order ID was invalid, we still render the default order received page (which will simply // state that the order was received, but will not output any other details: this makes it harder to probe for // valid order IDs than if we state that the order ID was not recognized). if ( ! $order ) { wc_get_template( 'checkout/thankyou.php', array( 'order' => false ) ); return; }
/** * Indicates if known (non-guest) shoppers need to be logged in before we let * them access the order received page. * * @param bool $verify_known_shoppers If verification is required. * * @since 8.4.0 */ $verify_known_shoppers = apply_filters( 'woocommerce_order_received_verify_known_shoppers', true ); $order_customer_id = $order->get_customer_id();
// For non-guest orders, require the user to be logged in before showing this page. if ( $verify_known_shoppers && $order_customer_id && get_current_user_id() !== $order_customer_id ) { wc_get_template( 'checkout/order-received.php', array( 'order' => false ) ); wc_print_notice( esc_html__( 'Please log in to your account to view this order.', 'woocommerce' ), 'notice' ); woocommerce_login_form( array( 'redirect' => $order->get_checkout_order_received_url() ) ); return; }
// For guest orders, request they verify their email address (unless we can identify them via the active user session). if ( self::guest_should_verify_email( $order, 'order-received' ) ) { wc_get_template( 'checkout/order-received.php', array( 'order' => false ) ); wc_get_template( 'checkout/form-verify-email.php', array( 'failed_submission' => ! empty( $_POST['email'] ), // phpcs:ignore WordPress.Security.NonceVerification.Missing 'verify_url' => $order->get_checkout_order_received_url(), ) ); return; }
// Otherwise, display the thank you (order received) page. wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) ); }
/** * Show the checkout. */ private static function checkout() { // Show non-cart errors. do_action( 'woocommerce_before_checkout_form_cart_notices' );
// Check cart has contents. if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) { return; }
// Check cart contents for errors. do_action( 'woocommerce_check_cart_items' );
// Calc totals. WC()->cart->calculate_totals();
// Get checkout object. $checkout = WC()->checkout();
if ( empty( $_POST ) && wc_notice_count( 'error' ) > 0 ) { // WPCS: input var ok, CSRF ok.
wc_get_template( 'checkout/cart-errors.php', array( 'checkout' => $checkout ) ); wc_clear_notices();
} else {
$non_js_checkout = ! empty( $_POST['woocommerce_checkout_update_totals'] ); // WPCS: input var ok, CSRF ok.
if ( wc_notice_count( 'error' ) === 0 && $non_js_checkout ) { wc_add_notice( __( 'The order totals have been updated. Please confirm your order by pressing the "Place order" button at the bottom of the page.', 'woocommerce' ) ); }
wc_get_template( 'checkout/form-checkout.php', array( 'checkout' => $checkout ) );
} }
/** * Tries to determine if the user's email address should be verified before rendering either the 'order received' or * 'order pay' pages. This should only be applied to guest orders. * * @param WC_Order $order The order for which a need for email verification is being determined. * @param string $context The context in which email verification is being tested. * * @return bool */ private static function guest_should_verify_email( WC_Order $order, string $context ): bool { // If we cannot match the order with the current user, ask that they verify their email address. $nonce_is_valid = wp_verify_nonce( filter_input( INPUT_POST, 'check_submission' ), 'wc_verify_email' ); $supplied_email = null; $order_id = $order->get_id();
if ( $nonce_is_valid ) { $supplied_email = sanitize_email( wp_unslash( filter_input( INPUT_POST, 'email' ) ) ); }
return Users::should_user_verify_order_email( $order_id, $supplied_email, $context ); } }
|