| 1 |
<?php declare( strict_types=1 ); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\PaymentMethods\Base; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 6 |
use MultiSafepay\Api\PaymentMethods\PaymentMethod; |
| 7 |
use MultiSafepay\Exception\InvalidDataInitializationException; |
| 8 |
use MultiSafepay\WooCommerce\Services\PaymentComponentService; |
| 9 |
use MultiSafepay\WooCommerce\Services\PaymentMethodService; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class BasePaymentMethodBlocks |
| 13 |
* |
| 14 |
* @package MultiSafepay\WooCommerce\PaymentMethods\Base |
| 15 |
*/ |
| 16 |
final class BasePaymentMethodBlocks extends AbstractPaymentMethodType { |
| 17 |
|
| 18 |
/** |
| 19 |
* Payment methods. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private $gateways = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Track initialization to avoid duplicate API calls. |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private $initialized = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Payment method name. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $name = 'multisafepay'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Initializes the array of payment methods |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @throws InvalidDataInitializationException |
| 44 |
*/ |
| 45 |
public function initialize(): void { |
| 46 |
if ( $this->initialized ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$payment_method_service = new PaymentMethodService(); |
| 51 |
$multisafepay_payment_methods = $payment_method_service->get_multisafepay_payment_methods_from_api(); |
| 52 |
$gateways = array(); |
| 53 |
foreach ( $multisafepay_payment_methods as $multisafepay_payment_method ) { |
| 54 |
$woocommerce_payment_gateways = array(); |
| 55 |
|
| 56 |
if ( isset( $multisafepay_payment_method['type'] ) && ( 'coupon' === $multisafepay_payment_method['type'] ) ) { |
| 57 |
$woocommerce_payment_gateways[] = new BaseGiftCardPaymentMethod( new PaymentMethod( $multisafepay_payment_method ) ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $multisafepay_payment_method['type'] ) && ( 'payment-method' === $multisafepay_payment_method['type'] ) ) { |
| 61 |
$woocommerce_payment_gateways[] = new BasePaymentMethod( new PaymentMethod( $multisafepay_payment_method ) ); |
| 62 |
foreach ( $multisafepay_payment_method['brands'] as $brand ) { |
| 63 |
if ( ! empty( $brand['allowed_countries'] ) && ! get_option( 'multisafepay_group_credit_cards', false ) ) { |
| 64 |
$brand['id'] .= '_' . $multisafepay_payment_method['id']; |
| 65 |
$brand['name'] .= ' - ' . $multisafepay_payment_method['name']; |
| 66 |
$woocommerce_payment_gateways[] = new BaseBrandedPaymentMethod( new PaymentMethod( $multisafepay_payment_method ), $brand ); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( $woocommerce_payment_gateways as $woocommerce_payment_gateway ) { |
| 72 |
$is_editing_checkout_page = $woocommerce_payment_gateway->admin_editing_checkout_page(); |
| 73 |
|
| 74 |
// Blocks support both redirect and direct flows. |
| 75 |
// In the frontend use runtime availability; in the checkout editor rely on settings. |
| 76 |
$can_be_displayed = $is_editing_checkout_page |
| 77 |
? $this->is_gateway_enabled_in_settings( $woocommerce_payment_gateway ) |
| 78 |
: $woocommerce_payment_gateway->is_available(); |
| 79 |
|
| 80 |
if ( $can_be_displayed ) { |
| 81 |
$gateways[] = $woocommerce_payment_gateway; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$this->gateways = $gateways; |
| 87 |
$this->initialized = true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Checks whether a gateway is enabled in WooCommerce settings. |
| 92 |
* |
| 93 |
* In the Checkout block editor we want to list only gateways that are actively enabled, |
| 94 |
* but without relying on runtime availability checks that depend on cart/customer context. |
| 95 |
* |
| 96 |
* @param BasePaymentMethod $gateway |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
private function is_gateway_enabled_in_settings( BasePaymentMethod $gateway ): bool { |
| 101 |
$settings = get_option( 'woocommerce_' . $gateway->id . '_settings', array() ); |
| 102 |
if ( ! is_array( $settings ) || ! isset( $settings['enabled'] ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return 'yes' === $settings['enabled']; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns an array of script handles to enqueue for |
| 111 |
* this payment method in the frontend context |
| 112 |
* |
| 113 |
* @return string[] |
| 114 |
* @throws InvalidDataInitializationException |
| 115 |
*/ |
| 116 |
public function get_payment_method_script_handles(): array { |
| 117 |
static $was_printed = false; |
| 118 |
|
| 119 |
if ( ! $was_printed ) { |
| 120 |
$asset_path = MULTISAFEPAY_PLUGIN_DIR_PATH . '/assets/public/js/multisafepay-blocks/build/index.asset.php'; |
| 121 |
$dependencies = array(); |
| 122 |
|
| 123 |
if ( is_file( $asset_path ) ) { |
| 124 |
$asset = require $asset_path; |
| 125 |
$dependencies = is_array( $asset ) && isset( $asset['dependencies'] ) ? $asset['dependencies'] : $dependencies; |
| 126 |
} |
| 127 |
|
| 128 |
wp_register_script( |
| 129 |
'multisafepay-payment-component-script', |
| 130 |
BasePaymentMethod::MULTISAFEPAY_COMPONENT_JS_URL, |
| 131 |
array(), |
| 132 |
MULTISAFEPAY_PLUGIN_VERSION, |
| 133 |
true |
| 134 |
); |
| 135 |
|
| 136 |
wp_enqueue_style( |
| 137 |
'multisafepay-payment-component-style', |
| 138 |
BasePaymentMethod::MULTISAFEPAY_COMPONENT_CSS_URL, |
| 139 |
array(), |
| 140 |
MULTISAFEPAY_PLUGIN_VERSION, |
| 141 |
'all' |
| 142 |
); |
| 143 |
|
| 144 |
if ( ! in_array( 'multisafepay-payment-component-script', $dependencies, true ) ) { |
| 145 |
$dependencies[] = 'multisafepay-payment-component-script'; |
| 146 |
} |
| 147 |
|
| 148 |
$gateways_data = $this->get_payment_method_data(); |
| 149 |
|
| 150 |
$needs_google_pay_js = false; |
| 151 |
foreach ( $gateways_data as $gateway_data ) { |
| 152 |
if ( ! is_array( $gateway_data ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ( $gateway_data['gateway_code'] ?? '' ) !== 'GOOGLEPAY' ) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! empty( $gateway_data['direct_button_enabled'] ) ) { |
| 161 |
$needs_google_pay_js = true; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ( $needs_google_pay_js ) { |
| 167 |
// Needed for Google Pay Direct button in Checkout Blocks. |
| 168 |
wp_register_script( |
| 169 |
'google-pay-js', |
| 170 |
'https://pay.google.com/gp/p/js/pay.js', |
| 171 |
array(), |
| 172 |
MULTISAFEPAY_PLUGIN_VERSION, |
| 173 |
true |
| 174 |
); |
| 175 |
|
| 176 |
if ( ! in_array( 'google-pay-js', $dependencies, true ) ) { |
| 177 |
$dependencies[] = 'google-pay-js'; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
wp_register_script( |
| 182 |
'multisafepay-payment-methods-blocks', |
| 183 |
MULTISAFEPAY_PLUGIN_URL . '/assets/public/js/multisafepay-blocks/build/index.js', |
| 184 |
$dependencies, |
| 185 |
MULTISAFEPAY_PLUGIN_VERSION, |
| 186 |
true |
| 187 |
); |
| 188 |
|
| 189 |
wp_localize_script( 'multisafepay-payment-methods-blocks', 'multisafepay_gateways', $gateways_data ); |
| 190 |
$was_printed = true; |
| 191 |
} |
| 192 |
|
| 193 |
return array( 'multisafepay-payment-methods-blocks' ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns an array of script handles to enqueue for this payment method in the admin context. |
| 198 |
* |
| 199 |
* WooCommerce Blocks calls a separate method for the editor context. We alia it to |
| 200 |
* get_payment_method_script_handles(), so the script registration and localization |
| 201 |
* also happen in the Checkout page editor. |
| 202 |
* |
| 203 |
* @return string[] |
| 204 |
* @throws InvalidDataInitializationException |
| 205 |
*/ |
| 206 |
public function get_payment_method_script_handles_for_admin(): array { |
| 207 |
return $this->get_payment_method_script_handles(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns an array of key=>value pairs of data |
| 212 |
* made available to the payment methods script. |
| 213 |
* |
| 214 |
* @return array |
| 215 |
* @throws InvalidDataInitializationException |
| 216 |
*/ |
| 217 |
public function get_payment_method_data(): array { |
| 218 |
if ( empty( $this->gateways ) ) { |
| 219 |
$this->initialize(); |
| 220 |
} |
| 221 |
|
| 222 |
$payment_methods_data = array(); |
| 223 |
$show_icons = (bool) get_option( 'multisafepay_checkout_block_payment_icons', false ); |
| 224 |
foreach ( $this->gateways as $gateway ) { |
| 225 |
$has_payment_component = $gateway->is_payment_component_enabled(); |
| 226 |
// In Blocks, avoid blocking page render on remote API calls (api_token, recurring tokens). |
| 227 |
// The frontend will refresh full config via AJAX when needed. |
| 228 |
$payment_component_config = $has_payment_component ? ( new PaymentComponentService() )->get_payment_component_arguments( $gateway, false, false ) : array(); |
| 229 |
|
| 230 |
$gateway_code = $gateway->get_payment_method_gateway_code(); |
| 231 |
|
| 232 |
$direct_button_enabled = false; |
| 233 |
$wallet_config = array(); |
| 234 |
|
| 235 |
if ( 'APPLEPAY' === $gateway_code ) { |
| 236 |
$direct_button_enabled = ( BasePaymentMethod::TRANSACTION_TYPE_DIRECT === $gateway->get_google_apple_pay_use_button( 'applepay' ) ); |
| 237 |
if ( $direct_button_enabled ) { |
| 238 |
$wallet_config = $gateway->get_applepay_wallet_config() ?? array(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if ( 'GOOGLEPAY' === $gateway_code ) { |
| 243 |
$direct_button_enabled = ( BasePaymentMethod::TRANSACTION_TYPE_DIRECT === $gateway->get_google_apple_pay_use_button( 'googlepay' ) ); |
| 244 |
if ( $direct_button_enabled ) { |
| 245 |
$wallet_config = $gateway->get_googlepay_wallet_config() ?? array(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$payment_methods_data[] = array( |
| 250 |
'id' => $gateway->get_payment_method_id(), |
| 251 |
'gateway_code' => $gateway_code, |
| 252 |
'title' => $gateway->get_title(), |
| 253 |
'description' => $gateway->get_description(), |
| 254 |
'icon' => $show_icons ? $gateway->get_payment_method_icon() : '', |
| 255 |
'is_admin' => is_admin(), |
| 256 |
'supports' => $gateway->supports, |
| 257 |
'has_payment_component' => $has_payment_component, |
| 258 |
'payment_component_config' => $payment_component_config, |
| 259 |
'direct_button_enabled' => $direct_button_enabled, |
| 260 |
'wallet_config' => $wallet_config, |
| 261 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 262 |
'nonce' => wp_create_nonce( 'total_price_nonce' ), |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
return $payment_methods_data; |
| 267 |
} |
| 268 |
} |
| 269 |
|