199 lines
6.6 KiB
PHP
Executable File
199 lines
6.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\PaymayaTokens;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\PaymayaTokenResource;
|
|
use App\Contracts\LcardActionLogsInterface;
|
|
use App\Helpers\HttpStatusCode;
|
|
use App\Http\Requests\PaymayaTokenFormValidation;
|
|
use App\Libraries\UuidHelper;
|
|
use Ramsey\Uuid\Uuid;
|
|
use App\Libraries\RNRPHLibrary;
|
|
|
|
|
|
class PaymayaTokensController extends Controller
|
|
{
|
|
|
|
public function __construct(
|
|
HttpStatusCode $httpStatusCode,
|
|
LcardActionLogsInterface $lcard_logs)
|
|
{
|
|
$this->format = $httpStatusCode;
|
|
$this->lcard_logs = $lcard_logs;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
try {
|
|
$list = PaymayaTokens::all();
|
|
// $this->lcard_logs->log(0,'PAYMAYATOKEN','VIEW','Member view all paymaya tokens');
|
|
return $this->format->mobile_success("Success", $list);
|
|
|
|
} catch (\Throwable $th) {
|
|
return $this->format->mobile_error('No Paymaya tokens found');
|
|
}
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(PaymayaTokenFormValidation $request)
|
|
{
|
|
try {
|
|
$data = PaymayaTokens::where('token',$request->get('token'))->where('is_active','<>',0)->get();
|
|
if(count($data) > 0){
|
|
return $this->format->mobile_error('Token already exist for '.$request->get('card_number'));
|
|
}
|
|
else
|
|
{
|
|
$uuid = new UuidHelper;
|
|
$paymayatoken = new PaymayaTokens();
|
|
$paymayatoken->card_number = $request->get('card_number');
|
|
$paymayatoken->customer_id = $request->get('customer_id');
|
|
$paymayatoken->token = $request->get('token');
|
|
$paymayatoken->is_active = 1;
|
|
$paymayatoken->paymaya_token_uuid = $uuid->generate_uuid1();
|
|
if(!$paymayatoken->save()) return $this->format->mobile_error('Failed to saved token.');
|
|
// $this->lcard_logs->log(0,'PAYMAYATOKEN','STORE','Member store paymaya token');
|
|
return $this->format->mobile_success('Paymaya token has been created', $paymayatoken);
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
|
return $this->format->mobile_error('Error saving');
|
|
}
|
|
}
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\PaymayaTokens $paymayaTokens
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($card_number)
|
|
{
|
|
try {
|
|
$data = PaymayaTokens::where('card_number',$card_number)->where('is_active','1')->get();
|
|
|
|
if(count($data) > 0){
|
|
|
|
$rnrph = new RNRPHLibrary();
|
|
|
|
// $this->lcard_logs->log(0,'PAYMAYATOKEN','VIEW','Member view paymaya token');
|
|
$tokenData = [];
|
|
$cards = [];
|
|
|
|
$rnrph->getPaymayaCustomerDetails($data[0]['customer_id']);
|
|
$cardsData = $rnrph->response['data'];
|
|
foreach($cardsData as $key => $cardDetail){
|
|
$c = get_object_vars($cardDetail);
|
|
$c["uuid"] = $data[0]["paymaya_token_uuid"];
|
|
$c["customer_id"] = $data[0]["customer_id"];
|
|
$cards[] = $c;
|
|
}
|
|
|
|
//SYNC ACCOUNT CARDS
|
|
$synced = [];
|
|
$todelete = [];
|
|
foreach($cards as $key => $paymayacards){
|
|
foreach($data as $key => $savedcards){
|
|
if($paymayacards["cardTokenId"] == $savedcards["token"]){
|
|
$c = $paymayacards;
|
|
$c["uuid"] = $savedcards["paymaya_token_uuid"];
|
|
$c["customer_id"] = $savedcards["customer_id"];
|
|
$synced[] = $c;
|
|
}else{
|
|
//IF SAVED TOKEN FROM THE DATABASE DOESNT HAVE A MATCH TO PAYMAYA API, DELETE
|
|
$todelete[] = $savedcards;
|
|
}
|
|
}
|
|
}
|
|
|
|
$res = [];
|
|
$res["customer_id"] = $data[0]["customer_id"];
|
|
$res["card_number"] = $data[0]["card_number"];
|
|
$res["is_active"] = $data[0]["is_active"];
|
|
$res["created_at"] = get_object_vars($data[0]["created_at"])["date"];
|
|
$res["updated_at"] = get_object_vars($data[0]["updated_at"])["date"];
|
|
// $res["cards"] = $cards;
|
|
// $res["full"] = $data;
|
|
// $res["to_delete"] = $todelete;
|
|
$res["cards"] = $synced;
|
|
|
|
// return $this->format->mobile_success('Success', $res);
|
|
return $this->format->mobile_success('Success', $res);
|
|
}
|
|
else
|
|
{
|
|
return $this->format->mobile_error('Paymaya token not found', []);
|
|
}
|
|
} catch (\Throwable $th) {
|
|
return $this->format->mobile_error('Error');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\PaymayaTokens $paymayaTokens
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(PaymayaTokens $paymayaTokens)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\PaymayaTokens $paymayaTokens
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, PaymayaTokens $paymayaTokens)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\PaymayaTokens $paymayaTokens
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($paymaya_token_uuid)
|
|
{
|
|
try {
|
|
PaymayaTokens::where('paymaya_token_uuid',$paymaya_token_uuid)
|
|
->update([
|
|
'is_active' => 0
|
|
]);
|
|
return $this->format->mobile_success('Success');
|
|
} catch (\Throwable $th) {
|
|
return $this->format->mobile_error('Error',$th);
|
|
|
|
}
|
|
//
|
|
}
|
|
}
|