Laravel收银台不存卡版本10.1

Laravel Cashier not storing card version 10.1

我正在使用 Cashier 10.1 创建一个新应用程序。过去,在注册用户时,我会从 Stripe 向订阅函数发送一个令牌。现在它说它需要一种付款方式(id)。我正在使用 Stripe 注册支付方式并将其传递给我的控制器,但它 returns 错误“此客户没有附加的支付来源 ”。我阅读了文档,但它仅提供了一个示例,说明如何通过传递给视图 $user->createSetupIntent() 来向当前用户添加订阅。下面是用户注册时的代码:

组件

       pay() {
            this.isLoading = true;

            if (this.form.payment_method == "cc") {
                let that = this;
                this.stripe
                    .createPaymentMethod({
                        type: "card",
                        card: that.card
                    })
                    .then(result => {
                        this.form.stripePayment = result.paymentMethod.id;
                        this.register();
                    })
                    .catch(e => {
                        console.log(e);
                    });
            } else {
                this.register();
            }
        },

        register() {
            this.form
                .post("/register")
                .then(data => {
                    this.isLoading = false;
                    if (data.type == "success") {
                        this.$swal({
                            type: "success",
                            title: "Great...",
                            text: data.message,
                            toast: true,
                            position: "top-end",
                            showConfirmButton: false,
                            timer: 2000
                        });

                        setTimeout(() => {
                            // window.location.replace(data.url);
                        }, 2000);
                    }
                })
                .catch(error => {
                    this.isLoading = false;
                });
        }

注册控制器

protected function create(request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|unique:users',
            'username' => 'required|alpha_dash|unique:users',
            'phone' => 'required',
            'city' => 'required',
            'state' => 'required',
            'password' => 'required|confirmed',
            'agree' => 'required',
        ]);

        $user = User::create([
            'username' => $request['username'],
            'name' => $request['name'],
            'email' => $request['email'],
            'phone' => $request['phone'],
            'city' => $request['city'],
            'state' => $request['state'],
            'password' => Hash::make($request['password']),
        ]);

        if ($request['subscription_type'] == 'premier' && $request['payment_method'] == 'cc') {

            $user->newSubscription('default',  env('STRIPE_PLAN_ID'))->create($request->input('stripePayment'), [
                'email' => $request['email'],
            ]);
        }



        if ($user) {
            $user->assignRole('subscriber');



            Auth::login($user);

            return response()->json([
                'type' => 'success',
                'message' => 'you are all set.',
                'url' => '/dashboard'
            ]);
        }

由于 Stripe 上的 Strong Customer Authentication (SCA). This means card payments require a different user experience, namely 3D Secure, in order to meet SCA requirements. It may be beneficial to read-up on the Payment Intents API,Cashier 已经更新了它与 Stripe 交互的方式,您可以通过首先创建与 Stripe 直接交互的支付意图,然后将其附加到您的Laravel 用户。

简单的解决方案可能是采用多步骤注册过程:

  • 第 1 步: 收集客户详细信息并在 Laravel 和 Stripe
  • 上创建用户
  • 第 2 步: 创建付款意向 $user->createSetupIntent() 并收集付款详细信息并保存给客户。
  • 第 3 步: 为用户订阅收银员计划

我终于找到了一个很好的解释新设置的 article。基本上,当我显示我的注册视图时,我现在创建一个新用户并在那里传递意图。我一直认为用户必须已经保存,而不仅仅是创建。所以如果有人想这样做:

显示视图

注册控制器

public function show()
    {
        $user = new User;

        return view('auth.register', [
            'intent' => $user->createSetupIntent()
        ]);
    }

将意图传递给我的 Vue 组件

<register-form stripe-key="{{ env('STRIPE_KEY') }}" stripe-intent="{{ $intent->client_secret }}">
            </register-form>

将条纹元素添加到 div:

 mounted() {
        // Create a Stripe client.
        this.stripe = Stripe(this.stripeKey);

        // Create an instance of Elements.
        var elements = this.stripe.elements();

        var style = {
            base: {
                color: "#32325d",
                fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
                fontSmoothing: "antialiased",
                fontSize: "16px",
                "::placeholder": {
                    color: "#aab7c4"
                }
            },
            invalid: {
                color: "#fa755a",
                iconColor: "#fa755a"
            }
        };

        // Create an instance of the card Element.
        this.card = elements.create("card", { style: style });

        // Add an instance of the card Element into the `card-element` <div>.
        this.card.mount("#card-element");
    },

处理数据

pay() {
            this.isLoading = true;

            if (this.form.payment_method == "cc") {
                this.setupCard();
            } else {
                this.register();
            }
        },

        setupCard() {
            this.stripe
                .handleCardSetup(this.stripeIntent, this.card, {
                    payment_method_data: {
                        billing_details: { name: this.form.name }
                    }
                })
                .then(data => {
                    this.form.stripePayment = data.setupIntent.payment_method;
                    if (this.form.stripePayment) this.register();
                })
                .catch(error => {
                    this.isLoading = false;
                    console.log(error);
                });
        },

        register(setupIntent) {
            this.form
                .post("/register")
                .then(data => {
                    this.isLoading = false;
                    if (data.type == "success") {
                        this.$swal({
                            type: "success",
                            title: "Great...",
                            text: data.message,
                            toast: true,
                            position: "top-end",
                            showConfirmButton: false,
                            timer: 2000
                        });

                        setTimeout(() => {
                            window.location.replace(data.url);
                        }, 2000);
                    }
                })
                .catch(error => {
                    this.isLoading = false;
                });
        }

保存用户

注册控制器

protected function create(request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|unique:users',
            'username' => 'required|alpha_dash|unique:users',
            'phone' => 'required',
            'city' => 'required',
            'state' => 'required',
            'password' => 'required|confirmed',
            'agree' => 'required',
        ]);

        $user = User::create([
            'username' => $request['username'],
            'name' => $request['name'],
            'email' => $request['email'],
            'phone' => $request['phone'],
            'city' => $request['city'],
            'state' => $request['state'],
            'password' => Hash::make($request['password']),
        ]);

        if ($request['subscription_type'] == 'premier' && $request['payment_method'] == 'cc') {

            $user->newSubscription('default',  env('STRIPE_PLAN_ID'))->create($request->input('stripePayment'), [
                'email' => $request['email'],
            ]);
        }



        if ($user) {
            $user->assignRole('subscriber');



            Auth::login($user);

            return response()->json([
                'type' => 'success',
                'message' => 'you are all set.',
                'url' => '/dashboard'
            ]);
        }
    }