|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Compatibility class for Subscriptions and Pre-Orders. |
|
8
|
|
|
* |
|
9
|
|
|
* @extends WC_Gateway_Stripe |
|
10
|
|
|
*/ |
|
11
|
|
|
class WC_Stripe_Compat extends WC_Gateway_Stripe { |
|
12
|
|
|
/** |
|
13
|
|
|
* Constructor |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct() { |
|
16
|
|
|
parent::__construct(); |
|
17
|
|
|
|
|
18
|
|
|
if ( class_exists( 'WC_Subscriptions_Order' ) ) { |
|
19
|
|
|
add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 ); |
|
20
|
|
|
add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 ); |
|
21
|
|
|
add_action( 'wcs_renewal_order_created', array( $this, 'delete_renewal_meta' ), 10 ); |
|
22
|
|
|
add_action( 'woocommerce_subscription_failing_payment_method_updated_stripe', array( $this, 'update_failing_payment_method' ), 10, 2 ); |
|
23
|
|
|
|
|
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 ); |
|
26
|
|
|
|
|
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' ) ); |
|
31
|
|
|
add_filter( 'wc_stripe_payment_metadata', array( $this, 'add_subscription_meta_data' ), 10, 2 ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ( class_exists( 'WC_Pre_Orders_Order' ) ) { |
|
35
|
|
|
add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ) ); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Checks to see if we need to hide the save checkbox field. |
|
41
|
|
|
* Because when cart contains a subs product, it will save regardless. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 4.0.0 |
|
44
|
|
|
* @version 4.0.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
public function maybe_hide_save_checkbox( $display_tokenization ) { |
|
47
|
|
|
if ( WC_Subscriptions_Cart::cart_contains_subscription() ) { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $display_tokenization; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Is $order_id a subscription? |
|
56
|
|
|
* @param int $order_id |
|
57
|
|
|
* @return boolean |
|
58
|
|
|
*/ |
|
59
|
|
|
public function has_subscription( $order_id ) { |
|
60
|
|
|
return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Is $order_id a pre-order? |
|
65
|
|
|
* @param int $order_id |
|
66
|
|
|
* @return boolean |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function is_pre_order( $order_id ) { |
|
69
|
|
|
return ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id ) ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Process the payment based on type. |
|
74
|
|
|
* @param int $order_id |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
|
78
|
|
|
if ( $this->has_subscription( $order_id ) ) { |
|
79
|
|
|
// Regular payment with force customer enabled |
|
80
|
|
|
return parent::process_payment( $order_id, true, true ); |
|
81
|
|
|
} elseif ( $this->is_pre_order( $order_id ) ) { |
|
82
|
|
|
return $this->process_pre_order( $order_id, $retry, $force_save_source ); |
|
83
|
|
|
} else { |
|
84
|
|
|
return parent::process_payment( $order_id, $retry, $force_save_source ); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Adds subscription related meta data on charge request. |
|
90
|
|
|
* |
|
91
|
|
|
* @since 4.0.0 |
|
92
|
|
|
* @param array $metadata |
|
93
|
|
|
* @param object $order |
|
94
|
|
|
*/ |
|
95
|
|
|
public function add_subscription_meta_data( $metadata, $order ) { |
|
96
|
|
|
if ( ! $this->has_subscription( $order->get_id() ) ) { |
|
97
|
|
|
return $metadata; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $metadata += array( |
|
101
|
|
|
'payment_type' => 'recurring', |
|
102
|
|
|
'site_url' => esc_url( get_site_url() ), |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Updates other subscription sources. |
|
108
|
|
|
* |
|
109
|
|
|
* @since 3.1.0 |
|
110
|
|
|
* @version 4.0.0 |
|
111
|
|
|
*/ |
|
112
|
|
|
public function save_source( $order, $source ) { |
|
113
|
|
|
parent::save_source( $order, $source ); |
|
114
|
|
|
|
|
115
|
|
|
$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
|
116
|
|
|
|
|
117
|
|
|
// Also store it on the subscriptions being purchased or paid for in the order |
|
118
|
|
|
if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) { |
|
119
|
|
|
$subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
|
120
|
|
|
} elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) { |
|
121
|
|
|
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); |
|
122
|
|
|
} else { |
|
123
|
|
|
$subscriptions = array(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
foreach ( $subscriptions as $subscription ) { |
|
127
|
|
|
$subscription_id = WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id(); |
|
128
|
|
|
update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer ); |
|
129
|
|
|
update_post_meta( $subscription_id, '_stripe_source_id', $source->source ); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* process_subscription_payment function. |
|
135
|
|
|
* @param mixed $order |
|
136
|
|
|
* @param int $amount (default: 0) |
|
137
|
|
|
* @param string $stripe_token (default: '') |
|
|
|
|
|
|
138
|
|
|
* @param bool initial_payment |
|
139
|
|
|
*/ |
|
140
|
|
|
public function process_subscription_payment( $order = '', $amount = 0 ) { |
|
141
|
|
View Code Duplication |
if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
|
|
|
|
|
142
|
|
|
/* translators: minimum amount */ |
|
143
|
|
|
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 ) ) ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$customer_id = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
|
|
|
|
|
|
147
|
|
|
$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
|
148
|
|
|
|
|
149
|
|
|
// Get source from order |
|
150
|
|
|
$prepared_source = $this->prepare_order_source( $order ); |
|
151
|
|
|
|
|
152
|
|
|
// Or fail :( |
|
|
|
|
|
|
153
|
|
|
if ( ! $prepared_source->customer ) { |
|
154
|
|
|
return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
158
|
|
|
|
|
159
|
|
|
// Make the request |
|
160
|
|
|
$request = $this->generate_payment_request( $order, $prepared_source ); |
|
161
|
|
|
$request['capture'] = 'true'; |
|
162
|
|
|
$request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
163
|
|
|
$response = WC_Stripe_API::request( $request ); |
|
164
|
|
|
|
|
165
|
|
|
// Process valid response |
|
166
|
|
|
if ( ! empty( $response->error ) ) { |
|
167
|
|
|
return $response; // Default catch all errors. |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$this->process_response( $response, $order ); |
|
171
|
|
|
|
|
172
|
|
|
return $response; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Process a retry for subscriptions with default source. |
|
177
|
|
|
* This is used when renewal failed. |
|
178
|
|
|
* |
|
179
|
|
|
* @todo refactor to avoid DRY. |
|
180
|
|
|
* @param mixed $order |
|
181
|
|
|
* @param int $amount (default: 0) |
|
182
|
|
|
* @param string $stripe_token (default: '') |
|
|
|
|
|
|
183
|
|
|
* @param bool initial_payment |
|
184
|
|
|
*/ |
|
185
|
|
|
public function retry_subscription_payment( $order = '', $amount = 0 ) { |
|
186
|
|
View Code Duplication |
if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
|
|
|
|
|
187
|
|
|
/* translators: minimum amount */ |
|
188
|
|
|
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 ) ) ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$customer_id = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
|
|
|
|
|
|
192
|
|
|
$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
|
193
|
|
|
|
|
194
|
|
|
// Get source from order |
|
195
|
|
|
$prepared_source = $this->prepare_order_source( $order ); |
|
196
|
|
|
|
|
197
|
|
|
// Or fail :( |
|
|
|
|
|
|
198
|
|
|
if ( ! $prepared_source->customer ) { |
|
199
|
|
|
return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// Passing empty source with charge customer default. |
|
203
|
|
|
$prepared_source->source = ''; |
|
204
|
|
|
|
|
205
|
|
|
WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
206
|
|
|
|
|
207
|
|
|
// Make the request |
|
208
|
|
|
$request = $this->generate_payment_request( $order, $prepared_source ); |
|
209
|
|
|
$request['capture'] = 'true'; |
|
210
|
|
|
$request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
211
|
|
|
$response = WC_Stripe_API::request( $request ); |
|
212
|
|
|
|
|
213
|
|
|
if ( ! empty( $response->error ) || is_wp_error( $response ) ) { |
|
214
|
|
|
/* translators: error message */ |
|
215
|
|
|
$renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->error->message ) ); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$this->process_response( $response, $order ); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Don't transfer Stripe customer/token meta to resubscribe orders. |
|
223
|
|
|
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
|
224
|
|
|
*/ |
|
225
|
|
|
public function delete_resubscribe_meta( $resubscribe_order ) { |
|
226
|
|
|
delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_customer_id' ); |
|
|
|
|
|
|
227
|
|
|
delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_source_id' ); |
|
|
|
|
|
|
228
|
|
|
// For BW compat will remove in future |
|
229
|
|
|
delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $resubscribe_order->id : $resubscribe_order->get_id() ), '_stripe_card_id' ); |
|
|
|
|
|
|
230
|
|
|
$this->delete_renewal_meta( $resubscribe_order ); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Don't transfer Stripe fee/ID meta to renewal orders. |
|
235
|
|
|
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription |
|
|
|
|
|
|
236
|
|
|
*/ |
|
237
|
|
|
public function delete_renewal_meta( $renewal_order ) { |
|
238
|
|
|
delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id() ), 'Stripe Fee' ); |
|
239
|
|
|
delete_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id() ), 'Net Revenue From Stripe' ); |
|
240
|
|
|
return $renewal_order; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* scheduled_subscription_payment function. |
|
245
|
|
|
* |
|
246
|
|
|
* @param $amount_to_charge float The amount to charge. |
|
247
|
|
|
* @param $renewal_order WC_Order A WC_Order object created to record the renewal payment. |
|
248
|
|
|
*/ |
|
249
|
|
|
public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { |
|
250
|
|
|
$response = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); |
|
251
|
|
|
|
|
252
|
|
|
if ( is_wp_error( $response ) ) { |
|
253
|
|
|
/* translators: error message */ |
|
254
|
|
|
$renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if ( ! empty( $response->error ) ) { |
|
258
|
|
|
// This is a very generic error to listen for but worth a retry before total fail. |
|
259
|
|
|
if ( isset( $response->error->type ) && 'invalid_request_error' === $response->error->type && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) { |
|
260
|
|
|
$this->retry_subscription_payment( $renewal_order, $amount_to_charge ); |
|
261
|
|
|
} else { |
|
262
|
|
|
/* translators: error message */ |
|
263
|
|
|
$renewal_order->update_status( 'failed', sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $response->error->message ) ); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Remove order meta |
|
270
|
|
|
* @param object $order |
|
271
|
|
|
*/ |
|
272
|
|
|
public function remove_order_source_before_retry( $order ) { |
|
273
|
|
|
$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
|
274
|
|
|
delete_post_meta( $order_id, '_stripe_source_id' ); |
|
275
|
|
|
// For BW compat will remove in the future. |
|
276
|
|
|
delete_post_meta( $order_id, '_stripe_card_id' ); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Remove order meta |
|
281
|
|
|
* @param object $order |
|
282
|
|
|
*/ |
|
283
|
|
|
public function remove_order_customer_before_retry( $order ) { |
|
284
|
|
|
$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
|
285
|
|
|
delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Update the customer_id for a subscription after using Stripe to complete a payment to make up for |
|
290
|
|
|
* an automatic renewal payment which previously failed. |
|
291
|
|
|
* |
|
292
|
|
|
* @access public |
|
293
|
|
|
* @param WC_Subscription $subscription The subscription for which the failing payment method relates. |
|
294
|
|
|
* @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
|
|
public function update_failing_payment_method( $subscription, $renewal_order ) { |
|
298
|
|
|
if ( WC_Stripe_Helper::is_pre_30() ) { |
|
299
|
|
|
update_post_meta( $subscription->id, '_stripe_customer_id', $renewal_order->stripe_customer_id ); |
|
300
|
|
|
update_post_meta( $subscription->id, '_stripe_source_id', $renewal_order->stripe_source_id ); |
|
301
|
|
|
|
|
302
|
|
|
} else { |
|
303
|
|
|
update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); |
|
304
|
|
|
update_post_meta( $subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) ); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Include the payment meta data required to process automatic recurring payments so that store managers can |
|
310
|
|
|
* manually set up automatic recurring payments for a customer via the Edit Subscriptions screen in 2.0+. |
|
311
|
|
|
* |
|
312
|
|
|
* @since 2.5 |
|
313
|
|
|
* @param array $payment_meta associative array of meta data required for automatic payments |
|
314
|
|
|
* @param WC_Subscription $subscription An instance of a subscription object |
|
315
|
|
|
* @return array |
|
316
|
|
|
*/ |
|
317
|
|
|
public function add_subscription_payment_meta( $payment_meta, $subscription ) { |
|
318
|
|
|
$source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_source_id', true ); |
|
319
|
|
|
|
|
320
|
|
|
// For BW compat will remove in future. |
|
321
|
|
|
if ( empty( $source_id ) ) { |
|
322
|
|
|
$source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
323
|
|
|
|
|
324
|
|
|
// Take this opportunity to update the key name. |
|
325
|
|
|
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 ); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$payment_meta[ $this->id ] = array( |
|
329
|
|
|
'post_meta' => array( |
|
330
|
|
|
'_stripe_customer_id' => array( |
|
331
|
|
|
'value' => get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ), |
|
332
|
|
|
'label' => 'Stripe Customer ID', |
|
333
|
|
|
), |
|
334
|
|
|
'_stripe_source_id' => array( |
|
335
|
|
|
'value' => $source_id, |
|
336
|
|
|
'label' => 'Stripe Source ID', |
|
337
|
|
|
), |
|
338
|
|
|
), |
|
339
|
|
|
); |
|
340
|
|
|
|
|
341
|
|
|
return $payment_meta; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Validate the payment meta data required to process automatic recurring payments so that store managers can |
|
346
|
|
|
* manually set up automatic recurring payments for a customer via the Edit Subscriptions screen in 2.0+. |
|
347
|
|
|
* |
|
348
|
|
|
* @since 2.5 |
|
349
|
|
|
* @since 4.0.4 Stripe sourd id field no longer needs to be required. |
|
350
|
|
|
* @param string $payment_method_id The ID of the payment method to validate |
|
351
|
|
|
* @param array $payment_meta associative array of meta data required for automatic payments |
|
352
|
|
|
* @return array |
|
353
|
|
|
*/ |
|
354
|
|
|
public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) { |
|
355
|
|
|
if ( $this->id === $payment_method_id ) { |
|
356
|
|
|
|
|
357
|
|
|
if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) { |
|
358
|
|
|
throw new Exception( __( 'A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe' ) ); |
|
359
|
|
View Code Duplication |
} elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) { |
|
|
|
|
|
|
360
|
|
|
throw new Exception( __( 'Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe' ) ); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
if ( |
|
364
|
|
|
( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
365
|
|
|
&& 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'card_' ) ) |
|
366
|
|
|
&& ( ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) |
|
367
|
|
|
&& 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'src_' ) ) ) { |
|
368
|
|
|
|
|
369
|
|
|
throw new Exception( __( 'Invalid source ID. A valid source "Stripe Source ID" must begin with "src_" or "card_".', 'woocommerce-gateway-stripe' ) ); |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Render the payment method used for a subscription in the "My Subscriptions" table |
|
376
|
|
|
* |
|
377
|
|
|
* @since 1.7.5 |
|
378
|
|
|
* @param string $payment_method_to_display the default payment method text to display |
|
379
|
|
|
* @param WC_Subscription $subscription the subscription details |
|
380
|
|
|
* @return string the subscription payment method |
|
381
|
|
|
*/ |
|
382
|
|
|
public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) { |
|
383
|
|
|
$customer_user = WC_Stripe_Helper::is_pre_30() ? $subscription->customer_user : $subscription->get_customer_id(); |
|
384
|
|
|
|
|
385
|
|
|
// bail for other payment methods |
|
386
|
|
|
if ( ( WC_Stripe_Helper::is_pre_30() ? $subscription->payment_method : $subscription->get_payment_method() ) !== $this->id || ! $customer_user ) { |
|
387
|
|
|
return $payment_method_to_display; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
$stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_source_id', true ); |
|
391
|
|
|
|
|
392
|
|
|
// For BW compat will remove in future. |
|
393
|
|
|
if ( empty( $stripe_source_id ) ) { |
|
394
|
|
|
$stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_card_id', true ); |
|
395
|
|
|
|
|
396
|
|
|
// Take this opportunity to update the key name. |
|
397
|
|
|
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 ); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
$stripe_customer = new WC_Stripe_Customer(); |
|
401
|
|
|
$stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->id : $subscription->get_id() ), '_stripe_customer_id', true ); |
|
402
|
|
|
|
|
403
|
|
|
// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. |
|
404
|
|
|
if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) { |
|
405
|
|
|
$user_id = $customer_user; |
|
406
|
|
|
$stripe_customer_id = get_user_meta( $user_id, '_stripe_customer_id', true ); |
|
407
|
|
|
$stripe_source_id = get_user_meta( $user_id, '_stripe_source_id', true ); |
|
408
|
|
|
|
|
409
|
|
|
// For BW compat will remove in future. |
|
410
|
|
|
if ( empty( $stripe_source_id ) ) { |
|
411
|
|
|
$stripe_source_id = get_user_meta( $user_id, '_stripe_card_id', true ); |
|
412
|
|
|
|
|
413
|
|
|
// Take this opportunity to update the key name. |
|
414
|
|
|
update_user_meta( $user_id, '_stripe_source_id', $stripe_source_id ); |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
// If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. |
|
419
|
|
|
if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->order ) { |
|
420
|
|
|
$stripe_customer_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_customer_id', true ); |
|
421
|
|
|
$stripe_source_id = get_post_meta( ( WC_Stripe_Helper::is_pre_30() ? $subscription->order->id : $subscription->get_parent_id() ), '_stripe_source_id', true ); |
|
422
|
|
|
|
|
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->order->id : $subscription->get_parent_id() ), '_stripe_card_id', true ); |
|
426
|
|
|
|
|
427
|
|
|
// Take this opportunity to update the key name. |
|
428
|
|
|
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 ); |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
$stripe_customer->set_id( $stripe_customer_id ); |
|
433
|
|
|
$sources = $stripe_customer->get_sources(); |
|
434
|
|
|
|
|
435
|
|
|
if ( $sources ) { |
|
|
|
|
|
|
436
|
|
|
$found_source = false; |
|
437
|
|
|
foreach ( $sources as $source ) { |
|
438
|
|
|
if ( isset( $source->type ) && 'card' === $source->type ) { |
|
439
|
|
|
$card = $source->card; |
|
440
|
|
|
} elseif ( isset( $source->object ) && 'card' === $source->object ) { |
|
441
|
|
|
$card = $source; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
if ( $source->id === $stripe_source_id ) { |
|
445
|
|
|
$found_source = true; |
|
446
|
|
|
|
|
447
|
|
|
if ( $card ) { |
|
448
|
|
|
/* translators: 1) card brand 2) last 4 digits */ |
|
449
|
|
|
$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 ); |
|
|
|
|
|
|
450
|
|
|
} else { |
|
451
|
|
|
$payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' ); |
|
452
|
|
|
} |
|
453
|
|
|
break; |
|
454
|
|
|
} |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
if ( ! $found_source ) { |
|
458
|
|
|
if ( 'card' === $sources[0]->type ) { |
|
459
|
|
|
$card = $sources[0]->card; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
if ( $card ) { |
|
463
|
|
|
/* translators: 1) card brand 2) last 4 digits */ |
|
464
|
|
|
$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 ); |
|
465
|
|
|
} else { |
|
466
|
|
|
$payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' ); |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return $payment_method_to_display; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Process the pre-order |
|
476
|
|
|
* @param int $order_id |
|
477
|
|
|
* @return array |
|
478
|
|
|
*/ |
|
479
|
|
|
public function process_pre_order( $order_id, $retry, $force_save_source ) { |
|
480
|
|
|
if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) { |
|
481
|
|
|
try { |
|
482
|
|
|
$order = wc_get_order( $order_id ); |
|
483
|
|
|
|
|
484
|
|
View Code Duplication |
if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
|
|
|
|
|
485
|
|
|
/* translators: minimum amount */ |
|
486
|
|
|
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_Helper::get_minimum_amount() / 100 ) ) ); |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
$source = $this->prepare_source( $this->create_source_object(), get_current_user_id(), true ); |
|
490
|
|
|
|
|
491
|
|
|
// We need a source on file to continue. |
|
492
|
|
|
if ( empty( $source->customer ) || empty( $source->source ) ) { |
|
493
|
|
|
throw new Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
// Store source to order meta |
|
497
|
|
|
$this->save_source( $order, $source ); |
|
498
|
|
|
|
|
499
|
|
|
// Remove cart |
|
500
|
|
|
WC()->cart->empty_cart(); |
|
501
|
|
|
|
|
502
|
|
|
// Is pre ordered! |
|
503
|
|
|
WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
504
|
|
|
|
|
505
|
|
|
// Return thank you page redirect |
|
506
|
|
|
return array( |
|
507
|
|
|
'result' => 'success', |
|
508
|
|
|
'redirect' => $this->get_return_url( $order ), |
|
509
|
|
|
); |
|
510
|
|
|
} catch ( Exception $e ) { |
|
511
|
|
|
wc_add_notice( $e->getMessage(), 'error' ); |
|
512
|
|
|
return; |
|
513
|
|
|
} |
|
514
|
|
|
} else { |
|
515
|
|
|
return parent::process_payment( $order_id, $retry, $force_save_source ); |
|
|
|
|
|
|
516
|
|
|
} |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
/** |
|
520
|
|
|
* Process a pre-order payment when the pre-order is released |
|
521
|
|
|
* @param WC_Order $order |
|
522
|
|
|
* @return void |
|
523
|
|
|
*/ |
|
524
|
|
|
public function process_pre_order_release_payment( $order ) { |
|
525
|
|
|
try { |
|
526
|
|
|
// Define some callbacks if the first attempt fails. |
|
527
|
|
|
$retry_callbacks = array( |
|
528
|
|
|
'remove_order_source_before_retry', |
|
529
|
|
|
'remove_order_customer_before_retry', |
|
530
|
|
|
); |
|
531
|
|
|
|
|
532
|
|
|
while ( 1 ) { |
|
533
|
|
|
$source = $this->prepare_order_source( $order ); |
|
534
|
|
|
$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source ) ); |
|
535
|
|
|
|
|
536
|
|
|
if ( ! empty( $response->error ) ) { |
|
537
|
|
|
if ( 0 === sizeof( $retry_callbacks ) ) { |
|
538
|
|
|
throw new Exception( $response->error->message ); |
|
539
|
|
|
} else { |
|
540
|
|
|
$retry_callback = array_shift( $retry_callbacks ); |
|
541
|
|
|
call_user_func( array( $this, $retry_callback ), $order ); |
|
542
|
|
|
} |
|
543
|
|
|
} else { |
|
544
|
|
|
// Successful |
|
545
|
|
|
$this->process_response( $response, $order ); |
|
546
|
|
|
break; |
|
547
|
|
|
} |
|
548
|
|
|
} |
|
549
|
|
|
} catch ( Exception $e ) { |
|
550
|
|
|
/* translators: error message */ |
|
551
|
|
|
$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $e->getMessage() ); |
|
552
|
|
|
|
|
553
|
|
|
// Mark order as failed if not already set, |
|
554
|
|
|
// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
|
555
|
|
|
if ( ! $order->has_status( 'failed' ) ) { |
|
556
|
|
|
$order->update_status( 'failed', $order_note ); |
|
557
|
|
|
} else { |
|
558
|
|
|
$order->add_order_note( $order_note ); |
|
559
|
|
|
} |
|
560
|
|
|
} |
|
561
|
|
|
} |
|
562
|
|
|
} |
|
563
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.