> */ public function fields() { return array( array( 'id' => 'rules', 'type' => 'checkout_rules', 'label' => __( 'Rules', 'admin-powerups-for-woocommerce' ), 'description' => __( '“Hide” removes the methods when the conditions match. “Only offer” shows them only when the conditions match. Cart total is the items after discounts, with tax, without shipping.', 'admin-powerups-for-woocommerce' ), 'default' => array(), 'payment' => true, 'targets' => 'gateways', 'targets_label' => __( 'Payment methods', 'admin-powerups-for-woocommerce' ), ), ); } /** * Sanitize a setting. Rules JSON that can't be read keeps the saved rules. * * @since 1.0.0 * @param array $field Field schema. * @param mixed $value Raw value. * @return mixed */ protected function sanitize_field( array $field, $value ) { if ( 'checkout_rules' !== $field['type'] ) { return parent::sanitize_field( $field, $value ); } $current = $this->setting( 'rules' ); return Rules::sanitize( $value, true, 'gateways', is_array( $current ) ? $current : array() ); } /** * Register the rules builder (works while the power-up is off). * * @since 1.0.0 * @param string $tab Tab being shown. * @return void */ public function admin_screen( $tab ) { add_action( 'apwc_render_field_checkout_rules', array( RulesField::class, 'render' ), 10, 3 ); RulesField::enqueue(); } /** * {@inheritDoc} */ public function boot() { add_filter( 'woocommerce_available_payment_gateways', array( $this, 'filter' ), 50 ); // Runs only once the route's own permission check passed (key / owner for the pay route). add_filter( 'rest_dispatch_request', array( $this, 'watch_pay_route' ), 10, 4 ); add_filter( 'rest_request_after_callbacks', array( $this, 'end_pay_route' ), 10, 3 ); } /** * Order being paid through the Store API ("pay for order" in the block checkout) while that * request runs: [ request, order ID ], or null. * * @var array{0: \WP_REST_Request, 1: int}|null */ private $paying = null; /** * Note the order while the Store API pays an existing order (POST /wc/store/v1/checkout/{id}), * so rules check that order, not the (usually empty) cart. `rest_dispatch_request` only fires * after WooCommerce's permission check passed (order owner, or order key + billing email), so a * refused request (e.g. the first request of a batch naming someone else's order) never * changes what the rules read. The route is recognised by the handler WordPress matched * (WooCommerce's CheckoutOrder route), never by parsing the path: REST routes match * case-insensitively, so /wc/store/v1/CHECKOUT/{id} reaches the same handler. * * @since 1.0.0 * @param mixed $result Dispatch result (passed through). * @param mixed $request Request. * @param mixed $route Matched route pattern. * @param mixed $handler Matched handler. * @return mixed */ public function watch_pay_route( $result, $request = null, $route = null, $handler = null ) { if ( null === $result && $request instanceof \WP_REST_Request && self::is_pay_handler( $handler ) ) { $id = absint( $request->get_url_params()['id'] ?? 0 ); if ( $id ) { $this->paying = array( $request, $id ); } } return $result; } /** * Whether a matched REST handler is WooCommerce's Store API "pay for order" route. * * @param mixed $handler Handler. * @return bool */ private static function is_pay_handler( $handler ) { $callback = is_array( $handler ) ? ( $handler['callback'] ?? null ) : null; // instanceof never autoloads: without the Store API nothing matches. return is_array( $callback ) && ( $callback[0] ?? null ) instanceof \Automattic\WooCommerce\StoreApi\Routes\V1\CheckoutOrder; } /** * Forget the paid order when its request ends: later requests (the next request of a batch) * read their own cart again. * * @since 1.0.0 * @param mixed $response Response (passed through). * @param mixed $handler Handler. * @param mixed $request Request. * @return mixed */ public function end_pay_route( $response, $handler = null, $request = null ) { if ( $this->paying && $this->paying[0] === $request ) { $this->paying = null; } return $response; } /** * The order the classic "pay for order" page pays, only on the page itself (not wc-ajax, admin-ajax * or REST, whose cart is what gets bought) and only with that order's key in the URL, as * WooCommerce requires to show and pay it. * * @since 1.0.0 * @return \WC_Order|null */ private function pay_page_order() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only; the key is the secret. if ( wp_doing_ajax() || ! empty( $_GET['wc-ajax'] ) || wp_is_serving_rest_request() ) { return null; } if ( ! function_exists( 'is_checkout_pay_page' ) || ! is_checkout_pay_page() ) { return null; } $key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; // An array becomes ''. // phpcs:enable WordPress.Security.NonceVerification.Recommended $order = '' !== $key ? wc_get_order( absint( get_query_var( 'order-pay' ) ) ) : null; return $order instanceof \WC_Order && hash_equals( (string) $order->get_order_key(), $key ) ? $order : null; } /** * Remove payment methods the rules take away. * * @since 1.0.0 * @param array $gateways Available gateways (id => gateway). * @return array */ public function filter( $gateways ) { $rules = (array) $this->setting( 'rules' ); if ( ! $rules || ! is_array( $gateways ) || ( is_admin() && ! wp_doing_ajax() ) ) { return $gateways; // Staff screens see every method. } $order = $this->paying ? wc_get_order( $this->paying[1] ) : $this->pay_page_order(); // Anything else (checkout, cart, wc-ajax, other Store API routes) is the shopper's cart. $context = $order instanceof \WC_Order ? Context::from_order( $order ) : Context::from_cart(); if ( ! $context ) { return $gateways; } $remove = Rules::removals( $rules, $context, static fn( $id, $target ) => $id === $target, array_map( 'strval', array_keys( $gateways ) ) ); return array_diff_key( $gateways, array_flip( $remove ) ); } }