Laravel Cashier-“此客户没有附加的付款来源或默认付款方式。”

时间:2020-08-25 18:44:21

标签: javascript php laravel stripe-payments laravel-cashier

我正在使用Laravel 5.8版本和Cashier版本10.7.1

我想在用户注册时开始收取用户订阅的月费。

所以我写到RegisterController.php

set -l test_python (fish -c "'$VIRTUALENV_HOME/$i' -V" 2>/dev/null)

我也添加到register.blade.php文件中:

public function register(Request $request)
    {
        $this->validator($request->all())->validate();


        DB::beginTransaction();

        event(new Registered($user = $this->create($request->all())));
        
        try {
            $newSubscription = $user->newSubscription('Standard Plan', 'price_1HJmedAuGtYRVrPxN9PNSPKi')->create($request->payment_method, ['email' => $user->email]);
        } catch ( IncompletePayment $exception ){
            DB::rollback();
            return redirect()->back()->with(['error_message' => $exception->getMessage()]);
        }

        DB::commit();

        $this->guard()->login($user);

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

和:

<div class="flex flex-wrap mb-6">
                            <label for="card-element" class="block text-gray-700 text-sm font-bold mb-2">
                                Credit Card Info
                            </label>
                            <div id="card-element" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"></div>
                            <div id="card-errors" class="text-red-400 text-bold mt-2 text-sm font-medium"></div>
                        </div>

当我尝试注册新用户并使用CC详细信息进行测试收费4242 4242 4242 4242时出现错误:

条带\异常\ InvalidRequestException该客户没有 附加的付款来源或默认付款方式。

出什么问题了?我该如何解决?

1 个答案:

答案 0 :(得分:1)

创建付款方式后,您需要attach it to a Customer

$stripe->paymentMethods->attach(
  'pm_123',
  ['customer' => 'cus_abc']
);