You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce支付完成后调用API创建/停用会员账户的实现方法咨询

WooCommerce 优势俱乐部账户同步实现方案

一、支付完成后触发API创建账户

  • 选用WooCommerce核心钩子woocommerce_order_status_completed,仅当订单状态变为「已完成」时触发(确保支付流程真正闭环)
  • 从订单对象中提取关联用户的关键信息,作为API请求参数
  • 使用WordPress内置的wp_remote_post发送POST请求,避免使用外部HTTP库
  • 记录API调用日志,方便后续排查问题

示例代码:

add_action('woocommerce_order_status_completed', 'create_club_account_on_payment', 10, 1);
function create_club_account_on_payment($order_id) {
    $order = wc_get_order($order_id);
    $user = $order->get_user();
    
    // 跳过未关联用户的访客订单
    if (!$user) return;

    // 组装API所需数据(根据你的API要求调整字段)
    $api_payload = [
        'user_id'       => $user->ID,
        'email'         => $user->user_email,
        'username'      => $user->user_login,
        'order_number'  => $order->get_order_number(),
        'membership'    => 'advantage_club'
    ];

    // 发送API请求
    $response = wp_remote_post('https://your-api-domain.com/create-account', [
        'method'    => 'POST',
        'headers'   => ['Content-Type' => 'application/json'],
        'body'      => json_encode($api_payload),
        'timeout'   => 15
    ]);

    // 处理响应并记录日志
    if (is_wp_error($response)) {
        error_log('Club account creation failed (Order ' . $order_id . '): ' . $response->get_error_message());
    } else {
        $status_code = wp_remote_retrieve_response_code($response);
        if ($status_code !== 200) {
            error_log('Club account API error (Order ' . $order_id . '): Status code ' . $status_code);
        } else {
            // 标记订单已完成同步,防止重复触发
            $order->update_meta_data('_club_account_created', '1');
            $order->save();
        }
    }
}

二、停止付费时触发API停用账户

如果是订阅类付费模式,推荐使用WooCommerce Subscriptions插件的专属钩子:

  • woocommerce_subscription_status_cancelled:用户主动取消订阅时触发
  • woocommerce_subscription_status_expired:订阅到期未续费触发
  • woocommerce_subscription_status_on-hold:订阅暂停时可选触发

示例代码:

// 绑定订阅取消、到期的钩子
add_action('woocommerce_subscription_status_cancelled', 'disable_club_account_on_stop_pay', 10, 1);
add_action('woocommerce_subscription_status_expired', 'disable_club_account_on_stop_pay', 10, 1);

function disable_club_account_on_stop_pay($subscription) {
    $user = $subscription->get_user();
    if (!$user) return;

    // 组装停用API的请求数据
    $api_payload = [
        'user_id'           => $user->ID,
        'subscription_id'   => $subscription->get_id(),
        'action'            => 'disable'
    ];

    // 发送API请求
    $response = wp_remote_post('https://your-api-domain.com/disable-account', [
        'method'    => 'POST',
        'headers'   => ['Content-Type' => 'application/json'],
        'body'      => json_encode($api_payload),
        'timeout'   => 15
    ]);

    // 处理响应并记录日志
    if (is_wp_error($response)) {
        error_log('Club account disable failed (Subscription ' . $subscription->get_id() . '): ' . $response->get_error_message());
    } else {
        $status_code = wp_remote_retrieve_response_code($response);
        if ($status_code !== 200) {
            error_log('Club account disable API error (Subscription ' . $subscription->get_id() . '): Status code ' . $status_code);
        } else {
            // 标记订阅已完成停用同步
            $subscription->update_meta_data('_club_account_disabled', '1');
            $subscription->save();
        }
    }
}

关键注意事项

  • 非订阅场景适配:如果是一次性购买长期权益的产品,需要通过WordPress定时任务(wp_cron)定期检查权益到期时间,触发停用API
  • API安全:在请求头中加入API密钥或签名,比如'Authorization' => 'Bearer YOUR_API_KEY',防止未授权调用
  • 重复触发防护:通过自定义元数据标记订单/订阅的同步状态,避免同一事件多次调用API
  • 调试与日志:开启WordPress调试模式或使用日志插件,跟踪API调用的请求和响应细节

内容的提问来源于stack exchange,提问作者Igor Alfarth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 19:00:07