| 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\WCGatewayMoneiMultibanco; |
| 8 |
|
| 9 |
final class MoneiMultibancoBlocksSupport extends AbstractPaymentMethodType { |
| 10 |
|
| 11 |
private $gateway; |
| 12 |
protected $name = 'monei_multibanco'; |
| 13 |
|
| 14 |
public function __construct( WCMoneiPaymentGateway $gateway ) { |
| 15 |
$this->gateway = $gateway; |
| 16 |
} |
| 17 |
|
| 18 |
public function initialize() { |
| 19 |
$this->settings = get_option( 'woocommerce_monei_multibanco_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-multibanco-blocks-integration'; |
| 39 |
|
| 40 |
wp_register_script( |
| 41 |
$script_name, |
| 42 |
WC_Monei()->plugin_url() . '/public/js/monei-block-checkout-multibanco.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 |
), |
| 51 |
WC_Monei()->version, |
| 52 |
true |
| 53 |
); |
| 54 |
|
| 55 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 56 |
wp_set_script_translations( $script_name, 'monei', WC_Monei()->plugin_path() . '/languages' ); |
| 57 |
} |
| 58 |
|
| 59 |
return array( $script_name ); |
| 60 |
} |
| 61 |
|
| 62 |
public function is_active() { |
| 63 |
// Order-pay page always uses classic checkout |
| 64 |
if ( is_checkout_pay_page() ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
$id = $this->gateway->getAccountId() ?? false; |
| 69 |
|
| 70 |
$key = $this->gateway->getApiKey() ?? false; |
| 71 |
|
| 72 |
if ( ! $id || ! $key ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return 'yes' === ( $this->get_setting( 'enabled' ) ?? 'no' ); |
| 77 |
} |
| 78 |
|
| 79 |
public function get_payment_method_data() { |
| 80 |
$total = WC()->cart !== null ? WC()->cart->get_total( false ) : 0; |
| 81 |
$data = array( |
| 82 |
'title' => $this->gateway->title, |
| 83 |
'description' => $this->gateway->description, |
| 84 |
'logo' => WC_Monei()->plugin_url() . '/public/images/multibanco-logo.svg', |
| 85 |
'supports' => $this->get_supported_features(), |
| 86 |
'currency' => get_woocommerce_currency(), |
| 87 |
'total' => $total, |
| 88 |
'language' => locale_iso_639_1_code(), |
| 89 |
// yes: test mode. |
| 90 |
// no: live, |
| 91 |
'testMode' => $this->gateway->getTestmode() ?? false, |
| 92 |
'accountId' => $this->gateway->getAccountId() ?? false, |
| 93 |
'sessionId' => WC()->session !== null ? WC()->session->get_customer_id() : '', |
| 94 |
); |
| 95 |
|
| 96 |
$hide_logo = $this->get_setting( 'hide_logo' ) ?? 'no'; |
| 97 |
if ( 'yes' === $hide_logo ) { |
| 98 |
unset( $data['logo'] ); |
| 99 |
} |
| 100 |
|
| 101 |
return $data; |
| 102 |
} |
| 103 |
} |
| 104 |
|