PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.26
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.26
1.10.27 1.10.26 1.10.25 1.10.24 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 All 171 releases
woocommerce-pos / includes / API / V1 / Payment_Gateways.php

Payment_Gateways.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.26, at includes/API/V1/Payment_Gateways.php

239 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * POS payment gateways controller.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\API\V1;
9
10 \defined( 'ABSPATH' ) || die;
11
12 use WC_Payment_Gateway;
13 use WC_REST_Controller;
14 use WCPOS\WooCommercePOS\Payments\Gateway_Contract;
15 use WP_REST_Request;
16 use WP_REST_Response;
17 use WP_REST_Server;
18
19 /**
20 * POS payment gateways controller.
21 *
22 * Exposes a lean POS-facing catalog of payment gateways. Unlike WooCommerce's
23 * core payment-gateways controller this deliberately does NOT serialize each
24 * gateway's admin settings schema: the POS only needs the capability contract
25 * (id/title/enabled/provider/pos_type/capabilities/provider_data), and calling
26 * WC_Settings_API::get_settings() forces every gateway's init_form_fields() to
27 * run in a non-admin REST context — which fatals on gateways that gate their
28 * settings code behind is_admin() (e.g. ToyyibPay).
29 */
30 class Payment_Gateways extends WC_REST_Controller {
31 /**
32 * REST namespace.
33 *
34 * @var string
35 */
36 protected $namespace = 'wcpos/v1';
37
38 /**
39 * REST base.
40 *
41 * @var string
42 */
43 protected $rest_base = 'payment-gateways';
44
45 /**
46 * Shared gateway contract helper.
47 *
48 * @var Gateway_Contract
49 */
50 private $gateway_contract;
51
52 /**
53 * Constructor.
54 */
55 public function __construct() {
56 $this->gateway_contract = new Gateway_Contract();
57 }
58
59 /**
60 * Register routes.
61 *
62 * Only the collection (catalog) route is exposed. Reading or updating an
63 * individual gateway's settings is intentionally not supported here.
64 */
65 public function register_routes(): void {
66 register_rest_route(
67 $this->namespace,
68 '/' . $this->rest_base,
69 array(
70 array(
71 'methods' => WP_REST_Server::READABLE,
72 'callback' => array( $this, 'get_items' ),
73 'permission_callback' => array( $this, 'get_items_permissions_check' ),
74 'args' => array(
75 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
76 ),
77 ),
78 'schema' => array( $this, 'get_public_item_schema' ),
79 )
80 );
81 }
82
83 /**
84 * Read permissions for payment gateway catalog.
85 *
86 * @param WP_REST_Request $request Request object.
87 *
88 * @return bool|\WP_Error
89 */
90 public function get_items_permissions_check( $request ) {
91 return current_user_can( 'publish_shop_orders' );
92 }
93
94 /**
95 * Return the POS payment gateway catalog.
96 *
97 * @param WP_REST_Request $request Request object.
98 *
99 * @return WP_REST_Response
100 */
101 public function get_items( $request ) {
102 WC()->payment_gateways();
103 $gateways = WC()->payment_gateways->payment_gateways();
104 $data = array();
105
106 foreach ( $gateways as $gateway ) {
107 $response = $this->prepare_item_for_response( $gateway, $request );
108 $data[] = $this->prepare_response_for_collection( $response );
109 }
110
111 return rest_ensure_response( $data );
112 }
113
114 /**
115 * Prepare a gateway for the POS response.
116 *
117 * Builds the payload from the gateway's already-populated public properties
118 * plus the POS contract helper. It never calls get_settings()/init_form_fields().
119 *
120 * @param WC_Payment_Gateway $item Gateway object.
121 * @param WP_REST_Request $request Request object.
122 */
123 public function prepare_item_for_response( $item, $request ): WP_REST_Response {
124 $data = array(
125 'id' => $item->id,
126 'title' => $this->gateway_contract->get_display_title( $item ),
127 'description' => $this->gateway_contract->get_display_description( $item ),
128 'enabled' => $this->gateway_contract->is_pos_enabled( $item ),
129 'provider' => $this->gateway_contract->get_provider( $item, $request ),
130 'pos_type' => $this->gateway_contract->infer_pos_type( $item, $request ),
131 'capabilities' => $this->gateway_contract->get_capabilities( $item, $request ),
132 'provider_data' => $this->gateway_contract->get_provider_data( $item, $request ),
133 );
134
135 // The status the merchant configured this gateway to settle a POS sale to,
136 // so the app can recognise a configured outcome WooCommerce does not call
137 // paid (Pending payment for a "pay by invoice" flow, On hold for a bank
138 // transfer). Null when the merchant has not stored one: the app must not
139 // treat an unpaid status as settled on a guess.
140 $settled_status = $this->gateway_contract->get_settled_order_status( $item->id );
141 $data['settled_order_status'] = '' === $settled_status ? null : $settled_status;
142
143 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
144 $data = $this->add_additional_fields_to_object( $data, $request );
145 $data = $this->filter_response_by_context( $data, $context );
146
147 return rest_ensure_response( $data );
148 }
149
150 /**
151 * Get the POS payment gateway schema.
152 *
153 * @return array
154 */
155 public function get_item_schema(): array {
156 $schema = array(
157 '$schema' => 'http://json-schema.org/draft-04/schema#',
158 'title' => 'pos_payment_gateway',
159 'type' => 'object',
160 'properties' => array(
161 'id' => array(
162 'description' => __( 'Payment gateway ID.', 'woocommerce-pos' ),
163 'type' => 'string',
164 'context' => array( 'view' ),
165 'readonly' => true,
166 ),
167 'title' => array(
168 'description' => __( 'Payment gateway title shown at the POS.', 'woocommerce-pos' ),
169 'type' => 'string',
170 'context' => array( 'view' ),
171 'readonly' => true,
172 ),
173 'description' => array(
174 'description' => __( 'Payment gateway description.', 'woocommerce-pos' ),
175 'type' => 'string',
176 'context' => array( 'view' ),
177 'readonly' => true,
178 ),
179 'enabled' => array(
180 'description' => __( 'Whether the gateway is enabled for the POS.', 'woocommerce-pos' ),
181 'type' => 'boolean',
182 'context' => array( 'view' ),
183 'readonly' => true,
184 ),
185 'provider' => array(
186 'description' => __( 'Provider family identifier.', 'woocommerce-pos' ),
187 'type' => 'string',
188 'context' => array( 'view' ),
189 'readonly' => true,
190 ),
191 'pos_type' => array(
192 'description' => __( 'POS handling type for the gateway.', 'woocommerce-pos' ),
193 'type' => 'string',
194 'context' => array( 'view' ),
195 'readonly' => true,
196 ),
197 'capabilities' => array(
198 'description' => __( 'POS capabilities exposed by the gateway.', 'woocommerce-pos' ),
199 'type' => 'object',
200 'context' => array( 'view' ),
201 'readonly' => true,
202 'properties' => array(
203 'supports_checkout' => array(
204 'type' => 'boolean',
205 'context' => array( 'view' ),
206 ),
207 'supports_automatic_refunds' => array(
208 'type' => 'boolean',
209 'context' => array( 'view' ),
210 ),
211 'supports_provider_refunds' => array(
212 'type' => 'boolean',
213 'context' => array( 'view' ),
214 ),
215 'requires_hardware' => array(
216 'type' => 'boolean',
217 'context' => array( 'view' ),
218 ),
219 ),
220 ),
221 'provider_data' => array(
222 'description' => __( 'Provider-specific public metadata.', 'woocommerce-pos' ),
223 'type' => 'object',
224 'context' => array( 'view' ),
225 'readonly' => true,
226 ),
227 'settled_order_status' => array(
228 'description' => __( 'Order status (without the wc- prefix) the merchant configured this gateway to settle a POS sale to, or null when none is stored.', 'woocommerce-pos' ),
229 'type' => array( 'string', 'null' ),
230 'context' => array( 'view' ),
231 'readonly' => true,
232 ),
233 ),
234 );
235
236 return $this->add_additional_fields_schema( $schema );
237 }
238 }
239