@@ -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,19 +15,19 @@ 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 ); |
|
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 | 23 | |
24 | 24 | // display the credit card used for a subscription in the "My Subscriptions" table |
25 | - add_filter( 'woocommerce_my_subscriptions_payment_method', array( $this, 'maybe_render_subscription_payment_method' ), 10, 2 ); |
|
25 | + add_filter('woocommerce_my_subscriptions_payment_method', array($this, 'maybe_render_subscription_payment_method'), 10, 2); |
|
26 | 26 | |
27 | 27 | // allow store managers to manually set Stripe as the payment method on a subscription |
28 | - add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 ); |
|
29 | - add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 ); |
|
30 | - add_filter( 'wc_stripe_display_save_payment_method_checkbox', array( $this, 'maybe_hide_save_checkbox' ) ); |
|
28 | + add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); |
|
29 | + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); |
|
30 | + add_filter('wc_stripe_display_save_payment_method_checkbox', array($this, 'maybe_hide_save_checkbox')); |
|
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | * @since 4.0.0 |
48 | 48 | * @version 4.0.0 |
49 | 49 | */ |
50 | - public function maybe_hide_save_checkbox( $display_tokenization ) { |
|
51 | - if ( WC_Subscriptions_Cart::cart_contains_subscription() ) { |
|
50 | + public function maybe_hide_save_checkbox($display_tokenization) { |
|
51 | + if (WC_Subscriptions_Cart::cart_contains_subscription()) { |
|
52 | 52 | return false; |
53 | 53 | } |
54 | 54 | |
@@ -60,8 +60,8 @@ discard block |
||
60 | 60 | * @param int $order_id |
61 | 61 | * @return boolean |
62 | 62 | */ |
63 | - public function has_subscription( $order_id ) { |
|
64 | - return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) ); |
|
63 | + public function has_subscription($order_id) { |
|
64 | + return (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id))); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @return bool |
72 | 72 | */ |
73 | 73 | public function is_subs_change_payment() { |
74 | - return ( isset( $_GET['pay_for_order'] ) && isset( $_GET['change_payment_method'] ) ); |
|
74 | + return (isset($_GET['pay_for_order']) && isset($_GET['change_payment_method'])); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -80,24 +80,24 @@ discard block |
||
80 | 80 | * @since 4.0.4 |
81 | 81 | * @param int $order_id |
82 | 82 | */ |
83 | - public function change_subs_payment_method( $order_id ) { |
|
83 | + public function change_subs_payment_method($order_id) { |
|
84 | 84 | try { |
85 | - $subscription = wc_get_order( $order_id ); |
|
86 | - $prepared_source = $this->prepare_source( get_current_user_id(), true ); |
|
85 | + $subscription = wc_get_order($order_id); |
|
86 | + $prepared_source = $this->prepare_source(get_current_user_id(), true); |
|
87 | 87 | $source_object = $prepared_source->source_object; |
88 | 88 | |
89 | 89 | // Check if we don't allow prepaid credit cards. |
90 | - if ( ! apply_filters( 'wc_stripe_allow_prepaid_card', true ) && $this->is_prepaid_card( $source_object ) ) { |
|
91 | - $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' ); |
|
92 | - throw new WC_Stripe_Exception( print_r( $source_object, true ), $localized_message ); |
|
90 | + if ( ! apply_filters('wc_stripe_allow_prepaid_card', true) && $this->is_prepaid_card($source_object)) { |
|
91 | + $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'); |
|
92 | + throw new WC_Stripe_Exception(print_r($source_object, true), $localized_message); |
|
93 | 93 | } |
94 | 94 | |
95 | - if ( empty( $prepared_source->source ) ) { |
|
96 | - $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); |
|
97 | - throw new WC_Stripe_Exception( print_r( $prepared_source, true ), $localized_message ); |
|
95 | + if (empty($prepared_source->source)) { |
|
96 | + $localized_message = __('Payment processing failed. Please retry.', 'woocommerce-gateway-stripe'); |
|
97 | + throw new WC_Stripe_Exception(print_r($prepared_source, true), $localized_message); |
|
98 | 98 | } |
99 | 99 | |
100 | - $this->save_source_to_order( $subscription, $prepared_source ); |
|
100 | + $this->save_source_to_order($subscription, $prepared_source); |
|
101 | 101 | |
102 | 102 | /* |
103 | 103 | * Check if card 3DS is required or optional with 3DS setting. |
@@ -106,41 +106,41 @@ discard block |
||
106 | 106 | * Note that if we need to save source, the original source must be first |
107 | 107 | * attached to a customer in Stripe before it can be charged. |
108 | 108 | */ |
109 | - if ( $this->is_3ds_required( $source_object ) ) { |
|
109 | + if ($this->is_3ds_required($source_object)) { |
|
110 | 110 | $order = $subscription->get_parent(); |
111 | - $response = $this->create_3ds_source( $order, $source_object, $subscription->get_view_order_url() ); |
|
111 | + $response = $this->create_3ds_source($order, $source_object, $subscription->get_view_order_url()); |
|
112 | 112 | |
113 | - if ( ! empty( $response->error ) ) { |
|
113 | + if ( ! empty($response->error)) { |
|
114 | 114 | $localized_message = $response->error->message; |
115 | 115 | |
116 | - $order->add_order_note( $localized_message ); |
|
116 | + $order->add_order_note($localized_message); |
|
117 | 117 | |
118 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
118 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | // Update order meta with 3DS source. |
122 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
123 | - update_post_meta( $order_id, '_stripe_source_id', $response->id ); |
|
122 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
123 | + update_post_meta($order_id, '_stripe_source_id', $response->id); |
|
124 | 124 | } else { |
125 | - $subscription->update_meta_data( '_stripe_source_id', $response->id ); |
|
125 | + $subscription->update_meta_data('_stripe_source_id', $response->id); |
|
126 | 126 | $subscription->save(); |
127 | 127 | } |
128 | 128 | |
129 | - WC_Stripe_Logger::log( 'Info: Redirecting to 3DS...' ); |
|
129 | + WC_Stripe_Logger::log('Info: Redirecting to 3DS...'); |
|
130 | 130 | |
131 | 131 | return array( |
132 | 132 | 'result' => 'success', |
133 | - 'redirect' => esc_url_raw( $response->redirect->url ), |
|
133 | + 'redirect' => esc_url_raw($response->redirect->url), |
|
134 | 134 | ); |
135 | 135 | } |
136 | 136 | |
137 | 137 | return array( |
138 | 138 | 'result' => 'success', |
139 | - 'redirect' => $this->get_return_url( $subscription ), |
|
139 | + 'redirect' => $this->get_return_url($subscription), |
|
140 | 140 | ); |
141 | - } catch ( WC_Stripe_Exception $e ) { |
|
142 | - wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
|
143 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
141 | + } catch (WC_Stripe_Exception $e) { |
|
142 | + wc_add_notice($e->getLocalizedMessage(), 'error'); |
|
143 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
@@ -149,16 +149,16 @@ discard block |
||
149 | 149 | * @param int $order_id |
150 | 150 | * @return array |
151 | 151 | */ |
152 | - public function process_payment( $order_id, $retry = true, $force_save_source = false, $previous_error = false ) { |
|
153 | - if ( $this->has_subscription( $order_id ) ) { |
|
154 | - if ( $this->is_subs_change_payment() ) { |
|
155 | - return $this->change_subs_payment_method( $order_id ); |
|
152 | + public function process_payment($order_id, $retry = true, $force_save_source = false, $previous_error = false) { |
|
153 | + if ($this->has_subscription($order_id)) { |
|
154 | + if ($this->is_subs_change_payment()) { |
|
155 | + return $this->change_subs_payment_method($order_id); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | // Regular payment with force customer enabled |
159 | - return parent::process_payment( $order_id, $retry, true, $previous_error ); |
|
159 | + return parent::process_payment($order_id, $retry, true, $previous_error); |
|
160 | 160 | } else { |
161 | - return parent::process_payment( $order_id, $retry, $force_save_source, $previous_error ); |
|
161 | + return parent::process_payment($order_id, $retry, $force_save_source, $previous_error); |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | |
@@ -168,8 +168,8 @@ discard block |
||
168 | 168 | * @param $amount_to_charge float The amount to charge. |
169 | 169 | * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
170 | 170 | */ |
171 | - public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
172 | - $this->process_subscription_payment( $amount_to_charge, $renewal_order, true, false ); |
|
171 | + public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
172 | + $this->process_subscription_payment($amount_to_charge, $renewal_order, true, false); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | /** |
@@ -183,86 +183,86 @@ discard block |
||
183 | 183 | * @param bool $retry Should we retry the process? |
184 | 184 | * @param object $previous_error |
185 | 185 | */ |
186 | - public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) { |
|
186 | + public function process_subscription_payment($amount = 0.0, $renewal_order, $retry = true, $previous_error) { |
|
187 | 187 | try { |
188 | - if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
188 | + if ($amount * 100 < WC_Stripe_Helper::get_minimum_amount()) { |
|
189 | 189 | /* translators: minimum amount */ |
190 | - 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 ) ) ); |
|
190 | + 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))); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | $order_id = WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id(); |
194 | 194 | |
195 | 195 | // Get source from order |
196 | - $prepared_source = $this->prepare_order_source( $renewal_order ); |
|
196 | + $prepared_source = $this->prepare_order_source($renewal_order); |
|
197 | 197 | $source_object = $prepared_source->source_object; |
198 | 198 | |
199 | - if ( ! $prepared_source->customer ) { |
|
200 | - return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
199 | + if ( ! $prepared_source->customer) { |
|
200 | + return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
201 | 201 | } |
202 | 202 | |
203 | - WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
203 | + WC_Stripe_Logger::log("Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}"); |
|
204 | 204 | |
205 | 205 | /* If we're doing a retry and source is chargeable, we need to pass |
206 | 206 | * a different idempotency key and retry for success. |
207 | 207 | */ |
208 | - if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) { |
|
209 | - add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 ); |
|
208 | + if (is_object($source_object) && empty($source_object->error) && $this->need_update_idempotency_key($source_object, $previous_error)) { |
|
209 | + add_filter('wc_stripe_idempotency_key', array($this, 'change_idempotency_key'), 10, 2); |
|
210 | 210 | } |
211 | 211 | |
212 | - 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 ) ) { |
|
212 | + 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)) { |
|
213 | 213 | // Passing empty source will charge customer default. |
214 | 214 | $prepared_source->source = ''; |
215 | 215 | } |
216 | 216 | |
217 | - $request = $this->generate_payment_request( $renewal_order, $prepared_source ); |
|
217 | + $request = $this->generate_payment_request($renewal_order, $prepared_source); |
|
218 | 218 | $request['capture'] = 'true'; |
219 | - $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
220 | - $response = WC_Stripe_API::request( $request ); |
|
219 | + $request['amount'] = WC_Stripe_Helper::get_stripe_amount($amount, $request['currency']); |
|
220 | + $response = WC_Stripe_API::request($request); |
|
221 | 221 | |
222 | - if ( ! empty( $response->error ) ) { |
|
222 | + if ( ! empty($response->error)) { |
|
223 | 223 | // We want to retry. |
224 | - if ( $this->is_retryable_error( $response->error ) ) { |
|
225 | - if ( $retry ) { |
|
224 | + if ($this->is_retryable_error($response->error)) { |
|
225 | + if ($retry) { |
|
226 | 226 | // Don't do anymore retries after this. |
227 | - if ( 5 <= $this->retry_interval ) { |
|
228 | - return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error ); |
|
227 | + if (5 <= $this->retry_interval) { |
|
228 | + return $this->process_subscription_payment($amount, $renewal_order, false, $response->error); |
|
229 | 229 | } |
230 | 230 | |
231 | - sleep( $this->retry_interval ); |
|
231 | + sleep($this->retry_interval); |
|
232 | 232 | |
233 | 233 | $this->retry_interval++; |
234 | 234 | |
235 | - return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error ); |
|
235 | + return $this->process_subscription_payment($amount, $renewal_order, true, $response->error); |
|
236 | 236 | } else { |
237 | - $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' ); |
|
238 | - $renewal_order->add_order_note( $localized_message ); |
|
239 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
237 | + $localized_message = __('Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe'); |
|
238 | + $renewal_order->add_order_note($localized_message); |
|
239 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
240 | 240 | } |
241 | 241 | } |
242 | 242 | |
243 | 243 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
244 | 244 | |
245 | - if ( 'card_error' === $response->error->type ) { |
|
246 | - $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
245 | + if ('card_error' === $response->error->type) { |
|
246 | + $localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message; |
|
247 | 247 | } else { |
248 | - $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
248 | + $localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message; |
|
249 | 249 | } |
250 | 250 | |
251 | - $renewal_order->add_order_note( $localized_message ); |
|
251 | + $renewal_order->add_order_note($localized_message); |
|
252 | 252 | |
253 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
253 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
254 | 254 | } |
255 | 255 | |
256 | - do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order ); |
|
256 | + do_action('wc_gateway_stripe_process_payment', $response, $renewal_order); |
|
257 | 257 | |
258 | - $this->process_response( $response, $renewal_order ); |
|
259 | - } catch ( WC_Stripe_Exception $e ) { |
|
260 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
258 | + $this->process_response($response, $renewal_order); |
|
259 | + } catch (WC_Stripe_Exception $e) { |
|
260 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
261 | 261 | |
262 | - do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); |
|
262 | + do_action('wc_gateway_stripe_process_payment_error', $e, $renewal_order); |
|
263 | 263 | |
264 | 264 | /* translators: error message */ |
265 | - $renewal_order->update_status( 'failed' ); |
|
265 | + $renewal_order->update_status('failed'); |
|
266 | 266 | } |
267 | 267 | } |
268 | 268 | |
@@ -272,24 +272,24 @@ discard block |
||
272 | 272 | * @since 3.1.0 |
273 | 273 | * @version 4.0.0 |
274 | 274 | */ |
275 | - public function save_source_to_order( $order, $source ) { |
|
276 | - parent::save_source_to_order( $order, $source ); |
|
275 | + public function save_source_to_order($order, $source) { |
|
276 | + parent::save_source_to_order($order, $source); |
|
277 | 277 | |
278 | 278 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
279 | 279 | |
280 | 280 | // Also store it on the subscriptions being purchased or paid for in the order |
281 | - if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) { |
|
282 | - $subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
|
283 | - } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) { |
|
284 | - $subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); |
|
281 | + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order_id)) { |
|
282 | + $subscriptions = wcs_get_subscriptions_for_order($order_id); |
|
283 | + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order_id)) { |
|
284 | + $subscriptions = wcs_get_subscriptions_for_renewal_order($order_id); |
|
285 | 285 | } else { |
286 | 286 | $subscriptions = array(); |
287 | 287 | } |
288 | 288 | |
289 | - foreach ( $subscriptions as $subscription ) { |
|
289 | + foreach ($subscriptions as $subscription) { |
|
290 | 290 | $subscription_id = WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id(); |
291 | - update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer ); |
|
292 | - update_post_meta( $subscription_id, '_stripe_source_id', $source->source ); |
|
291 | + update_post_meta($subscription_id, '_stripe_customer_id', $source->customer); |
|
292 | + update_post_meta($subscription_id, '_stripe_source_id', $source->source); |
|
293 | 293 | } |
294 | 294 | } |
295 | 295 | |
@@ -297,21 +297,21 @@ discard block |
||
297 | 297 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
298 | 298 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
299 | 299 | */ |
300 | - public function delete_resubscribe_meta( $resubscribe_order ) { |
|
301 | - delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_customer_id' ); |
|
302 | - delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_source_id' ); |
|
300 | + public function delete_resubscribe_meta($resubscribe_order) { |
|
301 | + delete_post_meta((WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_customer_id'); |
|
302 | + delete_post_meta((WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_source_id'); |
|
303 | 303 | // For BW compat will remove in future |
304 | - delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_card_id' ); |
|
305 | - $this->delete_renewal_meta( $resubscribe_order ); |
|
304 | + delete_post_meta((WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_card_id'); |
|
305 | + $this->delete_renewal_meta($resubscribe_order); |
|
306 | 306 | } |
307 | 307 | |
308 | 308 | /** |
309 | 309 | * Don't transfer Stripe fee/ID meta to renewal orders. |
310 | 310 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
311 | 311 | */ |
312 | - public function delete_renewal_meta( $renewal_order ) { |
|
313 | - WC_Stripe_Helper::delete_stripe_fee( $renewal_order ); |
|
314 | - WC_Stripe_Helper::delete_stripe_net( $renewal_order ); |
|
312 | + public function delete_renewal_meta($renewal_order) { |
|
313 | + WC_Stripe_Helper::delete_stripe_fee($renewal_order); |
|
314 | + WC_Stripe_Helper::delete_stripe_net($renewal_order); |
|
315 | 315 | |
316 | 316 | return $renewal_order; |
317 | 317 | } |
@@ -325,14 +325,14 @@ discard block |
||
325 | 325 | * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
326 | 326 | * @return void |
327 | 327 | */ |
328 | - public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
329 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
330 | - update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
331 | - update_post_meta( $subscription->id, '_stripe_source_id', $renewal_order->stripe_source_id ); |
|
328 | + public function update_failing_payment_method($subscription, $renewal_order) { |
|
329 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
330 | + update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
331 | + update_post_meta($subscription->id, '_stripe_source_id', $renewal_order->stripe_source_id); |
|
332 | 332 | |
333 | 333 | } else { |
334 | - update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); |
|
335 | - update_post_meta( $subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) ); |
|
334 | + update_post_meta($subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta('_stripe_customer_id', true)); |
|
335 | + update_post_meta($subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta('_stripe_source_id', true)); |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | |
@@ -345,21 +345,21 @@ discard block |
||
345 | 345 | * @param WC_Subscription $subscription An instance of a subscription object |
346 | 346 | * @return array |
347 | 347 | */ |
348 | - public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
349 | - $source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_source_id', true ); |
|
348 | + public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
349 | + $source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_source_id', true); |
|
350 | 350 | |
351 | 351 | // For BW compat will remove in future. |
352 | - if ( empty( $source_id ) ) { |
|
353 | - $source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
352 | + if (empty($source_id)) { |
|
353 | + $source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true); |
|
354 | 354 | |
355 | 355 | // Take this opportunity to update the key name. |
356 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $subscription->id, '_stripe_source_id', $source_id ) : update_post_meta( $subscription->get_id(), '_stripe_source_id', $source_id ); |
|
356 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($subscription->id, '_stripe_source_id', $source_id) : update_post_meta($subscription->get_id(), '_stripe_source_id', $source_id); |
|
357 | 357 | } |
358 | 358 | |
359 | - $payment_meta[ $this->id ] = array( |
|
359 | + $payment_meta[$this->id] = array( |
|
360 | 360 | 'post_meta' => array( |
361 | 361 | '_stripe_customer_id' => array( |
362 | - 'value' => get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ), |
|
362 | + 'value' => get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true), |
|
363 | 363 | 'label' => 'Stripe Customer ID', |
364 | 364 | ), |
365 | 365 | '_stripe_source_id' => array( |
@@ -382,22 +382,22 @@ discard block |
||
382 | 382 | * @param array $payment_meta associative array of meta data required for automatic payments |
383 | 383 | * @return array |
384 | 384 | */ |
385 | - public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
386 | - if ( $this->id === $payment_method_id ) { |
|
385 | + public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
386 | + if ($this->id === $payment_method_id) { |
|
387 | 387 | |
388 | - if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
389 | - throw new Exception( __( 'A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe' ) ); |
|
390 | - } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
391 | - throw new Exception( __( 'Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe' ) ); |
|
388 | + if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
389 | + throw new Exception(__('A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe')); |
|
390 | + } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
391 | + throw new Exception(__('Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe')); |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | if ( |
395 | - ( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
396 | - && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'card_' ) ) |
|
397 | - && ( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
398 | - && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'src_' ) ) ) { |
|
395 | + ( ! empty($payment_meta['post_meta']['_stripe_source_id']['value']) |
|
396 | + && 0 !== strpos($payment_meta['post_meta']['_stripe_source_id']['value'], 'card_')) |
|
397 | + && ( ! empty($payment_meta['post_meta']['_stripe_source_id']['value']) |
|
398 | + && 0 !== strpos($payment_meta['post_meta']['_stripe_source_id']['value'], 'src_')) ) { |
|
399 | 399 | |
400 | - throw new Exception( __( 'Invalid source ID. A valid source "Stripe Source ID" must begin with "src_" or "card_".', 'woocommerce-gateway-stripe' ) ); |
|
400 | + throw new Exception(__('Invalid source ID. A valid source "Stripe Source ID" must begin with "src_" or "card_".', 'woocommerce-gateway-stripe')); |
|
401 | 401 | } |
402 | 402 | } |
403 | 403 | } |
@@ -410,93 +410,93 @@ discard block |
||
410 | 410 | * @param WC_Subscription $subscription the subscription details |
411 | 411 | * @return string the subscription payment method |
412 | 412 | */ |
413 | - public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
413 | + public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
414 | 414 | $customer_user = WC_Stripe_Helper::is_pre_30() ? $subscription->customer_user : $subscription->get_customer_id(); |
415 | 415 | |
416 | 416 | // bail for other payment methods |
417 | - if ( ( WC_Stripe_Helper::is_pre_30() ? $subscription->payment_method : $subscription->get_payment_method() ) !== $this->id || ! $customer_user ) { |
|
417 | + if ((WC_Stripe_Helper::is_pre_30() ? $subscription->payment_method : $subscription->get_payment_method()) !== $this->id || ! $customer_user) { |
|
418 | 418 | return $payment_method_to_display; |
419 | 419 | } |
420 | 420 | |
421 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_source_id', true ); |
|
421 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_source_id', true); |
|
422 | 422 | |
423 | 423 | // For BW compat will remove in future. |
424 | - if ( empty( $stripe_source_id ) ) { |
|
425 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
424 | + if (empty($stripe_source_id)) { |
|
425 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true); |
|
426 | 426 | |
427 | 427 | // Take this opportunity to update the key name. |
428 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $subscription->id, '_stripe_source_id', $stripe_source_id ) : update_post_meta( $subscription->get_id(), '_stripe_source_id', $stripe_source_id ); |
|
428 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($subscription->id, '_stripe_source_id', $stripe_source_id) : update_post_meta($subscription->get_id(), '_stripe_source_id', $stripe_source_id); |
|
429 | 429 | } |
430 | 430 | |
431 | 431 | $stripe_customer = new WC_Stripe_Customer(); |
432 | - $stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ); |
|
432 | + $stripe_customer_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true); |
|
433 | 433 | |
434 | 434 | // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
435 | - if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
435 | + if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
436 | 436 | $user_id = $customer_user; |
437 | - $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
438 | - $stripe_source_id = get_user_meta( $user_id, '_stripe_source_id', true ); |
|
437 | + $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
438 | + $stripe_source_id = get_user_meta($user_id, '_stripe_source_id', true); |
|
439 | 439 | |
440 | 440 | // For BW compat will remove in future. |
441 | - if ( empty( $stripe_source_id ) ) { |
|
442 | - $stripe_source_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
441 | + if (empty($stripe_source_id)) { |
|
442 | + $stripe_source_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
443 | 443 | |
444 | 444 | // Take this opportunity to update the key name. |
445 | - update_user_meta( $user_id, '_stripe_source_id', $stripe_source_id ); |
|
445 | + update_user_meta($user_id, '_stripe_source_id', $stripe_source_id); |
|
446 | 446 | } |
447 | 447 | } |
448 | 448 | |
449 | 449 | // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
450 | - if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
451 | - $stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_customer_id', true ); |
|
452 | - $stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_source_id', true ); |
|
450 | + if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
451 | + $stripe_customer_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_customer_id', true); |
|
452 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_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_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_card_id', true ); |
|
455 | + if (empty($stripe_source_id)) { |
|
456 | + $stripe_source_id = get_post_meta((WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_card_id', true); |
|
457 | 457 | |
458 | 458 | // Take this opportunity to update the key name. |
459 | - WC_Stripe_Helper::is_pre_30() ? 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 ); |
|
459 | + WC_Stripe_Helper::is_pre_30() ? 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); |
|
460 | 460 | } |
461 | 461 | } |
462 | 462 | |
463 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
463 | + $stripe_customer->set_id($stripe_customer_id); |
|
464 | 464 | $sources = $stripe_customer->get_sources(); |
465 | 465 | |
466 | - if ( $sources ) { |
|
466 | + if ($sources) { |
|
467 | 467 | $card = false; |
468 | 468 | $found_source = false; |
469 | 469 | |
470 | - foreach ( $sources as $source ) { |
|
471 | - if ( isset( $source->type ) && 'card' === $source->type ) { |
|
470 | + foreach ($sources as $source) { |
|
471 | + if (isset($source->type) && 'card' === $source->type) { |
|
472 | 472 | $card = $source->card; |
473 | - } elseif ( isset( $source->object ) && 'card' === $source->object ) { |
|
473 | + } elseif (isset($source->object) && 'card' === $source->object) { |
|
474 | 474 | $card = $source; |
475 | 475 | } |
476 | 476 | |
477 | - if ( $source->id === $stripe_source_id ) { |
|
477 | + if ($source->id === $stripe_source_id) { |
|
478 | 478 | $found_source = true; |
479 | 479 | |
480 | - if ( $card ) { |
|
480 | + if ($card) { |
|
481 | 481 | /* translators: 1) card brand 2) last 4 digits */ |
482 | - $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 ); |
|
482 | + $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); |
|
483 | 483 | } else { |
484 | - $payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' ); |
|
484 | + $payment_method_to_display = __('N/A', 'woocommerce-gateway-stripe'); |
|
485 | 485 | } |
486 | 486 | break; |
487 | 487 | } |
488 | 488 | } |
489 | 489 | |
490 | - if ( ! $found_source ) { |
|
491 | - if ( isset( $sources[0]->type ) && 'card' === $sources[0]->type ) { |
|
490 | + if ( ! $found_source) { |
|
491 | + if (isset($sources[0]->type) && 'card' === $sources[0]->type) { |
|
492 | 492 | $card = $sources[0]->card; |
493 | 493 | } |
494 | 494 | |
495 | - if ( $card ) { |
|
495 | + if ($card) { |
|
496 | 496 | /* translators: 1) card brand 2) last 4 digits */ |
497 | - $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 ); |
|
497 | + $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); |
|
498 | 498 | } else { |
499 | - $payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' ); |
|
499 | + $payment_method_to_display = __('N/A', 'woocommerce-gateway-stripe'); |
|
500 | 500 | } |
501 | 501 | } |
502 | 502 | } |