Home > Software engineering >  How to prevent stripe from creating new user when receiving a payment?
How to prevent stripe from creating new user when receiving a payment?

Time:11-28

I am starting with stripe and still in test mode. I use Laravel 9 cashier and vuejs I also installed vue-stripe and created a view to test the checkout functionality. Here is my vue

<template>
  <div >
    <h1 >Stripe Payment Gateway integration</h1>
    <div  v-if="payId">

      <stripe-checkout 
      ref="checkoutRef" 
      :pk="publishableKey" 
      :sessionId="payId"
      :customerEmail="customerEmail" />
      <button  @click="submit">Pay Now!</button>
    </div>
  {{customerEmail}}

    <div v-if="subscribeId" >
      <stripe-checkout 
      ref="subRef" 
      :pk="publishableKey" 
      :sessionId="subscribeId"
      :customerEmail="customerEmail" />
      <button  @click="submitSub">Subscribe Now!</button>

    </div>

  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import { StripeCheckout , Stripe} from '@vue-stripe/vue-stripe';
import axios from "../axios"
import store from "../store"

export default {
  components: {
    StripeCheckout,
  },
  data() {
    return {
      publishableKey: 'pk_test_51M6ZtzI....................XgAyUVjlwG6MFos0AaqaQYJOf2YC3a6oWlZqMjFtTZj00Tue51qVs',
      payId: null,
      subscribeId: null,
      customerEmail: store.state.user.email
    }
  },
  methods: {
    getSession() {
      axios.get('api/getSession')
        .then(res => {
          console.log(res);
          this.payId = res.data.oneTime.id;
          this.subscribeId = res.data.sub.id;
        })
        .catch(err => {
          console.log(err);
        })
    },

    submit() {
      this.$refs.checkoutRef.redirectToCheckout();
    },
    submitSub() {
      this.$refs.subRef.redirectToCheckout();
    }
  },
  mounted() {
    this.getSession();
  
  }

}
</script>

and here is the StripeController to return the sessionId in the backend

<?php

namespace App\Http\Controllers;


use Stripe\StripeClient;
use Illuminate\Http\Request;
use Laravel\Cashier\Cashier;

class StripeController extends Controller
{
    public function getSession()
    {
        $stripe = new StripeClient(env('STRIPE_SECRET'));
        $user=auth()->user();
    
        $stripeCustomer = $user->createOrGetStripeCustomer(
            ['name'=>$user->name,'email'=>$user->email]); 

        $checkout = $stripe->checkout->sessions->create(
            [ 
                'success_url' => 'https://yahoo.com',
                'cancel_url' => 'https://google.com',
                'line_items' =>
                [
                    [
                        'price_data' =>
                        [
                            'currency' => 'eur',
                            'unit_amount' => 500,
                            'product_data' =>
                            [
                                'name' => 'Example Stripe Checkout'
                            ],
                        ],
                        'quantity' => 1,

                    ],
                ],

                'mode' => 'payment',
            ]
        );

        $sub = $stripe->checkout->sessions->create(
            [
                'success_url' => 'https://denentzat.fr',
                'cancel_url' => 'https://google.com',
                'line_items' =>
                [
                    [

                        'price'=>'price_1M84UNIWDjpHNQK1FXOj1k01',
                        'quantity' => 1,

                    ],
                ],

                'mode' => 'subscription',
            ]
        );
        return  ['oneTime'=>$checkout, 'sub'=>$sub];
    }

    public function webhook(Request $request){
        \Log::info($request->data);
        
        return response()->json(['status'=>'success']);
    }
}

The payment is done in both cases (pay or subscribe). Nevertheless, when I go in the customer tab of the dashboard, I can see that sometimes (I could not find for what reason) stripe create a guest user, or a normal user and eventually, there may be several users with the same email. How to prevent this? I was hoping that in passing the user email to the form (Stripe-checkout component) this will prefill the email field but it doesn't happen. Thank you for help.

CodePudding user response:

You can pre-create a Customer with their email, then passing in the Customer Id when creating the Checkout Session. (see Existing Customer on Stripe Doc).

  • Related