| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for POS payment gateway adapters. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Payments |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Payments; |
| 9 |
|
| 10 |
\defined( 'ABSPATH' ) || die; |
| 11 |
|
| 12 |
use WC_Order; |
| 13 |
use WC_Payment_Gateway; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
/** |
| 17 |
* WC gateway base that also exposes the POS gateway adapter interface. |
| 18 |
*/ |
| 19 |
abstract class Abstract_POS_Gateway extends WC_Payment_Gateway implements Gateway_Adapter_Interface { |
| 20 |
/** |
| 21 |
* Register the legacy public filter contract for compatibility. |
| 22 |
*/ |
| 23 |
protected function register_pos_gateway_contract_hooks(): void { |
| 24 |
add_filter( 'wcpos_payment_gateway_provider', array( $this, 'wcpos_provider' ), 10, 3 ); |
| 25 |
add_filter( 'wcpos_payment_gateway_pos_type', array( $this, 'wcpos_pos_type' ), 10, 3 ); |
| 26 |
add_filter( 'wcpos_payment_gateway_provider_data', array( $this, 'wcpos_provider_data' ), 10, 3 ); |
| 27 |
add_filter( 'wcpos_payment_gateway_bootstrap', array( $this, 'wcpos_bootstrap' ), 10, 4 ); |
| 28 |
add_filter( 'wcpos_process_checkout_action_' . $this->id, array( $this, 'wcpos_process_checkout_action' ), 10, 5 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Provider family identifier. |
| 33 |
* |
| 34 |
* @param WP_REST_Request|null $request Request object. |
| 35 |
*/ |
| 36 |
public function get_pos_provider( ?WP_REST_Request $request = null ): string { |
| 37 |
return $this->id; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* POS handling type. |
| 42 |
* |
| 43 |
* @param WP_REST_Request|null $request Request object. |
| 44 |
*/ |
| 45 |
public function get_pos_type( ?WP_REST_Request $request = null ): string { |
| 46 |
return 'manual'; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Provider-specific public metadata. |
| 51 |
* |
| 52 |
* @param WP_REST_Request|null $request Request object. |
| 53 |
*/ |
| 54 |
public function get_pos_provider_data( ?WP_REST_Request $request = null ): array { |
| 55 |
return array(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Whether the gateway supports the POS checkout flow. |
| 60 |
* |
| 61 |
* @param WP_REST_Request|null $request Request object. |
| 62 |
*/ |
| 63 |
public function supports_pos_checkout( ?WP_REST_Request $request = null ): bool { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Whether POS may initiate automatic refunds for this gateway. |
| 69 |
* |
| 70 |
* @param WP_REST_Request|null $request Request object. |
| 71 |
*/ |
| 72 |
public function supports_pos_automatic_refunds( ?WP_REST_Request $request = null ): bool { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Whether POS may initiate provider refunds for this gateway. |
| 78 |
* |
| 79 |
* @param WP_REST_Request|null $request Request object. |
| 80 |
*/ |
| 81 |
public function supports_pos_provider_refunds( ?WP_REST_Request $request = null ): bool { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Bootstrap response for the POS app. |
| 87 |
* |
| 88 |
* @param array $context Bootstrap context. |
| 89 |
* @param WP_REST_Request|null $request Request object. |
| 90 |
*/ |
| 91 |
public function get_pos_bootstrap_response( array $context, ?WP_REST_Request $request = null ): array { |
| 92 |
return array( |
| 93 |
'gateway_id' => $this->id, |
| 94 |
'status' => 'ready', |
| 95 |
'expires_at' => null, |
| 96 |
'provider_data' => $this->get_pos_provider_data( $request ), |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Legacy provider filter callback. |
| 102 |
* |
| 103 |
* @param string $provider Provider value. |
| 104 |
* @param mixed $gateway Gateway object. |
| 105 |
* @param mixed $request REST request. |
| 106 |
*/ |
| 107 |
public function wcpos_provider( $provider, $gateway, $request = null ) { |
| 108 |
if ( $this->matches_gateway( $gateway ) ) { |
| 109 |
return $this->get_pos_provider( $request instanceof WP_REST_Request ? $request : null ); |
| 110 |
} |
| 111 |
|
| 112 |
return $provider; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Legacy POS type filter callback. |
| 117 |
* |
| 118 |
* @param string $pos_type POS type. |
| 119 |
* @param mixed $gateway Gateway object. |
| 120 |
* @param mixed $request REST request. |
| 121 |
*/ |
| 122 |
public function wcpos_pos_type( $pos_type, $gateway, $request = null ) { |
| 123 |
if ( $this->matches_gateway( $gateway ) ) { |
| 124 |
return $this->get_pos_type( $request instanceof WP_REST_Request ? $request : null ); |
| 125 |
} |
| 126 |
|
| 127 |
return $pos_type; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Legacy provider-data filter callback. |
| 132 |
* |
| 133 |
* @param array $provider_data Provider data. |
| 134 |
* @param mixed $gateway Gateway object. |
| 135 |
* @param mixed $request REST request. |
| 136 |
*/ |
| 137 |
public function wcpos_provider_data( $provider_data, $gateway, $request = null ) { |
| 138 |
if ( $this->matches_gateway( $gateway ) ) { |
| 139 |
return $this->get_pos_provider_data( $request instanceof WP_REST_Request ? $request : null ); |
| 140 |
} |
| 141 |
|
| 142 |
return $provider_data; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Legacy bootstrap filter callback. |
| 147 |
* |
| 148 |
* @param array $response Bootstrap response. |
| 149 |
* @param string $gateway_id Gateway ID. |
| 150 |
* @param array $context Bootstrap context. |
| 151 |
* @param mixed $request REST request. |
| 152 |
*/ |
| 153 |
public function wcpos_bootstrap( $response, $gateway_id, $context = array(), $request = null ) { |
| 154 |
if ( $this->id === $gateway_id ) { |
| 155 |
return $this->get_pos_bootstrap_response( $context, $request instanceof WP_REST_Request ? $request : null ); |
| 156 |
} |
| 157 |
|
| 158 |
return $response; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Legacy checkout-action filter callback. |
| 163 |
* |
| 164 |
* @param array|\WP_Error $state Checkout state. |
| 165 |
* @param string $action Checkout action. |
| 166 |
* @param array $payment_data Payment data. |
| 167 |
* @param WC_Order $order Order object. |
| 168 |
* @param mixed $request REST request. |
| 169 |
* |
| 170 |
* @return array|\WP_Error |
| 171 |
*/ |
| 172 |
public function wcpos_process_checkout_action( $state, $action, $payment_data, $order, $request = null ) { |
| 173 |
if ( is_wp_error( $state ) ) { |
| 174 |
return $state; |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! is_array( $state ) || ( $state['gateway_id'] ?? '' ) !== $this->id || ! $order instanceof WC_Order ) { |
| 178 |
return $state; |
| 179 |
} |
| 180 |
|
| 181 |
return $this->process_pos_checkout_action( |
| 182 |
$state, |
| 183 |
(string) $action, |
| 184 |
$payment_data, |
| 185 |
$order, |
| 186 |
$request instanceof WP_REST_Request ? $request : null |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Whether the filtered gateway is this adapter's gateway. |
| 192 |
* |
| 193 |
* @param mixed $gateway Gateway object. |
| 194 |
*/ |
| 195 |
private function matches_gateway( $gateway ): bool { |
| 196 |
return $gateway instanceof WC_Payment_Gateway && $this->id === $gateway->id; |
| 197 |
} |
| 198 |
} |
| 199 |
|