WooCommerce REST API v2:如何处理付款?
使用,我成功地创建了处于挂起、未付款状态的订单 我可以看到,我可以将WooCommerce REST API v2:如何处理付款?,rest,woocommerce,payment-gateway,Rest,Woocommerce,Payment Gateway,使用,我成功地创建了处于挂起、未付款状态的订单 我可以看到,我可以将order.payment\u details.payment字段设置为true,这将在完成状态下创建订单并发送完成的订单电子邮件,但实际上它不会处理付款 使用REST API v2创建订单并让WooCommerce使用支付网关处理支付的正确方法是什么 或者我需要在服务器端的API中添加一个插件钩子吗?(我想是的) 这是我试过的 curl -X POST https://example.com/wc-api/v2/orders
order.payment\u details.payment
字段设置为true
,这将在完成状态下创建订单并发送完成的订单电子邮件,但实际上它不会处理付款
使用REST API v2创建订单并让WooCommerce使用支付网关处理支付的正确方法是什么
或者我需要在服务器端的API中添加一个插件钩子吗?(我想是的)
这是我试过的
curl -X POST https://example.com/wc-api/v2/orders \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"order": {
"customer_id": 2,
"payment_details": {
"method_id": "da_big_bank",
"method_title": "StackOverflow Money Laundering, Inc.",
"paid":true
},
"line_items": [
{
"product_id": 341,
"quantity": 1
}
]
}
}'
正如我所说,它在完成状态下生成一个订单,但实际上不使用我的网关(它不是“StackOverflow money Launing,Inc.”,是一个合法的网关,在使用我们的WooCommerce网站时确实有效)处理任何金钱,目前还没有一种使用WooCommerce REST API处理订单付款的方法
最后,我在woocommerce\u api\u create\u order
过滤器中编写了一个钩子,该过滤器在创建订单时立即处理订单以进行支付。如果处理失败,那么错误将添加到order->post->post_摘录
字段,使其在JSON响应中显示为order->note
为此,我还必须扩展支付网关,使其process\u payment()
方法接受$user\u id
作为输入。这是因为它是开箱即用的代码,用于对当前登录的用户进行操作,在我的情况下,可能在大多数情况下,该用户是REST客户端登录的系统用户,而不是购买的实际用户
扩展网关的另一个好处是,现在可以返回错误,而不是将错误写入wc\u add\u notice()
。因为这是一个REST服务,所以没有任何东西可以看到wc\u add\u notice()
谢谢你给我的指导 我做了一些修改,简化了步骤 详情如下:
add_filter('woocommerce_api_order_response', 'intercept_api_response', 1, 4);
/**
* Here, intercept api's response to include the url of payment
**/
function intercept_api_response($order_data, $order)
{
$order_data['payment_url'] = $order->payment_url;
return $order_data;
}
add_filter('woocommerce_api_create_order', 'intercept_on_api_create_order', 10, 3);
function intercept_on_api_create_order($id, $data, $api)
{
if (in_array($data['payment_details']['method_id'], ['pagseguro', 'paypal'])) {
$order = wc_get_order($id);
$order->calculate_totals();
if ($data['payment_details']['method_id'] == 'paypal') {
$paypal = new WC_Gateway_Paypal();
$payment = $paypal->process_payment($id);
}
update_post_meta($id, '_payment_url', $payment['redirect']);
}
return $payment;
}
我希望这可以帮助其他人。这是一项艰苦的工作,有大量的尝试和错误。如果您没有处理托管支付网关(需要用户重定向到自己的域以处理paypal之类的支付),那么您可以通过ajax请求以WC的方式完成:
// this is a function as a callback for a restful api - process_payment
// collect payment related info: billing, shipping info, including shipping_method & payment_method
// put all that info inside $_POST global var
// so that the nonce won't fail
$_REQUEST['_wpnonce'] =
wp_create_nonce( 'woocommerce-process_checkout' );
// make it look like an ajax request
wc_maybe_define_constant('DOING_AJAX', 1);
add_filter('woocommerce_payment_successful_result', function($response) {
// wp_send_json appropriate response
}
WC()->checkout()->process_checkout();
WC()->checkout()->process\u checkout()
将为您创建一个订单,如果WC()->cart
不为空
您也可以在托管支付网关上尝试此操作,但它将返回一个重定向
,您可以在移动应用程序的webview中打开该重定向并收集支付
我认为您无法通过API处理付款。至少,我看不到处理支付的部分。如何通过WooCommerce REST使用此插件进行支付此版本忽略了扩展网关和将用户ID传递到流程支付方法调用的关键步骤正确吗?正如在:函数process\u payment($order\u id,$user\u id)中一样,接受的答案对我有效。
// this is a function as a callback for a restful api - process_payment
// collect payment related info: billing, shipping info, including shipping_method & payment_method
// put all that info inside $_POST global var
// so that the nonce won't fail
$_REQUEST['_wpnonce'] =
wp_create_nonce( 'woocommerce-process_checkout' );
// make it look like an ajax request
wc_maybe_define_constant('DOING_AJAX', 1);
add_filter('woocommerce_payment_successful_result', function($response) {
// wp_send_json appropriate response
}
WC()->checkout()->process_checkout();