Search code examples
laravelvue.jsstripe-paymentse-commercelaravel-cashier

stripe error: No such PaymentMethod: 'pm_xxx'


really struggling to find the error where i missed. I am using Laravel (v8), Vue (v2) and Stripe(v3) for my e-commerce web-app. I implemented stripe in TEST mode successfully and it was working perfectly fine. And when I switched for live mode I am getting the following error: No such PaymentMethod: 'pm_1Yyl5xC4bpPAffpGV2p0ZL12'.

Front and backend scripts are as shown below.

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)
    },

the method pay now method as follows:

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
                }



            }

        },

and the Laravel/Cashier backend as follows:

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);
        }

    }

Solution

  • “No such...” errors are usually caused by either a mismatch in API keys (e.g. using a mixture of your test plus live keys) or by trying to access objects that exist on a different account (e.g. trying to perform an operation from your platform account on an object that was created on a connected account)