@@ -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,41 +58,41 @@ 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 | - $wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' ); |
|
80 | + $wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<'); |
|
81 | 81 | $order_id = $wc_pre_30 ? $order->id : $order->get_id(); |
82 | 82 | |
83 | 83 | // Also store it on the subscriptions being purchased or paid for in the order |
84 | - if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) { |
|
85 | - $subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
|
86 | - } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) { |
|
87 | - $subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); |
|
84 | + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order_id)) { |
|
85 | + $subscriptions = wcs_get_subscriptions_for_order($order_id); |
|
86 | + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order_id)) { |
|
87 | + $subscriptions = wcs_get_subscriptions_for_renewal_order($order_id); |
|
88 | 88 | } else { |
89 | 89 | $subscriptions = array(); |
90 | 90 | } |
91 | 91 | |
92 | - foreach ( $subscriptions as $subscription ) { |
|
92 | + foreach ($subscriptions as $subscription) { |
|
93 | 93 | $subscription_id = $wc_pre_30 ? $subscription->id : $subscription->get_id(); |
94 | - update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer ); |
|
95 | - update_post_meta( $subscription_id, '_stripe_card_id', $source->source ); |
|
94 | + update_post_meta($subscription_id, '_stripe_customer_id', $source->customer); |
|
95 | + update_post_meta($subscription_id, '_stripe_card_id', $source->source); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
@@ -103,53 +103,53 @@ discard block |
||
103 | 103 | * @param string $stripe_token (default: '') |
104 | 104 | * @param bool initial_payment |
105 | 105 | */ |
106 | - public function process_subscription_payment( $order = '', $amount = 0 ) { |
|
107 | - if ( $amount * 100 < WC_Stripe::get_minimum_amount() ) { |
|
108 | - 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 ) ) ); |
|
106 | + public function process_subscription_payment($order = '', $amount = 0) { |
|
107 | + if ($amount * 100 < WC_Stripe::get_minimum_amount()) { |
|
108 | + 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))); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | // Get source from order |
112 | - $source = $this->get_order_source( $order ); |
|
112 | + $source = $this->get_order_source($order); |
|
113 | 113 | |
114 | 114 | // If no order source was defined, use user source instead. |
115 | - if ( ! $source->customer ) { |
|
116 | - $source = $this->get_source( $order->customer_user ); |
|
115 | + if ( ! $source->customer) { |
|
116 | + $source = $this->get_source($order->customer_user); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | // Or fail :( |
120 | - if ( ! $source->customer ) { |
|
121 | - return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
120 | + if ( ! $source->customer) { |
|
121 | + return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
122 | 122 | } |
123 | 123 | |
124 | - $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
125 | - $this->log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
124 | + $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
125 | + $this->log("Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}"); |
|
126 | 126 | |
127 | 127 | // Make the request |
128 | - $request = $this->generate_payment_request( $order, $source ); |
|
128 | + $request = $this->generate_payment_request($order, $source); |
|
129 | 129 | $request['capture'] = 'true'; |
130 | - $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
130 | + $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
131 | 131 | $request['metadata'] = array( |
132 | 132 | 'payment_type' => 'recurring', |
133 | - 'site_url' => esc_url( get_site_url() ), |
|
133 | + 'site_url' => esc_url(get_site_url()), |
|
134 | 134 | ); |
135 | - $response = WC_Stripe_API::request( $request ); |
|
135 | + $response = WC_Stripe_API::request($request); |
|
136 | 136 | |
137 | 137 | // Process valid response |
138 | - if ( is_wp_error( $response ) ) { |
|
139 | - if ( 'missing' === $response->get_error_code() ) { |
|
138 | + if (is_wp_error($response)) { |
|
139 | + if ('missing' === $response->get_error_code()) { |
|
140 | 140 | // If we can't link customer to a card, we try to charge by customer ID. |
141 | - $request = $this->generate_payment_request( $order, $this->get_source( $order->customer_user ) ); |
|
141 | + $request = $this->generate_payment_request($order, $this->get_source($order->customer_user)); |
|
142 | 142 | $request['capture'] = 'true'; |
143 | - $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
143 | + $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
144 | 144 | $request['metadata'] = array( |
145 | 145 | 'payment_type' => 'recurring', |
146 | - 'site_url' => esc_url( get_site_url() ), |
|
146 | + 'site_url' => esc_url(get_site_url()), |
|
147 | 147 | ); |
148 | - $response = WC_Stripe_API::request( $request ); |
|
148 | + $response = WC_Stripe_API::request($request); |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | |
152 | - $this->process_response( $response, $order ); |
|
152 | + $this->process_response($response, $order); |
|
153 | 153 | |
154 | 154 | return $response; |
155 | 155 | } |
@@ -159,42 +159,42 @@ discard block |
||
159 | 159 | * @param int $order_id |
160 | 160 | * @return array |
161 | 161 | */ |
162 | - public function process_pre_order( $order_id, $retry, $force_customer ) { |
|
163 | - if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { |
|
162 | + public function process_pre_order($order_id, $retry, $force_customer) { |
|
163 | + if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) { |
|
164 | 164 | try { |
165 | - $order = wc_get_order( $order_id ); |
|
165 | + $order = wc_get_order($order_id); |
|
166 | 166 | |
167 | - if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
168 | - 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 ) ) ); |
|
167 | + if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
168 | + 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))); |
|
169 | 169 | } |
170 | 170 | |
171 | - $source = $this->get_source( get_current_user_id(), true ); |
|
171 | + $source = $this->get_source(get_current_user_id(), true); |
|
172 | 172 | |
173 | 173 | // We need a source on file to continue. |
174 | - if ( empty( $source->customer ) || empty( $source->source ) ) { |
|
175 | - throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
174 | + if (empty($source->customer) || empty($source->source)) { |
|
175 | + throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe')); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | // Store source to order meta |
179 | - $this->save_source( $order, $source ); |
|
179 | + $this->save_source($order, $source); |
|
180 | 180 | |
181 | 181 | // Remove cart |
182 | 182 | WC()->cart->empty_cart(); |
183 | 183 | |
184 | 184 | // Is pre ordered! |
185 | - WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
185 | + WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); |
|
186 | 186 | |
187 | 187 | // Return thank you page redirect |
188 | 188 | return array( |
189 | 189 | 'result' => 'success', |
190 | - 'redirect' => $this->get_return_url( $order ), |
|
190 | + 'redirect' => $this->get_return_url($order), |
|
191 | 191 | ); |
192 | - } catch ( Exception $e ) { |
|
193 | - wc_add_notice( $e->getMessage(), 'error' ); |
|
192 | + } catch (Exception $e) { |
|
193 | + wc_add_notice($e->getMessage(), 'error'); |
|
194 | 194 | return; |
195 | 195 | } |
196 | 196 | } else { |
197 | - return parent::process_payment( $order_id, $retry, $force_customer ); |
|
197 | + return parent::process_payment($order_id, $retry, $force_customer); |
|
198 | 198 | } |
199 | 199 | } |
200 | 200 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @param WC_Order $order |
204 | 204 | * @return void |
205 | 205 | */ |
206 | - public function process_pre_order_release_payment( $order ) { |
|
206 | + public function process_pre_order_release_payment($order) { |
|
207 | 207 | try { |
208 | 208 | // Define some callbacks if the first attempt fails. |
209 | 209 | $retry_callbacks = array( |
@@ -211,32 +211,32 @@ discard block |
||
211 | 211 | 'remove_order_customer_before_retry', |
212 | 212 | ); |
213 | 213 | |
214 | - while ( 1 ) { |
|
215 | - $source = $this->get_order_source( $order ); |
|
216 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
214 | + while (1) { |
|
215 | + $source = $this->get_order_source($order); |
|
216 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
217 | 217 | |
218 | - if ( is_wp_error( $response ) ) { |
|
219 | - if ( 0 === sizeof( $retry_callbacks ) ) { |
|
220 | - throw new Exception( $response->get_error_message() ); |
|
218 | + if (is_wp_error($response)) { |
|
219 | + if (0 === sizeof($retry_callbacks)) { |
|
220 | + throw new Exception($response->get_error_message()); |
|
221 | 221 | } else { |
222 | - $retry_callback = array_shift( $retry_callbacks ); |
|
223 | - call_user_func( array( $this, $retry_callback ), $order ); |
|
222 | + $retry_callback = array_shift($retry_callbacks); |
|
223 | + call_user_func(array($this, $retry_callback), $order); |
|
224 | 224 | } |
225 | 225 | } else { |
226 | 226 | // Successful |
227 | - $this->process_response( $response, $order ); |
|
227 | + $this->process_response($response, $order); |
|
228 | 228 | break; |
229 | 229 | } |
230 | 230 | } |
231 | - } catch ( Exception $e ) { |
|
232 | - $order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() ); |
|
231 | + } catch (Exception $e) { |
|
232 | + $order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage()); |
|
233 | 233 | |
234 | 234 | // Mark order as failed if not already set, |
235 | 235 | // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
236 | - if ( ! $order->has_status( 'failed' ) ) { |
|
237 | - $order->update_status( 'failed', $order_note ); |
|
236 | + if ( ! $order->has_status('failed')) { |
|
237 | + $order->update_status('failed', $order_note); |
|
238 | 238 | } else { |
239 | - $order->add_order_note( $order_note ); |
|
239 | + $order->add_order_note($order_note); |
|
240 | 240 | } |
241 | 241 | } |
242 | 242 | } |
@@ -245,20 +245,20 @@ discard block |
||
245 | 245 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
246 | 246 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
247 | 247 | */ |
248 | - public function delete_resubscribe_meta( $resubscribe_order ) { |
|
249 | - delete_post_meta( $resubscribe_order->id, '_stripe_customer_id' ); |
|
250 | - delete_post_meta( $resubscribe_order->id, '_stripe_card_id' ); |
|
251 | - $this->delete_renewal_meta( $resubscribe_order ); |
|
248 | + public function delete_resubscribe_meta($resubscribe_order) { |
|
249 | + delete_post_meta($resubscribe_order->id, '_stripe_customer_id'); |
|
250 | + delete_post_meta($resubscribe_order->id, '_stripe_card_id'); |
|
251 | + $this->delete_renewal_meta($resubscribe_order); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | /** |
255 | 255 | * Don't transfer Stripe fee/ID meta to renewal orders. |
256 | 256 | * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
257 | 257 | */ |
258 | - public function delete_renewal_meta( $renewal_order ) { |
|
259 | - delete_post_meta( $renewal_order->id, 'Stripe Fee' ); |
|
260 | - delete_post_meta( $renewal_order->id, 'Net Revenue From Stripe' ); |
|
261 | - delete_post_meta( $renewal_order->id, 'Stripe Payment ID' ); |
|
258 | + public function delete_renewal_meta($renewal_order) { |
|
259 | + delete_post_meta($renewal_order->id, 'Stripe Fee'); |
|
260 | + delete_post_meta($renewal_order->id, 'Net Revenue From Stripe'); |
|
261 | + delete_post_meta($renewal_order->id, 'Stripe Payment ID'); |
|
262 | 262 | return $renewal_order; |
263 | 263 | } |
264 | 264 | |
@@ -268,11 +268,11 @@ discard block |
||
268 | 268 | * @param $amount_to_charge float The amount to charge. |
269 | 269 | * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
270 | 270 | */ |
271 | - public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
272 | - $response = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); |
|
271 | + public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
272 | + $response = $this->process_subscription_payment($renewal_order, $amount_to_charge); |
|
273 | 273 | |
274 | - if ( is_wp_error( $response ) ) { |
|
275 | - $renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
274 | + if (is_wp_error($response)) { |
|
275 | + $renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message())); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | |
@@ -280,18 +280,18 @@ discard block |
||
280 | 280 | * Remove order meta |
281 | 281 | * @param object $order |
282 | 282 | */ |
283 | - public function remove_order_source_before_retry( $order ) { |
|
284 | - $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
285 | - delete_post_meta( $order_id, '_stripe_card_id' ); |
|
283 | + public function remove_order_source_before_retry($order) { |
|
284 | + $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
285 | + delete_post_meta($order_id, '_stripe_card_id'); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | /** |
289 | 289 | * Remove order meta |
290 | 290 | * @param object $order |
291 | 291 | */ |
292 | - public function remove_order_customer_before_retry( $order ) { |
|
293 | - $order_id = version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->id : $order->get_id(); |
|
294 | - delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
292 | + public function remove_order_customer_before_retry($order) { |
|
293 | + $order_id = version_compare(WC_VERSION, '3.0.0', '<') ? $order->id : $order->get_id(); |
|
294 | + delete_post_meta($order_id, '_stripe_customer_id'); |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | /** |
@@ -303,9 +303,9 @@ discard block |
||
303 | 303 | * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
304 | 304 | * @return void |
305 | 305 | */ |
306 | - public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
307 | - update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
308 | - update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id ); |
|
306 | + public function update_failing_payment_method($subscription, $renewal_order) { |
|
307 | + update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
308 | + update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id); |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | /** |
@@ -317,15 +317,15 @@ discard block |
||
317 | 317 | * @param WC_Subscription $subscription An instance of a subscription object |
318 | 318 | * @return array |
319 | 319 | */ |
320 | - public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
321 | - $payment_meta[ $this->id ] = array( |
|
320 | + public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
321 | + $payment_meta[$this->id] = array( |
|
322 | 322 | 'post_meta' => array( |
323 | 323 | '_stripe_customer_id' => array( |
324 | - 'value' => get_post_meta( $subscription->id, '_stripe_customer_id', true ), |
|
324 | + 'value' => get_post_meta($subscription->id, '_stripe_customer_id', true), |
|
325 | 325 | 'label' => 'Stripe Customer ID', |
326 | 326 | ), |
327 | 327 | '_stripe_card_id' => array( |
328 | - 'value' => get_post_meta( $subscription->id, '_stripe_card_id', true ), |
|
328 | + 'value' => get_post_meta($subscription->id, '_stripe_card_id', true), |
|
329 | 329 | 'label' => 'Stripe Card ID', |
330 | 330 | ), |
331 | 331 | ), |
@@ -342,17 +342,17 @@ discard block |
||
342 | 342 | * @param array $payment_meta associative array of meta data required for automatic payments |
343 | 343 | * @return array |
344 | 344 | */ |
345 | - public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
346 | - if ( $this->id === $payment_method_id ) { |
|
345 | + public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
346 | + if ($this->id === $payment_method_id) { |
|
347 | 347 | |
348 | - if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
349 | - throw new Exception( 'A "_stripe_customer_id" value is required.' ); |
|
350 | - } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
351 | - throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' ); |
|
348 | + if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
349 | + throw new Exception('A "_stripe_customer_id" value is required.'); |
|
350 | + } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
351 | + throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".'); |
|
352 | 352 | } |
353 | 353 | |
354 | - if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) { |
|
355 | - throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' ); |
|
354 | + if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) { |
|
355 | + throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".'); |
|
356 | 356 | } |
357 | 357 | } |
358 | 358 | } |
@@ -365,47 +365,47 @@ discard block |
||
365 | 365 | * @param WC_Subscription $subscription the subscription details |
366 | 366 | * @return string the subscription payment method |
367 | 367 | */ |
368 | - public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
369 | - $wc_pre_30 = version_compare( WC_VERSION, '3.0', '<' ); |
|
368 | + public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
369 | + $wc_pre_30 = version_compare(WC_VERSION, '3.0', '<'); |
|
370 | 370 | |
371 | 371 | $customer_user = $wc_pre_30 ? $subscription->customer_user : $subscription->get_customer_id(); |
372 | 372 | |
373 | 373 | // bail for other payment methods |
374 | - if ( $this->id !== ( $wc_pre_30 ? $subscription->payment_method : $subscription->get_payment_method() ) || ! $customer_user ) { |
|
374 | + if ($this->id !== ($wc_pre_30 ? $subscription->payment_method : $subscription->get_payment_method()) || ! $customer_user) { |
|
375 | 375 | return $payment_method_to_display; |
376 | 376 | } |
377 | 377 | |
378 | 378 | $stripe_customer = new WC_Stripe_Customer(); |
379 | - $stripe_customer_id = get_post_meta( ( $wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ); |
|
380 | - $stripe_card_id = get_post_meta( ( $wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
379 | + $stripe_customer_id = get_post_meta(($wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true); |
|
380 | + $stripe_card_id = get_post_meta(($wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true); |
|
381 | 381 | |
382 | 382 | // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
383 | - if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
383 | + if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
384 | 384 | $user_id = $customer_user; |
385 | - $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
386 | - $stripe_card_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
385 | + $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
386 | + $stripe_card_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
390 | - if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
391 | - $stripe_customer_id = get_post_meta( ( $wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_customer_id', true ); |
|
392 | - $stripe_card_id = get_post_meta( ( $wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_card_id', true ); |
|
390 | + if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
391 | + $stripe_customer_id = get_post_meta(($wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_customer_id', true); |
|
392 | + $stripe_card_id = get_post_meta(($wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_card_id', true); |
|
393 | 393 | } |
394 | 394 | |
395 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
395 | + $stripe_customer->set_id($stripe_customer_id); |
|
396 | 396 | $cards = $stripe_customer->get_cards(); |
397 | 397 | |
398 | - if ( $cards ) { |
|
398 | + if ($cards) { |
|
399 | 399 | $found_card = false; |
400 | - foreach ( $cards as $card ) { |
|
401 | - if ( $card->id === $stripe_card_id ) { |
|
400 | + foreach ($cards as $card) { |
|
401 | + if ($card->id === $stripe_card_id) { |
|
402 | 402 | $found_card = true; |
403 | - $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 ); |
|
403 | + $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); |
|
404 | 404 | break; |
405 | 405 | } |
406 | 406 | } |
407 | - if ( ! $found_card ) { |
|
408 | - $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 ); |
|
407 | + if ( ! $found_card) { |
|
408 | + $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); |
|
409 | 409 | } |
410 | 410 | } |
411 | 411 | |
@@ -420,11 +420,11 @@ discard block |
||
420 | 420 | * |
421 | 421 | * @param string $message |
422 | 422 | */ |
423 | - public function log( $message ) { |
|
424 | - $options = get_option( 'woocommerce_stripe_settings' ); |
|
423 | + public function log($message) { |
|
424 | + $options = get_option('woocommerce_stripe_settings'); |
|
425 | 425 | |
426 | - if ( 'yes' === $options['logging'] ) { |
|
427 | - WC_Stripe::log( $message ); |
|
426 | + if ('yes' === $options['logging']) { |
|
427 | + WC_Stripe::log($message); |
|
428 | 428 | } |
429 | 429 | } |
430 | 430 | } |