127 lines
6.1 KiB
PHP
127 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Single;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\SuperController;
|
|
use App\Models\BankAccount;
|
|
use App\Models\Customer;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Hekmatinasser\Verta\Verta;
|
|
use Carbon\Carbon;
|
|
use App\Models\Log;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
class BankAccountController extends SuperController
|
|
{
|
|
public function __construct(Request $request)
|
|
{
|
|
$query = BankAccount::where('customer_id', auth('customer')->id());
|
|
$request->request->add(['customer_id' => auth('customer')->id()]);
|
|
parent::__construct(BankAccount::class, [], ['*', BankAccount::$SELECT_STS_STR], null, $query);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$account = BankAccount::findOrFail($id);
|
|
if ($account->sts != 3 && $account->sts != 2) {
|
|
throw new UnprocessableEntityHttpException('امکان حذف این کارت بانکی وجود ندارد');
|
|
}
|
|
return parent::destroy($id);
|
|
}
|
|
|
|
public function listActiveBankAccount()
|
|
{
|
|
$query = BankAccount::where('customer_id', auth('customer')->id())
|
|
->whereIn('sts', [1, 2])->get();
|
|
return response()->json($query);
|
|
}
|
|
public function StoreBankAccount(Request $request)
|
|
{
|
|
if (strlen($request->cart_number) != 16) {
|
|
throw new UnprocessableEntityHttpException('شماره کارت اشتباه میباشد.');
|
|
}
|
|
if (BankAccount::where('cart_number', $request->cart_number)->exists()) {
|
|
throw new UnprocessableEntityHttpException('شماره کارت تکراری میباشد.');
|
|
}
|
|
|
|
$user = auth()->guard('customer')->user();
|
|
if ($user->n_code === null || $user->n_code === '') {
|
|
throw new UnprocessableEntityHttpException('لطفا شماره ملی خود را در پروفایل کاربری وارد کنید سپس اقدام به ثبت کارت بانکی کنید.');
|
|
}
|
|
if($user->birth_date == null && !isset($request->birth_date)){
|
|
throw new UnprocessableEntityHttpException('لطفا تاریخ تولد خود را در پروفایل کاربری وارد کنید سپس اقدام به ثبت کارت بانکی کنید.');
|
|
}
|
|
|
|
//with verification in jibit
|
|
if($this->Config('jibit_verification') == 'true') {
|
|
$p1 = $apiData['iban'] = preg_replace('/\D/', '', $request->cart_number);
|
|
$p2 = $apiData['nationalCode'] = $user->n_code;
|
|
$p3 = $apiData['birthDate'] = Verta::instance(Carbon::parse($request->birth_date ?? $user->birth_date))->format('Ymd');
|
|
// dd($p3);
|
|
// [$apiData['iban'],$apiData['nationalCode'],$apiData['birthDate']];
|
|
|
|
$apiData['r'] = $this->jibit('cartMatching', $apiData['iban'], $apiData['nationalCode'], $apiData['birthDate']);
|
|
try {
|
|
$logPayload = [
|
|
'cartMatching' => $apiData['r']['cartMatching'] ?? null,
|
|
'cartData' => $apiData['r']['cartData'] ?? null,
|
|
];
|
|
$log = new Log();
|
|
$log->customer_id = auth('customer')->id();
|
|
$log->data = json_encode($logPayload, JSON_UNESCAPED_UNICODE);
|
|
$log->save();
|
|
} catch (\Exception $e) {
|
|
\Log::warning('Bank account jibit log save failed', [
|
|
'customer_id' => auth('customer')->id(),
|
|
'message' => $e->getMessage(),
|
|
]);
|
|
}
|
|
|
|
if (isset($apiData['r']['cartData'])) {
|
|
$BA = BankAccount::create([
|
|
'customer_id' => $user->id,
|
|
'cart_number' => $apiData['r']['cartData']['number'],
|
|
'bank_name' => $apiData['r']['cartData']['ibanInfo']['bank'],
|
|
'account_number' => $apiData['r']['cartData']['ibanInfo']['depositNumber'],
|
|
'shaba_number' => $apiData['r']['cartData']['ibanInfo']['iban'],
|
|
'first_name' => $apiData['r']['cartData']['ibanInfo']['owners'][0]['firstName'],
|
|
'last_name' => $apiData['r']['cartData']['ibanInfo']['owners'][0]['lastName'],
|
|
'cart_status' => $apiData['r']['cartData']['ibanInfo']['status'],
|
|
'sts' => 2,
|
|
]);
|
|
Customer::where('id', $user->id)->update(['birth_date' => $request->birth_date ?? $user->birth_date]);
|
|
$data = ['success' => true, 'bankAccount' => $BA];
|
|
return response()->json($data);
|
|
|
|
} else {
|
|
$message = "احرازهویت برای شماره کارت کاربر " . $user->name . " توسط جیبیت انجام نشد. لطفا از پنل ادمین اقدام کنید. ";
|
|
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
|
|
$this->SendMessage($admin->mobile, $message);
|
|
});
|
|
$BA = BankAccount::create([
|
|
'customer_id' => $user->id,
|
|
'cart_number' => $request->cart_number,
|
|
'first_name' => $user->fname,
|
|
'last_name' => $user->lname,
|
|
'sts' => 1,
|
|
]);
|
|
throw new UnprocessableEntityHttpException('شماره کارت شما برای ادمین ارسال شد و پس از تایید قابل استفاده می باشد.');
|
|
return $apiData;
|
|
}
|
|
}
|
|
$customer = Customer::find(auth('customer')->id());
|
|
$message = "کارت جدیدی برای $customer->name ساخته شد منتظر تایید شما هستیم.";
|
|
$message .= $this->Config('name_fa');
|
|
|
|
User::where('active', 1) ->where('send_sms', 1)->get()->each(function($admin) use ($message) {
|
|
$this->Send($admin->mobile, $message);
|
|
});
|
|
$data = $request->only(['account_number', 'bank_name', 'cart_number', 'shaba_number', 'customer_id']);
|
|
BankAccount::create($data);
|
|
return response()->json($data);
|
|
}
|
|
|
|
}
|