Integrations/BankTransfer.php000064400000003300150732352120012277 0ustar00asset_api = $asset_api; } /** * Initializes the payment method type. */ public function initialize() { $this->settings = get_option( 'woocommerce_bacs_settings', [] ); } /** * Returns if this payment method should be active. If false, the scripts will not be enqueued. * * @return boolean */ public function is_active() { return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); } /** * Returns an array of scripts/handles to be registered for this payment method. * * @return array */ public function get_payment_method_script_handles() { $this->asset_api->register_script( 'wc-payment-method-bacs', 'assets/client/blocks/wc-payment-method-bacs.js' ); return [ 'wc-payment-method-bacs' ]; } /** * Returns an array of key=>value pairs of data made available to the payment methods script. * * @return array */ public function get_payment_method_data() { return [ 'title' => $this->get_setting( 'title' ), 'description' => $this->get_setting( 'description' ), 'supports' => $this->get_supported_features(), ]; } } Integrations/AbstractPaymentMethodType.php000064400000005425150732352120015035 0ustar00settings[ $name ] ) ? $this->settings[ $name ] : $default; } /** * Returns the name of the payment method. */ public function get_name() { return $this->name; } /** * Returns if this payment method should be active. If false, the scripts will not be enqueued. * * @return boolean */ public function is_active() { return true; } /** * Returns an array of script handles to enqueue for this payment method in * the frontend context * * @return string[] */ public function get_payment_method_script_handles() { return []; } /** * Returns an array of script handles to enqueue for this payment method in * the admin context * * @return string[] */ public function get_payment_method_script_handles_for_admin() { return $this->get_payment_method_script_handles(); } /** * Returns an array of supported features. * * @return string[] */ public function get_supported_features() { return [ 'products' ]; } /** * An array of key, value pairs of data made available to payment methods * client side. * * @return array */ public function get_payment_method_data() { return []; } /** * Returns an array of script handles to enqueue in the frontend context. * * Alias of get_payment_method_script_handles. Defined by IntegrationInterface. * * @return string[] */ public function get_script_handles() { return $this->get_payment_method_script_handles(); } /** * Returns an array of script handles to enqueue in the admin context. * * Alias of get_payment_method_script_handles_for_admin. Defined by IntegrationInterface. * * @return string[] */ public function get_editor_script_handles() { return $this->get_payment_method_script_handles_for_admin(); } /** * An array of key, value pairs of data made available to the block on the client side. * * Alias of get_payment_method_data. Defined by IntegrationInterface. * * @return array */ public function get_script_data() { return $this->get_payment_method_data(); } } Integrations/CashOnDelivery.php000064400000004775150732352120012617 0ustar00asset_api = $asset_api; } /** * Initializes the payment method type. */ public function initialize() { $this->settings = get_option( 'woocommerce_cod_settings', [] ); } /** * Returns if this payment method should be active. If false, the scripts will not be enqueued. * * @return boolean */ public function is_active() { return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); } /** * Return enable_for_virtual option. * * @return boolean True if store allows COD payment for orders containing only virtual products. */ private function get_enable_for_virtual() { return filter_var( $this->get_setting( 'enable_for_virtual', false ), FILTER_VALIDATE_BOOLEAN ); } /** * Return enable_for_methods option. * * @return array Array of shipping methods (string ids) that allow COD. (If empty, all support COD.) */ private function get_enable_for_methods() { $enable_for_methods = $this->get_setting( 'enable_for_methods', [] ); if ( '' === $enable_for_methods ) { return []; } return $enable_for_methods; } /** * Returns an array of scripts/handles to be registered for this payment method. * * @return array */ public function get_payment_method_script_handles() { $this->asset_api->register_script( 'wc-payment-method-cod', 'assets/client/blocks/wc-payment-method-cod.js' ); return [ 'wc-payment-method-cod' ]; } /** * Returns an array of key=>value pairs of data made available to the payment methods script. * * @return array */ public function get_payment_method_data() { return [ 'title' => $this->get_setting( 'title' ), 'description' => $this->get_setting( 'description' ), 'enableForVirtual' => $this->get_enable_for_virtual(), 'enableForShippingMethods' => $this->get_enable_for_methods(), 'supports' => $this->get_supported_features(), ]; } } Integrations/Cheque.php000064400000003305150732352120011136 0ustar00asset_api = $asset_api; } /** * Initializes the payment method type. */ public function initialize() { $this->settings = get_option( 'woocommerce_cheque_settings', [] ); } /** * Returns if this payment method should be active. If false, the scripts will not be enqueued. * * @return boolean */ public function is_active() { return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); } /** * Returns an array of scripts/handles to be registered for this payment method. * * @return array */ public function get_payment_method_script_handles() { $this->asset_api->register_script( 'wc-payment-method-cheque', 'assets/client/blocks/wc-payment-method-cheque.js' ); return [ 'wc-payment-method-cheque' ]; } /** * Returns an array of key=>value pairs of data made available to the payment methods script. * * @return array */ public function get_payment_method_data() { return [ 'title' => $this->get_setting( 'title' ), 'description' => $this->get_setting( 'description' ), 'supports' => $this->get_supported_features(), ]; } } Integrations/PayPal.php000064400000004633150732352120011117 0ustar00asset_api = $asset_api; } /** * Initializes the payment method type. */ public function initialize() { $this->settings = get_option( 'woocommerce_paypal_settings', [] ); } /** * Returns if this payment method should be active. If false, the scripts will not be enqueued. * * @return boolean */ public function is_active() { return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN ); } /** * Returns an array of scripts/handles to be registered for this payment method. * * @return array */ public function get_payment_method_script_handles() { $this->asset_api->register_script( 'wc-payment-method-paypal', 'assets/client/blocks/wc-payment-method-paypal.js' ); return [ 'wc-payment-method-paypal' ]; } /** * Returns an array of key=>value pairs of data made available to the payment methods script. * * @return array */ public function get_payment_method_data() { return [ 'title' => $this->get_setting( 'title' ), 'description' => $this->get_setting( 'description' ), 'supports' => $this->get_supported_features(), ]; } /** * Returns an array of supported features. * * @return string[] */ public function get_supported_features() { $gateway = new WC_Gateway_Paypal(); $features = array_filter( $gateway->supports, array( $gateway, 'supports' ) ); /** * Filter to control what features are available for each payment gateway. * * @since 4.4.0 * * @example See docs/examples/payment-gateways-features-list.md * * @param array $features List of supported features. * @param string $name Gateway name. * @return array Updated list of supported features. */ return apply_filters( '__experimental_woocommerce_blocks_payment_gateway_features_list', $features, $this->get_name() ); } } PaymentMethodTypeInterface.php000064400000002012150732352120012511 0ustar00payment_method_registry = $payment_method_registry; $this->asset_registry = $asset_registry; } /** * Initialize class features. */ public function init() { add_action( 'init', array( $this->payment_method_registry, 'initialize' ), 5 ); add_filter( 'woocommerce_blocks_register_script_dependencies', array( $this, 'add_payment_method_script_dependencies' ), 10, 2 ); add_action( 'woocommerce_blocks_checkout_enqueue_data', array( $this, 'add_payment_method_script_data' ) ); add_action( 'woocommerce_blocks_cart_enqueue_data', array( $this, 'add_payment_method_script_data' ) ); add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_integrations' ) ); add_action( 'wp_print_scripts', array( $this, 'verify_payment_methods_dependencies' ), 1 ); } /** * Add payment method script handles as script dependencies. * * @param array $dependencies Array of script dependencies. * @param string $handle Script handle. * @return array */ public function add_payment_method_script_dependencies( $dependencies, $handle ) { if ( ! in_array( $handle, [ 'wc-checkout-block', 'wc-checkout-block-frontend', 'wc-cart-block', 'wc-cart-block-frontend' ], true ) ) { return $dependencies; } return array_merge( $dependencies, $this->payment_method_registry->get_all_active_payment_method_script_dependencies() ); } /** * Returns true if the payment gateway is enabled. * * @param object $gateway Payment gateway. * @return boolean */ private function is_payment_gateway_enabled( $gateway ) { return filter_var( $gateway->enabled, FILTER_VALIDATE_BOOLEAN ); } /** * Add payment method data to Asset Registry. */ public function add_payment_method_script_data() { // Enqueue the order of enabled gateways. if ( ! $this->asset_registry->exists( 'paymentMethodSortOrder' ) ) { // We use payment_gateways() here to get the sort order of all enabled gateways. Some may be // programmatically disabled later on, but we still need to know where the enabled ones are in the list. $payment_gateways = WC()->payment_gateways->payment_gateways(); $enabled_gateways = array_filter( $payment_gateways, array( $this, 'is_payment_gateway_enabled' ) ); $this->asset_registry->add( 'paymentMethodSortOrder', array_keys( $enabled_gateways ) ); } // Enqueue all registered gateway data (settings/config etc). $script_data = $this->payment_method_registry->get_all_registered_script_data(); foreach ( $script_data as $asset_data_key => $asset_data_value ) { if ( ! $this->asset_registry->exists( $asset_data_key ) ) { $this->asset_registry->add( $asset_data_key, $asset_data_value ); } } } /** * Register payment method integrations bundled with blocks. * * @param PaymentMethodRegistry $payment_method_registry Payment method registry instance. */ public function register_payment_method_integrations( PaymentMethodRegistry $payment_method_registry ) { $payment_method_registry->register( Package::container()->get( Cheque::class ) ); $payment_method_registry->register( Package::container()->get( PayPal::class ) ); $payment_method_registry->register( Package::container()->get( BankTransfer::class ) ); $payment_method_registry->register( Package::container()->get( CashOnDelivery::class ) ); } /** * Verify all dependencies of registered payment methods have been registered. * If not, remove that payment method script from the list of dependencies * of Cart and Checkout block scripts so it doesn't break the blocks and show * an error in the admin. */ public function verify_payment_methods_dependencies() { // Check that the wc-blocks script is registered before continuing. Some extensions may cause this function to run // before the payment method scripts' dependencies are registered. if ( ! wp_script_is( 'wc-blocks', 'registered' ) ) { return; } $wp_scripts = wp_scripts(); $payment_method_scripts = $this->payment_method_registry->get_all_active_payment_method_script_dependencies(); foreach ( $payment_method_scripts as $payment_method_script ) { if ( ! array_key_exists( $payment_method_script, $wp_scripts->registered ) || ! property_exists( $wp_scripts->registered[ $payment_method_script ], 'deps' ) ) { continue; } $deps = $wp_scripts->registered[ $payment_method_script ]->deps; foreach ( $deps as $dep ) { if ( ! wp_script_is( $dep, 'registered' ) ) { $error_handle = $dep . '-dependency-error'; $error_message = sprintf( 'Payment gateway with handle \'%1$s\' has been deactivated in Cart and Checkout blocks because its dependency \'%2$s\' is not registered. Read the docs about registering assets for payment methods: https://github.com/woocommerce/woocommerce-blocks/blob/060f63c04f0f34f645200b5d4da9212125c49177/docs/third-party-developers/extensibility/checkout-payment-methods/payment-method-integration.md#registering-assets', esc_html( $payment_method_script ), esc_html( $dep ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( $error_message ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_register_script( $error_handle, '' ); wp_enqueue_script( $error_handle ); wp_add_inline_script( $error_handle, sprintf( 'console.error( "%s" );', $error_message ) ); $cart_checkout_scripts = [ 'wc-cart-block', 'wc-cart-block-frontend', 'wc-checkout-block', 'wc-checkout-block-frontend' ]; foreach ( $cart_checkout_scripts as $script_handle ) { if ( ! array_key_exists( $script_handle, $wp_scripts->registered ) || ! property_exists( $wp_scripts->registered[ $script_handle ], 'deps' ) ) { continue; } // Remove payment method script from dependencies. $wp_scripts->registered[ $script_handle ]->deps = array_diff( $wp_scripts->registered[ $script_handle ]->deps, [ $payment_method_script ] ); } } } } } } PaymentMethodRegistry.php000064400000003546150732352120011574 0ustar00get_all_registered(), function( $payment_method ) { return $payment_method->is_active(); } ); } /** * Gets an array of all registered payment method script handles, but only for active payment methods. * * @return string[] */ public function get_all_active_payment_method_script_dependencies() { $script_handles = []; $payment_methods = $this->get_all_active_registered(); foreach ( $payment_methods as $payment_method ) { $script_handles = array_merge( $script_handles, is_admin() ? $payment_method->get_payment_method_script_handles_for_admin() : $payment_method->get_payment_method_script_handles() ); } return array_unique( array_filter( $script_handles ) ); } /** * Gets an array of all registered payment method script data, but only for active payment methods. * * @return array */ public function get_all_registered_script_data() { $script_data = []; $payment_methods = $this->get_all_active_registered(); foreach ( $payment_methods as $payment_method ) { $script_data[ $payment_method->get_name() ] = $payment_method->get_payment_method_data(); } return array( 'paymentMethodData' => array_filter( $script_data ) ); } }