Laravel integrasi dengan midtrans
1. composer require midtrans/midtrans-php
2. composer dump-autoload
3. php artisan make:controller MidtransController -r
edit di bagian function index() jadi seperti di bawah.
public function index()
{
// Set your Merchant Server Key
\Midtrans\Config::$serverKey = 'SB-Mid-server-feM6Qc7DQ4tLnsJO_IyhlN2P';
// Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
\Midtrans\Config::$isProduction = false;
// Set sanitization on (default)
\Midtrans\Config::$isSanitized = true;
// Set 3DS transaction for credit card to true
\Midtrans\Config::$is3ds = true;
$params = array(
'transaction_details' => array(
'order_id' => rand(),
'gross_amount' => 10000,
)
);
$snapToken = \Midtrans\Snap::getSnapToken($params);
return view('midtrans',compact('snapToken'));
}
4. buat file midtrans.blade.php di folder resource/view
isi dengan kode di bawah
<html>
<body>
<button id="pay-button">Pay!</button>
<pre><div id="result-json">JSON result will appear here after payment:<br></div></pre>
<!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
<script type="text/javascript">
document.getElementById('pay-button').onclick = function(){
// SnapToken acquired from previous step
snap.pay('<?=$snapToken?>', {
// Optional
onSuccess: function(result){
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
},
// Optional
onPending: function(result){
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
},
// Optional
onError: function(result){
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
}
});
};
</script>
</body>
</html>
5. setting route web.php
Route::resource('midtrans','MidtransController');
6. php artisan serve
7. akses localhost:8000
oke
Tidak ada komentar:
Posting Komentar