class-wc-stripe-upe-payment-method.php
8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Abstract UPE Payment Method class
*
* Handles general functionality for UPE payment methods
*/
/**
* Extendable abstract class for payment methods.
*/
abstract class WC_Stripe_UPE_Payment_Method {
use WC_Stripe_Subscriptions_Utilities_Trait;
use WC_Stripe_Pre_Orders_Trait;
/**
* Stripe key name
*
* @var string
*/
protected $stripe_id;
/**
* Display title
*
* @var string
*/
protected $title;
/**
* Method label
*
* @var string
*/
protected $label;
/**
* Method description
*
* @var string
*/
protected $description;
/**
* Can payment method be saved or reused?
*
* @var bool
*/
protected $is_reusable;
/**
* Array of currencies supported by this UPE method
*
* @var array
*/
protected $supported_currencies;
/**
* Can this payment method be refunded?
*
* @var array
*/
protected $can_refund = true;
/**
* Wether this UPE method is enabled
*
* @var bool
*/
protected $enabled;
/**
* List of supported countries
*
* @var array
*/
protected $supported_countries;
/**
* Create instance of payment method
*/
public function __construct() {
$main_settings = get_option( 'woocommerce_stripe_settings' );
if ( isset( $main_settings['upe_checkout_experience_accepted_payments'] ) ) {
$enabled_upe_methods = $main_settings['upe_checkout_experience_accepted_payments'];
} else {
$enabled_upe_methods = [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ];
}
$this->enabled = in_array( static::STRIPE_ID, $enabled_upe_methods, true );
}
/**
* Returns payment method ID
*
* @return string
*/
public function get_id() {
return $this->stripe_id;
}
/**
* Returns true if the UPE method is enabled.
*
* @return bool
*/
public function is_enabled() {
return $this->enabled;
}
/**
* Returns true if the UPE method is available.
*
* @return bool
*/
public function is_available() {
return true;
}
/**
* Returns payment method title
*
* @param array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
public function get_title( $payment_details = false ) {
return $this->title;
}
/**
* Returns payment method label
*
* @return string
*/
public function get_label() {
return $this->label;
}
/**
* Returns payment method description
*
* @return string
*/
public function get_description() {
return $this->description;
}
/**
* Returns boolean dependent on whether payment method
* can be used at checkout
*
* @param int|null $order_id
* @return bool
*/
public function is_enabled_at_checkout( $order_id = null ) {
// Check capabilities first.
if ( ! $this->is_capability_active() ) {
return false;
}
// Check currency compatibility.
$currencies = $this->get_supported_currencies();
if ( ! empty( $currencies ) && ! in_array( $this->get_woocommerce_currency(), $currencies, true ) ) {
return false;
}
// If cart or order contains subscription, enable payment method if it's reusable.
if ( $this->is_subscription_item_in_cart() || ( ! empty( $order_id ) && $this->has_subscription( $order_id ) ) ) {
return $this->is_reusable();
}
// If cart or order contains pre-order, enable payment method if it's reusable.
if ( $this->is_pre_order_item_in_cart() || ( ! empty( $order_id ) && $this->has_pre_order( $order_id ) ) ) {
return $this->is_reusable();
}
return true;
}
/**
* Validates if a payment method is available on a given country
*
* @param string $country a two-letter country code
*
* @return bool Will return true if supported_countries is empty on payment method
*/
public function is_allowed_on_country( $country ) {
if ( ! empty( $this->supported_countries ) ) {
return in_array( $country, $this->supported_countries );
}
return true;
}
/**
* Returns boolean dependent on whether payment method
* will support saved payments/subscription payments
*
* @return bool
*/
public function is_reusable() {
return $this->is_reusable;
}
/**
* Returns boolean dependent on whether capability
* for site account is enabled for payment method.
*
* @return bool
*/
public function is_capability_active() {
// Treat all capabilities as active when in test mode.
$plugin_settings = get_option( 'woocommerce_stripe_settings' );
$test_mode_setting = ! empty( $plugin_settings['testmode'] ) ? $plugin_settings['testmode'] : 'no';
if ( 'yes' === $test_mode_setting ) {
return true;
}
// Otherwise, make sure the capability is available.
$capabilities = $this->get_capabilities_response();
if ( empty( $capabilities ) ) {
return false;
}
$key = $this->get_id() . '_payments';
return isset( $capabilities[ $key ] ) && 'active' === $capabilities[ $key ];
}
/**
* Returns capabilities response object for site account.
*
* @return object
*/
public function get_capabilities_response() {
$account = WC_Stripe::get_instance()->account;
$data = $account->get_cached_account_data();
if ( empty( $data ) || ! isset( $data['capabilities'] ) ) {
return [];
}
return $data['capabilities'];
}
/**
* Returns string representing payment method type
* to query to retrieve saved payment methods from Stripe.
*/
public function get_retrievable_type() {
return $this->is_reusable() ? WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID : null;
}
/**
* Create new WC payment token and add to user.
*
* @param int $user_id WP_User ID
* @param object $payment_method Stripe payment method object
*
* @return WC_Payment_Token_SEPA
*/
public function create_payment_token_for_user( $user_id, $payment_method ) {
$token = new WC_Payment_Token_SEPA();
$token->set_last4( $payment_method->sepa_debit->last4 );
$token->set_gateway_id( WC_Stripe_UPE_Payment_Gateway::ID );
$token->set_token( $payment_method->id );
$token->set_payment_method_type( $this->get_id() );
$token->set_user_id( $user_id );
$token->save();
return $token;
}
/**
* Returns the currencies this UPE method supports.
*
* @return array|null
*/
public function get_supported_currencies() {
return apply_filters(
'wc_stripe_' . static::STRIPE_ID . '_upe_supported_currencies',
$this->supported_currencies
);
}
/**
* Wrapper function for get_woocommerce_currency global function
*/
public function get_woocommerce_currency() {
return get_woocommerce_currency();
}
/**
* Returns whether the payment method requires automatic capture.
* By default all the UPE payment methods require automatic capture, except for "card".
*
* @return bool
*/
public function requires_automatic_capture() {
return true;
}
/**
* Returns the HTML for the subtext messaging in the old settings UI.
*
* @param string $stripe_method_status (optional) Status of this payment method based on the Stripe's account capabilities
* @return string
*/
public function get_subtext_messages( $stripe_method_status ) {
// can be either a `currency` or `activation` messaging, to be displayed in the old settings UI.
$messages = [];
if ( ! empty( $stripe_method_status ) && 'active' !== $stripe_method_status ) {
$text = __( 'Pending activation', 'woocommerce-gateway-stripe' );
$tooltip_content = sprintf(
/* translators: %1: Payment method name */
esc_attr__( '%1$s won\'t be visible to your customers until you provide the required information. Follow the instructions Stripe has sent to your e-mail address.', 'woocommerce-gateway-stripe' ),
$this->get_label()
);
$messages[] = $text . '<span class="tips" data-tip="' . $tooltip_content . '"><span class="woocommerce-help-tip" style="margin-top: 0;"></span></span>';
}
$currencies = $this->get_supported_currencies();
if ( ! empty( $currencies ) && ! in_array( get_woocommerce_currency(), $currencies, true ) ) {
/* translators: %s: List of comma-separated currencies. */
$tooltip_content = sprintf( esc_attr__( 'In order to be used at checkout, the payment method requires the store currency to be set to one of: %s', 'woocommerce-gateway-stripe' ), implode( ', ', $currencies ) );
$text = __( 'Requires currency', 'woocommerce-gateway-stripe' );
$messages[] = $text . '<span class="tips" data-tip="' . $tooltip_content . '"><span class="woocommerce-help-tip" style="margin-top: 0;"></span></span>';
}
return count( $messages ) > 0 ? join( ' – ', $messages ) : '';
}
/**
* Checks if payment method allows refund via stripe
*
* @return bool
*/
public function can_refund_via_stripe() {
return $this->can_refund;
}
}