694e1af651
- update jomweb/billplz package - refactor controllers, views with means of to simplify data fetch and manipuation
123 lines
4.0 KiB
PHP
123 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Billplz\Client;
|
|
use Illuminate\Http\Request;
|
|
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,
|
|
];
|
|
|
|
return $this->client->bill()->create(...$composed_data)->toArray();
|
|
}
|
|
|
|
public function redirect($request)
|
|
{
|
|
return $this->client->bill()->redirect($request);
|
|
}
|
|
|
|
public function webhook($request)
|
|
{
|
|
return $this->client->webhook($request);
|
|
}
|
|
|
|
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)
|
|
{
|
|
$arrahnTransactions = $this->getArrahnTransactions($date);
|
|
$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'],
|
|
'portal' => $transaction['tagportal'] == 1 ? 'Arrahn Pay' : 'SPARE',
|
|
];
|
|
});
|
|
|
|
return $data;
|
|
});
|
|
|
|
return $summarizedBillplzTransactions;
|
|
}
|
|
}
|