1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Controller_Payment_Method extends WC_Stripe_Rest_Controller {
protected $namespace = 'payment-method';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'token',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'payment_method_from_token' ),
'permission_callback' => '__return_true'
)
);
}
public function payment_method_from_token( $request ) {
$result = WC_Stripe_Gateway::load()->paymentMethods->create(
array(
'type' => 'card',
'card' => array( 'token' => $request->get_param( 'token' ) ),
)
);
if ( is_wp_error( $result ) ) {
return new WP_Error( 'payment-method', $result->get_error_message(), array( 'status' => 200 ) );
}
return rest_ensure_response( array( 'payment_method' => $result->jsonSerialize() ) );
}
}