woocommerce offtopic - Process checkout using WC REST API

With a main site using WordPressWooCommerce, and a remote site serving front end; is it possible to process checkout fo

With a main site using WordPress/WooCommerce, and a remote site serving front end; is it possible to process checkout form using WooCommerce REST API, and receive a redirect URL to the selected Payment Gateway?

I tried creating a custom AJAX function, but It seems it requires a nonce field on the submitted form, and that is problematic for the front end is not on the same server.

With a main site using WordPress/WooCommerce, and a remote site serving front end; is it possible to process checkout form using WooCommerce REST API, and receive a redirect URL to the selected Payment Gateway?

I tried creating a custom AJAX function, but It seems it requires a nonce field on the submitted form, and that is problematic for the front end is not on the same server.

Share Improve this question asked Nov 26, 2017 at 16:18 steakoverflowsteakoverflow 2291 gold badge3 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 8

To process payment for an WooCommerce Order, the workflow goes like the following:

  1. Create an WooCommerce Order
  2. Process Payment for this order, using one of WC_Payment_Gateways
  3. Change order status (default is pending->processing, or to complete if empty order)

IMHO, Step two can be implemented as the following:

  1. Use a front-end lib to tokenize payment info, using frameworks like Stripe or PayPal.
  2. Pass the payment token to your WordPress backend using WP REST API
  3. Let WordPress and WooCommerce process the payment as usual.

You can create a custom REST API endpoint to process payment for your WooCommerce website, add the following code to your code.

add_action( 'rest_api_init', 'wc_rest_payment_endpoints' );
function wc_rest_payment_endpoints() {
    /**
     * Handle Payment Method request.
     */
    register_rest_route( 'wc/v2', 'payment', array(
        'methods'  => 'POST',
        'callback' => 'wc_rest_payment_endpoint_handler',
    ) );
}
function wc_rest_payment_endpoint_handler( $request = null ) {
    $response       = array();
    $parameters     = $request->get_json_params();
    $payment_method = sanitize_text_field( $parameters['payment_method'] );
    $order_id       = sanitize_text_field( $parameters['order_id'] );
    $payment_token  = sanitize_text_field( $parameters['payment_token'] );
    $error          = new WP_Error();

    if ( $payment_method === "stripe" ) {
        $wc_gateway_stripe                = new WC_Gateway_Stripe();
        $_POST['stripe_token']            = $payment_token;
        $payment_result               = $wc_gateway_stripe->process_payment( $order_id );
        if ( $payment_result['result'] === "success" ) {
            $response['code']    = 200;
            $response['message'] = __( "Your Payment was Successful", "wc-rest-payment" );
        } else {
            return new WP_REST_Response( array("c"), 123 );
            $response['code']    = 401;
            $response['message'] = __( "Please enter valid card details", "wc-rest-payment" );
        }
    }  else {
        $response['code'] = 405;
        $response['message'] = __( "Please select an available payment method. Supported payment method can be found at https://wordpress/plugins/wc-rest-payment/#description", "wc-rest-payment" );
    }

    return new WP_REST_Response( $response, 123 );
}

If you're looking for a hassle-free way to do this, you checkout our plugin WC REST Payment.

It supports WooCommerce Gateways including Stripe, PayPal Express, PayPal Standard, Direct Bank Transfer, Cheque, Cash on Delivery.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745061240a4608978.html

相关推荐

  • woocommerce offtopic - Process checkout using WC REST API

    With a main site using WordPressWooCommerce, and a remote site serving front end; is it possible to process checkout fo

    20小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信