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