| 1 |
<?php |
| 2 |
|
| 3 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 4 |
|
| 5 |
/** |
| 6 |
* Vipps Recurring payment method integration for Gutenberg Blocks |
| 7 |
* |
| 8 |
* @since 2.6.0 |
| 9 |
*/ |
| 10 |
final class WC_Vipps_Recurring_Blocks_Support extends AbstractPaymentMethodType { |
| 11 |
/** |
| 12 |
* Payment method name defined by payment methods extending this class. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $name = 'vipps_recurring'; |
| 17 |
|
| 18 |
private ?WC_Gateway_Vipps_Recurring $gateway = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Initializes the payment method type. |
| 22 |
*/ |
| 23 |
public function initialize() { |
| 24 |
$this->settings = \WC_Vipps_Recurring_Helper::get_settings(); |
| 25 |
} |
| 26 |
|
| 27 |
private function gateway(): ?WC_Gateway_Vipps_Recurring { |
| 28 |
if ( $this->gateway ) { |
| 29 |
return $this->gateway; |
| 30 |
} |
| 31 |
|
| 32 |
$this->gateway = WC_Vipps_Recurring::get_instance()->gateway(); |
| 33 |
|
| 34 |
return $this->gateway; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns if this payment method should be active. If false, the scripts will not be enqueued. |
| 39 |
* |
| 40 |
* @return boolean |
| 41 |
*/ |
| 42 |
public function is_active(): bool { |
| 43 |
return $this->gateway()->is_available(); |
| 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(): array { |
| 52 |
$version = filemtime( WC_VIPPS_RECURRING_PLUGIN_PATH . "/assets/js/vipps-recurring-payment-method-block.js" ); |
| 53 |
$path = WC_VIPPS_RECURRING_PLUGIN_URL . '/assets/js/vipps-recurring-payment-method-block.js'; |
| 54 |
$handle = 'wc-payment-method-vipps_recurring'; |
| 55 |
$dependencies = [ 'wp-hooks', 'wp-i18n' ]; |
| 56 |
|
| 57 |
wp_register_script( $handle, $path, $dependencies, $version, true ); |
| 58 |
|
| 59 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 60 |
wp_set_script_translations( $handle, 'woo-vipps', dirname(WC_VIPPS_MAIN_FILE) . '/languages/' ); |
| 61 |
} |
| 62 |
|
| 63 |
return [ $handle ]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns an array of key=>value pairs of data made available to the payment methods script. |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get_payment_method_data(): array { |
| 72 |
return [ |
| 73 |
'title' => $this->gateway()->title, |
| 74 |
'description' => $this->gateway()->description, |
| 75 |
'logo' => apply_filters( |
| 76 |
'wc_vipps_recurring_checkout_logo_url', |
| 77 |
WC_VIPPS_RECURRING_PLUGIN_URL . '/assets/images/' . $this->gateway()->brand . '-mark.svg', |
| 78 |
$this->gateway()->brand |
| 79 |
), |
| 80 |
'brand' => $this->gateway()->brand, |
| 81 |
'supports' => $this->get_supported_features(), |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns an array of supported features. |
| 87 |
* |
| 88 |
* @return string[] |
| 89 |
*/ |
| 90 |
public function get_supported_features(): array { |
| 91 |
$gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 92 |
if ( isset( $gateways[ $this->name ] ) ) { |
| 93 |
$gateway = $gateways[ $this->name ]; |
| 94 |
|
| 95 |
return array_filter( $gateway->supports, [ $gateway, 'supports' ] ); |
| 96 |
} |
| 97 |
|
| 98 |
return []; |
| 99 |
} |
| 100 |
} |
| 101 |
|