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