Add online payment logger services

This commit is contained in:
Afiq Hamzah
2024-08-01 14:00:53 +08:00
parent 82a906ae1a
commit be55b6948d
4 changed files with 166 additions and 63 deletions
+44 -11
View File
@@ -2,9 +2,9 @@
namespace App\Services;
use App\Models\TransactionOnlineStatus;
use Billplz\Client;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class BillplzService
@@ -50,23 +50,56 @@ class BillplzService
];
try{
return $this->client->bill()->create(...$composed_data)->toArray();
} catch(\Exception $e){
if($e->getCode() === 404){
throw new \ErrorException('Please check your billplz payment gateway collection. Ensure that api_key, x_signature, and collection_id are correctly entered');
};
}
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);
};
}
}
public function redirect($request)
{
return $this->client->bill()->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);
}
}
public function webhook($request)
{
return $this->client->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);
}
}
public function getTransactions($date)
@@ -94,7 +127,7 @@ class BillplzService
public function getBillplzTransactionReview($date)
{
$date = Carbon::now()->subDays(8)->toDateString();
$date = Carbon::now()->toDateString();
$arrahnTransactions = $this->getArrahnTransactions($date);
// sort array by tarikh desc and masa key desc
@@ -0,0 +1,24 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class OnlineTransactionLoggerService
{
public static function logs($bill_id, $request, $response, $status)
{
try {
Http::arrahn()->post('/api/transaction/online/log', [
'bill_id' => $bill_id,
'payload' => json_encode($request),
'response' => json_encode($response),
'status' => json_encode($status),
]);
} catch (\Exception $e) {
// Log the exception itself
Log::error('Failed to log activity: ' . $e->getMessage());
}
}
}