@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | public function __construct() { |
| 41 | 41 | self::$_this = $this; |
| 42 | 42 | |
| 43 | - $this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' ); |
|
| 43 | + $this->_gateway_settings = get_option('woocommerce_stripe_settings', ''); |
|
| 44 | 44 | |
| 45 | 45 | $this->init(); |
| 46 | 46 | } |
@@ -57,13 +57,13 @@ discard block |
||
| 57 | 57 | * @version 3.1.0 |
| 58 | 58 | */ |
| 59 | 59 | public function init() { |
| 60 | - add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
|
| 61 | - add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 20 ); |
|
| 62 | - add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ) ); |
|
| 63 | - add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) ); |
|
| 64 | - add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) ); |
|
| 65 | - add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) ); |
|
| 66 | - add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) ); |
|
| 60 | + add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); |
|
| 61 | + add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 20); |
|
| 62 | + add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button')); |
|
| 63 | + add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay')); |
|
| 64 | + add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart')); |
|
| 65 | + add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods')); |
|
| 66 | + add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method')); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
@@ -73,38 +73,38 @@ discard block |
||
| 73 | 73 | * @version 3.1.0 |
| 74 | 74 | */ |
| 75 | 75 | public function payment_scripts() { |
| 76 | - if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
|
| 76 | + if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) { |
|
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ( ! $this->is_supported_product_type() ) { |
|
| 80 | + if ( ! $this->is_supported_product_type()) { |
|
| 81 | 81 | return; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 84 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
| 85 | 85 | |
| 86 | - wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
| 86 | + wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
| 87 | 87 | |
| 88 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
| 89 | - 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 ); |
|
| 88 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
| 89 | + 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); |
|
| 90 | 90 | |
| 91 | 91 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
| 92 | 92 | |
| 93 | 93 | $stripe_params = array( |
| 94 | 94 | 'key' => $publishable_key, |
| 95 | 95 | 'currency_code' => get_woocommerce_currency(), |
| 96 | - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
| 97 | - 'label' => get_bloginfo( 'name', 'display' ), |
|
| 98 | - 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
| 99 | - 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
| 100 | - 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
| 101 | - 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
| 102 | - 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
| 96 | + 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
| 97 | + 'label' => get_bloginfo('name', 'display'), |
|
| 98 | + 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
| 99 | + 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
| 100 | + 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
| 101 | + 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
| 102 | + 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
| 103 | 103 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
| 104 | 104 | 'is_cart_page' => is_cart() ? 'yes' : 'no', |
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | - wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) ); |
|
| 107 | + wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params)); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | */ |
| 114 | 114 | public function is_supported_product_type() { |
| 115 | - foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 116 | - if ( 'subscription' === $values['data']->product_type ) { |
|
| 115 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 116 | + if ('subscription' === $values['data']->product_type) { |
|
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -136,19 +136,19 @@ discard block |
||
| 136 | 136 | */ |
| 137 | 137 | if ( |
| 138 | 138 | 'yes' !== $this->_gateway_settings['apple_pay'] |
| 139 | - || ! isset( $gateways['stripe'] ) |
|
| 139 | + || ! isset($gateways['stripe']) |
|
| 140 | 140 | ) { |
| 141 | 141 | return; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - if ( ! $this->is_supported_product_type() ) { |
|
| 144 | + if ( ! $this->is_supported_product_type()) { |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
| 149 | - $country = strtolower( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) ); |
|
| 148 | + $apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
| 149 | + $country = strtolower(substr(get_option('woocommerce_default_country'), 0, 2)); |
|
| 150 | 150 | ?> |
| 151 | - <button class="apple-pay-button" lang="<?php echo esc_attr( $country ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button> |
|
| 151 | + <button class="apple-pay-button" lang="<?php echo esc_attr($country); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button> |
|
| 152 | 152 | <?php |
| 153 | 153 | } |
| 154 | 154 | |
@@ -159,11 +159,11 @@ discard block |
||
| 159 | 159 | * @version 3.1.0 |
| 160 | 160 | */ |
| 161 | 161 | public function generate_apple_pay_cart() { |
| 162 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
| 163 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 162 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
| 163 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 166 | + wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -173,29 +173,29 @@ discard block |
||
| 173 | 173 | * @version 3.1.0 |
| 174 | 174 | * @param array $address |
| 175 | 175 | */ |
| 176 | - public function calculate_shipping( $address = array() ) { |
|
| 177 | - $country = strtoupper( $address['countryCode'] ); |
|
| 178 | - $state = strtoupper( $address['administrativeArea'] ); |
|
| 176 | + public function calculate_shipping($address = array()) { |
|
| 177 | + $country = strtoupper($address['countryCode']); |
|
| 178 | + $state = strtoupper($address['administrativeArea']); |
|
| 179 | 179 | $postcode = $address['postalCode']; |
| 180 | 180 | $city = $address['locality']; |
| 181 | 181 | |
| 182 | 182 | WC()->shipping->reset_shipping(); |
| 183 | 183 | |
| 184 | - if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
| 185 | - throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
| 186 | - } elseif ( $postcode ) { |
|
| 187 | - $postcode = wc_format_postcode( $postcode, $country ); |
|
| 184 | + if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) { |
|
| 185 | + throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe')); |
|
| 186 | + } elseif ($postcode) { |
|
| 187 | + $postcode = wc_format_postcode($postcode, $country); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ( $country ) { |
|
| 191 | - WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
| 192 | - WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
| 190 | + if ($country) { |
|
| 191 | + WC()->customer->set_location($country, $state, $postcode, $city); |
|
| 192 | + WC()->customer->set_shipping_location($country, $state, $postcode, $city); |
|
| 193 | 193 | } else { |
| 194 | 194 | WC()->customer->set_to_base(); |
| 195 | 195 | WC()->customer->set_shipping_to_base(); |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - WC()->customer->calculated_shipping( true ); |
|
| 198 | + WC()->customer->calculated_shipping(true); |
|
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * Set the shipping package. |
@@ -217,17 +217,17 @@ discard block |
||
| 217 | 217 | $packages[0]['destination']['postcode'] = $postcode; |
| 218 | 218 | $packages[0]['destination']['city'] = $city; |
| 219 | 219 | |
| 220 | - foreach ( WC()->cart->get_cart() as $item ) { |
|
| 221 | - if ( $item['data']->needs_shipping() ) { |
|
| 222 | - if ( isset( $item['line_total'] ) ) { |
|
| 220 | + foreach (WC()->cart->get_cart() as $item) { |
|
| 221 | + if ($item['data']->needs_shipping()) { |
|
| 222 | + if (isset($item['line_total'])) { |
|
| 223 | 223 | $packages[0]['contents_cost'] += $item['line_total']; |
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
|
| 228 | + $packages = apply_filters('woocommerce_cart_shipping_packages', $packages); |
|
| 229 | 229 | |
| 230 | - WC()->shipping->calculate_shipping( $packages ); |
|
| 230 | + WC()->shipping->calculate_shipping($packages); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -237,30 +237,30 @@ discard block |
||
| 237 | 237 | * @version 3.1.0 |
| 238 | 238 | */ |
| 239 | 239 | public function get_shipping_methods() { |
| 240 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) { |
|
| 241 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 240 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) { |
|
| 241 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 245 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 244 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 245 | + define('WOOCOMMERCE_CART', true); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | try { |
| 249 | - $address = array_map( 'wc_clean', $_POST['address'] ); |
|
| 249 | + $address = array_map('wc_clean', $_POST['address']); |
|
| 250 | 250 | |
| 251 | - $this->calculate_shipping( $address ); |
|
| 251 | + $this->calculate_shipping($address); |
|
| 252 | 252 | |
| 253 | 253 | // Set the shipping options. |
| 254 | 254 | $currency = get_woocommerce_currency(); |
| 255 | 255 | $data = array(); |
| 256 | 256 | |
| 257 | - if ( ! empty( WC()->shipping->get_packages() ) && WC()->customer->has_calculated_shipping() ) { |
|
| 258 | - foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
|
| 259 | - if ( empty( $package['rates'] ) ) { |
|
| 260 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
| 257 | + if ( ! empty(WC()->shipping->get_packages()) && WC()->customer->has_calculated_shipping()) { |
|
| 258 | + foreach (WC()->shipping->get_packages() as $package_key => $package) { |
|
| 259 | + if (empty($package['rates'])) { |
|
| 260 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - foreach ( $package['rates'] as $key => $rate ) { |
|
| 263 | + foreach ($package['rates'] as $key => $rate) { |
|
| 264 | 264 | $data[] = array( |
| 265 | 265 | 'id' => $rate->id, |
| 266 | 266 | 'label' => $rate->label, |
@@ -274,16 +274,16 @@ discard block |
||
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | // Auto select the first shipping method. |
| 277 | - WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) ); |
|
| 277 | + WC()->session->set('chosen_shipping_methods', array($data[0]['id'])); |
|
| 278 | 278 | |
| 279 | 279 | WC()->cart->calculate_totals(); |
| 280 | 280 | |
| 281 | - wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 281 | + wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 282 | 282 | } else { |
| 283 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
| 283 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
| 284 | 284 | } |
| 285 | - } catch( Exception $e ) { |
|
| 286 | - wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 285 | + } catch (Exception $e) { |
|
| 286 | + wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 287 | 287 | } |
| 288 | 288 | } |
| 289 | 289 | |
@@ -294,29 +294,29 @@ discard block |
||
| 294 | 294 | * @version 3.1.0 |
| 295 | 295 | */ |
| 296 | 296 | public function update_shipping_method() { |
| 297 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 298 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 297 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 298 | + define('WOOCOMMERCE_CART', true); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) { |
|
| 302 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 301 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) { |
|
| 302 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] ); |
|
| 305 | + $selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']); |
|
| 306 | 306 | |
| 307 | - WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) ); |
|
| 307 | + WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier'])); |
|
| 308 | 308 | |
| 309 | 309 | WC()->cart->calculate_totals(); |
| 310 | 310 | |
| 311 | 311 | // Send back the new cart total. |
| 312 | 312 | $currency = get_woocommerce_currency(); |
| 313 | - $tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
|
| 313 | + $tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp)); |
|
| 314 | 314 | $data = array( |
| 315 | 315 | 'total' => WC()->cart->total, |
| 316 | 316 | ); |
| 317 | 317 | |
| 318 | 318 | // Include fees and taxes as displayItems. |
| 319 | - foreach ( WC()->cart->fees as $key => $fee ) { |
|
| 319 | + foreach (WC()->cart->fees as $key => $fee) { |
|
| 320 | 320 | $data['items'][] = array( |
| 321 | 321 | 'label' => $fee->name, |
| 322 | 322 | 'amount' => array( |
@@ -325,9 +325,9 @@ discard block |
||
| 325 | 325 | ), |
| 326 | 326 | ); |
| 327 | 327 | } |
| 328 | - if ( 0 < $tax_total ) { |
|
| 328 | + if (0 < $tax_total) { |
|
| 329 | 329 | $data['items'][] = array( |
| 330 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
| 330 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
| 331 | 331 | 'amount' => array( |
| 332 | 332 | 'currency' => $currency, |
| 333 | 333 | 'value' => $tax_total, |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | ); |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 338 | + wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** |
@@ -346,35 +346,35 @@ discard block |
||
| 346 | 346 | * @version 3.1.0 |
| 347 | 347 | */ |
| 348 | 348 | public function process_apple_pay() { |
| 349 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
|
| 350 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 349 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) { |
|
| 350 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | try { |
| 354 | - $result = array_map( 'wc_clean', $_POST['result'] ); |
|
| 354 | + $result = array_map('wc_clean', $_POST['result']); |
|
| 355 | 355 | |
| 356 | - $order = $this->create_order( $result ); |
|
| 356 | + $order = $this->create_order($result); |
|
| 357 | 357 | |
| 358 | 358 | // Handle payment. |
| 359 | - if ( $order->get_total() > 0 ) { |
|
| 359 | + if ($order->get_total() > 0) { |
|
| 360 | 360 | |
| 361 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 362 | - 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 ) ) ); |
|
| 361 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 362 | + 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))); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | - WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" ); |
|
| 365 | + WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}"); |
|
| 366 | 366 | |
| 367 | 367 | // Make the request. |
| 368 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
|
| 368 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id'])); |
|
| 369 | 369 | |
| 370 | - if ( is_wp_error( $response ) ) { |
|
| 370 | + if (is_wp_error($response)) { |
|
| 371 | 371 | $localized_messages = $this->get_localized_messages(); |
| 372 | 372 | |
| 373 | - throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
|
| 373 | + throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message())); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | // Process valid response. |
| 377 | - $this->process_response( $response, $order ); |
|
| 377 | + $this->process_response($response, $order); |
|
| 378 | 378 | } else { |
| 379 | 379 | $order->payment_complete(); |
| 380 | 380 | } |
@@ -382,23 +382,23 @@ discard block |
||
| 382 | 382 | // Remove cart. |
| 383 | 383 | WC()->cart->empty_cart(); |
| 384 | 384 | |
| 385 | - update_post_meta( $order->id, '_customer_user', get_current_user_id() ); |
|
| 385 | + update_post_meta($order->id, '_customer_user', get_current_user_id()); |
|
| 386 | 386 | |
| 387 | 387 | // Return thank you page redirect. |
| 388 | - wp_send_json( array( |
|
| 388 | + wp_send_json(array( |
|
| 389 | 389 | 'success' => 'true', |
| 390 | - 'redirect' => $this->get_return_url( $order ), |
|
| 391 | - ) ); |
|
| 390 | + 'redirect' => $this->get_return_url($order), |
|
| 391 | + )); |
|
| 392 | 392 | |
| 393 | - } catch ( Exception $e ) { |
|
| 394 | - WC()->session->set( 'refresh_totals', true ); |
|
| 395 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
| 393 | + } catch (Exception $e) { |
|
| 394 | + WC()->session->set('refresh_totals', true); |
|
| 395 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
| 396 | 396 | |
| 397 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 398 | - $this->send_failed_order_email( $order->id ); |
|
| 397 | + if ($order->has_status(array('pending', 'failed'))) { |
|
| 398 | + $this->send_failed_order_email($order->id); |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | - wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
|
| 401 | + wp_send_json(array('success' => 'false', 'msg' => $e->getMessage())); |
|
| 402 | 402 | } |
| 403 | 403 | } |
| 404 | 404 | |
@@ -408,14 +408,14 @@ discard block |
||
| 408 | 408 | * @param string $source token |
| 409 | 409 | * @return array() |
| 410 | 410 | */ |
| 411 | - protected function generate_payment_request( $order, $source ) { |
|
| 411 | + protected function generate_payment_request($order, $source) { |
|
| 412 | 412 | $post_data = array(); |
| 413 | - $post_data['currency'] = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() ); |
|
| 414 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
| 415 | - $post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ); |
|
| 413 | + $post_data['currency'] = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency()); |
|
| 414 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
| 415 | + $post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number()); |
|
| 416 | 416 | $post_data['capture'] = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false'; |
| 417 | 417 | |
| 418 | - if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
| 418 | + if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
| 419 | 419 | $post_data['receipt_email'] = $order->billing_email; |
| 420 | 420 | } |
| 421 | 421 | |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | * @param WC_Order $order |
| 431 | 431 | * @param object $source |
| 432 | 432 | */ |
| 433 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order ); |
|
| 433 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order); |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | /** |
@@ -439,14 +439,14 @@ discard block |
||
| 439 | 439 | * @since 3.1.0 |
| 440 | 440 | * @version 3.1.0 |
| 441 | 441 | */ |
| 442 | - public function build_shipping_methods( $shipping_methods ) { |
|
| 443 | - if ( empty( $shipping_methods ) ) { |
|
| 442 | + public function build_shipping_methods($shipping_methods) { |
|
| 443 | + if (empty($shipping_methods)) { |
|
| 444 | 444 | return array(); |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | $shipping = array(); |
| 448 | 448 | |
| 449 | - foreach( $shipping_methods as $method ) { |
|
| 449 | + foreach ($shipping_methods as $method) { |
|
| 450 | 450 | $shipping[] = array( |
| 451 | 451 | 'label' => $method['label'], |
| 452 | 452 | 'detail' => '', |
@@ -465,53 +465,53 @@ discard block |
||
| 465 | 465 | * @version 3.1.0 |
| 466 | 466 | */ |
| 467 | 467 | public function build_line_items() { |
| 468 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 469 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 468 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 469 | + define('WOOCOMMERCE_CART', true); |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - $decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 ); |
|
| 472 | + $decimals = apply_filters('wc_stripe_apple_pay_decimals', 2); |
|
| 473 | 473 | |
| 474 | 474 | $items = array(); |
| 475 | 475 | |
| 476 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 477 | - $amount = wc_format_decimal( $values['line_subtotal'], $decimals ); |
|
| 476 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 477 | + $amount = wc_format_decimal($values['line_subtotal'], $decimals); |
|
| 478 | 478 | $quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : ''; |
| 479 | 479 | |
| 480 | 480 | $item = array( |
| 481 | 481 | 'type' => 'final', |
| 482 | 482 | 'label' => $values['data']->post->post_title . $quantity_label, |
| 483 | - 'amount' => wc_format_decimal( $amount, $decimals ), |
|
| 483 | + 'amount' => wc_format_decimal($amount, $decimals), |
|
| 484 | 484 | ); |
| 485 | 485 | |
| 486 | 486 | $items[] = $item; |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | - $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals ); |
|
| 490 | - $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals ); |
|
| 491 | - $shipping = wc_format_decimal( WC()->cart->shipping_total, $decimals ); |
|
| 492 | - $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts; |
|
| 493 | - $order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals ); |
|
| 489 | + $discounts = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals); |
|
| 490 | + $tax = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals); |
|
| 491 | + $shipping = wc_format_decimal(WC()->cart->shipping_total, $decimals); |
|
| 492 | + $item_total = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts; |
|
| 493 | + $order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals); |
|
| 494 | 494 | |
| 495 | - if ( wc_tax_enabled() ) { |
|
| 495 | + if (wc_tax_enabled()) { |
|
| 496 | 496 | $items[] = array( |
| 497 | 497 | 'type' => 'final', |
| 498 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
| 498 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
| 499 | 499 | 'amount' => $tax, |
| 500 | 500 | ); |
| 501 | 501 | } |
| 502 | 502 | |
| 503 | - if ( WC()->cart->needs_shipping() ) { |
|
| 503 | + if (WC()->cart->needs_shipping()) { |
|
| 504 | 504 | $items[] = array( |
| 505 | 505 | 'type' => 'final', |
| 506 | - 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
|
| 506 | + 'label' => __('Shipping', 'woocommerce-gateway-stripe'), |
|
| 507 | 507 | 'amount' => $shipping, |
| 508 | 508 | ); |
| 509 | 509 | } |
| 510 | 510 | |
| 511 | - if ( WC()->cart->has_discount() ) { |
|
| 511 | + if (WC()->cart->has_discount()) { |
|
| 512 | 512 | $items[] = array( |
| 513 | 513 | 'type' => 'final', |
| 514 | - 'label' => __( 'Discount', 'woocommerce-gateway-stripe' ), |
|
| 514 | + 'label' => __('Discount', 'woocommerce-gateway-stripe'), |
|
| 515 | 515 | 'amount' => $discounts, |
| 516 | 516 | ); |
| 517 | 517 | } |
@@ -527,24 +527,24 @@ discard block |
||
| 527 | 527 | * @param array $data |
| 528 | 528 | * @return object $order |
| 529 | 529 | */ |
| 530 | - public function create_order( $data = array() ) { |
|
| 531 | - if ( empty( $data ) ) { |
|
| 532 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
| 530 | + public function create_order($data = array()) { |
|
| 531 | + if (empty($data)) { |
|
| 532 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | $order = wc_create_order(); |
| 536 | 536 | |
| 537 | - if ( is_wp_error( $order ) ) { |
|
| 538 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
| 539 | - } elseif ( false === $order ) { |
|
| 540 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
|
| 537 | + if (is_wp_error($order)) { |
|
| 538 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
| 539 | + } elseif (false === $order) { |
|
| 540 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521)); |
|
| 541 | 541 | } else { |
| 542 | 542 | $order_id = $order->id; |
| 543 | - do_action( 'woocommerce_new_order', $order_id ); |
|
| 543 | + do_action('woocommerce_new_order', $order_id); |
|
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | // Store the line items to the new/resumed order |
| 547 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 547 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 548 | 548 | $item_id = $order->add_product( |
| 549 | 549 | $values['data'], |
| 550 | 550 | $values['quantity'], |
@@ -560,47 +560,47 @@ discard block |
||
| 560 | 560 | ) |
| 561 | 561 | ); |
| 562 | 562 | |
| 563 | - if ( ! $item_id ) { |
|
| 564 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
|
| 563 | + if ( ! $item_id) { |
|
| 564 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525)); |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | // Allow plugins to add order item meta |
| 568 | - do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
|
| 568 | + do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key); |
|
| 569 | 569 | } |
| 570 | 570 | |
| 571 | 571 | // Store fees |
| 572 | - foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
|
| 573 | - $item_id = $order->add_fee( $fee ); |
|
| 572 | + foreach (WC()->cart->get_fees() as $fee_key => $fee) { |
|
| 573 | + $item_id = $order->add_fee($fee); |
|
| 574 | 574 | |
| 575 | - if ( ! $item_id ) { |
|
| 576 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
|
| 575 | + if ( ! $item_id) { |
|
| 576 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526)); |
|
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | // Allow plugins to add order item meta to fees |
| 580 | - do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
|
| 580 | + do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | // Store tax rows |
| 584 | - foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
|
| 585 | - 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 ) { |
|
| 586 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
|
| 584 | + foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) { |
|
| 585 | + 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) { |
|
| 586 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528)); |
|
| 587 | 587 | } |
| 588 | 588 | } |
| 589 | 589 | |
| 590 | 590 | // Store coupons |
| 591 | - foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
|
| 592 | - if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) { |
|
| 593 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
|
| 591 | + foreach (WC()->cart->get_coupons() as $code => $coupon) { |
|
| 592 | + if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) { |
|
| 593 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529)); |
|
| 594 | 594 | } |
| 595 | 595 | } |
| 596 | 596 | |
| 597 | 597 | // Billing address |
| 598 | 598 | $billing_address = array(); |
| 599 | - if ( ! empty( $data['token']['card'] ) ) { |
|
| 599 | + if ( ! empty($data['token']['card'])) { |
|
| 600 | 600 | // Name from Stripe is a full name string. |
| 601 | - $name = explode( ' ', $data['token']['card']['name'] ); |
|
| 602 | - $lastname = array_pop( $name ); |
|
| 603 | - $firstname = implode( ' ', $name ); |
|
| 601 | + $name = explode(' ', $data['token']['card']['name']); |
|
| 602 | + $lastname = array_pop($name); |
|
| 603 | + $firstname = implode(' ', $name); |
|
| 604 | 604 | $billing_address['first_name'] = $firstname; |
| 605 | 605 | $billing_address['last_name'] = $lastname; |
| 606 | 606 | $billing_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | |
| 616 | 616 | // Shipping address. |
| 617 | 617 | $shipping_address = array(); |
| 618 | - if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) { |
|
| 618 | + if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) { |
|
| 619 | 619 | $shipping_address['first_name'] = $data['shippingContact']['givenName']; |
| 620 | 620 | $shipping_address['last_name'] = $data['shippingContact']['familyName']; |
| 621 | 621 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -626,7 +626,7 @@ discard block |
||
| 626 | 626 | $shipping_address['city'] = $data['shippingContact']['locality']; |
| 627 | 627 | $shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
| 628 | 628 | $shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
| 629 | - } elseif ( ! empty( $data['shippingContact'] ) ) { |
|
| 629 | + } elseif ( ! empty($data['shippingContact'])) { |
|
| 630 | 630 | $shipping_address['first_name'] = $firstname; |
| 631 | 631 | $shipping_address['last_name'] = $lastname; |
| 632 | 632 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -639,34 +639,34 @@ discard block |
||
| 639 | 639 | $shipping_address['postcode'] = $data['token']['card']['address_zip']; |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | - $order->set_address( $billing_address, 'billing' ); |
|
| 643 | - $order->set_address( $shipping_address, 'shipping' ); |
|
| 642 | + $order->set_address($billing_address, 'billing'); |
|
| 643 | + $order->set_address($shipping_address, 'shipping'); |
|
| 644 | 644 | |
| 645 | - WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
|
| 645 | + WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages()); |
|
| 646 | 646 | |
| 647 | 647 | // Get the rate object selected by user. |
| 648 | - foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
|
| 649 | - foreach ( $package['rates'] as $key => $rate ) { |
|
| 648 | + foreach (WC()->shipping->get_packages() as $package_key => $package) { |
|
| 649 | + foreach ($package['rates'] as $key => $rate) { |
|
| 650 | 650 | // Loop through user chosen shipping methods. |
| 651 | - foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
|
| 652 | - if ( $method === $key ) { |
|
| 653 | - $order->add_shipping( $rate ); |
|
| 651 | + foreach (WC()->session->get('chosen_shipping_methods') as $method) { |
|
| 652 | + if ($method === $key) { |
|
| 653 | + $order->add_shipping($rate); |
|
| 654 | 654 | } |
| 655 | 655 | } |
| 656 | 656 | } |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 660 | - $order->set_payment_method( $available_gateways['stripe'] ); |
|
| 661 | - $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
|
| 662 | - $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
|
| 663 | - $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
|
| 664 | - $order->set_total( WC()->cart->tax_total, 'tax' ); |
|
| 665 | - $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
|
| 666 | - $order->set_total( WC()->cart->total ); |
|
| 660 | + $order->set_payment_method($available_gateways['stripe']); |
|
| 661 | + $order->set_total(WC()->cart->shipping_total, 'shipping'); |
|
| 662 | + $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); |
|
| 663 | + $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); |
|
| 664 | + $order->set_total(WC()->cart->tax_total, 'tax'); |
|
| 665 | + $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); |
|
| 666 | + $order->set_total(WC()->cart->total); |
|
| 667 | 667 | |
| 668 | 668 | // If we got here, the order was created without problems! |
| 669 | - wc_transaction_query( 'commit' ); |
|
| 669 | + wc_transaction_query('commit'); |
|
| 670 | 670 | |
| 671 | 671 | return $order; |
| 672 | 672 | } |
@@ -25,20 +25,20 @@ discard block |
||
| 25 | 25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 | 26 | */ |
| 27 | 27 | |
| 28 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 28 | +if ( ! defined('ABSPATH')) { |
|
| 29 | 29 | exit; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * Required minimums and constants |
| 34 | 34 | */ |
| 35 | -define( 'WC_STRIPE_VERSION', '3.0.6' ); |
|
| 36 | -define( 'WC_STRIPE_MIN_PHP_VER', '5.3.0' ); |
|
| 37 | -define( 'WC_STRIPE_MIN_WC_VER', '2.5.0' ); |
|
| 38 | -define( 'WC_STRIPE_MAIN_FILE', __FILE__ ); |
|
| 39 | -define( 'WC_STRIPE_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) ); |
|
| 35 | +define('WC_STRIPE_VERSION', '3.0.6'); |
|
| 36 | +define('WC_STRIPE_MIN_PHP_VER', '5.3.0'); |
|
| 37 | +define('WC_STRIPE_MIN_WC_VER', '2.5.0'); |
|
| 38 | +define('WC_STRIPE_MAIN_FILE', __FILE__); |
|
| 39 | +define('WC_STRIPE_PLUGIN_URL', untrailingslashit(plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__)))); |
|
| 40 | 40 | |
| 41 | -if ( ! class_exists( 'WC_Stripe' ) ) : |
|
| 41 | +if ( ! class_exists('WC_Stripe')) : |
|
| 42 | 42 | |
| 43 | 43 | class WC_Stripe { |
| 44 | 44 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * @return Singleton The *Singleton* instance. |
| 59 | 59 | */ |
| 60 | 60 | public static function get_instance() { |
| 61 | - if ( null === self::$instance ) { |
|
| 61 | + if (null === self::$instance) { |
|
| 62 | 62 | self::$instance = new self(); |
| 63 | 63 | } |
| 64 | 64 | return self::$instance; |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | * *Singleton* via the `new` operator from outside of this class. |
| 108 | 108 | */ |
| 109 | 109 | protected function __construct() { |
| 110 | - add_action( 'admin_init', array( $this, 'check_environment' ) ); |
|
| 111 | - add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 ); |
|
| 112 | - add_action( 'plugins_loaded', array( $this, 'init' ) ); |
|
| 110 | + add_action('admin_init', array($this, 'check_environment')); |
|
| 111 | + add_action('admin_notices', array($this, 'admin_notices'), 15); |
|
| 112 | + add_action('plugins_loaded', array($this, 'init')); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -117,33 +117,33 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function init() { |
| 119 | 119 | // Don't hook anything else in the plugin if we're in an incompatible environment |
| 120 | - if ( self::get_environment_warning() ) { |
|
| 120 | + if (self::get_environment_warning()) { |
|
| 121 | 121 | return; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php' ); |
|
| 125 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-customer.php' ); |
|
| 124 | + include_once(dirname(__FILE__) . '/includes/class-wc-stripe-api.php'); |
|
| 125 | + include_once(dirname(__FILE__) . '/includes/class-wc-stripe-customer.php'); |
|
| 126 | 126 | |
| 127 | 127 | // Init the gateway itself |
| 128 | 128 | $this->init_gateways(); |
| 129 | 129 | |
| 130 | - add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) ); |
|
| 131 | - add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) ); |
|
| 132 | - add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) ); |
|
| 133 | - add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'cancel_payment' ) ); |
|
| 134 | - add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'cancel_payment' ) ); |
|
| 135 | - add_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'woocommerce_get_customer_payment_tokens' ), 10, 3 ); |
|
| 136 | - add_action( 'woocommerce_payment_token_deleted', array( $this, 'woocommerce_payment_token_deleted' ), 10, 2 ); |
|
| 137 | - add_action( 'woocommerce_payment_token_set_default', array( $this, 'woocommerce_payment_token_set_default' ) ); |
|
| 130 | + add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links')); |
|
| 131 | + add_action('woocommerce_order_status_on-hold_to_processing', array($this, 'capture_payment')); |
|
| 132 | + add_action('woocommerce_order_status_on-hold_to_completed', array($this, 'capture_payment')); |
|
| 133 | + add_action('woocommerce_order_status_on-hold_to_cancelled', array($this, 'cancel_payment')); |
|
| 134 | + add_action('woocommerce_order_status_on-hold_to_refunded', array($this, 'cancel_payment')); |
|
| 135 | + add_filter('woocommerce_get_customer_payment_tokens', array($this, 'woocommerce_get_customer_payment_tokens'), 10, 3); |
|
| 136 | + add_action('woocommerce_payment_token_deleted', array($this, 'woocommerce_payment_token_deleted'), 10, 2); |
|
| 137 | + add_action('woocommerce_payment_token_set_default', array($this, 'woocommerce_payment_token_set_default')); |
|
| 138 | 138 | |
| 139 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-payment-request.php' ); |
|
| 139 | + include_once(dirname(__FILE__) . '/includes/class-wc-stripe-payment-request.php'); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * Allow this class and other classes to add slug keyed notices (to avoid duplication) |
| 144 | 144 | */ |
| 145 | - public function add_admin_notice( $slug, $class, $message ) { |
|
| 146 | - $this->notices[ $slug ] = array( |
|
| 145 | + public function add_admin_notice($slug, $class, $message) { |
|
| 146 | + $this->notices[$slug] = array( |
|
| 147 | 147 | 'class' => $class, |
| 148 | 148 | 'message' => $message |
| 149 | 149 | ); |
@@ -156,21 +156,21 @@ discard block |
||
| 156 | 156 | public function check_environment() { |
| 157 | 157 | $environment_warning = self::get_environment_warning(); |
| 158 | 158 | |
| 159 | - if ( $environment_warning && is_plugin_active( plugin_basename( __FILE__ ) ) ) { |
|
| 160 | - $this->add_admin_notice( 'bad_environment', 'error', $environment_warning ); |
|
| 159 | + if ($environment_warning && is_plugin_active(plugin_basename(__FILE__))) { |
|
| 160 | + $this->add_admin_notice('bad_environment', 'error', $environment_warning); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | // Check if secret key present. Otherwise prompt, via notice, to go to |
| 164 | 164 | // setting. |
| 165 | - if ( ! class_exists( 'WC_Stripe_API' ) ) { |
|
| 166 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php' ); |
|
| 165 | + if ( ! class_exists('WC_Stripe_API')) { |
|
| 166 | + include_once(dirname(__FILE__) . '/includes/class-wc-stripe-api.php'); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | $secret = WC_Stripe_API::get_secret_key(); |
| 170 | 170 | |
| 171 | - if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) { |
|
| 171 | + if (empty($secret) && ! (isset($_GET['page'], $_GET['section']) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'])) { |
|
| 172 | 172 | $setting_link = $this->get_setting_link(); |
| 173 | - $this->add_admin_notice( 'prompt_connect', 'notice notice-warning', sprintf( __( 'Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ) ); |
|
| 173 | + $this->add_admin_notice('prompt_connect', 'notice notice-warning', sprintf(__('Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe'), $setting_link)); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
@@ -179,24 +179,24 @@ discard block |
||
| 179 | 179 | * found or false if the environment has no problems. |
| 180 | 180 | */ |
| 181 | 181 | static function get_environment_warning() { |
| 182 | - if ( version_compare( phpversion(), WC_STRIPE_MIN_PHP_VER, '<' ) ) { |
|
| 183 | - $message = __( 'WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe' ); |
|
| 182 | + if (version_compare(phpversion(), WC_STRIPE_MIN_PHP_VER, '<')) { |
|
| 183 | + $message = __('WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe'); |
|
| 184 | 184 | |
| 185 | - return sprintf( $message, WC_STRIPE_MIN_PHP_VER, phpversion() ); |
|
| 185 | + return sprintf($message, WC_STRIPE_MIN_PHP_VER, phpversion()); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - if ( ! defined( 'WC_VERSION' ) ) { |
|
| 189 | - return __( 'WooCommerce Stripe requires WooCommerce to be activated to work.', 'woocommerce-gateway-stripe' ); |
|
| 188 | + if ( ! defined('WC_VERSION')) { |
|
| 189 | + return __('WooCommerce Stripe requires WooCommerce to be activated to work.', 'woocommerce-gateway-stripe'); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) { |
|
| 193 | - $message = __( 'WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe' ); |
|
| 192 | + if (version_compare(WC_VERSION, WC_STRIPE_MIN_WC_VER, '<')) { |
|
| 193 | + $message = __('WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe', 'woocommerce-gateway-stripe'); |
|
| 194 | 194 | |
| 195 | - return sprintf( $message, WC_STRIPE_MIN_WC_VER, WC_VERSION ); |
|
| 195 | + return sprintf($message, WC_STRIPE_MIN_WC_VER, WC_VERSION); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - if ( ! function_exists( 'curl_init' ) ) { |
|
| 199 | - return __( 'WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe' ); |
|
| 198 | + if ( ! function_exists('curl_init')) { |
|
| 199 | + return __('WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe'); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | return false; |
@@ -207,15 +207,15 @@ discard block |
||
| 207 | 207 | * |
| 208 | 208 | * @since 1.0.0 |
| 209 | 209 | */ |
| 210 | - public function plugin_action_links( $links ) { |
|
| 210 | + public function plugin_action_links($links) { |
|
| 211 | 211 | $setting_link = $this->get_setting_link(); |
| 212 | 212 | |
| 213 | 213 | $plugin_links = array( |
| 214 | - '<a href="' . $setting_link . '">' . __( 'Settings', 'woocommerce-gateway-stripe' ) . '</a>', |
|
| 215 | - '<a href="https://docs.woothemes.com/document/stripe/">' . __( 'Docs', 'woocommerce-gateway-stripe' ) . '</a>', |
|
| 216 | - '<a href="http://support.woothemes.com/">' . __( 'Support', 'woocommerce-gateway-stripe' ) . '</a>', |
|
| 214 | + '<a href="' . $setting_link . '">' . __('Settings', 'woocommerce-gateway-stripe') . '</a>', |
|
| 215 | + '<a href="https://docs.woothemes.com/document/stripe/">' . __('Docs', 'woocommerce-gateway-stripe') . '</a>', |
|
| 216 | + '<a href="http://support.woothemes.com/">' . __('Support', 'woocommerce-gateway-stripe') . '</a>', |
|
| 217 | 217 | ); |
| 218 | - return array_merge( $plugin_links, $links ); |
|
| 218 | + return array_merge($plugin_links, $links); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -226,20 +226,20 @@ discard block |
||
| 226 | 226 | * @return string Setting link |
| 227 | 227 | */ |
| 228 | 228 | public function get_setting_link() { |
| 229 | - $use_id_as_section = class_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false; |
|
| 229 | + $use_id_as_section = class_exists('WC') ? version_compare(WC()->version, '2.6', '>=') : false; |
|
| 230 | 230 | |
| 231 | - $section_slug = $use_id_as_section ? 'stripe' : strtolower( 'WC_Gateway_Stripe' ); |
|
| 231 | + $section_slug = $use_id_as_section ? 'stripe' : strtolower('WC_Gateway_Stripe'); |
|
| 232 | 232 | |
| 233 | - return admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug ); |
|
| 233 | + return admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | 237 | * Display any notices we've collected thus far (e.g. for connection, disconnection) |
| 238 | 238 | */ |
| 239 | 239 | public function admin_notices() { |
| 240 | - foreach ( (array) $this->notices as $notice_key => $notice ) { |
|
| 241 | - echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>"; |
|
| 242 | - echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); |
|
| 240 | + foreach ((array) $this->notices as $notice_key => $notice) { |
|
| 241 | + echo "<div class='" . esc_attr($notice['class']) . "'><p>"; |
|
| 242 | + echo wp_kses($notice['message'], array('a' => array('href' => array()))); |
|
| 243 | 243 | echo "</p></div>"; |
| 244 | 244 | } |
| 245 | 245 | } |
@@ -250,28 +250,28 @@ discard block |
||
| 250 | 250 | * @since 1.0.0 |
| 251 | 251 | */ |
| 252 | 252 | public function init_gateways() { |
| 253 | - if ( class_exists( 'WC_Subscriptions_Order' ) && function_exists( 'wcs_create_renewal_order' ) ) { |
|
| 253 | + if (class_exists('WC_Subscriptions_Order') && function_exists('wcs_create_renewal_order')) { |
|
| 254 | 254 | $this->subscription_support_enabled = true; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if ( class_exists( 'WC_Pre_Orders_Order' ) ) { |
|
| 257 | + if (class_exists('WC_Pre_Orders_Order')) { |
|
| 258 | 258 | $this->pre_order_enabled = true; |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - if ( ! class_exists( 'WC_Payment_Gateway' ) ) { |
|
| 261 | + if ( ! class_exists('WC_Payment_Gateway')) { |
|
| 262 | 262 | return; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - if ( class_exists( 'WC_Payment_Gateway_CC' ) ) { |
|
| 266 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php' ); |
|
| 267 | - include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-apple-pay.php' ); |
|
| 265 | + if (class_exists('WC_Payment_Gateway_CC')) { |
|
| 266 | + include_once(dirname(__FILE__) . '/includes/class-wc-gateway-stripe.php'); |
|
| 267 | + include_once(dirname(__FILE__) . '/includes/class-wc-stripe-apple-pay.php'); |
|
| 268 | 268 | } else { |
| 269 | - include_once( dirname( __FILE__ ) . '/includes/legacy/class-wc-gateway-stripe.php' ); |
|
| 270 | - include_once( dirname( __FILE__ ) . '/includes/legacy/class-wc-gateway-stripe-saved-cards.php' ); |
|
| 269 | + include_once(dirname(__FILE__) . '/includes/legacy/class-wc-gateway-stripe.php'); |
|
| 270 | + include_once(dirname(__FILE__) . '/includes/legacy/class-wc-gateway-stripe-saved-cards.php'); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | - load_plugin_textdomain( 'woocommerce-gateway-stripe', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' ); |
|
| 274 | - add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) ); |
|
| 273 | + load_plugin_textdomain('woocommerce-gateway-stripe', false, plugin_basename(dirname(__FILE__)) . '/languages'); |
|
| 274 | + add_filter('woocommerce_payment_gateways', array($this, 'add_gateways')); |
|
| 275 | 275 | |
| 276 | 276 | $load_addons = ( |
| 277 | 277 | $this->subscription_support_enabled |
@@ -279,8 +279,8 @@ discard block |
||
| 279 | 279 | $this->pre_order_enabled |
| 280 | 280 | ); |
| 281 | 281 | |
| 282 | - if ( $load_addons ) { |
|
| 283 | - require_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe-addons.php' ); |
|
| 282 | + if ($load_addons) { |
|
| 283 | + require_once(dirname(__FILE__) . '/includes/class-wc-gateway-stripe-addons.php'); |
|
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
@@ -289,8 +289,8 @@ discard block |
||
| 289 | 289 | * |
| 290 | 290 | * @since 1.0.0 |
| 291 | 291 | */ |
| 292 | - public function add_gateways( $methods ) { |
|
| 293 | - if ( $this->subscription_support_enabled || $this->pre_order_enabled ) { |
|
| 292 | + public function add_gateways($methods) { |
|
| 293 | + if ($this->subscription_support_enabled || $this->pre_order_enabled) { |
|
| 294 | 294 | $methods[] = 'WC_Gateway_Stripe_Addons'; |
| 295 | 295 | } else { |
| 296 | 296 | $methods[] = 'WC_Gateway_Stripe'; |
@@ -303,35 +303,35 @@ discard block |
||
| 303 | 303 | * |
| 304 | 304 | * @param int $order_id |
| 305 | 305 | */ |
| 306 | - public function capture_payment( $order_id ) { |
|
| 307 | - $order = wc_get_order( $order_id ); |
|
| 306 | + public function capture_payment($order_id) { |
|
| 307 | + $order = wc_get_order($order_id); |
|
| 308 | 308 | |
| 309 | - if ( 'stripe' === $order->payment_method ) { |
|
| 310 | - $charge = get_post_meta( $order_id, '_stripe_charge_id', true ); |
|
| 311 | - $captured = get_post_meta( $order_id, '_stripe_charge_captured', true ); |
|
| 309 | + if ('stripe' === $order->payment_method) { |
|
| 310 | + $charge = get_post_meta($order_id, '_stripe_charge_id', true); |
|
| 311 | + $captured = get_post_meta($order_id, '_stripe_charge_captured', true); |
|
| 312 | 312 | |
| 313 | - if ( $charge && 'no' === $captured ) { |
|
| 314 | - $result = WC_Stripe_API::request( array( |
|
| 313 | + if ($charge && 'no' === $captured) { |
|
| 314 | + $result = WC_Stripe_API::request(array( |
|
| 315 | 315 | 'amount' => $order->get_total() * 100, |
| 316 | 316 | 'expand[]' => 'balance_transaction' |
| 317 | - ), 'charges/' . $charge . '/capture' ); |
|
| 317 | + ), 'charges/' . $charge . '/capture'); |
|
| 318 | 318 | |
| 319 | - if ( is_wp_error( $result ) ) { |
|
| 320 | - $order->add_order_note( __( 'Unable to capture charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() ); |
|
| 319 | + if (is_wp_error($result)) { |
|
| 320 | + $order->add_order_note(__('Unable to capture charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message()); |
|
| 321 | 321 | } else { |
| 322 | - $order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
|
| 323 | - update_post_meta( $order->id, '_stripe_charge_captured', 'yes' ); |
|
| 322 | + $order->add_order_note(sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id)); |
|
| 323 | + update_post_meta($order->id, '_stripe_charge_captured', 'yes'); |
|
| 324 | 324 | |
| 325 | 325 | // Store other data such as fees |
| 326 | - update_post_meta( $order->id, 'Stripe Payment ID', $result->id ); |
|
| 326 | + update_post_meta($order->id, 'Stripe Payment ID', $result->id); |
|
| 327 | 327 | |
| 328 | - if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) { |
|
| 328 | + if (isset($result->balance_transaction) && isset($result->balance_transaction->fee)) { |
|
| 329 | 329 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
| 330 | 330 | // values are in the local currency of the Stripe account, not from WC. |
| 331 | - $fee = ! empty( $result->balance_transaction->fee ) ? number_format( $result->balance_transaction->fee / 100, 2, '.', '' ) : 0; |
|
| 332 | - $net = ! empty( $result->balance_transaction->net ) ? number_format( $result->balance_transaction->net / 100, 2, '.', '' ) : 0; |
|
| 333 | - update_post_meta( $order->id, 'Stripe Fee', $fee ); |
|
| 334 | - update_post_meta( $order->id, 'Net Revenue From Stripe', $net ); |
|
| 331 | + $fee = ! empty($result->balance_transaction->fee) ? number_format($result->balance_transaction->fee / 100, 2, '.', '') : 0; |
|
| 332 | + $net = ! empty($result->balance_transaction->net) ? number_format($result->balance_transaction->net / 100, 2, '.', '') : 0; |
|
| 333 | + update_post_meta($order->id, 'Stripe Fee', $fee); |
|
| 334 | + update_post_meta($order->id, 'Net Revenue From Stripe', $net); |
|
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | } |
@@ -343,23 +343,23 @@ discard block |
||
| 343 | 343 | * |
| 344 | 344 | * @param int $order_id |
| 345 | 345 | */ |
| 346 | - public function cancel_payment( $order_id ) { |
|
| 347 | - $order = wc_get_order( $order_id ); |
|
| 346 | + public function cancel_payment($order_id) { |
|
| 347 | + $order = wc_get_order($order_id); |
|
| 348 | 348 | |
| 349 | - if ( 'stripe' === $order->payment_method ) { |
|
| 350 | - $charge = get_post_meta( $order_id, '_stripe_charge_id', true ); |
|
| 349 | + if ('stripe' === $order->payment_method) { |
|
| 350 | + $charge = get_post_meta($order_id, '_stripe_charge_id', true); |
|
| 351 | 351 | |
| 352 | - if ( $charge ) { |
|
| 353 | - $result = WC_Stripe_API::request( array( |
|
| 352 | + if ($charge) { |
|
| 353 | + $result = WC_Stripe_API::request(array( |
|
| 354 | 354 | 'amount' => $order->get_total() * 100, |
| 355 | - ), 'charges/' . $charge . '/refund' ); |
|
| 355 | + ), 'charges/' . $charge . '/refund'); |
|
| 356 | 356 | |
| 357 | - if ( is_wp_error( $result ) ) { |
|
| 358 | - $order->add_order_note( __( 'Unable to refund charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() ); |
|
| 357 | + if (is_wp_error($result)) { |
|
| 358 | + $order->add_order_note(__('Unable to refund charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message()); |
|
| 359 | 359 | } else { |
| 360 | - $order->add_order_note( sprintf( __( 'Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
|
| 361 | - delete_post_meta( $order->id, '_stripe_charge_captured' ); |
|
| 362 | - delete_post_meta( $order->id, '_stripe_charge_id' ); |
|
| 360 | + $order->add_order_note(sprintf(__('Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id)); |
|
| 361 | + delete_post_meta($order->id, '_stripe_charge_captured'); |
|
| 362 | + delete_post_meta($order->id, '_stripe_charge_id'); |
|
| 363 | 363 | } |
| 364 | 364 | } |
| 365 | 365 | } |
@@ -370,28 +370,28 @@ discard block |
||
| 370 | 370 | * @param array $tokens |
| 371 | 371 | * @return array |
| 372 | 372 | */ |
| 373 | - public function woocommerce_get_customer_payment_tokens( $tokens, $customer_id, $gateway_id ) { |
|
| 374 | - if ( is_user_logged_in() && 'stripe' === $gateway_id && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
| 375 | - $stripe_customer = new WC_Stripe_Customer( $customer_id ); |
|
| 373 | + public function woocommerce_get_customer_payment_tokens($tokens, $customer_id, $gateway_id) { |
|
| 374 | + if (is_user_logged_in() && 'stripe' === $gateway_id && class_exists('WC_Payment_Token_CC')) { |
|
| 375 | + $stripe_customer = new WC_Stripe_Customer($customer_id); |
|
| 376 | 376 | $stripe_cards = $stripe_customer->get_cards(); |
| 377 | 377 | $stored_tokens = array(); |
| 378 | 378 | |
| 379 | - foreach ( $tokens as $token ) { |
|
| 379 | + foreach ($tokens as $token) { |
|
| 380 | 380 | $stored_tokens[] = $token->get_token(); |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | - foreach ( $stripe_cards as $card ) { |
|
| 384 | - if ( ! in_array( $card->id, $stored_tokens ) ) { |
|
| 383 | + foreach ($stripe_cards as $card) { |
|
| 384 | + if ( ! in_array($card->id, $stored_tokens)) { |
|
| 385 | 385 | $token = new WC_Payment_Token_CC(); |
| 386 | - $token->set_token( $card->id ); |
|
| 387 | - $token->set_gateway_id( 'stripe' ); |
|
| 388 | - $token->set_card_type( strtolower( $card->brand ) ); |
|
| 389 | - $token->set_last4( $card->last4 ); |
|
| 390 | - $token->set_expiry_month( $card->exp_month ); |
|
| 391 | - $token->set_expiry_year( $card->exp_year ); |
|
| 392 | - $token->set_user_id( $customer_id ); |
|
| 386 | + $token->set_token($card->id); |
|
| 387 | + $token->set_gateway_id('stripe'); |
|
| 388 | + $token->set_card_type(strtolower($card->brand)); |
|
| 389 | + $token->set_last4($card->last4); |
|
| 390 | + $token->set_expiry_month($card->exp_month); |
|
| 391 | + $token->set_expiry_year($card->exp_year); |
|
| 392 | + $token->set_user_id($customer_id); |
|
| 393 | 393 | $token->save(); |
| 394 | - $tokens[ $token->get_id() ] = $token; |
|
| 394 | + $tokens[$token->get_id()] = $token; |
|
| 395 | 395 | } |
| 396 | 396 | } |
| 397 | 397 | } |
@@ -401,21 +401,21 @@ discard block |
||
| 401 | 401 | /** |
| 402 | 402 | * Delete token from Stripe |
| 403 | 403 | */ |
| 404 | - public function woocommerce_payment_token_deleted( $token_id, $token ) { |
|
| 405 | - if ( 'stripe' === $token->get_gateway_id() ) { |
|
| 406 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 407 | - $stripe_customer->delete_card( $token->get_token() ); |
|
| 404 | + public function woocommerce_payment_token_deleted($token_id, $token) { |
|
| 405 | + if ('stripe' === $token->get_gateway_id()) { |
|
| 406 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 407 | + $stripe_customer->delete_card($token->get_token()); |
|
| 408 | 408 | } |
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | /** |
| 412 | 412 | * Set as default in Stripe |
| 413 | 413 | */ |
| 414 | - public function woocommerce_payment_token_set_default( $token_id ) { |
|
| 415 | - $token = WC_Payment_Tokens::get( $token_id ); |
|
| 416 | - if ( 'stripe' === $token->get_gateway_id() ) { |
|
| 417 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 418 | - $stripe_customer->set_default_card( $token->get_token() ); |
|
| 414 | + public function woocommerce_payment_token_set_default($token_id) { |
|
| 415 | + $token = WC_Payment_Tokens::get($token_id); |
|
| 416 | + if ('stripe' === $token->get_gateway_id()) { |
|
| 417 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 418 | + $stripe_customer->set_default_card($token->get_token()); |
|
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | |
@@ -425,7 +425,7 @@ discard block |
||
| 425 | 425 | public static function get_minimum_amount() { |
| 426 | 426 | |
| 427 | 427 | // Check order amount |
| 428 | - switch ( get_woocommerce_currency() ) { |
|
| 428 | + switch (get_woocommerce_currency()) { |
|
| 429 | 429 | case 'USD': |
| 430 | 430 | case 'CAD': |
| 431 | 431 | case 'EUR': |
@@ -470,15 +470,15 @@ discard block |
||
| 470 | 470 | * And fits on your back? |
| 471 | 471 | * It's log, log, log |
| 472 | 472 | */ |
| 473 | - public static function log( $message ) { |
|
| 474 | - if ( empty( self::$log ) ) { |
|
| 473 | + public static function log($message) { |
|
| 474 | + if (empty(self::$log)) { |
|
| 475 | 475 | self::$log = new WC_Logger(); |
| 476 | 476 | } |
| 477 | 477 | |
| 478 | - self::$log->add( 'woocommerce-gateway-stripe', $message ); |
|
| 478 | + self::$log->add('woocommerce-gateway-stripe', $message); |
|
| 479 | 479 | |
| 480 | - if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
|
| 481 | - error_log( $message ); |
|
| 480 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 481 | + error_log($message); |
|
| 482 | 482 | } |
| 483 | 483 | } |
| 484 | 484 | } |