Integrations
4 months ago
Api.php
2 years ago
PaymentMethodRegistry.php
2 years ago
PaymentMethodTypeInterface.php
2 years ago
Api.php
182 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Payments; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry; |
| 5 | use Automattic\WooCommerce\Blocks\Package; |
| 6 | use Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer; |
| 7 | use Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery; |
| 8 | use Automattic\WooCommerce\Blocks\Payments\Integrations\Cheque; |
| 9 | use Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal; |
| 10 | |
| 11 | /** |
| 12 | * The Api class provides an interface to payment method registration. |
| 13 | * |
| 14 | * @since 2.6.0 |
| 15 | */ |
| 16 | class Api { |
| 17 | /** |
| 18 | * Reference to the PaymentMethodRegistry instance. |
| 19 | * |
| 20 | * @var PaymentMethodRegistry |
| 21 | */ |
| 22 | private $payment_method_registry; |
| 23 | |
| 24 | /** |
| 25 | * Reference to the AssetDataRegistry instance. |
| 26 | * |
| 27 | * @var AssetDataRegistry |
| 28 | */ |
| 29 | private $asset_registry; |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * @param PaymentMethodRegistry $payment_method_registry An instance of Payment Method Registry. |
| 35 | * @param AssetDataRegistry $asset_registry Used for registering data to pass along to the request. |
| 36 | */ |
| 37 | public function __construct( PaymentMethodRegistry $payment_method_registry, AssetDataRegistry $asset_registry ) { |
| 38 | $this->payment_method_registry = $payment_method_registry; |
| 39 | $this->asset_registry = $asset_registry; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Initialize class features. |
| 44 | */ |
| 45 | public function init() { |
| 46 | add_action( 'init', array( $this->payment_method_registry, 'initialize' ), 5 ); |
| 47 | add_filter( 'woocommerce_blocks_register_script_dependencies', array( $this, 'add_payment_method_script_dependencies' ), 10, 2 ); |
| 48 | add_action( 'woocommerce_blocks_checkout_enqueue_data', array( $this, 'add_payment_method_script_data' ) ); |
| 49 | add_action( 'woocommerce_blocks_cart_enqueue_data', array( $this, 'add_payment_method_script_data' ) ); |
| 50 | add_action( 'woocommerce_blocks_payment_method_type_registration', array( $this, 'register_payment_method_integrations' ) ); |
| 51 | add_action( 'wp_print_scripts', array( $this, 'verify_payment_methods_dependencies' ), 1 ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add payment method script handles as script dependencies. |
| 56 | * |
| 57 | * @param array $dependencies Array of script dependencies. |
| 58 | * @param string $handle Script handle. |
| 59 | * @return array |
| 60 | */ |
| 61 | public function add_payment_method_script_dependencies( $dependencies, $handle ) { |
| 62 | if ( ! in_array( $handle, [ 'wc-checkout-block', 'wc-checkout-block-frontend', 'wc-cart-block', 'wc-cart-block-frontend' ], true ) ) { |
| 63 | return $dependencies; |
| 64 | } |
| 65 | return array_merge( $dependencies, $this->payment_method_registry->get_all_active_payment_method_script_dependencies() ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns true if the payment gateway is enabled. |
| 70 | * |
| 71 | * @param object $gateway Payment gateway. |
| 72 | * @return boolean |
| 73 | */ |
| 74 | private function is_payment_gateway_enabled( $gateway ) { |
| 75 | return filter_var( $gateway->enabled, FILTER_VALIDATE_BOOLEAN ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Add payment method data to Asset Registry. |
| 80 | */ |
| 81 | public function add_payment_method_script_data() { |
| 82 | // Enqueue the order of enabled gateways. |
| 83 | if ( ! $this->asset_registry->exists( 'paymentMethodSortOrder' ) ) { |
| 84 | // We use payment_gateways() here to get the sort order of all enabled gateways. Some may be |
| 85 | // programmatically disabled later on, but we still need to know where the enabled ones are in the list. |
| 86 | $payment_gateways = WC()->payment_gateways->payment_gateways(); |
| 87 | $enabled_gateways = array_filter( $payment_gateways, array( $this, 'is_payment_gateway_enabled' ) ); |
| 88 | $this->asset_registry->add( 'paymentMethodSortOrder', array_keys( $enabled_gateways ) ); |
| 89 | } |
| 90 | |
| 91 | // Enqueue all registered gateway data (settings/config etc). |
| 92 | $script_data = $this->payment_method_registry->get_all_registered_script_data(); |
| 93 | foreach ( $script_data as $asset_data_key => $asset_data_value ) { |
| 94 | if ( ! $this->asset_registry->exists( $asset_data_key ) ) { |
| 95 | $this->asset_registry->add( $asset_data_key, $asset_data_value ); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Register payment method integrations bundled with blocks. |
| 102 | * |
| 103 | * @param PaymentMethodRegistry $payment_method_registry Payment method registry instance. |
| 104 | */ |
| 105 | public function register_payment_method_integrations( PaymentMethodRegistry $payment_method_registry ) { |
| 106 | $payment_method_registry->register( |
| 107 | Package::container()->get( Cheque::class ) |
| 108 | ); |
| 109 | $payment_method_registry->register( |
| 110 | Package::container()->get( PayPal::class ) |
| 111 | ); |
| 112 | $payment_method_registry->register( |
| 113 | Package::container()->get( BankTransfer::class ) |
| 114 | ); |
| 115 | $payment_method_registry->register( |
| 116 | Package::container()->get( CashOnDelivery::class ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Verify all dependencies of registered payment methods have been registered. |
| 122 | * If not, remove that payment method script from the list of dependencies |
| 123 | * of Cart and Checkout block scripts so it doesn't break the blocks and show |
| 124 | * an error in the admin. |
| 125 | */ |
| 126 | public function verify_payment_methods_dependencies() { |
| 127 | // Check that the wc-blocks script is registered before continuing. Some extensions may cause this function to run |
| 128 | // before the payment method scripts' dependencies are registered. |
| 129 | if ( ! wp_script_is( 'wc-blocks', 'registered' ) ) { |
| 130 | return; |
| 131 | } |
| 132 | $wp_scripts = wp_scripts(); |
| 133 | $payment_method_scripts = $this->payment_method_registry->get_all_active_payment_method_script_dependencies(); |
| 134 | |
| 135 | foreach ( $payment_method_scripts as $payment_method_script ) { |
| 136 | if ( |
| 137 | ! array_key_exists( $payment_method_script, $wp_scripts->registered ) || |
| 138 | ! property_exists( $wp_scripts->registered[ $payment_method_script ], 'deps' ) |
| 139 | ) { |
| 140 | continue; |
| 141 | } |
| 142 | $deps = $wp_scripts->registered[ $payment_method_script ]->deps; |
| 143 | foreach ( $deps as $dep ) { |
| 144 | if ( ! wp_script_is( $dep, 'registered' ) ) { |
| 145 | $error_handle = $dep . '-dependency-error'; |
| 146 | $error_message = sprintf( |
| 147 | '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', |
| 148 | esc_html( $payment_method_script ), |
| 149 | esc_html( $dep ) |
| 150 | ); |
| 151 | |
| 152 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 153 | error_log( $error_message ); |
| 154 | |
| 155 | // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 156 | wp_register_script( $error_handle, '' ); |
| 157 | wp_enqueue_script( $error_handle ); |
| 158 | wp_add_inline_script( |
| 159 | $error_handle, |
| 160 | sprintf( 'console.error( "%s" );', $error_message ) |
| 161 | ); |
| 162 | |
| 163 | $cart_checkout_scripts = [ 'wc-cart-block', 'wc-cart-block-frontend', 'wc-checkout-block', 'wc-checkout-block-frontend' ]; |
| 164 | foreach ( $cart_checkout_scripts as $script_handle ) { |
| 165 | if ( |
| 166 | ! array_key_exists( $script_handle, $wp_scripts->registered ) || |
| 167 | ! property_exists( $wp_scripts->registered[ $script_handle ], 'deps' ) |
| 168 | ) { |
| 169 | continue; |
| 170 | } |
| 171 | // Remove payment method script from dependencies. |
| 172 | $wp_scripts->registered[ $script_handle ]->deps = array_diff( |
| 173 | $wp_scripts->registered[ $script_handle ]->deps, |
| 174 | [ $payment_method_script ] |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 |