@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | public function __construct() { |
48 | 48 | self::$_this = $this; |
49 | 49 | |
50 | - $this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' ); |
|
50 | + $this->_gateway_settings = get_option('woocommerce_stripe_settings', ''); |
|
51 | 51 | |
52 | - $this->statement_descriptor = ! empty( $this->_gateway_settings['statement_descriptor'] ) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
|
52 | + $this->statement_descriptor = ! empty($this->_gateway_settings['statement_descriptor']) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES); |
|
53 | 53 | |
54 | 54 | $this->init(); |
55 | 55 | } |
@@ -66,47 +66,47 @@ discard block |
||
66 | 66 | * @version 3.1.0 |
67 | 67 | */ |
68 | 68 | public function init() { |
69 | - add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) ); |
|
70 | - add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) ); |
|
69 | + add_action('wp_enqueue_scripts', array($this, 'cart_scripts')); |
|
70 | + add_action('wp_enqueue_scripts', array($this, 'single_scripts')); |
|
71 | 71 | |
72 | 72 | /** |
73 | 73 | * In order to display the Apple Pay button in the correct position, |
74 | 74 | * a new hook was added to WooCommerce 2.7. In older versions of WooCommerce, |
75 | 75 | * CSS is used to position the button. |
76 | 76 | */ |
77 | - if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) { |
|
78 | - add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 ); |
|
79 | - add_filter( 'body_class', array( $this, 'body_class' ) ); |
|
77 | + if (version_compare(WC_VERSION, '3.0.0', '<')) { |
|
78 | + add_action('woocommerce_after_add_to_cart_button', array($this, 'display_apple_pay_button'), 1); |
|
79 | + add_filter('body_class', array($this, 'body_class')); |
|
80 | 80 | } else { |
81 | - add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'display_apple_pay_button' ), 1 ); |
|
82 | - } |
|
83 | - |
|
84 | - add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 ); |
|
85 | - add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
|
86 | - add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 ); |
|
87 | - add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
|
88 | - add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) ); |
|
89 | - add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) ); |
|
90 | - add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) ); |
|
91 | - add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) ); |
|
92 | - add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) ); |
|
93 | - add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 ); |
|
81 | + add_action('woocommerce_after_add_to_cart_quantity', array($this, 'display_apple_pay_button'), 1); |
|
82 | + } |
|
83 | + |
|
84 | + add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 1); |
|
85 | + add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_separator_html'), 2); |
|
86 | + add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button'), 1); |
|
87 | + add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_separator_html'), 2); |
|
88 | + add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay')); |
|
89 | + add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart')); |
|
90 | + add_action('wc_ajax_wc_stripe_generate_apple_pay_single', array($this, 'generate_apple_pay_single')); |
|
91 | + add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods')); |
|
92 | + add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method')); |
|
93 | + add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | 97 | * Filters the gateway title to reflect Apple Pay. |
98 | 98 | * |
99 | 99 | */ |
100 | - public function filter_gateway_title( $title, $id ) { |
|
100 | + public function filter_gateway_title($title, $id) { |
|
101 | 101 | global $post; |
102 | 102 | |
103 | - if ( ! is_object( $post ) ) { |
|
103 | + if ( ! is_object($post)) { |
|
104 | 104 | return $title; |
105 | 105 | } |
106 | 106 | |
107 | - $method_title = get_post_meta( $post->ID, '_payment_method_title', true ); |
|
107 | + $method_title = get_post_meta($post->ID, '_payment_method_title', true); |
|
108 | 108 | |
109 | - if ( 'stripe' === $id && ! empty( $method_title ) ) { |
|
109 | + if ('stripe' === $id && ! empty($method_title)) { |
|
110 | 110 | return $method_title; |
111 | 111 | } |
112 | 112 | |
@@ -120,41 +120,41 @@ discard block |
||
120 | 120 | * @version 3.1.0 |
121 | 121 | */ |
122 | 122 | public function single_scripts() { |
123 | - if ( ! is_single() ) { |
|
123 | + if ( ! is_single()) { |
|
124 | 124 | return; |
125 | 125 | } |
126 | 126 | |
127 | 127 | global $post; |
128 | 128 | |
129 | - $product = wc_get_product( $post->ID ); |
|
129 | + $product = wc_get_product($post->ID); |
|
130 | 130 | |
131 | - if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
|
131 | + if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) { |
|
132 | 132 | return; |
133 | 133 | } |
134 | 134 | |
135 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
135 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
136 | 136 | |
137 | - wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
137 | + wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
138 | 138 | |
139 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
140 | - 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 ); |
|
139 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
140 | + 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); |
|
141 | 141 | |
142 | 142 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
143 | 143 | |
144 | 144 | $stripe_params = array( |
145 | 145 | 'key' => $publishable_key, |
146 | 146 | 'currency_code' => get_woocommerce_currency(), |
147 | - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
147 | + 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
148 | 148 | 'label' => $this->statement_descriptor, |
149 | - 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
150 | - 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
151 | - 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
152 | - 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
153 | - 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
149 | + 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
150 | + 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
151 | + 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
152 | + 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
153 | + 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
154 | 154 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
155 | 155 | ); |
156 | 156 | |
157 | - wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) ); |
|
157 | + wp_localize_script('woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters('wc_stripe_apple_pay_single_params', $stripe_params)); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
@@ -164,34 +164,34 @@ discard block |
||
164 | 164 | * @version 3.1.0 |
165 | 165 | */ |
166 | 166 | public function cart_scripts() { |
167 | - if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
|
167 | + if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) { |
|
168 | 168 | return; |
169 | 169 | } |
170 | 170 | |
171 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
171 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
172 | 172 | |
173 | - wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
173 | + wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
174 | 174 | |
175 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
176 | - 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 ); |
|
175 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
176 | + 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); |
|
177 | 177 | |
178 | 178 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
179 | 179 | |
180 | 180 | $stripe_params = array( |
181 | 181 | 'key' => $publishable_key, |
182 | 182 | 'currency_code' => get_woocommerce_currency(), |
183 | - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
183 | + 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
184 | 184 | 'label' => $this->statement_descriptor, |
185 | - 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
186 | - 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
187 | - 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
188 | - 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
189 | - 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
185 | + 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
186 | + 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
187 | + 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
188 | + 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
189 | + 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
190 | 190 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
191 | 191 | 'is_cart_page' => is_cart() ? 'yes' : 'no', |
192 | 192 | ); |
193 | 193 | |
194 | - wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) ); |
|
194 | + wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params)); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -220,26 +220,26 @@ discard block |
||
220 | 220 | */ |
221 | 221 | if ( |
222 | 222 | 'yes' !== $this->_gateway_settings['apple_pay'] |
223 | - || ! isset( $gateways['stripe'] ) |
|
223 | + || ! isset($gateways['stripe']) |
|
224 | 224 | ) { |
225 | 225 | return; |
226 | 226 | } |
227 | 227 | |
228 | - if ( is_single() ) { |
|
228 | + if (is_single()) { |
|
229 | 229 | global $post; |
230 | 230 | |
231 | - $product = wc_get_product( $post->ID ); |
|
231 | + $product = wc_get_product($post->ID); |
|
232 | 232 | |
233 | - if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
|
233 | + if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) { |
|
234 | 234 | return; |
235 | 235 | } |
236 | 236 | } |
237 | 237 | |
238 | - $apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
239 | - $button_lang = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en'; |
|
238 | + $apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
239 | + $button_lang = ! empty($this->_gateway_settings['apple_pay_button_lang']) ? strtolower($this->_gateway_settings['apple_pay_button_lang']) : 'en'; |
|
240 | 240 | ?> |
241 | 241 | <div class="apple-pay-button-wrapper"> |
242 | - <button class="apple-pay-button" lang="<?php echo esc_attr( $button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button> |
|
242 | + <button class="apple-pay-button" lang="<?php echo esc_attr($button_lang); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button> |
|
243 | 243 | </div> |
244 | 244 | <?php |
245 | 245 | } |
@@ -259,22 +259,22 @@ discard block |
||
259 | 259 | */ |
260 | 260 | if ( |
261 | 261 | 'yes' !== $this->_gateway_settings['apple_pay'] |
262 | - || ! isset( $gateways['stripe'] ) |
|
262 | + || ! isset($gateways['stripe']) |
|
263 | 263 | ) { |
264 | 264 | return; |
265 | 265 | } |
266 | 266 | |
267 | - if ( is_single() ) { |
|
267 | + if (is_single()) { |
|
268 | 268 | global $post; |
269 | 269 | |
270 | - $product = wc_get_product( $post->ID ); |
|
270 | + $product = wc_get_product($post->ID); |
|
271 | 271 | |
272 | - if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
|
272 | + if ( ! in_array((version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()), $this->supported_product_types())) { |
|
273 | 273 | return; |
274 | 274 | } |
275 | 275 | } |
276 | 276 | ?> |
277 | - <p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p> |
|
277 | + <p class="apple-pay-button-checkout-separator">- <?php esc_html_e('Or', 'woocommerce-gateway-stripe'); ?> -</p> |
|
278 | 278 | <?php |
279 | 279 | } |
280 | 280 | |
@@ -286,8 +286,8 @@ discard block |
||
286 | 286 | * @param array $classes |
287 | 287 | * @return array $classes |
288 | 288 | */ |
289 | - public function body_class( $classes ) { |
|
290 | - if ( 'yes' === $this->_gateway_settings['apple_pay'] && isset( $gateways['stripe'] ) ) { |
|
289 | + public function body_class($classes) { |
|
290 | + if ('yes' === $this->_gateway_settings['apple_pay'] && isset($gateways['stripe'])) { |
|
291 | 291 | $classes[] = 'wc-stripe-legacy'; |
292 | 292 | } |
293 | 293 | return $classes; |
@@ -300,46 +300,46 @@ discard block |
||
300 | 300 | * @version 3.1.0 |
301 | 301 | */ |
302 | 302 | public function generate_apple_pay_single() { |
303 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
304 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
303 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
304 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
305 | 305 | } |
306 | 306 | |
307 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
308 | - define( 'WOOCOMMERCE_CART', true ); |
|
307 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
308 | + define('WOOCOMMERCE_CART', true); |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | WC()->shipping->reset_shipping(); |
312 | 312 | |
313 | 313 | global $post; |
314 | 314 | |
315 | - $product = wc_get_product( $post->ID ); |
|
316 | - $qty = absint( $_POST['qty'] ); |
|
315 | + $product = wc_get_product($post->ID); |
|
316 | + $qty = absint($_POST['qty']); |
|
317 | 317 | |
318 | 318 | /** |
319 | 319 | * If this page is single product page, we need to simulate |
320 | 320 | * adding the product to the cart taken account if it is a |
321 | 321 | * simple or variable product. |
322 | 322 | */ |
323 | - if ( is_single() ) { |
|
323 | + if (is_single()) { |
|
324 | 324 | // First empty the cart to prevent wrong calculation. |
325 | 325 | WC()->cart->empty_cart(); |
326 | 326 | |
327 | - if ( 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) { |
|
328 | - $attributes = array_map( 'wc_clean', $_POST['attributes'] ); |
|
327 | + if ('variable' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type()) && isset($_POST['attributes'])) { |
|
328 | + $attributes = array_map('wc_clean', $_POST['attributes']); |
|
329 | 329 | |
330 | - $variation_id = $product->get_matching_variation( $attributes ); |
|
330 | + $variation_id = $product->get_matching_variation($attributes); |
|
331 | 331 | |
332 | - WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes ); |
|
332 | + WC()->cart->add_to_cart($product->get_id(), $qty, $variation_id, $attributes); |
|
333 | 333 | } |
334 | 334 | |
335 | - if ( 'simple' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ) ) { |
|
336 | - WC()->cart->add_to_cart( $product->get_id(), $qty ); |
|
335 | + if ('simple' === (version_compare(WC_VERSION, '3.0.0', '<') ? $product->product_type : $product->get_type())) { |
|
336 | + WC()->cart->add_to_cart($product->get_id(), $qty); |
|
337 | 337 | } |
338 | 338 | } |
339 | 339 | |
340 | 340 | WC()->cart->calculate_totals(); |
341 | 341 | |
342 | - wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
342 | + wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | /** |
@@ -349,11 +349,11 @@ discard block |
||
349 | 349 | * @version 3.1.0 |
350 | 350 | */ |
351 | 351 | public function generate_apple_pay_cart() { |
352 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
353 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
352 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
353 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
354 | 354 | } |
355 | 355 | |
356 | - wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
356 | + wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
357 | 357 | } |
358 | 358 | |
359 | 359 | /** |
@@ -363,29 +363,29 @@ discard block |
||
363 | 363 | * @version 3.1.0 |
364 | 364 | * @param array $address |
365 | 365 | */ |
366 | - public function calculate_shipping( $address = array() ) { |
|
367 | - $country = strtoupper( $address['countryCode'] ); |
|
368 | - $state = strtoupper( $address['administrativeArea'] ); |
|
366 | + public function calculate_shipping($address = array()) { |
|
367 | + $country = strtoupper($address['countryCode']); |
|
368 | + $state = strtoupper($address['administrativeArea']); |
|
369 | 369 | $postcode = $address['postalCode']; |
370 | 370 | $city = $address['locality']; |
371 | 371 | |
372 | 372 | WC()->shipping->reset_shipping(); |
373 | 373 | |
374 | - if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
375 | - throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
376 | - } elseif ( $postcode ) { |
|
377 | - $postcode = wc_format_postcode( $postcode, $country ); |
|
374 | + if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) { |
|
375 | + throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe')); |
|
376 | + } elseif ($postcode) { |
|
377 | + $postcode = wc_format_postcode($postcode, $country); |
|
378 | 378 | } |
379 | 379 | |
380 | - if ( $country ) { |
|
381 | - WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
382 | - WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
380 | + if ($country) { |
|
381 | + WC()->customer->set_location($country, $state, $postcode, $city); |
|
382 | + WC()->customer->set_shipping_location($country, $state, $postcode, $city); |
|
383 | 383 | } else { |
384 | 384 | WC()->customer->set_to_base(); |
385 | 385 | WC()->customer->set_shipping_to_base(); |
386 | 386 | } |
387 | 387 | |
388 | - WC()->customer->calculated_shipping( true ); |
|
388 | + WC()->customer->calculated_shipping(true); |
|
389 | 389 | |
390 | 390 | /** |
391 | 391 | * Set the shipping package. |
@@ -407,17 +407,17 @@ discard block |
||
407 | 407 | $packages[0]['destination']['postcode'] = $postcode; |
408 | 408 | $packages[0]['destination']['city'] = $city; |
409 | 409 | |
410 | - foreach ( WC()->cart->get_cart() as $item ) { |
|
411 | - if ( $item['data']->needs_shipping() ) { |
|
412 | - if ( isset( $item['line_total'] ) ) { |
|
410 | + foreach (WC()->cart->get_cart() as $item) { |
|
411 | + if ($item['data']->needs_shipping()) { |
|
412 | + if (isset($item['line_total'])) { |
|
413 | 413 | $packages[0]['contents_cost'] += $item['line_total']; |
414 | 414 | } |
415 | 415 | } |
416 | 416 | } |
417 | 417 | |
418 | - $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
|
418 | + $packages = apply_filters('woocommerce_cart_shipping_packages', $packages); |
|
419 | 419 | |
420 | - WC()->shipping->calculate_shipping( $packages ); |
|
420 | + WC()->shipping->calculate_shipping($packages); |
|
421 | 421 | } |
422 | 422 | |
423 | 423 | /** |
@@ -427,18 +427,18 @@ discard block |
||
427 | 427 | * @version 3.1.0 |
428 | 428 | */ |
429 | 429 | public function get_shipping_methods() { |
430 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) { |
|
431 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
430 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) { |
|
431 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
432 | 432 | } |
433 | 433 | |
434 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
435 | - define( 'WOOCOMMERCE_CART', true ); |
|
434 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
435 | + define('WOOCOMMERCE_CART', true); |
|
436 | 436 | } |
437 | 437 | |
438 | 438 | try { |
439 | - $address = array_map( 'wc_clean', $_POST['address'] ); |
|
439 | + $address = array_map('wc_clean', $_POST['address']); |
|
440 | 440 | |
441 | - $this->calculate_shipping( $address ); |
|
441 | + $this->calculate_shipping($address); |
|
442 | 442 | |
443 | 443 | // Set the shipping options. |
444 | 444 | $currency = get_woocommerce_currency(); |
@@ -446,13 +446,13 @@ discard block |
||
446 | 446 | |
447 | 447 | $packages = WC()->shipping->get_packages(); |
448 | 448 | |
449 | - if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
|
450 | - foreach ( $packages as $package_key => $package ) { |
|
451 | - if ( empty( $package['rates'] ) ) { |
|
452 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
449 | + if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) { |
|
450 | + foreach ($packages as $package_key => $package) { |
|
451 | + if (empty($package['rates'])) { |
|
452 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
453 | 453 | } |
454 | 454 | |
455 | - foreach ( $package['rates'] as $key => $rate ) { |
|
455 | + foreach ($package['rates'] as $key => $rate) { |
|
456 | 456 | $data[] = array( |
457 | 457 | 'id' => $rate->id, |
458 | 458 | 'label' => $rate->label, |
@@ -466,16 +466,16 @@ discard block |
||
466 | 466 | } |
467 | 467 | |
468 | 468 | // Auto select the first shipping method. |
469 | - WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) ); |
|
469 | + WC()->session->set('chosen_shipping_methods', array($data[0]['id'])); |
|
470 | 470 | |
471 | 471 | WC()->cart->calculate_totals(); |
472 | 472 | |
473 | - wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
473 | + wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
474 | 474 | } else { |
475 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
475 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
476 | 476 | } |
477 | - } catch ( Exception $e ) { |
|
478 | - wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
477 | + } catch (Exception $e) { |
|
478 | + wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
479 | 479 | } |
480 | 480 | } |
481 | 481 | |
@@ -486,29 +486,29 @@ discard block |
||
486 | 486 | * @version 3.1.0 |
487 | 487 | */ |
488 | 488 | public function update_shipping_method() { |
489 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
490 | - define( 'WOOCOMMERCE_CART', true ); |
|
489 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
490 | + define('WOOCOMMERCE_CART', true); |
|
491 | 491 | } |
492 | 492 | |
493 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) { |
|
494 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
493 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) { |
|
494 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
495 | 495 | } |
496 | 496 | |
497 | - $selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] ); |
|
497 | + $selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']); |
|
498 | 498 | |
499 | - WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) ); |
|
499 | + WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier'])); |
|
500 | 500 | |
501 | 501 | WC()->cart->calculate_totals(); |
502 | 502 | |
503 | 503 | // Send back the new cart total. |
504 | 504 | $currency = get_woocommerce_currency(); |
505 | - $tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
|
505 | + $tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp)); |
|
506 | 506 | $data = array( |
507 | 507 | 'total' => WC()->cart->total, |
508 | 508 | ); |
509 | 509 | |
510 | 510 | // Include fees and taxes as displayItems. |
511 | - foreach ( WC()->cart->fees as $key => $fee ) { |
|
511 | + foreach (WC()->cart->fees as $key => $fee) { |
|
512 | 512 | $data['items'][] = array( |
513 | 513 | 'label' => $fee->name, |
514 | 514 | 'amount' => array( |
@@ -517,9 +517,9 @@ discard block |
||
517 | 517 | ), |
518 | 518 | ); |
519 | 519 | } |
520 | - if ( 0 < $tax_total ) { |
|
520 | + if (0 < $tax_total) { |
|
521 | 521 | $data['items'][] = array( |
522 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
522 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
523 | 523 | 'amount' => array( |
524 | 524 | 'currency' => $currency, |
525 | 525 | 'value' => $tax_total, |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | ); |
528 | 528 | } |
529 | 529 | |
530 | - wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
530 | + wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
531 | 531 | } |
532 | 532 | |
533 | 533 | /** |
@@ -538,37 +538,37 @@ discard block |
||
538 | 538 | * @version 3.1.0 |
539 | 539 | */ |
540 | 540 | public function process_apple_pay() { |
541 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
|
542 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
541 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) { |
|
542 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
543 | 543 | } |
544 | 544 | |
545 | 545 | try { |
546 | - $result = array_map( 'wc_clean', $_POST['result'] ); |
|
546 | + $result = array_map('wc_clean', $_POST['result']); |
|
547 | 547 | |
548 | - $order = $this->create_order( $result ); |
|
548 | + $order = $this->create_order($result); |
|
549 | 549 | |
550 | - $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
550 | + $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
551 | 551 | |
552 | 552 | // Handle payment. |
553 | - if ( $order->get_total() > 0 ) { |
|
553 | + if ($order->get_total() > 0) { |
|
554 | 554 | |
555 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
556 | - 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 ) ) ); |
|
555 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
556 | + 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))); |
|
557 | 557 | } |
558 | 558 | |
559 | - WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" ); |
|
559 | + WC_Stripe::log("Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}"); |
|
560 | 560 | |
561 | 561 | // Make the request. |
562 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
|
562 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id'])); |
|
563 | 563 | |
564 | - if ( is_wp_error( $response ) ) { |
|
564 | + if (is_wp_error($response)) { |
|
565 | 565 | $localized_messages = $this->get_localized_messages(); |
566 | 566 | |
567 | - throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
|
567 | + throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message())); |
|
568 | 568 | } |
569 | 569 | |
570 | 570 | // Process valid response. |
571 | - $this->process_response( $response, $order ); |
|
571 | + $this->process_response($response, $order); |
|
572 | 572 | } else { |
573 | 573 | $order->payment_complete(); |
574 | 574 | } |
@@ -576,24 +576,24 @@ discard block |
||
576 | 576 | // Remove cart. |
577 | 577 | WC()->cart->empty_cart(); |
578 | 578 | |
579 | - update_post_meta( $order_id, '_customer_user', get_current_user_id() ); |
|
580 | - update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) ); |
|
579 | + update_post_meta($order_id, '_customer_user', get_current_user_id()); |
|
580 | + update_post_meta($order_id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe')); |
|
581 | 581 | |
582 | 582 | // Return thank you page redirect. |
583 | - wp_send_json( array( |
|
583 | + wp_send_json(array( |
|
584 | 584 | 'success' => 'true', |
585 | - 'redirect' => $this->get_return_url( $order ), |
|
586 | - ) ); |
|
585 | + 'redirect' => $this->get_return_url($order), |
|
586 | + )); |
|
587 | 587 | |
588 | - } catch ( Exception $e ) { |
|
589 | - WC()->session->set( 'refresh_totals', true ); |
|
590 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
588 | + } catch (Exception $e) { |
|
589 | + WC()->session->set('refresh_totals', true); |
|
590 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
591 | 591 | |
592 | - if ( is_object( $order ) && isset( $order_id ) && $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
593 | - $this->send_failed_order_email( $order_id ); |
|
592 | + if (is_object($order) && isset($order_id) && $order->has_status(array('pending', 'failed'))) { |
|
593 | + $this->send_failed_order_email($order_id); |
|
594 | 594 | } |
595 | 595 | |
596 | - wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
|
596 | + wp_send_json(array('success' => 'false', 'msg' => $e->getMessage())); |
|
597 | 597 | } |
598 | 598 | } |
599 | 599 | |
@@ -603,16 +603,16 @@ discard block |
||
603 | 603 | * @param string $source token |
604 | 604 | * @return array() |
605 | 605 | */ |
606 | - protected function generate_payment_request( $order, $source ) { |
|
606 | + protected function generate_payment_request($order, $source) { |
|
607 | 607 | $post_data = array(); |
608 | - $post_data['currency'] = strtolower( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ); |
|
609 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
610 | - $post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() ); |
|
608 | + $post_data['currency'] = strtolower(version_compare(WC_VERSION, '3.0.0', '<') ? $order->get_order_currency() : $order->get_currency()); |
|
609 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
610 | + $post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number()); |
|
611 | 611 | $post_data['capture'] = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false'; |
612 | 612 | |
613 | - $billing_email = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->billing_email : $order->get_billing_email(); |
|
613 | + $billing_email = version_compare(WC_VERSION, '3.0.0', '<') ? $order->billing_email : $order->get_billing_email(); |
|
614 | 614 | |
615 | - if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
615 | + if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
616 | 616 | $post_data['receipt_email'] = $billing_email; |
617 | 617 | } |
618 | 618 | |
@@ -627,7 +627,7 @@ discard block |
||
627 | 627 | * @param WC_Order $order |
628 | 628 | * @param object $source |
629 | 629 | */ |
630 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order ); |
|
630 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order); |
|
631 | 631 | } |
632 | 632 | |
633 | 633 | /** |
@@ -636,14 +636,14 @@ discard block |
||
636 | 636 | * @since 3.1.0 |
637 | 637 | * @version 3.1.0 |
638 | 638 | */ |
639 | - public function build_shipping_methods( $shipping_methods ) { |
|
640 | - if ( empty( $shipping_methods ) ) { |
|
639 | + public function build_shipping_methods($shipping_methods) { |
|
640 | + if (empty($shipping_methods)) { |
|
641 | 641 | return array(); |
642 | 642 | } |
643 | 643 | |
644 | 644 | $shipping = array(); |
645 | 645 | |
646 | - foreach ( $shipping_methods as $method ) { |
|
646 | + foreach ($shipping_methods as $method) { |
|
647 | 647 | $shipping[] = array( |
648 | 648 | 'label' => $method['label'], |
649 | 649 | 'detail' => '', |
@@ -662,65 +662,65 @@ discard block |
||
662 | 662 | * @version 3.1.0 |
663 | 663 | */ |
664 | 664 | public function build_line_items() { |
665 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
666 | - define( 'WOOCOMMERCE_CART', true ); |
|
665 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
666 | + define('WOOCOMMERCE_CART', true); |
|
667 | 667 | } |
668 | 668 | |
669 | - $decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 ); |
|
669 | + $decimals = apply_filters('wc_stripe_apple_pay_decimals', 2); |
|
670 | 670 | |
671 | 671 | $items = array(); |
672 | 672 | $subtotal = 0; |
673 | 673 | |
674 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
675 | - $amount = wc_format_decimal( $values['line_subtotal'], $decimals ); |
|
676 | - $subtotal += $values['line_subtotal']; |
|
674 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
675 | + $amount = wc_format_decimal($values['line_subtotal'], $decimals); |
|
676 | + $subtotal += $values['line_subtotal']; |
|
677 | 677 | $quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : ''; |
678 | 678 | |
679 | 679 | $item = array( |
680 | 680 | 'type' => 'final', |
681 | 681 | 'label' => $values['data']->post->post_title . $quantity_label, |
682 | - 'amount' => wc_format_decimal( $amount, $decimals ), |
|
682 | + 'amount' => wc_format_decimal($amount, $decimals), |
|
683 | 683 | ); |
684 | 684 | |
685 | 685 | $items[] = $item; |
686 | 686 | } |
687 | 687 | |
688 | 688 | // Default show only subtotal instead of itemization. |
689 | - if ( apply_filters( 'wc_stripe_apple_pay_disable_itemization', true ) ) { |
|
689 | + if (apply_filters('wc_stripe_apple_pay_disable_itemization', true)) { |
|
690 | 690 | $items = array(); |
691 | 691 | $items[] = array( |
692 | 692 | 'type' => 'final', |
693 | - 'label' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ), |
|
694 | - 'amount' => wc_format_decimal( $subtotal, $decimals ), |
|
693 | + 'label' => __('Sub-Total', 'woocommerce-gateway-stripe'), |
|
694 | + 'amount' => wc_format_decimal($subtotal, $decimals), |
|
695 | 695 | ); |
696 | 696 | } |
697 | 697 | |
698 | - $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals ); |
|
699 | - $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals ); |
|
700 | - $shipping = wc_format_decimal( WC()->cart->shipping_total, $decimals ); |
|
701 | - $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts; |
|
702 | - $order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals ); |
|
698 | + $discounts = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals); |
|
699 | + $tax = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals); |
|
700 | + $shipping = wc_format_decimal(WC()->cart->shipping_total, $decimals); |
|
701 | + $item_total = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts; |
|
702 | + $order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals); |
|
703 | 703 | |
704 | - if ( wc_tax_enabled() ) { |
|
704 | + if (wc_tax_enabled()) { |
|
705 | 705 | $items[] = array( |
706 | 706 | 'type' => 'final', |
707 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
707 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
708 | 708 | 'amount' => $tax, |
709 | 709 | ); |
710 | 710 | } |
711 | 711 | |
712 | - if ( WC()->cart->needs_shipping() ) { |
|
712 | + if (WC()->cart->needs_shipping()) { |
|
713 | 713 | $items[] = array( |
714 | 714 | 'type' => 'final', |
715 | - 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
|
715 | + 'label' => __('Shipping', 'woocommerce-gateway-stripe'), |
|
716 | 716 | 'amount' => $shipping, |
717 | 717 | ); |
718 | 718 | } |
719 | 719 | |
720 | - if ( WC()->cart->has_discount() ) { |
|
720 | + if (WC()->cart->has_discount()) { |
|
721 | 721 | $items[] = array( |
722 | 722 | 'type' => 'final', |
723 | - 'label' => __( 'Discount', 'woocommerce-gateway-stripe' ), |
|
723 | + 'label' => __('Discount', 'woocommerce-gateway-stripe'), |
|
724 | 724 | 'amount' => '-' . $discounts, |
725 | 725 | ); |
726 | 726 | } |
@@ -736,24 +736,24 @@ discard block |
||
736 | 736 | * @param array $data |
737 | 737 | * @return object $order |
738 | 738 | */ |
739 | - public function create_order( $data = array() ) { |
|
740 | - if ( empty( $data ) ) { |
|
741 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
739 | + public function create_order($data = array()) { |
|
740 | + if (empty($data)) { |
|
741 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
742 | 742 | } |
743 | 743 | |
744 | 744 | $order = wc_create_order(); |
745 | - $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
745 | + $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
746 | 746 | |
747 | - if ( is_wp_error( $order ) ) { |
|
748 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
749 | - } elseif ( false === $order ) { |
|
750 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
|
747 | + if (is_wp_error($order)) { |
|
748 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
749 | + } elseif (false === $order) { |
|
750 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521)); |
|
751 | 751 | } else { |
752 | - do_action( 'woocommerce_new_order', $order_id ); |
|
752 | + do_action('woocommerce_new_order', $order_id); |
|
753 | 753 | } |
754 | 754 | |
755 | 755 | // Store the line items to the new/resumed order |
756 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
756 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
757 | 757 | $item_id = $order->add_product( |
758 | 758 | $values['data'], |
759 | 759 | $values['quantity'], |
@@ -769,47 +769,47 @@ discard block |
||
769 | 769 | ) |
770 | 770 | ); |
771 | 771 | |
772 | - if ( ! $item_id ) { |
|
773 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
|
772 | + if ( ! $item_id) { |
|
773 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525)); |
|
774 | 774 | } |
775 | 775 | |
776 | 776 | // Allow plugins to add order item meta |
777 | - do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
|
777 | + do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key); |
|
778 | 778 | } |
779 | 779 | |
780 | 780 | // Store fees |
781 | - foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
|
782 | - $item_id = $order->add_fee( $fee ); |
|
781 | + foreach (WC()->cart->get_fees() as $fee_key => $fee) { |
|
782 | + $item_id = $order->add_fee($fee); |
|
783 | 783 | |
784 | - if ( ! $item_id ) { |
|
785 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
|
784 | + if ( ! $item_id) { |
|
785 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526)); |
|
786 | 786 | } |
787 | 787 | |
788 | 788 | // Allow plugins to add order item meta to fees |
789 | - do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
|
789 | + do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key); |
|
790 | 790 | } |
791 | 791 | |
792 | 792 | // Store tax rows |
793 | - foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
|
794 | - if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { |
|
795 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
|
793 | + foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) { |
|
794 | + if ($tax_rate_id && ! $order->add_tax($tax_rate_id, WC()->cart->get_tax_amount($tax_rate_id), WC()->cart->get_shipping_tax_amount($tax_rate_id)) && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) { |
|
795 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528)); |
|
796 | 796 | } |
797 | 797 | } |
798 | 798 | |
799 | 799 | // Store coupons |
800 | - foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
|
801 | - if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) { |
|
802 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
|
800 | + foreach (WC()->cart->get_coupons() as $code => $coupon) { |
|
801 | + if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) { |
|
802 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529)); |
|
803 | 803 | } |
804 | 804 | } |
805 | 805 | |
806 | 806 | // Billing address |
807 | 807 | $billing_address = array(); |
808 | - if ( ! empty( $data['token']['card'] ) ) { |
|
808 | + if ( ! empty($data['token']['card'])) { |
|
809 | 809 | // Name from Stripe is a full name string. |
810 | - $name = explode( ' ', $data['token']['card']['name'] ); |
|
811 | - $lastname = array_pop( $name ); |
|
812 | - $firstname = implode( ' ', $name ); |
|
810 | + $name = explode(' ', $data['token']['card']['name']); |
|
811 | + $lastname = array_pop($name); |
|
812 | + $firstname = implode(' ', $name); |
|
813 | 813 | $billing_address['first_name'] = $firstname; |
814 | 814 | $billing_address['last_name'] = $lastname; |
815 | 815 | $billing_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -824,7 +824,7 @@ discard block |
||
824 | 824 | |
825 | 825 | // Shipping address. |
826 | 826 | $shipping_address = array(); |
827 | - if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) { |
|
827 | + if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) { |
|
828 | 828 | $shipping_address['first_name'] = $data['shippingContact']['givenName']; |
829 | 829 | $shipping_address['last_name'] = $data['shippingContact']['familyName']; |
830 | 830 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -835,7 +835,7 @@ discard block |
||
835 | 835 | $shipping_address['city'] = $data['shippingContact']['locality']; |
836 | 836 | $shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
837 | 837 | $shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
838 | - } elseif ( ! empty( $data['shippingContact'] ) ) { |
|
838 | + } elseif ( ! empty($data['shippingContact'])) { |
|
839 | 839 | $shipping_address['first_name'] = $firstname; |
840 | 840 | $shipping_address['last_name'] = $lastname; |
841 | 841 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -848,34 +848,34 @@ discard block |
||
848 | 848 | $shipping_address['postcode'] = $data['token']['card']['address_zip']; |
849 | 849 | } |
850 | 850 | |
851 | - $order->set_address( $billing_address, 'billing' ); |
|
852 | - $order->set_address( $shipping_address, 'shipping' ); |
|
851 | + $order->set_address($billing_address, 'billing'); |
|
852 | + $order->set_address($shipping_address, 'shipping'); |
|
853 | 853 | |
854 | - WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
|
854 | + WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages()); |
|
855 | 855 | |
856 | 856 | // Get the rate object selected by user. |
857 | - foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
|
858 | - foreach ( $package['rates'] as $key => $rate ) { |
|
857 | + foreach (WC()->shipping->get_packages() as $package_key => $package) { |
|
858 | + foreach ($package['rates'] as $key => $rate) { |
|
859 | 859 | // Loop through user chosen shipping methods. |
860 | - foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
|
861 | - if ( $method === $key ) { |
|
862 | - $order->add_shipping( $rate ); |
|
860 | + foreach (WC()->session->get('chosen_shipping_methods') as $method) { |
|
861 | + if ($method === $key) { |
|
862 | + $order->add_shipping($rate); |
|
863 | 863 | } |
864 | 864 | } |
865 | 865 | } |
866 | 866 | } |
867 | 867 | |
868 | 868 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
869 | - $order->set_payment_method( $available_gateways['stripe'] ); |
|
870 | - $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
|
871 | - $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
|
872 | - $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
|
873 | - $order->set_total( WC()->cart->tax_total, 'tax' ); |
|
874 | - $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
|
875 | - $order->set_total( WC()->cart->total ); |
|
869 | + $order->set_payment_method($available_gateways['stripe']); |
|
870 | + $order->set_total(WC()->cart->shipping_total, 'shipping'); |
|
871 | + $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); |
|
872 | + $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); |
|
873 | + $order->set_total(WC()->cart->tax_total, 'tax'); |
|
874 | + $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); |
|
875 | + $order->set_total(WC()->cart->total); |
|
876 | 876 | |
877 | 877 | // If we got here, the order was created without problems! |
878 | - wc_transaction_query( 'commit' ); |
|
878 | + wc_transaction_query('commit'); |
|
879 | 879 | |
880 | 880 | return $order; |
881 | 881 | } |