Building e-commerce application with Laravel 5.8 and Billplz payment.

laravel new ecommerce
cd ecommercephp artisan make:auth && php artisan make:model Product -mrc && php artisan make:model Purchase -mrc
Change it to your setting accordingly.
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('pic')->nullable();
$table->string('desc')->nullable();
$table->string('price')->nullable();
$table->string('payment_link')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
public function up()
{
Schema::create('purchases', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('bill_id');
$table->timestamps();
});
Schema::table('purchases', function (Blueprint $table) {
$table->biginteger('user_id')->unsigned();
$table->biginteger('product_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
});
}
public function down()
{
Schema::table('purchases', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
$table->dropForeign(['product_id']);
$table->dropColumn('product_id');
});
Schema::dropIfExists('purchases');
}
php artisan migrate
Your database is ready, should be able to view it using any tools like phpmyadmin/sequelpro etc.
  • Product.php
  • Purchase.php
  • User.php
class Product extends Model
{
//
protected $fillable = [
'name','pic','desc','price','payment_link',
];
public function purchases()
{
return $this->hasMany('App\Purchase');
}
}
class Purchase extends Model
{
//
protected $fillable = [
'bill_id','user_id','product_id',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function product()
{
return $this->belongsTo('App\Product');
}
}
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function purchases()
{
return $this->hasMany('App\Purchase');
}
}
php artisan key:generate && php artisan serve
standard Laravel application build up
  • Normal Bills
  • Open Bills
  • Mass Bills

“The best part about Laravel being open source is the community become bigger and larger. Thus there are so many out there developing tools and library to help power up the frameworks. I am sure that each payment services out there would already have many supporting Laravel package for it, if not, at least one must be there. “

composer require neonexxa/billplz-wrapper
As written in the package guide, edit your .env appropriately
Neonexxa\BillplzWrapperV3\BillplzServiceProvider::class,
composer dump-autoload && php artisan config:cache && php artisan cache:clear && php artisan vendor:publish --provider="Neonexxa\BillplzWrapperV3\BillplzServiceProvider"
php artisan make:seeder ProductTableSeeder
use App\Product;
public function run()
{
//
$products = [
["name"=>"Apple","pic"=>"https://www.kofixlabs.co/logo.png","desc"=>"Our Apple","price"=>"1","payment_link"=>"rtvgkhf3"],
];
Product::insert($products);
}
$this->call(ProductTableSeeder::class);
php artisan db:seed
public function index()
{
$products = \App\Product::all();
return view(‘home’,compact(‘products’));
}
@foreach($products as $product)
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{$product->pic}}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{$product->name}}</h5>
<p class="card-text">{{$product->desc}}</p>
<a class="btn btn-primary" href="#"
onclick="event.preventDefault();
document.getElementById('newbill-form{{$product->id}}').submit();">
Buy RM {{$product->price}}
</a>
<form id="newbill-form{{$product->id}}" action="{{ route('purchase.store') }}" method="POST" style="display: none;">
@csrf
<input type="hidden" name="product" value="{{$product->id}}">
</form>
</div>
</div>
@endforeach
use Neonexxa\BillplzWrapperV3\BillplzBill;
use App\Product;
use Auth;
        //
$params = $request->all();
$product = Purchase::find($params['product']);
// from the guide
$res0 = new BillplzBill;
$res0->collection_id = $product->payment_link;
$res0->description = "New BIll";
$res0->email = Auth::user()->email;
$res0->name = Auth::user()->name;
$res0->amount = $product->price*100;
$res0->callback_url = "yourwebsite@example.com";
// and other optional params
$res0 = $res0->create_bill();
list($rhead ,$rbody, $rurl) = explode("\n\r\n", $res0);
$bplz_result = json_decode($rurl);

// Store the bill into our purchases
$purchase = new Purchase;
$purchase->user_id = Auth::user()->id;
$purchase->product_id = $product->id;
$purchase->bill_id = $bplz_result->id;
$purchase->save();
return redirect($bplz_result->url);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store