Laravel 收银员 - "This customer has no attached payment source or default payment method."

Laravel Cashier - "This customer has no attached payment source or default payment method."

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

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

所以我写入RegisterController.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());
    }

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

<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>

和:

<script src="https://js.stripe.com/v3/"></script>

    <script>
        const stripe = Stripe('pk_test_somekeysomekeyasdasd');
        console.log(stripe);
        const elements = stripe.elements();
        const cardElement = elements.create('card');
        cardElement.mount('#card-element');
        const cardHolderName = document.getElementById('name');
        const cardButton = document.getElementById('card-button');
        const clientSecret = cardButton.dataset.secret;
        let validCard = false;
        const cardError = document.getElementById('card-errors');
        cardElement.addEventListener('change', function(event) {
            
            if (event.error) {
                validCard = false;
                cardError.textContent = event.error.message;
            } else {
                validCard = true;
                cardError.textContent = '';
            }
        });
        var form = document.getElementById('signup-form');
        form.addEventListener('submit', async (e) => {
            event.preventDefault();
            const { paymentMethod, error } = await stripe.createPaymentMethod(
                'card', cardElement, {
                    billing_details: { name: cardHolderName.value }
                }
            );
            if (error) {
                // Display "error.message" to the user...
                console.log(error);
            } else {
                // The card has been verified successfully...
                var hiddenInput = document.createElement('input');
                hiddenInput.setAttribute('type', 'hidden');
                hiddenInput.setAttribute('name', 'payment_method');
                hiddenInput.setAttribute('value', paymentMethod.id);
                form.appendChild(hiddenInput);
                // Submit the form
                form.submit();
            }
        });
    
    </script>

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

Stripe \ Exception \ InvalidRequestException This customer has no attached payment source or default payment method.

问题是什么?我该如何解决?

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

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