| 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_Payment_Gateway; |
| 13 |
use WP_REST_Request; |
| 14 |
|
| 15 |
/** |
| 16 |
* Shared helper for the POS payment-gateway contract. |
| 17 |
*/ |
| 18 |
class Gateway_Contract { |
| 19 |
/** |
| 20 |
* Built-in gateways with manual POS handling. |
| 21 |
*/ |
| 22 |
private const MANUAL_GATEWAYS = array( 'pos_cash', 'pos_card' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Infer POS type for a gateway. |
| 26 |
* |
| 27 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 28 |
* @param WP_REST_Request $request Request object. |
| 29 |
*/ |
| 30 |
public function infer_pos_type( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string { |
| 31 |
$default = in_array( $gateway->id, self::MANUAL_GATEWAYS, true ) ? 'manual' : 'manual'; |
| 32 |
|
| 33 |
return (string) apply_filters( 'wcpos_payment_gateway_pos_type', $default, $gateway, $request ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Provider family identifier. |
| 38 |
* |
| 39 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 40 |
* @param WP_REST_Request $request Request object. |
| 41 |
*/ |
| 42 |
public function get_provider( WC_Payment_Gateway $gateway, WP_REST_Request $request ): string { |
| 43 |
return (string) apply_filters( 'wcpos_payment_gateway_provider', $gateway->id, $gateway, $request ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Provider-specific public metadata. |
| 48 |
* |
| 49 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 50 |
* @param WP_REST_Request $request Request object. |
| 51 |
*/ |
| 52 |
public function get_provider_data( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array { |
| 53 |
return (array) apply_filters( 'wcpos_payment_gateway_provider_data', array(), $gateway, $request ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Whether a gateway is enabled for POS. |
| 58 |
* |
| 59 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 60 |
*/ |
| 61 |
public function is_pos_enabled( WC_Payment_Gateway $gateway ): bool { |
| 62 |
$settings = woocommerce_pos_get_settings( 'payment_gateways' ); |
| 63 |
|
| 64 |
if ( is_wp_error( $settings ) ) { |
| 65 |
return wc_string_to_bool( $gateway->enabled ); |
| 66 |
} |
| 67 |
|
| 68 |
$pos_setting = $settings['gateways'][ $gateway->id ] ?? array(); |
| 69 |
|
| 70 |
return isset( $pos_setting['enabled'] ) ? (bool) $pos_setting['enabled'] : wc_string_to_bool( $gateway->enabled ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Whether a gateway supports the POS checkout contract. |
| 75 |
* |
| 76 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 77 |
* @param WP_REST_Request $request Request object. |
| 78 |
*/ |
| 79 |
public function supports_checkout( WC_Payment_Gateway $gateway, WP_REST_Request $request ): bool { |
| 80 |
$has_handler = false !== has_action( 'wcpos_process_checkout_action_' . $gateway->id ); |
| 81 |
|
| 82 |
return (bool) apply_filters( 'wcpos_payment_gateway_supports_checkout', $has_handler, $gateway, $request ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Capabilities exposed to the POS app. |
| 87 |
* |
| 88 |
* @param WC_Payment_Gateway $gateway Gateway object. |
| 89 |
* @param WP_REST_Request $request Request object. |
| 90 |
*/ |
| 91 |
public function get_capabilities( WC_Payment_Gateway $gateway, WP_REST_Request $request ): array { |
| 92 |
$pos_type = $this->infer_pos_type( $gateway, $request ); |
| 93 |
$supports_provider_refunds = ! in_array( $gateway->id, self::MANUAL_GATEWAYS, true ) && $gateway->supports( 'refunds' ); |
| 94 |
|
| 95 |
return array( |
| 96 |
'supports_checkout' => $this->supports_checkout( $gateway, $request ), |
| 97 |
'supports_automatic_refunds' => (bool) apply_filters( 'wcpos_payment_gateway_supports_automatic_refunds', $supports_provider_refunds, $gateway, $request ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 98 |
'supports_provider_refunds' => (bool) apply_filters( 'wcpos_payment_gateway_supports_provider_refunds', $supports_provider_refunds, $gateway, $request ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 99 |
'requires_hardware' => 'terminal' === $pos_type, |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Default bootstrap response. |
| 105 |
* |
| 106 |
* @param string $gateway_id Gateway ID. |
| 107 |
* @param array $context Bootstrap context. |
| 108 |
* @param WP_REST_Request $request Request object. |
| 109 |
*/ |
| 110 |
public function get_bootstrap_response( string $gateway_id, array $context, WP_REST_Request $request ): array { |
| 111 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public POS gateway contract filter. |
| 112 |
return (array) apply_filters( |
| 113 |
'wcpos_payment_gateway_bootstrap', |
| 114 |
array( |
| 115 |
'gateway_id' => $gateway_id, |
| 116 |
'status' => 'ready', |
| 117 |
'expires_at' => null, |
| 118 |
'provider_data' => array(), |
| 119 |
), |
| 120 |
$gateway_id, |
| 121 |
$context, |
| 122 |
$request |
| 123 |
); |
| 124 |
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Whether a checkout status is terminal. |
| 129 |
* |
| 130 |
* @param string $status Checkout status. |
| 131 |
*/ |
| 132 |
public function is_terminal_status( string $status ): bool { |
| 133 |
return in_array( $status, array( 'completed', 'failed', 'cancelled', 'awaiting_customer' ), true ); |
| 134 |
} |
| 135 |
} |
| 136 |
|