class-wc-stripe-blocks-support.php
13.6 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\Blocks\Payments\PaymentResult;
use Automattic\WooCommerce\Blocks\Payments\PaymentContext;
defined( 'ABSPATH' ) || exit;
/**
* WC_Stripe_Blocks_Support class.
*
* @extends AbstractPaymentMethodType
*/
final class WC_Stripe_Blocks_Support extends AbstractPaymentMethodType {
/**
* Payment method name defined by payment methods extending this class.
*
* @var string
*/
protected $name = 'stripe';
/**
* The Payment Request configuration class used for Shortcode PRBs. We use it here to retrieve
* the same configurations.
*
* @var WC_Stripe_Payment_Request
*/
private $payment_request_configuration;
/**
* Constructor
*
* @param WC_Stripe_Payment_Request The Stripe Payment Request configuration used for Payment
* Request buttons.
*/
public function __construct( $payment_request_configuration = null ) {
add_action( 'woocommerce_rest_checkout_process_payment_with_context', [ $this, 'add_payment_request_order_meta' ], 8, 2 );
add_action( 'woocommerce_rest_checkout_process_payment_with_context', [ $this, 'add_stripe_intents' ], 9999, 2 );
$this->payment_request_configuration = null !== $payment_request_configuration ? $payment_request_configuration : new WC_Stripe_Payment_Request();
}
/**
* Initializes the payment method type.
*/
public function initialize() {
$this->settings = get_option( 'woocommerce_stripe_settings', [] );
}
/**
* Returns if this payment method should be active. If false, the scripts will not be enqueued.
*
* @return boolean
*/
public function is_active() {
return ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'];
}
/**
* Returns an array of scripts/handles to be registered for this payment method.
*
* @return array
*/
public function get_payment_method_script_handles() {
// Ensure Stripe JS is enqueued
wp_register_script(
'stripe',
'https://js.stripe.com/v3/',
[],
'3.0',
true
);
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
$this->register_upe_payment_method_script_handles();
} else {
$this->register_legacy_payment_method_script_handles();
}
return [ 'wc-stripe-blocks-integration' ];
}
/**
* Registers the UPE JS scripts.
*/
private function register_upe_payment_method_script_handles() {
$asset_path = WC_STRIPE_PLUGIN_PATH . '/build/upe_blocks.asset.php';
$version = WC_STRIPE_VERSION;
$dependencies = [];
if ( file_exists( $asset_path ) ) {
$asset = require $asset_path;
$version = is_array( $asset ) && isset( $asset['version'] )
? $asset['version']
: $version;
$dependencies = is_array( $asset ) && isset( $asset['dependencies'] )
? $asset['dependencies']
: $dependencies;
}
wp_enqueue_style(
'wc-stripe-blocks-checkout-style',
WC_STRIPE_PLUGIN_URL . '/build/upe_blocks.css',
[],
$version
);
wp_register_script(
'wc-stripe-blocks-integration',
WC_STRIPE_PLUGIN_URL . '/build/upe_blocks.js',
array_merge( [ 'stripe' ], $dependencies ),
$version,
true
);
wp_set_script_translations(
'wc-stripe-blocks-integration',
'woocommerce-gateway-stripe'
);
}
/**
* Registers the classic JS scripts.
*/
private function register_legacy_payment_method_script_handles() {
$asset_path = WC_STRIPE_PLUGIN_PATH . '/build/index.asset.php';
$version = WC_STRIPE_VERSION;
$dependencies = [];
if ( file_exists( $asset_path ) ) {
$asset = require $asset_path;
$version = is_array( $asset ) && isset( $asset['version'] )
? $asset['version']
: $version;
$dependencies = is_array( $asset ) && isset( $asset['dependencies'] )
? $asset['dependencies']
: $dependencies;
}
wp_register_script(
'wc-stripe-blocks-integration',
WC_STRIPE_PLUGIN_URL . '/build/index.js',
array_merge( [ 'stripe' ], $dependencies ),
$version,
true
);
wp_set_script_translations(
'wc-stripe-blocks-integration',
'woocommerce-gateway-stripe'
);
}
/**
* Returns an array of key=>value pairs of data made available to the payment methods script.
*
* @return array
*/
public function get_payment_method_data() {
// We need to call array_merge_recursive so the blocks 'button' setting doesn't overwrite
// what's provided from the gateway or payment request configuration.
return array_replace_recursive(
$this->get_gateway_javascript_params(),
$this->get_payment_request_javascript_params(),
// Blocks-specific options
[
'icons' => $this->get_icons(),
'supports' => $this->get_supported_features(),
'showSavedCards' => $this->get_show_saved_cards(),
'showSaveOption' => $this->get_show_save_option(),
'isAdmin' => is_admin(),
'shouldShowPaymentRequestButton' => $this->should_show_payment_request_button(),
'button' => [
'customLabel' => $this->payment_request_configuration->get_button_label(),
],
]
);
}
/**
* Returns true if the PRB should be shown on the current page, false otherwise.
*
* Note: We use `has_block()` in this function, which isn't supported until WP 5.0. However,
* WooCommerce Blocks hasn't supported a WP version lower than 5.0 since 2019. Since this
* function is only called when the WooCommerce Blocks extension is available, it should be
* safe to call `has_block()` here.
* That said, we only run those checks if the `has_block()` function exists, just in case.
*
* @return boolean True if PRBs should be displayed, false otherwise
*/
private function should_show_payment_request_button() {
// TODO: Remove the `function_exists()` check once the minimum WP version has been bumped
// to version 5.0.
if ( function_exists( 'has_block' ) ) {
// Don't show if PRBs are supposed to be hidden on the cart page.
if (
has_block( 'woocommerce/cart' )
&& ! $this->payment_request_configuration->should_show_prb_on_cart_page()
) {
return false;
}
// Don't show if PRBs are supposed to be hidden on the checkout page.
if (
has_block( 'woocommerce/checkout' )
&& ! $this->payment_request_configuration->should_show_prb_on_checkout_page()
) {
return false;
}
// Don't show PRB if there are unsupported products in the cart.
if (
( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) )
&& ! $this->payment_request_configuration->allowed_items_in_cart()
) {
return false;
}
}
return $this->payment_request_configuration->should_show_payment_request_button();
}
/**
* Returns the Stripe Payment Gateway JavaScript configuration object.
*
* @return array the JS configuration from the Stripe Payment Gateway.
*/
private function get_gateway_javascript_params() {
$js_configuration = [];
$gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( isset( $gateways['stripe'] ) ) {
$js_configuration = $gateways['stripe']->javascript_params();
}
return apply_filters(
'wc_stripe_params',
$js_configuration
);
}
/**
* Returns the Stripe Payment Request JavaScript configuration object.
*
* @return array the JS configuration for Stripe Payment Requests.
*/
private function get_payment_request_javascript_params() {
return apply_filters(
'wc_stripe_payment_request_params',
$this->payment_request_configuration->javascript_params()
);
}
/**
* Determine if store allows cards to be saved during checkout.
*
* @return bool True if merchant allows shopper to save card (payment method) during checkout.
*/
private function get_show_saved_cards() {
return isset( $this->settings['saved_cards'] ) ? 'yes' === $this->settings['saved_cards'] : false;
}
/**
* Determine if the checkbox to enable the user to save their payment method should be shown.
*
* @return bool True if the save payment checkbox should be displayed to the user.
*/
private function get_show_save_option() {
$saved_cards = $this->get_show_saved_cards();
// This assumes that Stripe supports `tokenization` - currently this is true, based on
// https://github.com/woocommerce/woocommerce-gateway-stripe/blob/master/includes/class-wc-gateway-stripe.php#L95 .
// See https://github.com/woocommerce/woocommerce-gateway-stripe/blob/ad19168b63df86176cbe35c3e95203a245687640/includes/class-wc-gateway-stripe.php#L271 and
// https://github.com/woocommerce/woocommerce/wiki/Payment-Token-API .
return apply_filters( 'wc_stripe_display_save_payment_method_checkbox', filter_var( $saved_cards, FILTER_VALIDATE_BOOLEAN ) );
}
/**
* Returns the title string to use in the UI (customisable via admin settings screen).
*
* @return string Title / label string
*/
private function get_title() {
return isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'Credit / Debit Card', 'woocommerce-gateway-stripe' );
}
/**
* Return the icons urls.
*
* @return array Arrays of icons metadata.
*/
private function get_icons() {
$icons_src = [
'visa' => [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/visa.svg',
'alt' => __( 'Visa', 'woocommerce-gateway-stripe' ),
],
'amex' => [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/amex.svg',
'alt' => __( 'American Express', 'woocommerce-gateway-stripe' ),
],
'mastercard' => [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/mastercard.svg',
'alt' => __( 'Mastercard', 'woocommerce-gateway-stripe' ),
],
];
if ( 'USD' === get_woocommerce_currency() ) {
$icons_src['discover'] = [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/discover.svg',
'alt' => __( 'Discover', 'woocommerce-gateway-stripe' ),
];
$icons_src['jcb'] = [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/jcb.svg',
'alt' => __( 'JCB', 'woocommerce-gateway-stripe' ),
];
$icons_src['diners'] = [
'src' => WC_STRIPE_PLUGIN_URL . '/assets/images/diners.svg',
'alt' => __( 'Diners', 'woocommerce-gateway-stripe' ),
];
}
return $icons_src;
}
/**
* Add payment request data to the order meta as hooked on the
* woocommerce_rest_checkout_process_payment_with_context action.
*
* @param PaymentContext $context Holds context for the payment.
* @param PaymentResult $result Result object for the payment.
*/
public function add_payment_request_order_meta( PaymentContext $context, PaymentResult &$result ) {
$data = $context->payment_data;
if ( ! empty( $data['payment_request_type'] ) && 'stripe' === $context->payment_method ) {
$this->add_order_meta( $context->order, $data['payment_request_type'] );
}
// hook into stripe error processing so that we can capture the error to
// payment details (which is added to notices and thus not helpful for
// this context).
if ( 'stripe' === $context->payment_method ) {
add_action(
'wc_gateway_stripe_process_payment_error',
function( $error ) use ( &$result ) {
$payment_details = $result->payment_details;
$payment_details['errorMessage'] = wp_strip_all_tags( $error->getLocalizedMessage() );
$result->set_payment_details( $payment_details );
}
);
}
}
/**
* Handles any potential stripe intents on the order that need handled.
*
* This is configured to execute after legacy payment processing has
* happened on the woocommerce_rest_checkout_process_payment_with_context
* action hook.
*
* @param PaymentContext $context Holds context for the payment.
* @param PaymentResult $result Result object for the payment.
*/
public function add_stripe_intents( PaymentContext $context, PaymentResult &$result ) {
if ( 'stripe' === $context->payment_method
&& (
! empty( $result->payment_details['payment_intent_secret'] )
|| ! empty( $result->payment_details['setup_intent_secret'] )
)
) {
$payment_details = $result->payment_details;
$verification_endpoint = add_query_arg(
[
'order' => $context->order->get_id(),
'nonce' => wp_create_nonce( 'wc_stripe_confirm_pi' ),
'redirect_to' => rawurlencode( $result->redirect_url ),
],
home_url() . \WC_Ajax::get_endpoint( 'wc_stripe_verify_intent' )
);
if ( ! empty( $payment_details['save_payment_method'] ) ) {
$verification_endpoint = add_query_arg(
[ 'save_payment_method' => true ],
$verification_endpoint
);
}
$payment_details['verification_endpoint'] = $verification_endpoint;
$result->set_payment_details( $payment_details );
$result->set_status( 'success' );
}
}
/**
* Handles adding information about the payment request type used to the order meta.
*
* @param \WC_Order $order The order being processed.
* @param string $payment_request_type The payment request type used for payment.
*/
private function add_order_meta( \WC_Order $order, $payment_request_type ) {
if ( 'apple_pay' === $payment_request_type ) {
$order->set_payment_method_title( 'Apple Pay (Stripe)' );
$order->save();
} elseif ( 'google_pay' === $payment_request_type ) {
$order->set_payment_method_title( 'Google Pay (Stripe)' );
$order->save();
} elseif ( 'payment_request_api' === $payment_request_type ) {
$order->set_payment_method_title( 'Payment Request (Stripe)' );
$order->save();
}
}
/**
* Returns an array of supported features.
*
* @return string[]
*/
public function get_supported_features() {
$gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( isset( $gateways['stripe'] ) ) {
$gateway = $gateways['stripe'];
return array_filter( $gateway->supports, [ $gateway, 'supports' ] );
}
return [];
}
}