|
@@ -1,5 +1,5 @@ discard block |
|
|
block discarded – undo |
|
1
|
1
|
<?php |
|
2
|
|
-if ( ! defined( 'ABSPATH' ) ) { |
|
|
2
|
+if ( ! defined('ABSPATH')) { |
|
3
|
3
|
exit; |
|
4
|
4
|
} |
|
5
|
5
|
|
|
@@ -18,25 +18,25 @@ discard block |
|
|
block discarded – undo |
|
18
|
18
|
public function __construct() { |
|
19
|
19
|
parent::__construct(); |
|
20
|
20
|
|
|
21
|
|
- if ( class_exists( 'WC_Subscriptions_Order' ) ) { |
|
22
|
|
- add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 ); |
|
23
|
|
- add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 ); |
|
24
|
|
- add_action( 'wcs_renewal_order_created', array( $this, 'delete_renewal_meta' ), 10 ); |
|
25
|
|
- add_action( 'woocommerce_subscription_failing_payment_method_updated_stripe', array( $this, 'update_failing_payment_method' ), 10, 2 ); |
|
|
21
|
+ if (class_exists('WC_Subscriptions_Order')) { |
|
|
22
|
+ add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); |
|
|
23
|
+ add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); |
|
|
24
|
+ add_action('wcs_renewal_order_created', array($this, 'delete_renewal_meta'), 10); |
|
|
25
|
+ add_action('woocommerce_subscription_failing_payment_method_updated_stripe', array($this, 'update_failing_payment_method'), 10, 2); |
|
26
|
26
|
|
|
27
|
27
|
// display the credit card used for a subscription in the "My Subscriptions" table |
|
28
|
|
- add_filter( 'woocommerce_my_subscriptions_payment_method', array( $this, 'maybe_render_subscription_payment_method' ), 10, 2 ); |
|
|
28
|
+ add_filter('woocommerce_my_subscriptions_payment_method', array($this, 'maybe_render_subscription_payment_method'), 10, 2); |
|
29
|
29
|
|
|
30
|
30
|
// allow store managers to manually set Stripe as the payment method on a subscription |
|
31
|
|
- add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 ); |
|
32
|
|
- add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 ); |
|
|
31
|
+ add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); |
|
|
32
|
+ add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); |
|
33
|
33
|
} |
|
34
|
34
|
|
|
35
|
|
- if ( class_exists( 'WC_Pre_Orders_Order' ) ) { |
|
36
|
|
- add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ) ); |
|
|
35
|
+ if (class_exists('WC_Pre_Orders_Order')) { |
|
|
36
|
+ add_action('wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array($this, 'process_pre_order_release_payment')); |
|
37
|
37
|
} |
|
38
|
38
|
|
|
39
|
|
- $this->wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' ); |
|
|
39
|
+ $this->wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<'); |
|
40
|
40
|
} |
|
41
|
41
|
|
|
42
|
42
|
/** |
|
@@ -44,8 +44,8 @@ discard block |
|
|
block discarded – undo |
|
44
|
44
|
* @param int $order_id |
|
45
|
45
|
* @return boolean |
|
46
|
46
|
*/ |
|
47
|
|
- protected function is_subscription( $order_id ) { |
|
48
|
|
- return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) ); |
|
|
47
|
+ protected function is_subscription($order_id) { |
|
|
48
|
+ return (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id))); |
|
49
|
49
|
} |
|
50
|
50
|
|
|
51
|
51
|
/** |
|
@@ -53,8 +53,8 @@ discard block |
|
|
block discarded – undo |
|
53
|
53
|
* @param int $order_id |
|
54
|
54
|
* @return boolean |
|
55
|
55
|
*/ |
|
56
|
|
- protected function is_pre_order( $order_id ) { |
|
57
|
|
- return ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id ) ); |
|
|
56
|
+ protected function is_pre_order($order_id) { |
|
|
57
|
+ return (class_exists('WC_Pre_Orders_Order') && WC_Pre_Orders_Order::order_contains_pre_order($order_id)); |
|
58
|
58
|
} |
|
59
|
59
|
|
|
60
|
60
|
/** |
|
@@ -62,40 +62,40 @@ discard block |
|
|
block discarded – undo |
|
62
|
62
|
* @param int $order_id |
|
63
|
63
|
* @return array |
|
64
|
64
|
*/ |
|
65
|
|
- public function process_payment( $order_id, $retry = true, $force_customer = false ) { |
|
66
|
|
- if ( $this->is_subscription( $order_id ) ) { |
|
|
65
|
+ public function process_payment($order_id, $retry = true, $force_customer = false) { |
|
|
66
|
+ if ($this->is_subscription($order_id)) { |
|
67
|
67
|
// Regular payment with force customer enabled |
|
68
|
|
- return parent::process_payment( $order_id, true, true ); |
|
|
68
|
+ return parent::process_payment($order_id, true, true); |
|
69
|
69
|
|
|
70
|
|
- } elseif ( $this->is_pre_order( $order_id ) ) { |
|
71
|
|
- return $this->process_pre_order( $order_id, $retry, $force_customer ); |
|
|
70
|
+ } elseif ($this->is_pre_order($order_id)) { |
|
|
71
|
+ return $this->process_pre_order($order_id, $retry, $force_customer); |
|
72
|
72
|
|
|
73
|
73
|
} else { |
|
74
|
|
- return parent::process_payment( $order_id, $retry, $force_customer ); |
|
|
74
|
+ return parent::process_payment($order_id, $retry, $force_customer); |
|
75
|
75
|
} |
|
76
|
76
|
} |
|
77
|
77
|
|
|
78
|
78
|
/** |
|
79
|
79
|
* Updates other subscription sources. |
|
80
|
80
|
*/ |
|
81
|
|
- protected function save_source( $order, $source ) { |
|
82
|
|
- parent::save_source( $order, $source ); |
|
|
81
|
+ protected function save_source($order, $source) { |
|
|
82
|
+ parent::save_source($order, $source); |
|
83
|
83
|
|
|
84
|
|
- $order_id = $this->wc_pre_30 ? $order->id : $order->get_id(); |
|
|
84
|
+ $order_id = $this->wc_pre_30 ? $order->id : $order->get_id(); |
|
85
|
85
|
|
|
86
|
86
|
// Also store it on the subscriptions being purchased or paid for in the order |
|
87
|
|
- if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) { |
|
88
|
|
- $subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
|
89
|
|
- } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) { |
|
90
|
|
- $subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); |
|
|
87
|
+ if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order_id)) { |
|
|
88
|
+ $subscriptions = wcs_get_subscriptions_for_order($order_id); |
|
|
89
|
+ } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order_id)) { |
|
|
90
|
+ $subscriptions = wcs_get_subscriptions_for_renewal_order($order_id); |
|
91
|
91
|
} else { |
|
92
|
92
|
$subscriptions = array(); |
|
93
|
93
|
} |
|
94
|
94
|
|
|
95
|
|
- foreach ( $subscriptions as $subscription ) { |
|
|
95
|
+ foreach ($subscriptions as $subscription) { |
|
96
|
96
|
$subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); |
|
97
|
|
- update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer ); |
|
98
|
|
- update_post_meta( $subscription_id, '_stripe_card_id', $source->source ); |
|
|
97
|
+ update_post_meta($subscription_id, '_stripe_customer_id', $source->customer); |
|
|
98
|
+ update_post_meta($subscription_id, '_stripe_card_id', $source->source); |
|
99
|
99
|
} |
|
100
|
100
|
} |
|
101
|
101
|
|
|
@@ -106,55 +106,55 @@ discard block |
|
|
block discarded – undo |
|
106
|
106
|
* @param string $stripe_token (default: '') |
|
107
|
107
|
* @param bool initial_payment |
|
108
|
108
|
*/ |
|
109
|
|
- public function process_subscription_payment( $order = '', $amount = 0 ) { |
|
110
|
|
- if ( $amount * 100 < WC_Stripe::get_minimum_amount() ) { |
|
111
|
|
- 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
|
+ public function process_subscription_payment($order = '', $amount = 0) { |
|
|
110
|
+ if ($amount * 100 < WC_Stripe::get_minimum_amount()) { |
|
|
111
|
+ 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))); |
|
112
|
112
|
} |
|
113
|
113
|
|
|
114
|
114
|
// Get source from order |
|
115
|
|
- $source = $this->get_order_source( $order ); |
|
|
115
|
+ $source = $this->get_order_source($order); |
|
116
|
116
|
|
|
117
|
117
|
// If no order source was defined, use user source instead. |
|
118
|
|
- if ( ! $source->customer ) { |
|
119
|
|
- $source = $this->get_source( ( $this->wc_pre_30 ? $order->customer_user : $order->get_customer_id() ) ); |
|
|
118
|
+ if ( ! $source->customer) { |
|
|
119
|
+ $source = $this->get_source(($this->wc_pre_30 ? $order->customer_user : $order->get_customer_id())); |
|
120
|
120
|
} |
|
121
|
121
|
|
|
122
|
122
|
// Or fail :( |
|
123
|
|
- if ( ! $source->customer ) { |
|
124
|
|
- return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
|
123
|
+ if ( ! $source->customer) { |
|
|
124
|
+ return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); |
|
125
|
125
|
} |
|
126
|
126
|
|
|
127
|
127
|
$order_id = $this->wc_pre_30 ? $order->id : $order->get_id(); |
|
128
|
|
- $this->log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
|
128
|
+ $this->log("Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}"); |
|
129
|
129
|
|
|
130
|
130
|
// Make the request |
|
131
|
|
- $request = $this->generate_payment_request( $order, $source ); |
|
|
131
|
+ $request = $this->generate_payment_request($order, $source); |
|
132
|
132
|
$request['capture'] = 'true'; |
|
133
|
|
- $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
|
133
|
+ $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
134
|
134
|
$request['metadata'] = array( |
|
135
|
135
|
'payment_type' => 'recurring', |
|
136
|
|
- 'site_url' => esc_url( get_site_url() ), |
|
|
136
|
+ 'site_url' => esc_url(get_site_url()), |
|
137
|
137
|
); |
|
138
|
|
- $response = WC_Stripe_API::request( $request ); |
|
|
138
|
+ $response = WC_Stripe_API::request($request); |
|
139
|
139
|
|
|
140
|
140
|
// Process valid response |
|
141
|
|
- if ( is_wp_error( $response ) ) { |
|
142
|
|
- if ( 'missing' === $response->get_error_code() ) { |
|
|
141
|
+ if (is_wp_error($response)) { |
|
|
142
|
+ if ('missing' === $response->get_error_code()) { |
|
143
|
143
|
// If we can't link customer to a card, we try to charge by customer ID. |
|
144
|
|
- $request = $this->generate_payment_request( $order, $this->get_source( ( $this->wc_pre_30 ? $order->customer_user : $order->get_customer_id() ) ) ); |
|
|
144
|
+ $request = $this->generate_payment_request($order, $this->get_source(($this->wc_pre_30 ? $order->customer_user : $order->get_customer_id()))); |
|
145
|
145
|
$request['capture'] = 'true'; |
|
146
|
|
- $request['amount'] = $this->get_stripe_amount( $amount, $request['currency'] ); |
|
|
146
|
+ $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); |
|
147
|
147
|
$request['metadata'] = array( |
|
148
|
148
|
'payment_type' => 'recurring', |
|
149
|
|
- 'site_url' => esc_url( get_site_url() ), |
|
|
149
|
+ 'site_url' => esc_url(get_site_url()), |
|
150
|
150
|
); |
|
151
|
|
- $response = WC_Stripe_API::request( $request ); |
|
|
151
|
+ $response = WC_Stripe_API::request($request); |
|
152
|
152
|
} else { |
|
153
|
153
|
return $response; // Default catch all errors. |
|
154
|
154
|
} |
|
155
|
155
|
} |
|
156
|
156
|
|
|
157
|
|
- $this->process_response( $response, $order ); |
|
|
157
|
+ $this->process_response($response, $order); |
|
158
|
158
|
|
|
159
|
159
|
return $response; |
|
160
|
160
|
} |
|
@@ -164,42 +164,42 @@ discard block |
|
|
block discarded – undo |
|
164
|
164
|
* @param int $order_id |
|
165
|
165
|
* @return array |
|
166
|
166
|
*/ |
|
167
|
|
- public function process_pre_order( $order_id, $retry, $force_customer ) { |
|
168
|
|
- if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { |
|
|
167
|
+ public function process_pre_order($order_id, $retry, $force_customer) { |
|
|
168
|
+ if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) { |
|
169
|
169
|
try { |
|
170
|
|
- $order = wc_get_order( $order_id ); |
|
|
170
|
+ $order = wc_get_order($order_id); |
|
171
|
171
|
|
|
172
|
|
- if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
173
|
|
- 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 ) ) ); |
|
|
172
|
+ if ($order->get_total() * 100 < WC_Stripe::get_minimum_amount()) { |
|
|
173
|
+ 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))); |
|
174
|
174
|
} |
|
175
|
175
|
|
|
176
|
|
- $source = $this->get_source( get_current_user_id(), true ); |
|
|
176
|
+ $source = $this->get_source(get_current_user_id(), true); |
|
177
|
177
|
|
|
178
|
178
|
// We need a source on file to continue. |
|
179
|
|
- if ( empty( $source->customer ) || empty( $source->source ) ) { |
|
180
|
|
- throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
|
179
|
+ if (empty($source->customer) || empty($source->source)) { |
|
|
180
|
+ throw new Exception(__('Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe')); |
|
181
|
181
|
} |
|
182
|
182
|
|
|
183
|
183
|
// Store source to order meta |
|
184
|
|
- $this->save_source( $order, $source ); |
|
|
184
|
+ $this->save_source($order, $source); |
|
185
|
185
|
|
|
186
|
186
|
// Remove cart |
|
187
|
187
|
WC()->cart->empty_cart(); |
|
188
|
188
|
|
|
189
|
189
|
// Is pre ordered! |
|
190
|
|
- WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
|
190
|
+ WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); |
|
191
|
191
|
|
|
192
|
192
|
// Return thank you page redirect |
|
193
|
193
|
return array( |
|
194
|
194
|
'result' => 'success', |
|
195
|
|
- 'redirect' => $this->get_return_url( $order ), |
|
|
195
|
+ 'redirect' => $this->get_return_url($order), |
|
196
|
196
|
); |
|
197
|
|
- } catch ( Exception $e ) { |
|
198
|
|
- wc_add_notice( $e->getMessage(), 'error' ); |
|
|
197
|
+ } catch (Exception $e) { |
|
|
198
|
+ wc_add_notice($e->getMessage(), 'error'); |
|
199
|
199
|
return; |
|
200
|
200
|
} |
|
201
|
201
|
} else { |
|
202
|
|
- return parent::process_payment( $order_id, $retry, $force_customer ); |
|
|
202
|
+ return parent::process_payment($order_id, $retry, $force_customer); |
|
203
|
203
|
} |
|
204
|
204
|
} |
|
205
|
205
|
|
|
@@ -208,7 +208,7 @@ discard block |
|
|
block discarded – undo |
|
208
|
208
|
* @param WC_Order $order |
|
209
|
209
|
* @return void |
|
210
|
210
|
*/ |
|
211
|
|
- public function process_pre_order_release_payment( $order ) { |
|
|
211
|
+ public function process_pre_order_release_payment($order) { |
|
212
|
212
|
try { |
|
213
|
213
|
// Define some callbacks if the first attempt fails. |
|
214
|
214
|
$retry_callbacks = array( |
|
@@ -216,32 +216,32 @@ discard block |
|
|
block discarded – undo |
|
216
|
216
|
'remove_order_customer_before_retry', |
|
217
|
217
|
); |
|
218
|
218
|
|
|
219
|
|
- while ( 1 ) { |
|
220
|
|
- $source = $this->get_order_source( $order ); |
|
221
|
|
- $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
|
219
|
+ while (1) { |
|
|
220
|
+ $source = $this->get_order_source($order); |
|
|
221
|
+ $response = WC_Stripe_API::request($this->generate_payment_request($order, $source)); |
|
222
|
222
|
|
|
223
|
|
- if ( is_wp_error( $response ) ) { |
|
224
|
|
- if ( 0 === sizeof( $retry_callbacks ) ) { |
|
225
|
|
- throw new Exception( $response->get_error_message() ); |
|
|
223
|
+ if (is_wp_error($response)) { |
|
|
224
|
+ if (0 === sizeof($retry_callbacks)) { |
|
|
225
|
+ throw new Exception($response->get_error_message()); |
|
226
|
226
|
} else { |
|
227
|
|
- $retry_callback = array_shift( $retry_callbacks ); |
|
228
|
|
- call_user_func( array( $this, $retry_callback ), $order ); |
|
|
227
|
+ $retry_callback = array_shift($retry_callbacks); |
|
|
228
|
+ call_user_func(array($this, $retry_callback), $order); |
|
229
|
229
|
} |
|
230
|
230
|
} else { |
|
231
|
231
|
// Successful |
|
232
|
|
- $this->process_response( $response, $order ); |
|
|
232
|
+ $this->process_response($response, $order); |
|
233
|
233
|
break; |
|
234
|
234
|
} |
|
235
|
235
|
} |
|
236
|
|
- } catch ( Exception $e ) { |
|
237
|
|
- $order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() ); |
|
|
236
|
+ } catch (Exception $e) { |
|
|
237
|
+ $order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage()); |
|
238
|
238
|
|
|
239
|
239
|
// Mark order as failed if not already set, |
|
240
|
240
|
// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
|
241
|
|
- if ( ! $order->has_status( 'failed' ) ) { |
|
242
|
|
- $order->update_status( 'failed', $order_note ); |
|
|
241
|
+ if ( ! $order->has_status('failed')) { |
|
|
242
|
+ $order->update_status('failed', $order_note); |
|
243
|
243
|
} else { |
|
244
|
|
- $order->add_order_note( $order_note ); |
|
|
244
|
+ $order->add_order_note($order_note); |
|
245
|
245
|
} |
|
246
|
246
|
} |
|
247
|
247
|
} |
|
@@ -250,20 +250,20 @@ discard block |
|
|
block discarded – undo |
|
250
|
250
|
* Don't transfer Stripe customer/token meta to resubscribe orders. |
|
251
|
251
|
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
|
252
|
252
|
*/ |
|
253
|
|
- public function delete_resubscribe_meta( $resubscribe_order ) { |
|
254
|
|
- delete_post_meta( ( $this->wc_pre_30 ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_customer_id' ); |
|
255
|
|
- delete_post_meta( ( $this->wc_pre_30 ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_card_id' ); |
|
256
|
|
- $this->delete_renewal_meta( $resubscribe_order ); |
|
|
253
|
+ public function delete_resubscribe_meta($resubscribe_order) { |
|
|
254
|
+ delete_post_meta(($this->wc_pre_30 ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_customer_id'); |
|
|
255
|
+ delete_post_meta(($this->wc_pre_30 ? $resubscribe_order->id : $resubscribe_order->get_id()), '_stripe_card_id'); |
|
|
256
|
+ $this->delete_renewal_meta($resubscribe_order); |
|
257
|
257
|
} |
|
258
|
258
|
|
|
259
|
259
|
/** |
|
260
|
260
|
* Don't transfer Stripe fee/ID meta to renewal orders. |
|
261
|
261
|
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
|
262
|
262
|
*/ |
|
263
|
|
- public function delete_renewal_meta( $renewal_order ) { |
|
264
|
|
- delete_post_meta( ( $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id() ), 'Stripe Fee' ); |
|
265
|
|
- delete_post_meta( ( $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id() ), 'Net Revenue From Stripe' ); |
|
266
|
|
- delete_post_meta( ( $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id() ), 'Stripe Payment ID' ); |
|
|
263
|
+ public function delete_renewal_meta($renewal_order) { |
|
|
264
|
+ delete_post_meta(($this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id()), 'Stripe Fee'); |
|
|
265
|
+ delete_post_meta(($this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id()), 'Net Revenue From Stripe'); |
|
|
266
|
+ delete_post_meta(($this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id()), 'Stripe Payment ID'); |
|
267
|
267
|
return $renewal_order; |
|
268
|
268
|
} |
|
269
|
269
|
|
|
@@ -273,11 +273,11 @@ discard block |
|
|
block discarded – undo |
|
273
|
273
|
* @param $amount_to_charge float The amount to charge. |
|
274
|
274
|
* @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
|
275
|
275
|
*/ |
|
276
|
|
- public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
277
|
|
- $response = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); |
|
|
276
|
+ public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { |
|
|
277
|
+ $response = $this->process_subscription_payment($renewal_order, $amount_to_charge); |
|
278
|
278
|
|
|
279
|
|
- if ( is_wp_error( $response ) ) { |
|
280
|
|
- $renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
|
279
|
+ if (is_wp_error($response)) { |
|
|
280
|
+ $renewal_order->update_status('failed', sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $response->get_error_message())); |
|
281
|
281
|
} |
|
282
|
282
|
} |
|
283
|
283
|
|
|
@@ -285,18 +285,18 @@ discard block |
|
|
block discarded – undo |
|
285
|
285
|
* Remove order meta |
|
286
|
286
|
* @param object $order |
|
287
|
287
|
*/ |
|
288
|
|
- public function remove_order_source_before_retry( $order ) { |
|
|
288
|
+ public function remove_order_source_before_retry($order) { |
|
289
|
289
|
$order_id = $this->wc_pre_30 ? $order->id : $order->get_id(); |
|
290
|
|
- delete_post_meta( $order_id, '_stripe_card_id' ); |
|
|
290
|
+ delete_post_meta($order_id, '_stripe_card_id'); |
|
291
|
291
|
} |
|
292
|
292
|
|
|
293
|
293
|
/** |
|
294
|
294
|
* Remove order meta |
|
295
|
295
|
* @param object $order |
|
296
|
296
|
*/ |
|
297
|
|
- public function remove_order_customer_before_retry( $order ) { |
|
|
297
|
+ public function remove_order_customer_before_retry($order) { |
|
298
|
298
|
$order_id = $this->wc_pre_30 ? $order->id : $order->get_id(); |
|
299
|
|
- delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
|
299
|
+ delete_post_meta($order_id, '_stripe_customer_id'); |
|
300
|
300
|
} |
|
301
|
301
|
|
|
302
|
302
|
/** |
|
@@ -308,13 +308,13 @@ discard block |
|
|
block discarded – undo |
|
308
|
308
|
* @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
|
309
|
309
|
* @return void |
|
310
|
310
|
*/ |
|
311
|
|
- public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
312
|
|
- if ( $this->wc_pre_30 ) { |
|
313
|
|
- update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
314
|
|
- update_post_meta( $subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id ); |
|
|
311
|
+ public function update_failing_payment_method($subscription, $renewal_order) { |
|
|
312
|
+ if ($this->wc_pre_30) { |
|
|
313
|
+ update_post_meta($subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id); |
|
|
314
|
+ update_post_meta($subscription->id, '_stripe_card_id', $renewal_order->stripe_card_id); |
|
315
|
315
|
} else { |
|
316
|
|
- update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( 'stripe_customer_id', true ) ); |
|
317
|
|
- update_post_meta( $subscription->get_id(), '_stripe_card_id', $renewal_order->get_meta( 'stripe_card_id', true ) ); |
|
|
316
|
+ update_post_meta($subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta('stripe_customer_id', true)); |
|
|
317
|
+ update_post_meta($subscription->get_id(), '_stripe_card_id', $renewal_order->get_meta('stripe_card_id', true)); |
|
318
|
318
|
} |
|
319
|
319
|
} |
|
320
|
320
|
|
|
@@ -327,15 +327,15 @@ discard block |
|
|
block discarded – undo |
|
327
|
327
|
* @param WC_Subscription $subscription An instance of a subscription object |
|
328
|
328
|
* @return array |
|
329
|
329
|
*/ |
|
330
|
|
- public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
331
|
|
- $payment_meta[ $this->id ] = array( |
|
|
330
|
+ public function add_subscription_payment_meta($payment_meta, $subscription) { |
|
|
331
|
+ $payment_meta[$this->id] = array( |
|
332
|
332
|
'post_meta' => array( |
|
333
|
333
|
'_stripe_customer_id' => array( |
|
334
|
|
- 'value' => get_post_meta( ( $this->wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ), |
|
|
334
|
+ 'value' => get_post_meta(($this->wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true), |
|
335
|
335
|
'label' => 'Stripe Customer ID', |
|
336
|
336
|
), |
|
337
|
337
|
'_stripe_card_id' => array( |
|
338
|
|
- 'value' => get_post_meta( ( $this->wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ), |
|
|
338
|
+ 'value' => get_post_meta(($this->wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true), |
|
339
|
339
|
'label' => 'Stripe Card ID', |
|
340
|
340
|
), |
|
341
|
341
|
), |
|
@@ -352,17 +352,17 @@ discard block |
|
|
block discarded – undo |
|
352
|
352
|
* @param array $payment_meta associative array of meta data required for automatic payments |
|
353
|
353
|
* @return array |
|
354
|
354
|
*/ |
|
355
|
|
- public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
356
|
|
- if ( $this->id === $payment_method_id ) { |
|
|
355
|
+ public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { |
|
|
356
|
+ if ($this->id === $payment_method_id) { |
|
357
|
357
|
|
|
358
|
|
- if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
359
|
|
- throw new Exception( 'A "_stripe_customer_id" value is required.' ); |
|
360
|
|
- } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
361
|
|
- throw new Exception( 'Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".' ); |
|
|
358
|
+ if ( ! isset($payment_meta['post_meta']['_stripe_customer_id']['value']) || empty($payment_meta['post_meta']['_stripe_customer_id']['value'])) { |
|
|
359
|
+ throw new Exception('A "_stripe_customer_id" value is required.'); |
|
|
360
|
+ } elseif (0 !== strpos($payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_')) { |
|
|
361
|
+ throw new Exception('Invalid customer ID. A valid "_stripe_customer_id" must begin with "cus_".'); |
|
362
|
362
|
} |
|
363
|
363
|
|
|
364
|
|
- if ( ! empty( $payment_meta['post_meta']['_stripe_card_id']['value'] ) && 0 !== strpos( $payment_meta['post_meta']['_stripe_card_id']['value'], 'card_' ) ) { |
|
365
|
|
- throw new Exception( 'Invalid card ID. A valid "_stripe_card_id" must begin with "card_".' ); |
|
|
364
|
+ if ( ! empty($payment_meta['post_meta']['_stripe_card_id']['value']) && 0 !== strpos($payment_meta['post_meta']['_stripe_card_id']['value'], 'card_')) { |
|
|
365
|
+ throw new Exception('Invalid card ID. A valid "_stripe_card_id" must begin with "card_".'); |
|
366
|
366
|
} |
|
367
|
367
|
} |
|
368
|
368
|
} |
|
@@ -375,45 +375,45 @@ discard block |
|
|
block discarded – undo |
|
375
|
375
|
* @param WC_Subscription $subscription the subscription details |
|
376
|
376
|
* @return string the subscription payment method |
|
377
|
377
|
*/ |
|
378
|
|
- public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
|
378
|
+ public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { |
|
379
|
379
|
$customer_user = $this->wc_pre_30 ? $subscription->customer_user : $subscription->get_customer_id(); |
|
380
|
380
|
|
|
381
|
381
|
// bail for other payment methods |
|
382
|
|
- if ( $this->id !== ( $this->wc_pre_30 ? $subscription->payment_method : $subscription->get_payment_method() ) || ! $customer_user ) { |
|
|
382
|
+ if ($this->id !== ($this->wc_pre_30 ? $subscription->payment_method : $subscription->get_payment_method()) || ! $customer_user) { |
|
383
|
383
|
return $payment_method_to_display; |
|
384
|
384
|
} |
|
385
|
385
|
|
|
386
|
386
|
$stripe_customer = new WC_Stripe_Customer(); |
|
387
|
|
- $stripe_customer_id = get_post_meta( ( $this->wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ); |
|
388
|
|
- $stripe_card_id = get_post_meta( ( $this->wc_pre_30 ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
|
387
|
+ $stripe_customer_id = get_post_meta(($this->wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_customer_id', true); |
|
|
388
|
+ $stripe_card_id = get_post_meta(($this->wc_pre_30 ? $subscription->id : $subscription->get_id()), '_stripe_card_id', true); |
|
389
|
389
|
|
|
390
|
390
|
// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
|
391
|
|
- if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
|
391
|
+ if ( ! $stripe_customer_id || ! is_string($stripe_customer_id)) { |
|
392
|
392
|
$user_id = $customer_user; |
|
393
|
|
- $stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
394
|
|
- $stripe_card_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
|
393
|
+ $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); |
|
|
394
|
+ $stripe_card_id = get_user_meta($user_id, '_stripe_card_id', true); |
|
395
|
395
|
} |
|
396
|
396
|
|
|
397
|
397
|
// If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
|
398
|
|
- if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
399
|
|
- $stripe_customer_id = get_post_meta( ( $this->wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_customer_id', true ); |
|
400
|
|
- $stripe_card_id = get_post_meta( ( $this->wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_card_id', true ); |
|
|
398
|
+ if (( ! $stripe_customer_id || ! is_string($stripe_customer_id)) && false !== $subscription->order) { |
|
|
399
|
+ $stripe_customer_id = get_post_meta(($this->wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_customer_id', true); |
|
|
400
|
+ $stripe_card_id = get_post_meta(($this->wc_pre_30 ? $subscription->order->id : $subscription->get_parent_id()), '_stripe_card_id', true); |
|
401
|
401
|
} |
|
402
|
402
|
|
|
403
|
|
- $stripe_customer->set_id( $stripe_customer_id ); |
|
|
403
|
+ $stripe_customer->set_id($stripe_customer_id); |
|
404
|
404
|
$cards = $stripe_customer->get_cards(); |
|
405
|
405
|
|
|
406
|
|
- if ( $cards ) { |
|
|
406
|
+ if ($cards) { |
|
407
|
407
|
$found_card = false; |
|
408
|
|
- foreach ( $cards as $card ) { |
|
409
|
|
- if ( $card->id === $stripe_card_id ) { |
|
|
408
|
+ foreach ($cards as $card) { |
|
|
409
|
+ if ($card->id === $stripe_card_id) { |
|
410
|
410
|
$found_card = true; |
|
411
|
|
- $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 ); |
|
|
411
|
+ $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); |
|
412
|
412
|
break; |
|
413
|
413
|
} |
|
414
|
414
|
} |
|
415
|
|
- if ( ! $found_card ) { |
|
416
|
|
- $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 ); |
|
|
415
|
+ if ( ! $found_card) { |
|
|
416
|
+ $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); |
|
417
|
417
|
} |
|
418
|
418
|
} |
|
419
|
419
|
|
|
@@ -428,11 +428,11 @@ discard block |
|
|
block discarded – undo |
|
428
|
428
|
* |
|
429
|
429
|
* @param string $message |
|
430
|
430
|
*/ |
|
431
|
|
- public function log( $message ) { |
|
432
|
|
- $options = get_option( 'woocommerce_stripe_settings' ); |
|
|
431
|
+ public function log($message) { |
|
|
432
|
+ $options = get_option('woocommerce_stripe_settings'); |
|
433
|
433
|
|
|
434
|
|
- if ( 'yes' === $options['logging'] ) { |
|
435
|
|
- WC_Stripe::log( $message ); |
|
|
434
|
+ if ('yes' === $options['logging']) { |
|
|
435
|
+ WC_Stripe::log($message); |
|
436
|
436
|
} |
|
437
|
437
|
} |
|
438
|
438
|
} |