Skip to content

Commit c709262

Browse files
authored
Expose fraud prevention token, send it to blocks checkout (#5104)
1 parent aa77faf commit c709262

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Fix blocks checkout when card testing prevention is active

client/checkout/blocks/generate-payment-method.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ const generatePaymentMethod = async ( api, elements, billingData ) => {
3232
paymentMethod: { id },
3333
} = await request.send();
3434

35+
const fraudPreventionToken = document
36+
.querySelector( '#wcpay-fraud-prevention-token' )
37+
?.getAttribute( 'value' );
38+
3539
return {
3640
type: 'success',
3741
meta: {
3842
paymentMethodData: {
3943
paymentMethod: PAYMENT_METHOD_NAME_CARD,
4044
'wcpay-payment-method': id,
45+
'wcpay-fraud-prevention-token': fraudPreventionToken ?? '',
4146
},
4247
},
4348
};

client/checkout/blocks/upe-fields.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,19 @@ const WCPayUPEFields = ( {
230230
};
231231
}
232232

233+
const fraudPreventionToken = document
234+
.querySelector( '#wcpay-fraud-prevention-token' )
235+
?.getAttribute( 'value' );
236+
233237
return {
234238
type: 'success',
235239
meta: {
236240
paymentMethodData: {
237241
paymentMethod: PAYMENT_METHOD_NAME_CARD,
238242
wc_payment_intent_id: paymentIntentId,
239243
wcpay_selected_upe_payment_type: selectedUPEPaymentType,
244+
'wcpay-fraud-prevention-token':
245+
fraudPreventionToken ?? '',
240246
},
241247
},
242248
};

includes/class-wc-payments-blocks-payment-method.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
9+
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
910
use WCPay\WC_Payments_Checkout;
1011

1112
/**
@@ -33,6 +34,8 @@ public function initialize() {
3334
$this->name = WC_Payment_Gateway_WCPay::GATEWAY_ID;
3435
$this->gateway = WC_Payments::get_gateway();
3536
$this->wc_payments_checkout = WC_Payments::get_wc_payments_checkout();
37+
38+
add_filter( 'the_content', [ $this, 'maybe_add_card_testing_token' ] );
3639
}
3740

3841
/**
@@ -102,4 +105,20 @@ public function get_payment_method_data() {
102105
$this->wc_payments_checkout->get_payment_fields_js_config()
103106
);
104107
}
108+
109+
/**
110+
* Adds the hidden input containing the card testing prevention token to the blocks checkout page.
111+
*
112+
* @param string $content The content that's going to be flushed to the browser.
113+
*
114+
* @return string
115+
*/
116+
public function maybe_add_card_testing_token( $content ) {
117+
$fraud_prevention_service = Fraud_Prevention_Service::get_instance();
118+
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
119+
if ( $fraud_prevention_service->is_enabled() && wp_script_is( 'WCPAY_BLOCKS_CHECKOUT' ) ) {
120+
$content .= '<input type="hidden" name="wcpay-fraud-prevention-token" id="wcpay-fraud-prevention-token" value="' . esc_attr( Fraud_Prevention_Service::get_instance()->get_token() ) . '">';
121+
}
122+
return $content;
123+
}
105124
}

0 commit comments

Comments
 (0)