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

如何为Stripe结账信息添加自定义字段并通过Webhook传输

解决方案:为PMPro + Stripe添加自定义客户标识符到Webhook载荷

要实现将自定义客户唯一标识符传入Stripe Webhook的需求,需要利用Paid Membership Pro官方提供的Stripe集成钩子,直接在创建Stripe客户或支付Intent时注入元数据。以下是具体步骤:

1. 核心代码实现(添加到主题/子主题的functions.php)

方法一:为Stripe客户添加元数据

这个钩子会在PMPro创建Stripe客户对象时触发,将用户的自定义标识符存入Stripe客户的元数据中,后续所有关联该客户的Webhook事件都会携带此信息:

// 为Stripe客户添加自定义唯一标识符元数据
add_filter( 'pmpro_stripe_customer_args', 'pmpro_add_custom_customer_id_to_stripe', 10, 2 );
function pmpro_add_custom_customer_id_to_stripe( $args, $user ) {
    // 替换为你存储客户唯一标识符的用户meta键名
    $custom_unique_id = get_user_meta( $user->ID, 'your_custom_unique_key', true );

    if ( ! empty( $custom_unique_id ) ) {
        // 将自定义ID加入Stripe客户元数据
        $args['metadata']['customer_unique_identifier'] = $custom_unique_id;
    }

    return $args;
}

方法二:为支付Intent添加元数据(可选)

如果需要在支付相关的Webhook事件(如payment_intent.succeeded)中直接获取该标识符,可以额外添加这个钩子,确保支付Intent本身也携带元数据:

// 为Stripe Payment Intent添加自定义唯一标识符元数据
add_filter( 'pmpro_stripe_payment_intent_args', 'pmpro_add_custom_id_to_stripe_payment_intent', 10, 3 );
function pmpro_add_custom_id_to_stripe_payment_intent( $args, $order, $user ) {
    $custom_unique_id = get_user_meta( $user->ID, 'your_custom_unique_key', true );

    if ( ! empty( $custom_unique_id ) ) {
        $args['metadata']['customer_unique_identifier'] = $custom_unique_id;
    }

    return $args;
}

2. 关键配置说明

  • 替换代码中的your_custom_unique_key为你在PMPro用户字段中创建的自定义字段的元键名(可在PMPro用户字段设置页面查看,或通过WordPress用户编辑页面的自定义字段面板确认)。
  • 元数据的键名customer_unique_identifier可以根据你的需求自定义,只要在Webhook处理时对应即可。

3. 验证方法

  1. 完成代码添加后,创建一个新的测试会员订单并完成支付。
  2. 登录Stripe后台,进入开发者 > Webhooks > 事件,找到对应的payment_intent.succeeded或customer.created事件。
  3. 查看事件的JSON载荷,在data.object.metadata下就能找到你添加的自定义标识符。

注意事项

  • 避免使用非PMPro官方的Stripe钩子,PMPro的集成逻辑会优先处理自身钩子,外部代码可能被覆盖。
  • 如果是已有客户,需要触发一次客户信息同步(比如在WP后台编辑该用户的会员信息并保存),Stripe才会更新客户元数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:05:25