ethar's avatar
Level 5

laravel stripe integration

i use cashier package to integrate laravel with stripe payament, using jquery, firstyly i create intent

public function payment(Request $request)
{
    $course = $this->checkStudentCourse($request->id);
    $paymentIntent = auth()->user()->createSetupIntent();
    // Attach the course to the student using the pivot table
    return view('Front.pages.auth.payment', compact('course','paymentIntent'));
}

in blade.php i using jquery

<script src="https://js.stripe.com/v3/"></script>
<script>
    var stripe = Stripe("{{ config('services.stripe.publishable_key') }}");
    var elements = stripe.elements();
    var cardElement = elements.create('card', {
        style: {
            base: {
                iconColor: '#c4f0ff',
                color: '#fff',
                fontWeight: '500',
                fontFamily: 'Roboto, Open Sans, Segoe UI, sans-serif',
                fontSize: '16px',
                fontSmoothing: 'antialiased',
                ':-webkit-autofill': {
                    color: '#fce883',
                },
                '::placeholder': {
                    color: '#87BBFD',
                },
            },
            invalid: {
                iconColor: '#FFC7EE',
                color: '#FFC7EE',
            },
        },
    });
    cardElement.mount('#card-element');
    $("#pay_button").on('click', function () {
        $("#pay_button").attr('disabled', true);
        stripe
            .confirmCardSetup('{{ $paymentIntent->client_secret }}', {
                payment_method: {
                    card: cardElement,
                    billing_details: {
                        name: "{{ auth()->user()->name }}",
                    },
                },
            })
            .then(function(result) {
               if(result.error){
                   $("#payment_error").val(result.error.message).removeClass('d-none');
                   $("#pay_button").attr('disabled', false);
               }else{
                   $("#payment_method").val(result.setupIntent.payment_method);
                   $("#pay_form").submit();
               }
            });
    })
</script>

then in pay function

    public function pay(Request $request)
    {
        $course = $this->checkStudentCourse($request->course_id);
        $user=auth()->user();
        $paymentMethod = $request->input('payment_method');
        try{
            $user->createOrGetStripeCustomer();
            $user->updateDefaultPaymentMethod($paymentMethod);
            $user->charge($course->price * 100, $paymentMethod);
            $user->attach($course->id);

        }catch(\Exception $ex){
            dd($ex->getMessage());
            return back()->with('error',$ex->getMessage());
        }
        return redirect(route('student.courses'));
    }

but i got this error "A return_url must be specified because this Payment Intent is configured to automatically accept the payment methods enabled in the Dashboard, some of which may require a full page redirect to succeed. If you do not want to accept redirect-based payment methods, set automatic_payment_methods[enabled] to true and automatic_payment_methods[allow_redirects] to never when creating Setup Intents and Payment Intents.

0 likes
5 replies
jaseofspades88's avatar

It's hardly a cryptic error message, it's telling you what to do even. This will be well documented in Stripe API documentation, for sure. Have you searched the documentation for this? Stripe is very well documented.

1 like
martinbean's avatar

@jaseofspades88 This guy pops in every now and again and spams the forum with low-quality and easily-answerable questions like this as if it’s their own personal ChatGPT client.

1 like
lesniergonzalez's avatar

Hi, i was the same problem.

The solution is add option param with the 'return_url' attribute, to the charge function in the server..

$options = [ 'return_url' => 'your_success_url'];
 $user->charge($course->price * 100, $paymentMethod, $options  );
1 like

Please or to participate in this conversation.