| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway\Blocks; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 6 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 7 |
|
| 8 |
class PayplugGenericBlock extends AbstractPaymentMethodType |
| 9 |
{ |
| 10 |
|
| 11 |
protected $gateway; |
| 12 |
|
| 13 |
protected $allowed_country_codes; |
| 14 |
|
| 15 |
public function initialize() |
| 16 |
{ |
| 17 |
if (class_exists('WC_Blocks_Utils')) { |
| 18 |
if (\WC_Blocks_Utils::has_block_in_page( wc_get_page_id('checkout'), 'woocommerce/checkout' )) { |
| 19 |
$gateways = WC()->payment_gateways->payment_gateways(); |
| 20 |
$this->gateway = $gateways[$this->name]; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
public function is_active() |
| 28 |
{ |
| 29 |
if (class_exists('WC_Blocks_Utils')) { |
| 30 |
if (\WC_Blocks_Utils::has_block_in_page( wc_get_page_id('checkout'), 'woocommerce/checkout' )) { |
| 31 |
|
| 32 |
$active = $this->gateway->is_available(); |
| 33 |
|
| 34 |
if ( method_exists( $this->gateway, "checkGateway" ) ) { |
| 35 |
$active = $this->gateway->checkGateway() && $active; |
| 36 |
} |
| 37 |
|
| 38 |
return $active && $this->gateway->check_gateway( WC()->payment_gateways->payment_gateways() ); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Returns an array of scripts/handles to be registered for this payment method. |
| 48 |
* |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function get_payment_method_script_handles() { |
| 52 |
|
| 53 |
$script_path = '/assets/js/blocks/wc-payplug-'.$this->get_name().'-blocks.js'; |
| 54 |
$script_asset_path = PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/blocks/frontend/wc-payplug-'.$this->get_name().'-blocks.asset.php'; |
| 55 |
$script_asset = file_exists( $script_asset_path ) ? require( $script_asset_path ) : array('dependencies' => array(), 'version' => '1.0.0'); |
| 56 |
$script_url = PAYPLUG_GATEWAY_PLUGIN_URL. $script_path; |
| 57 |
|
| 58 |
wp_register_script('wc-payplug-'.$this->get_name().'-blocks', $script_url, $script_asset[ 'dependencies' ], $script_asset[ 'version' ], true ); |
| 59 |
return [ 'wc-payplug-'.$this->get_name().'-blocks' ]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns an array of supported features. |
| 64 |
* |
| 65 |
* @return string[] |
| 66 |
*/ |
| 67 |
public function get_supported_features() { |
| 68 |
return $this->gateway->supports; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Returns an associative array of data to be exposed for the payment method's client side. |
| 74 |
*/ |
| 75 |
public function get_payment_method_data() { |
| 76 |
$account = PayplugWoocommerceHelper::generic_get_account_data_from_options( $this->name ); |
| 77 |
$this->allowed_country_codes = !empty($account["payment_methods"][ $this->name ]['allowed_countries']) ? $account["payment_methods"][ $this->name ]['allowed_countries'] : null; |
| 78 |
return [ |
| 79 |
'enabled' => $this->is_active(), |
| 80 |
'name' => $this->gateway->id, |
| 81 |
'title' => $this->gateway->title, |
| 82 |
'description' => $this->gateway->description, |
| 83 |
'allowed_country_codes' => $this->allowed_country_codes |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|