| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\WooCommerce; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; |
| 6 |
|
| 7 |
/** |
| 8 |
* Exit if accessed directly |
| 9 |
*/ |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Blocks checkout support for the Better Payment (Stripe) gateway. |
| 16 |
* |
| 17 |
* The gateway is a redirect method with no fields, so the client side is a |
| 18 |
* hand-authored registration script (assets/js/woocommerce-blocks.js — no |
| 19 |
* build step) that surfaces the title/description supplied here. |
| 20 |
* |
| 21 |
* Instantiated only from Loader::register_blocks_support(), which guards on |
| 22 |
* AbstractPaymentMethodType existing. |
| 23 |
* |
| 24 |
* @since 2.4.0 |
| 25 |
*/ |
| 26 |
class Blocks extends AbstractPaymentMethodType { |
| 27 |
|
| 28 |
/** |
| 29 |
* Payment method name (matches the gateway id). |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $name = Gateway::GATEWAY_ID; |
| 34 |
|
| 35 |
/** |
| 36 |
* The gateway instance, resolved lazily from the registered gateways. |
| 37 |
* |
| 38 |
* @var Gateway|null |
| 39 |
*/ |
| 40 |
private $gateway = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize settings. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function initialize() { |
| 48 |
$this->settings = get_option( 'woocommerce_' . Gateway::GATEWAY_ID . '_settings', array() ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Resolve the registered gateway instance. |
| 53 |
* |
| 54 |
* @return Gateway|null |
| 55 |
*/ |
| 56 |
private function get_gateway() { |
| 57 |
if ( null === $this->gateway && function_exists( 'WC' ) ) { |
| 58 |
// payment_gateways() is a method on the WooCommerce class — |
| 59 |
// property access here silently resolves to null and would |
| 60 |
// report the method inactive on every blocks checkout. |
| 61 |
$gateways = WC()->payment_gateways()->payment_gateways(); |
| 62 |
|
| 63 |
if ( isset( $gateways[ Gateway::GATEWAY_ID ] ) ) { |
| 64 |
$this->gateway = $gateways[ Gateway::GATEWAY_ID ]; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $this->gateway; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Whether the payment method is usable at checkout. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function is_active() { |
| 77 |
$gateway = $this->get_gateway(); |
| 78 |
|
| 79 |
return $gateway ? $gateway->is_available() : false; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Register and return the client registration script. |
| 84 |
* |
| 85 |
* @return string[] |
| 86 |
*/ |
| 87 |
public function get_payment_method_script_handles() { |
| 88 |
wp_register_script( |
| 89 |
'better-payment-wc-blocks', |
| 90 |
BETTER_PAYMENT_ASSETS . '/js/woocommerce-blocks.js', |
| 91 |
array( 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities' ), |
| 92 |
BETTER_PAYMENT_VERSION, |
| 93 |
true |
| 94 |
); |
| 95 |
|
| 96 |
return array( 'better-payment-wc-blocks' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Data exposed to the client via wc.wcSettings. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function get_payment_method_data() { |
| 105 |
return array( |
| 106 |
'title' => $this->get_setting( 'title', __( 'Better Payment', 'better-payment' ) ), |
| 107 |
'description' => $this->get_setting( 'description', __( 'Pay securely using Stripe via Better Payment.', 'better-payment' ) ), |
| 108 |
'supports' => array( 'products' ), |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|