Laravel Cashier Stripe 地址信息

问题描述 投票:0回答:2

我在向 stripe 提供客户地址信息时遇到问题。

这是我的条带托管结账流程的结账流程:

 $user = $request->user();
        
        return $user->newSubscription(config('stripe.product'), $plan)
            ->allowPromotionCodes()
            ->checkout([
                'success_url' => route('success'),
                'cancel_url' => route('dashboard'),
                'customer' => [
                    'address' => [
                        'line1'       => $user->street,
                        'line2'       => $user->company,
                        'city'        => $user->city,
                        'postal_code' => $user->plz,
                        'country'     => $user->country,
                        'state'       =>  $user->state,
                        ],
                    'name' => $user->name,
                    'email' => $user->email,
                ],
            ]);

订阅工作正常,stripe 提供的计费页面也正常。

如果我在将地址信息发送到 stripe 之前添加地址信息,则所有地址字段都会正确填写。但条纹并不能拯救他们。

当我检查条带中的日志以创建新客户时,这就是结果(测试模式):

{
  "object": {
    "id": "cus_R5bYAaOTUyIhBz",
    "object": "customer",
    "address": null,
    "balance": 0,
    "created": 1729773631,
    "currency": null,
    "default_source": null,
    "delinquent": false,
    "description": null,
    "discount": null,
    "email": "[email protected]",
    "invoice_prefix": "B079FB88",
    "invoice_settings": {
      "custom_fields": null,
      "default_payment_method": null,
      "footer": null,
      "rendering_options": null
    },
    "livemode": false,
    "metadata": {
    },
    "name": "Kuame Greer",
    "phone": null,
    "preferred_locales": [
    ],
    "shipping": null,
    "tax_exempt": "none",
    "test_clock": null
  }
}

如您所见,地址对象为空。

我的 Laravel 版本是 v10.48.22,cashier/stripe 版本是 v15.4.3。

我已经配置了网络钩子,其他一切都工作正常

在这里您可以看到此测试用例的网络钩子:

2024-10-24 14:40:32   --> customer.created [evt_1QDQL6LkBjrUprZrDsqUbpIS]
2024-10-24 14:40:32  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQL6LkBjrUprZrDsqUbpIS]
2024-10-24 14:40:59   --> charge.succeeded [evt_3QDQLVLkBjrUprZr3pODaVpl]
2024-10-24 14:40:59   --> checkout.session.completed [evt_1QDQLXLkBjrUprZrZJfRgmM4]
2024-10-24 14:40:59  <--  [200] POST http://localhost:8000/stripe/webhook [evt_3QDQLVLkBjrUprZr3pODaVpl]
2024-10-24 14:40:59  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLXLkBjrUprZrZJfRgmM4]
2024-10-24 14:40:59   --> payment_method.attached [evt_1QDQLXLkBjrUprZrK7ZSntor]
2024-10-24 14:40:59   --> customer.updated [evt_1QDQLXLkBjrUprZrROH0loqv]
2024-10-24 14:40:59  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLXLkBjrUprZrK7ZSntor]
2024-10-24 14:40:59   --> customer.subscription.created [evt_1QDQLXLkBjrUprZr8apagy2E]
2024-10-24 14:41:00   --> customer.subscription.updated [evt_1QDQLXLkBjrUprZrJtNlNzrE]
2024-10-24 14:41:00   --> payment_intent.succeeded [evt_3QDQLVLkBjrUprZr3UMdJy8Y]
2024-10-24 14:41:00   --> payment_intent.created [evt_3QDQLVLkBjrUprZr3WAiZ3Tf]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLXLkBjrUprZrROH0loqv]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLXLkBjrUprZr8apagy2E]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLXLkBjrUprZrJtNlNzrE]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_3QDQLVLkBjrUprZr3UMdJy8Y]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_3QDQLVLkBjrUprZr3WAiZ3Tf]
2024-10-24 14:41:00   --> invoice.created [evt_1QDQLYLkBjrUprZrGFq5uaiy]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLYLkBjrUprZrGFq5uaiy]
2024-10-24 14:41:00   --> invoice.finalized [evt_1QDQLYLkBjrUprZr1jWBUzxo]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLYLkBjrUprZr1jWBUzxo]
2024-10-24 14:41:00   --> invoice.updated [evt_1QDQLYLkBjrUprZr0mMUimgL]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLYLkBjrUprZr0mMUimgL]
2024-10-24 14:41:00   --> invoice.paid [evt_1QDQLYLkBjrUprZrdJWCKNyP]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLYLkBjrUprZrdJWCKNyP]
2024-10-24 14:41:00   --> invoice.payment_succeeded [evt_1QDQLYLkBjrUprZrWgc2e6sz]
2024-10-24 14:41:00  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQLYLkBjrUprZrWgc2e6sz]
2024-10-24 14:41:58   --> billing_portal.session.created [evt_1QDQMULkBjrUprZr9S3iWOPj]
2024-10-24 14:41:58  <--  [200] POST http://localhost:8000/stripe/webhook [evt_1QDQMULkBjrUprZr9S3iWOPj]

我做错了什么?

感谢您的帮助,

干杯 拉斐尔

laravel stripe-payments laravel-cashier
2个回答
0
投票

好的,我现在找到了解决方案。 使用地址信息结账时似乎无法创建用户。

所以我手动创建了用户,然后添加地址信息,然后使用这个客户进行结账:

$user = $request->user();

    
        $stripeCustomer = $user->createOrGetStripeCustomer();

        
        $user->updateStripeCustomer([
            'address' => [
                'line1'       => $user->street,
                'line2'       => null,
                'city'        => $user->city,
                'postal_code' => $user->plz,
                'country'     => $user->country,
            ],
            'name'  => $user->name,
            'email' => $user->email,
        ]);

        return $user->newSubscription(config('stripe.product'), $plan)
            ->allowPromotionCodes()
            ->checkout([
                'success_url' => route('success'),
                'cancel_url' => route('dashboard'),
                'customer' => $stripeCustomer->id, 
            ]);

0
投票

通过 Stripe Checkout 提交的账单地址不会保存在 Customer 对象上,而是保存在结账后创建的 PaymentMethod 对象上 - https://docs.stripe.com/api/ payment_methods/object# payment_method_object-billing_details

如果您手头已有地址,则可以使用上面建议的替代方案。如果您依赖通过 Stripe Checkout 页面收集地址,您可以在会话完成后更新客户对象。

您可以监听

checkout.session.completed
webhook 事件,检索
payment_intent
下关联的付款方式,检查账单详细信息并通过调用 API 更新 Customer 对象。

© www.soinside.com 2019 - 2024. All rights reserved.