Files
erahn-click/app/Services/BillplzService.php
T
Afiq Hamzah c48ffc22c8 Throw exception in catch block to ensure that the code execution / flowed stops
- This to prevent broken data to be entered into tables
2024-08-01 14:50:51 +08:00

189 lines
6.7 KiB
PHP

<?php
namespace App\Services;
use App\Models\TransactionOnlineStatus;
use Billplz\Client;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
class BillplzService
{
public $client;
public $config;
public $callbackUrl;
public $paymentTitle;
public $redirectUrl;
public function __construct()
{
$appEnvironment = config('app.env');
$billplzConfig = $appEnvironment == 'production' ? config('services.billplz.production') : config('services.billplz.sandbox');
$client = Client::make($billplzConfig['api_key'], $billplzConfig['x_signature']);
$client->useVersion('v4');
if ($appEnvironment != 'production') {
$client->useSandbox();
}
$this->callbackUrl = route('pay.callback');
$this->redirectUrl = ['redirect_url' => route('pay.paymentStatus')];
$this->paymentTitle = "Pembayaran Ansuran";
$this->config = $billplzConfig;
$this->client = $client;
}
public function getCollections()
{
return $this->client->collection()->all()->toArray();
}
public function createBill(array $data)
{
$composed_data = [
$this->config['collection_id'],
...$data,
$this->callbackUrl,
$this->paymentTitle,
$this->redirectUrl,
];
try{
$response = $this->client->bill()->create(...$composed_data)->toArray();
OnlineTransactionLoggerService::logs($response['id'], $composed_data, $response, TransactionOnlineStatus::BILL_CREATED);
return $response;
} catch(\Exception $e){
if($e->getCode() === 404){
$errorMessage = 'Please check your billplz payment gateway collection. Ensure that api_key, x_signature, and collection_id are correctly entered';
OnlineTransactionLoggerService::logs( null, $composed_data, $errorMessage, TransactionOnlineStatus::BILL_CREATED);
throw new \ErrorException($errorMessage);
} else {
OnlineTransactionLoggerService::logs( null, $composed_data, $e->getMessage(), TransactionOnlineStatus::BILL_CREATED);
};
throw $e;
}
}
public function redirect($request)
{
try{
$response = $this->client->bill()->redirect($request);
OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $response, TransactionOnlineStatus::UPDATE_TRANSACTION_REDIRECT);
return $response;
} catch(\Exception $e){
OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $e->getMessage(), TransactionOnlineStatus::UPDATE_TRANSACTION_REDIRECT);
throw $e;
}
}
public function webhook($request)
{
try {
$response = $this->client->webhook($request);
OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $response, TransactionOnlineStatus::UPDATE_TRANSACTION_CALLBACK);
return $response;
} catch (\Exception $e) {
OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $e->getMessage(), TransactionOnlineStatus::UPDATE_TRANSACTION_CALLBACK);
throw $e;
}
}
public function getTransactions($date)
{
$arrahnTransactions = $this->getArrahnTransactions($date);
$billplzID = collect($arrahnTransactions)->pluck('billplz_id')->unique()->values();
$billplzTransactions = collect($billplzID)->map(function ($billId) {
return $this->client->transaction()->get($billId)->toArray();
})
->map(function ($data) {
$latestTransaction = reset($data['transactions']) == false ? [] : reset($data['transactions']);
$billId = ['bill_id' => $data['bill_id']];
return array_merge($billId, $latestTransaction);
})
->keyBy('bill_id');
return $billplzTransactions;
}
public function getArrahnTransactions($date)
{
return Http::arrahn()->get('/api/transaction/online/query', ['date' => $date])->json();
}
public function getBillplzTransactionReview($date)
{
$date = Carbon::now()->toDateString();
$arrahnTransactions = $this->getArrahnTransactions($date);
// sort array by tarikh desc and masa key desc
array_multisort(array_column($arrahnTransactions, 'tarikh'), SORT_DESC, array_column($arrahnTransactions, 'masa'), SORT_DESC, $arrahnTransactions);
$arrahnTransactionsGroupedByBill = collect($arrahnTransactions)->groupBy('billplz_id');
$billplzID = $arrahnTransactionsGroupedByBill->keys();
$billplzTransactions = $this->getTransactions($date);
$merged = $billplzID->map(function ($billId) use ($arrahnTransactionsGroupedByBill, $billplzTransactions) {
return [
'bill_id' => $billId,
'arrahn_transactions' => $arrahnTransactionsGroupedByBill->get($billId)->toArray(),
'billplz_latest_transaction' => $billplzTransactions->get($billId),
];
});
$summarizedBillplzTransactions = collect($merged)->map(function ($data) {
$data['arrahn_transactions'] = collect($data['arrahn_transactions'])->map(function ($transaction) {
return[
'nokp' => $transaction['nokp'],
'norujukan' => $transaction['norujukan'],
'kodcaw' => $transaction['kodcaw'],
'total_payment' => $transaction['total_payment'],
'customer_name' => $transaction['customer_name'],
'status' => $transaction['status'],
'paid_at' => $transaction['paid_at'],
'tarikh_attempt_bayar' => $transaction['tarikh'],
'masa_attempt_bayar' => $transaction['masa'],
'portal' => $transaction['tagportal'] == 1 ? 'Arrahn Pay' : 'SPARE',
];
});
return $data;
});
return $summarizedBillplzTransactions;
}
public function getTransactionByBillId($billId)
{
try {
return $this->client->bill()->transaction($billId)->toArray();
} catch (\Throwable $th) {
logger()->error('Error when querying billplz id : ' . $billId);
logger()->error($th->getMessage());
throw $th;
}
}
}