PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.22
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.22
1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 All 167 releases
woocommerce-pos / includes / Payments / Gateway_Contract.php

Gateway_Contract.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.22, at includes/Payments/Gateway_Contract.php

206 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Human-readable gateway name for POS and settings display.
23 *
24 * Falls back from the public title to the admin method title to the id,
25 * because some gateways never assign a public title.
26 *
27 * @param WC_Payment_Gateway $gateway Gateway object.
28 */
29 public function get_display_title( WC_Payment_Gateway $gateway ): string {
30 foreach ( array( $gateway->get_title(), $gateway->method_title ) as $title ) {
31 if ( is_string( $title ) && '' !== trim( $title ) ) {
32 return $title;
33 }
34 }
35
36 return (string) $gateway->id;
37 }
38
39 /**
40 * Gateway description for POS and settings display, falling back to the
41 * admin method description.
42 *
43 * @param WC_Payment_Gateway $gateway Gateway object.
44 */
45 public function get_display_description( WC_Payment_Gateway $gateway ): string {
46 foreach ( array( $gateway->get_description(), $gateway->method_description ) as $description ) {
47 if ( is_string( $description ) && '' !== trim( $description ) ) {
48 return $description;
49 }
50 }
51
52 return '';
53 }
54
55 /**
56 * Infer POS type for a gateway.
57 *
58 * @param WC_Payment_Gateway $gateway Gateway object.
59 * @param WP_REST_Request $request Request object.
60 */
61 public function infer_pos_type( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string {
62 return $this->get_adapter( $gateway )->get_pos_type( $request );
63 }
64
65 /**
66 * Provider family identifier.
67 *
68 * @param WC_Payment_Gateway $gateway Gateway object.
69 * @param WP_REST_Request $request Request object.
70 */
71 public function get_provider( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string {
72 return $this->get_adapter( $gateway )->get_pos_provider( $request );
73 }
74
75 /**
76 * Provider-specific public metadata.
77 *
78 * @param WC_Payment_Gateway $gateway Gateway object.
79 * @param WP_REST_Request $request Request object.
80 */
81 public function get_provider_data( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array {
82 return $this->get_adapter( $gateway )->get_pos_provider_data( $request );
83 }
84
85 /**
86 * Whether a gateway is enabled for POS.
87 *
88 * @param WC_Payment_Gateway $gateway Gateway object.
89 */
90 public function is_pos_enabled( WC_Payment_Gateway $gateway ): bool {
91 $settings = woocommerce_pos_get_settings( 'payment_gateways' );
92
93 if ( is_wp_error( $settings ) ) {
94 return wc_string_to_bool( $gateway->enabled );
95 }
96
97 $pos_setting = $settings['gateways'][ $gateway->id ] ?? array();
98
99 return isset( $pos_setting['enabled'] ) ? (bool) $pos_setting['enabled'] : wc_string_to_bool( $gateway->enabled );
100 }
101
102 /**
103 * Whether a gateway supports the POS checkout contract.
104 *
105 * @param WC_Payment_Gateway $gateway Gateway object.
106 * @param WP_REST_Request $request Request object.
107 */
108 public function supports_checkout( WC_Payment_Gateway $gateway, WP_REST_Request $request ): bool {
109 return $this->get_adapter( $gateway )->supports_pos_checkout( $request );
110 }
111
112 /**
113 * Capabilities exposed to the POS app.
114 *
115 * @param WC_Payment_Gateway $gateway Gateway object.
116 * @param WP_REST_Request $request Request object.
117 */
118 public function get_capabilities( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array {
119 $adapter = $this->get_adapter( $gateway );
120 $pos_type = $adapter->get_pos_type( $request );
121
122 return array(
123 'supports_checkout' => $adapter->supports_pos_checkout( $request ),
124 'supports_automatic_refunds' => $adapter->supports_pos_automatic_refunds( $request ),
125 'supports_provider_refunds' => $adapter->supports_pos_provider_refunds( $request ),
126 'requires_hardware' => 'terminal' === $pos_type,
127 );
128 }
129
130 /**
131 * Default bootstrap response.
132 *
133 * The optional gateway parameter allows direct PHP adapters to provide the
134 * response while preserving the existing public method signature for callers
135 * that only have a gateway ID and depend on the legacy filter contract.
136 *
137 * @param string $gateway_id Gateway ID.
138 * @param array $context Bootstrap context.
139 * @param WP_REST_Request $request Request object.
140 * @param WC_Payment_Gateway|null $gateway Gateway object.
141 */
142 public function get_bootstrap_response( string $gateway_id, array $context, WP_REST_Request $request, ?WC_Payment_Gateway $gateway = null ): array {
143 if ( $gateway instanceof WC_Payment_Gateway ) {
144 return $this->get_adapter( $gateway )->get_pos_bootstrap_response( $context, $request );
145 }
146
147 // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter.
148 return (array) apply_filters(
149 'wcpos_payment_gateway_bootstrap',
150 array(
151 'gateway_id' => $gateway_id,
152 'status' => 'ready',
153 'expires_at' => null,
154 'provider_data' => array(),
155 ),
156 $gateway_id,
157 $context,
158 $request
159 );
160 // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
161 }
162
163 /**
164 * Process a POS checkout action through the gateway adapter.
165 *
166 * @param WC_Payment_Gateway $gateway Gateway object.
167 * @param int $order_id Order ID.
168 * @param string $action Checkout action.
169 * @param array $payment_data Payment data.
170 * @param WC_Order $order Order object.
171 * @param WP_REST_Request $request Request object.
172 *
173 * @return array|WP_Error
174 */
175 public function process_checkout_action( WC_Payment_Gateway $gateway, int $order_id, string $action, array $payment_data, WC_Order $order, WP_REST_Request $request ) {
176 $state = array(
177 'checkout_id' => wp_generate_uuid4(),
178 'order_id' => $order_id,
179 'gateway_id' => $gateway->id,
180 'status' => 'processing',
181 'provider_data' => array(),
182 'terminal' => false,
183 );
184
185 return $this->get_adapter( $gateway )->process_pos_checkout_action( $state, $action, $payment_data, $order, $request );
186 }
187
188 /**
189 * Whether a checkout status is terminal.
190 *
191 * @param string $status Checkout status.
192 */
193 public function is_terminal_status( string $status ): bool {
194 return in_array( $status, array( 'completed', 'failed', 'cancelled', 'awaiting_customer' ), true );
195 }
196
197 /**
198 * Wrap a WooCommerce gateway with the POS adapter shim.
199 *
200 * @param WC_Payment_Gateway $gateway Gateway object.
201 */
202 private function get_adapter( WC_Payment_Gateway $gateway ): Gateway_Adapter_Interface {
203 return new Filter_Gateway_Adapter( $gateway );
204 }
205 }
206