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.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 1.9.17 All 166 releases
woocommerce-pos / includes / Services / Settings / Payment_Gateways_Section.php

Payment_Gateways_Section.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.22, at includes/Services/Settings/Payment_Gateways_Section.php

155 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Payment Gateways Settings Section.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\Services\Settings;
9
10 use WC_Payment_Gateways;
11 use WCPOS\WooCommercePOS\Payments\Gateway_Contract;
12
13 /**
14 * The Payment Gateways Settings Section.
15 *
16 * Read is fully overridden: the stored option is merged over defaults, the
17 * legacy global checkout order_status is applied in memory (never persisted
18 * from a read path), and the view is rebuilt from the currently installed
19 * WooCommerce gateways because gateways can be installed/uninstalled at any
20 * time.
21 */
22 class Payment_Gateways_Section extends Abstract_Section {
23 /**
24 * Section id.
25 */
26 public function id(): string {
27 return 'payment_gateways';
28 }
29
30 /**
31 * Section defaults.
32 */
33 public function defaults(): array {
34 return array(
35 'default_gateway' => 'pos_cash',
36 'gateways' => array(
37 'pos_cash' => array(
38 'order' => 0,
39 'enabled' => true,
40 'order_status' => 'wc-completed',
41 ),
42 'pos_card' => array(
43 'order' => 1,
44 'enabled' => true,
45 'order_status' => 'wc-completed',
46 ),
47 ),
48 );
49 }
50
51 /**
52 * REST endpoint args for payment-gateway updates.
53 */
54 public function endpoint_args(): array {
55 return array(
56 'auto_print_receipt' => array(
57 'validate_callback' => function ( $param, $request, $key ) {
58 return \is_bool( $param );
59 },
60 ),
61 'default_gateway' => array(
62 'validate_callback' => function ( $param, $request, $key ) {
63 return \is_string( $param );
64 },
65 ),
66 'gateways' => array(
67 'validate_callback' => function ( $param, $request, $key ) {
68 return \is_array( $param );
69 },
70 ),
71 );
72 }
73
74 /**
75 * Read the gateway settings view. Pure — no DB writes.
76 */
77 public function read(): array {
78 // Note: I need to re-init the gateways here to pass the tests, but it seems to work fine in the app.
79 WC_Payment_Gateways::instance()->init();
80 $installed_gateways = WC_Payment_Gateways::instance()->payment_gateways();
81 $raw_gw_option = $this->read_raw();
82 $gateways_settings = array_replace_recursive( $this->defaults(), $raw_gw_option );
83
84 // Migrate: if the old global checkout order_status exists, apply it to all
85 // gateways that have no explicit per-gateway status. In memory only —
86 // the legacy key stays in the checkout option until the user saves.
87 $checkout_settings = get_option( self::DB_PREFIX . 'checkout', array() );
88 if ( \is_array( $checkout_settings ) && isset( $checkout_settings['order_status'] ) ) {
89 $global_status = $checkout_settings['order_status'];
90 if ( \is_string( $global_status ) && '' !== $global_status ) {
91 foreach ( $gateways_settings['gateways'] as $gw_id => &$gw_data ) {
92 // Check the raw DB value, not the merged value (which includes defaults).
93 if ( ! isset( $raw_gw_option['gateways'][ $gw_id ]['order_status'] ) ) {
94 $gw_data['order_status'] = $global_status;
95 }
96 }
97 unset( $gw_data );
98 }
99 }
100
101 // NOTE - gateways can be installed and uninstalled, so we need to assume the settings data is stale.
102 $response = array(
103 'default_gateway' => $gateways_settings['default_gateway'],
104 'gateways' => array(),
105 );
106
107 // Gateways that represent deferred/unverified payment default to on-hold.
108 $on_hold_gateways = array( 'bacs', 'cheque' );
109
110 $contract = new Gateway_Contract();
111
112 // loop through installed gateways and merge with saved settings.
113 foreach ( $installed_gateways as $id => $gateway ) {
114 // sanity check for gateway class.
115 if ( ! is_a( $gateway, 'WC_Payment_Gateway' ) || 'pre_install_woocommerce_payments_promotion' === $id ) {
116 continue;
117 }
118
119 $default_status = \in_array( $id, $on_hold_gateways, true ) ? 'wc-on-hold' : 'wc-completed';
120
121 $response['gateways'][ $id ] = array_replace_recursive(
122 array(
123 'id' => $gateway->id,
124 'title' => $contract->get_display_title( $gateway ),
125 'description' => $contract->get_display_description( $gateway ),
126 'enabled' => false,
127 'order' => 999,
128 'order_status' => $default_status,
129 ),
130 $gateways_settings['gateways'][ $id ] ?? array()
131 );
132
133 if ( ! is_string( $response['gateways'][ $id ]['title'] ) || '' === trim( $response['gateways'][ $id ]['title'] ) ) {
134 $response['gateways'][ $id ]['title'] = $contract->get_display_title( $gateway );
135 }
136
137 // A saved empty string is a deliberate blank and is kept.
138 if ( ! is_string( $response['gateways'][ $id ]['description'] ) ) {
139 $response['gateways'][ $id ]['description'] = $contract->get_display_description( $gateway );
140 }
141 }
142
143 /**
144 * Filters the payment gateways settings.
145 *
146 * @since 1.0.0
147 *
148 * @param array $settings The payment gateways settings.
149 *
150 * @hook woocommerce_pos_payment_gateways_settings
151 */
152 return apply_filters( 'woocommerce_pos_payment_gateways_settings', $response );
153 }
154 }
155