swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Stripe_Connection_Tokens_Controller |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * REST controller for connection tokens. |
| 10 | */ |
| 11 | class WC_REST_Stripe_Connection_Tokens_Controller extends WC_Stripe_REST_Base_Controller { |
| 12 | |
| 13 | /** |
| 14 | * Endpoint path. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $rest_base = 'wc_stripe/connection_tokens'; |
| 19 | |
| 20 | /** |
| 21 | * Stripe payment gateway. |
| 22 | * |
| 23 | * @var WC_Gateway_Stripe |
| 24 | */ |
| 25 | private $gateway; |
| 26 | |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @param WC_Gateway_Stripe $gateway Stripe payment gateway. |
| 31 | */ |
| 32 | public function __construct( WC_Gateway_Stripe $gateway ) { |
| 33 | $this->gateway = $gateway; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Configure REST API routes. |
| 38 | */ |
| 39 | public function register_routes() { |
| 40 | register_rest_route( |
| 41 | $this->namespace, |
| 42 | '/' . $this->rest_base, |
| 43 | [ |
| 44 | 'methods' => WP_REST_Server::CREATABLE, |
| 45 | 'callback' => [ $this, 'create_token' ], |
| 46 | 'permission_callback' => [ $this, 'check_permission' ], |
| 47 | ] |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Create a connection token via API. |
| 53 | * |
| 54 | * @param WP_REST_Request $request Full data about the request. |
| 55 | */ |
| 56 | public function create_token( $request ) { |
| 57 | $response = WC_Stripe_API::request( [], 'terminal/connection_tokens' ); |
| 58 | |
| 59 | if ( ! isset( $response->secret ) ) { |
| 60 | return rest_ensure_response( new WP_Error( 'wc_stripe_no_token', __( 'Stripe API did not return a connection token.', 'woocommerce-gateway-stripe' ) ) ); |
| 61 | } |
| 62 | |
| 63 | $response->test_mode = $this->gateway->is_in_test_mode(); |
| 64 | return rest_ensure_response( $response ); |
| 65 | } |
| 66 | } |