Skip to content
Advertisement

Embedding Checkout on Laravel Blade

I’m trying to display an embedded checkout form from Stripe (https://stripe.com/docs/checkout). I notice that if I place the embedded code between @section('content') and @endsection, it will not display the form when I click the Pay with card button. However, if I remove @section('content') or place the embedded code outside of it, then it will display the form perfectly, however, this will break the structure of my layout.

index.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Billing</div>

                <div class="panel-body">
                <h1>Stripe</h1>

                //EMBEDDED CODE STARTS HERE
                <form action="/your-server-side-code" method="POST">
                    <script
                        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                        data-key="pk_test_sTxf2K6z6oemUXlC0NRcowxO"
                        data-amount="2000"
                        data-name="Demo Site"
                        data-description="2 widgets"
                        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                        data-locale="auto"
                        data-zip-code="true"
                        data-currency="gbp">
                    </script>
                </form>
                //EMBEDDED CODE ENDS HERE


                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Thanks!

EDITED: layout.app

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">

                    <!-- Collapsed Hamburger -->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <!-- Branding Image -->
                    <a class="navbar-brand" href="{{ url('/') }}">
                        {{ config('app.name', 'Laravel') }}
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <!-- Left Side Of Navbar -->
                    <ul class="nav navbar-nav">
                        &nbsp;
                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="nav navbar-nav navbar-right">
                        <!-- Authentication Links -->
                        @guest
                            <li><a href="{{ route('login') }}">Login</a></li>
                            <li><a href="{{ route('register') }}">Register</a></li>
                        @else
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>

                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <a href="{{ route('logout') }}"
                                            onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                            Logout
                                        </a>
                                        <a href="{{ route('home') }}">Home</a>

                                        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                            {{ csrf_field() }}
                                        </form>
                                    </li>
                                </ul>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>

        @yield('content')
    </div>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

Advertisement

Answer

So I figured out what’s the problem, it’s because the <script> tag conflict with Vue components.

So there are two solutions:

1) Create a duplicate layout without the Vue element identifier <div id="app"></div>

or

2) Create a script tag using the mount() hook in Vue component.

createStripeScript () {
        const script = document.createElement('script')
        script.src = 'https://checkout.stripe.com/checkout.js'
        script.onload = () => this.initStripe()
        document.body.appendChild(script)
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement