| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Services; |
| 4 |
|
| 5 |
use MultiSafepay\WooCommerce\PaymentMethods\Base\BasePaymentMethod; |
| 6 |
use MultiSafepay\WooCommerce\Utils\QrCheckoutManager; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class PaymentComponentService |
| 10 |
* |
| 11 |
* @package MultiSafepay\WooCommerce\Services |
| 12 |
*/ |
| 13 |
class PaymentComponentService { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var SdkService |
| 17 |
*/ |
| 18 |
public $sdk_service; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var ApiTokenService |
| 22 |
*/ |
| 23 |
public $api_token_service; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var PaymentMethodService |
| 27 |
*/ |
| 28 |
public $payment_method_service; |
| 29 |
|
| 30 |
/** |
| 31 |
* ApiTokenService constructor. |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
$this->sdk_service = new SdkService(); |
| 35 |
$this->api_token_service = new ApiTokenService(); |
| 36 |
$this->payment_method_service = new PaymentMethodService(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Return the arguments required when the payment component needs to be initialized |
| 41 |
* |
| 42 |
* @param BasePaymentMethod $woocommerce_payment_gateway |
| 43 |
* @param bool $validate_checkout |
| 44 |
* @param bool $include_api_token |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function get_payment_component_arguments( BasePaymentMethod $woocommerce_payment_gateway, bool $validate_checkout = false, bool $include_api_token = true ): array { |
| 48 |
$customer = ( function_exists( 'WC' ) && WC() && WC()->customer ) ? WC()->customer : null; |
| 49 |
$billing_country = $customer ? $customer->get_billing_country() : ''; |
| 50 |
$fallback_country = ( function_exists( 'WC' ) && WC() && WC()->countries ) ? WC()->countries->get_base_country() : ''; |
| 51 |
|
| 52 |
$payment_component_arguments = array( |
| 53 |
'debug' => (bool) get_option( 'multisafepay_debugmode', false ), |
| 54 |
'env' => $this->sdk_service->get_test_mode() ? 'test' : 'live', |
| 55 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 56 |
'nonce' => wp_create_nonce( 'payment_component_arguments_nonce' ), |
| 57 |
'api_token' => $include_api_token ? $this->api_token_service->get_api_token() : '', |
| 58 |
'orderData' => array( |
| 59 |
'currency' => get_woocommerce_currency(), |
| 60 |
'amount' => ( $this->get_total_amount() * 100 ), |
| 61 |
'customer' => array( |
| 62 |
'locale' => strtoupper( substr( ( new CustomerService() )->get_locale(), 0, 2 ) ), |
| 63 |
'country' => ! empty( $billing_country ) ? $billing_country : $fallback_country, |
| 64 |
), |
| 65 |
'payment_options' => $this->build_payment_options(), |
| 66 |
), |
| 67 |
'gateway' => $woocommerce_payment_gateway->get_payment_method_gateway_code(), |
| 68 |
'qr_supported' => $woocommerce_payment_gateway->is_qr_enabled() || $woocommerce_payment_gateway->is_qr_only_enabled(), |
| 69 |
); |
| 70 |
|
| 71 |
// Payment Component Template ID. |
| 72 |
$template_id = get_option( 'multisafepay_payment_component_template_id', false ); |
| 73 |
if ( ! empty( $template_id ) ) { |
| 74 |
$payment_component_arguments['orderData']['payment_options']['template_id'] = $template_id; |
| 75 |
} |
| 76 |
|
| 77 |
// Tokenization and recurring model |
| 78 |
if ( $include_api_token && $woocommerce_payment_gateway->is_tokenization_enabled() && is_user_logged_in() ) { |
| 79 |
$payment_component_arguments['recurring'] = array( |
| 80 |
'model' => 'cardOnFile', |
| 81 |
'tokens' => $this->sdk_service->get_payment_tokens( |
| 82 |
(string) get_current_user_id(), |
| 83 |
sanitize_text_field( $woocommerce_payment_gateway->get_payment_method_gateway_code() ) |
| 84 |
), |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
// Payment Component QR |
| 89 |
if ( $validate_checkout ) { |
| 90 |
$this->add_qr_configuration( $payment_component_arguments, $woocommerce_payment_gateway ); |
| 91 |
} |
| 92 |
|
| 93 |
return $payment_component_arguments; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Build payment options array |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
private function build_payment_options(): array { |
| 102 |
return array( |
| 103 |
'template' => array( |
| 104 |
'settings' => array( |
| 105 |
'embed_mode' => 1, |
| 106 |
), |
| 107 |
'merge' => true, |
| 108 |
), |
| 109 |
'settings' => array( |
| 110 |
'connect' => array( |
| 111 |
'issuers_display_mode' => 'select', |
| 112 |
), |
| 113 |
), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add QR configuration to payment component arguments |
| 119 |
* |
| 120 |
* @param array $payment_component_arguments |
| 121 |
* @param BasePaymentMethod $woocommerce_payment_gateway |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
private function add_qr_configuration( array &$payment_component_arguments, BasePaymentMethod $woocommerce_payment_gateway ): void { |
| 125 |
$qr_checkout_manager = new QrCheckoutManager(); |
| 126 |
if ( ! $qr_checkout_manager->validate_checkout_fields() ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$is_qr_only_enabled = $woocommerce_payment_gateway->is_qr_only_enabled(); |
| 131 |
|
| 132 |
if ( $woocommerce_payment_gateway->is_qr_enabled() || $is_qr_only_enabled ) { |
| 133 |
$qr_config = array( |
| 134 |
'enabled' => 1, |
| 135 |
'size' => 206, |
| 136 |
); |
| 137 |
|
| 138 |
if ( $is_qr_only_enabled ) { |
| 139 |
$qr_config['qr_only'] = 1; |
| 140 |
} |
| 141 |
$this->add_qr_width_to_config( $qr_config, $woocommerce_payment_gateway ); |
| 142 |
$payment_component_arguments['orderData']['payment_options']['settings']['connect']['qr'] = $qr_config; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Add QR width to the configuration if specified |
| 148 |
* |
| 149 |
* @param array $qr_config |
| 150 |
* @param BasePaymentMethod $woocommerce_payment_gateway |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
private function add_qr_width_to_config( array &$qr_config, BasePaymentMethod $woocommerce_payment_gateway ): void { |
| 154 |
$qr_width = $woocommerce_payment_gateway->get_qr_width(); |
| 155 |
if ( ! empty( $qr_width ) && is_numeric( $qr_width ) ) { |
| 156 |
$qr_config['size'] = (int) $qr_width; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Return the arguments required when the payment component needs to be initialized via a WP AJAX request |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function refresh_payment_component_config() { |
| 166 |
$payment_component_arguments_nonce = sanitize_key( $_POST['nonce'] ?? '' ); |
| 167 |
if ( ! wp_verify_nonce( wp_unslash( $payment_component_arguments_nonce ), 'payment_component_arguments_nonce' ) ) { |
| 168 |
wp_send_json( array() ); |
| 169 |
} |
| 170 |
$gateway_id = sanitize_key( $_POST['gateway_id'] ?? '' ); |
| 171 |
$woocommerce_payment_gateway = $this->payment_method_service->get_woocommerce_payment_gateway_by_id( $gateway_id ); |
| 172 |
$validate_checkout_fields = ( $woocommerce_payment_gateway->is_payment_component_enabled() && $woocommerce_payment_gateway->is_qr_enabled() || $woocommerce_payment_gateway->is_qr_only_enabled() ); |
| 173 |
$payment_component_arguments = $this->get_payment_component_arguments( $woocommerce_payment_gateway, $validate_checkout_fields ); |
| 174 |
wp_send_json( $payment_component_arguments ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Return the total amount of the cart or order |
| 179 |
* |
| 180 |
* @return float |
| 181 |
*/ |
| 182 |
private function get_total_amount(): float { |
| 183 |
$total_amount = ( WC()->cart ) ? (float) WC()->cart->get_total( '' ) : 0.0; |
| 184 |
|
| 185 |
if ( is_wc_endpoint_url( 'order-pay' ) ) { |
| 186 |
$order_id = absint( get_query_var( 'order-pay' ) ); |
| 187 |
if ( 0 < $order_id ) { |
| 188 |
$order = wc_get_order( $order_id ); |
| 189 |
if ( $order ) { |
| 190 |
$total_amount = (float) $order->get_total(); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return (float) $total_amount; |
| 196 |
} |
| 197 |
} |
| 198 |
|