|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SubscriptionsChangePaymentCompat class file. |
| 4 | + */ |
| 5 | + |
| 6 | +declare( strict_types=1 ); |
| 7 | + |
| 8 | +namespace Automattic\WooCommerce\FraudProtection\Compat; |
| 9 | + |
| 10 | +use Automattic\WooCommerce\FraudProtection\ApiClient; |
| 11 | +use Automattic\WooCommerce\FraudProtection\BlockedSessionNotice; |
| 12 | +use Automattic\WooCommerce\FraudProtection\ClassicFormDataExtractionTrait; |
| 13 | +use Automattic\WooCommerce\FraudProtection\FraudProtectionController; |
| 14 | +use Automattic\WooCommerce\FraudProtection\SessionVerifier; |
| 15 | + |
| 16 | +defined( 'ABSPATH' ) || exit; |
| 17 | + |
| 18 | +/** |
| 19 | + * Integrates Blackbox fraud protection into the WooCommerce Subscriptions |
| 20 | + * "Change Payment Method" flow. |
| 21 | + * |
| 22 | + * WooCommerce Subscriptions hijacks the pay-for-order page with its own |
| 23 | + * request handler, so WC Core's `woocommerce_before_pay_action` never fires |
| 24 | + * and the PayForOrderProtector server-side hook is bypassed. This compat |
| 25 | + * layer hooks the Subscriptions action that fires after nonce verification |
| 26 | + * to verify the session and block on BLOCK decisions. |
| 27 | + * |
| 28 | + * The JS side is already handled: PayForOrderProtector enqueues pay-for-order.js |
| 29 | + * on `is_checkout_pay_page()`, which matches the change-payment page. The script |
| 30 | + * gates the `#order_review` form submission to inject the Blackbox session ID. |
| 31 | + * |
| 32 | + * On BLOCK, the request is stopped entirely via redirect + exit, preventing both |
| 33 | + * `update_payment_method()` and `process_payment()` from running. |
| 34 | + * |
| 35 | + * Fail-open: SessionVerifier handles all internal errors and returns ALLOW. |
| 36 | + * |
| 37 | + * Lives in Compat/ rather than as a top-level Protector because it targets |
| 38 | + * a third-party extension hook and is only relevant when Subscriptions is active. |
| 39 | + * |
| 40 | + * @internal |
| 41 | + */ |
| 42 | +class SubscriptionsChangePaymentCompat { |
| 43 | + |
| 44 | + use ClassicFormDataExtractionTrait; |
| 45 | + |
| 46 | + /** |
| 47 | + * Source identifier for verify requests from this compat layer. |
| 48 | + */ |
| 49 | + private const SOURCE = 'subscriptions_change_payment_method'; |
| 50 | + |
| 51 | + /** |
| 52 | + * Session verifier instance. |
| 53 | + * |
| 54 | + * @var SessionVerifier |
| 55 | + */ |
| 56 | + private SessionVerifier $session_verifier; |
| 57 | + |
| 58 | + /** |
| 59 | + * Blocked session notice instance. |
| 60 | + * |
| 61 | + * @var BlockedSessionNotice |
| 62 | + */ |
| 63 | + private BlockedSessionNotice $blocked_session_notice; |
| 64 | + |
| 65 | + /** |
| 66 | + * Initialize with dependencies. |
| 67 | + * |
| 68 | + * @internal |
| 69 | + * |
| 70 | + * @param SessionVerifier $session_verifier The session verifier instance. |
| 71 | + * @param BlockedSessionNotice $blocked_session_notice The blocked session notice instance. |
| 72 | + */ |
| 73 | + final public function init( |
| 74 | + SessionVerifier $session_verifier, |
| 75 | + BlockedSessionNotice $blocked_session_notice |
| 76 | + ): void { |
| 77 | + $this->session_verifier = $session_verifier; |
| 78 | + $this->blocked_session_notice = $blocked_session_notice; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Register hooks for change-payment-method fraud protection. |
| 83 | + * |
| 84 | + * The hook only fires when WooCommerce Subscriptions is active and |
| 85 | + * a customer submits the change-payment-method form. |
| 86 | + * |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function register(): void { |
| 90 | + if ( ! FraudProtectionController::feature_is_enabled() ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + add_action( 'woocommerce_subscription_change_payment_method_via_pay_shortcode', array( $this, 'verify_and_block' ) ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Verify the session with Blackbox and block if needed. |
| 99 | + * |
| 100 | + * Called during the `woocommerce_subscription_change_payment_method_via_pay_shortcode` |
| 101 | + * action, which fires AFTER nonce verification but BEFORE `update_payment_method()` |
| 102 | + * and `process_payment()`. |
| 103 | + * |
| 104 | + * On BLOCK, stops the request entirely via redirect + exit. This prevents the |
| 105 | + * Subscriptions handler from reaching `update_payment_method()` (which saves to DB |
| 106 | + * and triggers gateway cancellation) and `process_payment()`. |
| 107 | + * |
| 108 | + * Fail-open: SessionVerifier handles all internal errors and returns ALLOW. |
| 109 | + * |
| 110 | + * @internal |
| 111 | + * |
| 112 | + * @param \WC_Order $subscription The subscription being updated (WC_Subscription extends WC_Order). |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function verify_and_block( \WC_Order $subscription ): void { |
| 116 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified by Subscriptions handler. |
| 117 | + $request_data = $this->build_request_data( $_POST ); |
| 118 | + |
| 119 | + $decision = $this->session_verifier->verify_session( |
| 120 | + $this->get_blackbox_session_id(), |
| 121 | + self::SOURCE, |
| 122 | + $subscription->get_id(), |
| 123 | + $request_data |
| 124 | + ); |
| 125 | + |
| 126 | + if ( ApiClient::DECISION_BLOCK === $decision ) { |
| 127 | + $message = $this->blocked_session_notice->get_message_html( 'generic' ); |
| 128 | + if ( ! wc_has_notice( $message, 'error' ) ) { |
| 129 | + wc_add_notice( $message, 'error' ); |
| 130 | + } |
| 131 | + |
| 132 | + // Stop the request entirely — the Subscriptions handler would otherwise |
| 133 | + // proceed to update_payment_method() and process_payment(). |
| 134 | + // Redirect to the view-subscription page rather than back to the |
| 135 | + // change-payment form, which would be unusable in a blocked state. |
| 136 | + wp_safe_redirect( $subscription->get_view_order_url() ); |
| 137 | + exit; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments