Jumat, 22 Januari 2021

Laravel API Client dengan Guzzle


1. composer require guzzlehttp/guzzle


<?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use GuzzleHttp\Client;
    use App\Models\Branch;
    class ProductController extends Controller
    {
    public function index()
    {
    $branch = Branch::query();
    $branch->truncate();
    $url = 'https://mnfdev.agrobogautama.co.id/api/getbranch';
    $client = new Client();
    $headers = [
    'Content-Type' => 'application/json',
    'x-api-key' => 'xTYuaskl76510!#$%Zhyuulattr6!',
    ];
    $data = '{}';
    $options = [
    'body' => $data,
    'headers' => $headers,
    ];
    $response = $client->request('get', $url, $options);
    $res = json_decode($response->getBody());
    $data = [];
    $res = $res->result->data;
    foreach($res as $key => $item){
    $data[] = [
    "branch_id"=> $item->branch_id,
    "branch_name"=> $item->branch_name,
    "branch_latitude"=> $item->branch_latitude,
    "branch_longitude"=> $item->branch_longitude,
    "company_id"=> $item->company_id,
    "company_name"=> $item->company_name,
    "region_id"=> $item->region_id,
    "region_name"=> $item->region_name
    ];
    }
    $jd = count($res);
    $chunk = array_chunk($data,$jd);
    foreach($chunk as $d){
    Branch::insert($d);
    }
        }
    }


Senin, 18 Januari 2021

Laravel integrasi dengan midtrans

 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


Rabu, 13 Januari 2021

Request Ajax Secara Interval


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="data-id"></div>
<script>
function fetchdata(){
    $.ajax({
        url: "{{ route('user.index') }}",
        type: 'get',
        success: function(data){
            // eksekusi kode di sini
            // alert(data);
            $('#data-id').html(data) // ini contohnya
        },
        complete:function(data){
            setTimeout(fetchdata,100);
        }
    });
}

$(document).ready(function(){
    setTimeout(fetchdata,1000);
});
</script>

flutter firebase notification