Develop an E-Commerce Website With Laravel 5.4 - Part 9

Published Sep 20, 2017Last updated Dec 11, 2017
Develop an E-Commerce Website With Laravel 5.4 - Part 9

All posts in this series:

In this post we are going to last stage that is checkout process. The Cart Items will be added in orders and order_items table and use Stripe for Payment Processing.

After creating migrations for Orderand OrderItem models I am going to install Stripe package

Installation of Stripe

composer require stripe/stripe-php on Console and it will install Stripe PHP SDK.

I am going to add the checkout form below the cart html cart/show.blade.php

In OrderController I created a method checkout that is responsible for both payment and adding order data into the tables. The function looks like:

public function checkout( Request $request ) {

    $session_id   = session()->get( '_token' );
    $order_number = substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyz" ), 0,
        4 ) . '-' . substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyz" ), 0,
        4 ) . '-' . substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyz" ), 0, 4 );
    $order_number = strtoupper( $order_number );

    $entries = Cart::where( [ 'session_id' => $session_id ] )->get();
    Stripe::setApiKey( config( 'services.stripe.secret' ) );
    $total = 0;
    foreach ( $entries as $entry ) {
      $total += floatval( $entry['price'] );
    }
    $gross_amount = intval( $total * 100 ); // to convert into cents;
    $charge       = Charge::create( [
      "amount"      => $gross_amount,
      "currency"    => "usd",
      'source'      => $request->get( 'stripeToken' ),
      "description" => 'For Order No:-' . $order_number
    ] );

    //Successful Transaction Id
    $charge_id = $charge->id;
    //Proceed Db handlings
    if ( $charge_id != null ) {

      DB::beginTransaction();
      try {
        $order = Order::create( [
          'order_identity'        => $order_number,
          'session_id'            => $session_id,
          'stripe_transaction_id' => $charge_id,
          'gross_price'           => $gross_amount,
          'status'                => 1
        ] );

        foreach ( $entries as $entry ) {
          OrderItem::create( [
            'order_identity' => $order_number,
            'order_id'       => $order->id,
            'product_id'     => $entry->product_id,
            'quantity'       => $entry->qty,
            'price'          => $entry->price,
          ] );
        }
        //Reset Cart after order submission
        //Cart::where( 'user_id', $loggedInUserId )->delete();
        DB::commit();

      } catch ( Exception  $ex ) {
        DB::rollBack();
        print_r( $ex->getMessage() );
      }

      return redirect( '/' );
    }

I am create a unique order_number. Using Stripe you must have Key and Secret. Once you are registered you can go to Stripe Dashboard and can have your keys. I set those keys in .env file and then call them(Visit Stripe Dashboard for further details)

Calling Stripe::Charge() method to make a charge. In case of successful transaction I am storing chargeId into the table for future reference and then redirecting it. I am also using Transaction to make sure both orders and order_items have correct data stored.

That's it from Laravel 5.4 series. The code has been pushed to Github

Liked my articles, want me to write more on different topics? You can send me token of appreciation by donating amount on below Crypto coins addresses. Thanks

BTC = 12stJs8vZNuuVfjZSSzpLPA96quNissk1b
ETH = 0x23f016d7a8e408e5551ae7aa51b3fe1534165463

Discover and read more posts from Adnan Siddiqi
get started
Enjoy this post?

Leave a like and comment for Adnan

2