@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | */ |
86 | 86 | public function __construct() { |
87 | 87 | $this->id = 'stripe'; |
88 | - $this->method_title = __( 'Stripe', 'woocommerce-gateway-stripe' ); |
|
89 | - $this->method_description = __( 'Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' ); |
|
88 | + $this->method_title = __('Stripe', 'woocommerce-gateway-stripe'); |
|
89 | + $this->method_description = __('Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe'); |
|
90 | 90 | $this->has_fields = true; |
91 | 91 | $this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s'; |
92 | 92 | $this->supports = array( |
@@ -114,35 +114,35 @@ discard block |
||
114 | 114 | $this->init_settings(); |
115 | 115 | |
116 | 116 | // Get setting values. |
117 | - $this->title = $this->get_option( 'title' ); |
|
118 | - $this->description = $this->get_option( 'description' ); |
|
119 | - $this->enabled = $this->get_option( 'enabled' ); |
|
120 | - $this->testmode = 'yes' === $this->get_option( 'testmode' ); |
|
121 | - $this->capture = 'yes' === $this->get_option( 'capture', 'yes' ); |
|
122 | - $this->stripe_checkout = 'yes' === $this->get_option( 'stripe_checkout' ); |
|
123 | - $this->stripe_checkout_locale = $this->get_option( 'stripe_checkout_locale' ); |
|
124 | - $this->stripe_checkout_image = $this->get_option( 'stripe_checkout_image', '' ); |
|
125 | - $this->saved_cards = 'yes' === $this->get_option( 'saved_cards' ); |
|
126 | - $this->secret_key = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' ); |
|
127 | - $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); |
|
128 | - $this->bitcoin = 'USD' === strtoupper( get_woocommerce_currency() ) && 'yes' === $this->get_option( 'stripe_bitcoin' ); |
|
129 | - $this->logging = 'yes' === $this->get_option( 'logging' ); |
|
117 | + $this->title = $this->get_option('title'); |
|
118 | + $this->description = $this->get_option('description'); |
|
119 | + $this->enabled = $this->get_option('enabled'); |
|
120 | + $this->testmode = 'yes' === $this->get_option('testmode'); |
|
121 | + $this->capture = 'yes' === $this->get_option('capture', 'yes'); |
|
122 | + $this->stripe_checkout = 'yes' === $this->get_option('stripe_checkout'); |
|
123 | + $this->stripe_checkout_locale = $this->get_option('stripe_checkout_locale'); |
|
124 | + $this->stripe_checkout_image = $this->get_option('stripe_checkout_image', ''); |
|
125 | + $this->saved_cards = 'yes' === $this->get_option('saved_cards'); |
|
126 | + $this->secret_key = $this->testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key'); |
|
127 | + $this->publishable_key = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key'); |
|
128 | + $this->bitcoin = 'USD' === strtoupper(get_woocommerce_currency()) && 'yes' === $this->get_option('stripe_bitcoin'); |
|
129 | + $this->logging = 'yes' === $this->get_option('logging'); |
|
130 | 130 | |
131 | - if ( $this->stripe_checkout ) { |
|
132 | - $this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' ); |
|
131 | + if ($this->stripe_checkout) { |
|
132 | + $this->order_button_text = __('Continue to payment', 'woocommerce-gateway-stripe'); |
|
133 | 133 | } |
134 | 134 | |
135 | - if ( $this->testmode ) { |
|
136 | - $this->description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' ); |
|
137 | - $this->description = trim( $this->description ); |
|
135 | + if ($this->testmode) { |
|
136 | + $this->description .= ' ' . sprintf(__('TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe'), 'https://stripe.com/docs/testing'); |
|
137 | + $this->description = trim($this->description); |
|
138 | 138 | } |
139 | 139 | |
140 | - WC_Stripe_API::set_secret_key( $this->secret_key ); |
|
140 | + WC_Stripe_API::set_secret_key($this->secret_key); |
|
141 | 141 | |
142 | 142 | // Hooks. |
143 | - add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
|
144 | - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
145 | - add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
|
143 | + add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); |
|
144 | + add_action('admin_notices', array($this, 'admin_notices')); |
|
145 | + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -152,24 +152,24 @@ discard block |
||
152 | 152 | * @return string |
153 | 153 | */ |
154 | 154 | public function get_icon() { |
155 | - $ext = version_compare( WC()->version, '2.6', '>=' ) ? '.svg' : '.png'; |
|
156 | - $style = version_compare( WC()->version, '2.6', '>=' ) ? 'style="margin-left: 0.3em"' : ''; |
|
155 | + $ext = version_compare(WC()->version, '2.6', '>=') ? '.svg' : '.png'; |
|
156 | + $style = version_compare(WC()->version, '2.6', '>=') ? 'style="margin-left: 0.3em"' : ''; |
|
157 | 157 | |
158 | - $icon = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext ) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
159 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext ) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
160 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext ) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
158 | + $icon = '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
159 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
160 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
161 | 161 | |
162 | - if ( 'USD' === get_woocommerce_currency() ) { |
|
163 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext ) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
164 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext ) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
165 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext ) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
162 | + if ('USD' === get_woocommerce_currency()) { |
|
163 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
164 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
165 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
166 | 166 | } |
167 | 167 | |
168 | - if ( 'yes' === $this->bitcoin && 'yes' === $this->stripe_checkout ) { |
|
169 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE ) ) . '" alt="Bitcoin" width="32" ' . $style . ' />'; |
|
168 | + if ('yes' === $this->bitcoin && 'yes' === $this->stripe_checkout) { |
|
169 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(plugins_url('/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE)) . '" alt="Bitcoin" width="32" ' . $style . ' />'; |
|
170 | 170 | } |
171 | 171 | |
172 | - return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id ); |
|
172 | + return apply_filters('woocommerce_gateway_icon', $icon, $this->id); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | /** |
@@ -180,11 +180,11 @@ discard block |
||
180 | 180 | * |
181 | 181 | * @return float|int |
182 | 182 | */ |
183 | - public function get_stripe_amount( $total, $currency = '' ) { |
|
184 | - if ( ! $currency ) { |
|
183 | + public function get_stripe_amount($total, $currency = '') { |
|
184 | + if ( ! $currency) { |
|
185 | 185 | $currency = get_woocommerce_currency(); |
186 | 186 | } |
187 | - switch ( strtoupper( $currency ) ) { |
|
187 | + switch (strtoupper($currency)) { |
|
188 | 188 | // Zero decimal currencies. |
189 | 189 | case 'BIF' : |
190 | 190 | case 'CLP' : |
@@ -201,10 +201,10 @@ discard block |
||
201 | 201 | case 'XAF' : |
202 | 202 | case 'XOF' : |
203 | 203 | case 'XPF' : |
204 | - $total = absint( $total ); |
|
204 | + $total = absint($total); |
|
205 | 205 | break; |
206 | 206 | default : |
207 | - $total = round( $total, 2 ) * 100; // In cents. |
|
207 | + $total = round($total, 2) * 100; // In cents. |
|
208 | 208 | break; |
209 | 209 | } |
210 | 210 | return $total; |
@@ -214,13 +214,13 @@ discard block |
||
214 | 214 | * Check if SSL is enabled and notify the user |
215 | 215 | */ |
216 | 216 | public function admin_notices() { |
217 | - if ( 'no' === $this->enabled ) { |
|
217 | + if ('no' === $this->enabled) { |
|
218 | 218 | return; |
219 | 219 | } |
220 | 220 | |
221 | 221 | // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected. |
222 | - if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) { |
|
223 | - echo '<div class="error stripe-ssl-message"><p>' . sprintf( __( 'Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '</p></div>'; |
|
222 | + if ((function_exists('wc_site_is_https') && ! wc_site_is_https()) && ('no' === get_option('woocommerce_force_ssl_checkout') && ! class_exists('WordPressHTTPS'))) { |
|
223 | + echo '<div class="error stripe-ssl-message"><p>' . sprintf(__('Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout')) . '</p></div>'; |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | |
@@ -228,11 +228,11 @@ discard block |
||
228 | 228 | * Check if this gateway is enabled |
229 | 229 | */ |
230 | 230 | public function is_available() { |
231 | - if ( 'yes' === $this->enabled ) { |
|
232 | - if ( ! $this->testmode && is_checkout() && ! is_ssl() ) { |
|
231 | + if ('yes' === $this->enabled) { |
|
232 | + if ( ! $this->testmode && is_checkout() && ! is_ssl()) { |
|
233 | 233 | return false; |
234 | 234 | } |
235 | - if ( ! $this->secret_key || ! $this->publishable_key ) { |
|
235 | + if ( ! $this->secret_key || ! $this->publishable_key) { |
|
236 | 236 | return false; |
237 | 237 | } |
238 | 238 | return true; |
@@ -244,9 +244,9 @@ discard block |
||
244 | 244 | * Initialise Gateway Settings Form Fields |
245 | 245 | */ |
246 | 246 | public function init_form_fields() { |
247 | - $this->form_fields = include( 'settings-stripe.php' ); |
|
247 | + $this->form_fields = include('settings-stripe.php'); |
|
248 | 248 | |
249 | - wc_enqueue_js( " |
|
249 | + wc_enqueue_js(" |
|
250 | 250 | jQuery( function( $ ) { |
251 | 251 | $( '#woocommerce_stripe_stripe_checkout' ).change(function(){ |
252 | 252 | if ( $( this ).is( ':checked' ) ) { |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | var value = $( this ).val(); |
261 | 261 | |
262 | 262 | if ( value.indexOf( '_test_' ) >= 0 ) { |
263 | - $( this ).css( 'border-color', 'red' ).after( '<span class=\"description stripe-error-description\" style=\"color:red; display:block;\">" . __( 'This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe' ) . "</span>' ); |
|
263 | + $( this ).css( 'border-color', 'red' ).after( '<span class=\"description stripe-error-description\" style=\"color:red; display:block;\">" . __('This is not a valid live key. Live keys start with "sk_live_" and "pk_live_".', 'woocommerce-gateway-stripe') . "</span>' ); |
|
264 | 264 | } else { |
265 | 265 | $( this ).css( 'border-color', '' ); |
266 | 266 | $( '.stripe-error-description', $( this ).parent() ).remove(); |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | var value = $( this ).val(); |
272 | 272 | |
273 | 273 | if ( value.indexOf( '_live_' ) >= 0 ) { |
274 | - $( this ).css( 'border-color', 'red' ).after( '<span class=\"description stripe-error-description\" style=\"color:red; display:block;\">" . __( 'This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe' ) . "</span>' ); |
|
274 | + $( this ).css( 'border-color', 'red' ).after( '<span class=\"description stripe-error-description\" style=\"color:red; display:block;\">" . __('This is not a valid test key. Test keys start with "sk_test_" and "pk_test_".', 'woocommerce-gateway-stripe') . "</span>' ); |
|
275 | 275 | } else { |
276 | 276 | $( this ).css( 'border-color', '' ); |
277 | 277 | $( '.stripe-error-description', $( this ).parent() ).remove(); |
@@ -286,46 +286,46 @@ discard block |
||
286 | 286 | */ |
287 | 287 | public function payment_fields() { |
288 | 288 | $user = wp_get_current_user(); |
289 | - $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; |
|
289 | + $display_tokenization = $this->supports('tokenization') && is_checkout() && $this->saved_cards; |
|
290 | 290 | |
291 | - if ( $user->ID ) { |
|
292 | - $user_email = get_user_meta( $user->ID, 'billing_email', true ); |
|
291 | + if ($user->ID) { |
|
292 | + $user_email = get_user_meta($user->ID, 'billing_email', true); |
|
293 | 293 | $user_email = $user_email ? $user_email : $user->user_email; |
294 | 294 | } else { |
295 | 295 | $user_email = ''; |
296 | 296 | } |
297 | 297 | |
298 | - if ( is_add_payment_method_page() ) { |
|
299 | - $pay_button_text = __( 'Add Card', 'woocommerce-gateway-stripe' ); |
|
298 | + if (is_add_payment_method_page()) { |
|
299 | + $pay_button_text = __('Add Card', 'woocommerce-gateway-stripe'); |
|
300 | 300 | } else { |
301 | 301 | $pay_button_text = ''; |
302 | 302 | } |
303 | 303 | |
304 | 304 | echo '<div |
305 | 305 | id="stripe-payment-data" |
306 | - data-panel-label="' . esc_attr( $pay_button_text ) . '" |
|
306 | + data-panel-label="' . esc_attr($pay_button_text) . '" |
|
307 | 307 | data-description="" |
308 | - data-email="' . esc_attr( $user_email ) . '" |
|
309 | - data-amount="' . esc_attr( $this->get_stripe_amount( WC()->cart->total ) ) . '" |
|
310 | - data-name="' . esc_attr( sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name', 'display' ) ) ) . '" |
|
311 | - data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '" |
|
312 | - data-image="' . esc_attr( $this->stripe_checkout_image ) . '" |
|
313 | - data-bitcoin="' . esc_attr( $this->bitcoin ? 'true' : 'false' ) . '" |
|
314 | - data-locale="' . esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ) . '">'; |
|
308 | + data-email="' . esc_attr($user_email) . '" |
|
309 | + data-amount="' . esc_attr($this->get_stripe_amount(WC()->cart->total)) . '" |
|
310 | + data-name="' . esc_attr(sprintf(__('%s', 'woocommerce-gateway-stripe'), get_bloginfo('name', 'display'))) . '" |
|
311 | + data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '" |
|
312 | + data-image="' . esc_attr($this->stripe_checkout_image) . '" |
|
313 | + data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '" |
|
314 | + data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '">'; |
|
315 | 315 | |
316 | - if ( $this->description ) { |
|
317 | - echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) ); |
|
316 | + if ($this->description) { |
|
317 | + echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description))); |
|
318 | 318 | } |
319 | 319 | |
320 | - if ( $display_tokenization ) { |
|
320 | + if ($display_tokenization) { |
|
321 | 321 | $this->tokenization_script(); |
322 | 322 | $this->saved_payment_methods(); |
323 | 323 | } |
324 | 324 | |
325 | - if ( ! $this->stripe_checkout ) { |
|
325 | + if ( ! $this->stripe_checkout) { |
|
326 | 326 | $this->form(); |
327 | 327 | |
328 | - if ( $display_tokenization ) { |
|
328 | + if ($display_tokenization) { |
|
329 | 329 | $this->save_payment_method_checkbox(); |
330 | 330 | } |
331 | 331 | } |
@@ -341,20 +341,20 @@ discard block |
||
341 | 341 | * @return array |
342 | 342 | */ |
343 | 343 | public function get_localized_messages() { |
344 | - return apply_filters( 'wc_stripe_localized_messages', array( |
|
345 | - 'invalid_number' => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ), |
|
346 | - 'invalid_expiry_month' => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ), |
|
347 | - 'invalid_expiry_year' => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ), |
|
348 | - 'invalid_cvc' => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ), |
|
349 | - 'incorrect_number' => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ), |
|
350 | - 'expired_card' => __( 'The card has expired.', 'woocommerce-gateway-stripe' ), |
|
351 | - 'incorrect_cvc' => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ), |
|
352 | - 'incorrect_zip' => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ), |
|
353 | - 'card_declined' => __( 'The card was declined.', 'woocommerce-gateway-stripe' ), |
|
354 | - 'missing' => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ), |
|
355 | - 'processing_error' => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ), |
|
356 | - 'invalid_request_error' => __( 'Could not find payment information.', 'woocommerce-gateway-stripe' ), |
|
357 | - ) ); |
|
344 | + return apply_filters('wc_stripe_localized_messages', array( |
|
345 | + 'invalid_number' => __('The card number is not a valid credit card number.', 'woocommerce-gateway-stripe'), |
|
346 | + 'invalid_expiry_month' => __('The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe'), |
|
347 | + 'invalid_expiry_year' => __('The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe'), |
|
348 | + 'invalid_cvc' => __('The card\'s security code is invalid.', 'woocommerce-gateway-stripe'), |
|
349 | + 'incorrect_number' => __('The card number is incorrect.', 'woocommerce-gateway-stripe'), |
|
350 | + 'expired_card' => __('The card has expired.', 'woocommerce-gateway-stripe'), |
|
351 | + 'incorrect_cvc' => __('The card\'s security code is incorrect.', 'woocommerce-gateway-stripe'), |
|
352 | + 'incorrect_zip' => __('The card\'s zip code failed validation.', 'woocommerce-gateway-stripe'), |
|
353 | + 'card_declined' => __('The card was declined.', 'woocommerce-gateway-stripe'), |
|
354 | + 'missing' => __('There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe'), |
|
355 | + 'processing_error' => __('An error occurred while processing the card.', 'woocommerce-gateway-stripe'), |
|
356 | + 'invalid_request_error' => __('Could not find payment information.', 'woocommerce-gateway-stripe'), |
|
357 | + )); |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | /** |
@@ -365,29 +365,29 @@ discard block |
||
365 | 365 | * @access public |
366 | 366 | */ |
367 | 367 | public function payment_scripts() { |
368 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
368 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
369 | 369 | |
370 | - if ( $this->stripe_checkout ) { |
|
371 | - wp_enqueue_script( 'stripe', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true ); |
|
372 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe_checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
370 | + if ($this->stripe_checkout) { |
|
371 | + wp_enqueue_script('stripe', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true); |
|
372 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe_checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
373 | 373 | } else { |
374 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
375 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery-payment', 'stripe' ), WC_STRIPE_VERSION, true ); |
|
374 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
375 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery-payment', 'stripe'), WC_STRIPE_VERSION, true); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | $stripe_params = array( |
379 | 379 | 'key' => $this->publishable_key, |
380 | - 'i18n_terms' => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ), |
|
381 | - 'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ), |
|
380 | + 'i18n_terms' => __('Please accept the terms and conditions first', 'woocommerce-gateway-stripe'), |
|
381 | + 'i18n_required_fields' => __('Please fill in required checkout fields first', 'woocommerce-gateway-stripe'), |
|
382 | 382 | ); |
383 | 383 | |
384 | 384 | // If we're on the pay page we need to pass stripe.js the address of the order. |
385 | - if ( is_checkout_pay_page() && isset( $_GET['order'] ) && isset( $_GET['order_id'] ) ) { |
|
386 | - $order_key = urldecode( $_GET['order'] ); |
|
387 | - $order_id = absint( $_GET['order_id'] ); |
|
388 | - $order = wc_get_order( $order_id ); |
|
385 | + if (is_checkout_pay_page() && isset($_GET['order']) && isset($_GET['order_id'])) { |
|
386 | + $order_key = urldecode($_GET['order']); |
|
387 | + $order_id = absint($_GET['order_id']); |
|
388 | + $order = wc_get_order($order_id); |
|
389 | 389 | |
390 | - if ( $order->id === $order_id && $order->order_key === $order_key ) { |
|
390 | + if ($order->id === $order_id && $order->order_key === $order_key) { |
|
391 | 391 | $stripe_params['billing_first_name'] = $order->billing_first_name; |
392 | 392 | $stripe_params['billing_last_name'] = $order->billing_last_name; |
393 | 393 | $stripe_params['billing_address_1'] = $order->billing_address_1; |
@@ -399,14 +399,14 @@ discard block |
||
399 | 399 | } |
400 | 400 | } |
401 | 401 | |
402 | - $stripe_params['no_prepaid_card_msg'] = __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ); |
|
403 | - $stripe_params['allow_prepaid_card'] = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no'; |
|
404 | - $stripe_params['stripe_checkout_require_billing_address'] = apply_filters( 'wc_stripe_checkout_require_billing_address', false ) ? 'yes' : 'no'; |
|
402 | + $stripe_params['no_prepaid_card_msg'] = __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe'); |
|
403 | + $stripe_params['allow_prepaid_card'] = apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no'; |
|
404 | + $stripe_params['stripe_checkout_require_billing_address'] = apply_filters('wc_stripe_checkout_require_billing_address', false) ? 'yes' : 'no'; |
|
405 | 405 | |
406 | 406 | // merge localized messages to be use in JS |
407 | - $stripe_params = array_merge( $stripe_params, $this->get_localized_messages() ); |
|
407 | + $stripe_params = array_merge($stripe_params, $this->get_localized_messages()); |
|
408 | 408 | |
409 | - wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) ); |
|
409 | + wp_localize_script('woocommerce_stripe', 'wc_stripe_params', apply_filters('wc_stripe_params', $stripe_params)); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | /** |
@@ -415,24 +415,24 @@ discard block |
||
415 | 415 | * @param object $source |
416 | 416 | * @return array() |
417 | 417 | */ |
418 | - protected function generate_payment_request( $order, $source ) { |
|
418 | + protected function generate_payment_request($order, $source) { |
|
419 | 419 | $post_data = array(); |
420 | - $post_data['currency'] = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() ); |
|
421 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
422 | - $post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ); |
|
420 | + $post_data['currency'] = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency()); |
|
421 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
422 | + $post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number()); |
|
423 | 423 | $post_data['capture'] = $this->capture ? 'true' : 'false'; |
424 | 424 | |
425 | - if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
425 | + if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
426 | 426 | $post_data['receipt_email'] = $order->billing_email; |
427 | 427 | } |
428 | 428 | |
429 | - $post_data['expand[]'] = 'balance_transaction'; |
|
429 | + $post_data['expand[]'] = 'balance_transaction'; |
|
430 | 430 | |
431 | - if ( $source->customer ) { |
|
431 | + if ($source->customer) { |
|
432 | 432 | $post_data['customer'] = $source->customer; |
433 | 433 | } |
434 | 434 | |
435 | - if ( $source->source ) { |
|
435 | + if ($source->source) { |
|
436 | 436 | $post_data['source'] = $source->source; |
437 | 437 | } |
438 | 438 | |
@@ -444,7 +444,7 @@ discard block |
||
444 | 444 | * @param WC_Order $order |
445 | 445 | * @param object $source |
446 | 446 | */ |
447 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source ); |
|
447 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source); |
|
448 | 448 | } |
449 | 449 | |
450 | 450 | /** |
@@ -456,22 +456,22 @@ discard block |
||
456 | 456 | * @throws Exception When card was not added or for and invalid card. |
457 | 457 | * @return object |
458 | 458 | */ |
459 | - protected function get_source( $user_id, $force_customer = false ) { |
|
460 | - $stripe_customer = new WC_Stripe_Customer( $user_id ); |
|
459 | + protected function get_source($user_id, $force_customer = false) { |
|
460 | + $stripe_customer = new WC_Stripe_Customer($user_id); |
|
461 | 461 | $stripe_source = false; |
462 | 462 | $token_id = false; |
463 | 463 | |
464 | 464 | // New CC info was entered and we have a new token to process |
465 | - if ( isset( $_POST['stripe_token'] ) ) { |
|
466 | - $stripe_token = wc_clean( $_POST['stripe_token'] ); |
|
467 | - $maybe_saved_card = isset( $_POST['wc-stripe-new-payment-method'] ) && ! empty( $_POST['wc-stripe-new-payment-method'] ); |
|
465 | + if (isset($_POST['stripe_token'])) { |
|
466 | + $stripe_token = wc_clean($_POST['stripe_token']); |
|
467 | + $maybe_saved_card = isset($_POST['wc-stripe-new-payment-method']) && ! empty($_POST['wc-stripe-new-payment-method']); |
|
468 | 468 | |
469 | 469 | // This is true if the user wants to store the card to their account. |
470 | - if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_customer ) { |
|
471 | - $stripe_source = $stripe_customer->add_card( $stripe_token ); |
|
470 | + if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_customer) { |
|
471 | + $stripe_source = $stripe_customer->add_card($stripe_token); |
|
472 | 472 | |
473 | - if ( is_wp_error( $stripe_source ) ) { |
|
474 | - throw new Exception( $stripe_source->get_error_message() ); |
|
473 | + if (is_wp_error($stripe_source)) { |
|
474 | + throw new Exception($stripe_source->get_error_message()); |
|
475 | 475 | } |
476 | 476 | |
477 | 477 | } else { |
@@ -482,13 +482,13 @@ discard block |
||
482 | 482 | } |
483 | 483 | |
484 | 484 | // Use an existing token, and then process the payment |
485 | - elseif ( isset( $_POST['wc-stripe-payment-token'] ) && 'new' !== $_POST['wc-stripe-payment-token'] ) { |
|
486 | - $token_id = wc_clean( $_POST['wc-stripe-payment-token'] ); |
|
487 | - $token = WC_Payment_Tokens::get( $token_id ); |
|
485 | + elseif (isset($_POST['wc-stripe-payment-token']) && 'new' !== $_POST['wc-stripe-payment-token']) { |
|
486 | + $token_id = wc_clean($_POST['wc-stripe-payment-token']); |
|
487 | + $token = WC_Payment_Tokens::get($token_id); |
|
488 | 488 | |
489 | - if ( ! $token || $token->get_user_id() !== get_current_user_id() ) { |
|
490 | - WC()->session->set( 'refresh_totals', true ); |
|
491 | - throw new Exception( __( 'Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe' ) ); |
|
489 | + if ( ! $token || $token->get_user_id() !== get_current_user_id()) { |
|
490 | + WC()->session->set('refresh_totals', true); |
|
491 | + throw new Exception(__('Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe')); |
|
492 | 492 | } |
493 | 493 | |
494 | 494 | $stripe_source = $token->get_token(); |
@@ -512,16 +512,16 @@ discard block |
||
512 | 512 | * @param object $order |
513 | 513 | * @return object |
514 | 514 | */ |
515 | - protected function get_order_source( $order = null ) { |
|
515 | + protected function get_order_source($order = null) { |
|
516 | 516 | $stripe_customer = new WC_Stripe_Customer(); |
517 | 517 | $stripe_source = false; |
518 | 518 | $token_id = false; |
519 | 519 | |
520 | - if ( $order ) { |
|
521 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_customer_id', true ) ) { |
|
522 | - $stripe_customer->set_id( $meta_value ); |
|
520 | + if ($order) { |
|
521 | + if ($meta_value = get_post_meta($order->id, '_stripe_customer_id', true)) { |
|
522 | + $stripe_customer->set_id($meta_value); |
|
523 | 523 | } |
524 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_card_id', true ) ) { |
|
524 | + if ($meta_value = get_post_meta($order->id, '_stripe_card_id', true)) { |
|
525 | 525 | $stripe_source = $meta_value; |
526 | 526 | } |
527 | 527 | } |
@@ -544,25 +544,25 @@ discard block |
||
544 | 544 | * |
545 | 545 | * @return array|void |
546 | 546 | */ |
547 | - public function process_payment( $order_id, $retry = true, $force_customer = false ) { |
|
547 | + public function process_payment($order_id, $retry = true, $force_customer = false) { |
|
548 | 548 | try { |
549 | - $order = wc_get_order( $order_id ); |
|
550 | - $source = $this->get_source( get_current_user_id(), $force_customer ); |
|
549 | + $order = wc_get_order($order_id); |
|
550 | + $source = $this->get_source(get_current_user_id(), $force_customer); |
|
551 | 551 | |
552 | - if ( empty( $source->source ) && empty( $source->customer ) ) { |
|
553 | - $error_msg = __( 'Please enter your card details to make a payment.', 'woocommerce-gateway-stripe' ); |
|
554 | - $error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' ); |
|
555 | - throw new Exception( $error_msg ); |
|
552 | + if (empty($source->source) && empty($source->customer)) { |
|
553 | + $error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe'); |
|
554 | + $error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe'); |
|
555 | + throw new Exception($error_msg); |
|
556 | 556 | } |
557 | 557 | |
558 | 558 | // Store source to order meta. |
559 | - $this->save_source( $order, $source ); |
|
559 | + $this->save_source($order, $source); |
|
560 | 560 | |
561 | 561 | // Handle payment. |
562 | - if ( $order->get_total() > 0 ) { |
|
562 | + if ($order->get_total() > 0) { |
|
563 | 563 | |
564 | 564 | // Check order amount |
565 | - switch ( get_woocommerce_currency() ) { |
|
565 | + switch (get_woocommerce_currency()) { |
|
566 | 566 | case 'USD': |
567 | 567 | case 'CAD': |
568 | 568 | case 'EUR': |
@@ -594,33 +594,33 @@ discard block |
||
594 | 594 | $minimum_amount = 50; |
595 | 595 | break; |
596 | 596 | } |
597 | - if ( $order->get_total() * 100 < $minimum_amount ) { |
|
598 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( $minimum_amount / 100 ) ) ); |
|
597 | + if ($order->get_total() * 100 < $minimum_amount) { |
|
598 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price($minimum_amount / 100))); |
|
599 | 599 | } |
600 | 600 | |
601 | - WC_Stripe::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
601 | + WC_Stripe::log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
602 | 602 | |
603 | 603 | // Make the request. |
604 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
604 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
605 | 605 | |
606 | - if ( is_wp_error( $response ) ) { |
|
606 | + if (is_wp_error($response)) { |
|
607 | 607 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
608 | - if ( 'customer' === $response->get_error_code() && $retry ) { |
|
609 | - delete_user_meta( get_current_user_id(), '_stripe_customer_id' ); |
|
610 | - return $this->process_payment( $order_id, false, $force_customer ); |
|
608 | + if ('customer' === $response->get_error_code() && $retry) { |
|
609 | + delete_user_meta(get_current_user_id(), '_stripe_customer_id'); |
|
610 | + return $this->process_payment($order_id, false, $force_customer); |
|
611 | 611 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
612 | - } elseif ( 'source' === $response->get_error_code() && $source->token_id ) { |
|
613 | - $token = WC_Payment_Tokens::get( $source->token_id ); |
|
612 | + } elseif ('source' === $response->get_error_code() && $source->token_id) { |
|
613 | + $token = WC_Payment_Tokens::get($source->token_id); |
|
614 | 614 | $token->delete(); |
615 | - throw new Exception( __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ) ); |
|
615 | + throw new Exception(__('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe')); |
|
616 | 616 | } |
617 | 617 | $localized_messages = $this->get_localized_messages(); |
618 | 618 | |
619 | - throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
|
619 | + throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message())); |
|
620 | 620 | } |
621 | 621 | |
622 | 622 | // Process valid response. |
623 | - $this->process_response( $response, $order ); |
|
623 | + $this->process_response($response, $order); |
|
624 | 624 | } else { |
625 | 625 | $order->payment_complete(); |
626 | 626 | } |
@@ -631,16 +631,16 @@ discard block |
||
631 | 631 | // Return thank you page redirect. |
632 | 632 | return array( |
633 | 633 | 'result' => 'success', |
634 | - 'redirect' => $this->get_return_url( $order ) |
|
634 | + 'redirect' => $this->get_return_url($order) |
|
635 | 635 | ); |
636 | 636 | |
637 | - } catch ( Exception $e ) { |
|
638 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
639 | - WC()->session->set( 'refresh_totals', true ); |
|
640 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
637 | + } catch (Exception $e) { |
|
638 | + wc_add_notice($e->getMessage(), 'error'); |
|
639 | + WC()->session->set('refresh_totals', true); |
|
640 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
641 | 641 | |
642 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
643 | - $this->send_failed_order_email( $order_id ); |
|
642 | + if ($order->has_status(array('pending', 'failed'))) { |
|
643 | + $this->send_failed_order_email($order_id); |
|
644 | 644 | } |
645 | 645 | |
646 | 646 | return; |
@@ -653,49 +653,49 @@ discard block |
||
653 | 653 | * @param WC_Order $order For to which the source applies. |
654 | 654 | * @param stdClass $source Source information. |
655 | 655 | */ |
656 | - protected function save_source( $order, $source ) { |
|
656 | + protected function save_source($order, $source) { |
|
657 | 657 | // Store source in the order. |
658 | - if ( $source->customer ) { |
|
659 | - update_post_meta( $order->id, '_stripe_customer_id', $source->customer ); |
|
658 | + if ($source->customer) { |
|
659 | + update_post_meta($order->id, '_stripe_customer_id', $source->customer); |
|
660 | 660 | } |
661 | - if ( $source->source ) { |
|
662 | - update_post_meta( $order->id, '_stripe_card_id', $source->source ); |
|
661 | + if ($source->source) { |
|
662 | + update_post_meta($order->id, '_stripe_card_id', $source->source); |
|
663 | 663 | } |
664 | 664 | } |
665 | 665 | |
666 | 666 | /** |
667 | 667 | * Store extra meta data for an order from a Stripe Response. |
668 | 668 | */ |
669 | - public function process_response( $response, $order ) { |
|
670 | - WC_Stripe::log( "Processing response: " . print_r( $response, true ) ); |
|
669 | + public function process_response($response, $order) { |
|
670 | + WC_Stripe::log("Processing response: " . print_r($response, true)); |
|
671 | 671 | |
672 | 672 | // Store charge data |
673 | - update_post_meta( $order->id, '_stripe_charge_id', $response->id ); |
|
674 | - update_post_meta( $order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' ); |
|
673 | + update_post_meta($order->id, '_stripe_charge_id', $response->id); |
|
674 | + update_post_meta($order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no'); |
|
675 | 675 | |
676 | 676 | // Store other data such as fees |
677 | - if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) { |
|
678 | - $fee = number_format( $response->balance_transaction->fee / 100, 2, '.', '' ); |
|
679 | - update_post_meta( $order->id, 'Stripe Fee', $fee ); |
|
680 | - update_post_meta( $order->id, 'Net Revenue From Stripe', $order->get_total() - $fee ); |
|
677 | + if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) { |
|
678 | + $fee = number_format($response->balance_transaction->fee / 100, 2, '.', ''); |
|
679 | + update_post_meta($order->id, 'Stripe Fee', $fee); |
|
680 | + update_post_meta($order->id, 'Net Revenue From Stripe', $order->get_total() - $fee); |
|
681 | 681 | } |
682 | 682 | |
683 | - if ( $response->captured ) { |
|
684 | - $order->payment_complete( $response->id ); |
|
683 | + if ($response->captured) { |
|
684 | + $order->payment_complete($response->id); |
|
685 | 685 | |
686 | - $message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id ); |
|
687 | - $order->add_order_note( $message ); |
|
688 | - WC_Stripe::log( 'Success: ' . $message ); |
|
686 | + $message = sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $response->id); |
|
687 | + $order->add_order_note($message); |
|
688 | + WC_Stripe::log('Success: ' . $message); |
|
689 | 689 | |
690 | 690 | } else { |
691 | - add_post_meta( $order->id, '_transaction_id', $response->id, true ); |
|
691 | + add_post_meta($order->id, '_transaction_id', $response->id, true); |
|
692 | 692 | |
693 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
693 | + if ($order->has_status(array('pending', 'failed'))) { |
|
694 | 694 | $order->reduce_order_stock(); |
695 | 695 | } |
696 | 696 | |
697 | - $order->update_status( 'on-hold', sprintf( __( 'Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe' ), $response->id ) ); |
|
698 | - WC_Stripe::log( "Successful auth: $response->id" ); |
|
697 | + $order->update_status('on-hold', sprintf(__('Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe'), $response->id)); |
|
698 | + WC_Stripe::log("Successful auth: $response->id"); |
|
699 | 699 | } |
700 | 700 | |
701 | 701 | return $response; |
@@ -707,32 +707,32 @@ discard block |
||
707 | 707 | * @since 3.0.0 |
708 | 708 | */ |
709 | 709 | public function add_payment_method() { |
710 | - if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
711 | - wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
710 | + if (empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
711 | + wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error'); |
|
712 | 712 | return; |
713 | 713 | } |
714 | 714 | |
715 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
716 | - $card = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) ); |
|
715 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
716 | + $card = $stripe_customer->add_card(wc_clean($_POST['stripe_token'])); |
|
717 | 717 | |
718 | - if ( is_wp_error( $card ) ) { |
|
718 | + if (is_wp_error($card)) { |
|
719 | 719 | $localized_messages = $this->get_localized_messages(); |
720 | - $error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ); |
|
720 | + $error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe'); |
|
721 | 721 | |
722 | 722 | // loop through the errors to find matching localized message |
723 | - foreach ( $card->errors as $error => $msg ) { |
|
724 | - if ( isset( $localized_messages[ $error ] ) ) { |
|
725 | - $error_msg = $localized_messages[ $error ]; |
|
723 | + foreach ($card->errors as $error => $msg) { |
|
724 | + if (isset($localized_messages[$error])) { |
|
725 | + $error_msg = $localized_messages[$error]; |
|
726 | 726 | } |
727 | 727 | } |
728 | 728 | |
729 | - wc_add_notice( $error_msg, 'error' ); |
|
729 | + wc_add_notice($error_msg, 'error'); |
|
730 | 730 | return; |
731 | 731 | } |
732 | 732 | |
733 | 733 | return array( |
734 | 734 | 'result' => 'success', |
735 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
735 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
736 | 736 | ); |
737 | 737 | } |
738 | 738 | |
@@ -742,36 +742,36 @@ discard block |
||
742 | 742 | * @param float $amount |
743 | 743 | * @return bool |
744 | 744 | */ |
745 | - public function process_refund( $order_id, $amount = null, $reason = '' ) { |
|
746 | - $order = wc_get_order( $order_id ); |
|
745 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
746 | + $order = wc_get_order($order_id); |
|
747 | 747 | |
748 | - if ( ! $order || ! $order->get_transaction_id() ) { |
|
748 | + if ( ! $order || ! $order->get_transaction_id()) { |
|
749 | 749 | return false; |
750 | 750 | } |
751 | 751 | |
752 | 752 | $body = array(); |
753 | 753 | |
754 | - if ( ! is_null( $amount ) ) { |
|
755 | - $body['amount'] = $this->get_stripe_amount( $amount ); |
|
754 | + if ( ! is_null($amount)) { |
|
755 | + $body['amount'] = $this->get_stripe_amount($amount); |
|
756 | 756 | } |
757 | 757 | |
758 | - if ( $reason ) { |
|
758 | + if ($reason) { |
|
759 | 759 | $body['metadata'] = array( |
760 | 760 | 'reason' => $reason, |
761 | 761 | ); |
762 | 762 | } |
763 | 763 | |
764 | - WC_Stripe::log( "Info: Beginning refund for order $order_id for the amount of {$amount}" ); |
|
764 | + WC_Stripe::log("Info: Beginning refund for order $order_id for the amount of {$amount}"); |
|
765 | 765 | |
766 | - $response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' ); |
|
766 | + $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); |
|
767 | 767 | |
768 | - if ( is_wp_error( $response ) ) { |
|
769 | - WC_Stripe::log( "Error: " . $response->get_error_message() ); |
|
768 | + if (is_wp_error($response)) { |
|
769 | + WC_Stripe::log("Error: " . $response->get_error_message()); |
|
770 | 770 | return $response; |
771 | - } elseif ( ! empty( $response->id ) ) { |
|
772 | - $refund_message = sprintf( __( 'Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason ); |
|
773 | - $order->add_order_note( $refund_message ); |
|
774 | - WC_Stripe::log( "Success: " . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
771 | + } elseif ( ! empty($response->id)) { |
|
772 | + $refund_message = sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason); |
|
773 | + $order->add_order_note($refund_message); |
|
774 | + WC_Stripe::log("Success: " . html_entity_decode(strip_tags($refund_message))); |
|
775 | 775 | return true; |
776 | 776 | } |
777 | 777 | } |
@@ -784,10 +784,10 @@ discard block |
||
784 | 784 | * @param int $order_id |
785 | 785 | * @return null |
786 | 786 | */ |
787 | - public function send_failed_order_email( $order_id ) { |
|
787 | + public function send_failed_order_email($order_id) { |
|
788 | 788 | $emails = WC()->mailer()->get_emails(); |
789 | - if ( ! empty( $emails ) && ! empty( $order_id ) ) { |
|
790 | - $emails['WC_Email_Failed_Order']->trigger( $order_id ); |
|
789 | + if ( ! empty($emails) && ! empty($order_id)) { |
|
790 | + $emails['WC_Email_Failed_Order']->trigger($order_id); |
|
791 | 791 | } |
792 | 792 | } |
793 | 793 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -16,22 +16,22 @@ discard block |
||
16 | 16 | public function __construct() { |
17 | 17 | parent::__construct(); |
18 | 18 | |
19 | - if ( class_exists( 'WC_Subscriptions_Order' ) ) { |
|
20 | - add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 ); |
|
21 | - add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 ); |
|
22 | - add_action( 'wcs_renewal_order_created', array( $this, 'delete_renewal_meta' ), 10 ); |
|
23 | - add_action( 'woocommerce_subscription_failing_payment_method_updated_stripe', array( $this, 'update_failing_payment_method' ), 10, 2 ); |
|
19 | + if (class_exists('WC_Subscriptions_Order')) { |
|
20 | + add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); |
|
21 | + add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); |
|
22 | + add_action('wcs_renewal_order_created', array($this, 'delete_renewal_meta'), 10); |
|
23 | + add_action('woocommerce_subscription_failing_payment_method_updated_stripe', array($this, 'update_failing_payment_method'), 10, 2); |
|
24 | 24 | |
25 | 25 | // display the credit card used for a subscription in the "My Subscriptions" table |
26 | - add_filter( 'woocommerce_my_subscriptions_payment_method', array( $this, 'maybe_render_subscription_payment_method' ), 10, 2 ); |
|
26 | + add_filter('woocommerce_my_subscriptions_payment_method', array($this, 'maybe_render_subscription_payment_method'), 10, 2); |
|
27 | 27 | |
28 | 28 | // allow store managers to manually set Stripe as the payment method on a subscription |
29 | - add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 ); |
|
30 | - add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 ); |
|
29 | + add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); |
|
30 | + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); |
|
31 | 31 | } |
32 | 32 | |
33 | - if ( class_exists( 'WC_Pre_Orders_Order' ) ) { |
|
34 | - add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ) ); |
|
33 | + if (class_exists('WC_Pre_Orders_Order')) { |
|
34 | + add_action('wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array($this, 'process_pre_order_release_payment')); |
|
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
@@ -40,8 +40,8 @@ discard block |
||
40 | 40 | * @param int $order_id |
41 | 41 | * @return boolean |
42 | 42 | */ |
43 | - protected function is_subscription( $order_id ) { |
|
44 | - return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) ); |
|
43 | + protected function is_subscription($order_id) { |
|
44 | + return (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id))); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | * @param int $order_id |
50 | 50 | * @return boolean |
51 | 51 | */ |
52 | - protected function is_pre_order( $order_id ) { |
|
53 | - return ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id ) ); |
|
52 | + protected function is_pre_order($order_id) { |
|
53 | + return (class_exists('WC_Pre_Orders_Order') && WC_Pre_Orders_Order::order_contains_pre_order($order_id)); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -58,37 +58,37 @@ discard block |
||
58 | 58 | * @param int $order_id |
59 | 59 | * @return array |
60 | 60 | */ |
61 | - public function process_payment( $order_id, $retry = true, $force_customer = false ) { |
|
62 | - if ( $this->is_subscription( $order_id ) ) { |
|
61 | + public function process_payment($order_id, $retry = true, $force_customer = false) { |
|
62 | + if ($this->is_subscription($order_id)) { |
|
63 | 63 | // Regular payment with force customer enabled |
64 | - return parent::process_payment( $order_id, true, true ); |
|
64 | + return parent::process_payment($order_id, true, true); |
|
65 | 65 | |
66 | - } elseif ( $this->is_pre_order( $order_id ) ) { |
|
67 | - return $this->process_pre_order( $order_id, $retry, $force_customer ); |
|
66 | + } elseif ($this->is_pre_order($order_id)) { |
|
67 | + return $this->process_pre_order($order_id, $retry, $force_customer); |
|
68 | 68 | |
69 | 69 | } else { |
70 | - return parent::process_payment( $order_id, $retry, $force_customer ); |
|
70 | + return parent::process_payment($order_id, $retry, $force_customer); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
75 | 75 | * Updates other subscription sources. |
76 | 76 | */ |
77 | - protected function save_source( $order, $source ) { |
|
78 | - parent::save_source( $order, $source ); |
|
77 | + protected function save_source($order, $source) { |
|
78 | + parent::save_source($order, $source); |
|
79 | 79 | |
80 | 80 | // Also store it on the subscriptions being purchased or paid for in the order |
81 | - if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order->id ) ) { |
|
82 | - $subscriptions = wcs_get_subscriptions_for_order( $order->id ); |
|
83 | - } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order->id ) ) { |
|
84 | - $subscriptions = wcs_get_subscriptions_for_renewal_order( $order->id ); |
|
81 | + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order->id)) { |
|
82 | + $subscriptions = wcs_get_subscriptions_for_order($order->id); |
|
83 | + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order->id)) { |
|
84 | + $subscriptions = wcs_get_subscriptions_for_renewal_order($order->id); |
|
85 | 85 | } else { |
86 | 86 | $subscriptions = array(); |
87 | 87 | } |
88 | 88 | |
89 | - foreach( $subscriptions as $subscription ) { |
|
90 | - update_post_meta( $subscription->id, '_stripe_customer_id', $source->customer ); |
|
91 | - update_post_meta( $subscription->id, '_stripe_card_id', $source->source ); |
|
89 | + foreach ($subscriptions as $subscription) { |
|
90 | + update_post_meta($subscription->id, '_stripe_customer_id', $source->customer); |
|
91 | + update_post_meta($subscription->id, '_stripe_card_id', $source->source); |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
@@ -99,10 +99,10 @@ discard block |
||
99 | 99 | * @param string $stripe_token (default: '') |
100 | 100 | * @param bool initial_payment |
101 | 101 | */ |
102 | - public function process_subscription_payment( $order = '', $amount = 0 ) { |
|
102 | + public function process_subscription_payment($order = '', $amount = 0) { |
|
103 | 103 | |
104 | 104 | // Check order amount |
105 | - switch ( get_woocommerce_currency() ) { |
|
105 | + switch (get_woocommerce_currency()) { |
|
106 | 106 | case 'USD': |
107 | 107 | case 'CAD': |
108 | 108 | case 'EUR': |
@@ -134,37 +134,37 @@ discard block |
||
134 | 134 | $minimum_amount = 50; |
135 | 135 | break; |
136 | 136 | } |
137 | - if ( $amount * 100 < $minimum_amount ) { |
|
138 | - 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( $minimum_amount / 100 ) ) ); |
|
137 | + if ($amount * 100 < $minimum_amount) { |
|
138 | + 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($minimum_amount / 100))); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | // Get source from order |
142 | - $source = $this->get_order_source( $order ); |
|
142 | + $source = $this->get_order_source($order); |
|
143 | 143 | |
144 | 144 | // If no order source was defined, use user source instead. |
145 | - if ( ! $source->customer ) { |
|
146 | - $source = $this->get_source( $order->customer_user ); |
|
145 | + if ( ! $source->customer) { |
|
146 | + $source = $this->get_source($order->customer_user); |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | // Or fail :( |
150 | - if ( ! $source->customer ) { |
|
151 | - return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
150 | + if ( ! $source->customer) { |
|
151 | + return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
152 | 152 | } |
153 | 153 | |
154 | - WC_Stripe::log( "Info: Begin processing subscription payment for order {$order->id} for the amount of {$amount}" ); |
|
154 | + WC_Stripe::log("Info: Begin processing subscription payment for order {$order->id} for the amount of {$amount}"); |
|
155 | 155 | |
156 | 156 | // Make the request |
157 | - $request = $this->generate_payment_request( $order, $source ); |
|
157 | + $request = $this->generate_payment_request($order, $source); |
|
158 | 158 | $request['capture'] = 'true'; |
159 | - $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
159 | + $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
160 | 160 | $request['metadata'] = array( |
161 | 161 | 'payment_type' => 'recurring' |
162 | 162 | ); |
163 | - $response = WC_Stripe_API::request( $request ); |
|
163 | + $response = WC_Stripe_API::request($request); |
|
164 | 164 | |
165 | 165 | // Process valid response |
166 | - if ( ! is_wp_error( $response ) ) { |
|
167 | - $this->process_response( $response, $order ); |
|
166 | + if ( ! is_wp_error($response)) { |
|
167 | + $this->process_response($response, $order); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | return $response; |
@@ -175,13 +175,13 @@ discard block |
||
175 | 175 | * @param int $order_id |
176 | 176 | * @return array |
177 | 177 | */ |
178 | - public function process_pre_order( $order_id, $retry, $force_customer ) { |
|
179 | - if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { |
|
178 | + public function process_pre_order($order_id, $retry, $force_customer) { |
|
179 | + if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) { |
|
180 | 180 | try { |
181 | - $order = wc_get_order( $order_id ); |
|
181 | + $order = wc_get_order($order_id); |
|
182 | 182 | |
183 | 183 | // Check order amount |
184 | - switch ( get_woocommerce_currency() ) { |
|
184 | + switch (get_woocommerce_currency()) { |
|
185 | 185 | case 'USD': |
186 | 186 | case 'CAD': |
187 | 187 | case 'EUR': |
@@ -213,19 +213,19 @@ discard block |
||
213 | 213 | $minimum_amount = 50; |
214 | 214 | break; |
215 | 215 | } |
216 | - if ( $order->get_total() * 100 < $minimum_amount ) { |
|
217 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( $minimum_amount / 100 ) ) ); |
|
216 | + if ($order->get_total() * 100 < $minimum_amount) { |
|
217 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price($minimum_amount / 100))); |
|
218 | 218 | } |
219 | 219 | |
220 | - $source = $this->get_source( get_current_user_id(), true ); |
|
220 | + $source = $this->get_source(get_current_user_id(), true); |
|
221 | 221 | |
222 | 222 | // We need a source on file to continue. |
223 | - if ( empty( $source->customer ) || empty( $source->source ) ) { |
|
224 | - throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
223 | + if (empty($source->customer) || empty($source->source)) { |
|
224 | + throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe')); |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | // Store source to order meta |
228 | - $this->save_source( $order, $source ); |
|
228 | + $this->save_source($order, $source); |
|
229 | 229 | |
230 | 230 | // Reduce stock levels |
231 | 231 | $order->reduce_order_stock(); |
@@ -234,19 +234,19 @@ discard block |
||
234 | 234 | WC()->cart->empty_cart(); |
235 | 235 | |
236 | 236 | // Is pre ordered! |
237 | - WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
237 | + WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); |
|
238 | 238 | |
239 | 239 | // Return thank you page redirect |
240 | 240 | return array( |
241 | 241 | 'result' => 'success', |
242 | - 'redirect' => $this->get_return_url( $order ) |
|
242 | + 'redirect' => $this->get_return_url($order) |
|
243 | 243 | ); |
244 | - } catch ( Exception $e ) { |
|
245 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
244 | + } catch (Exception $e) { |
|
245 | + wc_add_notice($e->getMessage(), 'error'); |
|
246 | 246 | return; |
247 | 247 | } |
248 | 248 | } else { |
249 | - return parent::process_payment( $order_id, $retry, $force_customer ); |
|
249 | + return parent::process_payment($order_id, $retry, $force_customer); |
|
250 | 250 | } |
251 | 251 | } |
252 | 252 | |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * @param WC_Order $order |
256 | 256 | * @return void |
257 | 257 | */ |
258 | - public function process_pre_order_release_payment( $order ) { |
|
258 | + public function process_pre_order_release_payment($order) { |
|
259 | 259 | try { |
260 | 260 | // Define some callbacks if the first attempt fails. |
261 | 261 | $retry_callbacks = array( |
@@ -263,33 +263,33 @@ discard block |
||
263 | 263 | 'remove_order_customer_before_retry', |
264 | 264 | ); |
265 | 265 | |
266 | - while ( 1 ) { |
|
267 | - $source = $this->get_order_source( $order ); |
|
268 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
266 | + while (1) { |
|
267 | + $source = $this->get_order_source($order); |
|
268 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
269 | 269 | |
270 | - if ( is_wp_error( $response ) ) { |
|
271 | - if ( 0 === sizeof( $retry_callbacks ) ) { |
|
272 | - throw new Exception( $response->get_error_message() ); |
|
270 | + if (is_wp_error($response)) { |
|
271 | + if (0 === sizeof($retry_callbacks)) { |
|
272 | + throw new Exception($response->get_error_message()); |
|
273 | 273 | } else { |
274 | - $retry_callback = array_shift( $retry_callbacks ); |
|
275 | - call_user_func( array( $this, $retry_callback ), $order ); |
|
274 | + $retry_callback = array_shift($retry_callbacks); |
|
275 | + call_user_func(array($this, $retry_callback), $order); |
|
276 | 276 | } |
277 | 277 | } else { |
278 | 278 | // Successful |
279 | - $this->process_response( $response, $order ); |
|
279 | + $this->process_response($response, $order); |
|
280 | 280 | break; |
281 | 281 | } |
282 | 282 | } |
283 | 283 | |
284 | - } catch ( Exception $e ) { |
|
285 | - $order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() ); |
|
284 | + } catch (Exception $e) { |
|
285 | + $order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage()); |
|
286 | 286 | |
287 | 287 | // Mark order as failed if not already set, |
288 | 288 | // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
289 | - if ( ! $order->has_status( 'failed' ) ) { |
|
290 | - $order->update_status( 'failed', $order_note ); |
|
289 | + if ( ! $order->has_status('failed')) { |
|
290 | + $order->update_status('failed', $order_note); |
|
291 | 291 | } else { |
292 | - $order->add_order_note( $order_note ); |
|
292 | + $order->add_order_note($order_note); |
|
293 | 293 | } |
294 | 294 | } |
295 | 295 | } |
@@ -298,20 +298,20 @@ discard block |
||
298 | 298 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
299 | 299 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
300 | 300 | */ |
301 | - public function delete_resubscribe_meta( $resubscribe_order ) { |
|
302 | - delete_post_meta( $resubscribe_order->id, '_stripe_customer_id' ); |
|
303 | - delete_post_meta( $resubscribe_order->id, '_stripe_card_id' ); |
|
304 | - $this->delete_renewal_meta( $resubscribe_order ); |
|
301 | + public function delete_resubscribe_meta($resubscribe_order) { |
|
302 | + delete_post_meta($resubscribe_order->id, '_stripe_customer_id'); |
|
303 | + delete_post_meta($resubscribe_order->id, '_stripe_card_id'); |
|
304 | + $this->delete_renewal_meta($resubscribe_order); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
308 | 308 | * Don't transfer Stripe fee/ID meta to renewal orders. |
309 | 309 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
310 | 310 | */ |
311 | - public function delete_renewal_meta( $renewal_order ) { |
|
312 | - delete_post_meta( $renewal_order->id, 'Stripe Fee' ); |
|
313 | - delete_post_meta( $renewal_order->id, 'Net Revenue From Stripe' ); |
|
314 | - delete_post_meta( $renewal_order->id, 'Stripe Payment ID' ); |
|
311 | + public function delete_renewal_meta($renewal_order) { |
|
312 | + delete_post_meta($renewal_order->id, 'Stripe Fee'); |
|
313 | + delete_post_meta($renewal_order->id, 'Net Revenue From Stripe'); |
|
314 | + delete_post_meta($renewal_order->id, 'Stripe Payment ID'); |
|
315 | 315 | return $renewal_order; |
316 | 316 | } |
317 | 317 | |
@@ -321,11 +321,11 @@ discard block |
||
321 | 321 | * @param $amount_to_charge float The amount to charge. |
322 | 322 | * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
323 | 323 | */ |
324 | - public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
325 | - $response = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); |
|
324 | + public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
325 | + $response = $this->process_subscription_payment($renewal_order, $amount_to_charge); |
|
326 | 326 | |
327 | - if ( is_wp_error( $response ) ) { |
|
328 | - $renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
327 | + if (is_wp_error($response)) { |
|
328 | + $renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message())); |
|
329 | 329 | } |
330 | 330 | } |
331 | 331 | |
@@ -333,16 +333,16 @@ discard block |
||
333 | 333 | * Remove order meta |
334 | 334 | * @param object $order |
335 | 335 | */ |
336 | - public function remove_order_source_before_retry( $order ) { |
|
337 | - delete_post_meta( $order->id, '_stripe_card_id' ); |
|
336 | + public function remove_order_source_before_retry($order) { |
|
337 | + delete_post_meta($order->id, '_stripe_card_id'); |
|
338 | 338 | } |
339 | 339 | |
340 | 340 | /** |
341 | 341 | * Remove order meta |
342 | 342 | * @param object $order |
343 | 343 | */ |
344 | - public function remove_order_customer_before_retry( $order ) { |
|
345 | - delete_post_meta( $order->id, '_stripe_customer_id' ); |
|
344 | + public function remove_order_customer_before_retry($order) { |
|
345 | + delete_post_meta($order->id, '_stripe_customer_id'); |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | /** |
@@ -354,9 +354,9 @@ discard block |
||
354 | 354 | * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
355 | 355 | * @return void |
356 | 356 | */ |
357 | - public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
358 | - update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
359 | - update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id ); |
|
357 | + public function update_failing_payment_method($subscription, $renewal_order) { |
|
358 | + update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
359 | + update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id); |
|
360 | 360 | } |
361 | 361 | |
362 | 362 | /** |
@@ -368,15 +368,15 @@ discard block |
||
368 | 368 | * @param WC_Subscription $subscription An instance of a subscription object |
369 | 369 | * @return array |
370 | 370 | */ |
371 | - public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
372 | - $payment_meta[ $this->id ] = array( |
|
371 | + public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
372 | + $payment_meta[$this->id] = array( |
|
373 | 373 | 'post_meta' => array( |
374 | 374 | '_stripe_customer_id' => array( |
375 | - 'value' => get_post_meta( $subscription->id, '_stripe_customer_id', true ), |
|
375 | + 'value' => get_post_meta($subscription->id, '_stripe_customer_id', true), |
|
376 | 376 | 'label' => 'Stripe Customer ID', |
377 | 377 | ), |
378 | 378 | '_stripe_card_id' => array( |
379 | - 'value' => get_post_meta( $subscription->id, '_stripe_card_id', true ), |
|
379 | + 'value' => get_post_meta($subscription->id, '_stripe_card_id', true), |
|
380 | 380 | 'label' => 'Stripe Card ID', |
381 | 381 | ), |
382 | 382 | ), |
@@ -393,17 +393,17 @@ discard block |
||
393 | 393 | * @param array $payment_meta associative array of meta data required for automatic payments |
394 | 394 | * @return array |
395 | 395 | */ |
396 | - public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
397 | - if ( $this->id === $payment_method_id ) { |
|
396 | + public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
397 | + if ($this->id === $payment_method_id) { |
|
398 | 398 | |
399 | - if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
400 | - throw new Exception( 'A "_stripe_customer_id" value is required.' ); |
|
401 | - } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
402 | - throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' ); |
|
399 | + if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
400 | + throw new Exception('A "_stripe_customer_id" value is required.'); |
|
401 | + } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
402 | + throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".'); |
|
403 | 403 | } |
404 | 404 | |
405 | - if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) { |
|
406 | - throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' ); |
|
405 | + if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) { |
|
406 | + throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".'); |
|
407 | 407 | } |
408 | 408 | } |
409 | 409 | } |
@@ -416,43 +416,43 @@ discard block |
||
416 | 416 | * @param WC_Subscription $subscription the subscription details |
417 | 417 | * @return string the subscription payment method |
418 | 418 | */ |
419 | - public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
419 | + public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
420 | 420 | // bail for other payment methods |
421 | - if ( $this->id !== $subscription->payment_method || ! $subscription->customer_user ) { |
|
421 | + if ($this->id !== $subscription->payment_method || ! $subscription->customer_user) { |
|
422 | 422 | return $payment_method_to_display; |
423 | 423 | } |
424 | 424 | |
425 | 425 | $stripe_customer = new WC_Stripe_Customer(); |
426 | - $stripe_customer_id = get_post_meta( $subscription->id, '_stripe_customer_id', true ); |
|
427 | - $stripe_card_id = get_post_meta( $subscription->id, '_stripe_card_id', true ); |
|
426 | + $stripe_customer_id = get_post_meta($subscription->id, '_stripe_customer_id', true); |
|
427 | + $stripe_card_id = get_post_meta($subscription->id, '_stripe_card_id', true); |
|
428 | 428 | |
429 | 429 | // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
430 | - if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
430 | + if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
431 | 431 | $user_id = $subscription->customer_user; |
432 | - $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
433 | - $stripe_card_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
432 | + $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
433 | + $stripe_card_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
434 | 434 | } |
435 | 435 | |
436 | 436 | // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
437 | - if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
438 | - $stripe_customer_id = get_post_meta( $subscription->order->id, '_stripe_customer_id', true ); |
|
439 | - $stripe_card_id = get_post_meta( $subscription->order->id, '_stripe_card_id', true ); |
|
437 | + if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
438 | + $stripe_customer_id = get_post_meta($subscription->order->id, '_stripe_customer_id', true); |
|
439 | + $stripe_card_id = get_post_meta($subscription->order->id, '_stripe_card_id', true); |
|
440 | 440 | } |
441 | 441 | |
442 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
442 | + $stripe_customer->set_id($stripe_customer_id); |
|
443 | 443 | $cards = $stripe_customer->get_cards(); |
444 | 444 | |
445 | - if ( $cards ) { |
|
445 | + if ($cards) { |
|
446 | 446 | $found_card = false; |
447 | - foreach ( $cards as $card ) { |
|
448 | - if ( $card->id === $stripe_card_id ) { |
|
447 | + foreach ($cards as $card) { |
|
448 | + if ($card->id === $stripe_card_id) { |
|
449 | 449 | $found_card = true; |
450 | - $payment_method_to_display = sprintf( __( 'Via %s card ending in %s', 'woocommerce-gateway-stripe' ), ( isset( $card->type ) ? $card->type : $card->brand ), $card->last4 ); |
|
450 | + $payment_method_to_display = sprintf(__('Via %s card ending in %s', 'woocommerce-gateway-stripe'), (isset($card->type) ? $card->type : $card->brand), $card->last4); |
|
451 | 451 | break; |
452 | 452 | } |
453 | 453 | } |
454 | - if ( ! $found_card ) { |
|
455 | - $payment_method_to_display = sprintf( __( 'Via %s card ending in %s', 'woocommerce-gateway-stripe' ), ( isset( $cards[0]->type ) ? $cards[0]->type : $cards[0]->brand ), $cards[0]->last4 ); |
|
454 | + if ( ! $found_card) { |
|
455 | + $payment_method_to_display = sprintf(__('Via %s card ending in %s', 'woocommerce-gateway-stripe'), (isset($cards[0]->type) ? $cards[0]->type : $cards[0]->brand), $cards[0]->last4); |
|
456 | 456 | } |
457 | 457 | } |
458 | 458 |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -15,8 +15,8 @@ discard block |
||
15 | 15 | */ |
16 | 16 | public function __construct() { |
17 | 17 | $this->id = 'stripe'; |
18 | - $this->method_title = __( 'Stripe', 'woocommerce-gateway-stripe' ); |
|
19 | - $this->method_description = __( 'Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' ); |
|
18 | + $this->method_title = __('Stripe', 'woocommerce-gateway-stripe'); |
|
19 | + $this->method_description = __('Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe'); |
|
20 | 20 | $this->has_fields = true; |
21 | 21 | $this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s'; |
22 | 22 | $this->supports = array( |
@@ -42,35 +42,35 @@ discard block |
||
42 | 42 | $this->init_settings(); |
43 | 43 | |
44 | 44 | // Get setting values. |
45 | - $this->title = $this->get_option( 'title' ); |
|
46 | - $this->description = $this->get_option( 'description' ); |
|
47 | - $this->enabled = $this->get_option( 'enabled' ); |
|
48 | - $this->testmode = 'yes' === $this->get_option( 'testmode' ); |
|
49 | - $this->capture = 'yes' === $this->get_option( 'capture', 'yes' ); |
|
50 | - $this->stripe_checkout = 'yes' === $this->get_option( 'stripe_checkout' ); |
|
51 | - $this->stripe_checkout_locale = $this->get_option( 'stripe_checkout_locale' ); |
|
52 | - $this->stripe_checkout_image = $this->get_option( 'stripe_checkout_image', '' ); |
|
53 | - $this->saved_cards = 'yes' === $this->get_option( 'saved_cards' ); |
|
54 | - $this->secret_key = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' ); |
|
55 | - $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); |
|
56 | - $this->bitcoin = 'USD' === strtoupper( get_woocommerce_currency() ) && 'yes' === $this->get_option( 'stripe_bitcoin' ); |
|
57 | - $this->logging = 'yes' === $this->get_option( 'logging' ); |
|
58 | - |
|
59 | - if ( $this->stripe_checkout ) { |
|
60 | - $this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' ); |
|
45 | + $this->title = $this->get_option('title'); |
|
46 | + $this->description = $this->get_option('description'); |
|
47 | + $this->enabled = $this->get_option('enabled'); |
|
48 | + $this->testmode = 'yes' === $this->get_option('testmode'); |
|
49 | + $this->capture = 'yes' === $this->get_option('capture', 'yes'); |
|
50 | + $this->stripe_checkout = 'yes' === $this->get_option('stripe_checkout'); |
|
51 | + $this->stripe_checkout_locale = $this->get_option('stripe_checkout_locale'); |
|
52 | + $this->stripe_checkout_image = $this->get_option('stripe_checkout_image', ''); |
|
53 | + $this->saved_cards = 'yes' === $this->get_option('saved_cards'); |
|
54 | + $this->secret_key = $this->testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key'); |
|
55 | + $this->publishable_key = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key'); |
|
56 | + $this->bitcoin = 'USD' === strtoupper(get_woocommerce_currency()) && 'yes' === $this->get_option('stripe_bitcoin'); |
|
57 | + $this->logging = 'yes' === $this->get_option('logging'); |
|
58 | + |
|
59 | + if ($this->stripe_checkout) { |
|
60 | + $this->order_button_text = __('Continue to payment', 'woocommerce-gateway-stripe'); |
|
61 | 61 | } |
62 | 62 | |
63 | - if ( $this->testmode ) { |
|
64 | - $this->description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' ); |
|
65 | - $this->description = trim( $this->description ); |
|
63 | + if ($this->testmode) { |
|
64 | + $this->description .= ' ' . sprintf(__('TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe'), 'https://stripe.com/docs/testing'); |
|
65 | + $this->description = trim($this->description); |
|
66 | 66 | } |
67 | 67 | |
68 | - WC_Stripe_API::set_secret_key( $this->secret_key ); |
|
68 | + WC_Stripe_API::set_secret_key($this->secret_key); |
|
69 | 69 | |
70 | 70 | // Hooks |
71 | - add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
|
72 | - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
73 | - add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
|
71 | + add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); |
|
72 | + add_action('admin_notices', array($this, 'admin_notices')); |
|
73 | + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -80,35 +80,35 @@ discard block |
||
80 | 80 | * @return string |
81 | 81 | */ |
82 | 82 | public function get_icon() { |
83 | - $ext = version_compare( WC()->version, '2.6', '>=' ) ? '.svg' : '.png'; |
|
84 | - $style = version_compare( WC()->version, '2.6', '>=' ) ? 'style="margin-left: 0.3em"' : ''; |
|
83 | + $ext = version_compare(WC()->version, '2.6', '>=') ? '.svg' : '.png'; |
|
84 | + $style = version_compare(WC()->version, '2.6', '>=') ? 'style="margin-left: 0.3em"' : ''; |
|
85 | 85 | |
86 | - $icon = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext ) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
87 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext ) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
88 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext ) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
86 | + $icon = '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
87 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
88 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
89 | 89 | |
90 | - if ( 'USD' === get_woocommerce_currency() ) { |
|
91 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext ) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
92 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext ) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
93 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext ) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
90 | + if ('USD' === get_woocommerce_currency()) { |
|
91 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
92 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
93 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
94 | 94 | } |
95 | 95 | |
96 | - if ( $this->bitcoin ) { |
|
97 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE ) ) . '" alt="Bitcoin" width="32" ' . $style . ' />'; |
|
96 | + if ($this->bitcoin) { |
|
97 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(plugins_url('/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE)) . '" alt="Bitcoin" width="32" ' . $style . ' />'; |
|
98 | 98 | } |
99 | 99 | |
100 | - return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id ); |
|
100 | + return apply_filters('woocommerce_gateway_icon', $icon, $this->id); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
104 | 104 | * Get Stripe amount to pay |
105 | 105 | * @return float |
106 | 106 | */ |
107 | - public function get_stripe_amount( $total, $currency = '' ) { |
|
108 | - if ( ! $currency ) { |
|
107 | + public function get_stripe_amount($total, $currency = '') { |
|
108 | + if ( ! $currency) { |
|
109 | 109 | $currency = get_woocommerce_currency(); |
110 | 110 | } |
111 | - switch ( strtoupper( $currency ) ) { |
|
111 | + switch (strtoupper($currency)) { |
|
112 | 112 | // Zero decimal currencies |
113 | 113 | case 'BIF' : |
114 | 114 | case 'CLP' : |
@@ -125,10 +125,10 @@ discard block |
||
125 | 125 | case 'XAF' : |
126 | 126 | case 'XOF' : |
127 | 127 | case 'XPF' : |
128 | - $total = absint( $total ); |
|
128 | + $total = absint($total); |
|
129 | 129 | break; |
130 | 130 | default : |
131 | - $total = round( $total, 2 ) * 100; // In cents |
|
131 | + $total = round($total, 2) * 100; // In cents |
|
132 | 132 | break; |
133 | 133 | } |
134 | 134 | return $total; |
@@ -138,31 +138,31 @@ discard block |
||
138 | 138 | * Check if SSL is enabled and notify the user |
139 | 139 | */ |
140 | 140 | public function admin_notices() { |
141 | - if ( $this->enabled == 'no' ) { |
|
141 | + if ($this->enabled == 'no') { |
|
142 | 142 | return; |
143 | 143 | } |
144 | 144 | |
145 | - $addons = ( class_exists( 'WC_Subscriptions_Order' ) || class_exists( 'WC_Pre_Orders_Order' ) ) ? '_addons' : ''; |
|
145 | + $addons = (class_exists('WC_Subscriptions_Order') || class_exists('WC_Pre_Orders_Order')) ? '_addons' : ''; |
|
146 | 146 | |
147 | 147 | // Check required fields |
148 | - if ( ! $this->secret_key ) { |
|
149 | - echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Please enter your secret key <a href="%s">here</a>', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons ) ) . '</p></div>'; |
|
148 | + if ( ! $this->secret_key) { |
|
149 | + echo '<div class="error"><p>' . sprintf(__('Stripe error: Please enter your secret key <a href="%s">here</a>', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons)) . '</p></div>'; |
|
150 | 150 | return; |
151 | 151 | |
152 | - } elseif ( ! $this->publishable_key ) { |
|
153 | - echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Please enter your publishable key <a href="%s">here</a>', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons ) ) . '</p></div>'; |
|
152 | + } elseif ( ! $this->publishable_key) { |
|
153 | + echo '<div class="error"><p>' . sprintf(__('Stripe error: Please enter your publishable key <a href="%s">here</a>', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons)) . '</p></div>'; |
|
154 | 154 | return; |
155 | 155 | } |
156 | 156 | |
157 | 157 | // Simple check for duplicate keys |
158 | - if ( $this->secret_key == $this->publishable_key ) { |
|
159 | - echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Your secret and publishable keys match. Please check and re-enter.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons ) ) . '</p></div>'; |
|
158 | + if ($this->secret_key == $this->publishable_key) { |
|
159 | + echo '<div class="error"><p>' . sprintf(__('Stripe error: Your secret and publishable keys match. Please check and re-enter.', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_stripe' . $addons)) . '</p></div>'; |
|
160 | 160 | return; |
161 | 161 | } |
162 | 162 | |
163 | 163 | // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected |
164 | - if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) { |
|
165 | - echo '<div class="error"><p>' . sprintf( __( 'Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '</p></div>'; |
|
164 | + if ((function_exists('wc_site_is_https') && ! wc_site_is_https()) && ('no' === get_option('woocommerce_force_ssl_checkout') && ! class_exists('WordPressHTTPS'))) { |
|
165 | + echo '<div class="error"><p>' . sprintf(__('Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe'), admin_url('admin.php?page=wc-settings&tab=checkout')) . '</p></div>'; |
|
166 | 166 | } |
167 | 167 | } |
168 | 168 | |
@@ -170,11 +170,11 @@ discard block |
||
170 | 170 | * Check if this gateway is enabled |
171 | 171 | */ |
172 | 172 | public function is_available() { |
173 | - if ( 'yes' === $this->enabled ) { |
|
174 | - if ( ! $this->testmode && is_checkout() && ! is_ssl() ) { |
|
173 | + if ('yes' === $this->enabled) { |
|
174 | + if ( ! $this->testmode && is_checkout() && ! is_ssl()) { |
|
175 | 175 | return false; |
176 | 176 | } |
177 | - if ( ! $this->secret_key || ! $this->publishable_key ) { |
|
177 | + if ( ! $this->secret_key || ! $this->publishable_key) { |
|
178 | 178 | return false; |
179 | 179 | } |
180 | 180 | return true; |
@@ -186,9 +186,9 @@ discard block |
||
186 | 186 | * Initialise Gateway Settings Form Fields |
187 | 187 | */ |
188 | 188 | public function init_form_fields() { |
189 | - $this->form_fields = include( untrailingslashit( plugin_dir_path( WC_STRIPE_MAIN_FILE ) ) . '/includes/settings-stripe.php' ); |
|
189 | + $this->form_fields = include(untrailingslashit(plugin_dir_path(WC_STRIPE_MAIN_FILE)) . '/includes/settings-stripe.php'); |
|
190 | 190 | |
191 | - wc_enqueue_js( " |
|
191 | + wc_enqueue_js(" |
|
192 | 192 | jQuery( function( $ ) { |
193 | 193 | $( '#woocommerce_stripe_stripe_checkout' ).change(function(){ |
194 | 194 | if ( $( this ).is( ':checked' ) ) { |
@@ -208,25 +208,25 @@ discard block |
||
208 | 208 | ?> |
209 | 209 | <fieldset class="stripe-legacy-payment-fields"> |
210 | 210 | <?php |
211 | - if ( $this->description ) { |
|
212 | - echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) ); |
|
211 | + if ($this->description) { |
|
212 | + echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description))); |
|
213 | 213 | } |
214 | - if ( $this->saved_cards && is_user_logged_in() ) { |
|
215 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
214 | + if ($this->saved_cards && is_user_logged_in()) { |
|
215 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
216 | 216 | ?> |
217 | 217 | <p class="form-row form-row-wide"> |
218 | - <a class="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_class', 'button' ); ?>" style="float:right;" href="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); ?>#saved-cards"><?php esc_html_e( 'Manage cards', 'woocommerce-gateway-stripe' ); ?></a> |
|
218 | + <a class="<?php echo apply_filters('wc_stripe_manage_saved_cards_class', 'button'); ?>" style="float:right;" href="<?php echo apply_filters('wc_stripe_manage_saved_cards_url', get_permalink(get_option('woocommerce_myaccount_page_id'))); ?>#saved-cards"><?php esc_html_e('Manage cards', 'woocommerce-gateway-stripe'); ?></a> |
|
219 | 219 | <?php |
220 | - if ( $cards = $stripe_customer->get_cards() ) { |
|
220 | + if ($cards = $stripe_customer->get_cards()) { |
|
221 | 221 | $default_card = $cards[0]->id; |
222 | - foreach ( (array) $cards as $card ) { |
|
223 | - if ( 'card' !== $card->object ) { |
|
222 | + foreach ((array) $cards as $card) { |
|
223 | + if ('card' !== $card->object) { |
|
224 | 224 | continue; |
225 | 225 | } |
226 | 226 | ?> |
227 | - <label for="stripe_card_<?php echo $card->id; ?>" class="brand-<?php echo esc_attr( strtolower( $card->brand ) ); ?>"> |
|
228 | - <input type="radio" id="stripe_card_<?php echo $card->id; ?>" name="wc-stripe-payment-token" value="<?php echo $card->id; ?>" <?php checked( $default_card, $card->id ) ?> /> |
|
229 | - <?php printf( __( '%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe' ), $card->brand, $card->last4, $card->exp_month, $card->exp_year ); ?> |
|
227 | + <label for="stripe_card_<?php echo $card->id; ?>" class="brand-<?php echo esc_attr(strtolower($card->brand)); ?>"> |
|
228 | + <input type="radio" id="stripe_card_<?php echo $card->id; ?>" name="wc-stripe-payment-token" value="<?php echo $card->id; ?>" <?php checked($default_card, $card->id) ?> /> |
|
229 | + <?php printf(__('%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe'), $card->brand, $card->last4, $card->exp_month, $card->exp_year); ?> |
|
230 | 230 | </label> |
231 | 231 | <?php |
232 | 232 | } |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | ?> |
235 | 235 | <label for="new"> |
236 | 236 | <input type="radio" id="new" name="wc-stripe-payment-token" value="new" /> |
237 | - <?php _e( 'Use a new credit card', 'woocommerce-gateway-stripe' ); ?> |
|
237 | + <?php _e('Use a new credit card', 'woocommerce-gateway-stripe'); ?> |
|
238 | 238 | </label> |
239 | 239 | </p> |
240 | 240 | <?php |
@@ -242,8 +242,8 @@ discard block |
||
242 | 242 | |
243 | 243 | $user = wp_get_current_user(); |
244 | 244 | |
245 | - if ( $user ) { |
|
246 | - $user_email = get_user_meta( $user->ID, 'billing_email', true ); |
|
245 | + if ($user) { |
|
246 | + $user_email = get_user_meta($user->ID, 'billing_email', true); |
|
247 | 247 | $user_email = $user_email ? $user_email : $user->user_email; |
248 | 248 | } else { |
249 | 249 | $user_email = ''; |
@@ -251,22 +251,22 @@ discard block |
||
251 | 251 | |
252 | 252 | $display = ''; |
253 | 253 | |
254 | - if ( $this->stripe_checkout || $this->saved_cards && ! empty( $cards ) ) { |
|
254 | + if ($this->stripe_checkout || $this->saved_cards && ! empty($cards)) { |
|
255 | 255 | $display = 'style="display:none;"'; |
256 | 256 | } |
257 | 257 | |
258 | 258 | echo '<div ' . $display . ' id="stripe-payment-data" |
259 | 259 | data-description="" |
260 | - data-email="' . esc_attr( $user_email ) . '" |
|
261 | - data-amount="' . esc_attr( $this->get_stripe_amount( WC()->cart->total ) ) . '" |
|
262 | - data-name="' . esc_attr( sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name', 'display' ) ) ) . '" |
|
263 | - data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '" |
|
264 | - data-image="' . esc_attr( $this->stripe_checkout_image ) . '" |
|
265 | - data-bitcoin="' . esc_attr( $this->bitcoin ? 'true' : 'false' ) . '" |
|
266 | - data-locale="' . esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ) . '">'; |
|
267 | - |
|
268 | - if ( ! $this->stripe_checkout ) { |
|
269 | - $this->credit_card_form( array( 'fields_have_names' => false ) ); |
|
260 | + data-email="' . esc_attr($user_email) . '" |
|
261 | + data-amount="' . esc_attr($this->get_stripe_amount(WC()->cart->total)) . '" |
|
262 | + data-name="' . esc_attr(sprintf(__('%s', 'woocommerce-gateway-stripe'), get_bloginfo('name', 'display'))) . '" |
|
263 | + data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '" |
|
264 | + data-image="' . esc_attr($this->stripe_checkout_image) . '" |
|
265 | + data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '" |
|
266 | + data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '">'; |
|
267 | + |
|
268 | + if ( ! $this->stripe_checkout) { |
|
269 | + $this->credit_card_form(array('fields_have_names' => false)); |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | echo '</div>'; |
@@ -283,27 +283,27 @@ discard block |
||
283 | 283 | * @access public |
284 | 284 | */ |
285 | 285 | public function payment_scripts() { |
286 | - if ( $this->stripe_checkout ) { |
|
287 | - wp_enqueue_script( 'stripe', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true ); |
|
288 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe_checkout.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
286 | + if ($this->stripe_checkout) { |
|
287 | + wp_enqueue_script('stripe', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true); |
|
288 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe_checkout.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
289 | 289 | } else { |
290 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
291 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe.js', WC_STRIPE_MAIN_FILE ), array( 'jquery-payment', 'stripe' ), WC_STRIPE_VERSION, true ); |
|
290 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
291 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe.js', WC_STRIPE_MAIN_FILE), array('jquery-payment', 'stripe'), WC_STRIPE_VERSION, true); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | $stripe_params = array( |
295 | 295 | 'key' => $this->publishable_key, |
296 | - 'i18n_terms' => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ), |
|
297 | - 'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ), |
|
296 | + 'i18n_terms' => __('Please accept the terms and conditions first', 'woocommerce-gateway-stripe'), |
|
297 | + 'i18n_required_fields' => __('Please fill in required checkout fields first', 'woocommerce-gateway-stripe'), |
|
298 | 298 | ); |
299 | 299 | |
300 | 300 | // If we're on the pay page we need to pass stripe.js the address of the order. |
301 | - if ( is_checkout_pay_page() && isset( $_GET['order'] ) && isset( $_GET['order_id'] ) ) { |
|
302 | - $order_key = urldecode( $_GET['order'] ); |
|
303 | - $order_id = absint( $_GET['order_id'] ); |
|
304 | - $order = wc_get_order( $order_id ); |
|
301 | + if (is_checkout_pay_page() && isset($_GET['order']) && isset($_GET['order_id'])) { |
|
302 | + $order_key = urldecode($_GET['order']); |
|
303 | + $order_id = absint($_GET['order_id']); |
|
304 | + $order = wc_get_order($order_id); |
|
305 | 305 | |
306 | - if ( $order->id === $order_id && $order->order_key === $order_key ) { |
|
306 | + if ($order->id === $order_id && $order->order_key === $order_key) { |
|
307 | 307 | $stripe_params['billing_first_name'] = $order->billing_first_name; |
308 | 308 | $stripe_params['billing_last_name'] = $order->billing_last_name; |
309 | 309 | $stripe_params['billing_address_1'] = $order->billing_address_1; |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | - wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) ); |
|
318 | + wp_localize_script('woocommerce_stripe', 'wc_stripe_params', apply_filters('wc_stripe_params', $stripe_params)); |
|
319 | 319 | } |
320 | 320 | |
321 | 321 | /** |
@@ -324,24 +324,24 @@ discard block |
||
324 | 324 | * @param object $source |
325 | 325 | * @return array() |
326 | 326 | */ |
327 | - protected function generate_payment_request( $order, $source ) { |
|
327 | + protected function generate_payment_request($order, $source) { |
|
328 | 328 | $post_data = array(); |
329 | - $post_data['currency'] = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() ); |
|
330 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
331 | - $post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ); |
|
329 | + $post_data['currency'] = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency()); |
|
330 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
331 | + $post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number()); |
|
332 | 332 | $post_data['capture'] = $this->capture ? 'true' : 'false'; |
333 | 333 | |
334 | - if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
334 | + if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
335 | 335 | $post_data['receipt_email'] = $order->billing_email; |
336 | 336 | } |
337 | 337 | |
338 | - $post_data['expand[]'] = 'balance_transaction'; |
|
338 | + $post_data['expand[]'] = 'balance_transaction'; |
|
339 | 339 | |
340 | - if ( $source->customer ) { |
|
340 | + if ($source->customer) { |
|
341 | 341 | $post_data['customer'] = $source->customer; |
342 | 342 | } |
343 | 343 | |
344 | - if ( $source->source ) { |
|
344 | + if ($source->source) { |
|
345 | 345 | $post_data['source'] = $source->source; |
346 | 346 | } |
347 | 347 | |
@@ -353,22 +353,22 @@ discard block |
||
353 | 353 | * @param bool $force_customer Should we force customer creation? |
354 | 354 | * @return object |
355 | 355 | */ |
356 | - protected function get_source( $user_id, $force_customer = false ) { |
|
357 | - $stripe_customer = new WC_Stripe_Customer( $user_id ); |
|
356 | + protected function get_source($user_id, $force_customer = false) { |
|
357 | + $stripe_customer = new WC_Stripe_Customer($user_id); |
|
358 | 358 | $stripe_source = false; |
359 | 359 | $token_id = false; |
360 | 360 | |
361 | 361 | // New CC info was entered and we have a new token to process |
362 | - if ( isset( $_POST['stripe_token'] ) ) { |
|
363 | - $stripe_token = wc_clean( $_POST['stripe_token'] ); |
|
364 | - $maybe_saved_card = isset( $_POST['wc-stripe-new-payment-method'] ) && ! empty( $_POST['wc-stripe-new-payment-method'] ); |
|
362 | + if (isset($_POST['stripe_token'])) { |
|
363 | + $stripe_token = wc_clean($_POST['stripe_token']); |
|
364 | + $maybe_saved_card = isset($_POST['wc-stripe-new-payment-method']) && ! empty($_POST['wc-stripe-new-payment-method']); |
|
365 | 365 | |
366 | 366 | // This is true if the user wants to store the card to their account. |
367 | - if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_customer ) { |
|
368 | - $stripe_source = $stripe_customer->add_card( $stripe_token ); |
|
367 | + if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_customer) { |
|
368 | + $stripe_source = $stripe_customer->add_card($stripe_token); |
|
369 | 369 | |
370 | - if ( is_wp_error( $stripe_source ) ) { |
|
371 | - throw new Exception( $stripe_source->get_error_message() ); |
|
370 | + if (is_wp_error($stripe_source)) { |
|
371 | + throw new Exception($stripe_source->get_error_message()); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | } else { |
@@ -379,8 +379,8 @@ discard block |
||
379 | 379 | } |
380 | 380 | |
381 | 381 | // Use an existing token, and then process the payment |
382 | - elseif ( isset( $_POST['wc-stripe-payment-token'] ) && 'new' !== $_POST['wc-stripe-payment-token'] ) { |
|
383 | - $stripe_source = wc_clean( $_POST['wc-stripe-payment-token'] ); |
|
382 | + elseif (isset($_POST['wc-stripe-payment-token']) && 'new' !== $_POST['wc-stripe-payment-token']) { |
|
383 | + $stripe_source = wc_clean($_POST['wc-stripe-payment-token']); |
|
384 | 384 | } |
385 | 385 | |
386 | 386 | return (object) array( |
@@ -401,16 +401,16 @@ discard block |
||
401 | 401 | * @param object $order |
402 | 402 | * @return object |
403 | 403 | */ |
404 | - protected function get_order_source( $order = null ) { |
|
404 | + protected function get_order_source($order = null) { |
|
405 | 405 | $stripe_customer = new WC_Stripe_Customer(); |
406 | 406 | $stripe_source = false; |
407 | 407 | $token_id = false; |
408 | 408 | |
409 | - if ( $order ) { |
|
410 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_customer_id', true ) ) { |
|
411 | - $stripe_customer->set_id( $meta_value ); |
|
409 | + if ($order) { |
|
410 | + if ($meta_value = get_post_meta($order->id, '_stripe_customer_id', true)) { |
|
411 | + $stripe_customer->set_id($meta_value); |
|
412 | 412 | } |
413 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_card_id', true ) ) { |
|
413 | + if ($meta_value = get_post_meta($order->id, '_stripe_card_id', true)) { |
|
414 | 414 | $stripe_source = $meta_value; |
415 | 415 | } |
416 | 416 | } |
@@ -425,25 +425,25 @@ discard block |
||
425 | 425 | /** |
426 | 426 | * Process the payment |
427 | 427 | */ |
428 | - public function process_payment( $order_id, $retry = true, $force_customer = false ) { |
|
428 | + public function process_payment($order_id, $retry = true, $force_customer = false) { |
|
429 | 429 | try { |
430 | - $order = wc_get_order( $order_id ); |
|
431 | - $source = $this->get_source( get_current_user_id(), $force_customer ); |
|
430 | + $order = wc_get_order($order_id); |
|
431 | + $source = $this->get_source(get_current_user_id(), $force_customer); |
|
432 | 432 | |
433 | - if ( empty( $source->source ) && empty( $source->customer ) ) { |
|
434 | - $error_msg = __( 'Please enter your card details to make a payment.', 'woocommerce-gateway-stripe' ); |
|
435 | - $error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' ); |
|
436 | - throw new Exception( $error_msg ); |
|
433 | + if (empty($source->source) && empty($source->customer)) { |
|
434 | + $error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe'); |
|
435 | + $error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe'); |
|
436 | + throw new Exception($error_msg); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | // Store source to order meta |
440 | - $this->save_source( $order, $source ); |
|
440 | + $this->save_source($order, $source); |
|
441 | 441 | |
442 | 442 | // Handle payment |
443 | - if ( $order->get_total() > 0 ) { |
|
443 | + if ($order->get_total() > 0) { |
|
444 | 444 | |
445 | 445 | // Check order amount |
446 | - switch ( get_woocommerce_currency() ) { |
|
446 | + switch (get_woocommerce_currency()) { |
|
447 | 447 | case 'USD': |
448 | 448 | case 'CAD': |
449 | 449 | case 'EUR': |
@@ -475,26 +475,26 @@ discard block |
||
475 | 475 | $minimum_amount = 50; |
476 | 476 | break; |
477 | 477 | } |
478 | - if ( $order->get_total() * 100 < $minimum_amount ) { |
|
479 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( $minimum_amount / 100 ) ) ); |
|
478 | + if ($order->get_total() * 100 < $minimum_amount) { |
|
479 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price($minimum_amount / 100))); |
|
480 | 480 | } |
481 | 481 | |
482 | - WC_Stripe::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
482 | + WC_Stripe::log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
483 | 483 | |
484 | 484 | // Make the request |
485 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
485 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
486 | 486 | |
487 | - if ( is_wp_error( $response ) ) { |
|
487 | + if (is_wp_error($response)) { |
|
488 | 488 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
489 | - if ( 'customer' === $response->get_error_code() && $retry ) { |
|
490 | - delete_user_meta( get_current_user_id(), '_stripe_customer_id' ); |
|
491 | - return $this->process_payment( $order_id, false, $force_customer ); |
|
489 | + if ('customer' === $response->get_error_code() && $retry) { |
|
490 | + delete_user_meta(get_current_user_id(), '_stripe_customer_id'); |
|
491 | + return $this->process_payment($order_id, false, $force_customer); |
|
492 | 492 | } |
493 | - throw new Exception( $response->get_error_code() . ': ' . $response->get_error_message() ); |
|
493 | + throw new Exception($response->get_error_code() . ': ' . $response->get_error_message()); |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | // Process valid response |
497 | - $this->process_response( $response, $order ); |
|
497 | + $this->process_response($response, $order); |
|
498 | 498 | } else { |
499 | 499 | $order->payment_complete(); |
500 | 500 | } |
@@ -505,13 +505,13 @@ discard block |
||
505 | 505 | // Return thank you page redirect |
506 | 506 | return array( |
507 | 507 | 'result' => 'success', |
508 | - 'redirect' => $this->get_return_url( $order ) |
|
508 | + 'redirect' => $this->get_return_url($order) |
|
509 | 509 | ); |
510 | 510 | |
511 | - } catch ( Exception $e ) { |
|
512 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
513 | - WC()->session->set( 'refresh_totals', true ); |
|
514 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
511 | + } catch (Exception $e) { |
|
512 | + wc_add_notice($e->getMessage(), 'error'); |
|
513 | + WC()->session->set('refresh_totals', true); |
|
514 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
515 | 515 | return; |
516 | 516 | } |
517 | 517 | } |
@@ -519,45 +519,45 @@ discard block |
||
519 | 519 | /** |
520 | 520 | * Save source to order. |
521 | 521 | */ |
522 | - protected function save_source( $order, $source ) { |
|
522 | + protected function save_source($order, $source) { |
|
523 | 523 | // Store source in the order |
524 | - if ( $source->customer ) { |
|
525 | - update_post_meta( $order->id, '_stripe_customer_id', $source->customer ); |
|
524 | + if ($source->customer) { |
|
525 | + update_post_meta($order->id, '_stripe_customer_id', $source->customer); |
|
526 | 526 | } |
527 | - if ( $source->source ) { |
|
528 | - update_post_meta( $order->id, '_stripe_card_id', $source->source->id ); |
|
527 | + if ($source->source) { |
|
528 | + update_post_meta($order->id, '_stripe_card_id', $source->source->id); |
|
529 | 529 | } |
530 | 530 | } |
531 | 531 | |
532 | 532 | /** |
533 | 533 | * Store extra meta data for an order from a Stripe Response. |
534 | 534 | */ |
535 | - public function process_response( $response, $order ) { |
|
536 | - WC_Stripe::log( "Processing response: " . print_r( $response, true ) ); |
|
535 | + public function process_response($response, $order) { |
|
536 | + WC_Stripe::log("Processing response: " . print_r($response, true)); |
|
537 | 537 | |
538 | 538 | // Store charge data |
539 | - update_post_meta( $order->id, '_stripe_charge_id', $response->id ); |
|
540 | - update_post_meta( $order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' ); |
|
539 | + update_post_meta($order->id, '_stripe_charge_id', $response->id); |
|
540 | + update_post_meta($order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no'); |
|
541 | 541 | |
542 | 542 | // Store other data such as fees |
543 | - if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) { |
|
544 | - $fee = number_format( $response->balance_transaction->fee / 100, 2, '.', '' ); |
|
545 | - update_post_meta( $order->id, 'Stripe Fee', $fee ); |
|
546 | - update_post_meta( $order->id, 'Net Revenue From Stripe', $order->get_total() - $fee ); |
|
543 | + if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) { |
|
544 | + $fee = number_format($response->balance_transaction->fee / 100, 2, '.', ''); |
|
545 | + update_post_meta($order->id, 'Stripe Fee', $fee); |
|
546 | + update_post_meta($order->id, 'Net Revenue From Stripe', $order->get_total() - $fee); |
|
547 | 547 | } |
548 | 548 | |
549 | - if ( $response->captured ) { |
|
550 | - $order->payment_complete( $response->id ); |
|
551 | - WC_Stripe::log( "Successful charge: $response->id" ); |
|
549 | + if ($response->captured) { |
|
550 | + $order->payment_complete($response->id); |
|
551 | + WC_Stripe::log("Successful charge: $response->id"); |
|
552 | 552 | } else { |
553 | - add_post_meta( $order->id, '_transaction_id', $response->id, true ); |
|
553 | + add_post_meta($order->id, '_transaction_id', $response->id, true); |
|
554 | 554 | |
555 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
555 | + if ($order->has_status(array('pending', 'failed'))) { |
|
556 | 556 | $order->reduce_order_stock(); |
557 | 557 | } |
558 | 558 | |
559 | - $order->update_status( 'on-hold', sprintf( __( 'Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe' ), $response->id ) ); |
|
560 | - WC_Stripe::log( "Successful auth: $response->id" ); |
|
559 | + $order->update_status('on-hold', sprintf(__('Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe'), $response->id)); |
|
560 | + WC_Stripe::log("Successful auth: $response->id"); |
|
561 | 561 | } |
562 | 562 | |
563 | 563 | return $response; |
@@ -569,21 +569,21 @@ discard block |
||
569 | 569 | * @since 3.0.0 |
570 | 570 | */ |
571 | 571 | public function add_payment_method() { |
572 | - if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
573 | - wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
572 | + if (empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
573 | + wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error'); |
|
574 | 574 | return; |
575 | 575 | } |
576 | 576 | |
577 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
578 | - $result = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) ); |
|
577 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
578 | + $result = $stripe_customer->add_card(wc_clean($_POST['stripe_token'])); |
|
579 | 579 | |
580 | - if ( is_wp_error( $result ) ) { |
|
581 | - throw new Exception( $result->get_error_message() ); |
|
580 | + if (is_wp_error($result)) { |
|
581 | + throw new Exception($result->get_error_message()); |
|
582 | 582 | } |
583 | 583 | |
584 | 584 | return array( |
585 | 585 | 'result' => 'success', |
586 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
586 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
587 | 587 | ); |
588 | 588 | } |
589 | 589 | |
@@ -593,36 +593,36 @@ discard block |
||
593 | 593 | * @param float $amount |
594 | 594 | * @return bool |
595 | 595 | */ |
596 | - public function process_refund( $order_id, $amount = null, $reason = '' ) { |
|
597 | - $order = wc_get_order( $order_id ); |
|
596 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
597 | + $order = wc_get_order($order_id); |
|
598 | 598 | |
599 | - if ( ! $order || ! $order->get_transaction_id() ) { |
|
599 | + if ( ! $order || ! $order->get_transaction_id()) { |
|
600 | 600 | return false; |
601 | 601 | } |
602 | 602 | |
603 | 603 | $body = array(); |
604 | 604 | |
605 | - if ( ! is_null( $amount ) ) { |
|
606 | - $body['amount'] = $this->get_stripe_amount( $amount ); |
|
605 | + if ( ! is_null($amount)) { |
|
606 | + $body['amount'] = $this->get_stripe_amount($amount); |
|
607 | 607 | } |
608 | 608 | |
609 | - if ( $reason ) { |
|
609 | + if ($reason) { |
|
610 | 610 | $body['metadata'] = array( |
611 | 611 | 'reason' => $reason, |
612 | 612 | ); |
613 | 613 | } |
614 | 614 | |
615 | - WC_Stripe::log( "Info: Beginning refund for order $order_id for the amount of {$amount}" ); |
|
615 | + WC_Stripe::log("Info: Beginning refund for order $order_id for the amount of {$amount}"); |
|
616 | 616 | |
617 | - $response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' ); |
|
617 | + $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); |
|
618 | 618 | |
619 | - if ( is_wp_error( $response ) ) { |
|
620 | - WC_Stripe::log( "Error: " . $response->get_error_message() ); |
|
619 | + if (is_wp_error($response)) { |
|
620 | + WC_Stripe::log("Error: " . $response->get_error_message()); |
|
621 | 621 | return $response; |
622 | - } elseif ( ! empty( $response->id ) ) { |
|
623 | - $refund_message = sprintf( __( 'Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason ); |
|
624 | - $order->add_order_note( $refund_message ); |
|
625 | - WC_Stripe::log( "Success: " . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
622 | + } elseif ( ! empty($response->id)) { |
|
623 | + $refund_message = sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason); |
|
624 | + $order->add_order_note($refund_message); |
|
625 | + WC_Stripe::log("Success: " . html_entity_decode(strip_tags($refund_message))); |
|
626 | 626 | return true; |
627 | 627 | } |
628 | 628 | } |