| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway\Blocks; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 6 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 7 |
use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| 8 |
|
| 9 |
class PayplugGenericBlock extends AbstractPaymentMethodType |
| 10 |
{ |
| 11 |
use ServiceGetter; |
| 12 |
|
| 13 |
protected $gateway; |
| 14 |
|
| 15 |
protected $allowed_country_codes; |
| 16 |
|
| 17 |
public function initialize() |
| 18 |
{ |
| 19 |
if (class_exists('WC_Blocks_Utils')) { |
| 20 |
if (\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout') || |
| 21 |
\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('cart'), 'woocommerce/cart') |
| 22 |
) { |
| 23 |
$gateways = WC()->payment_gateways->payment_gateways(); |
| 24 |
$this->gateway = $gateways[$this->name]; |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function is_active() |
| 30 |
{ |
| 31 |
if (class_exists('WC_Blocks_Utils')) { |
| 32 |
if (\WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout')) { |
| 33 |
if (empty($this->gateway)) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
$active = false; |
| 38 |
if (method_exists($this->gateway, 'is_available')) { |
| 39 |
$active = $this->gateway->is_available(); |
| 40 |
} |
| 41 |
|
| 42 |
if (method_exists($this->gateway, 'checkGateway')) { |
| 43 |
$active = $this->gateway->checkGateway() && $active; |
| 44 |
} |
| 45 |
|
| 46 |
return $active && $this->gateway->check_gateway(WC()->payment_gateways->payment_gateways()); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns an array of scripts/handles to be registered for this payment method. |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function get_payment_method_script_handles() |
| 57 |
{ |
| 58 |
$script_path = '/assets/js/blocks/wc-payplug-' . $this->get_name() . '-blocks.js'; |
| 59 |
$script_asset_path = PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/blocks/frontend/wc-payplug-' . $this->get_name() . '-blocks.asset.php'; |
| 60 |
$script_asset = file_exists($script_asset_path) ? require($script_asset_path) : ['dependencies' => [], 'version' => '1.0.0']; |
| 61 |
$script_url = PAYPLUG_GATEWAY_PLUGIN_URL . $script_path; |
| 62 |
|
| 63 |
wp_register_script('wc-payplug-' . $this->get_name() . '-blocks', $script_url, $script_asset['dependencies'], $script_asset['version'], true); |
| 64 |
|
| 65 |
return ['wc-payplug-' . $this->get_name() . '-blocks']; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns an array of supported features. |
| 70 |
* |
| 71 |
* @return string[] |
| 72 |
*/ |
| 73 |
public function get_supported_features() |
| 74 |
{ |
| 75 |
$features = $this->gateway->supports; |
| 76 |
|
| 77 |
// Only include tokenization in supported features when order amount is between min and max allowed values |
| 78 |
$cart_total = WC()->cart ? WC()->cart->get_total('') : 0; |
| 79 |
$cart_total_cents = (int) PayplugWoocommerceHelper::get_payplug_amount($cart_total); |
| 80 |
$min_amount = PayplugWoocommerceHelper::get_minimum_amount(); |
| 81 |
$max_amount = PayplugWoocommerceHelper::get_maximum_amount(); |
| 82 |
|
| 83 |
if (!($cart_total_cents >= $min_amount && $cart_total_cents <= $max_amount)) { |
| 84 |
// Remove tokenization from supported features if outside allowed amount range |
| 85 |
$features = array_diff($features, ['tokenization']); |
| 86 |
} |
| 87 |
|
| 88 |
return $features; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns an associative array of data to be exposed for the payment method's client side. |
| 93 |
*/ |
| 94 |
public function get_payment_method_data() |
| 95 |
{ |
| 96 |
$account = PayplugWoocommerceHelper::generic_get_account_data_from_options($this->name); |
| 97 |
$this->allowed_country_codes = !empty($account['payment_methods'][$this->name]['allowed_countries']) ? $account['payment_methods'][$this->name]['allowed_countries'] : null; |
| 98 |
|
| 99 |
// Only show saved cards when order amount is between min and max allowed values |
| 100 |
$cart_total = WC()->cart ? WC()->cart->get_total('') : 0; |
| 101 |
$cart_total_cents = (int) PayplugWoocommerceHelper::get_payplug_amount($cart_total); |
| 102 |
$min_amount = PayplugWoocommerceHelper::get_minimum_amount(); |
| 103 |
$max_amount = PayplugWoocommerceHelper::get_maximum_amount(); |
| 104 |
$configuration = $this->get_service('configuration'); |
| 105 |
$save_card = $configuration->get_option('payment_methods.configuration.payplug.save_card') |
| 106 |
&& $cart_total_cents >= $min_amount |
| 107 |
&& $cart_total_cents <= $max_amount; |
| 108 |
|
| 109 |
return [ |
| 110 |
'enabled' => $this->is_active(), |
| 111 |
'name' => $this->gateway->id, |
| 112 |
'title' => $this->gateway->title, |
| 113 |
'description' => $this->gateway->description, |
| 114 |
'allowed_country_codes' => $this->allowed_country_codes, |
| 115 |
'save_card' => $save_card, |
| 116 |
]; |
| 117 |
} |
| 118 |
} |
| 119 |
|