| 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\Services\express\ExpressCheckoutAssets; |
| 8 |
|
| 9 |
final class MoneiPaypalBlocksSupport extends AbstractPaymentMethodType { |
| 10 |
|
| 11 |
private $gateway; |
| 12 |
protected $name = 'monei_paypal'; |
| 13 |
|
| 14 |
public function __construct( WCMoneiPaymentGateway $gateway ) { |
| 15 |
$this->gateway = $gateway; |
| 16 |
} |
| 17 |
|
| 18 |
public function initialize() { |
| 19 |
$this->settings = get_option( 'woocommerce_monei_paypal_settings', array() ); |
| 20 |
} |
| 21 |
|
| 22 |
public function get_payment_method_script_handles() { |
| 23 |
// Order-pay page uses classic checkout, not blocks |
| 24 |
if ( is_checkout_pay_page() ) { |
| 25 |
return array(); |
| 26 |
} |
| 27 |
|
| 28 |
// Register and enqueue blocks checkout CSS |
| 29 |
wp_register_style( |
| 30 |
'monei-blocks-checkout', |
| 31 |
WC_Monei()->plugin_url() . '/public/css/monei-blocks-checkout.css', |
| 32 |
array(), |
| 33 |
WC_Monei()->version, |
| 34 |
'all' |
| 35 |
); |
| 36 |
wp_enqueue_style( 'monei-blocks-checkout' ); |
| 37 |
|
| 38 |
$script_name = 'wc-monei-paypal-blocks-integration'; |
| 39 |
|
| 40 |
wp_register_script( |
| 41 |
$script_name, |
| 42 |
WC_Monei()->plugin_url() . '/public/js/monei-block-checkout-paypal.min.js', |
| 43 |
array( |
| 44 |
'wc-blocks-checkout', |
| 45 |
'wc-blocks-registry', |
| 46 |
'wc-settings', |
| 47 |
'wp-element', |
| 48 |
'wp-html-entities', |
| 49 |
'wp-i18n', |
| 50 |
'monei', |
| 51 |
), |
| 52 |
WC_Monei()->version, |
| 53 |
true |
| 54 |
); |
| 55 |
|
| 56 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 57 |
wp_set_script_translations( $script_name, 'monei', WC_Monei()->plugin_path() . '/languages' ); |
| 58 |
} |
| 59 |
|
| 60 |
$handles = array( $script_name ); |
| 61 |
|
| 62 |
// Registered from here as well as from the Apple/Google method, so express still |
| 63 |
// loads on a store that only turned it on for PayPal. The handle is the same one, |
| 64 |
// and registering it twice is a no-op. |
| 65 |
$express_handle = ExpressCheckoutAssets::register_blocks_script(); |
| 66 |
|
| 67 |
if ( '' !== $express_handle ) { |
| 68 |
$handles[] = $express_handle; |
| 69 |
|
| 70 |
// The express component builds a wallet, which needs the SDK. The regular |
| 71 |
// PayPal block gets it through its own dependency list; this one is loaded |
| 72 |
// even when the merchant only enabled express. |
| 73 |
if ( ! wp_script_is( 'monei', 'registered' ) ) { |
| 74 |
wp_register_script( 'monei', 'https://js.monei.com/v3/monei.js', '', '3.0', true ); |
| 75 |
} |
| 76 |
|
| 77 |
wp_enqueue_script( 'monei' ); |
| 78 |
} |
| 79 |
|
| 80 |
return $handles; |
| 81 |
} |
| 82 |
|
| 83 |
public function is_active() { |
| 84 |
// Order-pay page always uses classic checkout |
| 85 |
if ( is_checkout_pay_page() ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
$id = $this->gateway->getAccountId() ?? false; |
| 90 |
|
| 91 |
$key = $this->gateway->getApiKey() ?? false; |
| 92 |
|
| 93 |
if ( ! $id || ! $key ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return 'yes' === ( $this->get_setting( 'enabled' ) ?? 'no' ); |
| 98 |
} |
| 99 |
|
| 100 |
public function get_payment_method_data() { |
| 101 |
$total = WC()->cart !== null ? WC()->cart->get_total( false ) : 0; |
| 102 |
$paypal_style = $this->get_setting( 'paypal_style' ); |
| 103 |
$paypal_mode = $this->get_setting( 'mode' ); |
| 104 |
$redirect_flow = ( ! empty( $paypal_mode ) && 'yes' === $paypal_mode ); |
| 105 |
|
| 106 |
if ( ! $paypal_style ) { |
| 107 |
$paypal_style = '{}'; |
| 108 |
} |
| 109 |
$data = array( |
| 110 |
'title' => $this->gateway->title, |
| 111 |
'logo' => WC_Monei()->plugin_url() . '/public/images/paypal-logo.svg', |
| 112 |
'supports' => $this->get_supported_features(), |
| 113 |
'currency' => get_woocommerce_currency(), |
| 114 |
'total' => $total, |
| 115 |
'language' => locale_iso_639_1_code(), |
| 116 |
// yes: test mode. |
| 117 |
// no: live, |
| 118 |
'testMode' => $this->gateway->getTestmode() ?? false, |
| 119 |
'accountId' => $this->gateway->getAccountId() ?? false, |
| 120 |
'sessionId' => WC()->session !== null ? WC()->session->get_customer_id() : '', |
| 121 |
'paypalStyle' => json_decode( $paypal_style ), |
| 122 |
'redirectFlow' => $redirect_flow, |
| 123 |
'description' => $this->get_setting( 'description' ), |
| 124 |
'express' => ExpressCheckoutAssets::get_script_data(), |
| 125 |
); |
| 126 |
|
| 127 |
$hide_logo = $this->get_setting( 'hide_logo' ); |
| 128 |
if ( 'yes' === $hide_logo ) { |
| 129 |
unset( $data['logo'] ); |
| 130 |
} |
| 131 |
|
| 132 |
return $data; |
| 133 |
} |
| 134 |
} |
| 135 |
|