要处理WooCommerce订单的付款,工作流如下所示:
创建WooCommerce订单,使用其中一个WC\\U付款网关处理此订单的付款(默认为挂起->处理,或在订单为空时完成)
IMHO,步骤二可以按以下方式执行:使用Stripe或PayPal等框架,使用前端lib标记支付信息使用WP REST API将支付令牌传递给WordPress后端,让WordPress和WooCommerce像往常一样处理支付
您可以创建自定义REST API端点来处理WooCommerce网站的付款,请将以下代码添加到代码中。
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.org/plugins/wc-rest-payment/#description", "wc-rest-payment" );
}
return new WP_REST_Response( $response, 123 );
}
如果您正在寻找一种无麻烦的方法来实现这一点,请签出我们的插件
WC REST Payment.
它支持WooCommerce网关,包括Stripe, PayPal Express, 贝宝标准,直接银行转账,支票,货到付款。