Skip to content

Commit

Permalink
integrasi midtrans blum fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gigihridho committed Jan 9, 2021
1 parent e57a306 commit b7581ec
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

MIDTRANS_SERVER_KEY =""
MIDTRANS_CLIENT_KEY =""
MIDTRANS_IS_PRODUCTION =false
MIDTRANS_IS_SANITIZED =true
MIDTRANS_IS_3DS =true
86 changes: 86 additions & 0 deletions app/Http/Controllers/CheckoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Exception;

use Midtrans\Snap;
use Midtrans\Config;

use App\Cart;
use App\Transaction;
use App\TransactionDetail;
use App\User;
use Illuminate\Support\Facades\Auth;

class CheckoutController extends Controller
{
public function process(Request $request){
$user = Auth::user();
$user->update($request->except('total_price'));

//proses checkout
$code = 'STORE-' . mt_rand(00000,99999);
$carts = Cart::with(['product','user'])->where('users_id',Auth::user()->id)->get();

$transaction = Transaction::create([
'users_id' => Auth::user()->id,
'insurance_price' => 0,
'shipping_price' => 0,
'total_price' => $request->total_price,
'transaction_status' => 'PENDING',
'code' => $code
]);

foreach ($carts as $cart) {
$trx = 'TRX-' . mt_rand(00000,99999);

TransactionDetail::create([
'transactions_id' => $transaction->id,
'products_id' => $cart->product->id,
'price' => $cart->product->price,
'shipping_status' => 'PENDING',
'resi' => '',
'code' => $trx
]);
}
//konfig midtrans

Config::$serverKey = config('services.midtrans.serverKey');
Config::$isProduction = config('services.midtrans.isProduction');
Config::$isSanitized = config('services.midtrans.isSanitized');
Config::$is3ds = config('services.midtrans.is3ds');

//buat array to send midtrans
$midtrans = [
'transaction_detail' => [
'order_id' => $code,
'gross_amount' => (int) $request->total_price,
],
'customer_detail' => [
'first_name' => Auth::user()->name,
'email' => Auth::user()->email,
],
'enabled_payments' => [
'gopay', 'bni_va','bank_transfer'
],
'vtweb' => []
];
try {
// Get Snap Payment Page URL
$paymentUrl = Snap::createTransaction($midtrans)->redirect_url;

// Redirect to Snap Payment Page
return redirect($paymentUrl);
}
catch (Exception $e) {
echo $e->getMessage();
}
}

public function callback(Request $request){

}
}
8 changes: 7 additions & 1 deletion app/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

class Transaction extends Model
{
//
protected $fillable = [
'users_id', 'insurance_price', 'shipping_price', 'transaction_status', 'total_price', 'code'
];

protected $hidden = [

];
}
16 changes: 16 additions & 0 deletions app/TransactionDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TransactionDetail extends Model
{
protected $fillable = [
'transactions_id', 'products_id', 'price', 'shipping_status', 'resi', 'code'
];

protected $hidden = [

];
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"laravel/framework": "^7.29",
"laravel/tinker": "^2.5",
"laravel/ui": "2.4",
"midtrans/midtrans-php": "^2.3",
"sentry/sentry-laravel": "1.8.0",
"yajra/laravel-datatables-oracle": "~9.0"
},
Expand Down
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'midtrans' => [
'serverKey' => env('MIDTRANS_SERVER_KEY'),
'clientKey' => env('MIDTRANS_CLIENT_KEY'),
'isProduction' => env('MIDTRANS_IS_PRODUCTION'),
'isSanitized' => env('MIDTRANS_IS_SANITIZED'),
'is3ds' => env('MIDTRANS_IS_3DS'),
]

];
8 changes: 5 additions & 3 deletions resources/views/pages/cart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
<h2 class="mb-4">Shipping Details</h2>
</div>
</div>
<form action="" id="locations">
<form action="{{ route('checkout') }}" id="locations" enctype="multipart/form-data" method="POST">
@csrf
<input type="hidden" name="total_price" value="{{ $totalPrice }}">
<div class="row mb-2" data-aos="fade-up" data-aos-delay="200">
<div class="col-md-6">
<div class="form-group">
Expand Down Expand Up @@ -159,9 +161,9 @@
<div class="product-sutitle">Total</div>
</div>
<div class="col-8 col-md-3">
<a href="/success.html" class="btn btn-success mt-4 px-4 btn-block">
<button type="submit" class="btn btn-success mt-4 px-4 btn-block">
Checkout Now
</a>
</button>
</div>
</div>
</form>
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
Route::delete('/cart/{id}','CartController@delete')->name('cart-delete');
Route::get('/success','CartController@success')->name('success');

Route::post('/checkout','CheckoutController@process')->name('checkout');
Route::post('/checkout/callback','CheckoutController@callback')->name('midtrans-callback');

Route::get('/register/success','Auth\RegisterController@success')->name('register-success');

Route::get('/dashboard','DashboardController@index')->name('dashboard');
Expand Down

0 comments on commit b7581ec

Please sign in to comment.