【问题标题】:stripe error: No such PaymentMethod: 'pm_xxx'条纹错误:没有这样的 PaymentMethod:'pm_xxx'
【发布时间】:2021-08-23 00:17:51
【问题描述】:

真的很难找到我错过的错误。我正在为我的电子商务网络应用程序使用 Laravel (v8)、Vue (v2) 和 Stripe(v3)。我成功地在 TEST 模式下实现了条带,它工作得很好。当我切换到实时模式时,我收到以下错误: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);
        }

    }

【问题讨论】:

    标签: laravel vue.js stripe-payments e-commerce laravel-cashier


    【解决方案1】:

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

    【讨论】:

    • 是的,我修好了,你是。当应用程序上传到共享主机时,它仍在保存导致错误的测试公钥。非常感谢朋友!
    【解决方案2】:

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

    所以这应该可行。

    $paymentMethod = $user->addPaymentMethod($request->input('payment_method_id'));
    
    $paymentId = $paymentMethod->id  // use this for the charge
    

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2016-07-10
      • 2018-10-21
      • 1970-01-01
      • 2022-06-24
      • 2017-05-12
      • 2020-12-30
      • 2019-05-20
      • 2020-01-28
      相关资源
      最近更新 更多