| 1 |
<?php |
| 2 |
/** |
| 3 |
* POS gateway bootstrap 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_Error; |
| 16 |
use WP_REST_Request; |
| 17 |
use WP_REST_Server; |
| 18 |
|
| 19 |
/** |
| 20 |
* POS gateway bootstrap controller. |
| 21 |
*/ |
| 22 |
class Gateway_Bootstrap_Controller extends WC_REST_Controller { |
| 23 |
/** |
| 24 |
* Shared gateway contract helper. |
| 25 |
* |
| 26 |
* @var Gateway_Contract |
| 27 |
*/ |
| 28 |
private $gateway_contract; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
$this->gateway_contract = new Gateway_Contract(); |
| 35 |
} |
| 36 |
/** |
| 37 |
* REST namespace. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $namespace = 'wcpos/v1'; |
| 42 |
|
| 43 |
/** |
| 44 |
* REST base. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $rest_base = 'payment-gateways'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Register routes. |
| 52 |
*/ |
| 53 |
public function register_routes(): void { |
| 54 |
register_rest_route( |
| 55 |
$this->namespace, |
| 56 |
'/' . $this->rest_base . '/(?P<gateway_id>[a-zA-Z0-9_-]+)/bootstrap', |
| 57 |
array( |
| 58 |
array( |
| 59 |
'methods' => WP_REST_Server::CREATABLE, |
| 60 |
'callback' => array( $this, 'create_item' ), |
| 61 |
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
| 62 |
'args' => array( |
| 63 |
'context' => array( |
| 64 |
'type' => 'array', |
| 65 |
'validate_callback' => array( $this, 'validate_context_param' ), |
| 66 |
'sanitize_callback' => array( $this, 'sanitize_context_param' ), |
| 67 |
), |
| 68 |
), |
| 69 |
), |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create permissions check. |
| 76 |
* |
| 77 |
* @param WP_REST_Request $request Request object. |
| 78 |
*/ |
| 79 |
public function create_item_permissions_check( $request ) { |
| 80 |
return current_user_can( 'publish_shop_orders' ) |
| 81 |
? true |
| 82 |
: new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot bootstrap payment gateways.', 'woocommerce-pos' ), array( 'status' => rest_authorization_required_code() ) ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Return bootstrap data for a gateway. |
| 87 |
* |
| 88 |
* @param WP_REST_Request $request Request object. |
| 89 |
*/ |
| 90 |
public function create_item( $request ) { |
| 91 |
$gateway_id = (string) $request['gateway_id']; |
| 92 |
$gateway = $this->get_gateway( $gateway_id ); |
| 93 |
|
| 94 |
if ( ! $gateway ) { |
| 95 |
return new WP_Error( |
| 96 |
'wcpos_payment_gateway_not_found', |
| 97 |
/* translators: REST API schema field label or error message. */ |
| 98 |
__( 'Payment gateway not found.', 'woocommerce-pos' ), |
| 99 |
array( 'status' => 404 ) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
$context = (array) $request->get_param( 'context' ); |
| 104 |
$data = $this->gateway_contract->get_bootstrap_response( $gateway_id, $context, $request, $gateway ); |
| 105 |
|
| 106 |
return rest_ensure_response( $data ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Validate bootstrap context input. |
| 111 |
* |
| 112 |
* @param mixed $value Context value. |
| 113 |
*/ |
| 114 |
public function validate_context_param( $value ): bool { |
| 115 |
return is_array( $value ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Sanitize bootstrap context input. |
| 120 |
* |
| 121 |
* @param mixed $value Context value. |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public function sanitize_context_param( $value ): array { |
| 126 |
return is_array( $value ) ? $value : array(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get a gateway by ID. |
| 131 |
* |
| 132 |
* @param string $gateway_id Gateway ID. |
| 133 |
*/ |
| 134 |
private function get_gateway( string $gateway_id ): ?WC_Payment_Gateway { |
| 135 |
WC()->payment_gateways(); |
| 136 |
$gateways = WC()->payment_gateways->payment_gateways(); |
| 137 |
|
| 138 |
return $gateways[ $gateway_id ] ?? null; |
| 139 |
} |
| 140 |
} |
| 141 |
|