stripe error: No such PaymentMethod: 'pm_xxx'

stripe error: No such PaymentMethod: 'pm_xxx'

真的很难找到我错过的错误。我正在为我的电子商务网络应用程序使用 Laravel (v8)、Vue (v2) 和 Stripe(v3)。我在 TEST 模式下成功实现了 stripe,并且运行良好。当我切换到实时模式时,出现以下错误:No such PaymentMethod: 'pm_1Yyl5xC4bpPAffpGV2p0ZL12'.

前后端脚本如下图

async mounted(){
        
this.stripe = await loadStripe(process.env.MIX_STRIPE_KEY);

        const elements = this.stripe.elements()
        this.cardElement = elements.create('card', {
            classes: {
                base: 'bg-gray-100 rounded border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 p-3 leading-8 transition-colors duration-200 ease-in-out'
            }
        })

        this.cardElement.mount('#card-element')
        window.scrollTo(0, 0)
    },

支付方式如下:

async processPayment(){
            //send the payment information to Laravel + Stripe

            this.paymentProcessing = true
            this.billingAddressValidations = {}
            this.stripeErrors = null
            const {paymentMethod, error} = await this.stripe.createPaymentMethod(
                'card', this.cardElement, {
                    billing_details: {
                        name: this.card2.fullName,
                    },
                }
            )

            if(error){
                this.paymentProcessing = false
                alert(error.message)
            } else {

                this.shippingAddress.payment_method_id = paymentMethod.id
                this.shippingAddress.amount = this.cart.reduce((acc, item) => acc + this.itemPrice(item) * item.quantity, 0)
                this.shippingAddress.cart = JSON.stringify(this.cart)
                this.shippingAddress.isBillingAddress = this.isBillingAddress
                this.shippingAddress.billingAddress = this.billingAddress
                this.shippingAddress.orderDetails = this.orderDetails


                const res = await this.apiCall('POST','/api/purchase', this.shippingAddress )
                if(res.status === 200 || res.status === 201 ){
                    this.paymentProcessing = false

                    this.$store.commit('updateOrder', res.data)
                    await this.$store.dispatch('clearCart')
                    //this.orderConfirmation(response.data.transaction_id)
                    await this.$router.push({ name: 'order.summary' })
                }
                if(res.status === 206){
                    if(res.data.type === 'billing_address_error') {
                        this.billingAddressValidations = res.data
                    }
                    if(res.data.type === 'stripe_error') {
                        this.stripeErrors = res.data
                    }
                    this.paymentProcessing = false
                }



            }

        },

和 Laravel/Cashier 后端如下:

public function purchase(Request $request)
    {
        
        $order_number = "JT2070215";


        $user = User::firstOrCreate(
            [
                'email' => $request->input('email')
            ],
            [
                'password' => Hash::make(Str::random(12)),
                'name' => $request->input('first_name') . ' ' . $request->input('last_name'),
                'address' => $request->input('address'),
                'city' => $request->input('city'),
                'line2' => $request->input('line2'),
                'zip_code' => $request->input('post_code'),
            ]
        );

        try {
            $user->createOrGetStripeCustomer();
            $user->addPaymentMethod($request->input('payment_method_id'));
            $payment = $user->charge(
                $request->input('amount'),
                    $request->input('payment_method_id'),[
                    'currency'=>'GBP',
                    'customer'=>$user->stripe_id
                ],
                
            );

            $payment = $payment->asStripePaymentIntent();

            $order = $user->orders()
                ->create([
                    'transaction_id' => $payment->charges->data[0]->id,
                    'total' => $payment->charges->data[0]->amount,
                    'name' => $request->first_name." ".$request->last_name,
                    'email' => $request->email,
                    'address' => $request->address,
                    'city' => $request->city,
                    'country' => $request->country,
                    'post_code' => $request->postal_code,
                    'order_number'=>$order_number
                ]);

    dispatch(new OrderConfirmationEmailJob($order->transaction_id));
            return $order;

        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage(), 'type'=>'stripe_error'], 206);
        }

    }

“No such...”错误通常是由于 API 密钥不匹配(例如,混合使用测试密钥和实时密钥)或尝试访问存在于不同服务器上的对象帐户(例如,尝试从您的平台帐户对在关联帐户上创建的对象执行操作)

您好,您正在尝试传递创建付款方式时使用的 payment_token。为了让您获得 ID,您需要从刚刚创建的付款方式中检索它。

所以这应该有效。

$paymentMethod = $user->addPaymentMethod($request->input('payment_method_id'));

$paymentId = $paymentMethod->id  // use this for the charge