|
@@ -1,5 +1,5 @@ discard block |
|
|
block discarded – undo |
|
1
|
1
|
<?php |
|
2
|
|
-if ( ! defined( 'ABSPATH' ) ) { |
|
|
2
|
+if ( ! defined('ABSPATH')) { |
|
3
|
3
|
exit; |
|
4
|
4
|
} |
|
5
|
5
|
|
|
@@ -103,29 +103,29 @@ discard block |
|
|
block discarded – undo |
|
103
|
103
|
public function __construct() { |
|
104
|
104
|
self::$_this = $this; |
|
105
|
105
|
|
|
106
|
|
- $gateway_settings = get_option( 'woocommerce_stripe_settings', '' ); |
|
|
106
|
+ $gateway_settings = get_option('woocommerce_stripe_settings', ''); |
|
107
|
107
|
|
|
108
|
|
- $this->statement_descriptor = ! empty( $gateway_settings['statement_descriptor'] ) ? $gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
|
|
108
|
+ $this->statement_descriptor = ! empty($gateway_settings['statement_descriptor']) ? $gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES); |
|
109
|
109
|
|
|
110
|
110
|
// If both site title and statement descriptor is not set. Fallback. |
|
111
|
|
- if ( empty( $this->statement_descriptor ) ) { |
|
|
111
|
+ if (empty($this->statement_descriptor)) { |
|
112
|
112
|
$this->statement_descriptor = $_SERVER['SERVER_NAME']; |
|
113
|
113
|
} |
|
114
|
114
|
|
|
115
|
115
|
$this->dp = wc_get_price_decimals(); |
|
116
|
|
- $this->enabled = ( ! empty( $gateway_settings['enabled'] ) && 'yes' === $gateway_settings['enabled'] ) ? true : false; |
|
117
|
|
- $this->testmode = ( ! empty( $gateway_settings['testmode'] ) && 'yes' === $gateway_settings['testmode'] ) ? true : false; |
|
118
|
|
- $this->capture = ( ! empty( $gateway_settings['capture'] ) && 'yes' === $gateway_settings['capture'] ) ? true : false; |
|
119
|
|
- $this->saved_cards = ( ! empty( $gateway_settings['saved_cards'] ) && 'yes' === $gateway_settings['saved_cards'] ) ? true : false; |
|
120
|
|
- $this->apple_pay = ( ! empty( $gateway_settings['apple_pay'] ) && 'yes' === $gateway_settings['apple_pay'] ) ? true : false; |
|
121
|
|
- $this->apple_pay_button = ! empty( $gateway_settings['apple_pay_button'] ) ? $gateway_settings['apple_pay_button'] : 'black'; |
|
122
|
|
- $this->apple_pay_button_lang = ! empty( $gateway_settings['apple_pay_button_lang'] ) ? $gateway_settings['apple_pay_button_lang'] : 'en'; |
|
123
|
|
- $this->logging = ( ! empty( $gateway_settings['logging'] ) && 'yes' === $gateway_settings['logging'] ) ? true : false; |
|
124
|
|
- $this->publishable_key = ! empty( $gateway_settings['publishable_key'] ) ? $gateway_settings['publishable_key'] : ''; |
|
|
116
|
+ $this->enabled = ( ! empty($gateway_settings['enabled']) && 'yes' === $gateway_settings['enabled']) ? true : false; |
|
|
117
|
+ $this->testmode = ( ! empty($gateway_settings['testmode']) && 'yes' === $gateway_settings['testmode']) ? true : false; |
|
|
118
|
+ $this->capture = ( ! empty($gateway_settings['capture']) && 'yes' === $gateway_settings['capture']) ? true : false; |
|
|
119
|
+ $this->saved_cards = ( ! empty($gateway_settings['saved_cards']) && 'yes' === $gateway_settings['saved_cards']) ? true : false; |
|
|
120
|
+ $this->apple_pay = ( ! empty($gateway_settings['apple_pay']) && 'yes' === $gateway_settings['apple_pay']) ? true : false; |
|
|
121
|
+ $this->apple_pay_button = ! empty($gateway_settings['apple_pay_button']) ? $gateway_settings['apple_pay_button'] : 'black'; |
|
|
122
|
+ $this->apple_pay_button_lang = ! empty($gateway_settings['apple_pay_button_lang']) ? $gateway_settings['apple_pay_button_lang'] : 'en'; |
|
|
123
|
+ $this->logging = ( ! empty($gateway_settings['logging']) && 'yes' === $gateway_settings['logging']) ? true : false; |
|
|
124
|
+ $this->publishable_key = ! empty($gateway_settings['publishable_key']) ? $gateway_settings['publishable_key'] : ''; |
|
125
|
125
|
$this->is_shipping_enabled = $this->is_shipping_enabled(); |
|
126
|
126
|
|
|
127
|
|
- if ( $this->testmode ) { |
|
128
|
|
- $this->publishable_key = ! empty( $gateway_settings['test_publishable_key'] ) ? $gateway_settings['test_publishable_key'] : ''; |
|
|
127
|
+ if ($this->testmode) { |
|
|
128
|
+ $this->publishable_key = ! empty($gateway_settings['test_publishable_key']) ? $gateway_settings['test_publishable_key'] : ''; |
|
129
|
129
|
} |
|
130
|
130
|
|
|
131
|
131
|
$this->init(); |
|
@@ -144,53 +144,53 @@ discard block |
|
|
block discarded – undo |
|
144
|
144
|
*/ |
|
145
|
145
|
public function init() { |
|
146
|
146
|
// If Apple Pay is not enabled no need to proceed further. |
|
147
|
|
- if ( ! $this->apple_pay ) { |
|
|
147
|
+ if ( ! $this->apple_pay) { |
|
148
|
148
|
return; |
|
149
|
149
|
} |
|
150
|
150
|
|
|
151
|
|
- add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) ); |
|
152
|
|
- add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) ); |
|
|
151
|
+ add_action('wp_enqueue_scripts', array($this, 'cart_scripts')); |
|
|
152
|
+ add_action('wp_enqueue_scripts', array($this, 'single_scripts')); |
|
153
|
153
|
|
|
154
|
154
|
/** |
|
155
|
155
|
* In order to display the Apple Pay button in the correct position, |
|
156
|
156
|
* a new hook was added to WooCommerce 3.0. In older versions of WooCommerce, |
|
157
|
157
|
* CSS is used to position the button. |
|
158
|
158
|
*/ |
|
159
|
|
- if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) { |
|
160
|
|
- add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 ); |
|
|
159
|
+ if (version_compare(WC_VERSION, '3.0.0', '<')) { |
|
|
160
|
+ add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1); |
|
161
|
161
|
} else { |
|
162
|
|
- add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 ); |
|
|
162
|
+ add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1); |
|
163
|
163
|
} |
|
164
|
164
|
|
|
165
|
|
- add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 ); |
|
166
|
|
- add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
|
167
|
|
- add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 ); |
|
168
|
|
- add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
|
169
|
|
- add_action( 'wc_ajax_wc_stripe_log_apple_pay_errors', array( $this, 'log_apple_pay_errors' ) ); |
|
170
|
|
- add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) ); |
|
171
|
|
- add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) ); |
|
172
|
|
- add_action( 'wc_ajax_wc_stripe_apple_pay_clear_cart', array( $this, 'clear_cart' ) ); |
|
173
|
|
- add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) ); |
|
174
|
|
- add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) ); |
|
175
|
|
- add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) ); |
|
176
|
|
- add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 ); |
|
177
|
|
- add_filter( 'woocommerce_validate_postcode', array( $this, 'postal_code_validation' ), 10, 3 ); |
|
|
165
|
+ add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1); |
|
|
166
|
+ add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2); |
|
|
167
|
+ add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1); |
|
|
168
|
+ add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2); |
|
|
169
|
+ add_action('wc_ajax_wc_stripe_log_apple_pay_errors', array($this, 'log_apple_pay_errors')); |
|
|
170
|
+ add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay')); |
|
|
171
|
+ add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart')); |
|
|
172
|
+ add_action('wc_ajax_wc_stripe_apple_pay_clear_cart', array($this, 'clear_cart')); |
|
|
173
|
+ add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single')); |
|
|
174
|
+ add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods')); |
|
|
175
|
+ add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method')); |
|
|
176
|
+ add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2); |
|
|
177
|
+ add_filter('woocommerce_validate_postcode', array($this, 'postal_code_validation'), 10, 3); |
|
178
|
178
|
} |
|
179
|
179
|
|
|
180
|
180
|
/** |
|
181
|
181
|
* Filters the gateway title to reflect Apple Pay. |
|
182
|
182
|
* |
|
183
|
183
|
*/ |
|
184
|
|
- public function filter_gateway_title( $title, $id ) { |
|
|
184
|
+ public function filter_gateway_title($title, $id) { |
|
185
|
185
|
global $post; |
|
186
|
186
|
|
|
187
|
|
- if ( ! is_object( $post ) ) { |
|
|
187
|
+ if ( ! is_object($post)) { |
|
188
|
188
|
return $title; |
|
189
|
189
|
} |
|
190
|
190
|
|
|
191
|
|
- $method_title = get_post_meta( $post->ID, '_payment_method_title', true ); |
|
|
191
|
+ $method_title = get_post_meta($post->ID, '_payment_method_title', true); |
|
192
|
192
|
|
|
193
|
|
- if ( 'stripe' === $id && ! empty( $method_title ) && 'Apple Pay (Stripe)' === $method_title ) { |
|
|
193
|
+ if ('stripe' === $id && ! empty($method_title) && 'Apple Pay (Stripe)' === $method_title) { |
|
194
|
194
|
return $method_title; |
|
195
|
195
|
} |
|
196
|
196
|
|
|
@@ -204,13 +204,13 @@ discard block |
|
|
block discarded – undo |
|
204
|
204
|
* @version 3.1.4 |
|
205
|
205
|
*/ |
|
206
|
206
|
public function log_apple_pay_errors() { |
|
207
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) && ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
208
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
207
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce') && ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
|
208
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
209
|
209
|
} |
|
210
|
210
|
|
|
211
|
|
- $errors = wc_clean( stripslashes( $_POST['errors'] ) ); |
|
|
211
|
+ $errors = wc_clean(stripslashes($_POST['errors'])); |
|
212
|
212
|
|
|
213
|
|
- $this->log( $errors ); |
|
|
213
|
+ $this->log($errors); |
|
214
|
214
|
|
|
215
|
215
|
exit; |
|
216
|
216
|
} |
|
@@ -221,10 +221,10 @@ discard block |
|
|
block discarded – undo |
|
221
|
221
|
* @since 3.1.4 |
|
222
|
222
|
* @version 3.1.4 |
|
223
|
223
|
*/ |
|
224
|
|
- public function postal_code_validation( $valid, $postcode, $country ) { |
|
|
224
|
+ public function postal_code_validation($valid, $postcode, $country) { |
|
225
|
225
|
$gateways = WC()->payment_gateways->get_available_payment_gateways(); |
|
226
|
226
|
|
|
227
|
|
- if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) { |
|
|
227
|
+ if ( ! $this->apple_pay || ! isset($gateways['stripe'])) { |
|
228
|
228
|
return $valid; |
|
229
|
229
|
} |
|
230
|
230
|
|
|
@@ -233,7 +233,7 @@ discard block |
|
|
block discarded – undo |
|
233
|
233
|
* when passing it back from the shippingcontactselected object. This causes WC to invalidate |
|
234
|
234
|
* the order and not let it go through. The remedy for now is just to remove this validation. |
|
235
|
235
|
*/ |
|
236
|
|
- if ( 'GB' === $country || 'CA' === $country ) { |
|
|
236
|
+ if ('GB' === $country || 'CA' === $country) { |
|
237
|
237
|
return true; |
|
238
|
238
|
} |
|
239
|
239
|
|
|
@@ -248,9 +248,9 @@ discard block |
|
|
block discarded – undo |
|
248
|
248
|
* @return bool |
|
249
|
249
|
*/ |
|
250
|
250
|
public function is_shipping_enabled() { |
|
251
|
|
- $shipping_enabled = get_option( 'woocommerce_ship_to_countries', '' ); |
|
|
251
|
+ $shipping_enabled = get_option('woocommerce_ship_to_countries', ''); |
|
252
|
252
|
|
|
253
|
|
- if ( 'disabled' === $shipping_enabled ) { |
|
|
253
|
+ if ('disabled' === $shipping_enabled) { |
|
254
|
254
|
return false; |
|
255
|
255
|
} |
|
256
|
256
|
|
|
@@ -264,42 +264,42 @@ discard block |
|
|
block discarded – undo |
|
264
|
264
|
* @version 3.1.4 |
|
265
|
265
|
*/ |
|
266
|
266
|
public function single_scripts() { |
|
267
|
|
- if ( ! is_single() ) { |
|
|
267
|
+ if ( ! is_single()) { |
|
268
|
268
|
return; |
|
269
|
269
|
} |
|
270
|
270
|
|
|
271
|
271
|
global $post; |
|
272
|
272
|
|
|
273
|
|
- $product = wc_get_product( $post->ID ); |
|
|
273
|
+ $product = wc_get_product($post->ID); |
|
274
|
274
|
|
|
275
|
|
- if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
|
|
275
|
+ if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) { |
|
276
|
276
|
return; |
|
277
|
277
|
} |
|
278
|
278
|
|
|
279
|
|
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
|
279
|
+ $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
280
|
280
|
|
|
281
|
|
- wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
|
281
|
+ wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
282
|
282
|
|
|
283
|
|
- wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
284
|
|
- wp_enqueue_script( 'woocommerce_stripe_apple_pay_single', plugins_url( 'assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
|
283
|
+ wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
|
284
|
+ wp_enqueue_script('woocommerce_stripe_apple_pay_single', plugins_url('assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
285
|
285
|
|
|
286
|
286
|
$stripe_params = array( |
|
287
|
287
|
'key' => $this->publishable_key, |
|
288
|
288
|
'currency_code' => get_woocommerce_currency(), |
|
289
|
|
- 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
|
289
|
+ 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
290
|
290
|
'label' => $this->statement_descriptor . ' (via WooCommerce)', |
|
291
|
|
- 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
292
|
|
- 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
293
|
|
- 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
294
|
|
- 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
295
|
|
- 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
296
|
|
- 'needs_shipping' => ( $product->needs_shipping() && $this->is_shipping_enabled ) ? 'yes' : 'no', |
|
|
291
|
+ 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
|
292
|
+ 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
|
293
|
+ 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
|
294
|
+ 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
|
295
|
+ 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
|
296
|
+ 'needs_shipping' => ($product->needs_shipping() && $this->is_shipping_enabled) ? 'yes' : 'no', |
|
297
|
297
|
'i18n' => array( |
|
298
|
|
- 'sub_total' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ), |
|
|
298
|
+ 'sub_total' => __('Sub-Total', 'woocommerce-gateway-stripe'), |
|
299
|
299
|
), |
|
300
|
300
|
); |
|
301
|
301
|
|
|
302
|
|
- wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) ); |
|
|
302
|
+ wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params)); |
|
303
|
303
|
} |
|
304
|
304
|
|
|
305
|
305
|
/** |
|
@@ -309,32 +309,32 @@ discard block |
|
|
block discarded – undo |
|
309
|
309
|
* @version 3.1.0 |
|
310
|
310
|
*/ |
|
311
|
311
|
public function cart_scripts() { |
|
312
|
|
- if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
|
|
312
|
+ if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) { |
|
313
|
313
|
return; |
|
314
|
314
|
} |
|
315
|
315
|
|
|
316
|
|
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
|
316
|
+ $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
317
|
317
|
|
|
318
|
|
- wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
|
318
|
+ wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
319
|
319
|
|
|
320
|
|
- wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
321
|
|
- wp_enqueue_script( 'woocommerce_stripe_apple_pay', plugins_url( 'assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
|
320
|
+ wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
|
321
|
+ wp_enqueue_script('woocommerce_stripe_apple_pay', plugins_url('assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
322
|
322
|
|
|
323
|
323
|
$stripe_params = array( |
|
324
|
324
|
'key' => $this->publishable_key, |
|
325
|
325
|
'currency_code' => get_woocommerce_currency(), |
|
326
|
|
- 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
|
326
|
+ 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
327
|
327
|
'label' => $this->statement_descriptor . ' (via WooCommerce)', |
|
328
|
|
- 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
329
|
|
- 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
330
|
|
- 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
331
|
|
- 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
332
|
|
- 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
333
|
|
- 'needs_shipping' => ( WC()->cart->needs_shipping() && $this->is_shipping_enabled ) ? 'yes' : 'no', |
|
|
328
|
+ 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
|
329
|
+ 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
|
330
|
+ 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
|
331
|
+ 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
|
332
|
+ 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
|
333
|
+ 'needs_shipping' => (WC()->cart->needs_shipping() && $this->is_shipping_enabled) ? 'yes' : 'no', |
|
334
|
334
|
'is_cart_page' => is_cart() ? 'yes' : 'no', |
|
335
|
335
|
); |
|
336
|
336
|
|
|
337
|
|
- wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) ); |
|
|
337
|
+ wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params)); |
|
338
|
338
|
} |
|
339
|
339
|
|
|
340
|
340
|
/** |
|
@@ -345,11 +345,11 @@ discard block |
|
|
block discarded – undo |
|
345
|
345
|
* @return array |
|
346
|
346
|
*/ |
|
347
|
347
|
public function supported_product_types() { |
|
348
|
|
- return apply_filters( 'wc_stripe_apple_pay_supported_types', array( |
|
|
348
|
+ return apply_filters('wc_stripe_apple_pay_supported_types', array( |
|
349
|
349
|
'simple', |
|
350
|
350
|
'variable', |
|
351
|
351
|
'variation', |
|
352
|
|
- ) ); |
|
|
352
|
+ )); |
|
353
|
353
|
} |
|
354
|
354
|
|
|
355
|
355
|
/** |
|
@@ -361,10 +361,10 @@ discard block |
|
|
block discarded – undo |
|
361
|
361
|
* @return bool |
|
362
|
362
|
*/ |
|
363
|
363
|
public function allowed_items_in_cart() { |
|
364
|
|
- foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { |
|
365
|
|
- $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); |
|
|
364
|
+ foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { |
|
|
365
|
+ $_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key); |
|
366
|
366
|
|
|
367
|
|
- if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $_product->product_type : $_product->get_type() ), $this->supported_product_types() ) ) { |
|
|
367
|
+ if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $_product->product_type : $_product->get_type()), $this->supported_product_types())) { |
|
368
|
368
|
return false; |
|
369
|
369
|
} |
|
370
|
370
|
} |
|
@@ -385,29 +385,29 @@ discard block |
|
|
block discarded – undo |
|
385
|
385
|
* In order for the Apple Pay button to show on product detail page, |
|
386
|
386
|
* Apple Pay must be enabled and Stripe gateway must be enabled. |
|
387
|
387
|
*/ |
|
388
|
|
- if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) { |
|
389
|
|
- $this->log( 'Apple Pay not enabled or Stripe is not an available gateway ( Apple Pay button disabled )' ); |
|
|
388
|
+ if ( ! $this->apple_pay || ! isset($gateways['stripe'])) { |
|
|
389
|
+ $this->log('Apple Pay not enabled or Stripe is not an available gateway ( Apple Pay button disabled )'); |
|
390
|
390
|
return; |
|
391
|
391
|
} |
|
392
|
392
|
|
|
393
|
|
- if ( is_single() ) { |
|
|
393
|
+ if (is_single()) { |
|
394
|
394
|
global $post; |
|
395
|
395
|
|
|
396
|
|
- $product = wc_get_product( $post->ID ); |
|
|
396
|
+ $product = wc_get_product($post->ID); |
|
397
|
397
|
|
|
398
|
|
- if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
|
|
398
|
+ if ( ! is_object($product) || ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) { |
|
399
|
399
|
return; |
|
400
|
400
|
} |
|
401
|
401
|
} else { |
|
402
|
|
- if ( ! $this->allowed_items_in_cart() ) { |
|
403
|
|
- $this->log( 'Items in the cart has unsupported product type ( Apple Pay button disabled )' ); |
|
|
402
|
+ if ( ! $this->allowed_items_in_cart()) { |
|
|
403
|
+ $this->log('Items in the cart has unsupported product type ( Apple Pay button disabled )'); |
|
404
|
404
|
return; |
|
405
|
405
|
} |
|
406
|
406
|
} |
|
407
|
407
|
|
|
408
|
408
|
?> |
|
409
|
409
|
<div class="apple-pay-button-wrapper"> |
|
410
|
|
- <button class="apple-pay-button" lang="<?php echo esc_attr( $this->apple_pay_button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $this->apple_pay_button ); ?>;" alt="<?php esc_attr_e( 'Buy with Apple Pay', 'woocommerce-gateway-stripe' ); ?>"></button> |
|
|
410
|
+ <button class="apple-pay-button" lang="<?php echo esc_attr($this->apple_pay_button_lang); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($this->apple_pay_button); ?>;" alt="<?php esc_attr_e('Buy with Apple Pay', 'woocommerce-gateway-stripe'); ?>"></button> |
|
411
|
411
|
</div> |
|
412
|
412
|
<?php |
|
413
|
413
|
} |
|
@@ -425,17 +425,17 @@ discard block |
|
|
block discarded – undo |
|
425
|
425
|
* In order for the Apple Pay button to show on cart page, |
|
426
|
426
|
* Apple Pay must be enabled and Stripe gateway must be enabled. |
|
427
|
427
|
*/ |
|
428
|
|
- if ( ! $this->apple_pay || ! isset( $gateways['stripe'] ) ) { |
|
429
|
|
- $this->log( 'Apple Pay not enabled or Stripe is not an available gateway ( Apple Pay button disabled )' ); |
|
|
428
|
+ if ( ! $this->apple_pay || ! isset($gateways['stripe'])) { |
|
|
429
|
+ $this->log('Apple Pay not enabled or Stripe is not an available gateway ( Apple Pay button disabled )'); |
|
430
|
430
|
return; |
|
431
|
431
|
} |
|
432
|
432
|
|
|
433
|
|
- if ( ! $this->allowed_items_in_cart() ) { |
|
434
|
|
- $this->log( 'Items in the cart has unsupported product type ( Apple Pay button disabled )' ); |
|
|
433
|
+ if ( ! $this->allowed_items_in_cart()) { |
|
|
434
|
+ $this->log('Items in the cart has unsupported product type ( Apple Pay button disabled )'); |
|
435
|
435
|
return; |
|
436
|
436
|
} |
|
437
|
437
|
?> |
|
438
|
|
- <p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p> |
|
|
438
|
+ <p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p> |
|
439
|
439
|
<?php |
|
440
|
440
|
} |
|
441
|
441
|
|
|
@@ -446,45 +446,45 @@ discard block |
|
|
block discarded – undo |
|
446
|
446
|
* @version 3.1.0 |
|
447
|
447
|
*/ |
|
448
|
448
|
public function generate_apple_pay_single() { |
|
449
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
450
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
449
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
|
450
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
451
|
451
|
} |
|
452
|
452
|
|
|
453
|
|
- if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
454
|
|
- define( 'WOOCOMMERCE_CART', true ); |
|
|
453
|
+ if ( ! defined('WOOCOMMERCE_CART')) { |
|
|
454
|
+ define('WOOCOMMERCE_CART', true); |
|
455
|
455
|
} |
|
456
|
456
|
|
|
457
|
457
|
WC()->shipping->reset_shipping(); |
|
458
|
458
|
|
|
459
|
459
|
global $post; |
|
460
|
460
|
|
|
461
|
|
- $product = wc_get_product( $post->ID ); |
|
462
|
|
- $qty = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] ); |
|
|
461
|
+ $product = wc_get_product($post->ID); |
|
|
462
|
+ $qty = ! isset($_POST['qty']) ? 1 : absint($_POST['qty']); |
|
463
|
463
|
|
|
464
|
464
|
/** |
|
465
|
465
|
* If this page is single product page, we need to simulate |
|
466
|
466
|
* adding the product to the cart taken account if it is a |
|
467
|
467
|
* simple or variable product. |
|
468
|
468
|
*/ |
|
469
|
|
- if ( is_single() ) { |
|
|
469
|
+ if (is_single()) { |
|
470
|
470
|
// First empty the cart to prevent wrong calculation. |
|
471
|
471
|
WC()->cart->empty_cart(); |
|
472
|
472
|
|
|
473
|
|
- if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) { |
|
474
|
|
- $attributes = array_map( 'wc_clean', $_POST['attributes'] ); |
|
|
473
|
+ if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) { |
|
|
474
|
+ $attributes = array_map('wc_clean', $_POST['attributes']); |
|
475
|
475
|
|
|
476
|
|
- if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) { |
|
477
|
|
- $variation_id = $product->get_matching_variation( $attributes ); |
|
|
476
|
+ if (version_compare(WC_VERSION, '3.0.0', '<')) { |
|
|
477
|
+ $variation_id = $product->get_matching_variation($attributes); |
|
478
|
478
|
} else { |
|
479
|
|
- $data_store = WC_Data_Store::load( 'product' ); |
|
480
|
|
- $variation_id = $data_store->find_matching_product_variation( $product, $attributes ); |
|
|
479
|
+ $data_store = WC_Data_Store::load('product'); |
|
|
480
|
+ $variation_id = $data_store->find_matching_product_variation($product, $attributes); |
|
481
|
481
|
} |
|
482
|
482
|
|
|
483
|
|
- WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes ); |
|
|
483
|
+ WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes); |
|
484
|
484
|
} |
|
485
|
485
|
|
|
486
|
|
- if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) { |
|
487
|
|
- WC()->cart->add_to_cart( $product->get_id(), $qty ); |
|
|
486
|
+ if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) { |
|
|
487
|
+ WC()->cart->add_to_cart($product->get_id(), $qty); |
|
488
|
488
|
} |
|
489
|
489
|
} |
|
490
|
490
|
|
|
@@ -492,7 +492,7 @@ discard block |
|
|
block discarded – undo |
|
492
|
492
|
|
|
493
|
493
|
$build_items = $this->build_line_items(); |
|
494
|
494
|
|
|
495
|
|
- wp_send_json( array( 'line_items' => $build_items['line_items'], 'total' => $build_items['total'] ) ); |
|
|
495
|
+ wp_send_json(array('line_items' => $build_items['line_items'], 'total' => $build_items['total'])); |
|
496
|
496
|
} |
|
497
|
497
|
|
|
498
|
498
|
/** |
|
@@ -502,13 +502,13 @@ discard block |
|
|
block discarded – undo |
|
502
|
502
|
* @version 3.1.0 |
|
503
|
503
|
*/ |
|
504
|
504
|
public function generate_apple_pay_cart() { |
|
505
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
506
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
505
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
|
506
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
507
|
507
|
} |
|
508
|
508
|
|
|
509
|
509
|
$build_items = $this->build_line_items(); |
|
510
|
510
|
|
|
511
|
|
- wp_send_json( array( 'line_items' => $build_items['line_items'], 'total' => $build_items['total'] ) ); |
|
|
511
|
+ wp_send_json(array('line_items' => $build_items['line_items'], 'total' => $build_items['total'])); |
|
512
|
512
|
} |
|
513
|
513
|
|
|
514
|
514
|
/** |
|
@@ -529,32 +529,32 @@ discard block |
|
|
block discarded – undo |
|
529
|
529
|
* @version 3.1.0 |
|
530
|
530
|
* @param array $address |
|
531
|
531
|
*/ |
|
532
|
|
- public function calculate_shipping( $address = array() ) { |
|
533
|
|
- $country = strtoupper( $address['countryCode'] ); |
|
534
|
|
- $state = strtoupper( $address['administrativeArea'] ); |
|
|
532
|
+ public function calculate_shipping($address = array()) { |
|
|
533
|
+ $country = strtoupper($address['countryCode']); |
|
|
534
|
+ $state = strtoupper($address['administrativeArea']); |
|
535
|
535
|
$postcode = $address['postalCode']; |
|
536
|
536
|
$city = $address['locality']; |
|
537
|
537
|
|
|
538
|
538
|
WC()->shipping->reset_shipping(); |
|
539
|
539
|
|
|
540
|
|
- if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
541
|
|
- throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
542
|
|
- } elseif ( $postcode ) { |
|
543
|
|
- $postcode = wc_format_postcode( $postcode, $country ); |
|
|
540
|
+ if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) { |
|
|
541
|
+ throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe')); |
|
|
542
|
+ } elseif ($postcode) { |
|
|
543
|
+ $postcode = wc_format_postcode($postcode, $country); |
|
544
|
544
|
} |
|
545
|
545
|
|
|
546
|
|
- if ( $country ) { |
|
547
|
|
- WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
548
|
|
- WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
|
546
|
+ if ($country) { |
|
|
547
|
+ WC()->customer->set_location($country, $state, $postcode, $city); |
|
|
548
|
+ WC()->customer->set_shipping_location($country, $state, $postcode, $city); |
|
549
|
549
|
} else { |
|
550
|
550
|
WC()->customer->set_to_base(); |
|
551
|
551
|
WC()->customer->set_shipping_to_base(); |
|
552
|
552
|
} |
|
553
|
553
|
|
|
554
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
555
|
|
- WC()->customer->calculated_shipping( true ); |
|
|
554
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
555
|
+ WC()->customer->calculated_shipping(true); |
|
556
|
556
|
} else { |
|
557
|
|
- WC()->customer->set_calculated_shipping( true ); |
|
|
557
|
+ WC()->customer->set_calculated_shipping(true); |
|
558
|
558
|
WC()->customer->save(); |
|
559
|
559
|
} |
|
560
|
560
|
|
|
@@ -578,17 +578,17 @@ discard block |
|
|
block discarded – undo |
|
578
|
578
|
$packages[0]['destination']['postcode'] = $postcode; |
|
579
|
579
|
$packages[0]['destination']['city'] = $city; |
|
580
|
580
|
|
|
581
|
|
- foreach ( WC()->cart->get_cart() as $item ) { |
|
582
|
|
- if ( $item['data']->needs_shipping() ) { |
|
583
|
|
- if ( isset( $item['line_total'] ) ) { |
|
|
581
|
+ foreach (WC()->cart->get_cart() as $item) { |
|
|
582
|
+ if ($item['data']->needs_shipping()) { |
|
|
583
|
+ if (isset($item['line_total'])) { |
|
584
|
584
|
$packages[0]['contents_cost'] += $item['line_total']; |
|
585
|
585
|
} |
|
586
|
586
|
} |
|
587
|
587
|
} |
|
588
|
588
|
|
|
589
|
|
- $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
|
|
589
|
+ $packages = apply_filters('woocommerce_cart_shipping_packages', $packages); |
|
590
|
590
|
|
|
591
|
|
- WC()->shipping->calculate_shipping( $packages ); |
|
|
591
|
+ WC()->shipping->calculate_shipping($packages); |
|
592
|
592
|
} |
|
593
|
593
|
|
|
594
|
594
|
/** |
|
@@ -598,18 +598,18 @@ discard block |
|
|
block discarded – undo |
|
598
|
598
|
* @version 3.1.0 |
|
599
|
599
|
*/ |
|
600
|
600
|
public function get_shipping_methods() { |
|
601
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) { |
|
602
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
601
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) { |
|
|
602
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
603
|
603
|
} |
|
604
|
604
|
|
|
605
|
|
- if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
606
|
|
- define( 'WOOCOMMERCE_CART', true ); |
|
|
605
|
+ if ( ! defined('WOOCOMMERCE_CART')) { |
|
|
606
|
+ define('WOOCOMMERCE_CART', true); |
|
607
|
607
|
} |
|
608
|
608
|
|
|
609
|
609
|
try { |
|
610
|
|
- $address = array_map( 'wc_clean', $_POST['address'] ); |
|
|
610
|
+ $address = array_map('wc_clean', $_POST['address']); |
|
611
|
611
|
|
|
612
|
|
- $this->calculate_shipping( $address ); |
|
|
612
|
+ $this->calculate_shipping($address); |
|
613
|
613
|
|
|
614
|
614
|
// Set the shipping options. |
|
615
|
615
|
$currency = get_woocommerce_currency(); |
|
@@ -617,13 +617,13 @@ discard block |
|
|
block discarded – undo |
|
617
|
617
|
|
|
618
|
618
|
$packages = WC()->shipping->get_packages(); |
|
619
|
619
|
|
|
620
|
|
- if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
|
621
|
|
- foreach ( $packages as $package_key => $package ) { |
|
622
|
|
- if ( empty( $package['rates'] ) ) { |
|
623
|
|
- throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
|
620
|
+ if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) { |
|
|
621
|
+ foreach ($packages as $package_key => $package) { |
|
|
622
|
+ if (empty($package['rates'])) { |
|
|
623
|
+ throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
624
|
624
|
} |
|
625
|
625
|
|
|
626
|
|
- foreach ( $package['rates'] as $key => $rate ) { |
|
|
626
|
+ foreach ($package['rates'] as $key => $rate) { |
|
627
|
627
|
$data[] = array( |
|
628
|
628
|
'id' => $rate->id, |
|
629
|
629
|
'label' => $rate->label, |
|
@@ -637,19 +637,19 @@ discard block |
|
|
block discarded – undo |
|
637
|
637
|
} |
|
638
|
638
|
|
|
639
|
639
|
// Auto select the first shipping method. |
|
640
|
|
- WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) ); |
|
|
640
|
+ WC()->session->set('chosen_shipping_methods', array($data[0]['id'])); |
|
641
|
641
|
|
|
642
|
642
|
WC()->cart->calculate_totals(); |
|
643
|
643
|
|
|
644
|
644
|
$build_items = $this->build_line_items(); |
|
645
|
645
|
|
|
646
|
|
- wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $build_items['line_items'], 'total' => $build_items['total'] ) ); |
|
|
646
|
+ wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $build_items['line_items'], 'total' => $build_items['total'])); |
|
647
|
647
|
} else { |
|
648
|
|
- throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
|
648
|
+ throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
649
|
649
|
} |
|
650
|
|
- } catch ( Exception $e ) { |
|
|
650
|
+ } catch (Exception $e) { |
|
651
|
651
|
$build_items = $this->build_line_items(); |
|
652
|
|
- wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $build_items['line_items'], 'total' => $build_items['total'] ) ); |
|
|
652
|
+ wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $build_items['line_items'], 'total' => $build_items['total'])); |
|
653
|
653
|
} |
|
654
|
654
|
} |
|
655
|
655
|
|
|
@@ -660,22 +660,22 @@ discard block |
|
|
block discarded – undo |
|
660
|
660
|
* @version 3.1.0 |
|
661
|
661
|
*/ |
|
662
|
662
|
public function update_shipping_method() { |
|
663
|
|
- if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
664
|
|
- define( 'WOOCOMMERCE_CART', true ); |
|
|
663
|
+ if ( ! defined('WOOCOMMERCE_CART')) { |
|
|
664
|
+ define('WOOCOMMERCE_CART', true); |
|
665
|
665
|
} |
|
666
|
666
|
|
|
667
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) { |
|
668
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
667
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) { |
|
|
668
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
669
|
669
|
} |
|
670
|
670
|
|
|
671
|
|
- $selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] ); |
|
|
671
|
+ $selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']); |
|
672
|
672
|
|
|
673
|
|
- WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) ); |
|
|
673
|
+ WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier'])); |
|
674
|
674
|
|
|
675
|
675
|
WC()->cart->calculate_totals(); |
|
676
|
676
|
|
|
677
|
677
|
$build_items = $this->build_line_items(); |
|
678
|
|
- wp_send_json( array( 'success' => 'true', 'line_items' => $build_items['line_items'], 'total' => $build_items['total'] ) ); |
|
|
678
|
+ wp_send_json(array('success' => 'true', 'line_items' => $build_items['line_items'], 'total' => $build_items['total'])); |
|
679
|
679
|
} |
|
680
|
680
|
|
|
681
|
681
|
/** |
|
@@ -686,37 +686,37 @@ discard block |
|
|
block discarded – undo |
|
686
|
686
|
* @version 3.1.0 |
|
687
|
687
|
*/ |
|
688
|
688
|
public function process_apple_pay() { |
|
689
|
|
- if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
|
690
|
|
- wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
|
689
|
+ if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) { |
|
|
690
|
+ wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
691
|
691
|
} |
|
692
|
692
|
|
|
693
|
693
|
try { |
|
694
|
|
- $result = array_map( 'wc_clean', $_POST['result'] ); |
|
|
694
|
+ $result = array_map('wc_clean', $_POST['result']); |
|
695
|
695
|
|
|
696
|
|
- $order = $this->create_order( $result ); |
|
|
696
|
+ $order = $this->create_order($result); |
|
697
|
697
|
|
|
698
|
|
- $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
|
698
|
+ $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
699
|
699
|
|
|
700
|
700
|
// Handle payment. |
|
701
|
|
- if ( $order->get_total() > 0 ) { |
|
|
701
|
+ if ($order->get_total() > 0) { |
|
702
|
702
|
|
|
703
|
|
- if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
704
|
|
- return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
|
703
|
+ if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
|
704
|
+ return new WP_Error('stripe_error', sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
705
|
705
|
} |
|
706
|
706
|
|
|
707
|
|
- $this->log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" ); |
|
|
707
|
+ $this->log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}"); |
|
708
|
708
|
|
|
709
|
709
|
// Make the request. |
|
710
|
|
- $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
|
|
710
|
+ $response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id'])); |
|
711
|
711
|
|
|
712
|
|
- if ( is_wp_error( $response ) ) { |
|
|
712
|
+ if (is_wp_error($response)) { |
|
713
|
713
|
$localized_messages = $this->get_localized_messages(); |
|
714
|
714
|
|
|
715
|
|
- throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
|
|
715
|
+ throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message())); |
|
716
|
716
|
} |
|
717
|
717
|
|
|
718
|
718
|
// Process valid response. |
|
719
|
|
- $this->process_response( $response, $order ); |
|
|
719
|
+ $this->process_response($response, $order); |
|
720
|
720
|
} else { |
|
721
|
721
|
$order->payment_complete(); |
|
722
|
722
|
} |
|
@@ -724,24 +724,24 @@ discard block |
|
|
block discarded – undo |
|
724
|
724
|
// Remove cart. |
|
725
|
725
|
WC()->cart->empty_cart(); |
|
726
|
726
|
|
|
727
|
|
- update_post_meta( $order_id, '_customer_user', get_current_user_id() ); |
|
728
|
|
- update_post_meta( $order_id, '_payment_method_title', 'Apple Pay (Stripe)' ); |
|
|
727
|
+ update_post_meta($order_id, '_customer_user', get_current_user_id()); |
|
|
728
|
+ update_post_meta($order_id, '_payment_method_title', 'Apple Pay (Stripe)'); |
|
729
|
729
|
|
|
730
|
730
|
// Return thank you page redirect. |
|
731
|
|
- wp_send_json( array( |
|
|
731
|
+ wp_send_json(array( |
|
732
|
732
|
'success' => 'true', |
|
733
|
|
- 'redirect' => $this->get_return_url( $order ), |
|
734
|
|
- ) ); |
|
|
733
|
+ 'redirect' => $this->get_return_url($order), |
|
|
734
|
+ )); |
|
735
|
735
|
|
|
736
|
|
- } catch ( Exception $e ) { |
|
737
|
|
- WC()->session->set( 'refresh_totals', true ); |
|
738
|
|
- $this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
|
736
|
+ } catch (Exception $e) { |
|
|
737
|
+ WC()->session->set('refresh_totals', true); |
|
|
738
|
+ $this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
739
|
739
|
|
|
740
|
|
- if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
741
|
|
- $this->send_failed_order_email( $order_id ); |
|
|
740
|
+ if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) { |
|
|
741
|
+ $this->send_failed_order_email($order_id); |
|
742
|
742
|
} |
|
743
|
743
|
|
|
744
|
|
- wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
|
|
744
|
+ wp_send_json(array('success' => 'false', 'msg' => $e->getMessage())); |
|
745
|
745
|
} |
|
746
|
746
|
} |
|
747
|
747
|
|
|
@@ -751,16 +751,16 @@ discard block |
|
|
block discarded – undo |
|
751
|
751
|
* @param string $source token |
|
752
|
752
|
* @return array() |
|
753
|
753
|
*/ |
|
754
|
|
- protected function generate_payment_request( $order, $source ) { |
|
|
754
|
+ protected function generate_payment_request($order, $source) { |
|
755
|
755
|
$post_data = array(); |
|
756
|
|
- $post_data['currency'] = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ); |
|
757
|
|
- $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
758
|
|
- $post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() ); |
|
|
756
|
+ $post_data['currency'] = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency()); |
|
|
757
|
+ $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
|
758
|
+ $post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number()); |
|
759
|
759
|
$post_data['capture'] = $this->capture ? 'true' : 'false'; |
|
760
|
760
|
|
|
761
|
|
- $billing_email = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email(); |
|
|
761
|
+ $billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email(); |
|
762
|
762
|
|
|
763
|
|
- if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
|
763
|
+ if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
764
|
764
|
$post_data['receipt_email'] = $billing_email; |
|
765
|
765
|
} |
|
766
|
766
|
|
|
@@ -775,7 +775,7 @@ discard block |
|
|
block discarded – undo |
|
775
|
775
|
* @param WC_Order $order |
|
776
|
776
|
* @param object $source |
|
777
|
777
|
*/ |
|
778
|
|
- return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order ); |
|
|
778
|
+ return apply_filters('wc_stripe_generate_payment_request', $post_data, $order); |
|
779
|
779
|
} |
|
780
|
780
|
|
|
781
|
781
|
/** |
|
@@ -784,14 +784,14 @@ discard block |
|
|
block discarded – undo |
|
784
|
784
|
* @since 3.1.0 |
|
785
|
785
|
* @version 3.1.0 |
|
786
|
786
|
*/ |
|
787
|
|
- public function build_shipping_methods( $shipping_methods ) { |
|
788
|
|
- if ( empty( $shipping_methods ) ) { |
|
|
787
|
+ public function build_shipping_methods($shipping_methods) { |
|
|
788
|
+ if (empty($shipping_methods)) { |
|
789
|
789
|
return array(); |
|
790
|
790
|
} |
|
791
|
791
|
|
|
792
|
792
|
$shipping = array(); |
|
793
|
793
|
|
|
794
|
|
- foreach ( $shipping_methods as $method ) { |
|
|
794
|
+ foreach ($shipping_methods as $method) { |
|
795
|
795
|
$shipping[] = array( |
|
796
|
796
|
'label' => $method['label'], |
|
797
|
797
|
'detail' => '', |
|
@@ -810,70 +810,70 @@ discard block |
|
|
block discarded – undo |
|
810
|
810
|
* @version 3.2.0 |
|
811
|
811
|
*/ |
|
812
|
812
|
public function build_line_items() { |
|
813
|
|
- if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
814
|
|
- define( 'WOOCOMMERCE_CART', true ); |
|
|
813
|
+ if ( ! defined('WOOCOMMERCE_CART')) { |
|
|
814
|
+ define('WOOCOMMERCE_CART', true); |
|
815
|
815
|
} |
|
816
|
816
|
|
|
817
|
817
|
$items = array(); |
|
818
|
818
|
$subtotal = 0; |
|
819
|
819
|
|
|
820
|
|
- foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { |
|
821
|
|
- $amount = wc_format_decimal( $cart_item['line_subtotal'], $this->dp ); |
|
822
|
|
- $subtotal += $cart_item['line_subtotal']; |
|
|
820
|
+ foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { |
|
|
821
|
+ $amount = wc_format_decimal($cart_item['line_subtotal'], $this->dp); |
|
|
822
|
+ $subtotal += $cart_item['line_subtotal']; |
|
823
|
823
|
$quantity_label = 1 < $cart_item['quantity'] ? ' (x' . $cart_item['quantity'] . ')' : ''; |
|
824
|
824
|
|
|
825
|
|
- $product_name = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->post->post_title : $cart_item['data']->get_name(); |
|
|
825
|
+ $product_name = version_compare(WC_VERSION, '3.0', '<') ? $cart_item['data']->post->post_title : $cart_item['data']->get_name(); |
|
826
|
826
|
|
|
827
|
827
|
$item = array( |
|
828
|
828
|
'type' => 'final', |
|
829
|
829
|
'label' => $product_name . $quantity_label, |
|
830
|
|
- 'amount' => wc_format_decimal( $amount, $this->dp ), |
|
|
830
|
+ 'amount' => wc_format_decimal($amount, $this->dp), |
|
831
|
831
|
); |
|
832
|
832
|
|
|
833
|
833
|
$items[] = $item; |
|
834
|
834
|
} |
|
835
|
835
|
|
|
836
|
836
|
// Default show only subtotal instead of itemization. |
|
837
|
|
- if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) { |
|
|
837
|
+ if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) { |
|
838
|
838
|
$items = array(); |
|
839
|
839
|
$items[] = array( |
|
840
|
840
|
'type' => 'final', |
|
841
|
|
- 'label' => esc_html( __( 'Sub-Total', 'woocommerce-gateway-stripe' ) ), |
|
842
|
|
- 'amount' => wc_format_decimal( $subtotal, $this->dp ), |
|
|
841
|
+ 'label' => esc_html(__('Sub-Total', 'woocommerce-gateway-stripe')), |
|
|
842
|
+ 'amount' => wc_format_decimal($subtotal, $this->dp), |
|
843
|
843
|
); |
|
844
|
844
|
} |
|
845
|
845
|
|
|
846
|
|
- $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $this->dp ); |
|
847
|
|
- $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $this->dp ); |
|
848
|
|
- $shipping = wc_format_decimal( WC()->cart->shipping_total, $this->dp ); |
|
849
|
|
- $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $this->dp ) + $discounts; |
|
850
|
|
- $order_total = wc_format_decimal( $item_total + $tax + $shipping, $this->dp ); |
|
|
846
|
+ $discounts = wc_format_decimal(WC()->cart->get_cart_discount_total(), $this->dp); |
|
|
847
|
+ $tax = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $this->dp); |
|
|
848
|
+ $shipping = wc_format_decimal(WC()->cart->shipping_total, $this->dp); |
|
|
849
|
+ $item_total = wc_format_decimal(WC()->cart->cart_contents_total, $this->dp) + $discounts; |
|
|
850
|
+ $order_total = wc_format_decimal($item_total + $tax + $shipping, $this->dp); |
|
851
|
851
|
|
|
852
|
|
- if ( wc_tax_enabled() ) { |
|
|
852
|
+ if (wc_tax_enabled()) { |
|
853
|
853
|
$items[] = array( |
|
854
|
854
|
'type' => 'final', |
|
855
|
|
- 'label' => esc_html( __( 'Tax', 'woocommerce-gateway-stripe' ) ), |
|
|
855
|
+ 'label' => esc_html(__('Tax', 'woocommerce-gateway-stripe')), |
|
856
|
856
|
'amount' => $tax, |
|
857
|
857
|
); |
|
858
|
858
|
} |
|
859
|
859
|
|
|
860
|
|
- if ( WC()->cart->needs_shipping() && $this->is_shipping_enabled ) { |
|
|
860
|
+ if (WC()->cart->needs_shipping() && $this->is_shipping_enabled) { |
|
861
|
861
|
$items[] = array( |
|
862
|
862
|
'type' => 'final', |
|
863
|
|
- 'label' => esc_html( __( 'Shipping', 'woocommerce-gateway-stripe' ) ), |
|
|
863
|
+ 'label' => esc_html(__('Shipping', 'woocommerce-gateway-stripe')), |
|
864
|
864
|
'amount' => $shipping, |
|
865
|
865
|
); |
|
866
|
866
|
} |
|
867
|
867
|
|
|
868
|
|
- if ( WC()->cart->has_discount() ) { |
|
|
868
|
+ if (WC()->cart->has_discount()) { |
|
869
|
869
|
$items[] = array( |
|
870
|
870
|
'type' => 'final', |
|
871
|
|
- 'label' => esc_html( __( 'Discount', 'woocommerce-gateway-stripe' ) ), |
|
|
871
|
+ 'label' => esc_html(__('Discount', 'woocommerce-gateway-stripe')), |
|
872
|
872
|
'amount' => '-' . $discounts, |
|
873
|
873
|
); |
|
874
|
874
|
} |
|
875
|
875
|
|
|
876
|
|
- return array( 'line_items' => $items, 'total' => $order_total ); |
|
|
876
|
+ return array('line_items' => $items, 'total' => $order_total); |
|
877
|
877
|
} |
|
878
|
878
|
|
|
879
|
879
|
/** |
|
@@ -884,28 +884,28 @@ discard block |
|
|
block discarded – undo |
|
884
|
884
|
* @param array $data |
|
885
|
885
|
* @return object $order |
|
886
|
886
|
*/ |
|
887
|
|
- public function create_order( $data = array() ) { |
|
888
|
|
- if ( empty( $data ) ) { |
|
889
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
|
887
|
+ public function create_order($data = array()) { |
|
|
888
|
+ if (empty($data)) { |
|
|
889
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
890
|
890
|
} |
|
891
|
891
|
|
|
892
|
|
- if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
893
|
|
- define( 'WOOCOMMERCE_CART', true ); |
|
|
892
|
+ if ( ! defined('WOOCOMMERCE_CART')) { |
|
|
893
|
+ define('WOOCOMMERCE_CART', true); |
|
894
|
894
|
} |
|
895
|
895
|
|
|
896
|
896
|
$order = wc_create_order(); |
|
897
|
|
- $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
|
897
|
+ $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
898
|
898
|
|
|
899
|
|
- if ( is_wp_error( $order ) ) { |
|
900
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
901
|
|
- } elseif ( false === $order ) { |
|
902
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
|
|
899
|
+ if (is_wp_error($order)) { |
|
|
900
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
|
901
|
+ } elseif (false === $order) { |
|
|
902
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521)); |
|
903
|
903
|
} else { |
|
904
|
|
- do_action( 'woocommerce_new_order', $order_id ); |
|
|
904
|
+ do_action('woocommerce_new_order', $order_id); |
|
905
|
905
|
} |
|
906
|
906
|
|
|
907
|
907
|
// Store the line items to the new/resumed order |
|
908
|
|
- foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
|
908
|
+ foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
909
|
909
|
$item_id = $order->add_product( |
|
910
|
910
|
$values['data'], |
|
911
|
911
|
$values['quantity'], |
|
@@ -921,25 +921,25 @@ discard block |
|
|
block discarded – undo |
|
921
|
921
|
) |
|
922
|
922
|
); |
|
923
|
923
|
|
|
924
|
|
- if ( ! $item_id ) { |
|
925
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
|
|
924
|
+ if ( ! $item_id) { |
|
|
925
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525)); |
|
926
|
926
|
} |
|
927
|
927
|
|
|
928
|
928
|
// Allow plugins to add order item meta |
|
929
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
930
|
|
- do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
|
|
929
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
930
|
+ do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key); |
|
931
|
931
|
} else { |
|
932
|
|
- do_action( 'woocommerce_new_order_item', $item_id, wc_get_product( $item_id ), $order->get_id() ); |
|
|
932
|
+ do_action('woocommerce_new_order_item', $item_id, wc_get_product($item_id), $order->get_id()); |
|
933
|
933
|
} |
|
934
|
934
|
} |
|
935
|
935
|
|
|
936
|
936
|
// Store fees |
|
937
|
|
- foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
|
938
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
939
|
|
- $item_id = $order->add_fee( $fee ); |
|
|
937
|
+ foreach (WC()->cart->get_fees() as $fee_key => $fee) { |
|
|
938
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
939
|
+ $item_id = $order->add_fee($fee); |
|
940
|
940
|
} else { |
|
941
|
941
|
$item = new WC_Order_Item_Fee(); |
|
942
|
|
- $item->set_props( array( |
|
|
942
|
+ $item->set_props(array( |
|
943
|
943
|
'name' => $fee->name, |
|
944
|
944
|
'tax_class' => $fee->taxable ? $fee->tax_class : 0, |
|
945
|
945
|
'total' => $fee->amount, |
|
@@ -948,79 +948,79 @@ discard block |
|
|
block discarded – undo |
|
948
|
948
|
'total' => $fee->tax_data, |
|
949
|
949
|
), |
|
950
|
950
|
'order_id' => $order->get_id(), |
|
951
|
|
- ) ); |
|
|
951
|
+ )); |
|
952
|
952
|
$item_id = $item->save(); |
|
953
|
|
- $order->add_item( $item ); |
|
|
953
|
+ $order->add_item($item); |
|
954
|
954
|
} |
|
955
|
955
|
|
|
956
|
|
- if ( ! $item_id ) { |
|
957
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
|
|
956
|
+ if ( ! $item_id) { |
|
|
957
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526)); |
|
958
|
958
|
} |
|
959
|
959
|
|
|
960
|
960
|
// Allow plugins to add order item meta to fees |
|
961
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
962
|
|
- do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
|
|
961
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
962
|
+ do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key); |
|
963
|
963
|
} else { |
|
964
|
|
- do_action( 'woocommerce_new_order_item', $item_id, $fee, $order->get_id() ); |
|
|
964
|
+ do_action('woocommerce_new_order_item', $item_id, $fee, $order->get_id()); |
|
965
|
965
|
} |
|
966
|
966
|
} |
|
967
|
967
|
|
|
968
|
968
|
// Store tax rows |
|
969
|
|
- foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
|
970
|
|
- $tax_amount = WC()->cart->get_tax_amount( $tax_rate_id ); |
|
971
|
|
- $shipping_tax_amount = WC()->cart->get_shipping_tax_amount( $tax_rate_id ); |
|
|
969
|
+ foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) { |
|
|
970
|
+ $tax_amount = WC()->cart->get_tax_amount($tax_rate_id); |
|
|
971
|
+ $shipping_tax_amount = WC()->cart->get_shipping_tax_amount($tax_rate_id); |
|
972
|
972
|
|
|
973
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
974
|
|
- $item_id = $order->add_tax( $tax_rate_id, $tax_amount, $shipping_tax_amount ); |
|
|
973
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
974
|
+ $item_id = $order->add_tax($tax_rate_id, $tax_amount, $shipping_tax_amount); |
|
975
|
975
|
} else { |
|
976
|
976
|
$item = new WC_Order_Item_Tax(); |
|
977
|
|
- $item->set_props( array( |
|
|
977
|
+ $item->set_props(array( |
|
978
|
978
|
'rate_id' => $tax_rate_id, |
|
979
|
979
|
'tax_total' => $tax_amount, |
|
980
|
980
|
'shipping_tax_total' => $shipping_tax_amount, |
|
981
|
|
- ) ); |
|
982
|
|
- $item->set_rate( $tax_rate_id ); |
|
983
|
|
- $item->set_order_id( $order->get_id() ); |
|
|
981
|
+ )); |
|
|
982
|
+ $item->set_rate($tax_rate_id); |
|
|
983
|
+ $item->set_order_id($order->get_id()); |
|
984
|
984
|
$item_id = $item->save(); |
|
985
|
|
- $order->add_item( $item ); |
|
|
985
|
+ $order->add_item($item); |
|
986
|
986
|
} |
|
987
|
987
|
|
|
988
|
|
- if ( $tax_rate_id && ! $item_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { |
|
989
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
|
|
988
|
+ if ($tax_rate_id && ! $item_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) { |
|
|
989
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528)); |
|
990
|
990
|
} |
|
991
|
991
|
} |
|
992
|
992
|
|
|
993
|
993
|
// Store coupons |
|
994
|
|
- $discount = WC()->cart->get_coupon_discount_amount( $code ); |
|
995
|
|
- $discount_tax = WC()->cart->get_coupon_discount_tax_amount( $code ); |
|
|
994
|
+ $discount = WC()->cart->get_coupon_discount_amount($code); |
|
|
995
|
+ $discount_tax = WC()->cart->get_coupon_discount_tax_amount($code); |
|
996
|
996
|
|
|
997
|
|
- foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
|
998
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
999
|
|
- $coupon_id = $order->add_coupon( $code, $discount, $discount_tax ); |
|
|
997
|
+ foreach (WC()->cart->get_coupons() as $code => $coupon) { |
|
|
998
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
999
|
+ $coupon_id = $order->add_coupon($code, $discount, $discount_tax); |
|
1000
|
1000
|
} else { |
|
1001
|
1001
|
$item = new WC_Order_Item_Coupon(); |
|
1002
|
|
- $item->set_props( array( |
|
|
1002
|
+ $item->set_props(array( |
|
1003
|
1003
|
'code' => $code, |
|
1004
|
1004
|
'discount' => $discount, |
|
1005
|
1005
|
'discount_tax' => $discount_tax, |
|
1006
|
1006
|
'order_id' => $order->get_id(), |
|
1007
|
|
- ) ); |
|
|
1007
|
+ )); |
|
1008
|
1008
|
$coupon_id = $item->save(); |
|
1009
|
|
- $order->add_item( $item ); |
|
|
1009
|
+ $order->add_item($item); |
|
1010
|
1010
|
} |
|
1011
|
1011
|
|
|
1012
|
|
- if ( ! $coupon_id ) { |
|
1013
|
|
- throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
|
|
1012
|
+ if ( ! $coupon_id) { |
|
|
1013
|
+ throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529)); |
|
1014
|
1014
|
} |
|
1015
|
1015
|
} |
|
1016
|
1016
|
|
|
1017
|
1017
|
// Billing address |
|
1018
|
1018
|
$billing_address = array(); |
|
1019
|
|
- if ( ! empty( $data['token']['card'] ) ) { |
|
|
1019
|
+ if ( ! empty($data['token']['card'])) { |
|
1020
|
1020
|
// Name from Stripe is a full name string. |
|
1021
|
|
- $name = explode( ' ', $data['token']['card']['name'] ); |
|
1022
|
|
- $lastname = array_pop( $name ); |
|
1023
|
|
- $firstname = implode( ' ', $name ); |
|
|
1021
|
+ $name = explode(' ', $data['token']['card']['name']); |
|
|
1022
|
+ $lastname = array_pop($name); |
|
|
1023
|
+ $firstname = implode(' ', $name); |
|
1024
|
1024
|
$billing_address['first_name'] = $firstname; |
|
1025
|
1025
|
$billing_address['last_name'] = $lastname; |
|
1026
|
1026
|
$billing_address['email'] = $data['shippingContact']['emailAddress']; |
|
@@ -1035,7 +1035,7 @@ discard block |
|
|
block discarded – undo |
|
1035
|
1035
|
|
|
1036
|
1036
|
// Shipping address. |
|
1037
|
1037
|
$shipping_address = array(); |
|
1038
|
|
- if ( WC()->cart->needs_shipping() && $this->is_shipping_enabled && ! empty( $data['shippingContact'] ) ) { |
|
|
1038
|
+ if (WC()->cart->needs_shipping() && $this->is_shipping_enabled && ! empty($data['shippingContact'])) { |
|
1039
|
1039
|
$shipping_address['first_name'] = $data['shippingContact']['givenName']; |
|
1040
|
1040
|
$shipping_address['last_name'] = $data['shippingContact']['familyName']; |
|
1041
|
1041
|
$shipping_address['email'] = $data['shippingContact']['emailAddress']; |
|
@@ -1046,7 +1046,7 @@ discard block |
|
|
block discarded – undo |
|
1046
|
1046
|
$shipping_address['city'] = $data['shippingContact']['locality']; |
|
1047
|
1047
|
$shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
|
1048
|
1048
|
$shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
|
1049
|
|
- } elseif ( ! empty( $data['shippingContact'] ) ) { |
|
|
1049
|
+ } elseif ( ! empty($data['shippingContact'])) { |
|
1050
|
1050
|
$shipping_address['first_name'] = $firstname; |
|
1051
|
1051
|
$shipping_address['last_name'] = $lastname; |
|
1052
|
1052
|
$shipping_address['email'] = $data['shippingContact']['emailAddress']; |
|
@@ -1059,33 +1059,33 @@ discard block |
|
|
block discarded – undo |
|
1059
|
1059
|
$shipping_address['postcode'] = $data['token']['card']['address_zip']; |
|
1060
|
1060
|
} |
|
1061
|
1061
|
|
|
1062
|
|
- $order->set_address( $billing_address, 'billing' ); |
|
1063
|
|
- $order->set_address( $shipping_address, 'shipping' ); |
|
|
1062
|
+ $order->set_address($billing_address, 'billing'); |
|
|
1063
|
+ $order->set_address($shipping_address, 'shipping'); |
|
1064
|
1064
|
|
|
1065
|
|
- WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
|
|
1065
|
+ WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages()); |
|
1066
|
1066
|
|
|
1067
|
1067
|
// Get the rate object selected by user. |
|
1068
|
|
- foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
|
1069
|
|
- foreach ( $package['rates'] as $key => $rate ) { |
|
|
1068
|
+ foreach (WC()->shipping->get_packages() as $package_key => $package) { |
|
|
1069
|
+ foreach ($package['rates'] as $key => $rate) { |
|
1070
|
1070
|
// Loop through user chosen shipping methods. |
|
1071
|
|
- foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
|
1072
|
|
- if ( $method === $key ) { |
|
1073
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
1074
|
|
- $order->add_shipping( $rate ); |
|
|
1071
|
+ foreach (WC()->session->get('chosen_shipping_methods') as $method) { |
|
|
1072
|
+ if ($method === $key) { |
|
|
1073
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
1074
|
+ $order->add_shipping($rate); |
|
1075
|
1075
|
} else { |
|
1076
|
1076
|
$item = new WC_Order_Item_Shipping(); |
|
1077
|
|
- $item->set_props( array( |
|
|
1077
|
+ $item->set_props(array( |
|
1078
|
1078
|
'method_title' => $rate->label, |
|
1079
|
1079
|
'method_id' => $rate->id, |
|
1080
|
|
- 'total' => wc_format_decimal( $rate->cost ), |
|
|
1080
|
+ 'total' => wc_format_decimal($rate->cost), |
|
1081
|
1081
|
'taxes' => $rate->taxes, |
|
1082
|
1082
|
'order_id' => $order->get_id(), |
|
1083
|
|
- ) ); |
|
1084
|
|
- foreach ( $rate->get_meta_data() as $key => $value ) { |
|
1085
|
|
- $item->add_meta_data( $key, $value, true ); |
|
|
1083
|
+ )); |
|
|
1084
|
+ foreach ($rate->get_meta_data() as $key => $value) { |
|
|
1085
|
+ $item->add_meta_data($key, $value, true); |
|
1086
|
1086
|
} |
|
1087
|
1087
|
$item->save(); |
|
1088
|
|
- $order->add_item( $item ); |
|
|
1088
|
+ $order->add_item($item); |
|
1089
|
1089
|
} |
|
1090
|
1090
|
} |
|
1091
|
1091
|
} |
|
@@ -1093,37 +1093,37 @@ discard block |
|
|
block discarded – undo |
|
1093
|
1093
|
} |
|
1094
|
1094
|
|
|
1095
|
1095
|
$available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
|
1096
|
|
- $order->set_payment_method( $available_gateways['stripe'] ); |
|
|
1096
|
+ $order->set_payment_method($available_gateways['stripe']); |
|
1097
|
1097
|
WC()->cart->calculate_totals(); |
|
1098
|
1098
|
|
|
1099
|
|
- if ( version_compare( WC_VERSION, '3.0', '<' ) ) { |
|
1100
|
|
- $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
|
1101
|
|
- $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
|
1102
|
|
- $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
|
1103
|
|
- $order->set_total( WC()->cart->tax_total, 'tax' ); |
|
1104
|
|
- $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
|
1105
|
|
- $order->set_total( WC()->cart->total ); |
|
|
1099
|
+ if (version_compare(WC_VERSION, '3.0', '<')) { |
|
|
1100
|
+ $order->set_total(WC()->cart->shipping_total, 'shipping'); |
|
|
1101
|
+ $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); |
|
|
1102
|
+ $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); |
|
|
1103
|
+ $order->set_total(WC()->cart->tax_total, 'tax'); |
|
|
1104
|
+ $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); |
|
|
1105
|
+ $order->set_total(WC()->cart->total); |
|
1106
|
1106
|
} else { |
|
1107
|
|
- $order->set_shipping_total( WC()->cart->shipping_total ); |
|
1108
|
|
- $order->set_discount_total( WC()->cart->get_cart_discount_total() ); |
|
1109
|
|
- $order->set_discount_tax( WC()->cart->get_cart_discount_tax_total() ); |
|
1110
|
|
- $order->set_cart_tax( WC()->cart->tax_total ); |
|
1111
|
|
- $order->set_shipping_tax( WC()->cart->shipping_tax_total ); |
|
1112
|
|
- |
|
1113
|
|
- $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $this->dp ); |
|
1114
|
|
- $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $this->dp ); |
|
1115
|
|
- $shipping = wc_format_decimal( WC()->cart->shipping_total, $this->dp ); |
|
1116
|
|
- $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $this->dp ) + $discounts; |
|
1117
|
|
- $order_total = wc_format_decimal( $item_total + $tax + $shipping, $this->dp ); |
|
1118
|
|
- |
|
1119
|
|
- $order->set_total( $order_total ); |
|
|
1107
|
+ $order->set_shipping_total(WC()->cart->shipping_total); |
|
|
1108
|
+ $order->set_discount_total(WC()->cart->get_cart_discount_total()); |
|
|
1109
|
+ $order->set_discount_tax(WC()->cart->get_cart_discount_tax_total()); |
|
|
1110
|
+ $order->set_cart_tax(WC()->cart->tax_total); |
|
|
1111
|
+ $order->set_shipping_tax(WC()->cart->shipping_tax_total); |
|
|
1112
|
+ |
|
|
1113
|
+ $discounts = wc_format_decimal(WC()->cart->get_cart_discount_total(), $this->dp); |
|
|
1114
|
+ $tax = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $this->dp); |
|
|
1115
|
+ $shipping = wc_format_decimal(WC()->cart->shipping_total, $this->dp); |
|
|
1116
|
+ $item_total = wc_format_decimal(WC()->cart->cart_contents_total, $this->dp) + $discounts; |
|
|
1117
|
+ $order_total = wc_format_decimal($item_total + $tax + $shipping, $this->dp); |
|
|
1118
|
+ |
|
|
1119
|
+ $order->set_total($order_total); |
|
1120
|
1120
|
$order->save(); |
|
1121
|
1121
|
} |
|
1122
|
1122
|
|
|
1123
|
1123
|
// If we got here, the order was created without problems! |
|
1124
|
|
- wc_transaction_query( 'commit' ); |
|
|
1124
|
+ wc_transaction_query('commit'); |
|
1125
|
1125
|
|
|
1126
|
|
- do_action( 'woocommerce_checkout_update_order_meta', $order_id, array() ); |
|
|
1126
|
+ do_action('woocommerce_checkout_update_order_meta', $order_id, array()); |
|
1127
|
1127
|
|
|
1128
|
1128
|
return $order; |
|
1129
|
1129
|
} |
|
@@ -1136,9 +1136,9 @@ discard block |
|
|
block discarded – undo |
|
1136
|
1136
|
* |
|
1137
|
1137
|
* @param string $message |
|
1138
|
1138
|
*/ |
|
1139
|
|
- public function log( $message ) { |
|
1140
|
|
- if ( $this->logging ) { |
|
1141
|
|
- WC_Stripe::log( 'Apple Pay: ' . $message ); |
|
|
1139
|
+ public function log($message) { |
|
|
1140
|
+ if ($this->logging) { |
|
|
1141
|
+ WC_Stripe::log('Apple Pay: ' . $message); |
|
1142
|
1142
|
} |
|
1143
|
1143
|
} |
|
1144
|
1144
|
} |