Files
lelong/app/Http/Controllers/ApiController.php
T
Azfar Arsyad 0f66b280a8 commit
2022-01-25 15:04:23 +08:00

289 lines
7.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use PDF;
use App\Models\Cawangan;
use App\Models\JenisEmas;
use App\Models\JenisKarat;
use App\Models\Simulasi;
use App\Models\SimulasiBid;
use App\Models\SimulasiBidMarhun;
use App\Models\SimKarat;
use App\Models\SimKarat_temp;
use App\Models\CawanganSimulasi;
use App\Models\Lelong;
use App\Models\CawanganLelong;
use App\Models\LelongBid;
use App\Models\LelongBidMarhun;
use App\Models\LelKarat;
use App\Models\LelKarat_temp;
use Session;
use Carbon\Carbon;
use Money\Money;
use Billplz\Client;
use Http\Client\Common\HttpMethodsClient;
use Http\Adapter\Guzzle7\Client as GuzzleHttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
class ApiController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function __construct(){
parent::__construct();
if(config('app.env') != 'production'){
$this->api_key = 'abecfd0c-98d8-4635-abc2-2fcba8c60c11';
$this->XSignature_Key = 'S-GWr7z03XjAhqPsFfu_H5nQ';
$this->collection_id = 'bwdtejv0';
}else{
$this->api_key = 'f3e82fe1-a422-4660-97f0-65cc59ff6c47';
$this->XSignature_Key = 'S-alZUbzmD2_TF3vmrRqgehQ';
}
}
public function biditem()
{
$currentdate = Carbon::now();
$currentdate = $currentdate->toDateString();
// dd($currentdate->toDateString());
$lelong = Lelong::where('tarikh_lelong','=',$currentdate)->get();
$arrayID = [];
foreach($lelong as $data)
{
array_push($arrayID,$data->id);
}
$lelongbid = LelongBid::whereIn('lelong_id',$arrayID)
->join('lelongbidmarhun','lelongbid.id','=','lelongbidmarhun.lelongbid_id')
->get();
$surat = LelongBid::whereIn('lelong_id',$arrayID)->get();
if(sizeof($lelongbid)!=0)
{
return response()->json([
'success' => true,
'lelong' => $lelong,
'surat' => $surat,
'item' => $lelongbid
]);
}
else
{
return response()->json([
'success' => false,
'message' => 'No Data',
], 401);
}
}
public function karat()
{
$jenisemas = JenisEmas::all();
$jeniskarat = JenisKarat::all();
if(sizeof($jenisemas)!=0 && sizeof($jeniskarat)!=0 )
{
return response()->json([
'success' => true,
'karat' => $jeniskarat,
'emas' => $jenisemas
]);
}
else
{
return response()->json([
'success' => false,
'message' => 'No Data',
], 401);
}
}
public function feepayment(Request $request)
{
$arraycollection_id = array();
$http = new HttpMethodsClient(
new GuzzleHttpClient(),
new GuzzleMessageFactory()
);
$billplz = new Client($http, $this->api_key);
if(config('app.env') != 'production'){
$billplz->useSandbox();
}
$billplz->useVersion('v4');
$bill = $billplz->bill();
$current = Carbon::now();
$current = new Carbon();
$day = $current->day;
$month = $current->month;
$year = $current->format('y');
$total_bayaran = 10;
$response = $bill->create(
$collection_id,
// $request->email,
$request->notel,
$request->name,
Money::MYR($total_bayaran),
route('pay.callback'),
'Bayaran Ansuran',
['redirect_url' => route('pay.paymentStatus')]
);
$billcode = $response->toArray();
$reference_no = 'LPKB' . $day . $month . $year . sprintf("%08d", $nosiri);
$resit = new Resit;
$resit->user_id = $request->user_id;
$resit->lelongbid_id = null;
$resit->reference_no = $reference_no;
$resit->billplz_id = $billcode['id'];
$resit->resit_type = 1;
$resit->total_payment = $total_bayaran;
$resit->save();
// foreach($array_payment as $index1=>$arraypay)
// {
// $previous_siri = Http::get(config('api.url') . '/api/online/transaction')->json();
// if($previous_siri){
// $nosiri = $previous_siri['nosiri'] + 1;
// }else{
// $nosiri = 1;
// }
// $reference_no = 'LPKB' . $day . $month . $year . sprintf("%08d", $nosiri);
// $new_transaction = Http::post(config('api.url') . '/api/online/transaction',[
// 'nokp'=> Auth::user()->icno,
// 'reference_no'=> $reference_no,
// 'nosiri'=> $nosiri,
// 'billplz_id' => $billcode['id'],
// 'norujukan' => $arraypay['norujukan'],
// 'kodcaw' => $arraypay['kodcaw'],
// 'total_payment'=> $arraypay['bayaran'],
// 'customer_name' => Auth::user()->name,
// ])->json();
// }
return redirect($billcode['url'] . '?auto_submit=true');
// return view('pembayaran.result.berjaya',compact('arraybayaran'));
}
public function callback(Request $request){
$http = new HttpMethodsClient(
new GuzzleHttpClient(),
new GuzzleMessageFactory()
);
$billplz = new Client($http, $this->api_key);
if(config('app.env') != 'production'){
$billplz->useSandbox();
}
$billplz->useVersion('v4');
$bill = $billplz->bill();
$data = $bill->webhook($request->toArray());
if($data['billplz']['paid'] == 'true'){
$resit = Resit::where('billplz_id','=',$data['billplz']['id'])->first();
$resit->status = 'PAID';
$resit->paid_at = $data['billplz']['paid_at'];
$resit->save();
// $transactionOnline = Http::get(config('api.url') . '/api/transaction/online/express',[
// 'billplz_id' => $data['billplz']['id']
// ])->json();
return response()->json([
'success' => true,
'resit' => $resit
]);
}else{
$resit = Resit::where('billplz_id','=',$data['billplz']['id'])->first();
$resit->status = 'UNSUCCESSFUL';
$resit->paid_at = null;
$resit->save();
return response()->json([
'success' => false,
'resit' => $resit
]);
}
}
public function response(Request $request){
$http = new HttpMethodsClient(
new GuzzleHttpClient(),
new GuzzleMessageFactory()
);
$billplz = new Client($http, $this->api_key);
if(config('app.env') != 'production'){
$billplz->useSandbox();
}
$billplz->useVersion('v4');
$bill = $billplz->bill();
$data = $bill->webhook($request->toArray());
if($data['billplz']['paid'] == 'true'){
Http::put(config('api.url') . '/api/online/transaction',[
'billplz_id' => $data['billplz']['id'],
'status' => 'PAID',
'paid_at' => $data['billplz']['paid_at']
]);
$transactionOnline = Http::get(config('api.url') . '/api/transaction/online/login',[
'billplz_id' => $data['billplz']['id']
])->json();
return redirect()->route('pay.success',[
'transactionOnline' => $transactionOnline
]);
}else{
Http::put(config('api.url') . '/api/online/transaction',[
'billplz_id' => $data['billplz']['id'],
'status' => 'UNSUCCESSFUL',
'paid_at' => null
]);
return redirect()->route('pay.failed');
}
}
}