80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payments;
|
|
|
|
use App\Enums\PaymentStatusEnum;
|
|
use App\Models\FibTransaction;
|
|
use App\Models\Payment;
|
|
use App\Models\Transaction;
|
|
use App\Services\FIB\CreatePaymentService;
|
|
use App\Services\FIB\DTO\FIBPaymentCreateDTO;
|
|
use App\Services\Payments\DTO\PaymentCreateDTO;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class PaymentService
|
|
{
|
|
protected CreatePaymentService $create_payment_service;
|
|
|
|
public function __construct(CreatePaymentService $create_payment_service)
|
|
{
|
|
$this->create_payment_service = $create_payment_service;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws Throwable
|
|
*/
|
|
public function createPayment(PaymentCreateDTO $dto)
|
|
{
|
|
return DB::transaction(function () use ($dto) {
|
|
|
|
$payment = Payment::query()->create([
|
|
'user_id' => $dto->user_id,
|
|
'username' => $dto->username,
|
|
'track_id' => $dto->track_id,
|
|
'amount' => $dto->amount,
|
|
'currency' => $dto->currency,
|
|
'callback_url' => $dto->callback_url,
|
|
'status_id' => PaymentStatusEnum::Unpaid->value,
|
|
'status_name' => PaymentStatusEnum::Unpaid->name,
|
|
'ip' => $dto->ip,
|
|
'user_gateway_id' => $dto->user_gateway_id,
|
|
]);
|
|
|
|
$response = $this->create_payment_service->run();
|
|
$fib_response_dto = FIBPaymentCreateDTO::fromResponse($response);
|
|
|
|
$transaction = Transaction::query()->create([
|
|
'user_id' => $payment->user_id,
|
|
'amount' => $payment->amount,
|
|
'currency' => $payment->currency,
|
|
'callback_url' => $payment->callback_url,
|
|
// 'description' => '',
|
|
// 'expires_in' => '',
|
|
'payment_id' => $payment->id,
|
|
'payment_track_id' => $payment->track_id,
|
|
'user_gateway_id' => $payment->user_gateway_id,
|
|
// 'status' => '',
|
|
]);
|
|
|
|
FibTransaction::query()->create([
|
|
'transaction_id' => $transaction->id,
|
|
'qrcode' => $fib_response_dto->qrCode,
|
|
'readable_code' => $fib_response_dto->readableCode,
|
|
'personalAppLink' => $fib_response_dto->personalAppLink,
|
|
'businessAppLink' => $fib_response_dto->businessAppLink,
|
|
'corporateAppLink' => $fib_response_dto->corporateAppLink,
|
|
'paymentId' => $fib_response_dto->paymentId,
|
|
'paymentMethod' => 'fib',
|
|
'validUntil' => $fib_response_dto->validUntil,
|
|
'status_id' => PaymentStatusEnum::Unpaid->value,
|
|
'status_name' => PaymentStatusEnum::Unpaid->name,
|
|
]);
|
|
|
|
return $payment;
|
|
});
|
|
}
|
|
}
|