| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Gateways\Blocks; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 6 |
use Monei\Gateways\Abstracts\WCMoneiPaymentGateway; |
| 7 |
use Monei\Gateways\PaymentMethods\WCGatewayMoneiAppleGoogle; |
| 8 |
use Monei\Services\express\ExpressCheckoutAssets; |
| 9 |
|
| 10 |
final class MoneiAppleGoogleBlocksSupport extends AbstractPaymentMethodType { |
| 11 |
|
| 12 |
protected $name = 'monei_apple_google'; |
| 13 |
/** @var WCGatewayMoneiAppleGoogle */ |
| 14 |
public WCGatewayMoneiAppleGoogle $gateway; |
| 15 |
|
| 16 |
/** |
| 17 |
* @param WCGatewayMoneiAppleGoogle $gateway |
| 18 |
*/ |
| 19 |
public function __construct( WCGatewayMoneiAppleGoogle $gateway ) { |
| 20 |
$this->gateway = $gateway; |
| 21 |
} |
| 22 |
|
| 23 |
public function initialize() { |
| 24 |
$this->settings = get_option( 'woocommerce_monei_apple_google_settings', array() ); |
| 25 |
} |
| 26 |
|
| 27 |
public function is_active() { |
| 28 |
// Order-pay page always uses classic checkout |
| 29 |
if ( is_checkout_pay_page() ) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
$id = $this->gateway->getAccountId() ?? false; |
| 34 |
|
| 35 |
$key = $this->gateway->getApiKey() ?? false; |
| 36 |
|
| 37 |
if ( ! $id || ! $key ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
// Hide when neither Apple nor Google is available |
| 42 |
if ( ! $this->gateway->isAppleAvailable() && ! $this->gateway->isGoogleAvailable() ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
return 'yes' === ( $this->get_setting( 'enabled' ) ?? 'no' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function get_payment_method_script_handles() { |
| 50 |
// Order-pay page uses classic checkout, not blocks |
| 51 |
if ( is_checkout_pay_page() ) { |
| 52 |
return array(); |
| 53 |
} |
| 54 |
|
| 55 |
// Register and enqueue blocks checkout CSS |
| 56 |
wp_register_style( |
| 57 |
'monei-blocks-checkout', |
| 58 |
WC_Monei()->plugin_url() . '/public/css/monei-blocks-checkout.css', |
| 59 |
array(), |
| 60 |
WC_Monei()->version, |
| 61 |
'all' |
| 62 |
); |
| 63 |
wp_enqueue_style( 'monei-blocks-checkout' ); |
| 64 |
|
| 65 |
wp_register_script( 'monei', 'https://js.monei.com/v3/monei.js', '', '3.0', true ); |
| 66 |
wp_enqueue_script( 'monei' ); |
| 67 |
|
| 68 |
$script_name = 'wc-monei-apple-google-blocks-integration'; |
| 69 |
|
| 70 |
wp_register_script( |
| 71 |
$script_name, |
| 72 |
WC_Monei()->plugin_url() . '/public/js/monei-block-checkout-apple-google.min.js', |
| 73 |
array( |
| 74 |
'wc-blocks-checkout', |
| 75 |
'wc-blocks-registry', |
| 76 |
'wc-settings', |
| 77 |
'wp-element', |
| 78 |
'wp-html-entities', |
| 79 |
'wp-i18n', |
| 80 |
'monei', |
| 81 |
), |
| 82 |
WC_Monei()->version, |
| 83 |
true |
| 84 |
); |
| 85 |
|
| 86 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 87 |
wp_set_script_translations( $script_name, 'monei', WC_Monei()->plugin_path() . '/languages' ); |
| 88 |
} |
| 89 |
|
| 90 |
$handles = array( $script_name ); |
| 91 |
|
| 92 |
// One express script serves both wallets and both blocks: WooCommerce merges |
| 93 |
// every active payment method's handles into `wc-cart-block-frontend` and |
| 94 |
// `wc-checkout-block-frontend` alike. |
| 95 |
$express_handle = ExpressCheckoutAssets::register_blocks_script(); |
| 96 |
|
| 97 |
if ( '' !== $express_handle ) { |
| 98 |
$handles[] = $express_handle; |
| 99 |
} |
| 100 |
|
| 101 |
return $handles; |
| 102 |
} |
| 103 |
|
| 104 |
public function get_payment_method_data() { |
| 105 |
$supports = $this->gateway->supports; |
| 106 |
$total = WC()->cart !== null ? WC()->cart->get_total( false ) : 0; |
| 107 |
$isGoogleEnabled = $this->gateway->isGoogleAvailable(); |
| 108 |
$isAppleEnabled = $this->gateway->isAppleAvailable(); |
| 109 |
$logoApple = WC_Monei()->plugin_url() . '/public/images/apple-logo.svg'; |
| 110 |
$logoGoogle = WC_Monei()->plugin_url() . '/public/images/google-logo.svg'; |
| 111 |
$payment_request_style = $this->get_setting( 'payment_request_style' ) ?? '{"height": "42"}'; |
| 112 |
|
| 113 |
// Get separate titles with fallback to defaults |
| 114 |
$apple_pay_title = $this->get_setting( 'apple_pay_title' ); |
| 115 |
$google_pay_title = $this->get_setting( 'google_pay_title' ); |
| 116 |
|
| 117 |
// Use specific titles if set, otherwise fall back to defaults |
| 118 |
$apple_pay_title = ! empty( $apple_pay_title ) ? $apple_pay_title : __( 'Apple Pay', 'monei' ); |
| 119 |
$google_pay_title = ! empty( $google_pay_title ) ? $google_pay_title : __( 'Google Pay', 'monei' ); |
| 120 |
|
| 121 |
// Add test mode suffix if enabled |
| 122 |
if ( $this->gateway->getTestmode() ) { |
| 123 |
$test_suffix = ' (' . __( 'Test Mode', 'monei' ) . ')'; |
| 124 |
$apple_pay_title .= $test_suffix; |
| 125 |
$google_pay_title .= $test_suffix; |
| 126 |
} |
| 127 |
|
| 128 |
$data = array( |
| 129 |
'applePayTitle' => $apple_pay_title, |
| 130 |
'googlePayTitle' => $google_pay_title, |
| 131 |
'description' => $this->gateway->description === ' ' ? '' : $this->gateway->description, |
| 132 |
'logoGoogle' => $isGoogleEnabled ? $logoGoogle : false, |
| 133 |
'logoApple' => $isAppleEnabled ? $logoApple : false, |
| 134 |
'supports' => $supports, |
| 135 |
// yes: test mode. |
| 136 |
// no: live, |
| 137 |
'testMode' => $this->gateway->getTestmode(), |
| 138 |
'accountId' => $this->gateway->getAccountId() ?? false, |
| 139 |
'sessionId' => WC()->session !== null ? WC()->session->get_customer_id() : '', |
| 140 |
'currency' => get_woocommerce_currency(), |
| 141 |
'total' => $total, |
| 142 |
'language' => locale_iso_639_1_code(), |
| 143 |
'paymentRequestStyle' => json_decode( $payment_request_style ), |
| 144 |
'express' => ExpressCheckoutAssets::get_script_data(), |
| 145 |
); |
| 146 |
|
| 147 |
return $data; |
| 148 |
} |
| 149 |
} |
| 150 |
|