PluginProbe
MONEI Payments for WooCommerce / trunk
MONEI Payments for WooCommerce vtrunk
7.3.3 7.3.2 7.3.1 7.3.0 7.2.4 7.2.3 7.2.2 7.2.0 7.2.1 7.1.3 2.1.0 3.0.0 3.1.0 3.1.1 4.0.0 4.1.0 4.1.1 4.2.0 4.2.1 5.0 5.1.0 5.1.1 5.1.2 5.2.2 5.2.3 All 87 releases
monei / src / Gateways / Blocks / MoneiCCBlocksSupport.php

MoneiCCBlocksSupport.php in MONEI Payments for WooCommerce trunk, at src/Gateways/Blocks/MoneiCCBlocksSupport.php

162 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\WCGatewayMoneiCC;
8 use Monei\Helpers\CardBrandHelper;
9
10 final class MoneiCCBlocksSupport extends AbstractPaymentMethodType {
11
12 private $gateway;
13 protected $name = 'monei';
14 private CardBrandHelper $cardBrandHelper;
15
16 public function __construct( WCMoneiPaymentGateway $gateway, CardBrandHelper $cardBrandHelper ) {
17 $this->gateway = $gateway;
18 $this->cardBrandHelper = $cardBrandHelper;
19 }
20
21 public function initialize() {
22 $this->settings = get_option( 'woocommerce_monei_settings', array() );
23 add_filter( 'woocommerce_saved_payment_methods_list', array( $this, 'filter_saved_payment_methods_list' ), 10, 2 );
24 }
25
26 public function is_active() {
27 // Order-pay page always uses classic checkout
28 if ( is_checkout_pay_page() ) {
29 return false;
30 }
31
32 $id = $this->gateway->getAccountId() ?? false;
33 $key = $this->gateway->getApiKey() ?? false;
34
35 if ( ! $id || ! $key ) {
36 return false;
37 }
38 return 'yes' === ( $this->get_setting( 'enabled' ) ?? 'no' );
39 }
40
41 /**
42 * Removes all saved payment methods when the setting to save cards is disabled.
43 *
44 * @param array $paymentMethods List of payment methods passed from wc_get_customer_saved_methods_list().
45 * @param int $customer_id The customer to fetch payment methods for.
46 * @return array Filtered list of customers payment methods.
47 */
48 public function filter_saved_payment_methods_list( $paymentMethods, $customer_id ) {
49 if ( 'no' === $this->get_setting( 'tokenization' ) ) {
50 return array();
51 }
52 return $paymentMethods;
53 }
54
55 public function get_payment_method_script_handles() {
56 // Order-pay page uses classic checkout, not blocks
57 if ( is_checkout_pay_page() ) {
58 return array();
59 }
60
61 // Register and enqueue blocks checkout CSS
62 wp_register_style(
63 'monei-blocks-checkout',
64 WC_Monei()->plugin_url() . '/public/css/monei-blocks-checkout.css',
65 array(),
66 WC_Monei()->version,
67 'all'
68 );
69 wp_enqueue_style( 'monei-blocks-checkout' );
70
71 wp_register_script( 'monei', 'https://js.monei.com/v3/monei.js', '', '3.0', true );
72 wp_enqueue_script( 'monei' );
73
74 $script_name = 'wc-monei-cc-blocks-integration';
75
76 wp_register_script(
77 $script_name,
78 WC_Monei()->plugin_url() . '/public/js/monei-block-checkout-cc.min.js',
79 array(
80 'wc-blocks-checkout',
81 'wc-blocks-registry',
82 'wc-settings',
83 'wp-element',
84 'wp-html-entities',
85 'wp-i18n',
86 'monei',
87 ),
88 WC_Monei()->version,
89 true
90 );
91
92 if ( function_exists( 'wp_set_script_translations' ) ) {
93 wp_set_script_translations( $script_name, 'monei', WC_Monei()->plugin_path() . '/languages' );
94 }
95
96 return array( $script_name );
97 }
98
99 public function get_payment_method_data() {
100 if ( 'no' === $this->get_setting( 'tokenization' ) ) {
101 $supports = $this->gateway->supports;
102 } else {
103 $supports = array(
104 'features' => $this->gateway->supports,
105 'showSavedCards' => true,
106 'showSaveOption' => true,
107 );
108 }
109 $total = WC()->cart !== null ? WC()->cart->get_total( false ) : 0;
110 $card_input_style = $this->get_setting( 'card_input_style' );
111 if ( ! $card_input_style ) {
112 $card_input_style = WCGatewayMoneiCC::DEFAULT_CARD_INPUT_STYLE;
113 }
114
115 $redirect_mode = $this->get_setting( 'mode' ) ?? 'no';
116 $description = '';
117 if ( 'yes' === $redirect_mode && $this->gateway->description !== '&nbsp;' ) {
118 $description = $this->gateway->description;
119 }
120
121 $data = array(
122 'title' => $this->gateway->title,
123 'description' => $description,
124 'logo' => WC_Monei()->plugin_url() . '/public/images/monei-cards.svg',
125 'cardholderName' => esc_attr__( 'Cardholder Name', 'monei' ),
126 'nameErrorString' => esc_html__( 'Please enter a valid name. Special characters are not allowed.', 'monei' ),
127 'cardErrorString' => esc_html__( 'Please check your card details.', 'monei' ),
128 'tokenErrorString' => esc_html__( 'MONEI token could not be generated.', 'monei' ),
129 'redirected' => esc_html__( 'You will be redirected to the payment page', 'monei' ),
130 'supports' => $supports,
131 // yes: test mode.
132 // no: live,
133 'testMode' => $this->gateway->getTestmode(),
134 // yes: redirect the customer to the Hosted Payment Page.
135 // no: credit card input will be rendered directly on the checkout page
136 'redirect' => $redirect_mode,
137 // yes: Can save credit card and use saved cards.
138 // no: Cannot save/use
139 'tokenization' => $this->get_setting( 'tokenization' ) ?? 'no',
140 'accountId' => $this->gateway->getAccountId() ?? false,
141 'sessionId' => WC()->session !== null ? WC()->session->get_customer_id() : '',
142 'currency' => get_woocommerce_currency(),
143 'total' => $total,
144 'language' => locale_iso_639_1_code(),
145 'cardInputStyle' => json_decode( $card_input_style ),
146 'cardFieldLayout' => $this->get_setting( 'card_field_layout', 'split' ),
147 'cardBrands' => $this->cardBrandHelper->getCardBrandsConfig(),
148 );
149
150 if ( 'yes' === $this->get_setting( 'hide_logo' ) ) {
151 unset( $data['logo'] );
152 }
153
154 // Remove logo when card brands are available
155 if ( ! empty( $data['cardBrands'] ) && count( array_filter( array_keys( $data['cardBrands'] ), fn( $key ) => $key !== 'default' ) ) > 0 ) {
156 unset( $data['logo'] );
157 }
158
159 return $data;
160 }
161 }
162