@@ -96,7 +96,6 @@ discard block |
||
| 96 | 96 | * process_subscription_payment function. |
| 97 | 97 | * @param mixed $order |
| 98 | 98 | * @param int $amount (default: 0) |
| 99 | - * @param string $stripe_token (default: '') |
|
| 100 | 99 | * @param bool initial_payment |
| 101 | 100 | */ |
| 102 | 101 | public function process_subscription_payment( $order = '', $amount = 0 ) { |
@@ -139,6 +138,8 @@ discard block |
||
| 139 | 138 | /** |
| 140 | 139 | * Process the pre-order |
| 141 | 140 | * @param int $order_id |
| 141 | + * @param boolean $retry |
|
| 142 | + * @param boolean $force_customer |
|
| 142 | 143 | * @return array |
| 143 | 144 | */ |
| 144 | 145 | public function process_pre_order( $order_id, $retry, $force_customer ) { |
@@ -239,7 +240,7 @@ discard block |
||
| 239 | 240 | |
| 240 | 241 | /** |
| 241 | 242 | * Don't transfer Stripe fee/ID meta to renewal orders. |
| 242 | - * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
|
| 243 | + * @param integer $renewal_order |
|
| 243 | 244 | */ |
| 244 | 245 | public function delete_renewal_meta( $renewal_order ) { |
| 245 | 246 | delete_post_meta( $renewal_order->id, 'Stripe Fee' ); |
@@ -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,38 +99,38 @@ 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 ) { |
|
| 103 | - if ( $amount * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 104 | - return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
| 102 | + public function process_subscription_payment($order = '', $amount = 0) { |
|
| 103 | + if ($amount * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 104 | + return new WP_Error('stripe_error', sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | // Get source from order |
| 108 | - $source = $this->get_order_source( $order ); |
|
| 108 | + $source = $this->get_order_source($order); |
|
| 109 | 109 | |
| 110 | 110 | // If no order source was defined, use user source instead. |
| 111 | - if ( ! $source->customer ) { |
|
| 112 | - $source = $this->get_source( $order->customer_user ); |
|
| 111 | + if ( ! $source->customer) { |
|
| 112 | + $source = $this->get_source($order->customer_user); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | // Or fail :( |
| 116 | - if ( ! $source->customer ) { |
|
| 117 | - return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
| 116 | + if ( ! $source->customer) { |
|
| 117 | + return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - $this->log( "Info: Begin processing subscription payment for order {$order->id} for the amount of {$amount}" ); |
|
| 120 | + $this->log("Info: Begin processing subscription payment for order {$order->id} for the amount of {$amount}"); |
|
| 121 | 121 | |
| 122 | 122 | // Make the request |
| 123 | - $request = $this->generate_payment_request( $order, $source ); |
|
| 123 | + $request = $this->generate_payment_request($order, $source); |
|
| 124 | 124 | $request['capture'] = 'true'; |
| 125 | - $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
| 125 | + $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
| 126 | 126 | $request['metadata'] = array( |
| 127 | 127 | 'payment_type' => 'recurring', |
| 128 | 128 | ); |
| 129 | - $response = WC_Stripe_API::request( $request ); |
|
| 129 | + $response = WC_Stripe_API::request($request); |
|
| 130 | 130 | |
| 131 | 131 | // Process valid response |
| 132 | - if ( ! is_wp_error( $response ) ) { |
|
| 133 | - $this->process_response( $response, $order ); |
|
| 132 | + if ( ! is_wp_error($response)) { |
|
| 133 | + $this->process_response($response, $order); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | return $response; |
@@ -141,42 +141,42 @@ discard block |
||
| 141 | 141 | * @param int $order_id |
| 142 | 142 | * @return array |
| 143 | 143 | */ |
| 144 | - public function process_pre_order( $order_id, $retry, $force_customer ) { |
|
| 145 | - if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { |
|
| 144 | + public function process_pre_order($order_id, $retry, $force_customer) { |
|
| 145 | + if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) { |
|
| 146 | 146 | try { |
| 147 | - $order = wc_get_order( $order_id ); |
|
| 147 | + $order = wc_get_order($order_id); |
|
| 148 | 148 | |
| 149 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 150 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
| 149 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 150 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $source = $this->get_source( get_current_user_id(), true ); |
|
| 153 | + $source = $this->get_source(get_current_user_id(), true); |
|
| 154 | 154 | |
| 155 | 155 | // We need a source on file to continue. |
| 156 | - if ( empty( $source->customer ) || empty( $source->source ) ) { |
|
| 157 | - throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
| 156 | + if (empty($source->customer) || empty($source->source)) { |
|
| 157 | + throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe')); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Store source to order meta |
| 161 | - $this->save_source( $order, $source ); |
|
| 161 | + $this->save_source($order, $source); |
|
| 162 | 162 | |
| 163 | 163 | // Remove cart |
| 164 | 164 | WC()->cart->empty_cart(); |
| 165 | 165 | |
| 166 | 166 | // Is pre ordered! |
| 167 | - WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
| 167 | + WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); |
|
| 168 | 168 | |
| 169 | 169 | // Return thank you page redirect |
| 170 | 170 | return array( |
| 171 | 171 | 'result' => 'success', |
| 172 | - 'redirect' => $this->get_return_url( $order ), |
|
| 172 | + 'redirect' => $this->get_return_url($order), |
|
| 173 | 173 | ); |
| 174 | - } catch ( Exception $e ) { |
|
| 175 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
| 174 | + } catch (Exception $e) { |
|
| 175 | + wc_add_notice($e->getMessage(), 'error'); |
|
| 176 | 176 | return; |
| 177 | 177 | } |
| 178 | 178 | } else { |
| 179 | - return parent::process_payment( $order_id, $retry, $force_customer ); |
|
| 179 | + return parent::process_payment($order_id, $retry, $force_customer); |
|
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | * @param WC_Order $order |
| 186 | 186 | * @return void |
| 187 | 187 | */ |
| 188 | - public function process_pre_order_release_payment( $order ) { |
|
| 188 | + public function process_pre_order_release_payment($order) { |
|
| 189 | 189 | try { |
| 190 | 190 | // Define some callbacks if the first attempt fails. |
| 191 | 191 | $retry_callbacks = array( |
@@ -193,32 +193,32 @@ discard block |
||
| 193 | 193 | 'remove_order_customer_before_retry', |
| 194 | 194 | ); |
| 195 | 195 | |
| 196 | - while ( 1 ) { |
|
| 197 | - $source = $this->get_order_source( $order ); |
|
| 198 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
| 196 | + while (1) { |
|
| 197 | + $source = $this->get_order_source($order); |
|
| 198 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
| 199 | 199 | |
| 200 | - if ( is_wp_error( $response ) ) { |
|
| 201 | - if ( 0 === sizeof( $retry_callbacks ) ) { |
|
| 202 | - throw new Exception( $response->get_error_message() ); |
|
| 200 | + if (is_wp_error($response)) { |
|
| 201 | + if (0 === sizeof($retry_callbacks)) { |
|
| 202 | + throw new Exception($response->get_error_message()); |
|
| 203 | 203 | } else { |
| 204 | - $retry_callback = array_shift( $retry_callbacks ); |
|
| 205 | - call_user_func( array( $this, $retry_callback ), $order ); |
|
| 204 | + $retry_callback = array_shift($retry_callbacks); |
|
| 205 | + call_user_func(array($this, $retry_callback), $order); |
|
| 206 | 206 | } |
| 207 | 207 | } else { |
| 208 | 208 | // Successful |
| 209 | - $this->process_response( $response, $order ); |
|
| 209 | + $this->process_response($response, $order); |
|
| 210 | 210 | break; |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | - } catch ( Exception $e ) { |
|
| 214 | - $order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() ); |
|
| 213 | + } catch (Exception $e) { |
|
| 214 | + $order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage()); |
|
| 215 | 215 | |
| 216 | 216 | // Mark order as failed if not already set, |
| 217 | 217 | // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
| 218 | - if ( ! $order->has_status( 'failed' ) ) { |
|
| 219 | - $order->update_status( 'failed', $order_note ); |
|
| 218 | + if ( ! $order->has_status('failed')) { |
|
| 219 | + $order->update_status('failed', $order_note); |
|
| 220 | 220 | } else { |
| 221 | - $order->add_order_note( $order_note ); |
|
| 221 | + $order->add_order_note($order_note); |
|
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | } |
@@ -227,20 +227,20 @@ discard block |
||
| 227 | 227 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
| 228 | 228 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
| 229 | 229 | */ |
| 230 | - public function delete_resubscribe_meta( $resubscribe_order ) { |
|
| 231 | - delete_post_meta( $resubscribe_order->id, '_stripe_customer_id' ); |
|
| 232 | - delete_post_meta( $resubscribe_order->id, '_stripe_card_id' ); |
|
| 233 | - $this->delete_renewal_meta( $resubscribe_order ); |
|
| 230 | + public function delete_resubscribe_meta($resubscribe_order) { |
|
| 231 | + delete_post_meta($resubscribe_order->id, '_stripe_customer_id'); |
|
| 232 | + delete_post_meta($resubscribe_order->id, '_stripe_card_id'); |
|
| 233 | + $this->delete_renewal_meta($resubscribe_order); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | 237 | * Don't transfer Stripe fee/ID meta to renewal orders. |
| 238 | 238 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
| 239 | 239 | */ |
| 240 | - public function delete_renewal_meta( $renewal_order ) { |
|
| 241 | - delete_post_meta( $renewal_order->id, 'Stripe Fee' ); |
|
| 242 | - delete_post_meta( $renewal_order->id, 'Net Revenue From Stripe' ); |
|
| 243 | - delete_post_meta( $renewal_order->id, 'Stripe Payment ID' ); |
|
| 240 | + public function delete_renewal_meta($renewal_order) { |
|
| 241 | + delete_post_meta($renewal_order->id, 'Stripe Fee'); |
|
| 242 | + delete_post_meta($renewal_order->id, 'Net Revenue From Stripe'); |
|
| 243 | + delete_post_meta($renewal_order->id, 'Stripe Payment ID'); |
|
| 244 | 244 | return $renewal_order; |
| 245 | 245 | } |
| 246 | 246 | |
@@ -250,11 +250,11 @@ discard block |
||
| 250 | 250 | * @param $amount_to_charge float The amount to charge. |
| 251 | 251 | * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
| 252 | 252 | */ |
| 253 | - public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
| 254 | - $response = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); |
|
| 253 | + public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
| 254 | + $response = $this->process_subscription_payment($renewal_order, $amount_to_charge); |
|
| 255 | 255 | |
| 256 | - if ( is_wp_error( $response ) ) { |
|
| 257 | - $renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
| 256 | + if (is_wp_error($response)) { |
|
| 257 | + $renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message())); |
|
| 258 | 258 | } |
| 259 | 259 | } |
| 260 | 260 | |
@@ -262,16 +262,16 @@ discard block |
||
| 262 | 262 | * Remove order meta |
| 263 | 263 | * @param object $order |
| 264 | 264 | */ |
| 265 | - public function remove_order_source_before_retry( $order ) { |
|
| 266 | - delete_post_meta( $order->id, '_stripe_card_id' ); |
|
| 265 | + public function remove_order_source_before_retry($order) { |
|
| 266 | + delete_post_meta($order->id, '_stripe_card_id'); |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | 270 | * Remove order meta |
| 271 | 271 | * @param object $order |
| 272 | 272 | */ |
| 273 | - public function remove_order_customer_before_retry( $order ) { |
|
| 274 | - delete_post_meta( $order->id, '_stripe_customer_id' ); |
|
| 273 | + public function remove_order_customer_before_retry($order) { |
|
| 274 | + delete_post_meta($order->id, '_stripe_customer_id'); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | /** |
@@ -283,9 +283,9 @@ discard block |
||
| 283 | 283 | * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
| 284 | 284 | * @return void |
| 285 | 285 | */ |
| 286 | - public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
| 287 | - update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
| 288 | - update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id ); |
|
| 286 | + public function update_failing_payment_method($subscription, $renewal_order) { |
|
| 287 | + update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
| 288 | + update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /** |
@@ -297,15 +297,15 @@ discard block |
||
| 297 | 297 | * @param WC_Subscription $subscription An instance of a subscription object |
| 298 | 298 | * @return array |
| 299 | 299 | */ |
| 300 | - public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
| 301 | - $payment_meta[ $this->id ] = array( |
|
| 300 | + public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
| 301 | + $payment_meta[$this->id] = array( |
|
| 302 | 302 | 'post_meta' => array( |
| 303 | 303 | '_stripe_customer_id' => array( |
| 304 | - 'value' => get_post_meta( $subscription->id, '_stripe_customer_id', true ), |
|
| 304 | + 'value' => get_post_meta($subscription->id, '_stripe_customer_id', true), |
|
| 305 | 305 | 'label' => 'Stripe Customer ID', |
| 306 | 306 | ), |
| 307 | 307 | '_stripe_card_id' => array( |
| 308 | - 'value' => get_post_meta( $subscription->id, '_stripe_card_id', true ), |
|
| 308 | + 'value' => get_post_meta($subscription->id, '_stripe_card_id', true), |
|
| 309 | 309 | 'label' => 'Stripe Card ID', |
| 310 | 310 | ), |
| 311 | 311 | ), |
@@ -322,17 +322,17 @@ discard block |
||
| 322 | 322 | * @param array $payment_meta associative array of meta data required for automatic payments |
| 323 | 323 | * @return array |
| 324 | 324 | */ |
| 325 | - public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
| 326 | - if ( $this->id === $payment_method_id ) { |
|
| 325 | + public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
| 326 | + if ($this->id === $payment_method_id) { |
|
| 327 | 327 | |
| 328 | - if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
| 329 | - throw new Exception( 'A "_stripe_customer_id" value is required.' ); |
|
| 330 | - } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
| 331 | - throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' ); |
|
| 328 | + if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
| 329 | + throw new Exception('A "_stripe_customer_id" value is required.'); |
|
| 330 | + } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
| 331 | + throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".'); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) { |
|
| 335 | - throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' ); |
|
| 334 | + if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) { |
|
| 335 | + throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".'); |
|
| 336 | 336 | } |
| 337 | 337 | } |
| 338 | 338 | } |
@@ -345,43 +345,43 @@ discard block |
||
| 345 | 345 | * @param WC_Subscription $subscription the subscription details |
| 346 | 346 | * @return string the subscription payment method |
| 347 | 347 | */ |
| 348 | - public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
| 348 | + public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
| 349 | 349 | // bail for other payment methods |
| 350 | - if ( $this->id !== $subscription->payment_method || ! $subscription->customer_user ) { |
|
| 350 | + if ($this->id !== $subscription->payment_method || ! $subscription->customer_user) { |
|
| 351 | 351 | return $payment_method_to_display; |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | $stripe_customer = new WC_Stripe_Customer(); |
| 355 | - $stripe_customer_id = get_post_meta( $subscription->id, '_stripe_customer_id', true ); |
|
| 356 | - $stripe_card_id = get_post_meta( $subscription->id, '_stripe_card_id', true ); |
|
| 355 | + $stripe_customer_id = get_post_meta($subscription->id, '_stripe_customer_id', true); |
|
| 356 | + $stripe_card_id = get_post_meta($subscription->id, '_stripe_card_id', true); |
|
| 357 | 357 | |
| 358 | 358 | // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
| 359 | - if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
| 359 | + if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
| 360 | 360 | $user_id = $subscription->customer_user; |
| 361 | - $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
| 362 | - $stripe_card_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
| 361 | + $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
| 362 | + $stripe_card_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
| 366 | - if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
| 367 | - $stripe_customer_id = get_post_meta( $subscription->order->id, '_stripe_customer_id', true ); |
|
| 368 | - $stripe_card_id = get_post_meta( $subscription->order->id, '_stripe_card_id', true ); |
|
| 366 | + if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
| 367 | + $stripe_customer_id = get_post_meta($subscription->order->id, '_stripe_customer_id', true); |
|
| 368 | + $stripe_card_id = get_post_meta($subscription->order->id, '_stripe_card_id', true); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
| 371 | + $stripe_customer->set_id($stripe_customer_id); |
|
| 372 | 372 | $cards = $stripe_customer->get_cards(); |
| 373 | 373 | |
| 374 | - if ( $cards ) { |
|
| 374 | + if ($cards) { |
|
| 375 | 375 | $found_card = false; |
| 376 | - foreach ( $cards as $card ) { |
|
| 377 | - if ( $card->id === $stripe_card_id ) { |
|
| 376 | + foreach ($cards as $card) { |
|
| 377 | + if ($card->id === $stripe_card_id) { |
|
| 378 | 378 | $found_card = true; |
| 379 | - $payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $card->type ) ? $card->type : $card->brand ), $card->last4 ); |
|
| 379 | + $payment_method_to_display = sprintf(__('Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe'), (isset($card->type) ? $card->type : $card->brand), $card->last4); |
|
| 380 | 380 | break; |
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | - if ( ! $found_card ) { |
|
| 384 | - $payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $cards[0]->type ) ? $cards[0]->type : $cards[0]->brand ), $cards[0]->last4 ); |
|
| 383 | + if ( ! $found_card) { |
|
| 384 | + $payment_method_to_display = sprintf(__('Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe'), (isset($cards[0]->type) ? $cards[0]->type : $cards[0]->brand), $cards[0]->last4); |
|
| 385 | 385 | } |
| 386 | 386 | } |
| 387 | 387 | |
@@ -396,11 +396,11 @@ discard block |
||
| 396 | 396 | * |
| 397 | 397 | * @param string $message |
| 398 | 398 | */ |
| 399 | - public function log( $message ) { |
|
| 400 | - $options = get_option( 'woocommerce_stripe_settings' ); |
|
| 399 | + public function log($message) { |
|
| 400 | + $options = get_option('woocommerce_stripe_settings'); |
|
| 401 | 401 | |
| 402 | - if ( 'yes' === $options['logging'] ) { |
|
| 403 | - WC_Stripe::log( $message ); |
|
| 402 | + if ('yes' === $options['logging']) { |
|
| 403 | + WC_Stripe::log($message); |
|
| 404 | 404 | } |
| 405 | 405 | } |
| 406 | 406 | } |
@@ -177,7 +177,7 @@ |
||
| 177 | 177 | * @param float $total Amount due. |
| 178 | 178 | * @param string $currency Accepted currency. |
| 179 | 179 | * |
| 180 | - * @return float|int |
|
| 180 | + * @return double |
|
| 181 | 181 | */ |
| 182 | 182 | public function get_stripe_amount( $total, $currency = '' ) { |
| 183 | 183 | if ( ! $currency ) { |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -113,8 +113,8 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function __construct() { |
| 115 | 115 | $this->id = 'stripe'; |
| 116 | - $this->method_title = __( 'Stripe', 'woocommerce-gateway-stripe' ); |
|
| 117 | - $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' ); |
|
| 116 | + $this->method_title = __('Stripe', 'woocommerce-gateway-stripe'); |
|
| 117 | + $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'); |
|
| 118 | 118 | $this->has_fields = true; |
| 119 | 119 | $this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s'; |
| 120 | 120 | $this->supports = array( |
@@ -142,39 +142,39 @@ discard block |
||
| 142 | 142 | $this->init_settings(); |
| 143 | 143 | |
| 144 | 144 | // Get setting values. |
| 145 | - $this->title = $this->get_option( 'title' ); |
|
| 146 | - $this->description = $this->get_option( 'description' ); |
|
| 147 | - $this->enabled = $this->get_option( 'enabled' ); |
|
| 148 | - $this->testmode = 'yes' === $this->get_option( 'testmode' ); |
|
| 149 | - $this->capture = 'yes' === $this->get_option( 'capture', 'yes' ); |
|
| 150 | - $this->statement_descriptor = $this->get_option( 'statement_descriptor', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); |
|
| 151 | - $this->stripe_checkout = 'yes' === $this->get_option( 'stripe_checkout' ); |
|
| 152 | - $this->stripe_checkout_locale = $this->get_option( 'stripe_checkout_locale' ); |
|
| 153 | - $this->stripe_checkout_image = $this->get_option( 'stripe_checkout_image', '' ); |
|
| 154 | - $this->saved_cards = 'yes' === $this->get_option( 'saved_cards' ); |
|
| 155 | - $this->secret_key = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' ); |
|
| 156 | - $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); |
|
| 157 | - $this->bitcoin = 'USD' === strtoupper( get_woocommerce_currency() ) && 'yes' === $this->get_option( 'stripe_bitcoin' ); |
|
| 158 | - $this->apple_pay = 'yes' === $this->get_option( 'apple_pay' ); |
|
| 159 | - $this->apple_pay_button = $this->get_option( 'apple_pay_button', 'black' ); |
|
| 160 | - $this->logging = 'yes' === $this->get_option( 'logging' ); |
|
| 161 | - $this->allow_remember_me = 'yes' === $this->get_option( 'allow_remember_me', 'no' ); |
|
| 162 | - |
|
| 163 | - if ( $this->stripe_checkout ) { |
|
| 164 | - $this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - if ( $this->testmode ) { |
|
| 168 | - $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' ); |
|
| 169 | - $this->description = trim( $this->description ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - WC_Stripe_API::set_secret_key( $this->secret_key ); |
|
| 145 | + $this->title = $this->get_option('title'); |
|
| 146 | + $this->description = $this->get_option('description'); |
|
| 147 | + $this->enabled = $this->get_option('enabled'); |
|
| 148 | + $this->testmode = 'yes' === $this->get_option('testmode'); |
|
| 149 | + $this->capture = 'yes' === $this->get_option('capture', 'yes'); |
|
| 150 | + $this->statement_descriptor = $this->get_option('statement_descriptor', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES)); |
|
| 151 | + $this->stripe_checkout = 'yes' === $this->get_option('stripe_checkout'); |
|
| 152 | + $this->stripe_checkout_locale = $this->get_option('stripe_checkout_locale'); |
|
| 153 | + $this->stripe_checkout_image = $this->get_option('stripe_checkout_image', ''); |
|
| 154 | + $this->saved_cards = 'yes' === $this->get_option('saved_cards'); |
|
| 155 | + $this->secret_key = $this->testmode ? $this->get_option('test_secret_key') : $this->get_option('secret_key'); |
|
| 156 | + $this->publishable_key = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key'); |
|
| 157 | + $this->bitcoin = 'USD' === strtoupper(get_woocommerce_currency()) && 'yes' === $this->get_option('stripe_bitcoin'); |
|
| 158 | + $this->apple_pay = 'yes' === $this->get_option('apple_pay'); |
|
| 159 | + $this->apple_pay_button = $this->get_option('apple_pay_button', 'black'); |
|
| 160 | + $this->logging = 'yes' === $this->get_option('logging'); |
|
| 161 | + $this->allow_remember_me = 'yes' === $this->get_option('allow_remember_me', 'no'); |
|
| 162 | + |
|
| 163 | + if ($this->stripe_checkout) { |
|
| 164 | + $this->order_button_text = __('Continue to payment', 'woocommerce-gateway-stripe'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + if ($this->testmode) { |
|
| 168 | + $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'); |
|
| 169 | + $this->description = trim($this->description); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + WC_Stripe_API::set_secret_key($this->secret_key); |
|
| 173 | 173 | |
| 174 | 174 | // Hooks. |
| 175 | - add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
|
| 176 | - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
| 177 | - add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
|
| 175 | + add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); |
|
| 176 | + add_action('admin_notices', array($this, 'admin_notices')); |
|
| 177 | + add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -184,24 +184,24 @@ discard block |
||
| 184 | 184 | * @return string |
| 185 | 185 | */ |
| 186 | 186 | public function get_icon() { |
| 187 | - $ext = version_compare( WC()->version, '2.6', '>=' ) ? '.svg' : '.png'; |
|
| 188 | - $style = version_compare( WC()->version, '2.6', '>=' ) ? 'style="margin-left: 0.3em"' : ''; |
|
| 187 | + $ext = version_compare(WC()->version, '2.6', '>=') ? '.svg' : '.png'; |
|
| 188 | + $style = version_compare(WC()->version, '2.6', '>=') ? 'style="margin-left: 0.3em"' : ''; |
|
| 189 | 189 | |
| 190 | - $icon = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext ) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
| 191 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext ) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
| 192 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext ) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
| 190 | + $icon = '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/visa' . $ext) . '" alt="Visa" width="32" ' . $style . ' />'; |
|
| 191 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard' . $ext) . '" alt="Mastercard" width="32" ' . $style . ' />'; |
|
| 192 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/amex' . $ext) . '" alt="Amex" width="32" ' . $style . ' />'; |
|
| 193 | 193 | |
| 194 | - if ( 'USD' === get_woocommerce_currency() ) { |
|
| 195 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext ) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
| 196 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext ) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
| 197 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext ) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
| 194 | + if ('USD' === get_woocommerce_currency()) { |
|
| 195 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/discover' . $ext) . '" alt="Discover" width="32" ' . $style . ' />'; |
|
| 196 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb' . $ext) . '" alt="JCB" width="32" ' . $style . ' />'; |
|
| 197 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(WC()->plugin_url() . '/assets/images/icons/credit-cards/diners' . $ext) . '" alt="Diners" width="32" ' . $style . ' />'; |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - if ( $this->bitcoin && $this->stripe_checkout ) { |
|
| 201 | - $icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE ) ) . '" alt="Bitcoin" width="24" ' . $style . ' />'; |
|
| 200 | + if ($this->bitcoin && $this->stripe_checkout) { |
|
| 201 | + $icon .= '<img src="' . WC_HTTPS::force_https_url(plugins_url('/assets/images/bitcoin' . $ext, WC_STRIPE_MAIN_FILE)) . '" alt="Bitcoin" width="24" ' . $style . ' />'; |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id ); |
|
| 204 | + return apply_filters('woocommerce_gateway_icon', $icon, $this->id); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -212,11 +212,11 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return float|int |
| 214 | 214 | */ |
| 215 | - public function get_stripe_amount( $total, $currency = '' ) { |
|
| 216 | - if ( ! $currency ) { |
|
| 215 | + public function get_stripe_amount($total, $currency = '') { |
|
| 216 | + if ( ! $currency) { |
|
| 217 | 217 | $currency = get_woocommerce_currency(); |
| 218 | 218 | } |
| 219 | - switch ( strtoupper( $currency ) ) { |
|
| 219 | + switch (strtoupper($currency)) { |
|
| 220 | 220 | // Zero decimal currencies. |
| 221 | 221 | case 'BIF' : |
| 222 | 222 | case 'CLP' : |
@@ -233,10 +233,10 @@ discard block |
||
| 233 | 233 | case 'XAF' : |
| 234 | 234 | case 'XOF' : |
| 235 | 235 | case 'XPF' : |
| 236 | - $total = absint( $total ); |
|
| 236 | + $total = absint($total); |
|
| 237 | 237 | break; |
| 238 | 238 | default : |
| 239 | - $total = round( $total, 2 ) * 100; // In cents. |
|
| 239 | + $total = round($total, 2) * 100; // In cents. |
|
| 240 | 240 | break; |
| 241 | 241 | } |
| 242 | 242 | return $total; |
@@ -246,13 +246,13 @@ discard block |
||
| 246 | 246 | * Check if SSL is enabled and notify the user |
| 247 | 247 | */ |
| 248 | 248 | public function admin_notices() { |
| 249 | - if ( 'no' === $this->enabled ) { |
|
| 249 | + if ('no' === $this->enabled) { |
|
| 250 | 250 | return; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected. |
| 254 | - if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) { |
|
| 255 | - 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>'; |
|
| 254 | + if ((function_exists('wc_site_is_https') && ! wc_site_is_https()) && ('no' === get_option('woocommerce_force_ssl_checkout') && ! class_exists('WordPressHTTPS'))) { |
|
| 255 | + 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>'; |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
@@ -260,11 +260,11 @@ discard block |
||
| 260 | 260 | * Check if this gateway is enabled |
| 261 | 261 | */ |
| 262 | 262 | public function is_available() { |
| 263 | - if ( 'yes' === $this->enabled ) { |
|
| 264 | - if ( ! $this->testmode && is_checkout() && ! is_ssl() ) { |
|
| 263 | + if ('yes' === $this->enabled) { |
|
| 264 | + if ( ! $this->testmode && is_checkout() && ! is_ssl()) { |
|
| 265 | 265 | return false; |
| 266 | 266 | } |
| 267 | - if ( ! $this->secret_key || ! $this->publishable_key ) { |
|
| 267 | + if ( ! $this->secret_key || ! $this->publishable_key) { |
|
| 268 | 268 | return false; |
| 269 | 269 | } |
| 270 | 270 | return true; |
@@ -276,9 +276,9 @@ discard block |
||
| 276 | 276 | * Initialise Gateway Settings Form Fields |
| 277 | 277 | */ |
| 278 | 278 | public function init_form_fields() { |
| 279 | - $this->form_fields = include( 'settings-stripe.php' ); |
|
| 279 | + $this->form_fields = include('settings-stripe.php'); |
|
| 280 | 280 | |
| 281 | - wc_enqueue_js( " |
|
| 281 | + wc_enqueue_js(" |
|
| 282 | 282 | jQuery( function( $ ) { |
| 283 | 283 | $( document.body ).on( 'change', '#woocommerce_stripe_testmode', function() { |
| 284 | 284 | var test_secret_key = $( '#woocommerce_stripe_test_secret_key' ).parents( 'tr' ).eq( 0 ), |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | var value = $( this ).val(); |
| 324 | 324 | |
| 325 | 325 | if ( value.indexOf( '_test_' ) >= 0 ) { |
| 326 | - $( 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>' ); |
|
| 326 | + $( 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>' ); |
|
| 327 | 327 | } else { |
| 328 | 328 | $( this ).css( 'border-color', '' ); |
| 329 | 329 | $( '.stripe-error-description', $( this ).parent() ).remove(); |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | var value = $( this ).val(); |
| 335 | 335 | |
| 336 | 336 | if ( value.indexOf( '_live_' ) >= 0 ) { |
| 337 | - $( 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>' ); |
|
| 337 | + $( 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>' ); |
|
| 338 | 338 | } else { |
| 339 | 339 | $( this ).css( 'border-color', '' ); |
| 340 | 340 | $( '.stripe-error-description', $( this ).parent() ).remove(); |
@@ -349,54 +349,54 @@ discard block |
||
| 349 | 349 | */ |
| 350 | 350 | public function payment_fields() { |
| 351 | 351 | $user = wp_get_current_user(); |
| 352 | - $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; |
|
| 352 | + $display_tokenization = $this->supports('tokenization') && is_checkout() && $this->saved_cards; |
|
| 353 | 353 | $total = WC()->cart->total; |
| 354 | 354 | |
| 355 | 355 | // If paying from order, we need to get total from order not cart. |
| 356 | - if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) ) { |
|
| 357 | - $order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) ); |
|
| 356 | + if (isset($_GET['pay_for_order']) && isset($_GET['key'])) { |
|
| 357 | + $order = wc_get_order(wc_get_order_id_by_order_key(wc_clean($_GET['key']))); |
|
| 358 | 358 | $total = $order->get_total(); |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | - if ( $user->ID ) { |
|
| 362 | - $user_email = get_user_meta( $user->ID, 'billing_email', true ); |
|
| 361 | + if ($user->ID) { |
|
| 362 | + $user_email = get_user_meta($user->ID, 'billing_email', true); |
|
| 363 | 363 | $user_email = $user_email ? $user_email : $user->user_email; |
| 364 | 364 | } else { |
| 365 | 365 | $user_email = ''; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if ( is_add_payment_method_page() ) { |
|
| 369 | - $pay_button_text = __( 'Add Card', 'woocommerce-gateway-stripe' ); |
|
| 368 | + if (is_add_payment_method_page()) { |
|
| 369 | + $pay_button_text = __('Add Card', 'woocommerce-gateway-stripe'); |
|
| 370 | 370 | } else { |
| 371 | 371 | $pay_button_text = ''; |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | echo '<div |
| 375 | 375 | id="stripe-payment-data" |
| 376 | - data-panel-label="' . esc_attr( $pay_button_text ) . '" |
|
| 376 | + data-panel-label="' . esc_attr($pay_button_text) . '" |
|
| 377 | 377 | data-description="" |
| 378 | - data-email="' . esc_attr( $user_email ) . '" |
|
| 379 | - data-amount="' . esc_attr( $this->get_stripe_amount( $total ) ) . '" |
|
| 380 | - data-name="' . esc_attr( $this->statement_descriptor ) . '" |
|
| 381 | - data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '" |
|
| 382 | - data-image="' . esc_attr( $this->stripe_checkout_image ) . '" |
|
| 383 | - data-bitcoin="' . esc_attr( $this->bitcoin ? 'true' : 'false' ) . '" |
|
| 384 | - data-locale="' . esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ) . '" |
|
| 385 | - data-allow-remember-me="' . esc_attr( $this->allow_remember_me ? 'true' : 'false' ) . '">'; |
|
| 378 | + data-email="' . esc_attr($user_email) . '" |
|
| 379 | + data-amount="' . esc_attr($this->get_stripe_amount($total)) . '" |
|
| 380 | + data-name="' . esc_attr($this->statement_descriptor) . '" |
|
| 381 | + data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '" |
|
| 382 | + data-image="' . esc_attr($this->stripe_checkout_image) . '" |
|
| 383 | + data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '" |
|
| 384 | + data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '" |
|
| 385 | + data-allow-remember-me="' . esc_attr($this->allow_remember_me ? 'true' : 'false') . '">'; |
|
| 386 | 386 | |
| 387 | - if ( $this->description ) { |
|
| 388 | - echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) ); |
|
| 387 | + if ($this->description) { |
|
| 388 | + echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description))); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | - if ( $display_tokenization ) { |
|
| 391 | + if ($display_tokenization) { |
|
| 392 | 392 | $this->tokenization_script(); |
| 393 | 393 | $this->saved_payment_methods(); |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | - if ( ! $this->stripe_checkout ) { |
|
| 396 | + if ( ! $this->stripe_checkout) { |
|
| 397 | 397 | $this->form(); |
| 398 | 398 | |
| 399 | - if ( $display_tokenization ) { |
|
| 399 | + if ($display_tokenization) { |
|
| 400 | 400 | $this->save_payment_method_checkbox(); |
| 401 | 401 | } |
| 402 | 402 | } |
@@ -412,20 +412,20 @@ discard block |
||
| 412 | 412 | * @return array |
| 413 | 413 | */ |
| 414 | 414 | public function get_localized_messages() { |
| 415 | - return apply_filters( 'wc_stripe_localized_messages', array( |
|
| 416 | - 'invalid_number' => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ), |
|
| 417 | - 'invalid_expiry_month' => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ), |
|
| 418 | - 'invalid_expiry_year' => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ), |
|
| 419 | - 'invalid_cvc' => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ), |
|
| 420 | - 'incorrect_number' => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ), |
|
| 421 | - 'expired_card' => __( 'The card has expired.', 'woocommerce-gateway-stripe' ), |
|
| 422 | - 'incorrect_cvc' => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ), |
|
| 423 | - 'incorrect_zip' => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ), |
|
| 424 | - 'card_declined' => __( 'The card was declined.', 'woocommerce-gateway-stripe' ), |
|
| 425 | - 'missing' => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ), |
|
| 426 | - 'processing_error' => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ), |
|
| 427 | - 'invalid_request_error' => __( 'Could not find payment information.', 'woocommerce-gateway-stripe' ), |
|
| 428 | - ) ); |
|
| 415 | + return apply_filters('wc_stripe_localized_messages', array( |
|
| 416 | + 'invalid_number' => __('The card number is not a valid credit card number.', 'woocommerce-gateway-stripe'), |
|
| 417 | + 'invalid_expiry_month' => __('The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe'), |
|
| 418 | + 'invalid_expiry_year' => __('The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe'), |
|
| 419 | + 'invalid_cvc' => __('The card\'s security code is invalid.', 'woocommerce-gateway-stripe'), |
|
| 420 | + 'incorrect_number' => __('The card number is incorrect.', 'woocommerce-gateway-stripe'), |
|
| 421 | + 'expired_card' => __('The card has expired.', 'woocommerce-gateway-stripe'), |
|
| 422 | + 'incorrect_cvc' => __('The card\'s security code is incorrect.', 'woocommerce-gateway-stripe'), |
|
| 423 | + 'incorrect_zip' => __('The card\'s zip code failed validation.', 'woocommerce-gateway-stripe'), |
|
| 424 | + 'card_declined' => __('The card was declined.', 'woocommerce-gateway-stripe'), |
|
| 425 | + 'missing' => __('There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe'), |
|
| 426 | + 'processing_error' => __('An error occurred while processing the card.', 'woocommerce-gateway-stripe'), |
|
| 427 | + 'invalid_request_error' => __('Could not find payment information.', 'woocommerce-gateway-stripe'), |
|
| 428 | + )); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** |
@@ -436,30 +436,30 @@ discard block |
||
| 436 | 436 | * @access public |
| 437 | 437 | */ |
| 438 | 438 | public function payment_scripts() { |
| 439 | - if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
|
| 439 | + if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) { |
|
| 440 | 440 | return; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 443 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
| 444 | 444 | |
| 445 | - if ( $this->stripe_checkout ) { |
|
| 446 | - wp_enqueue_script( 'stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true ); |
|
| 447 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe_checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
| 445 | + if ($this->stripe_checkout) { |
|
| 446 | + wp_enqueue_script('stripe_checkout', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true); |
|
| 447 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe_checkout' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
| 448 | 448 | } else { |
| 449 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
| 450 | - wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery-payment', 'stripe' ), WC_STRIPE_VERSION, true ); |
|
| 449 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
| 450 | + wp_enqueue_script('woocommerce_stripe', plugins_url('assets/js/stripe' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery-payment', 'stripe'), WC_STRIPE_VERSION, true); |
|
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | $stripe_params = array( |
| 454 | 454 | 'key' => $this->publishable_key, |
| 455 | - 'i18n_terms' => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ), |
|
| 456 | - 'i18n_required_fields' => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ), |
|
| 455 | + 'i18n_terms' => __('Please accept the terms and conditions first', 'woocommerce-gateway-stripe'), |
|
| 456 | + 'i18n_required_fields' => __('Please fill in required checkout fields first', 'woocommerce-gateway-stripe'), |
|
| 457 | 457 | ); |
| 458 | 458 | |
| 459 | 459 | // If we're on the pay page we need to pass stripe.js the address of the order. |
| 460 | - if ( isset( $_GET['pay_for_order'] ) && 'true' === $_GET['pay_for_order'] ) { |
|
| 461 | - $order_id = wc_get_order_id_by_order_key( urldecode( $_GET['key'] ) ); |
|
| 462 | - $order = wc_get_order( $order_id ); |
|
| 460 | + if (isset($_GET['pay_for_order']) && 'true' === $_GET['pay_for_order']) { |
|
| 461 | + $order_id = wc_get_order_id_by_order_key(urldecode($_GET['key'])); |
|
| 462 | + $order = wc_get_order($order_id); |
|
| 463 | 463 | |
| 464 | 464 | $stripe_params['billing_first_name'] = $order->billing_first_name; |
| 465 | 465 | $stripe_params['billing_last_name'] = $order->billing_last_name; |
@@ -471,14 +471,14 @@ discard block |
||
| 471 | 471 | $stripe_params['billing_country'] = $order->billing_country; |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - $stripe_params['no_prepaid_card_msg'] = __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ); |
|
| 475 | - $stripe_params['allow_prepaid_card'] = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no'; |
|
| 476 | - $stripe_params['stripe_checkout_require_billing_address'] = apply_filters( 'wc_stripe_checkout_require_billing_address', false ) ? 'yes' : 'no'; |
|
| 474 | + $stripe_params['no_prepaid_card_msg'] = __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe'); |
|
| 475 | + $stripe_params['allow_prepaid_card'] = apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no'; |
|
| 476 | + $stripe_params['stripe_checkout_require_billing_address'] = apply_filters('wc_stripe_checkout_require_billing_address', false) ? 'yes' : 'no'; |
|
| 477 | 477 | |
| 478 | 478 | // merge localized messages to be use in JS |
| 479 | - $stripe_params = array_merge( $stripe_params, $this->get_localized_messages() ); |
|
| 479 | + $stripe_params = array_merge($stripe_params, $this->get_localized_messages()); |
|
| 480 | 480 | |
| 481 | - wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) ); |
|
| 481 | + wp_localize_script('woocommerce_stripe', 'wc_stripe_params', apply_filters('wc_stripe_params', $stripe_params)); |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | /** |
@@ -487,31 +487,31 @@ discard block |
||
| 487 | 487 | * @param object $source |
| 488 | 488 | * @return array() |
| 489 | 489 | */ |
| 490 | - protected function generate_payment_request( $order, $source ) { |
|
| 490 | + protected function generate_payment_request($order, $source) { |
|
| 491 | 491 | $post_data = array(); |
| 492 | - $post_data['currency'] = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() ); |
|
| 493 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
| 494 | - $post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() ); |
|
| 492 | + $post_data['currency'] = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency()); |
|
| 493 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
| 494 | + $post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number()); |
|
| 495 | 495 | $post_data['capture'] = $this->capture ? 'true' : 'false'; |
| 496 | 496 | |
| 497 | - if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
| 497 | + if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
| 498 | 498 | $post_data['receipt_email'] = $order->billing_email; |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | - $post_data['expand[]'] = 'balance_transaction'; |
|
| 501 | + $post_data['expand[]'] = 'balance_transaction'; |
|
| 502 | 502 | |
| 503 | 503 | $metadata = array( |
| 504 | - __( 'Customer Name', 'woocommerce-gateway-stripe' ) => sanitize_text_field( $order->billing_first_name ) . ' ' . sanitize_text_field( $order->billing_last_name ), |
|
| 505 | - __( 'Customer Email', 'woocommerce-gateway-stripe' ) => sanitize_email( $order->billing_email ), |
|
| 504 | + __('Customer Name', 'woocommerce-gateway-stripe') => sanitize_text_field($order->billing_first_name) . ' ' . sanitize_text_field($order->billing_last_name), |
|
| 505 | + __('Customer Email', 'woocommerce-gateway-stripe') => sanitize_email($order->billing_email), |
|
| 506 | 506 | ); |
| 507 | 507 | |
| 508 | - $post_data['metadata'] = apply_filters( 'wc_stripe_payment_metadata', $metadata, $order, $source ); |
|
| 508 | + $post_data['metadata'] = apply_filters('wc_stripe_payment_metadata', $metadata, $order, $source); |
|
| 509 | 509 | |
| 510 | - if ( $source->customer ) { |
|
| 510 | + if ($source->customer) { |
|
| 511 | 511 | $post_data['customer'] = $source->customer; |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | - if ( $source->source ) { |
|
| 514 | + if ($source->source) { |
|
| 515 | 515 | $post_data['source'] = $source->source; |
| 516 | 516 | } |
| 517 | 517 | |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | * @param WC_Order $order |
| 524 | 524 | * @param object $source |
| 525 | 525 | */ |
| 526 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source ); |
|
| 526 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source); |
|
| 527 | 527 | } |
| 528 | 528 | |
| 529 | 529 | /** |
@@ -535,37 +535,37 @@ discard block |
||
| 535 | 535 | * @throws Exception When card was not added or for and invalid card. |
| 536 | 536 | * @return object |
| 537 | 537 | */ |
| 538 | - protected function get_source( $user_id, $force_customer = false ) { |
|
| 539 | - $stripe_customer = new WC_Stripe_Customer( $user_id ); |
|
| 538 | + protected function get_source($user_id, $force_customer = false) { |
|
| 539 | + $stripe_customer = new WC_Stripe_Customer($user_id); |
|
| 540 | 540 | $stripe_source = false; |
| 541 | 541 | $token_id = false; |
| 542 | 542 | |
| 543 | 543 | // New CC info was entered and we have a new token to process |
| 544 | - if ( isset( $_POST['stripe_token'] ) ) { |
|
| 545 | - $stripe_token = wc_clean( $_POST['stripe_token'] ); |
|
| 546 | - $maybe_saved_card = isset( $_POST['wc-stripe-new-payment-method'] ) && ! empty( $_POST['wc-stripe-new-payment-method'] ); |
|
| 544 | + if (isset($_POST['stripe_token'])) { |
|
| 545 | + $stripe_token = wc_clean($_POST['stripe_token']); |
|
| 546 | + $maybe_saved_card = isset($_POST['wc-stripe-new-payment-method']) && ! empty($_POST['wc-stripe-new-payment-method']); |
|
| 547 | 547 | |
| 548 | 548 | // This is true if the user wants to store the card to their account. |
| 549 | - if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_customer ) { |
|
| 550 | - $stripe_source = $stripe_customer->add_card( $stripe_token ); |
|
| 549 | + if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_customer) { |
|
| 550 | + $stripe_source = $stripe_customer->add_card($stripe_token); |
|
| 551 | 551 | |
| 552 | - if ( is_wp_error( $stripe_source ) ) { |
|
| 553 | - throw new Exception( $stripe_source->get_error_message() ); |
|
| 552 | + if (is_wp_error($stripe_source)) { |
|
| 553 | + throw new Exception($stripe_source->get_error_message()); |
|
| 554 | 554 | } |
| 555 | 555 | } else { |
| 556 | 556 | // Not saving token, so don't define customer either. |
| 557 | 557 | $stripe_source = $stripe_token; |
| 558 | 558 | $stripe_customer = false; |
| 559 | 559 | } |
| 560 | - } elseif ( isset( $_POST['wc-stripe-payment-token'] ) && 'new' !== $_POST['wc-stripe-payment-token'] ) { |
|
| 560 | + } elseif (isset($_POST['wc-stripe-payment-token']) && 'new' !== $_POST['wc-stripe-payment-token']) { |
|
| 561 | 561 | // Use an existing token, and then process the payment |
| 562 | 562 | |
| 563 | - $token_id = wc_clean( $_POST['wc-stripe-payment-token'] ); |
|
| 564 | - $token = WC_Payment_Tokens::get( $token_id ); |
|
| 563 | + $token_id = wc_clean($_POST['wc-stripe-payment-token']); |
|
| 564 | + $token = WC_Payment_Tokens::get($token_id); |
|
| 565 | 565 | |
| 566 | - if ( ! $token || $token->get_user_id() !== get_current_user_id() ) { |
|
| 567 | - WC()->session->set( 'refresh_totals', true ); |
|
| 568 | - throw new Exception( __( 'Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe' ) ); |
|
| 566 | + if ( ! $token || $token->get_user_id() !== get_current_user_id()) { |
|
| 567 | + WC()->session->set('refresh_totals', true); |
|
| 568 | + throw new Exception(__('Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe')); |
|
| 569 | 569 | } |
| 570 | 570 | |
| 571 | 571 | $stripe_source = $token->get_token(); |
@@ -589,16 +589,16 @@ discard block |
||
| 589 | 589 | * @param object $order |
| 590 | 590 | * @return object |
| 591 | 591 | */ |
| 592 | - protected function get_order_source( $order = null ) { |
|
| 592 | + protected function get_order_source($order = null) { |
|
| 593 | 593 | $stripe_customer = new WC_Stripe_Customer(); |
| 594 | 594 | $stripe_source = false; |
| 595 | 595 | $token_id = false; |
| 596 | 596 | |
| 597 | - if ( $order ) { |
|
| 598 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_customer_id', true ) ) { |
|
| 599 | - $stripe_customer->set_id( $meta_value ); |
|
| 597 | + if ($order) { |
|
| 598 | + if ($meta_value = get_post_meta($order->id, '_stripe_customer_id', true)) { |
|
| 599 | + $stripe_customer->set_id($meta_value); |
|
| 600 | 600 | } |
| 601 | - if ( $meta_value = get_post_meta( $order->id, '_stripe_card_id', true ) ) { |
|
| 601 | + if ($meta_value = get_post_meta($order->id, '_stripe_card_id', true)) { |
|
| 602 | 602 | $stripe_source = $meta_value; |
| 603 | 603 | } |
| 604 | 604 | } |
@@ -621,57 +621,57 @@ discard block |
||
| 621 | 621 | * |
| 622 | 622 | * @return array|void |
| 623 | 623 | */ |
| 624 | - public function process_payment( $order_id, $retry = true, $force_customer = false ) { |
|
| 624 | + public function process_payment($order_id, $retry = true, $force_customer = false) { |
|
| 625 | 625 | try { |
| 626 | - $order = wc_get_order( $order_id ); |
|
| 627 | - $source = $this->get_source( get_current_user_id(), $force_customer ); |
|
| 626 | + $order = wc_get_order($order_id); |
|
| 627 | + $source = $this->get_source(get_current_user_id(), $force_customer); |
|
| 628 | 628 | |
| 629 | - if ( empty( $source->source ) && empty( $source->customer ) ) { |
|
| 630 | - $error_msg = __( 'Please enter your card details to make a payment.', 'woocommerce-gateway-stripe' ); |
|
| 631 | - $error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' ); |
|
| 632 | - throw new Exception( $error_msg ); |
|
| 629 | + if (empty($source->source) && empty($source->customer)) { |
|
| 630 | + $error_msg = __('Please enter your card details to make a payment.', 'woocommerce-gateway-stripe'); |
|
| 631 | + $error_msg .= ' ' . __('Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe'); |
|
| 632 | + throw new Exception($error_msg); |
|
| 633 | 633 | } |
| 634 | 634 | |
| 635 | 635 | // Store source to order meta. |
| 636 | - $this->save_source( $order, $source ); |
|
| 636 | + $this->save_source($order, $source); |
|
| 637 | 637 | |
| 638 | 638 | // Handle payment. |
| 639 | - if ( $order->get_total() > 0 ) { |
|
| 639 | + if ($order->get_total() > 0) { |
|
| 640 | 640 | |
| 641 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 642 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
| 641 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 642 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
| 643 | 643 | } |
| 644 | 644 | |
| 645 | - $this->log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
| 645 | + $this->log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
| 646 | 646 | |
| 647 | 647 | // Make the request. |
| 648 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
| 648 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
| 649 | 649 | |
| 650 | - if ( is_wp_error( $response ) ) { |
|
| 650 | + if (is_wp_error($response)) { |
|
| 651 | 651 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
| 652 | - if ( 'customer' === $response->get_error_code() && $retry ) { |
|
| 653 | - delete_user_meta( get_current_user_id(), '_stripe_customer_id' ); |
|
| 654 | - return $this->process_payment( $order_id, false, $force_customer ); |
|
| 652 | + if ('customer' === $response->get_error_code() && $retry) { |
|
| 653 | + delete_user_meta(get_current_user_id(), '_stripe_customer_id'); |
|
| 654 | + return $this->process_payment($order_id, false, $force_customer); |
|
| 655 | 655 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
| 656 | - } elseif ( 'source' === $response->get_error_code() && $source->token_id ) { |
|
| 657 | - $token = WC_Payment_Tokens::get( $source->token_id ); |
|
| 656 | + } elseif ('source' === $response->get_error_code() && $source->token_id) { |
|
| 657 | + $token = WC_Payment_Tokens::get($source->token_id); |
|
| 658 | 658 | $token->delete(); |
| 659 | - $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
| 660 | - $order->add_order_note( $message ); |
|
| 661 | - throw new Exception( $message ); |
|
| 659 | + $message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe'); |
|
| 660 | + $order->add_order_note($message); |
|
| 661 | + throw new Exception($message); |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | $localized_messages = $this->get_localized_messages(); |
| 665 | 665 | |
| 666 | - $message = isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message(); |
|
| 666 | + $message = isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message(); |
|
| 667 | 667 | |
| 668 | - $order->add_order_note( $message ); |
|
| 668 | + $order->add_order_note($message); |
|
| 669 | 669 | |
| 670 | - throw new Exception( $message ); |
|
| 670 | + throw new Exception($message); |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | // Process valid response. |
| 674 | - $this->process_response( $response, $order ); |
|
| 674 | + $this->process_response($response, $order); |
|
| 675 | 675 | } else { |
| 676 | 676 | $order->payment_complete(); |
| 677 | 677 | } |
@@ -679,23 +679,23 @@ discard block |
||
| 679 | 679 | // Remove cart. |
| 680 | 680 | WC()->cart->empty_cart(); |
| 681 | 681 | |
| 682 | - do_action( 'wc_gateway_stripe_process_payment', $response, $order ); |
|
| 682 | + do_action('wc_gateway_stripe_process_payment', $response, $order); |
|
| 683 | 683 | |
| 684 | 684 | // Return thank you page redirect. |
| 685 | 685 | return array( |
| 686 | 686 | 'result' => 'success', |
| 687 | - 'redirect' => $this->get_return_url( $order ), |
|
| 687 | + 'redirect' => $this->get_return_url($order), |
|
| 688 | 688 | ); |
| 689 | 689 | |
| 690 | - } catch ( Exception $e ) { |
|
| 691 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
| 692 | - $this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
| 690 | + } catch (Exception $e) { |
|
| 691 | + wc_add_notice($e->getMessage(), 'error'); |
|
| 692 | + $this->log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
| 693 | 693 | |
| 694 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 695 | - $this->send_failed_order_email( $order_id ); |
|
| 694 | + if ($order->has_status(array('pending', 'failed'))) { |
|
| 695 | + $this->send_failed_order_email($order_id); |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | - do_action( 'wc_gateway_stripe_process_payment_error', $e, $order ); |
|
| 698 | + do_action('wc_gateway_stripe_process_payment_error', $e, $order); |
|
| 699 | 699 | |
| 700 | 700 | return array( |
| 701 | 701 | 'result' => 'fail', |
@@ -710,52 +710,52 @@ discard block |
||
| 710 | 710 | * @param WC_Order $order For to which the source applies. |
| 711 | 711 | * @param stdClass $source Source information. |
| 712 | 712 | */ |
| 713 | - protected function save_source( $order, $source ) { |
|
| 713 | + protected function save_source($order, $source) { |
|
| 714 | 714 | // Store source in the order. |
| 715 | - if ( $source->customer ) { |
|
| 716 | - update_post_meta( $order->id, '_stripe_customer_id', $source->customer ); |
|
| 715 | + if ($source->customer) { |
|
| 716 | + update_post_meta($order->id, '_stripe_customer_id', $source->customer); |
|
| 717 | 717 | } |
| 718 | - if ( $source->source ) { |
|
| 719 | - update_post_meta( $order->id, '_stripe_card_id', $source->source ); |
|
| 718 | + if ($source->source) { |
|
| 719 | + update_post_meta($order->id, '_stripe_card_id', $source->source); |
|
| 720 | 720 | } |
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | /** |
| 724 | 724 | * Store extra meta data for an order from a Stripe Response. |
| 725 | 725 | */ |
| 726 | - public function process_response( $response, $order ) { |
|
| 727 | - $this->log( 'Processing response: ' . print_r( $response, true ) ); |
|
| 726 | + public function process_response($response, $order) { |
|
| 727 | + $this->log('Processing response: ' . print_r($response, true)); |
|
| 728 | 728 | |
| 729 | 729 | // Store charge data |
| 730 | - update_post_meta( $order->id, '_stripe_charge_id', $response->id ); |
|
| 731 | - update_post_meta( $order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' ); |
|
| 730 | + update_post_meta($order->id, '_stripe_charge_id', $response->id); |
|
| 731 | + update_post_meta($order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no'); |
|
| 732 | 732 | |
| 733 | 733 | // Store other data such as fees |
| 734 | - if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) { |
|
| 734 | + if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) { |
|
| 735 | 735 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
| 736 | 736 | // values are in the local currency of the Stripe account, not from WC. |
| 737 | - $fee = ! empty( $response->balance_transaction->fee ) ? number_format( $response->balance_transaction->fee / 100, 2, '.', '' ) : 0; |
|
| 738 | - $net = ! empty( $response->balance_transaction->net ) ? number_format( $response->balance_transaction->net / 100, 2, '.', '' ) : 0; |
|
| 739 | - update_post_meta( $order->id, 'Stripe Fee', $fee ); |
|
| 740 | - update_post_meta( $order->id, 'Net Revenue From Stripe', $net ); |
|
| 737 | + $fee = ! empty($response->balance_transaction->fee) ? number_format($response->balance_transaction->fee / 100, 2, '.', '') : 0; |
|
| 738 | + $net = ! empty($response->balance_transaction->net) ? number_format($response->balance_transaction->net / 100, 2, '.', '') : 0; |
|
| 739 | + update_post_meta($order->id, 'Stripe Fee', $fee); |
|
| 740 | + update_post_meta($order->id, 'Net Revenue From Stripe', $net); |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | - if ( $response->captured ) { |
|
| 744 | - $order->payment_complete( $response->id ); |
|
| 743 | + if ($response->captured) { |
|
| 744 | + $order->payment_complete($response->id); |
|
| 745 | 745 | |
| 746 | - $message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id ); |
|
| 747 | - $order->add_order_note( $message ); |
|
| 748 | - $this->log( 'Success: ' . $message ); |
|
| 746 | + $message = sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $response->id); |
|
| 747 | + $order->add_order_note($message); |
|
| 748 | + $this->log('Success: ' . $message); |
|
| 749 | 749 | |
| 750 | 750 | } else { |
| 751 | - add_post_meta( $order->id, '_transaction_id', $response->id, true ); |
|
| 751 | + add_post_meta($order->id, '_transaction_id', $response->id, true); |
|
| 752 | 752 | |
| 753 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 753 | + if ($order->has_status(array('pending', 'failed'))) { |
|
| 754 | 754 | $order->reduce_order_stock(); |
| 755 | 755 | } |
| 756 | 756 | |
| 757 | - $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 ) ); |
|
| 758 | - $this->log( "Successful auth: $response->id" ); |
|
| 757 | + $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)); |
|
| 758 | + $this->log("Successful auth: $response->id"); |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | return $response; |
@@ -767,32 +767,32 @@ discard block |
||
| 767 | 767 | * @since 3.0.0 |
| 768 | 768 | */ |
| 769 | 769 | public function add_payment_method() { |
| 770 | - if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
| 771 | - wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
| 770 | + if (empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
| 771 | + wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error'); |
|
| 772 | 772 | return; |
| 773 | 773 | } |
| 774 | 774 | |
| 775 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 776 | - $card = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) ); |
|
| 775 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 776 | + $card = $stripe_customer->add_card(wc_clean($_POST['stripe_token'])); |
|
| 777 | 777 | |
| 778 | - if ( is_wp_error( $card ) ) { |
|
| 778 | + if (is_wp_error($card)) { |
|
| 779 | 779 | $localized_messages = $this->get_localized_messages(); |
| 780 | - $error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ); |
|
| 780 | + $error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe'); |
|
| 781 | 781 | |
| 782 | 782 | // loop through the errors to find matching localized message |
| 783 | - foreach ( $card->errors as $error => $msg ) { |
|
| 784 | - if ( isset( $localized_messages[ $error ] ) ) { |
|
| 785 | - $error_msg = $localized_messages[ $error ]; |
|
| 783 | + foreach ($card->errors as $error => $msg) { |
|
| 784 | + if (isset($localized_messages[$error])) { |
|
| 785 | + $error_msg = $localized_messages[$error]; |
|
| 786 | 786 | } |
| 787 | 787 | } |
| 788 | 788 | |
| 789 | - wc_add_notice( $error_msg, 'error' ); |
|
| 789 | + wc_add_notice($error_msg, 'error'); |
|
| 790 | 790 | return; |
| 791 | 791 | } |
| 792 | 792 | |
| 793 | 793 | return array( |
| 794 | 794 | 'result' => 'success', |
| 795 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
| 795 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
| 796 | 796 | ); |
| 797 | 797 | } |
| 798 | 798 | |
@@ -802,36 +802,36 @@ discard block |
||
| 802 | 802 | * @param float $amount |
| 803 | 803 | * @return bool |
| 804 | 804 | */ |
| 805 | - public function process_refund( $order_id, $amount = null, $reason = '' ) { |
|
| 806 | - $order = wc_get_order( $order_id ); |
|
| 805 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
| 806 | + $order = wc_get_order($order_id); |
|
| 807 | 807 | |
| 808 | - if ( ! $order || ! $order->get_transaction_id() ) { |
|
| 808 | + if ( ! $order || ! $order->get_transaction_id()) { |
|
| 809 | 809 | return false; |
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | $body = array(); |
| 813 | 813 | |
| 814 | - if ( ! is_null( $amount ) ) { |
|
| 815 | - $body['amount'] = $this->get_stripe_amount( $amount ); |
|
| 814 | + if ( ! is_null($amount)) { |
|
| 815 | + $body['amount'] = $this->get_stripe_amount($amount); |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | - if ( $reason ) { |
|
| 818 | + if ($reason) { |
|
| 819 | 819 | $body['metadata'] = array( |
| 820 | 820 | 'reason' => $reason, |
| 821 | 821 | ); |
| 822 | 822 | } |
| 823 | 823 | |
| 824 | - $this->log( "Info: Beginning refund for order $order_id for the amount of {$amount}" ); |
|
| 824 | + $this->log("Info: Beginning refund for order $order_id for the amount of {$amount}"); |
|
| 825 | 825 | |
| 826 | - $response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' ); |
|
| 826 | + $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); |
|
| 827 | 827 | |
| 828 | - if ( is_wp_error( $response ) ) { |
|
| 829 | - $this->log( 'Error: ' . $response->get_error_message() ); |
|
| 828 | + if (is_wp_error($response)) { |
|
| 829 | + $this->log('Error: ' . $response->get_error_message()); |
|
| 830 | 830 | return $response; |
| 831 | - } elseif ( ! empty( $response->id ) ) { |
|
| 832 | - $refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason ); |
|
| 833 | - $order->add_order_note( $refund_message ); |
|
| 834 | - $this->log( 'Success: ' . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
| 831 | + } elseif ( ! empty($response->id)) { |
|
| 832 | + $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason); |
|
| 833 | + $order->add_order_note($refund_message); |
|
| 834 | + $this->log('Success: ' . html_entity_decode(strip_tags($refund_message))); |
|
| 835 | 835 | return true; |
| 836 | 836 | } |
| 837 | 837 | } |
@@ -844,10 +844,10 @@ discard block |
||
| 844 | 844 | * @param int $order_id |
| 845 | 845 | * @return null |
| 846 | 846 | */ |
| 847 | - public function send_failed_order_email( $order_id ) { |
|
| 847 | + public function send_failed_order_email($order_id) { |
|
| 848 | 848 | $emails = WC()->mailer()->get_emails(); |
| 849 | - if ( ! empty( $emails ) && ! empty( $order_id ) ) { |
|
| 850 | - $emails['WC_Email_Failed_Order']->trigger( $order_id ); |
|
| 849 | + if ( ! empty($emails) && ! empty($order_id)) { |
|
| 850 | + $emails['WC_Email_Failed_Order']->trigger($order_id); |
|
| 851 | 851 | } |
| 852 | 852 | } |
| 853 | 853 | |
@@ -859,9 +859,9 @@ discard block |
||
| 859 | 859 | * |
| 860 | 860 | * @param string $message |
| 861 | 861 | */ |
| 862 | - public function log( $message ) { |
|
| 863 | - if ( $this->logging ) { |
|
| 864 | - WC_Stripe::log( $message ); |
|
| 862 | + public function log($message) { |
|
| 863 | + if ($this->logging) { |
|
| 864 | + WC_Stripe::log($message); |
|
| 865 | 865 | } |
| 866 | 866 | } |
| 867 | 867 | } |
@@ -23,7 +23,6 @@ |
||
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * Set secret API Key. |
| 26 | - * @param string $key |
|
| 27 | 26 | */ |
| 28 | 27 | public static function set_secret_key( $secret_key ) { |
| 29 | 28 | self::$secret_key = $secret_key; |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * Set secret API Key. |
| 26 | 26 | * @param string $key |
| 27 | 27 | */ |
| 28 | - public static function set_secret_key( $secret_key ) { |
|
| 28 | + public static function set_secret_key($secret_key) { |
|
| 29 | 29 | self::$secret_key = $secret_key; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -34,11 +34,11 @@ discard block |
||
| 34 | 34 | * @return string |
| 35 | 35 | */ |
| 36 | 36 | public static function get_secret_key() { |
| 37 | - if ( ! self::$secret_key ) { |
|
| 38 | - $options = get_option( 'woocommerce_stripe_settings' ); |
|
| 37 | + if ( ! self::$secret_key) { |
|
| 38 | + $options = get_option('woocommerce_stripe_settings'); |
|
| 39 | 39 | |
| 40 | - if ( isset( $options['testmode'], $options['secret_key'], $options['test_secret_key'] ) ) { |
|
| 41 | - self::set_secret_key( 'yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key'] ); |
|
| 40 | + if (isset($options['testmode'], $options['secret_key'], $options['test_secret_key'])) { |
|
| 41 | + self::set_secret_key('yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key']); |
|
| 42 | 42 | } |
| 43 | 43 | } |
| 44 | 44 | return self::$secret_key; |
@@ -51,39 +51,39 @@ discard block |
||
| 51 | 51 | * @param string $api |
| 52 | 52 | * @return array|WP_Error |
| 53 | 53 | */ |
| 54 | - public static function request( $request, $api = 'charges', $method = 'POST' ) { |
|
| 55 | - self::log( "{$api} request: " . print_r( $request, true ) ); |
|
| 54 | + public static function request($request, $api = 'charges', $method = 'POST') { |
|
| 55 | + self::log("{$api} request: " . print_r($request, true)); |
|
| 56 | 56 | |
| 57 | 57 | $response = wp_safe_remote_post( |
| 58 | 58 | self::ENDPOINT . $api, |
| 59 | 59 | array( |
| 60 | 60 | 'method' => $method, |
| 61 | 61 | 'headers' => array( |
| 62 | - 'Authorization' => 'Basic ' . base64_encode( self::get_secret_key() . ':' ), |
|
| 62 | + 'Authorization' => 'Basic ' . base64_encode(self::get_secret_key() . ':'), |
|
| 63 | 63 | 'Stripe-Version' => '2016-03-07', |
| 64 | 64 | ), |
| 65 | - 'body' => apply_filters( 'woocommerce_stripe_request_body', $request, $api ), |
|
| 65 | + 'body' => apply_filters('woocommerce_stripe_request_body', $request, $api), |
|
| 66 | 66 | 'timeout' => 70, |
| 67 | 67 | 'user-agent' => 'WooCommerce ' . WC()->version, |
| 68 | 68 | ) |
| 69 | 69 | ); |
| 70 | 70 | |
| 71 | - if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
|
| 72 | - self::log( 'Error Response: ' . print_r( $response, true ) ); |
|
| 73 | - return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the payment gateway.', 'woocommerce-gateway-stripe' ) ); |
|
| 71 | + if (is_wp_error($response) || empty($response['body'])) { |
|
| 72 | + self::log('Error Response: ' . print_r($response, true)); |
|
| 73 | + return new WP_Error('stripe_error', __('There was a problem connecting to the payment gateway.', 'woocommerce-gateway-stripe')); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - $parsed_response = json_decode( $response['body'] ); |
|
| 76 | + $parsed_response = json_decode($response['body']); |
|
| 77 | 77 | // Handle response |
| 78 | - if ( ! empty( $parsed_response->error ) ) { |
|
| 79 | - if ( ! empty( $parsed_response->error->param ) ) { |
|
| 78 | + if ( ! empty($parsed_response->error)) { |
|
| 79 | + if ( ! empty($parsed_response->error->param)) { |
|
| 80 | 80 | $code = $parsed_response->error->param; |
| 81 | - } elseif ( ! empty( $parsed_response->error->code ) ) { |
|
| 81 | + } elseif ( ! empty($parsed_response->error->code)) { |
|
| 82 | 82 | $code = $parsed_response->error->code; |
| 83 | 83 | } else { |
| 84 | 84 | $code = 'stripe_error'; |
| 85 | 85 | } |
| 86 | - return new WP_Error( $code, $parsed_response->error->message ); |
|
| 86 | + return new WP_Error($code, $parsed_response->error->message); |
|
| 87 | 87 | } else { |
| 88 | 88 | return $parsed_response; |
| 89 | 89 | } |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @param string $message |
| 99 | 99 | */ |
| 100 | - public static function log( $message ) { |
|
| 101 | - $options = get_option( 'woocommerce_stripe_settings' ); |
|
| 100 | + public static function log($message) { |
|
| 101 | + $options = get_option('woocommerce_stripe_settings'); |
|
| 102 | 102 | |
| 103 | - if ( 'yes' === $options['logging'] ) { |
|
| 104 | - WC_Stripe::log( $message ); |
|
| 103 | + if ('yes' === $options['logging']) { |
|
| 104 | + WC_Stripe::log($message); |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -210,7 +210,6 @@ |
||
| 210 | 210 | /** |
| 211 | 211 | * Get a customers saved cards using their Stripe ID. Cached. |
| 212 | 212 | * |
| 213 | - * @param string $customer_id |
|
| 214 | 213 | * @return array |
| 215 | 214 | */ |
| 216 | 215 | public function get_cards() { |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | * Constructor |
| 33 | 33 | * @param integer $user_id |
| 34 | 34 | */ |
| 35 | - public function __construct( $user_id = 0 ) { |
|
| 36 | - if ( $user_id ) { |
|
| 37 | - $this->set_user_id( $user_id ); |
|
| 38 | - $this->set_id( get_user_meta( $user_id, '_stripe_customer_id', true ) ); |
|
| 35 | + public function __construct($user_id = 0) { |
|
| 36 | + if ($user_id) { |
|
| 37 | + $this->set_user_id($user_id); |
|
| 38 | + $this->set_id(get_user_meta($user_id, '_stripe_customer_id', true)); |
|
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | * Set Stripe customer ID. |
| 52 | 52 | * @param [type] $id [description] |
| 53 | 53 | */ |
| 54 | - public function set_id( $id ) { |
|
| 55 | - $this->id = wc_clean( $id ); |
|
| 54 | + public function set_id($id) { |
|
| 55 | + $this->id = wc_clean($id); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -60,15 +60,15 @@ discard block |
||
| 60 | 60 | * @return int |
| 61 | 61 | */ |
| 62 | 62 | public function get_user_id() { |
| 63 | - return absint( $this->user_id ); |
|
| 63 | + return absint($this->user_id); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Set User ID used by WordPress. |
| 68 | 68 | * @param int $user_id |
| 69 | 69 | */ |
| 70 | - public function set_user_id( $user_id ) { |
|
| 71 | - $this->user_id = absint( $user_id ); |
|
| 70 | + public function set_user_id($user_id) { |
|
| 71 | + $this->user_id = absint($user_id); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | * @return WP_User |
| 77 | 77 | */ |
| 78 | 78 | protected function get_user() { |
| 79 | - return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false; |
|
| 79 | + return $this->get_user_id() ? get_user_by('id', $this->get_user_id()) : false; |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Store data from the Stripe API about this customer |
| 84 | 84 | */ |
| 85 | - public function set_customer_data( $data ) { |
|
| 85 | + public function set_customer_data($data) { |
|
| 86 | 86 | $this->customer_data = $data; |
| 87 | 87 | } |
| 88 | 88 | |
@@ -90,12 +90,12 @@ discard block |
||
| 90 | 90 | * Get data from the Stripe API about this customer |
| 91 | 91 | */ |
| 92 | 92 | public function get_customer_data() { |
| 93 | - if ( empty( $this->customer_data ) && $this->get_id() && false === ( $this->customer_data = get_transient( 'stripe_customer_' . $this->get_id() ) ) ) { |
|
| 94 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() ); |
|
| 93 | + if (empty($this->customer_data) && $this->get_id() && false === ($this->customer_data = get_transient('stripe_customer_' . $this->get_id()))) { |
|
| 94 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id()); |
|
| 95 | 95 | |
| 96 | - if ( ! is_wp_error( $response ) ) { |
|
| 97 | - $this->set_customer_data( $response ); |
|
| 98 | - set_transient( 'stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48 ); |
|
| 96 | + if ( ! is_wp_error($response)) { |
|
| 97 | + $this->set_customer_data($response); |
|
| 98 | + set_transient('stripe_customer_' . $this->get_id(), $response, HOUR_IN_SECONDS * 48); |
|
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | return $this->customer_data; |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | $data = $this->get_customer_data(); |
| 110 | 110 | $source = ''; |
| 111 | 111 | |
| 112 | - if ( $data ) { |
|
| 112 | + if ($data) { |
|
| 113 | 113 | $source = $data->default_source; |
| 114 | 114 | } |
| 115 | 115 | |
@@ -121,10 +121,10 @@ discard block |
||
| 121 | 121 | * @param array $args |
| 122 | 122 | * @return WP_Error|int |
| 123 | 123 | */ |
| 124 | - public function create_customer( $args = array() ) { |
|
| 125 | - if ( $user = $this->get_user() ) { |
|
| 126 | - $billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true ); |
|
| 127 | - $billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true ); |
|
| 124 | + public function create_customer($args = array()) { |
|
| 125 | + if ($user = $this->get_user()) { |
|
| 126 | + $billing_first_name = get_user_meta($user->ID, 'billing_first_name', true); |
|
| 127 | + $billing_last_name = get_user_meta($user->ID, 'billing_last_name', true); |
|
| 128 | 128 | |
| 129 | 129 | $defaults = array( |
| 130 | 130 | 'email' => $user->user_email, |
@@ -139,26 +139,26 @@ discard block |
||
| 139 | 139 | |
| 140 | 140 | $metadata = array(); |
| 141 | 141 | |
| 142 | - $defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user ); |
|
| 142 | + $defaults['metadata'] = apply_filters('wc_stripe_customer_metadata', $metadata, $user); |
|
| 143 | 143 | |
| 144 | - $args = wp_parse_args( $args, $defaults ); |
|
| 145 | - $response = WC_Stripe_API::request( $args, 'customers' ); |
|
| 144 | + $args = wp_parse_args($args, $defaults); |
|
| 145 | + $response = WC_Stripe_API::request($args, 'customers'); |
|
| 146 | 146 | |
| 147 | - if ( is_wp_error( $response ) ) { |
|
| 147 | + if (is_wp_error($response)) { |
|
| 148 | 148 | return $response; |
| 149 | - } elseif ( empty( $response->id ) ) { |
|
| 150 | - return new WP_Error( 'stripe_error', __( 'Could not create Stripe customer.', 'woocommerce-gateway-stripe' ) ); |
|
| 149 | + } elseif (empty($response->id)) { |
|
| 150 | + return new WP_Error('stripe_error', __('Could not create Stripe customer.', 'woocommerce-gateway-stripe')); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $this->set_id( $response->id ); |
|
| 153 | + $this->set_id($response->id); |
|
| 154 | 154 | $this->clear_cache(); |
| 155 | - $this->set_customer_data( $response ); |
|
| 155 | + $this->set_customer_data($response); |
|
| 156 | 156 | |
| 157 | - if ( $this->get_user_id() ) { |
|
| 158 | - update_user_meta( $this->get_user_id(), '_stripe_customer_id', $response->id ); |
|
| 157 | + if ($this->get_user_id()) { |
|
| 158 | + update_user_meta($this->get_user_id(), '_stripe_customer_id', $response->id); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
| 161 | + do_action('woocommerce_stripe_add_customer', $args, $response); |
|
| 162 | 162 | |
| 163 | 163 | return $response->id; |
| 164 | 164 | } |
@@ -169,44 +169,44 @@ discard block |
||
| 169 | 169 | * @param bool $retry |
| 170 | 170 | * @return WP_Error|int |
| 171 | 171 | */ |
| 172 | - public function add_card( $token, $retry = true ) { |
|
| 173 | - if ( ! $this->get_id() ) { |
|
| 174 | - if ( ( $response = $this->create_customer() ) && is_wp_error( $response ) ) { |
|
| 172 | + public function add_card($token, $retry = true) { |
|
| 173 | + if ( ! $this->get_id()) { |
|
| 174 | + if (($response = $this->create_customer()) && is_wp_error($response)) { |
|
| 175 | 175 | return $response; |
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - $response = WC_Stripe_API::request( array( |
|
| 179 | + $response = WC_Stripe_API::request(array( |
|
| 180 | 180 | 'source' => $token, |
| 181 | - ), 'customers/' . $this->get_id() . '/sources' ); |
|
| 181 | + ), 'customers/' . $this->get_id() . '/sources'); |
|
| 182 | 182 | |
| 183 | - if ( is_wp_error( $response ) ) { |
|
| 184 | - if ( 'customer' === $response->get_error_code() && $retry ) { |
|
| 183 | + if (is_wp_error($response)) { |
|
| 184 | + if ('customer' === $response->get_error_code() && $retry) { |
|
| 185 | 185 | $this->create_customer(); |
| 186 | - return $this->add_card( $token, false ); |
|
| 186 | + return $this->add_card($token, false); |
|
| 187 | 187 | } else { |
| 188 | 188 | return $response; |
| 189 | 189 | } |
| 190 | - } elseif ( empty( $response->id ) ) { |
|
| 191 | - return new WP_Error( 'error', __( 'Unable to add card', 'woocommerce-gateway-stripe' ) ); |
|
| 190 | + } elseif (empty($response->id)) { |
|
| 191 | + return new WP_Error('error', __('Unable to add card', 'woocommerce-gateway-stripe')); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // Add token to WooCommerce |
| 195 | - if ( $this->get_user_id() && class_exists( 'WC_Payment_Token_CC' ) ) { |
|
| 195 | + if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) { |
|
| 196 | 196 | $token = new WC_Payment_Token_CC(); |
| 197 | - $token->set_token( $response->id ); |
|
| 198 | - $token->set_gateway_id( 'stripe' ); |
|
| 199 | - $token->set_card_type( strtolower( $response->brand ) ); |
|
| 200 | - $token->set_last4( $response->last4 ); |
|
| 201 | - $token->set_expiry_month( $response->exp_month ); |
|
| 202 | - $token->set_expiry_year( $response->exp_year ); |
|
| 203 | - $token->set_user_id( $this->get_user_id() ); |
|
| 197 | + $token->set_token($response->id); |
|
| 198 | + $token->set_gateway_id('stripe'); |
|
| 199 | + $token->set_card_type(strtolower($response->brand)); |
|
| 200 | + $token->set_last4($response->last4); |
|
| 201 | + $token->set_expiry_month($response->exp_month); |
|
| 202 | + $token->set_expiry_year($response->exp_year); |
|
| 203 | + $token->set_user_id($this->get_user_id()); |
|
| 204 | 204 | $token->save(); |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | $this->clear_cache(); |
| 208 | 208 | |
| 209 | - do_action( 'woocommerce_stripe_add_card', $this->get_id(), $token, $response ); |
|
| 209 | + do_action('woocommerce_stripe_add_card', $this->get_id(), $token, $response); |
|
| 210 | 210 | |
| 211 | 211 | return $response->id; |
| 212 | 212 | } |
@@ -220,20 +220,20 @@ discard block |
||
| 220 | 220 | public function get_cards() { |
| 221 | 221 | $cards = array(); |
| 222 | 222 | |
| 223 | - if ( $this->get_id() && false === ( $cards = get_transient( 'stripe_cards_' . $this->get_id() ) ) ) { |
|
| 224 | - $response = WC_Stripe_API::request( array( |
|
| 223 | + if ($this->get_id() && false === ($cards = get_transient('stripe_cards_' . $this->get_id()))) { |
|
| 224 | + $response = WC_Stripe_API::request(array( |
|
| 225 | 225 | 'limit' => 100, |
| 226 | - ), 'customers/' . $this->get_id() . '/sources', 'GET' ); |
|
| 226 | + ), 'customers/' . $this->get_id() . '/sources', 'GET'); |
|
| 227 | 227 | |
| 228 | - if ( is_wp_error( $response ) ) { |
|
| 228 | + if (is_wp_error($response)) { |
|
| 229 | 229 | return array(); |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - if ( is_array( $response->data ) ) { |
|
| 232 | + if (is_array($response->data)) { |
|
| 233 | 233 | $cards = $response->data; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - set_transient( 'stripe_cards_' . $this->get_id(), $cards, HOUR_IN_SECONDS * 48 ); |
|
| 236 | + set_transient('stripe_cards_' . $this->get_id(), $cards, HOUR_IN_SECONDS * 48); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | return $cards; |
@@ -243,13 +243,13 @@ discard block |
||
| 243 | 243 | * Delete a card from stripe. |
| 244 | 244 | * @param string $card_id |
| 245 | 245 | */ |
| 246 | - public function delete_card( $card_id ) { |
|
| 247 | - $response = WC_Stripe_API::request( array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field( $card_id ), 'DELETE' ); |
|
| 246 | + public function delete_card($card_id) { |
|
| 247 | + $response = WC_Stripe_API::request(array(), 'customers/' . $this->get_id() . '/sources/' . sanitize_text_field($card_id), 'DELETE'); |
|
| 248 | 248 | |
| 249 | 249 | $this->clear_cache(); |
| 250 | 250 | |
| 251 | - if ( ! is_wp_error( $response ) ) { |
|
| 252 | - do_action( 'wc_stripe_delete_card', $this->get_id(), $response ); |
|
| 251 | + if ( ! is_wp_error($response)) { |
|
| 252 | + do_action('wc_stripe_delete_card', $this->get_id(), $response); |
|
| 253 | 253 | |
| 254 | 254 | return true; |
| 255 | 255 | } |
@@ -261,15 +261,15 @@ discard block |
||
| 261 | 261 | * Set default card in Stripe |
| 262 | 262 | * @param string $card_id |
| 263 | 263 | */ |
| 264 | - public function set_default_card( $card_id ) { |
|
| 265 | - $response = WC_Stripe_API::request( array( |
|
| 266 | - 'default_source' => sanitize_text_field( $card_id ), |
|
| 267 | - ), 'customers/' . $this->get_id(), 'POST' ); |
|
| 264 | + public function set_default_card($card_id) { |
|
| 265 | + $response = WC_Stripe_API::request(array( |
|
| 266 | + 'default_source' => sanitize_text_field($card_id), |
|
| 267 | + ), 'customers/' . $this->get_id(), 'POST'); |
|
| 268 | 268 | |
| 269 | 269 | $this->clear_cache(); |
| 270 | 270 | |
| 271 | - if ( ! is_wp_error( $response ) ) { |
|
| 272 | - do_action( 'wc_stripe_set_default_card', $this->get_id(), $response ); |
|
| 271 | + if ( ! is_wp_error($response)) { |
|
| 272 | + do_action('wc_stripe_set_default_card', $this->get_id(), $response); |
|
| 273 | 273 | |
| 274 | 274 | return true; |
| 275 | 275 | } |
@@ -281,8 +281,8 @@ discard block |
||
| 281 | 281 | * Deletes caches for this users cards. |
| 282 | 282 | */ |
| 283 | 283 | public function clear_cache() { |
| 284 | - delete_transient( 'stripe_cards_' . $this->get_id() ); |
|
| 285 | - delete_transient( 'stripe_customer_' . $this->get_id() ); |
|
| 284 | + delete_transient('stripe_cards_' . $this->get_id()); |
|
| 285 | + delete_transient('stripe_customer_' . $this->get_id()); |
|
| 286 | 286 | $this->customer_data = array(); |
| 287 | 287 | } |
| 288 | 288 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -12,25 +12,25 @@ discard block |
||
| 12 | 12 | * Constructor |
| 13 | 13 | */ |
| 14 | 14 | public function __construct() { |
| 15 | - add_action( 'wp', array( $this, 'delete_card' ) ); |
|
| 16 | - add_action( 'woocommerce_after_my_account', array( $this, 'output' ) ); |
|
| 17 | - add_action( 'wp', array( $this, 'default_card' ) ); |
|
| 15 | + add_action('wp', array($this, 'delete_card')); |
|
| 16 | + add_action('woocommerce_after_my_account', array($this, 'output')); |
|
| 17 | + add_action('wp', array($this, 'default_card')); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Display saved cards |
| 22 | 22 | */ |
| 23 | 23 | public function output() { |
| 24 | - if ( ! is_user_logged_in() ) { |
|
| 24 | + if ( ! is_user_logged_in()) { |
|
| 25 | 25 | return; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 28 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 29 | 29 | $stripe_cards = $stripe_customer->get_cards(); |
| 30 | 30 | $default_card = $stripe_customer->get_default_card(); |
| 31 | 31 | |
| 32 | - if ( $stripe_cards ) { |
|
| 33 | - wc_get_template( 'saved-cards.php', array( 'cards' => $stripe_cards, 'default_card' => $default_card ), 'woocommerce-gateway-stripe/', untrailingslashit( plugin_dir_path( WC_STRIPE_MAIN_FILE ) ) . '/includes/legacy/templates/' ); |
|
| 32 | + if ($stripe_cards) { |
|
| 33 | + wc_get_template('saved-cards.php', array('cards' => $stripe_cards, 'default_card' => $default_card), 'woocommerce-gateway-stripe/', untrailingslashit(plugin_dir_path(WC_STRIPE_MAIN_FILE)) . '/includes/legacy/templates/'); |
|
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
@@ -38,22 +38,22 @@ discard block |
||
| 38 | 38 | * Delete a card |
| 39 | 39 | */ |
| 40 | 40 | public function delete_card() { |
| 41 | - if ( ! isset( $_POST['stripe_delete_card'] ) || ! is_account_page() ) { |
|
| 41 | + if ( ! isset($_POST['stripe_delete_card']) || ! is_account_page()) { |
|
| 42 | 42 | return; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 45 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 46 | 46 | $stripe_customer_id = $stripe_customer->get_id(); |
| 47 | - $delete_card = sanitize_text_field( $_POST['stripe_delete_card'] ); |
|
| 47 | + $delete_card = sanitize_text_field($_POST['stripe_delete_card']); |
|
| 48 | 48 | |
| 49 | - if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_del_card" ) ) { |
|
| 50 | - wp_die( __( 'Unable to make default card, please try again', 'woocommerce-gateway-stripe' ) ); |
|
| 49 | + if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce($_POST['_wpnonce'], "stripe_del_card")) { |
|
| 50 | + wp_die(__('Unable to make default card, please try again', 'woocommerce-gateway-stripe')); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if ( ! $stripe_customer->delete_card( $delete_card ) ) { |
|
| 54 | - wc_add_notice( __( 'Unable to delete card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
| 53 | + if ( ! $stripe_customer->delete_card($delete_card)) { |
|
| 54 | + wc_add_notice(__('Unable to delete card.', 'woocommerce-gateway-stripe'), 'error'); |
|
| 55 | 55 | } else { |
| 56 | - wc_add_notice( __( 'Card deleted.', 'woocommerce-gateway-stripe' ), 'success' ); |
|
| 56 | + wc_add_notice(__('Card deleted.', 'woocommerce-gateway-stripe'), 'success'); |
|
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
@@ -61,22 +61,22 @@ discard block |
||
| 61 | 61 | * Make a card as default method |
| 62 | 62 | */ |
| 63 | 63 | public function default_card() { |
| 64 | - if ( ! isset( $_POST['stripe_default_card'] ) || ! is_account_page() ) { |
|
| 64 | + if ( ! isset($_POST['stripe_default_card']) || ! is_account_page()) { |
|
| 65 | 65 | return; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 68 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 69 | 69 | $stripe_customer_id = $stripe_customer->get_id(); |
| 70 | - $default_source = sanitize_text_field( $_POST['stripe_default_card'] ); |
|
| 70 | + $default_source = sanitize_text_field($_POST['stripe_default_card']); |
|
| 71 | 71 | |
| 72 | - if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce( $_POST['_wpnonce'], "stripe_default_card" ) ) { |
|
| 73 | - wp_die( __( 'Unable to make default card, please try again', 'woocommerce-gateway-stripe' ) ); |
|
| 72 | + if ( ! is_user_logged_in() || ! $stripe_customer_id || ! wp_verify_nonce($_POST['_wpnonce'], "stripe_default_card")) { |
|
| 73 | + wp_die(__('Unable to make default card, please try again', 'woocommerce-gateway-stripe')); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - if ( ! $stripe_customer->set_default_card( $default_source ) ) { |
|
| 77 | - wc_add_notice( __( 'Unable to update default card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
| 76 | + if ( ! $stripe_customer->set_default_card($default_source)) { |
|
| 77 | + wc_add_notice(__('Unable to update default card.', 'woocommerce-gateway-stripe'), 'error'); |
|
| 78 | 78 | } else { |
| 79 | - wc_add_notice( __( 'Default card updated.', 'woocommerce-gateway-stripe' ), 'success' ); |
|
| 79 | + wc_add_notice(__('Default card updated.', 'woocommerce-gateway-stripe'), 'success'); |
|
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -1,37 +1,37 @@ |
||
| 1 | -<h2 id="saved-cards" style="margin-top:40px;"><?php _e( 'Saved cards', 'woocommerce-gateway-stripe' ); ?></h2> |
|
| 1 | +<h2 id="saved-cards" style="margin-top:40px;"><?php _e('Saved cards', 'woocommerce-gateway-stripe'); ?></h2> |
|
| 2 | 2 | <table class="shop_table"> |
| 3 | 3 | <thead> |
| 4 | 4 | <tr> |
| 5 | - <th><?php esc_html_e( 'Card', 'woocommerce-gateway-stripe' ); ?></th> |
|
| 6 | - <th><?php esc_html_e( 'Expires', 'woocommerce-gateway-stripe' ); ?></th> |
|
| 5 | + <th><?php esc_html_e('Card', 'woocommerce-gateway-stripe'); ?></th> |
|
| 6 | + <th><?php esc_html_e('Expires', 'woocommerce-gateway-stripe'); ?></th> |
|
| 7 | 7 | <th></th> |
| 8 | 8 | </tr> |
| 9 | 9 | </thead> |
| 10 | 10 | <tbody> |
| 11 | - <?php foreach ( $cards as $card ) : |
|
| 12 | - if ( 'card' !== $card->object ) { |
|
| 11 | + <?php foreach ($cards as $card) : |
|
| 12 | + if ('card' !== $card->object) { |
|
| 13 | 13 | continue; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | $is_default_card = $card->id === $default_card ? true : false; |
| 17 | 17 | ?> |
| 18 | 18 | <tr> |
| 19 | - <td><?php printf( __( '%s card ending in %s', 'woocommerce-gateway-stripe' ), $card->brand, $card->last4 ); ?> |
|
| 20 | - <?php if ( $is_default_card ) echo '<br />' . __( '(Default)', 'woocommerce-gateway-stripe' ); ?> |
|
| 19 | + <td><?php printf(__('%s card ending in %s', 'woocommerce-gateway-stripe'), $card->brand, $card->last4); ?> |
|
| 20 | + <?php if ($is_default_card) echo '<br />' . __('(Default)', 'woocommerce-gateway-stripe'); ?> |
|
| 21 | 21 | </td> |
| 22 | - <td><?php printf( __( 'Expires %s/%s', 'woocommerce-gateway-stripe' ), $card->exp_month, $card->exp_year ); ?></td> |
|
| 22 | + <td><?php printf(__('Expires %s/%s', 'woocommerce-gateway-stripe'), $card->exp_month, $card->exp_year); ?></td> |
|
| 23 | 23 | <td> |
| 24 | 24 | <form action="" method="POST"> |
| 25 | - <?php wp_nonce_field ( 'stripe_del_card' ); ?> |
|
| 26 | - <input type="hidden" name="stripe_delete_card" value="<?php echo esc_attr( $card->id ); ?>"> |
|
| 27 | - <input type="submit" class="button" value="<?php esc_attr_e( 'Delete card', 'woocommerce-gateway-stripe' ); ?>"> |
|
| 25 | + <?php wp_nonce_field('stripe_del_card'); ?> |
|
| 26 | + <input type="hidden" name="stripe_delete_card" value="<?php echo esc_attr($card->id); ?>"> |
|
| 27 | + <input type="submit" class="button" value="<?php esc_attr_e('Delete card', 'woocommerce-gateway-stripe'); ?>"> |
|
| 28 | 28 | </form> |
| 29 | 29 | |
| 30 | - <?php if ( ! $is_default_card ) { ?> |
|
| 30 | + <?php if ( ! $is_default_card) { ?> |
|
| 31 | 31 | <form action="" method="POST" style="margin-top:10px;"> |
| 32 | - <?php wp_nonce_field ( 'stripe_default_card' ); ?> |
|
| 33 | - <input type="hidden" name="stripe_default_card" value="<?php echo esc_attr( $card->id ); ?>"> |
|
| 34 | - <input type="submit" class="button" value="<?php esc_attr_e( 'Make Default', 'woocommerce-gateway-stripe' ); ?>"> |
|
| 32 | + <?php wp_nonce_field('stripe_default_card'); ?> |
|
| 33 | + <input type="hidden" name="stripe_default_card" value="<?php echo esc_attr($card->id); ?>"> |
|
| 34 | + <input type="submit" class="button" value="<?php esc_attr_e('Make Default', 'woocommerce-gateway-stripe'); ?>"> |
|
| 35 | 35 | </form> |
| 36 | 36 | <?php } ?> |
| 37 | 37 | </td> |
@@ -17,7 +17,10 @@ |
||
| 17 | 17 | ?> |
| 18 | 18 | <tr> |
| 19 | 19 | <td><?php printf( __( '%s card ending in %s', 'woocommerce-gateway-stripe' ), $card->brand, $card->last4 ); ?> |
| 20 | - <?php if ( $is_default_card ) echo '<br />' . __( '(Default)', 'woocommerce-gateway-stripe' ); ?> |
|
| 20 | + <?php if ( $is_default_card ) { |
|
| 21 | + echo '<br />' . __( '(Default)', 'woocommerce-gateway-stripe' ); |
|
| 22 | +} |
|
| 23 | +?> |
|
| 21 | 24 | </td> |
| 22 | 25 | <td><?php printf( __( 'Expires %s/%s', 'woocommerce-gateway-stripe' ), $card->exp_month, $card->exp_year ); ?></td> |
| 23 | 26 | <td> |
@@ -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( 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(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,28 +324,28 @@ 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 | |
| 348 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source ); |
|
| 348 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source); |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -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,43 +425,43 @@ 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 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 446 | - throw new Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
| 445 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 446 | + throw new Exception(sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | - WC_Stripe::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
| 449 | + WC_Stripe::log("Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
| 450 | 450 | |
| 451 | 451 | // Make the request |
| 452 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
| 452 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
| 453 | 453 | |
| 454 | - if ( is_wp_error( $response ) ) { |
|
| 454 | + if (is_wp_error($response)) { |
|
| 455 | 455 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
| 456 | - if ( 'customer' === $response->get_error_code() && $retry ) { |
|
| 457 | - delete_user_meta( get_current_user_id(), '_stripe_customer_id' ); |
|
| 458 | - return $this->process_payment( $order_id, false, $force_customer ); |
|
| 456 | + if ('customer' === $response->get_error_code() && $retry) { |
|
| 457 | + delete_user_meta(get_current_user_id(), '_stripe_customer_id'); |
|
| 458 | + return $this->process_payment($order_id, false, $force_customer); |
|
| 459 | 459 | } |
| 460 | - throw new Exception( $response->get_error_code() . ': ' . $response->get_error_message() ); |
|
| 460 | + throw new Exception($response->get_error_code() . ': ' . $response->get_error_message()); |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | // Process valid response |
| 464 | - $this->process_response( $response, $order ); |
|
| 464 | + $this->process_response($response, $order); |
|
| 465 | 465 | } else { |
| 466 | 466 | $order->payment_complete(); |
| 467 | 467 | } |
@@ -472,13 +472,13 @@ discard block |
||
| 472 | 472 | // Return thank you page redirect |
| 473 | 473 | return array( |
| 474 | 474 | 'result' => 'success', |
| 475 | - 'redirect' => $this->get_return_url( $order ) |
|
| 475 | + 'redirect' => $this->get_return_url($order) |
|
| 476 | 476 | ); |
| 477 | 477 | |
| 478 | - } catch ( Exception $e ) { |
|
| 479 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
| 480 | - WC()->session->set( 'refresh_totals', true ); |
|
| 481 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
| 478 | + } catch (Exception $e) { |
|
| 479 | + wc_add_notice($e->getMessage(), 'error'); |
|
| 480 | + WC()->session->set('refresh_totals', true); |
|
| 481 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
| 482 | 482 | return; |
| 483 | 483 | } |
| 484 | 484 | } |
@@ -486,45 +486,45 @@ discard block |
||
| 486 | 486 | /** |
| 487 | 487 | * Save source to order. |
| 488 | 488 | */ |
| 489 | - protected function save_source( $order, $source ) { |
|
| 489 | + protected function save_source($order, $source) { |
|
| 490 | 490 | // Store source in the order |
| 491 | - if ( $source->customer ) { |
|
| 492 | - update_post_meta( $order->id, '_stripe_customer_id', $source->customer ); |
|
| 491 | + if ($source->customer) { |
|
| 492 | + update_post_meta($order->id, '_stripe_customer_id', $source->customer); |
|
| 493 | 493 | } |
| 494 | - if ( $source->source ) { |
|
| 495 | - update_post_meta( $order->id, '_stripe_card_id', $source->source->id ); |
|
| 494 | + if ($source->source) { |
|
| 495 | + update_post_meta($order->id, '_stripe_card_id', $source->source->id); |
|
| 496 | 496 | } |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | /** |
| 500 | 500 | * Store extra meta data for an order from a Stripe Response. |
| 501 | 501 | */ |
| 502 | - public function process_response( $response, $order ) { |
|
| 503 | - WC_Stripe::log( "Processing response: " . print_r( $response, true ) ); |
|
| 502 | + public function process_response($response, $order) { |
|
| 503 | + WC_Stripe::log("Processing response: " . print_r($response, true)); |
|
| 504 | 504 | |
| 505 | 505 | // Store charge data |
| 506 | - update_post_meta( $order->id, '_stripe_charge_id', $response->id ); |
|
| 507 | - update_post_meta( $order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no' ); |
|
| 506 | + update_post_meta($order->id, '_stripe_charge_id', $response->id); |
|
| 507 | + update_post_meta($order->id, '_stripe_charge_captured', $response->captured ? 'yes' : 'no'); |
|
| 508 | 508 | |
| 509 | 509 | // Store other data such as fees |
| 510 | - if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) { |
|
| 511 | - $fee = number_format( $response->balance_transaction->fee / 100, 2, '.', '' ); |
|
| 512 | - update_post_meta( $order->id, 'Stripe Fee', $fee ); |
|
| 513 | - update_post_meta( $order->id, 'Net Revenue From Stripe', $order->get_total() - $fee ); |
|
| 510 | + if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) { |
|
| 511 | + $fee = number_format($response->balance_transaction->fee / 100, 2, '.', ''); |
|
| 512 | + update_post_meta($order->id, 'Stripe Fee', $fee); |
|
| 513 | + update_post_meta($order->id, 'Net Revenue From Stripe', $order->get_total() - $fee); |
|
| 514 | 514 | } |
| 515 | 515 | |
| 516 | - if ( $response->captured ) { |
|
| 517 | - $order->payment_complete( $response->id ); |
|
| 518 | - WC_Stripe::log( "Successful charge: $response->id" ); |
|
| 516 | + if ($response->captured) { |
|
| 517 | + $order->payment_complete($response->id); |
|
| 518 | + WC_Stripe::log("Successful charge: $response->id"); |
|
| 519 | 519 | } else { |
| 520 | - add_post_meta( $order->id, '_transaction_id', $response->id, true ); |
|
| 520 | + add_post_meta($order->id, '_transaction_id', $response->id, true); |
|
| 521 | 521 | |
| 522 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 522 | + if ($order->has_status(array('pending', 'failed'))) { |
|
| 523 | 523 | $order->reduce_order_stock(); |
| 524 | 524 | } |
| 525 | 525 | |
| 526 | - $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 ) ); |
|
| 527 | - WC_Stripe::log( "Successful auth: $response->id" ); |
|
| 526 | + $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)); |
|
| 527 | + WC_Stripe::log("Successful auth: $response->id"); |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | return $response; |
@@ -536,21 +536,21 @@ discard block |
||
| 536 | 536 | * @since 3.0.0 |
| 537 | 537 | */ |
| 538 | 538 | public function add_payment_method() { |
| 539 | - if ( empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
| 540 | - wc_add_notice( __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ), 'error' ); |
|
| 539 | + if (empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
| 540 | + wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error'); |
|
| 541 | 541 | return; |
| 542 | 542 | } |
| 543 | 543 | |
| 544 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 545 | - $result = $stripe_customer->add_card( wc_clean( $_POST['stripe_token'] ) ); |
|
| 544 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 545 | + $result = $stripe_customer->add_card(wc_clean($_POST['stripe_token'])); |
|
| 546 | 546 | |
| 547 | - if ( is_wp_error( $result ) ) { |
|
| 548 | - throw new Exception( $result->get_error_message() ); |
|
| 547 | + if (is_wp_error($result)) { |
|
| 548 | + throw new Exception($result->get_error_message()); |
|
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | return array( |
| 552 | 552 | 'result' => 'success', |
| 553 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
| 553 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
| 554 | 554 | ); |
| 555 | 555 | } |
| 556 | 556 | |
@@ -560,36 +560,36 @@ discard block |
||
| 560 | 560 | * @param float $amount |
| 561 | 561 | * @return bool |
| 562 | 562 | */ |
| 563 | - public function process_refund( $order_id, $amount = null, $reason = '' ) { |
|
| 564 | - $order = wc_get_order( $order_id ); |
|
| 563 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
| 564 | + $order = wc_get_order($order_id); |
|
| 565 | 565 | |
| 566 | - if ( ! $order || ! $order->get_transaction_id() ) { |
|
| 566 | + if ( ! $order || ! $order->get_transaction_id()) { |
|
| 567 | 567 | return false; |
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | $body = array(); |
| 571 | 571 | |
| 572 | - if ( ! is_null( $amount ) ) { |
|
| 573 | - $body['amount'] = $this->get_stripe_amount( $amount ); |
|
| 572 | + if ( ! is_null($amount)) { |
|
| 573 | + $body['amount'] = $this->get_stripe_amount($amount); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | - if ( $reason ) { |
|
| 576 | + if ($reason) { |
|
| 577 | 577 | $body['metadata'] = array( |
| 578 | 578 | 'reason' => $reason, |
| 579 | 579 | ); |
| 580 | 580 | } |
| 581 | 581 | |
| 582 | - WC_Stripe::log( "Info: Beginning refund for order $order_id for the amount of {$amount}" ); |
|
| 582 | + WC_Stripe::log("Info: Beginning refund for order $order_id for the amount of {$amount}"); |
|
| 583 | 583 | |
| 584 | - $response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' ); |
|
| 584 | + $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); |
|
| 585 | 585 | |
| 586 | - if ( is_wp_error( $response ) ) { |
|
| 587 | - WC_Stripe::log( "Error: " . $response->get_error_message() ); |
|
| 586 | + if (is_wp_error($response)) { |
|
| 587 | + WC_Stripe::log("Error: " . $response->get_error_message()); |
|
| 588 | 588 | return $response; |
| 589 | - } elseif ( ! empty( $response->id ) ) { |
|
| 590 | - $refund_message = sprintf( __( 'Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe' ), wc_price( $response->amount / 100 ), $response->id, $reason ); |
|
| 591 | - $order->add_order_note( $refund_message ); |
|
| 592 | - WC_Stripe::log( "Success: " . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
| 589 | + } elseif ( ! empty($response->id)) { |
|
| 590 | + $refund_message = sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason); |
|
| 591 | + $order->add_order_note($refund_message); |
|
| 592 | + WC_Stripe::log("Success: " . html_entity_decode(strip_tags($refund_message))); |
|
| 593 | 593 | return true; |
| 594 | 594 | } |
| 595 | 595 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if ( ! defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | public function __construct() { |
| 48 | 48 | self::$_this = $this; |
| 49 | 49 | |
| 50 | - $this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' ); |
|
| 50 | + $this->_gateway_settings = get_option('woocommerce_stripe_settings', ''); |
|
| 51 | 51 | |
| 52 | - $this->statement_descriptor = ! empty( $this->_gateway_settings['statement_descriptor'] ) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
|
| 52 | + $this->statement_descriptor = ! empty($this->_gateway_settings['statement_descriptor']) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES); |
|
| 53 | 53 | |
| 54 | 54 | $this->init(); |
| 55 | 55 | } |
@@ -66,30 +66,30 @@ discard block |
||
| 66 | 66 | * @version 3.1.0 |
| 67 | 67 | */ |
| 68 | 68 | public function init() { |
| 69 | - add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
|
| 70 | - add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 20 ); |
|
| 71 | - add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ) ); |
|
| 72 | - add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) ); |
|
| 73 | - add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) ); |
|
| 74 | - add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) ); |
|
| 75 | - add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) ); |
|
| 76 | - add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 ); |
|
| 69 | + add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); |
|
| 70 | + add_action('woocommerce_proceed_to_checkout', array($this, 'display_apple_pay_button'), 20); |
|
| 71 | + add_action('woocommerce_checkout_before_customer_details', array($this, 'display_apple_pay_button')); |
|
| 72 | + add_action('wc_ajax_wc_stripe_apple_pay', array($this, 'process_apple_pay')); |
|
| 73 | + add_action('wc_ajax_wc_stripe_generate_apple_pay_cart', array($this, 'generate_apple_pay_cart')); |
|
| 74 | + add_action('wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array($this, 'get_shipping_methods')); |
|
| 75 | + add_action('wc_ajax_wc_stripe_apple_pay_update_shipping_method', array($this, 'update_shipping_method')); |
|
| 76 | + add_filter('woocommerce_gateway_title', array($this, 'filter_gateway_title'), 10, 2); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Filters the gateway title to reflect Apple Pay. |
| 81 | 81 | * |
| 82 | 82 | */ |
| 83 | - public function filter_gateway_title( $title, $id ) { |
|
| 83 | + public function filter_gateway_title($title, $id) { |
|
| 84 | 84 | global $post; |
| 85 | 85 | |
| 86 | - if ( ! is_object( $post ) ) { |
|
| 86 | + if ( ! is_object($post)) { |
|
| 87 | 87 | return $title; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - $method_title = get_post_meta( $post->ID, '_payment_method_title', true ); |
|
| 90 | + $method_title = get_post_meta($post->ID, '_payment_method_title', true); |
|
| 91 | 91 | |
| 92 | - if ( 'stripe' === $id && ! empty( $method_title ) ) { |
|
| 92 | + if ('stripe' === $id && ! empty($method_title)) { |
|
| 93 | 93 | return $method_title; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -103,38 +103,38 @@ discard block |
||
| 103 | 103 | * @version 3.1.0 |
| 104 | 104 | */ |
| 105 | 105 | public function payment_scripts() { |
| 106 | - if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
|
| 106 | + if ( ! is_cart() && ! is_checkout() && ! isset($_GET['pay_for_order'])) { |
|
| 107 | 107 | return; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if ( ! $this->is_supported_product_type() ) { |
|
| 110 | + if ( ! $this->is_supported_product_type()) { |
|
| 111 | 111 | return; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 114 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
| 115 | 115 | |
| 116 | - wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
|
| 116 | + wp_enqueue_style('stripe_apple_pay', plugins_url('assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE), array(), WC_STRIPE_VERSION); |
|
| 117 | 117 | |
| 118 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
| 119 | - wp_enqueue_script( 'woocommerce_stripe_apple_pay', plugins_url( 'assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
|
| 118 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
| 119 | + wp_enqueue_script('woocommerce_stripe_apple_pay', plugins_url('assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('stripe'), WC_STRIPE_VERSION, true); |
|
| 120 | 120 | |
| 121 | 121 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
| 122 | 122 | |
| 123 | 123 | $stripe_params = array( |
| 124 | 124 | 'key' => $publishable_key, |
| 125 | 125 | 'currency_code' => get_woocommerce_currency(), |
| 126 | - 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
|
| 126 | + 'country_code' => substr(get_option('woocommerce_default_country'), 0, 2), |
|
| 127 | 127 | 'label' => $this->statement_descriptor, |
| 128 | - 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
| 129 | - 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
|
| 130 | - 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
|
| 131 | - 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
|
| 132 | - 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
|
| 128 | + 'ajaxurl' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
| 129 | + 'stripe_apple_pay_nonce' => wp_create_nonce('_wc_stripe_apple_pay_nonce'), |
|
| 130 | + 'stripe_apple_pay_cart_nonce' => wp_create_nonce('_wc_stripe_apple_pay_cart_nonce'), |
|
| 131 | + 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce('_wc_stripe_apple_pay_get_shipping_methods_nonce'), |
|
| 132 | + 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce('_wc_stripe_apple_pay_update_shipping_method_nonce'), |
|
| 133 | 133 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
| 134 | 134 | 'is_cart_page' => is_cart() ? 'yes' : 'no', |
| 135 | 135 | ); |
| 136 | 136 | |
| 137 | - wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) ); |
|
| 137 | + wp_localize_script('woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters('wc_stripe_apple_pay_params', $stripe_params)); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -142,8 +142,8 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | */ |
| 144 | 144 | public function is_supported_product_type() { |
| 145 | - foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 146 | - if ( 'subscription' === $values['data']->product_type ) { |
|
| 145 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 146 | + if ('subscription' === $values['data']->product_type) { |
|
| 147 | 147 | return false; |
| 148 | 148 | } |
| 149 | 149 | } |
@@ -166,19 +166,19 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | if ( |
| 168 | 168 | 'yes' !== $this->_gateway_settings['apple_pay'] |
| 169 | - || ! isset( $gateways['stripe'] ) |
|
| 169 | + || ! isset($gateways['stripe']) |
|
| 170 | 170 | ) { |
| 171 | 171 | return; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - if ( ! $this->is_supported_product_type() ) { |
|
| 174 | + if ( ! $this->is_supported_product_type()) { |
|
| 175 | 175 | return; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - $apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
| 179 | - $country = strtolower( substr( get_option( 'woocommerce_default_country' ), 0, 2 ) ); |
|
| 178 | + $apple_pay_button = ! empty($this->_gateway_settings['apple_pay_button']) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
|
| 179 | + $country = strtolower(substr(get_option('woocommerce_default_country'), 0, 2)); |
|
| 180 | 180 | ?> |
| 181 | - <button class="apple-pay-button" lang="<?php echo esc_attr( $country ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button> |
|
| 181 | + <button class="apple-pay-button" lang="<?php echo esc_attr($country); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr($apple_pay_button); ?>;"></button> |
|
| 182 | 182 | <?php |
| 183 | 183 | } |
| 184 | 184 | |
@@ -189,11 +189,11 @@ discard block |
||
| 189 | 189 | * @version 3.1.0 |
| 190 | 190 | */ |
| 191 | 191 | public function generate_apple_pay_cart() { |
| 192 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
|
| 193 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 192 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce')) { |
|
| 193 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 196 | + wp_send_json(array('line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** |
@@ -203,29 +203,29 @@ discard block |
||
| 203 | 203 | * @version 3.1.0 |
| 204 | 204 | * @param array $address |
| 205 | 205 | */ |
| 206 | - public function calculate_shipping( $address = array() ) { |
|
| 207 | - $country = strtoupper( $address['countryCode'] ); |
|
| 208 | - $state = strtoupper( $address['administrativeArea'] ); |
|
| 206 | + public function calculate_shipping($address = array()) { |
|
| 207 | + $country = strtoupper($address['countryCode']); |
|
| 208 | + $state = strtoupper($address['administrativeArea']); |
|
| 209 | 209 | $postcode = $address['postalCode']; |
| 210 | 210 | $city = $address['locality']; |
| 211 | 211 | |
| 212 | 212 | WC()->shipping->reset_shipping(); |
| 213 | 213 | |
| 214 | - if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
| 215 | - throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
| 216 | - } elseif ( $postcode ) { |
|
| 217 | - $postcode = wc_format_postcode( $postcode, $country ); |
|
| 214 | + if ($postcode && ! WC_Validation::is_postcode($postcode, $country)) { |
|
| 215 | + throw new Exception(__('Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe')); |
|
| 216 | + } elseif ($postcode) { |
|
| 217 | + $postcode = wc_format_postcode($postcode, $country); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if ( $country ) { |
|
| 221 | - WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
| 222 | - WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
| 220 | + if ($country) { |
|
| 221 | + WC()->customer->set_location($country, $state, $postcode, $city); |
|
| 222 | + WC()->customer->set_shipping_location($country, $state, $postcode, $city); |
|
| 223 | 223 | } else { |
| 224 | 224 | WC()->customer->set_to_base(); |
| 225 | 225 | WC()->customer->set_shipping_to_base(); |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - WC()->customer->calculated_shipping( true ); |
|
| 228 | + WC()->customer->calculated_shipping(true); |
|
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | 231 | * Set the shipping package. |
@@ -247,17 +247,17 @@ discard block |
||
| 247 | 247 | $packages[0]['destination']['postcode'] = $postcode; |
| 248 | 248 | $packages[0]['destination']['city'] = $city; |
| 249 | 249 | |
| 250 | - foreach ( WC()->cart->get_cart() as $item ) { |
|
| 251 | - if ( $item['data']->needs_shipping() ) { |
|
| 252 | - if ( isset( $item['line_total'] ) ) { |
|
| 250 | + foreach (WC()->cart->get_cart() as $item) { |
|
| 251 | + if ($item['data']->needs_shipping()) { |
|
| 252 | + if (isset($item['line_total'])) { |
|
| 253 | 253 | $packages[0]['contents_cost'] += $item['line_total']; |
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
|
| 258 | + $packages = apply_filters('woocommerce_cart_shipping_packages', $packages); |
|
| 259 | 259 | |
| 260 | - WC()->shipping->calculate_shipping( $packages ); |
|
| 260 | + WC()->shipping->calculate_shipping($packages); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -267,18 +267,18 @@ discard block |
||
| 267 | 267 | * @version 3.1.0 |
| 268 | 268 | */ |
| 269 | 269 | public function get_shipping_methods() { |
| 270 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) { |
|
| 271 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 270 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce')) { |
|
| 271 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 275 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 274 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 275 | + define('WOOCOMMERCE_CART', true); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | try { |
| 279 | - $address = array_map( 'wc_clean', $_POST['address'] ); |
|
| 279 | + $address = array_map('wc_clean', $_POST['address']); |
|
| 280 | 280 | |
| 281 | - $this->calculate_shipping( $address ); |
|
| 281 | + $this->calculate_shipping($address); |
|
| 282 | 282 | |
| 283 | 283 | // Set the shipping options. |
| 284 | 284 | $currency = get_woocommerce_currency(); |
@@ -286,13 +286,13 @@ discard block |
||
| 286 | 286 | |
| 287 | 287 | $packages = WC()->shipping->get_packages(); |
| 288 | 288 | |
| 289 | - if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
|
| 290 | - foreach ( $packages as $package_key => $package ) { |
|
| 291 | - if ( empty( $package['rates'] ) ) { |
|
| 292 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
| 289 | + if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) { |
|
| 290 | + foreach ($packages as $package_key => $package) { |
|
| 291 | + if (empty($package['rates'])) { |
|
| 292 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - foreach ( $package['rates'] as $key => $rate ) { |
|
| 295 | + foreach ($package['rates'] as $key => $rate) { |
|
| 296 | 296 | $data[] = array( |
| 297 | 297 | 'id' => $rate->id, |
| 298 | 298 | 'label' => $rate->label, |
@@ -306,16 +306,16 @@ discard block |
||
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | // Auto select the first shipping method. |
| 309 | - WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) ); |
|
| 309 | + WC()->session->set('chosen_shipping_methods', array($data[0]['id'])); |
|
| 310 | 310 | |
| 311 | 311 | WC()->cart->calculate_totals(); |
| 312 | 312 | |
| 313 | - wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 313 | + wp_send_json(array('success' => 'true', 'shipping_methods' => $this->build_shipping_methods($data), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 314 | 314 | } else { |
| 315 | - throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
|
| 315 | + throw new Exception(__('Unable to find shipping method for address.', 'woocommerce-gateway-stripe')); |
|
| 316 | 316 | } |
| 317 | - } catch( Exception $e ) { |
|
| 318 | - wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 317 | + } catch (Exception $e) { |
|
| 318 | + wp_send_json(array('success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 319 | 319 | } |
| 320 | 320 | } |
| 321 | 321 | |
@@ -326,29 +326,29 @@ discard block |
||
| 326 | 326 | * @version 3.1.0 |
| 327 | 327 | */ |
| 328 | 328 | public function update_shipping_method() { |
| 329 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 330 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 329 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 330 | + define('WOOCOMMERCE_CART', true); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) { |
|
| 334 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 333 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce')) { |
|
| 334 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - $selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] ); |
|
| 337 | + $selected_shipping_method = array_map('wc_clean', $_POST['selected_shipping_method']); |
|
| 338 | 338 | |
| 339 | - WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) ); |
|
| 339 | + WC()->session->set('chosen_shipping_methods', array($selected_shipping_method['identifier'])); |
|
| 340 | 340 | |
| 341 | 341 | WC()->cart->calculate_totals(); |
| 342 | 342 | |
| 343 | 343 | // Send back the new cart total. |
| 344 | 344 | $currency = get_woocommerce_currency(); |
| 345 | - $tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
|
| 345 | + $tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp)); |
|
| 346 | 346 | $data = array( |
| 347 | 347 | 'total' => WC()->cart->total, |
| 348 | 348 | ); |
| 349 | 349 | |
| 350 | 350 | // Include fees and taxes as displayItems. |
| 351 | - foreach ( WC()->cart->fees as $key => $fee ) { |
|
| 351 | + foreach (WC()->cart->fees as $key => $fee) { |
|
| 352 | 352 | $data['items'][] = array( |
| 353 | 353 | 'label' => $fee->name, |
| 354 | 354 | 'amount' => array( |
@@ -357,9 +357,9 @@ discard block |
||
| 357 | 357 | ), |
| 358 | 358 | ); |
| 359 | 359 | } |
| 360 | - if ( 0 < $tax_total ) { |
|
| 360 | + if (0 < $tax_total) { |
|
| 361 | 361 | $data['items'][] = array( |
| 362 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
| 362 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
| 363 | 363 | 'amount' => array( |
| 364 | 364 | 'currency' => $currency, |
| 365 | 365 | 'value' => $tax_total, |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | ); |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | - wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
|
| 370 | + wp_send_json(array('success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total)); |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | /** |
@@ -378,35 +378,35 @@ discard block |
||
| 378 | 378 | * @version 3.1.0 |
| 379 | 379 | */ |
| 380 | 380 | public function process_apple_pay() { |
| 381 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
|
| 382 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
| 381 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_apple_pay_nonce')) { |
|
| 382 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | try { |
| 386 | - $result = array_map( 'wc_clean', $_POST['result'] ); |
|
| 386 | + $result = array_map('wc_clean', $_POST['result']); |
|
| 387 | 387 | |
| 388 | - $order = $this->create_order( $result ); |
|
| 388 | + $order = $this->create_order($result); |
|
| 389 | 389 | |
| 390 | 390 | // Handle payment. |
| 391 | - if ( $order->get_total() > 0 ) { |
|
| 391 | + if ($order->get_total() > 0) { |
|
| 392 | 392 | |
| 393 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 394 | - return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
|
| 393 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
| 394 | + return new WP_Error('stripe_error', sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe::get_minimum_amount() / 100))); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | - WC_Stripe::log( "Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}" ); |
|
| 397 | + WC_Stripe::log("Info: Begin processing payment for order $order->id for the amount of {$order->get_total()}"); |
|
| 398 | 398 | |
| 399 | 399 | // Make the request. |
| 400 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
|
| 400 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $result['token']['id'])); |
|
| 401 | 401 | |
| 402 | - if ( is_wp_error( $response ) ) { |
|
| 402 | + if (is_wp_error($response)) { |
|
| 403 | 403 | $localized_messages = $this->get_localized_messages(); |
| 404 | 404 | |
| 405 | - throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
|
| 405 | + throw new Exception((isset($localized_messages[$response->get_error_code()]) ? $localized_messages[$response->get_error_code()] : $response->get_error_message())); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | // Process valid response. |
| 409 | - $this->process_response( $response, $order ); |
|
| 409 | + $this->process_response($response, $order); |
|
| 410 | 410 | } else { |
| 411 | 411 | $order->payment_complete(); |
| 412 | 412 | } |
@@ -414,24 +414,24 @@ discard block |
||
| 414 | 414 | // Remove cart. |
| 415 | 415 | WC()->cart->empty_cart(); |
| 416 | 416 | |
| 417 | - update_post_meta( $order->id, '_customer_user', get_current_user_id() ); |
|
| 418 | - update_post_meta( $order->id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) ); |
|
| 417 | + update_post_meta($order->id, '_customer_user', get_current_user_id()); |
|
| 418 | + update_post_meta($order->id, '_payment_method_title', __('Apple Pay (Stripe)', 'woocommerce-gateway-stripe')); |
|
| 419 | 419 | |
| 420 | 420 | // Return thank you page redirect. |
| 421 | - wp_send_json( array( |
|
| 421 | + wp_send_json(array( |
|
| 422 | 422 | 'success' => 'true', |
| 423 | - 'redirect' => $this->get_return_url( $order ), |
|
| 424 | - ) ); |
|
| 423 | + 'redirect' => $this->get_return_url($order), |
|
| 424 | + )); |
|
| 425 | 425 | |
| 426 | - } catch ( Exception $e ) { |
|
| 427 | - WC()->session->set( 'refresh_totals', true ); |
|
| 428 | - WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
|
| 426 | + } catch (Exception $e) { |
|
| 427 | + WC()->session->set('refresh_totals', true); |
|
| 428 | + WC_Stripe::log(sprintf(__('Error: %s', 'woocommerce-gateway-stripe'), $e->getMessage())); |
|
| 429 | 429 | |
| 430 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 431 | - $this->send_failed_order_email( $order->id ); |
|
| 430 | + if ($order->has_status(array('pending', 'failed'))) { |
|
| 431 | + $this->send_failed_order_email($order->id); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | - wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
|
| 434 | + wp_send_json(array('success' => 'false', 'msg' => $e->getMessage())); |
|
| 435 | 435 | } |
| 436 | 436 | } |
| 437 | 437 | |
@@ -441,14 +441,14 @@ discard block |
||
| 441 | 441 | * @param string $source token |
| 442 | 442 | * @return array() |
| 443 | 443 | */ |
| 444 | - protected function generate_payment_request( $order, $source ) { |
|
| 444 | + protected function generate_payment_request($order, $source) { |
|
| 445 | 445 | $post_data = array(); |
| 446 | - $post_data['currency'] = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() ); |
|
| 447 | - $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
| 448 | - $post_data['description'] = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() ); |
|
| 446 | + $post_data['currency'] = strtolower($order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency()); |
|
| 447 | + $post_data['amount'] = $this->get_stripe_amount($order->get_total(), $post_data['currency']); |
|
| 448 | + $post_data['description'] = sprintf(__('%s - Order %s', 'woocommerce-gateway-stripe'), $this->statement_descriptor, $order->get_order_number()); |
|
| 449 | 449 | $post_data['capture'] = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false'; |
| 450 | 450 | |
| 451 | - if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
| 451 | + if ( ! empty($order->billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
| 452 | 452 | $post_data['receipt_email'] = $order->billing_email; |
| 453 | 453 | } |
| 454 | 454 | |
@@ -463,7 +463,7 @@ discard block |
||
| 463 | 463 | * @param WC_Order $order |
| 464 | 464 | * @param object $source |
| 465 | 465 | */ |
| 466 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order ); |
|
| 466 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | /** |
@@ -472,14 +472,14 @@ discard block |
||
| 472 | 472 | * @since 3.1.0 |
| 473 | 473 | * @version 3.1.0 |
| 474 | 474 | */ |
| 475 | - public function build_shipping_methods( $shipping_methods ) { |
|
| 476 | - if ( empty( $shipping_methods ) ) { |
|
| 475 | + public function build_shipping_methods($shipping_methods) { |
|
| 476 | + if (empty($shipping_methods)) { |
|
| 477 | 477 | return array(); |
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | $shipping = array(); |
| 481 | 481 | |
| 482 | - foreach( $shipping_methods as $method ) { |
|
| 482 | + foreach ($shipping_methods as $method) { |
|
| 483 | 483 | $shipping[] = array( |
| 484 | 484 | 'label' => $method['label'], |
| 485 | 485 | 'detail' => '', |
@@ -498,53 +498,53 @@ discard block |
||
| 498 | 498 | * @version 3.1.0 |
| 499 | 499 | */ |
| 500 | 500 | public function build_line_items() { |
| 501 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 502 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 501 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 502 | + define('WOOCOMMERCE_CART', true); |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | - $decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 ); |
|
| 505 | + $decimals = apply_filters('wc_stripe_apple_pay_decimals', 2); |
|
| 506 | 506 | |
| 507 | 507 | $items = array(); |
| 508 | 508 | |
| 509 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 510 | - $amount = wc_format_decimal( $values['line_subtotal'], $decimals ); |
|
| 509 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 510 | + $amount = wc_format_decimal($values['line_subtotal'], $decimals); |
|
| 511 | 511 | $quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : ''; |
| 512 | 512 | |
| 513 | 513 | $item = array( |
| 514 | 514 | 'type' => 'final', |
| 515 | 515 | 'label' => $values['data']->post->post_title . $quantity_label, |
| 516 | - 'amount' => wc_format_decimal( $amount, $decimals ), |
|
| 516 | + 'amount' => wc_format_decimal($amount, $decimals), |
|
| 517 | 517 | ); |
| 518 | 518 | |
| 519 | 519 | $items[] = $item; |
| 520 | 520 | } |
| 521 | 521 | |
| 522 | - $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals ); |
|
| 523 | - $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals ); |
|
| 524 | - $shipping = wc_format_decimal( WC()->cart->shipping_total, $decimals ); |
|
| 525 | - $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts; |
|
| 526 | - $order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals ); |
|
| 522 | + $discounts = wc_format_decimal(WC()->cart->get_cart_discount_total(), $decimals); |
|
| 523 | + $tax = wc_format_decimal(WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals); |
|
| 524 | + $shipping = wc_format_decimal(WC()->cart->shipping_total, $decimals); |
|
| 525 | + $item_total = wc_format_decimal(WC()->cart->cart_contents_total, $decimals) + $discounts; |
|
| 526 | + $order_total = wc_format_decimal($item_total + $tax + $shipping, $decimals); |
|
| 527 | 527 | |
| 528 | - if ( wc_tax_enabled() ) { |
|
| 528 | + if (wc_tax_enabled()) { |
|
| 529 | 529 | $items[] = array( |
| 530 | 530 | 'type' => 'final', |
| 531 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
| 531 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
| 532 | 532 | 'amount' => $tax, |
| 533 | 533 | ); |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | - if ( WC()->cart->needs_shipping() ) { |
|
| 536 | + if (WC()->cart->needs_shipping()) { |
|
| 537 | 537 | $items[] = array( |
| 538 | 538 | 'type' => 'final', |
| 539 | - 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
|
| 539 | + 'label' => __('Shipping', 'woocommerce-gateway-stripe'), |
|
| 540 | 540 | 'amount' => $shipping, |
| 541 | 541 | ); |
| 542 | 542 | } |
| 543 | 543 | |
| 544 | - if ( WC()->cart->has_discount() ) { |
|
| 544 | + if (WC()->cart->has_discount()) { |
|
| 545 | 545 | $items[] = array( |
| 546 | 546 | 'type' => 'final', |
| 547 | - 'label' => __( 'Discount', 'woocommerce-gateway-stripe' ), |
|
| 547 | + 'label' => __('Discount', 'woocommerce-gateway-stripe'), |
|
| 548 | 548 | 'amount' => $discounts, |
| 549 | 549 | ); |
| 550 | 550 | } |
@@ -560,24 +560,24 @@ discard block |
||
| 560 | 560 | * @param array $data |
| 561 | 561 | * @return object $order |
| 562 | 562 | */ |
| 563 | - public function create_order( $data = array() ) { |
|
| 564 | - if ( empty( $data ) ) { |
|
| 565 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
| 563 | + public function create_order($data = array()) { |
|
| 564 | + if (empty($data)) { |
|
| 565 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | $order = wc_create_order(); |
| 569 | 569 | |
| 570 | - if ( is_wp_error( $order ) ) { |
|
| 571 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
|
| 572 | - } elseif ( false === $order ) { |
|
| 573 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
|
| 570 | + if (is_wp_error($order)) { |
|
| 571 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 520)); |
|
| 572 | + } elseif (false === $order) { |
|
| 573 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 521)); |
|
| 574 | 574 | } else { |
| 575 | 575 | $order_id = $order->id; |
| 576 | - do_action( 'woocommerce_new_order', $order_id ); |
|
| 576 | + do_action('woocommerce_new_order', $order_id); |
|
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | // Store the line items to the new/resumed order |
| 580 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 580 | + foreach (WC()->cart->get_cart() as $cart_item_key => $values) { |
|
| 581 | 581 | $item_id = $order->add_product( |
| 582 | 582 | $values['data'], |
| 583 | 583 | $values['quantity'], |
@@ -593,47 +593,47 @@ discard block |
||
| 593 | 593 | ) |
| 594 | 594 | ); |
| 595 | 595 | |
| 596 | - if ( ! $item_id ) { |
|
| 597 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
|
| 596 | + if ( ! $item_id) { |
|
| 597 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 525)); |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | // Allow plugins to add order item meta |
| 601 | - do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
|
| 601 | + do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key); |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | // Store fees |
| 605 | - foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
|
| 606 | - $item_id = $order->add_fee( $fee ); |
|
| 605 | + foreach (WC()->cart->get_fees() as $fee_key => $fee) { |
|
| 606 | + $item_id = $order->add_fee($fee); |
|
| 607 | 607 | |
| 608 | - if ( ! $item_id ) { |
|
| 609 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
|
| 608 | + if ( ! $item_id) { |
|
| 609 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 526)); |
|
| 610 | 610 | } |
| 611 | 611 | |
| 612 | 612 | // Allow plugins to add order item meta to fees |
| 613 | - do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
|
| 613 | + do_action('woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key); |
|
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | // Store tax rows |
| 617 | - foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
|
| 618 | - if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { |
|
| 619 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
|
| 617 | + foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) { |
|
| 618 | + if ($tax_rate_id && ! $order->add_tax($tax_rate_id, WC()->cart->get_tax_amount($tax_rate_id), WC()->cart->get_shipping_tax_amount($tax_rate_id)) && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) { |
|
| 619 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 528)); |
|
| 620 | 620 | } |
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | // Store coupons |
| 624 | - foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
|
| 625 | - if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) { |
|
| 626 | - throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
|
| 624 | + foreach (WC()->cart->get_coupons() as $code => $coupon) { |
|
| 625 | + if ( ! $order->add_coupon($code, WC()->cart->get_coupon_discount_amount($code), WC()->cart->get_coupon_discount_tax_amount($code))) { |
|
| 626 | + throw new Exception(sprintf(__('Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe'), 529)); |
|
| 627 | 627 | } |
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | // Billing address |
| 631 | 631 | $billing_address = array(); |
| 632 | - if ( ! empty( $data['token']['card'] ) ) { |
|
| 632 | + if ( ! empty($data['token']['card'])) { |
|
| 633 | 633 | // Name from Stripe is a full name string. |
| 634 | - $name = explode( ' ', $data['token']['card']['name'] ); |
|
| 635 | - $lastname = array_pop( $name ); |
|
| 636 | - $firstname = implode( ' ', $name ); |
|
| 634 | + $name = explode(' ', $data['token']['card']['name']); |
|
| 635 | + $lastname = array_pop($name); |
|
| 636 | + $firstname = implode(' ', $name); |
|
| 637 | 637 | $billing_address['first_name'] = $firstname; |
| 638 | 638 | $billing_address['last_name'] = $lastname; |
| 639 | 639 | $billing_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -648,7 +648,7 @@ discard block |
||
| 648 | 648 | |
| 649 | 649 | // Shipping address. |
| 650 | 650 | $shipping_address = array(); |
| 651 | - if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) { |
|
| 651 | + if (WC()->cart->needs_shipping() && ! empty($data['shippingContact'])) { |
|
| 652 | 652 | $shipping_address['first_name'] = $data['shippingContact']['givenName']; |
| 653 | 653 | $shipping_address['last_name'] = $data['shippingContact']['familyName']; |
| 654 | 654 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -659,7 +659,7 @@ discard block |
||
| 659 | 659 | $shipping_address['city'] = $data['shippingContact']['locality']; |
| 660 | 660 | $shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
| 661 | 661 | $shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
| 662 | - } elseif ( ! empty( $data['shippingContact'] ) ) { |
|
| 662 | + } elseif ( ! empty($data['shippingContact'])) { |
|
| 663 | 663 | $shipping_address['first_name'] = $firstname; |
| 664 | 664 | $shipping_address['last_name'] = $lastname; |
| 665 | 665 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
@@ -672,34 +672,34 @@ discard block |
||
| 672 | 672 | $shipping_address['postcode'] = $data['token']['card']['address_zip']; |
| 673 | 673 | } |
| 674 | 674 | |
| 675 | - $order->set_address( $billing_address, 'billing' ); |
|
| 676 | - $order->set_address( $shipping_address, 'shipping' ); |
|
| 675 | + $order->set_address($billing_address, 'billing'); |
|
| 676 | + $order->set_address($shipping_address, 'shipping'); |
|
| 677 | 677 | |
| 678 | - WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
|
| 678 | + WC()->shipping->calculate_shipping(WC()->cart->get_shipping_packages()); |
|
| 679 | 679 | |
| 680 | 680 | // Get the rate object selected by user. |
| 681 | - foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
|
| 682 | - foreach ( $package['rates'] as $key => $rate ) { |
|
| 681 | + foreach (WC()->shipping->get_packages() as $package_key => $package) { |
|
| 682 | + foreach ($package['rates'] as $key => $rate) { |
|
| 683 | 683 | // Loop through user chosen shipping methods. |
| 684 | - foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
|
| 685 | - if ( $method === $key ) { |
|
| 686 | - $order->add_shipping( $rate ); |
|
| 684 | + foreach (WC()->session->get('chosen_shipping_methods') as $method) { |
|
| 685 | + if ($method === $key) { |
|
| 686 | + $order->add_shipping($rate); |
|
| 687 | 687 | } |
| 688 | 688 | } |
| 689 | 689 | } |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | 692 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 693 | - $order->set_payment_method( $available_gateways['stripe'] ); |
|
| 694 | - $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
|
| 695 | - $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
|
| 696 | - $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
|
| 697 | - $order->set_total( WC()->cart->tax_total, 'tax' ); |
|
| 698 | - $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
|
| 699 | - $order->set_total( WC()->cart->total ); |
|
| 693 | + $order->set_payment_method($available_gateways['stripe']); |
|
| 694 | + $order->set_total(WC()->cart->shipping_total, 'shipping'); |
|
| 695 | + $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); |
|
| 696 | + $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); |
|
| 697 | + $order->set_total(WC()->cart->tax_total, 'tax'); |
|
| 698 | + $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); |
|
| 699 | + $order->set_total(WC()->cart->total); |
|
| 700 | 700 | |
| 701 | 701 | // If we got here, the order was created without problems! |
| 702 | - wc_transaction_query( 'commit' ); |
|
| 702 | + wc_transaction_query('commit'); |
|
| 703 | 703 | |
| 704 | 704 | return $order; |
| 705 | 705 | } |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * @version 3.1.0 |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 10 | +if ( ! defined('ABSPATH')) { |
|
| 11 | 11 | exit; |
| 12 | 12 | } |
| 13 | 13 | |
@@ -20,12 +20,12 @@ discard block |
||
| 20 | 20 | * Initialize class actions. |
| 21 | 21 | */ |
| 22 | 22 | public function __construct() { |
| 23 | - add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) ); |
|
| 23 | + add_action('wp_enqueue_scripts', array($this, 'scripts')); |
|
| 24 | 24 | |
| 25 | - add_action( 'wc_ajax_wc_stripe_get_cart_details', array( $this, 'ajax_get_cart_details' ) ); |
|
| 26 | - add_action( 'wc_ajax_wc_stripe_get_shipping_options', array( $this, 'ajax_get_shipping_options' ) ); |
|
| 27 | - add_action( 'wc_ajax_wc_stripe_update_shipping_method', array( $this, 'ajax_update_shipping_method' ) ); |
|
| 28 | - add_action( 'wc_ajax_wc_stripe_create_order', array( $this, 'ajax_create_order' ) ); |
|
| 25 | + add_action('wc_ajax_wc_stripe_get_cart_details', array($this, 'ajax_get_cart_details')); |
|
| 26 | + add_action('wc_ajax_wc_stripe_get_shipping_options', array($this, 'ajax_get_shipping_options')); |
|
| 27 | + add_action('wc_ajax_wc_stripe_update_shipping_method', array($this, 'ajax_update_shipping_method')); |
|
| 28 | + add_action('wc_ajax_wc_stripe_create_order', array($this, 'ajax_create_order')); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
@@ -34,10 +34,10 @@ discard block |
||
| 34 | 34 | * @return bool |
| 35 | 35 | */ |
| 36 | 36 | protected function is_activated() { |
| 37 | - $options = get_option( 'woocommerce_stripe_settings', array() ); |
|
| 38 | - $enabled = isset( $options['enabled'] ) && 'yes' === $options['enabled']; |
|
| 39 | - $stripe_checkout = isset( $options['stripe_checkout'] ) && 'yes' !== $options['stripe_checkout']; |
|
| 40 | - $request_payment_api = isset( $options['request_payment_api'] ) && 'yes' === $options['request_payment_api']; |
|
| 37 | + $options = get_option('woocommerce_stripe_settings', array()); |
|
| 38 | + $enabled = isset($options['enabled']) && 'yes' === $options['enabled']; |
|
| 39 | + $stripe_checkout = isset($options['stripe_checkout']) && 'yes' !== $options['stripe_checkout']; |
|
| 40 | + $request_payment_api = isset($options['request_payment_api']) && 'yes' === $options['request_payment_api']; |
|
| 41 | 41 | |
| 42 | 42 | return $enabled && $stripe_checkout && $request_payment_api && is_ssl(); |
| 43 | 43 | } |
@@ -48,9 +48,9 @@ discard block |
||
| 48 | 48 | * @return string |
| 49 | 49 | */ |
| 50 | 50 | protected function get_publishable_key() { |
| 51 | - $options = get_option( 'woocommerce_stripe_settings', array() ); |
|
| 51 | + $options = get_option('woocommerce_stripe_settings', array()); |
|
| 52 | 52 | |
| 53 | - if ( empty( $options ) ) { |
|
| 53 | + if (empty($options)) { |
|
| 54 | 54 | return ''; |
| 55 | 55 | } |
| 56 | 56 | |
@@ -62,39 +62,39 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | public function scripts() { |
| 64 | 64 | // Load PaymentRequest only on cart for now. |
| 65 | - if ( ! is_cart() ) { |
|
| 65 | + if ( ! is_cart()) { |
|
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - if ( ! $this->is_activated() ) { |
|
| 69 | + if ( ! $this->is_activated()) { |
|
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 73 | + $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
|
| 74 | 74 | |
| 75 | - wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
|
| 76 | - wp_enqueue_script( 'google-payment-request-shim', 'https://storage.googleapis.com/prshim/v1/payment-shim.js', '', '1.0', false ); |
|
| 77 | - wp_enqueue_script( 'wc-stripe-payment-request', plugins_url( 'assets/js/payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery', 'stripe' ), WC_STRIPE_VERSION, true ); |
|
| 75 | + wp_enqueue_script('stripe', 'https://js.stripe.com/v2/', '', '1.0', true); |
|
| 76 | + wp_enqueue_script('google-payment-request-shim', 'https://storage.googleapis.com/prshim/v1/payment-shim.js', '', '1.0', false); |
|
| 77 | + wp_enqueue_script('wc-stripe-payment-request', plugins_url('assets/js/payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE), array('jquery', 'stripe'), WC_STRIPE_VERSION, true); |
|
| 78 | 78 | |
| 79 | 79 | wp_localize_script( |
| 80 | 80 | 'wc-stripe-payment-request', |
| 81 | 81 | 'wcStripePaymentRequestParams', |
| 82 | 82 | array( |
| 83 | - 'ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
|
| 83 | + 'ajax_url' => WC_AJAX::get_endpoint('%%endpoint%%'), |
|
| 84 | 84 | 'stripe' => array( |
| 85 | 85 | 'key' => $this->get_publishable_key(), |
| 86 | - 'allow_prepaid_card' => apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no', |
|
| 86 | + 'allow_prepaid_card' => apply_filters('wc_stripe_allow_prepaid_card', true) ? 'yes' : 'no', |
|
| 87 | 87 | ), |
| 88 | 88 | 'nonce' => array( |
| 89 | - 'payment' => wp_create_nonce( 'wc-stripe-payment-request' ), |
|
| 90 | - 'shipping' => wp_create_nonce( 'wc-stripe-payment-request-shipping' ), |
|
| 91 | - 'update_shipping' => wp_create_nonce( 'wc-stripe-update-shipping-method' ), |
|
| 92 | - 'checkout' => wp_create_nonce( 'woocommerce-process_checkout' ), |
|
| 89 | + 'payment' => wp_create_nonce('wc-stripe-payment-request'), |
|
| 90 | + 'shipping' => wp_create_nonce('wc-stripe-payment-request-shipping'), |
|
| 91 | + 'update_shipping' => wp_create_nonce('wc-stripe-update-shipping-method'), |
|
| 92 | + 'checkout' => wp_create_nonce('woocommerce-process_checkout'), |
|
| 93 | 93 | ), |
| 94 | 94 | 'i18n' => array( |
| 95 | - 'no_prepaid_card' => __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ), |
|
| 95 | + 'no_prepaid_card' => __('Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe'), |
|
| 96 | 96 | /* translators: Do not translate the [option] placeholder */ |
| 97 | - 'unknown_shipping' => __( 'Unknown shipping option "[option]".', 'woocommerce-gateway-stripe' ), |
|
| 97 | + 'unknown_shipping' => __('Unknown shipping option "[option]".', 'woocommerce-gateway-stripe'), |
|
| 98 | 98 | ), |
| 99 | 99 | ) |
| 100 | 100 | ); |
@@ -104,10 +104,10 @@ discard block |
||
| 104 | 104 | * Get cart details. |
| 105 | 105 | */ |
| 106 | 106 | public function ajax_get_cart_details() { |
| 107 | - check_ajax_referer( 'wc-stripe-payment-request', 'security' ); |
|
| 107 | + check_ajax_referer('wc-stripe-payment-request', 'security'); |
|
| 108 | 108 | |
| 109 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 110 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 109 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 110 | + define('WOOCOMMERCE_CART', true); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | WC()->cart->calculate_totals(); |
@@ -119,16 +119,16 @@ discard block |
||
| 119 | 119 | 'shipping_required' => WC()->cart->needs_shipping(), |
| 120 | 120 | 'order_data' => array( |
| 121 | 121 | 'total' => array( |
| 122 | - 'label' => __( 'Total', 'woocommerce-gateway-stripe' ), |
|
| 122 | + 'label' => __('Total', 'woocommerce-gateway-stripe'), |
|
| 123 | 123 | 'amount' => array( |
| 124 | - 'value' => max( 0, apply_filters( 'woocommerce_calculated_total', round( WC()->cart->cart_contents_total + WC()->cart->fee_total, WC()->cart->dp ), WC()->cart ) ), |
|
| 124 | + 'value' => max(0, apply_filters('woocommerce_calculated_total', round(WC()->cart->cart_contents_total + WC()->cart->fee_total, WC()->cart->dp), WC()->cart)), |
|
| 125 | 125 | 'currency' => $currency, |
| 126 | 126 | ), |
| 127 | 127 | ), |
| 128 | 128 | ), |
| 129 | 129 | ); |
| 130 | 130 | |
| 131 | - wp_send_json( $data ); |
|
| 131 | + wp_send_json($data); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @version 3.1.0 |
| 139 | 139 | * @param array $address |
| 140 | 140 | */ |
| 141 | - public function calculate_shipping( $address = array() ) { |
|
| 141 | + public function calculate_shipping($address = array()) { |
|
| 142 | 142 | $country = $address['country']; |
| 143 | 143 | $state = $address['state']; |
| 144 | 144 | $postcode = $address['postcode']; |
@@ -148,19 +148,19 @@ discard block |
||
| 148 | 148 | |
| 149 | 149 | WC()->shipping->reset_shipping(); |
| 150 | 150 | |
| 151 | - if ( $postcode && WC_Validation::is_postcode( $postcode, $country ) ) { |
|
| 152 | - $postcode = wc_format_postcode( $postcode, $country ); |
|
| 151 | + if ($postcode && WC_Validation::is_postcode($postcode, $country)) { |
|
| 152 | + $postcode = wc_format_postcode($postcode, $country); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if ( $country ) { |
|
| 156 | - WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
| 157 | - WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
| 155 | + if ($country) { |
|
| 156 | + WC()->customer->set_location($country, $state, $postcode, $city); |
|
| 157 | + WC()->customer->set_shipping_location($country, $state, $postcode, $city); |
|
| 158 | 158 | } else { |
| 159 | 159 | WC()->customer->set_to_base(); |
| 160 | 160 | WC()->customer->set_shipping_to_base(); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - WC()->customer->calculated_shipping( true ); |
|
| 163 | + WC()->customer->calculated_shipping(true); |
|
| 164 | 164 | |
| 165 | 165 | $packages = array(); |
| 166 | 166 | |
@@ -175,17 +175,17 @@ discard block |
||
| 175 | 175 | $packages[0]['destination']['address'] = $address_1; |
| 176 | 176 | $packages[0]['destination']['address_2'] = $address_2; |
| 177 | 177 | |
| 178 | - foreach ( WC()->cart->get_cart() as $item ) { |
|
| 179 | - if ( $item['data']->needs_shipping() ) { |
|
| 180 | - if ( isset( $item['line_total'] ) ) { |
|
| 178 | + foreach (WC()->cart->get_cart() as $item) { |
|
| 179 | + if ($item['data']->needs_shipping()) { |
|
| 180 | + if (isset($item['line_total'])) { |
|
| 181 | 181 | $packages[0]['contents_cost'] += $item['line_total']; |
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
|
| 186 | + $packages = apply_filters('woocommerce_cart_shipping_packages', $packages); |
|
| 187 | 187 | |
| 188 | - WC()->shipping->calculate_shipping( $packages ); |
|
| 188 | + WC()->shipping->calculate_shipping($packages); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -196,19 +196,19 @@ discard block |
||
| 196 | 196 | * @see WC_Shipping::get_packages(). |
| 197 | 197 | */ |
| 198 | 198 | public function ajax_get_shipping_options() { |
| 199 | - check_ajax_referer( 'wc-stripe-payment-request-shipping', 'security' ); |
|
| 199 | + check_ajax_referer('wc-stripe-payment-request-shipping', 'security'); |
|
| 200 | 200 | |
| 201 | 201 | // Set the shipping package. |
| 202 | - $posted = filter_input_array( INPUT_POST, array( |
|
| 202 | + $posted = filter_input_array(INPUT_POST, array( |
|
| 203 | 203 | 'country' => FILTER_SANITIZE_ENCODED, |
| 204 | 204 | 'state' => FILTER_SANITIZE_STRING, |
| 205 | 205 | 'postcode' => FILTER_SANITIZE_ENCODED, |
| 206 | 206 | 'city' => FILTER_SANITIZE_STRING, |
| 207 | 207 | 'address' => FILTER_SANITIZE_STRING, |
| 208 | 208 | 'address_2' => FILTER_SANITIZE_STRING, |
| 209 | - ) ); |
|
| 209 | + )); |
|
| 210 | 210 | |
| 211 | - $this->calculate_shipping( $posted ); |
|
| 211 | + $this->calculate_shipping($posted); |
|
| 212 | 212 | |
| 213 | 213 | // Set the shipping options. |
| 214 | 214 | $currency = get_woocommerce_currency(); |
@@ -216,13 +216,13 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | $packages = WC()->shipping->get_packages(); |
| 218 | 218 | |
| 219 | - if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
|
| 220 | - foreach ( $packages as $package_key => $package ) { |
|
| 221 | - if ( empty( $package['rates'] ) ) { |
|
| 219 | + if ( ! empty($packages) && WC()->customer->has_calculated_shipping()) { |
|
| 220 | + foreach ($packages as $package_key => $package) { |
|
| 221 | + if (empty($package['rates'])) { |
|
| 222 | 222 | break; |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - foreach ( $package['rates'] as $key => $rate ) { |
|
| 225 | + foreach ($package['rates'] as $key => $rate) { |
|
| 226 | 226 | $data[] = array( |
| 227 | 227 | 'id' => $rate->id, |
| 228 | 228 | 'label' => $rate->label, |
@@ -237,45 +237,45 @@ discard block |
||
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | // Auto select when have only one shipping method available. |
| 240 | - if ( 1 === count( $data ) ) { |
|
| 240 | + if (1 === count($data)) { |
|
| 241 | 241 | $data[0]['selected'] = true; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - wp_send_json( $data ); |
|
| 244 | + wp_send_json($data); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
| 248 | 248 | * Update shipping method. |
| 249 | 249 | */ |
| 250 | 250 | public function ajax_update_shipping_method() { |
| 251 | - check_ajax_referer( 'wc-stripe-update-shipping-method', 'security' ); |
|
| 251 | + check_ajax_referer('wc-stripe-update-shipping-method', 'security'); |
|
| 252 | 252 | |
| 253 | - if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
| 254 | - define( 'WOOCOMMERCE_CART', true ); |
|
| 253 | + if ( ! defined('WOOCOMMERCE_CART')) { |
|
| 254 | + define('WOOCOMMERCE_CART', true); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
|
| 258 | - $shipping_method = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
|
| 257 | + $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); |
|
| 258 | + $shipping_method = filter_input(INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 259 | 259 | |
| 260 | - if ( is_array( $shipping_method ) ) { |
|
| 261 | - foreach ( $shipping_method as $i => $value ) { |
|
| 262 | - $chosen_shipping_methods[ $i ] = wc_clean( $value ); |
|
| 260 | + if (is_array($shipping_method)) { |
|
| 261 | + foreach ($shipping_method as $i => $value) { |
|
| 262 | + $chosen_shipping_methods[$i] = wc_clean($value); |
|
| 263 | 263 | } |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
|
| 266 | + WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods); |
|
| 267 | 267 | |
| 268 | 268 | WC()->cart->calculate_totals(); |
| 269 | 269 | |
| 270 | 270 | // Send back the new cart total. |
| 271 | 271 | $currency = get_woocommerce_currency(); |
| 272 | - $tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
|
| 272 | + $tax_total = max(0, round(WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp)); |
|
| 273 | 273 | $data = array( |
| 274 | 274 | 'total' => WC()->cart->total, |
| 275 | 275 | ); |
| 276 | 276 | |
| 277 | 277 | // Include fees and taxes as displayItems. |
| 278 | - foreach ( WC()->cart->fees as $key => $fee ) { |
|
| 278 | + foreach (WC()->cart->fees as $key => $fee) { |
|
| 279 | 279 | $data['items'][] = array( |
| 280 | 280 | 'label' => $fee->name, |
| 281 | 281 | 'amount' => array( |
@@ -284,9 +284,9 @@ discard block |
||
| 284 | 284 | ), |
| 285 | 285 | ); |
| 286 | 286 | } |
| 287 | - if ( 0 < $tax_total ) { |
|
| 287 | + if (0 < $tax_total) { |
|
| 288 | 288 | $data['items'][] = array( |
| 289 | - 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
|
| 289 | + 'label' => __('Tax', 'woocommerce-gateway-stripe'), |
|
| 290 | 290 | 'amount' => array( |
| 291 | 291 | 'currency' => $currency, |
| 292 | 292 | 'value' => $tax_total, |
@@ -294,24 +294,24 @@ discard block |
||
| 294 | 294 | ); |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - wp_send_json( $data ); |
|
| 297 | + wp_send_json($data); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
| 301 | 301 | * Create order. |
| 302 | 302 | */ |
| 303 | 303 | public function ajax_create_order() { |
| 304 | - if ( WC()->cart->is_empty() ) { |
|
| 305 | - wp_send_json_error( __( 'Empty cart', 'woocommerce-gateway-stripe' ) ); |
|
| 304 | + if (WC()->cart->is_empty()) { |
|
| 305 | + wp_send_json_error(__('Empty cart', 'woocommerce-gateway-stripe')); |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | - if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
|
| 309 | - define( 'WOOCOMMERCE_CHECKOUT', true ); |
|
| 308 | + if ( ! defined('WOOCOMMERCE_CHECKOUT')) { |
|
| 309 | + define('WOOCOMMERCE_CHECKOUT', true); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | WC()->checkout()->process_checkout(); |
| 313 | 313 | |
| 314 | - die( 0 ); |
|
| 314 | + die(0); |
|
| 315 | 315 | } |
| 316 | 316 | } |
| 317 | 317 | |