94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\V3\Dashboard\Harim;
|
|
|
|
use App\Facades\Sms\Sms;
|
|
use App\Enums\HarimStates;
|
|
use App\Exceptions\ProhibitedAction;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\V3\Dashboard\Harim\Detail\HarimSubmitInvoiceRequest;
|
|
use App\Http\Traits\ApiResponse;
|
|
use App\Models\Harim;
|
|
use App\Models\HarimState;
|
|
use App\Services\Cartables\Harim\HarimPaymentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class DetailController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function states(): JsonResponse
|
|
{
|
|
return $this->successResponse(HarimState::query()->get(['id', 'name']));
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function histories(Harim $harim): JsonResponse
|
|
{
|
|
throw_if($harim->province_id != auth()->user()->province_id, new ProhibitedAction("شما دسترسی لازم جهت مشاهده ی این صفحه را ندارید"));
|
|
|
|
return $this->successResponse($harim->histories()
|
|
->orderBy('id', 'desc')
|
|
->get(['id', 'expert_description', 'action_name', 'previous_state_name']));
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
* @throws ProhibitedAction
|
|
*/
|
|
public function submitInvoice(Harim $harim, HarimPaymentService $harimpaymentService, HarimSubmitInvoiceRequest $request): JsonResponse
|
|
{
|
|
$lock = Cache::lock("harimPayment-{$harim->id}", 10);
|
|
throw_if(! $lock->get(), new ProhibitedAction('امکان درخواست مجدد تا 10 ثانیه دیگر وجود ندارد'));
|
|
|
|
$payment_amount = $harim->payment_amount;
|
|
|
|
$bill_code = $harimpaymentService->invoiceBillApi($harim->national_id, $payment_amount);
|
|
|
|
DB::transaction(function () use ($bill_code, $harim) {
|
|
|
|
$harim->histories()->update([
|
|
// 'bill_code' => $bill_code,
|
|
'status' => HarimStates::SODOR_FACTOR->value,
|
|
'status_fa' => HarimStates::SODOR_FACTOR->label(),
|
|
]);
|
|
|
|
});
|
|
|
|
$msg = "فاکتور با شناسه پرداخت \n" . explode("/", $harim->bill_code)[0]. "\n برای پرداخت از لینک زیر اقدام نمایید.\n\n"
|
|
. config('harim_web_services.Harim_Payment.LINK')."/#/pay/".explode("/", $harim->bill_code)[0]."/$payment_amount";
|
|
|
|
Sms::sendSms($harim->phone_number, $msg);
|
|
|
|
$lock->release();
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function checkPaymentStatus(Harim $harim, HarimPaymentService $harimpaymentService): JsonResponse
|
|
{
|
|
DB::transaction(function () use ($harim, $harimpaymentService) {
|
|
|
|
$response = json_decode($harimpaymentService->callPaymentStatusBillApi(explode('/', $harim->bill_code)[0]));
|
|
|
|
throw_if(! $response->isPayed, new ProhibitedAction('پرداخت انجام نشده است'));
|
|
|
|
$harim->histories()->update([
|
|
'status' => HarimStates::PARDAKHT_FACTOR->value,
|
|
'status_fa' => HarimStates::PARDAKHT_FACTOR->label(),
|
|
]);
|
|
|
|
});
|
|
|
|
return $this->successResponse($harim);
|
|
}
|
|
}
|