<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohammed El Yaakoubi</title>
    <description>The latest articles on Forem by Mohammed El Yaakoubi (@koubius).</description>
    <link>https://forem.com/koubius</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F827984%2F16d6a124-bfab-4cd8-8a1d-cc347eec9aec.png</url>
      <title>Forem: Mohammed El Yaakoubi</title>
      <link>https://forem.com/koubius</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/koubius"/>
    <language>en</language>
    <item>
      <title>Integrate Official Stripe Payment in your Laravel Application and Connect it to MySQL Database.</title>
      <dc:creator>Mohammed El Yaakoubi</dc:creator>
      <pubDate>Wed, 09 Mar 2022 14:42:20 +0000</pubDate>
      <link>https://forem.com/koubius/integrate-official-stripe-payment-in-your-laravel-application-and-connect-it-to-mysql-database-58f2</link>
      <guid>https://forem.com/koubius/integrate-official-stripe-payment-in-your-laravel-application-and-connect-it-to-mysql-database-58f2</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv77vjzs1afsamnqzv41y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv77vjzs1afsamnqzv41y.png" alt="Stripe-checkout"&gt;&lt;/a&gt;&lt;br&gt;
To integrate the official Stripe Payment Gateway in your Laravel application, you just have to follow these easy steps: &lt;/p&gt;
&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;

&lt;p&gt;Starting by making a Laravel installation , and if you've already settled it then you can follow the next steps.&lt;br&gt;
&lt;code&gt;composer create-project laravel/laravel stripe-checkout&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  checkout.blade.php
&lt;/h1&gt;

&lt;p&gt;In this step , you just have to set a button in your blade template  in order redirect to stripe checkout page .&lt;br&gt;
In this I will use this example :&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;button id="checkout-button" type="button"&amp;gt;Proceed to Checkout&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing Stripe-Php library via Composer
&lt;/h1&gt;

&lt;p&gt;Following it with installing the &lt;strong&gt;stripe-php&lt;/strong&gt; library via &lt;strong&gt;composer&lt;/strong&gt; :&lt;br&gt;
&lt;code&gt;composer require stripe/stripe-php&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Creating the Checkout Session
&lt;/h1&gt;

&lt;p&gt;In the checkout blade template that we had settled ,it is required to create the checkout session in it :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
require 'vendor/autoload.php';
// This is your test secret API key.
\Stripe\Stripe::setApiKey('sk_test_51JX1BnCwSqIoxRpKte18TcaGSEegx1UX1NKYaKGDCwFkbyDWeYiQW4cKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$session = \Stripe\Checkout\Session::create([
    'line_items' =&amp;gt; [[
      'price_data' =&amp;gt; [
        'currency' =&amp;gt; 'usd',
        'product_data' =&amp;gt; [
          'name' =&amp;gt; 'T-shirt',
        ],
        'unit_amount' =&amp;gt; 2000,
      ],
      'quantity' =&amp;gt; 1,
    ]],
    'mode' =&amp;gt; 'payment',
    'success_url' =&amp;gt; 'https://example.com/success',
    'cancel_url' =&amp;gt; 'https://example.com/cancel',
  ]);
?&amp;gt;
&amp;lt;script src="https://js.stripe.com/v3/"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
const stripe = Stripe('pk_test....') //Your Publishable key.
const btn = document.getElementById('checkout-button');
btn.addEventListener("click", function() 
{
stripe.redirectToCheckout({
$sessionId: "&amp;lt;?php echo $session-&amp;gt;id ?&amp;gt; "
})
}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your application.&lt;br&gt;
&lt;em&gt;In case you had the &lt;code&gt;require(vendor/autoload.php)&lt;/code&gt; error :&lt;/em&gt;&lt;br&gt;
Instead of &lt;code&gt;&amp;lt;?php require 'vendor/autoload.php';&lt;/code&gt;&lt;br&gt;
Add this&lt;br&gt;
&lt;code&gt;&amp;lt;?php require_once __DIR__. '/../../../vendor/autoload.php';&lt;/code&gt;&lt;br&gt;
Now run your application !&lt;/p&gt;
&lt;h1&gt;
  
  
  Connecting MySQL Database and Stripe Checkout details
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;In your &lt;strong&gt;VerifyCsrfToken.php&lt;/strong&gt; add this following line:
&lt;code&gt;protected $except = [ 'webhook'] ;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Now install expose via composer:(&lt;a href="https://expose.dev/docs/getting-started/installation" rel="noopener noreferrer"&gt;https://expose.dev/docs/getting-started/installation&lt;/a&gt;)
&lt;code&gt;composer global  require beyondcode/expose&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Activate your tokken :
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;expose tokken xxxxxxxxxxx //Create an account in expose.dev and you will get the tokken&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;expose share http://localhost&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You will get a global link for your laravel application only temporary. The reason for this, is to link webhook events to an existing website.
&lt;em&gt;Copy your laravel application exposed link
*Visit stripe dashboard(make sure you are in the **Test Mode&lt;/em&gt;&lt;em&gt;),Then clicking on Webhooks, in the **Endpoint URL&lt;/em&gt;* add your website link followed by &lt;em&gt;/webhook&lt;/em&gt;.Then select your events(charge.succeeded to see if the payment was successful)&lt;/li&gt;
&lt;li&gt;In your web.php:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::post('webhook/payment/succeeded', [App\Http\Controllers\StripePaymentController::class, 'stripePost']) ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In StripePaymentController.php
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Payment;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Stripe;

class StripePaymentController extends Controller
{
    public function stripePost(Request $request)
    {
        if($request-&amp;gt;type === "charge.succeeded"){
            try{

    Payment::create([
       'stripe_id' =&amp;gt; $request-&amp;gt;data['object']['id'],
        'amount' =&amp;gt; $request-&amp;gt;data['object']['amount'],
        'email' =&amp;gt; $request-&amp;gt;data['object']['billing_details']['email'],
        'name' =&amp;gt; $request-&amp;gt;data['object']['billing_details']['name']
    ]);
   } catch (\Exception $e) {
               return $e-&amp;gt;getMessage();
           }

        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In Payment.php Model: 
&lt;code&gt;protected $guarded = [] ;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In create-payments.php migration :
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('stripe_id'); // stripe payment id
            $table-&amp;gt;integer('amount');
            $table-&amp;gt;string('email');
            $table-&amp;gt;string('name');
            $table-&amp;gt;timestamps();
        });
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then Migrate the payments table .Then try the checkout and see the details after a successful checkout.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Final words
&lt;/h1&gt;

&lt;p&gt;This was an easy tutorial , to integrate stripe payment gateway in your laravel application.&lt;br&gt;
For more details watch my Youtube video to get the full view of the integration:&lt;a href="https://www.youtube.com/watch?v=c1YgWrgsSQk&amp;amp;ab_channel=Mithrandir" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=c1YgWrgsSQk&amp;amp;ab_channel=Mithrandir&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>stripe</category>
      <category>php</category>
    </item>
  </channel>
</rss>
