@@ -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,21 +15,21 @@ discard block |
||
15 | 15 | public function __construct() { |
16 | 16 | parent::__construct(); |
17 | 17 | |
18 | - if ( class_exists( 'WC_Subscriptions_Order' ) ) { |
|
19 | - add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 ); |
|
20 | - add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 ); |
|
21 | - add_action( 'wcs_renewal_order_created', array( $this, 'delete_renewal_meta' ), 10 ); |
|
22 | - add_action( 'woocommerce_subscription_failing_payment_method_updated_stripe', array( $this, 'update_failing_payment_method' ), 10, 2 ); |
|
23 | - add_action( 'wc_stripe_cards_payment_fields', array( $this, 'display_update_subs_payment_checkout' ) ); |
|
24 | - add_action( 'wc_stripe_add_payment_method_' . $this->id . '_success', array( $this, 'handle_add_payment_method_success' ), 10, 2 ); |
|
18 | + if (class_exists('WC_Subscriptions_Order')) { |
|
19 | + add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); |
|
20 | + add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); |
|
21 | + add_action('wcs_renewal_order_created', array($this, 'delete_renewal_meta'), 10); |
|
22 | + add_action('woocommerce_subscription_failing_payment_method_updated_stripe', array($this, 'update_failing_payment_method'), 10, 2); |
|
23 | + add_action('wc_stripe_cards_payment_fields', array($this, 'display_update_subs_payment_checkout')); |
|
24 | + add_action('wc_stripe_add_payment_method_' . $this->id . '_success', array($this, 'handle_add_payment_method_success'), 10, 2); |
|
25 | 25 | |
26 | 26 | // display the credit card used for a subscription in the "My Subscriptions" table |
27 | - add_filter( 'woocommerce_my_subscriptions_payment_method', array( $this, 'maybe_render_subscription_payment_method' ), 10, 2 ); |
|
27 | + add_filter('woocommerce_my_subscriptions_payment_method', array($this, 'maybe_render_subscription_payment_method'), 10, 2); |
|
28 | 28 | |
29 | 29 | // allow store managers to manually set Stripe as the payment method on a subscription |
30 | - add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 ); |
|
31 | - add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 ); |
|
32 | - add_filter( 'wc_stripe_display_save_payment_method_checkbox', array( $this, 'maybe_hide_save_checkbox' ) ); |
|
30 | + add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); |
|
31 | + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); |
|
32 | + add_filter('wc_stripe_display_save_payment_method_checkbox', array($this, 'maybe_hide_save_checkbox')); |
|
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
@@ -40,8 +40,8 @@ discard block |
||
40 | 40 | * @since 4.0.0 |
41 | 41 | * @version 4.0.0 |
42 | 42 | */ |
43 | - public function maybe_hide_save_checkbox( $display_tokenization ) { |
|
44 | - if ( WC_Subscriptions_Cart::cart_contains_subscription() ) { |
|
43 | + public function maybe_hide_save_checkbox($display_tokenization) { |
|
44 | + if (WC_Subscriptions_Cart::cart_contains_subscription()) { |
|
45 | 45 | return false; |
46 | 46 | } |
47 | 47 | |
@@ -53,8 +53,8 @@ discard block |
||
53 | 53 | * @param int $order_id |
54 | 54 | * @return boolean |
55 | 55 | */ |
56 | - public function has_subscription( $order_id ) { |
|
57 | - return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) ); |
|
56 | + public function has_subscription($order_id) { |
|
57 | + return (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id))); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @return bool |
65 | 65 | */ |
66 | 66 | public function is_subs_change_payment() { |
67 | - return ( isset( $_GET['pay_for_order'] ) && isset( $_GET['change_payment_method'] ) ); |
|
67 | + return (isset($_GET['pay_for_order']) && isset($_GET['change_payment_method'])); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function display_update_subs_payment_checkout() { |
77 | 77 | if ( |
78 | - apply_filters( 'wc_stripe_display_update_subs_payment_method_card_checkbox', true ) && |
|
79 | - wcs_user_has_subscription( get_current_user_id(), '', 'active' ) && |
|
78 | + apply_filters('wc_stripe_display_update_subs_payment_method_card_checkbox', true) && |
|
79 | + wcs_user_has_subscription(get_current_user_id(), '', 'active') && |
|
80 | 80 | is_add_payment_method_page() |
81 | 81 | ) { |
82 | 82 | printf( |
@@ -84,8 +84,8 @@ discard block |
||
84 | 84 | <input id="wc-%1$s-update-subs-payment-method-card" name="wc-%1$s-update-subs-payment-method-card" type="checkbox" value="true" style="width:auto;" /> |
85 | 85 | <label for="wc-%1$s-update-subs-payment-method-card" style="display:inline;">%2$s</label> |
86 | 86 | </p>', |
87 | - esc_attr( $this->id ), |
|
88 | - esc_html( apply_filters( 'wc_stripe_save_to_subs_text', __( 'Update the Payment Method used for all of my active subscriptions (optional).', 'woocommerce-gateway-stripe' ) ) ) |
|
87 | + esc_attr($this->id), |
|
88 | + esc_html(apply_filters('wc_stripe_save_to_subs_text', __('Update the Payment Method used for all of my active subscriptions (optional).', 'woocommerce-gateway-stripe'))) |
|
89 | 89 | ); |
90 | 90 | } |
91 | 91 | } |
@@ -97,16 +97,16 @@ discard block |
||
97 | 97 | * @param string $source_id |
98 | 98 | * @param object $source_object |
99 | 99 | */ |
100 | - public function handle_add_payment_method_success( $source_id, $source_object ) { |
|
101 | - if ( isset( $_POST[ 'wc-' . $this->id . '-update-subs-payment-method-card' ] ) ) { |
|
100 | + public function handle_add_payment_method_success($source_id, $source_object) { |
|
101 | + if (isset($_POST['wc-' . $this->id . '-update-subs-payment-method-card'])) { |
|
102 | 102 | $all_subs = wcs_get_users_subscriptions(); |
103 | 103 | |
104 | - if ( ! empty( $all_subs ) ) { |
|
105 | - foreach ( $all_subs as $sub ) { |
|
106 | - if ( 'active' === $sub->get_status() ) { |
|
107 | - update_post_meta( $sub->get_id(), '_stripe_source_id', $source_id ); |
|
108 | - update_post_meta( $sub->get_id(), '_payment_method', $this->id ); |
|
109 | - update_post_meta( $sub->get_id(), '_payment_method_title', $this->method_title ); |
|
104 | + if ( ! empty($all_subs)) { |
|
105 | + foreach ($all_subs as $sub) { |
|
106 | + if ('active' === $sub->get_status()) { |
|
107 | + update_post_meta($sub->get_id(), '_stripe_source_id', $source_id); |
|
108 | + update_post_meta($sub->get_id(), '_payment_method', $this->id); |
|
109 | + update_post_meta($sub->get_id(), '_payment_method_title', $this->method_title); |
|
110 | 110 | } |
111 | 111 | } |
112 | 112 | } |
@@ -120,34 +120,34 @@ discard block |
||
120 | 120 | * @since 4.1.11 Remove 3DS check as it is not needed. |
121 | 121 | * @param int $order_id |
122 | 122 | */ |
123 | - public function change_subs_payment_method( $order_id ) { |
|
123 | + public function change_subs_payment_method($order_id) { |
|
124 | 124 | try { |
125 | - $subscription = wc_get_order( $order_id ); |
|
126 | - $prepared_source = $this->prepare_source( get_current_user_id(), true ); |
|
125 | + $subscription = wc_get_order($order_id); |
|
126 | + $prepared_source = $this->prepare_source(get_current_user_id(), true); |
|
127 | 127 | $source_object = $prepared_source->source_object; |
128 | 128 | |
129 | 129 | // Check if we don't allow prepaid credit cards. |
130 | - if ( ! apply_filters( 'wc_stripe_allow_prepaid_card', true ) && $this->is_prepaid_card( $source_object ) ) { |
|
131 | - $localized_message = __( 'Sorry, we\'re not accepting prepaid cards at this time. Your credit card has not been charge. Please try with alternative payment method.', 'woocommerce-gateway-stripe' ); |
|
132 | - throw new WC_Stripe_Exception( print_r( $source_object, true ), $localized_message ); |
|
130 | + if ( ! apply_filters('wc_stripe_allow_prepaid_card', true) && $this->is_prepaid_card($source_object)) { |
|
131 | + $localized_message = __('Sorry, we\'re not accepting prepaid cards at this time. Your credit card has not been charge. Please try with alternative payment method.', 'woocommerce-gateway-stripe'); |
|
132 | + throw new WC_Stripe_Exception(print_r($source_object, true), $localized_message); |
|
133 | 133 | } |
134 | 134 | |
135 | - if ( empty( $prepared_source->source ) ) { |
|
136 | - $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); |
|
137 | - throw new WC_Stripe_Exception( print_r( $prepared_source, true ), $localized_message ); |
|
135 | + if (empty($prepared_source->source)) { |
|
136 | + $localized_message = __('Payment processing failed. Please retry.', 'woocommerce-gateway-stripe'); |
|
137 | + throw new WC_Stripe_Exception(print_r($prepared_source, true), $localized_message); |
|
138 | 138 | } |
139 | 139 | |
140 | - $this->save_source_to_order( $subscription, $prepared_source ); |
|
140 | + $this->save_source_to_order($subscription, $prepared_source); |
|
141 | 141 | |
142 | - do_action( 'wc_stripe_change_subs_payment_method_success', $prepared_source->source, $prepared_source ); |
|
142 | + do_action('wc_stripe_change_subs_payment_method_success', $prepared_source->source, $prepared_source); |
|
143 | 143 | |
144 | 144 | return array( |
145 | 145 | 'result' => 'success', |
146 | - 'redirect' => $this->get_return_url( $subscription ), |
|
146 | + 'redirect' => $this->get_return_url($subscription), |
|
147 | 147 | ); |
148 | - } catch ( WC_Stripe_Exception $e ) { |
|
149 | - wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
|
150 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
148 | + } catch (WC_Stripe_Exception $e) { |
|
149 | + wc_add_notice($e->getLocalizedMessage(), 'error'); |
|
150 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
151 | 151 | } |
152 | 152 | } |
153 | 153 | |
@@ -156,16 +156,16 @@ discard block |
||
156 | 156 | * @param int $order_id |
157 | 157 | * @return array |
158 | 158 | */ |
159 | - public function process_payment( $order_id, $retry = true, $force_save_source = false, $previous_error = false ) { |
|
160 | - if ( $this->has_subscription( $order_id ) ) { |
|
161 | - if ( $this->is_subs_change_payment() ) { |
|
162 | - return $this->change_subs_payment_method( $order_id ); |
|
159 | + public function process_payment($order_id, $retry = true, $force_save_source = false, $previous_error = false) { |
|
160 | + if ($this->has_subscription($order_id)) { |
|
161 | + if ($this->is_subs_change_payment()) { |
|
162 | + return $this->change_subs_payment_method($order_id); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | // Regular payment with force customer enabled |
166 | - return parent::process_payment( $order_id, $retry, true, $previous_error ); |
|
166 | + return parent::process_payment($order_id, $retry, true, $previous_error); |
|
167 | 167 | } else { |
168 | - return parent::process_payment( $order_id, $retry, $force_save_source, $previous_error ); |
|
168 | + return parent::process_payment($order_id, $retry, $force_save_source, $previous_error); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | * @param $amount_to_charge float The amount to charge. |
176 | 176 | * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
177 | 177 | */ |
178 | - public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
179 | - $this->process_subscription_payment( $amount_to_charge, $renewal_order, true, false ); |
|
178 | + public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
179 | + $this->process_subscription_payment($amount_to_charge, $renewal_order, true, false); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
@@ -190,86 +190,86 @@ discard block |
||
190 | 190 | * @param bool $retry Should we retry the process? |
191 | 191 | * @param object $previous_error |
192 | 192 | */ |
193 | - public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) { |
|
193 | + public function process_subscription_payment($amount = 0.0, $renewal_order, $retry = true, $previous_error) { |
|
194 | 194 | try { |
195 | - if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
195 | + if ($amount * 100 < WC_Stripe_Helper::get_minimum_amount()) { |
|
196 | 196 | /* translators: minimum amount */ |
197 | - 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_Helper::get_minimum_amount() / 100 ) ) ); |
|
197 | + 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_Helper::get_minimum_amount() / 100))); |
|
198 | 198 | } |
199 | 199 | |
200 | - $order_id = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $renewal_order->id : $renewal_order->get_id(); |
|
200 | + $order_id = WC_Stripe_Helper::is_wc_lt('3.0') ? $renewal_order->id : $renewal_order->get_id(); |
|
201 | 201 | |
202 | 202 | // Get source from order |
203 | - $prepared_source = $this->prepare_order_source( $renewal_order ); |
|
203 | + $prepared_source = $this->prepare_order_source($renewal_order); |
|
204 | 204 | $source_object = $prepared_source->source_object; |
205 | 205 | |
206 | - if ( ! $prepared_source->customer ) { |
|
207 | - return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
206 | + if ( ! $prepared_source->customer) { |
|
207 | + return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
208 | 208 | } |
209 | 209 | |
210 | - WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
210 | + WC_Stripe_Logger::log("Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}"); |
|
211 | 211 | |
212 | 212 | /* If we're doing a retry and source is chargeable, we need to pass |
213 | 213 | * a different idempotency key and retry for success. |
214 | 214 | */ |
215 | - if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) { |
|
216 | - add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 ); |
|
215 | + if (is_object($source_object) && empty($source_object->error) && $this->need_update_idempotency_key($source_object, $previous_error)) { |
|
216 | + add_filter('wc_stripe_idempotency_key', array($this, 'change_idempotency_key'), 10, 2); |
|
217 | 217 | } |
218 | 218 | |
219 | - if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) { |
|
219 | + if (($this->is_no_such_source_error($previous_error) || $this->is_no_linked_source_error($previous_error)) && apply_filters('wc_stripe_use_default_customer_source', true)) { |
|
220 | 220 | // Passing empty source will charge customer default. |
221 | 221 | $prepared_source->source = ''; |
222 | 222 | } |
223 | 223 | |
224 | - $request = $this->generate_payment_request( $renewal_order, $prepared_source ); |
|
224 | + $request = $this->generate_payment_request($renewal_order, $prepared_source); |
|
225 | 225 | $request['capture'] = 'true'; |
226 | - $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
227 | - $response = WC_Stripe_API::request( $request ); |
|
226 | + $request['amount'] = WC_Stripe_Helper::get_stripe_amount($amount, $request['currency']); |
|
227 | + $response = WC_Stripe_API::request($request); |
|
228 | 228 | |
229 | - if ( ! empty( $response->error ) ) { |
|
229 | + if ( ! empty($response->error)) { |
|
230 | 230 | // We want to retry. |
231 | - if ( $this->is_retryable_error( $response->error ) ) { |
|
232 | - if ( $retry ) { |
|
231 | + if ($this->is_retryable_error($response->error)) { |
|
232 | + if ($retry) { |
|
233 | 233 | // Don't do anymore retries after this. |
234 | - if ( 5 <= $this->retry_interval ) { |
|
235 | - return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error ); |
|
234 | + if (5 <= $this->retry_interval) { |
|
235 | + return $this->process_subscription_payment($amount, $renewal_order, false, $response->error); |
|
236 | 236 | } |
237 | 237 | |
238 | - sleep( $this->retry_interval ); |
|
238 | + sleep($this->retry_interval); |
|
239 | 239 | |
240 | 240 | $this->retry_interval++; |
241 | 241 | |
242 | - return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error ); |
|
242 | + return $this->process_subscription_payment($amount, $renewal_order, true, $response->error); |
|
243 | 243 | } else { |
244 | - $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' ); |
|
245 | - $renewal_order->add_order_note( $localized_message ); |
|
246 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
244 | + $localized_message = __('Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe'); |
|
245 | + $renewal_order->add_order_note($localized_message); |
|
246 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
247 | 247 | } |
248 | 248 | } |
249 | 249 | |
250 | 250 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
251 | 251 | |
252 | - if ( 'card_error' === $response->error->type ) { |
|
253 | - $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
252 | + if ('card_error' === $response->error->type) { |
|
253 | + $localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message; |
|
254 | 254 | } else { |
255 | - $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
255 | + $localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message; |
|
256 | 256 | } |
257 | 257 | |
258 | - $renewal_order->add_order_note( $localized_message ); |
|
258 | + $renewal_order->add_order_note($localized_message); |
|
259 | 259 | |
260 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
260 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
261 | 261 | } |
262 | 262 | |
263 | - do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order ); |
|
263 | + do_action('wc_gateway_stripe_process_payment', $response, $renewal_order); |
|
264 | 264 | |
265 | - $this->process_response( $response, $renewal_order ); |
|
266 | - } catch ( WC_Stripe_Exception $e ) { |
|
267 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
265 | + $this->process_response($response, $renewal_order); |
|
266 | + } catch (WC_Stripe_Exception $e) { |
|
267 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
268 | 268 | |
269 | - do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); |
|
269 | + do_action('wc_gateway_stripe_process_payment_error', $e, $renewal_order); |
|
270 | 270 | |
271 | 271 | /* translators: error message */ |
272 | - $renewal_order->update_status( 'failed' ); |
|
272 | + $renewal_order->update_status('failed'); |
|
273 | 273 | } |
274 | 274 | } |
275 | 275 | |
@@ -279,24 +279,24 @@ discard block |
||
279 | 279 | * @since 3.1.0 |
280 | 280 | * @version 4.0.0 |
281 | 281 | */ |
282 | - public function save_source_to_order( $order, $source ) { |
|
283 | - parent::save_source_to_order( $order, $source ); |
|
282 | + public function save_source_to_order($order, $source) { |
|
283 | + parent::save_source_to_order($order, $source); |
|
284 | 284 | |
285 | - $order_id = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $order->id : $order->get_id(); |
|
285 | + $order_id = WC_Stripe_Helper::is_wc_lt('3.0') ? $order->id : $order->get_id(); |
|
286 | 286 | |
287 | 287 | // Also store it on the subscriptions being purchased or paid for in the order |
288 | - if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) { |
|
289 | - $subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
|
290 | - } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) { |
|
291 | - $subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); |
|
288 | + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order_id)) { |
|
289 | + $subscriptions = wcs_get_subscriptions_for_order($order_id); |
|
290 | + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order_id)) { |
|
291 | + $subscriptions = wcs_get_subscriptions_for_renewal_order($order_id); |
|
292 | 292 | } else { |
293 | 293 | $subscriptions = array(); |
294 | 294 | } |
295 | 295 | |
296 | - foreach ( $subscriptions as $subscription ) { |
|
297 | - $subscription_id = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->id : $subscription->get_id(); |
|
298 | - update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer ); |
|
299 | - update_post_meta( $subscription_id, '_stripe_source_id', $source->source ); |
|
296 | + foreach ($subscriptions as $subscription) { |
|
297 | + $subscription_id = WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->id : $subscription->get_id(); |
|
298 | + update_post_meta($subscription_id, '_stripe_customer_id', $source->customer); |
|
299 | + update_post_meta($subscription_id, '_stripe_source_id', $source->source); |
|
300 | 300 | } |
301 | 301 | } |
302 | 302 | |
@@ -304,26 +304,26 @@ discard block |
||
304 | 304 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
305 | 305 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
306 | 306 | */ |
307 | - public function delete_resubscribe_meta( $resubscribe_order ) { |
|
308 | - delete_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_customer_id' ); |
|
309 | - delete_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_source_id' ); |
|
307 | + public function delete_resubscribe_meta($resubscribe_order) { |
|
308 | + delete_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_customer_id'); |
|
309 | + delete_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_source_id'); |
|
310 | 310 | // For BW compat will remove in future |
311 | - delete_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_card_id' ); |
|
311 | + delete_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_card_id'); |
|
312 | 312 | // delete payment intent ID |
313 | - delete_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_intent_id' ); |
|
314 | - $this->delete_renewal_meta( $resubscribe_order ); |
|
313 | + delete_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_intent_id'); |
|
314 | + $this->delete_renewal_meta($resubscribe_order); |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | /** |
318 | 318 | * Don't transfer Stripe fee/ID meta to renewal orders. |
319 | 319 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
320 | 320 | */ |
321 | - public function delete_renewal_meta( $renewal_order ) { |
|
322 | - WC_Stripe_Helper::delete_stripe_fee( $renewal_order ); |
|
323 | - WC_Stripe_Helper::delete_stripe_net( $renewal_order ); |
|
321 | + public function delete_renewal_meta($renewal_order) { |
|
322 | + WC_Stripe_Helper::delete_stripe_fee($renewal_order); |
|
323 | + WC_Stripe_Helper::delete_stripe_net($renewal_order); |
|
324 | 324 | |
325 | 325 | // delete payment intent ID |
326 | - delete_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $renewal_order->id : $renewal_order->get_id() ), '_stripe_intent_id' ); |
|
326 | + delete_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $renewal_order->id : $renewal_order->get_id()), '_stripe_intent_id'); |
|
327 | 327 | |
328 | 328 | return $renewal_order; |
329 | 329 | } |
@@ -337,14 +337,14 @@ discard block |
||
337 | 337 | * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
338 | 338 | * @return void |
339 | 339 | */ |
340 | - public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
341 | - if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) { |
|
342 | - update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
343 | - update_post_meta( $subscription->id, '_stripe_source_id', $renewal_order->stripe_source_id ); |
|
340 | + public function update_failing_payment_method($subscription, $renewal_order) { |
|
341 | + if (WC_Stripe_Helper::is_wc_lt('3.0')) { |
|
342 | + update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
343 | + update_post_meta($subscription->id, '_stripe_source_id', $renewal_order->stripe_source_id); |
|
344 | 344 | |
345 | 345 | } else { |
346 | - update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); |
|
347 | - update_post_meta( $subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) ); |
|
346 | + update_post_meta($subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta('_stripe_customer_id', true)); |
|
347 | + update_post_meta($subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta('_stripe_source_id', true)); |
|
348 | 348 | } |
349 | 349 | } |
350 | 350 | |
@@ -357,23 +357,23 @@ discard block |
||
357 | 357 | * @param WC_Subscription $subscription An instance of a subscription object |
358 | 358 | * @return array |
359 | 359 | */ |
360 | - public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
361 | - $subscription_id = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->id : $subscription->get_id(); |
|
362 | - $source_id = get_post_meta( $subscription_id, '_stripe_source_id', true ); |
|
360 | + public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
361 | + $subscription_id = WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->id : $subscription->get_id(); |
|
362 | + $source_id = get_post_meta($subscription_id, '_stripe_source_id', true); |
|
363 | 363 | |
364 | 364 | // For BW compat will remove in future. |
365 | - if ( empty( $source_id ) ) { |
|
366 | - $source_id = get_post_meta( $subscription_id, '_stripe_card_id', true ); |
|
365 | + if (empty($source_id)) { |
|
366 | + $source_id = get_post_meta($subscription_id, '_stripe_card_id', true); |
|
367 | 367 | |
368 | 368 | // Take this opportunity to update the key name. |
369 | - update_post_meta( $subscription_id, '_stripe_source_id', $source_id ); |
|
370 | - delete_post_meta( $subscription_id, '_stripe_card_id', $source_id ); |
|
369 | + update_post_meta($subscription_id, '_stripe_source_id', $source_id); |
|
370 | + delete_post_meta($subscription_id, '_stripe_card_id', $source_id); |
|
371 | 371 | } |
372 | 372 | |
373 | - $payment_meta[ $this->id ] = array( |
|
373 | + $payment_meta[$this->id] = array( |
|
374 | 374 | 'post_meta' => array( |
375 | 375 | '_stripe_customer_id' => array( |
376 | - 'value' => get_post_meta( $subscription_id, '_stripe_customer_id', true ), |
|
376 | + 'value' => get_post_meta($subscription_id, '_stripe_customer_id', true), |
|
377 | 377 | 'label' => 'Stripe Customer ID', |
378 | 378 | ), |
379 | 379 | '_stripe_source_id' => array( |
@@ -396,22 +396,22 @@ discard block |
||
396 | 396 | * @param array $payment_meta associative array of meta data required for automatic payments |
397 | 397 | * @return array |
398 | 398 | */ |
399 | - public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
400 | - if ( $this->id === $payment_method_id ) { |
|
399 | + public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
400 | + if ($this->id === $payment_method_id) { |
|
401 | 401 | |
402 | - if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
403 | - throw new Exception( __( 'A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe' ) ); |
|
404 | - } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
405 | - throw new Exception( __( 'Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe' ) ); |
|
402 | + if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
403 | + throw new Exception(__('A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe')); |
|
404 | + } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
405 | + throw new Exception(__('Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe')); |
|
406 | 406 | } |
407 | 407 | |
408 | 408 | if ( |
409 | - ( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
410 | - && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'card_' ) ) |
|
411 | - && ( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
412 | - && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'src_' ) ) ) { |
|
409 | + ( ! empty($payment_meta['post_meta']['_stripe_source_id']['value']) |
|
410 | + && 0 !== strpos($payment_meta['post_meta']['_stripe_source_id']['value'], 'card_')) |
|
411 | + && ( ! empty($payment_meta['post_meta']['_stripe_source_id']['value']) |
|
412 | + && 0 !== strpos($payment_meta['post_meta']['_stripe_source_id']['value'], 'src_')) ) { |
|
413 | 413 | |
414 | - throw new Exception( __( 'Invalid source ID. A valid source "Stripe Source ID" must begin with "src_" or "card_".', 'woocommerce-gateway-stripe' ) ); |
|
414 | + throw new Exception(__('Invalid source ID. A valid source "Stripe Source ID" must begin with "src_" or "card_".', 'woocommerce-gateway-stripe')); |
|
415 | 415 | } |
416 | 416 | } |
417 | 417 | } |
@@ -424,75 +424,75 @@ discard block |
||
424 | 424 | * @param WC_Subscription $subscription the subscription details |
425 | 425 | * @return string the subscription payment method |
426 | 426 | */ |
427 | - public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
428 | - $customer_user = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->customer_user : $subscription->get_customer_id(); |
|
427 | + public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
428 | + $customer_user = WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->customer_user : $subscription->get_customer_id(); |
|
429 | 429 | |
430 | 430 | // bail for other payment methods |
431 | - if ( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->payment_method : $subscription->get_payment_method() ) !== $this->id || ! $customer_user ) { |
|
431 | + if ((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->payment_method : $subscription->get_payment_method()) !== $this->id || ! $customer_user) { |
|
432 | 432 | return $payment_method_to_display; |
433 | 433 | } |
434 | 434 | |
435 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->id : $subscription->get_id() ), '_stripe_source_id', true ); |
|
435 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->id : $subscription->get_id()), '_stripe_source_id', true); |
|
436 | 436 | |
437 | 437 | // For BW compat will remove in future. |
438 | - if ( empty( $stripe_source_id ) ) { |
|
439 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
438 | + if (empty($stripe_source_id)) { |
|
439 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true); |
|
440 | 440 | |
441 | 441 | // Take this opportunity to update the key name. |
442 | - WC_Stripe_Helper::is_wc_lt( '3.0' ) ? update_post_meta( $subscription->id, '_stripe_source_id', $stripe_source_id ) : update_post_meta( $subscription->get_id(), '_stripe_source_id', $stripe_source_id ); |
|
442 | + WC_Stripe_Helper::is_wc_lt('3.0') ? update_post_meta($subscription->id, '_stripe_source_id', $stripe_source_id) : update_post_meta($subscription->get_id(), '_stripe_source_id', $stripe_source_id); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | $stripe_customer = new WC_Stripe_Customer(); |
446 | - $stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ); |
|
446 | + $stripe_customer_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true); |
|
447 | 447 | |
448 | 448 | // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
449 | - if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
449 | + if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
450 | 450 | $user_id = $customer_user; |
451 | - $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
452 | - $stripe_source_id = get_user_meta( $user_id, '_stripe_source_id', true ); |
|
451 | + $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
452 | + $stripe_source_id = get_user_meta($user_id, '_stripe_source_id', true); |
|
453 | 453 | |
454 | 454 | // For BW compat will remove in future. |
455 | - if ( empty( $stripe_source_id ) ) { |
|
456 | - $stripe_source_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
455 | + if (empty($stripe_source_id)) { |
|
456 | + $stripe_source_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
457 | 457 | |
458 | 458 | // Take this opportunity to update the key name. |
459 | - update_user_meta( $user_id, '_stripe_source_id', $stripe_source_id ); |
|
459 | + update_user_meta($user_id, '_stripe_source_id', $stripe_source_id); |
|
460 | 460 | } |
461 | 461 | } |
462 | 462 | |
463 | 463 | // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
464 | - if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
465 | - $stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_customer_id', true ); |
|
466 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_source_id', true ); |
|
464 | + if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
465 | + $stripe_customer_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_customer_id', true); |
|
466 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_source_id', true); |
|
467 | 467 | |
468 | 468 | // For BW compat will remove in future. |
469 | - if ( empty( $stripe_source_id ) ) { |
|
470 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_card_id', true ); |
|
469 | + if (empty($stripe_source_id)) { |
|
470 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_wc_lt('3.0') ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_card_id', true); |
|
471 | 471 | |
472 | 472 | // Take this opportunity to update the key name. |
473 | - WC_Stripe_Helper::is_wc_lt( '3.0' ) ? update_post_meta( $subscription->order->id, '_stripe_source_id', $stripe_source_id ) : update_post_meta( $subscription->get_parent_id(), '_stripe_source_id', $stripe_source_id ); |
|
473 | + WC_Stripe_Helper::is_wc_lt('3.0') ? update_post_meta($subscription->order->id, '_stripe_source_id', $stripe_source_id) : update_post_meta($subscription->get_parent_id(), '_stripe_source_id', $stripe_source_id); |
|
474 | 474 | } |
475 | 475 | } |
476 | 476 | |
477 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
477 | + $stripe_customer->set_id($stripe_customer_id); |
|
478 | 478 | |
479 | 479 | $sources = $stripe_customer->get_sources(); |
480 | - $payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' ); |
|
480 | + $payment_method_to_display = __('N/A', 'woocommerce-gateway-stripe'); |
|
481 | 481 | |
482 | - if ( $sources ) { |
|
482 | + if ($sources) { |
|
483 | 483 | $card = false; |
484 | 484 | |
485 | - foreach ( $sources as $source ) { |
|
486 | - if ( isset( $source->type ) && 'card' === $source->type ) { |
|
485 | + foreach ($sources as $source) { |
|
486 | + if (isset($source->type) && 'card' === $source->type) { |
|
487 | 487 | $card = $source->card; |
488 | - } elseif ( isset( $source->object ) && 'card' === $source->object ) { |
|
488 | + } elseif (isset($source->object) && 'card' === $source->object) { |
|
489 | 489 | $card = $source; |
490 | 490 | } |
491 | 491 | |
492 | - if ( $source->id === $stripe_source_id ) { |
|
493 | - if ( $card ) { |
|
492 | + if ($source->id === $stripe_source_id) { |
|
493 | + if ($card) { |
|
494 | 494 | /* translators: 1) card brand 2) last 4 digits */ |
495 | - $payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $card->brand ) ? $card->brand : __( 'N/A', 'woocommerce-gateway-stripe' ) ), $card->last4 ); |
|
495 | + $payment_method_to_display = sprintf(__('Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe'), (isset($card->brand) ? $card->brand : __('N/A', 'woocommerce-gateway-stripe')), $card->last4); |
|
496 | 496 | } |
497 | 497 | |
498 | 498 | break; |