class-wc-payments-blocks-payment-method.php
3.72 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
<?php
/**
* Class WC_Payments_Blocks_Payment_Method
*
* @package WooCommerce\Payments
*/
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
use WCPay\WC_Payments_Checkout;
/**
* The payment method, which allows the gateway to work with WooCommerce Blocks.
*/
class WC_Payments_Blocks_Payment_Method extends AbstractPaymentMethodType {
/**
* The gateway instance.
*
* @var WC_Payment_Gateway_WCPay
*/
private $gateway;
/**
* WC Payments Checkout
*
* @var WC_Payments_Checkout
*/
private $wc_payments_checkout;
/**
* Initializes the class.
*/
public function initialize() {
$this->name = WC_Payment_Gateway_WCPay::GATEWAY_ID;
$this->gateway = WC_Payments::get_gateway();
$this->wc_payments_checkout = WC_Payments::get_wc_payments_checkout();
add_filter( 'the_content', [ $this, 'maybe_add_card_testing_token' ] );
}
/**
* Checks whether the gateway is active.
*
* @return boolean True when active.
*/
public function is_active() {
return $this->gateway->is_available();
}
/**
* Defines all scripts, necessary for the payment method.
*
* @return string[] A list of script handles.
*/
public function get_payment_method_script_handles() {
wp_enqueue_style(
'wc-blocks-checkout-style',
plugins_url( 'dist/upe-blocks-checkout.css', WCPAY_PLUGIN_FILE ),
[],
'1.0'
);
wp_register_script(
'stripe',
'https://js.stripe.com/v3/',
[],
'3.0',
true
);
wp_register_script(
'WCPAY_BLOCKS_CHECKOUT',
plugins_url( 'dist/blocks-checkout.js', WCPAY_PLUGIN_FILE ),
[ 'stripe' ],
'1.0.1',
true
);
wp_set_script_translations( 'WCPAY_BLOCKS_CHECKOUT', 'woocommerce-payments' );
return [ 'WCPAY_BLOCKS_CHECKOUT' ];
}
/**
* Loads the data about the gateway, which will be exposed in JavaScript.
*
* @return array An associative array, containing all necessary values.
*/
public function get_payment_method_data() {
$is_platform_checkout_eligible = WC_Payments_Features::is_platform_checkout_eligible(); // Feature flag.
$is_platform_checkout_enabled = 'yes' === $this->gateway->get_option( 'platform_checkout', 'no' );
$platform_checkout_config = [];
if ( $is_platform_checkout_eligible && $is_platform_checkout_enabled ) {
$platform_checkout_config = [
'platformCheckoutHost' => defined( 'PLATFORM_CHECKOUT_FRONTEND_HOST' ) ? PLATFORM_CHECKOUT_FRONTEND_HOST : 'https://pay.woo.com',
];
}
return array_merge(
[
'title' => $this->gateway->get_option( 'title', '' ),
'description' => $this->gateway->get_option( 'description', '' ),
'is_admin' => is_admin(), // Used to display payment method preview in wp-admin.
],
$platform_checkout_config,
$this->wc_payments_checkout->get_payment_fields_js_config()
);
}
/**
* Adds the hidden input containing the card testing prevention token to the blocks checkout page.
*
* @param string $content The content that's going to be flushed to the browser.
*
* @return string
*/
public function maybe_add_card_testing_token( $content ) {
if ( ! wp_script_is( 'WCPAY_BLOCKS_CHECKOUT' ) || ! WC()->session ) {
return $content;
}
$fraud_prevention_service = Fraud_Prevention_Service::get_instance();
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( $fraud_prevention_service->is_enabled() ) {
$content .= '<input type="hidden" name="wcpay-fraud-prevention-token" id="wcpay-fraud-prevention-token" value="' . esc_attr( Fraud_Prevention_Service::get_instance()->get_token() ) . '">';
}
return $content;
}
}