| 1 |
<?php |
| 2 |
/** |
| 3 |
* POS gateway contract helper. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 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_Error; |
| 15 |
use WP_REST_Request; |
| 16 |
|
| 17 |
/** |
| 18 |
* Shared helper for the POS payment-gateway contract. |
| 19 |
*/ |
| 20 |
class Gateway_Contract { |
| 21 |
/** |
| 22 |
* Infer POS type for a gateway. |
| 23 |
* |
| 24 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 25 |
* @param WP_REST_Request $request Request object. |
| 26 |
*/ |
| 27 |
public function infer_pos_type( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string { |
| 28 |
return $this->get_adapter( $gateway )->get_pos_type( $request ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Provider family identifier. |
| 33 |
* |
| 34 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 35 |
* @param WP_REST_Request $request Request object. |
| 36 |
*/ |
| 37 |
public function get_provider( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string { |
| 38 |
return $this->get_adapter( $gateway )->get_pos_provider( $request ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Provider-specific public metadata. |
| 43 |
* |
| 44 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 45 |
* @param WP_REST_Request $request Request object. |
| 46 |
*/ |
| 47 |
public function get_provider_data( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array { |
| 48 |
return $this->get_adapter( $gateway )->get_pos_provider_data( $request ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Whether a gateway is enabled for POS. |
| 53 |
* |
| 54 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 55 |
*/ |
| 56 |
public function is_pos_enabled( WC_Payment_Gateway $gateway ): bool { |
| 57 |
$settings = woocommerce_pos_get_settings( 'payment_gateways' ); |
| 58 |
|
| 59 |
if ( is_wp_error( $settings ) ) { |
| 60 |
return wc_string_to_bool( $gateway->enabled ); |
| 61 |
} |
| 62 |
|
| 63 |
$pos_setting = $settings['gateways'][ $gateway->id ] ?? array(); |
| 64 |
|
| 65 |
return isset( $pos_setting['enabled'] ) ? (bool) $pos_setting['enabled'] : wc_string_to_bool( $gateway->enabled ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Whether a gateway supports the POS checkout contract. |
| 70 |
* |
| 71 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 72 |
* @param WP_REST_Request $request Request object. |
| 73 |
*/ |
| 74 |
public function supports_checkout( WC_Payment_Gateway $gateway, WP_REST_Request $request ): bool { |
| 75 |
return $this->get_adapter( $gateway )->supports_pos_checkout( $request ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Capabilities exposed to the POS app. |
| 80 |
* |
| 81 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 82 |
* @param WP_REST_Request $request Request object. |
| 83 |
*/ |
| 84 |
public function get_capabilities( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array { |
| 85 |
$adapter = $this->get_adapter( $gateway ); |
| 86 |
$pos_type = $adapter->get_pos_type( $request ); |
| 87 |
|
| 88 |
return array( |
| 89 |
'supports_checkout' => $adapter->supports_pos_checkout( $request ), |
| 90 |
'supports_automatic_refunds' => $adapter->supports_pos_automatic_refunds( $request ), |
| 91 |
'supports_provider_refunds' => $adapter->supports_pos_provider_refunds( $request ), |
| 92 |
'requires_hardware' => 'terminal' === $pos_type, |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Default bootstrap response. |
| 98 |
* |
| 99 |
* The optional gateway parameter allows direct PHP adapters to provide the |
| 100 |
* response while preserving the existing public method signature for callers |
| 101 |
* that only have a gateway ID and depend on the legacy filter contract. |
| 102 |
* |
| 103 |
* @param string $gateway_id Gateway ID. |
| 104 |
* @param array $context Bootstrap context. |
| 105 |
* @param WP_REST_Request $request Request object. |
| 106 |
* @param WC_Payment_Gateway|null $gateway Gateway object. |
| 107 |
*/ |
| 108 |
public function get_bootstrap_response( string $gateway_id, array $context, WP_REST_Request $request, ?WC_Payment_Gateway $gateway = null ): array { |
| 109 |
if ( $gateway instanceof WC_Payment_Gateway ) { |
| 110 |
return $this->get_adapter( $gateway )->get_pos_bootstrap_response( $context, $request ); |
| 111 |
} |
| 112 |
|
| 113 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 114 |
return (array) apply_filters( |
| 115 |
'wcpos_payment_gateway_bootstrap', |
| 116 |
array( |
| 117 |
'gateway_id' => $gateway_id, |
| 118 |
'status' => 'ready', |
| 119 |
'expires_at' => null, |
| 120 |
'provider_data' => array(), |
| 121 |
), |
| 122 |
$gateway_id, |
| 123 |
$context, |
| 124 |
$request |
| 125 |
); |
| 126 |
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Process a POS checkout action through the gateway adapter. |
| 131 |
* |
| 132 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 133 |
* @param int $order_id Order ID. |
| 134 |
* @param string $action Checkout action. |
| 135 |
* @param array $payment_data Payment data. |
| 136 |
* @param WC_Order $order Order object. |
| 137 |
* @param WP_REST_Request $request Request object. |
| 138 |
* |
| 139 |
* @return array|WP_Error |
| 140 |
*/ |
| 141 |
public function process_checkout_action( WC_Payment_Gateway $gateway, int $order_id, string $action, array $payment_data, WC_Order $order, WP_REST_Request $request ) { |
| 142 |
$state = array( |
| 143 |
'checkout_id' => wp_generate_uuid4(), |
| 144 |
'order_id' => $order_id, |
| 145 |
'gateway_id' => $gateway->id, |
| 146 |
'status' => 'processing', |
| 147 |
'provider_data' => array(), |
| 148 |
'terminal' => false, |
| 149 |
); |
| 150 |
|
| 151 |
return $this->get_adapter( $gateway )->process_pos_checkout_action( $state, $action, $payment_data, $order, $request ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Whether a checkout status is terminal. |
| 156 |
* |
| 157 |
* @param string $status Checkout status. |
| 158 |
*/ |
| 159 |
public function is_terminal_status( string $status ): bool { |
| 160 |
return in_array( $status, array( 'completed', 'failed', 'cancelled', 'awaiting_customer' ), true ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Wrap a WooCommerce gateway with the POS adapter shim. |
| 165 |
* |
| 166 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 167 |
*/ |
| 168 |
private function get_adapter( WC_Payment_Gateway $gateway ): Gateway_Adapter_Interface { |
| 169 |
return new Filter_Gateway_Adapter( $gateway ); |
| 170 |
} |
| 171 |
} |
| 172 |
|