<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Doctrine\ODM\MongoDB\DocumentManager;
use App\Document\User;
use App\Document\Wallet;
use App\Document\Bank;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Document\Order ;
use App\Document\MarketOrder ;
use Symfony\Component\String\Slugger\SluggerInterface;
class UserController extends AbstractController
{
private $translator;
private $slug;
private $client;
private $doctrine;
private $slugger;
private $logger;
private $doc ;
public function __construct(TranslatorInterface $translator , HttpClientInterface $client , DocumentManager $doctrine , SluggerInterface $slugger )
{
$this->translator = $translator ;
$this->client = $client ;
$this->doc = $doctrine ;
$this->slug = $slugger ;
}
#[Route('/api/admin/users',methods: ['POST'], name: 'admin_get_users')]
public function AdminGetUsers(DocumentManager $doctrine , Request $request): JsonResponse
{
$users = $doctrine->createQueryBuilder(User::class)
->field('roles')->in(['ROLE_USER'])
->hydrate(false)
->getQuery()
->execute()
->toArray();
$persianMonthsCount = array_fill(1, 12, 0); // Initialize counts for each Persian month
foreach ($users as $user) {
// Check if 'registerDate' exists and is valid
if (!isset($user['registerdate']) || !is_numeric($user['registerdate'])) {
continue; // Skip invalid dates
}
$registerDate = (new \DateTime())->setTimestamp($user['registerdate']);
$gregorianMonth = (int) $registerDate->format('m'); // Get Gregorian month
// Determine the corresponding Persian month based on the Gregorian month and day
$dayOfMonth = (int) $registerDate->format('d');
$persianMonth = 0;
// Mapping Gregorian month ranges to Persian months
if ($gregorianMonth === 3 && $dayOfMonth >= 21 || $gregorianMonth === 4 && $dayOfMonth <= 20) {
$persianMonth = 1; // Farvardin
} elseif ($gregorianMonth === 4 && $dayOfMonth >= 21 || $gregorianMonth === 5 && $dayOfMonth <= 20) {
$persianMonth = 2; // Ordibehesht
} elseif ($gregorianMonth === 5 && $dayOfMonth >= 21 || $gregorianMonth === 6 && $dayOfMonth <= 20) {
$persianMonth = 3; // Khordad
} elseif ($gregorianMonth === 6 && $dayOfMonth >= 21 || $gregorianMonth === 7 && $dayOfMonth <= 22) {
$persianMonth = 4; // Tir
} elseif ($gregorianMonth === 7 && $dayOfMonth >= 23 || $gregorianMonth === 8 && $dayOfMonth <= 22) {
$persianMonth = 5; // Mordad
} elseif ($gregorianMonth === 8 && $dayOfMonth >= 23 || $gregorianMonth === 9 && $dayOfMonth <= 22) {
$persianMonth = 6; // Shahrivar
} elseif ($gregorianMonth === 9 && $dayOfMonth >= 23 || $gregorianMonth === 10 && $dayOfMonth <= 22) {
$persianMonth = 7; // Mehr
} elseif ($gregorianMonth === 10 && $dayOfMonth >= 23 || $gregorianMonth === 11 && $dayOfMonth <= 21) {
$persianMonth = 8; // Aban
} elseif ($gregorianMonth === 11 && $dayOfMonth >= 22 || $gregorianMonth === 12 && $dayOfMonth <= 21) {
$persianMonth = 9; // Azar
} elseif ($gregorianMonth === 12 && $dayOfMonth >= 22 || $gregorianMonth === 1 && $dayOfMonth <= 20) {
$persianMonth = 10; // Dey
} elseif ($gregorianMonth === 1 && $dayOfMonth >= 21 || $gregorianMonth === 2 && $dayOfMonth <= 19) {
$persianMonth = 11; // Bahman
} elseif ($gregorianMonth === 2 && $dayOfMonth >= 20 || $gregorianMonth === 3 && $dayOfMonth <= 20) {
$persianMonth = 12; // Esfand
}
// Increment the count for the identified Persian month
if ($persianMonth > 0) {
$persianMonthsCount[$persianMonth]++;
}
}
$res = ["status" => "200" , "msg" => "admin get users" , "users" =>$users , "regcount" => array_values($persianMonthsCount) ];
return new JsonResponse($res);
}
#[Route('/api/admin/kyc_location',methods: ['POST'], name: 'admin_kyc_location')]
public function AdminKycLocation(DocumentManager $doctrine , Request $request): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user_id = $data[0]->id ;
$user = $doctrine->getRepository(User::class)->findOneBy(['id' => $user_id]);
$kyc_address_status = $user->getKycaddress();
if(is_null($kyc_address_status) || $kyc_address_status != true ) {
$user->setKycaddress(true);
$user->setLevel('3');
} else {
$user->setKycaddress(false);
}
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "location status changed" , 'test' => $kyc_address_status ];
return new JsonResponse($res);
}
#[Route('/api/admin/getuser',methods: ['POST'], name: 'admin_get_user')]
public function AdminGetUser(DocumentManager $doctrine , Request $request): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user_id = $data[0]->id ;
$user = $doctrine->createQueryBuilder(User::class)
->field('id')->equals($user_id)
->hydrate(false)
->getQuery()
->execute()
->toArray();
$cards = $doctrine->createQueryBuilder(Bank::class)
->field('user')->equals($user_id)
->hydrate(false)
->getQuery()
->execute()
->toArray();
$market_order = $doctrine->createQueryBuilder(MarketOrder::class)
->field('userid')->equals($user_id)
->hydrate(false)
->getQuery()
->execute()
->toArray();
$otc_order = $doctrine->createQueryBuilder(Order::class)
->field('userid')->equals($user_id)
->hydrate(false)
->getQuery()
->execute()
->toArray();
$res = ["status" => "200" , "msg" => "admin get user" , "user" => $user , "cards" => $cards , "market" => $market_order , "otc" => $otc_order];
return new JsonResponse($res);
}
#[Route('/api/admin/updatestatus',methods: ['POST'], name: 'update_status')]
public function updatestatus(DocumentManager $doctrine , Request $request ): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user_id = $data->id ;
$user_exist = $doctrine->getRepository(User::class)->findOneBy(['id' => $user_id]);
$live_status = $user_exist->getStatus();
if($live_status === "1") {
$status = 0 ;
} else {
$status = 1 ;
}
$user_exist->setStatus($status);
$doctrine->persist($user_exist);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "Profile Updated"] ;
return new JsonResponse($res);
}
#[Route('/api/passwordresetrequest',methods: ['POST'], name: 'password_reset_request')]
public function passwordresetrequest(DocumentManager $doctrine , Request $request): JsonResponse
{
$data = json_decode($request->getContent(), false);
$mobile = $data->mobile ;
$user = $doctrine->getRepository(User::class)->findOneBy(['username' => $mobile]);
$otp = random_int(10000, 99999);
$otptime = date('H:i:s \O\n d/m/Y');
if($user) {
$finduser = $doctrine->createQueryBuilder(User::class)
->findAndUpdate()
->field('id')->equals($user->getId())
->sort('priority', 'desc')
->field('otp')->set($otp)
->field('otptime')->set($otptime)
->getQuery()
->execute();
}
$res = ["status" => "200" , "msg" => "otp code sent"] ;
return new JsonResponse($res);
}
#[Route('/api/passwordreset',methods: ['POST'], name: 'password_reset')]
public function passwordreset(DocumentManager $doctrine , Request $request ,GoogleAuthenticatorInterface $twofactor, UserPasswordHasherInterface $passwordHasher): JsonResponse
{
$data = json_decode($request->getContent(), false);
$mobile = $data->mobile ;
$otp = $data->otp ;
$gauthcode = $data->gauth ;
$password = $data->password ;
$user = $doctrine->getRepository(User::class)->findOneBy(['username' => $mobile]);
if($user) {
$google_auth_secret = $user->getGoogleAuthenticatorSecret();
if($google_auth_secret) {
$google_auth_check = $twofactor->checkCode($user,$gauthcode);
if($google_auth_check) {
if($user->getOtp() === $otp) {
$hashedPassword = $passwordHasher->hashPassword(
$user,
$password
);
$user->setPassword($hashedPassword);
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "New Password Set"] ;
} else {
$res = ["status" => "400" , "msg" => "Wrong Otp Code" ,"token" => null] ;
}
} else {
$res = ["status" => "400" , "msg" => "Wrong Google Auth Code" , "token" => null] ;
}
} else {
if($user->getOtp() === $otp) {
$hashedPassword = $passwordHasher->hashPassword(
$user,
$password
);
$user->setPassword($hashedPassword);
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "New Password Set"] ;
} else {
$res = ["status" => "400" , "msg" => "Wrong Otp Code" ,"token" => null] ;
}
}
}
return new JsonResponse($res);
}
// #[Route('/api/register',methods: ['POST'], name: 'user_register')]
// public function userregister(DocumentManager $doctrine , Request $request , UserPasswordHasherInterface $passwordHasher): JsonResponse
// {
// $data = json_decode($request->getContent(), false);
// $mobile = $data->mobile ;
// $password = $data->password ;
// $name = $data->name ;
// $family = $data->family ;
// $user_exist = $doctrine->getRepository(User::class)->findOneBy(['username' => $mobile]);
// if (($user_exist)) {
// $res = ["status" => "400" , "msg" => "Username Exsit" ,] ;
// return new JsonResponse($res);
// } else {
// $otp = random_int(10000, 99999);
// $otptime = date('H:i:s \O\n d/m/Y');
// $user = new User() ;
// $user->setUsername($mobile);
// $hashedPassword = $passwordHasher->hashPassword(
// $user,
// $password
// );
// $user->setPassword($hashedPassword);
// $user->setRoles(["ROLE_USER"]);
// $user->setFirstname($name);
// $user->setLastname($family);
// $user->setOtp($otp);
// $user->setOtptime($otptime);
// $user->setStatus(0);
// $user->setLevel(0);
// $result = $doctrine->persist($user);
// $doctrine->flush();
// $res = ["status" => "200" , "msg" => "User Registered"] ;
// return new JsonResponse($res);
// }
// }
public function Ref_code_Gen() {
$code = strtoupper(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'));
$ref_code = substr($code,0,8) ;
$ref_code_exist = $this->doc->getRepository(User::class)->findOneBy(['referralcode' => $ref_code ]);
if($ref_code_exist) {
Ref_code_Gen();
} else {
return $ref_code ;
}
}
#[Route('/api/otpsend',methods: ['POST'], name: 'otp_send')]
public function onetimepassword(DocumentManager $doctrine , Request $request): JsonResponse
{
$data = json_decode($request->getContent(), false);
$mobile = $data->mobile ;
$two_step_active = False ;
if(!preg_match("/^09[0-9]{9}$/", $mobile)) {
$translated = $this->translator->trans('Invalid Mobile');
$res = ["status" => "400" , "msg" => $translated] ;
return new JsonResponse($res);
}
$user = $doctrine->getRepository(User::class)->findOneBy(['username' => $mobile]);
$otp = random_int(10000, 99999);
$otptime = date('H:i:s \O\n d/m/Y');
if(is_null($user)) {
$ref_code = $this->Ref_code_Gen();
$user = new User() ;
$user->setUsername($mobile);
$user->setStatus(0);
$user->setRoles(["ROLE_USER"]);
$user->setOtp($otp);
$user->setFata(false);
$user->setReferralCode($ref_code);
$user->setOtptime($otptime);
$user->setRegisterdate(time());
$doctrine->persist($user);
$doctrine->flush();
} else {
$finduser = $doctrine->createQueryBuilder(User::class)
->findAndUpdate()
->field('id')->equals($user->getId())
->sort('priority', 'desc')
->field('otp')->set($otp)
->field('otptime')->set($otptime)
->getQuery()
->execute();
$two_step_active = $user->getGauthstatus();
}
$response = $this->forward('App\Controller\NotificationController::sms_send_otp', [
'msg' => $otp,
'number' =>$mobile,
]);
$content = json_decode($response->getContent()) ;
$ret = $content ;
$res = ["status" => 200 , "msg" => "otp sent" , "gauth" => $two_step_active ] ;
return new JsonResponse($res);
}
#[Route('/api/user/fataseen',methods: ['POST'], name: 'fata_seen')]
public function FataSeen(DocumentManager $doctrine , Request $request ): JsonResponse
{
$user = $this->getUser();
$fata = $user->getFata();
if(($fata) === "" || $fata = 0 ) {
$user->setFata(true);
$doctrine->persist($user);
$doctrine->flush();
}
$res = ["status" => "200" , "msg" => "fata seen" ] ;
return new JsonResponse($res);
}
#[Route('/api/otpcheck',methods: ['POST'], name: 'otp_check')]
public function onetimepasswordcheck(DocumentManager $doctrine , Request $request ,GoogleAuthenticatorInterface $twofactor, JWTTokenManagerInterface $JWTManager): JsonResponse
{
$data = json_decode($request->getContent(), false);
$mobile = $data->username ;
$otp = $data->password ;
$gauthcode = $data->gauth ;
$user = $doctrine->getRepository(User::class)->findOneBy(['username' => $mobile]);
if($user) {
$google_auth_status = $user->getGauthstatus();
if(!$google_auth_status) {
$secret = $twofactor->generateSecret($user);
$user->setGoogleAuthenticatorSecret($secret);
$doctrine->persist($user);
$doctrine->flush();
}
// $wallets_exist = $doctrine->createQueryBuilder(Wallet::class)
// ->field('userid')->equals($user->getId())
// ->field('pair')->in(['RIAL', 'USDT'])
// ->hydrate(false)
// ->getQuery()
// ->execute()
// ->toArray();
// if(!$wallets_exist) {
// $need_wallets = array("RIAL","USDT", "BTC", "ETH", "DOGE", "ADA", "SOL", "DOT", "TRX", "BNB", "LTC");
// foreach($need_wallets as $need_wallet) {
// $wall = new Wallet();
// $wall->setuserid($user->getId());
// $wall->setAddress('');
// $wall->setnetwork('main');
// $wall->setpair($need_wallet);
// $wall->setballance(0);
// $doctrine->persist($wall);
// $doctrine->flush();
// }
// }
if($google_auth_status) {
$google_auth_check = $twofactor->checkCode($user,$gauthcode);
if($google_auth_check) {
if($user->getOtp() === $otp) {
$res = ["status" => "200" , "msg" => "success", "userlevel" => $user->getLevel() , "token" => $JWTManager->create($user)] ;
} else {
$res = ["status" => "400" , "msg" => "Wrong Otp Code" ,"token" => null] ;
}
} else {
$res = ["status" => "400" , "msg" => "Wrong Google Auth Code" , "token" => null] ;
}
} else {
if($user->getOtp() === $otp) {
$res = ["status" => "200" , "msg" => "success", "userlevel" => $user->getLevel() ,"userstatus" => $user->getStatus() , "token" => $JWTManager->create($user)] ;
} else {
$res = ["status" => "400" , "msg" => "Wrong Otp Code" ,"token" => null] ;
}
}
}
return new JsonResponse($res);
}
#[Route('/api/gauth/activation',methods: ['POST'], name: 'google_auth_activation')]
public function googleauthactivator(DocumentManager $doctrine , Request $request , GoogleAuthenticatorInterface $twofactor): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user = $this->getUser();
$two_code = $data->gauth ;
$google_auth_status = $user->getGauthstatus();
$google_auth_check = $twofactor->checkCode($user,$two_code);
if($google_auth_check) {
if($google_auth_status) {
$user->setGauthstatus(False);
} else {
$user->setGauthstatus(True);
}
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "gauth activation"] ;
} else {
$res = ["status" => "400" , "msg" => "Wrong Code" ] ;
}
return new JsonResponse($res);
}
#[Route('/api/gauth',methods: ['POST'], name: 'google_auth')]
public function googleauth(DocumentManager $doctrine , Request $request , GoogleAuthenticatorInterface $twofactor): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user = $this->getUser();
$google_auth_status = $user->isGoogleAuthenticatorEnabled();
if($google_auth_status) {
$user->setGoogleAuthenticatorSecret(null);
}
else {
$secret = $twofactor->generateSecret($user);
$user->setGoogleAuthenticatorSecret($secret);
}
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "400" , "gauthsecret" => $user->getGoogleAuthenticatorSecret()] ;
return new JsonResponse($res);
}
#[Route('/api/updateprofile',methods: ['POST'], name: 'update_profile')]
public function updateprofile(DocumentManager $doctrine , Request $request ): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user = $this->getUser();
$firstname = $data->firstname ;
$lastname = $data->lastname ;
$nationalid = $data->nationalid ;
$country = $data->country ;
$birthdate = $data->birthdate ;
$identitydocument = $data->identitydocument ;
$user->setFirstname($firstname);
$user->setLastname($lastname);
$user->setNationalid($nationalid);
$user->setCountry($country);
$user->setBirthdate($birthdate);
$user->setIdentitydocument($identitydocument);
$doctrine->persist($user);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "Profile Updated"] ;
return new JsonResponse($res);
}
#[Route('/api/user/profile',methods: ['POST'], name: 'user_profile')]
public function userprofile(DocumentManager $doctrine , Request $request ): JsonResponse
{
$data = json_decode($request->getContent(), false);
$user = $this->getUser();
$result = array(
"fullname" => $user->getFirstname() . " " . $user->getLastname() ,
"mobile" => $user->getUsername(),
"status" => $user->getStatus() ,
"nid" => $user->getNationalid() ,
"level" => $user->getLevel(),
"fata" => $user->getFata(),
"email" => $user->getEmail(),
"twofa" => $user->getGoogleAuthenticatorSecret(),
"gauth" => $user->getGauthstatus(),
"referralcode" => $user->getReferralCode(),
"telphone" => $user->getTelphone(),
"address" => $user->getAddress()
);
$res = ["status" => "200" , "msg" => "get Profile" , "user" => $result] ;
return new JsonResponse($res);
}
public function getfinotoken(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://trusttether.org/api/auth',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"username" : "hamed",
"password" : "hamed110"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response) ;
}
#[Route('/api/kyc/location',methods: ['POST'], name: 'user_kyc_location')]
public function kyclocation(DocumentManager $doctrine , Request $request ): JsonResponse
{
$user = $this->getUser();
$data = json_decode($request->getContent(), false);
$file = $request->files->get('file');
if (!$file) {
$translated = $this->translator->trans('bill not found');
$res = ["status" => "400" , "msg" => $translated ] ;
return new JsonResponse($res);
}
$address =$request->get('address') ;
$telphone = $request->get('telphone') ;
$doc = $this->saveFile($file);
$user->setTelphone($telphone);
$user->setAddress($address);
$user->setKycdocument($doc);
$doctrine->persist($user);
$doctrine->flush();
$translated = $this->translator->trans('location registerd');
$res = ["status" => "200" , "msg" => $translated ] ;
return new JsonResponse($res);
}
#[Route('/api/kyc/infocheck',methods: ['POST'], name: 'user_kyc_info')]
public function infocheck(DocumentManager $doctrine , Request $request ): JsonResponse
{
$server_token = "c736234831ab9805856a7239e5a7acec6145901f";
$user = $this->getUser();
$data = json_decode($request->getContent(), false);
$name = $data->name ;
$family = $data->family ;
$birthdate = $data->birthday ;
$email = $data->email ;
$referral = $data->referral ;
$nid = $user->getNationalid() ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://service.zohal.io/api/v0/services/inquiry/national_identity_inquiry',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"national_code" : "'.$nid.'",
"birth_date" : "'.$birthdate.'"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer '.$server_token.''
),
));
$response = curl_exec($curl);
curl_close($curl);
$responseArray = json_decode($response, true);
if (isset($responseArray['response_body']['data']['matched']) && $responseArray['response_body']['data']['matched'] === true) {
$user->setLastname($family);
$user->setFirstname($name);
$user->setEmail($email);
$user->setReferral($referral);
$user->setBirthdate($birthdate);
$user->setLevel(1);
$user->setStatus(1);
$doctrine->persist($user);
$doctrine->flush();
}
$res = ["status" => "200" , "msg" => "اطلاعات شما جهت بررسی ارسال گردید" , "res" => $responseArray['response_body']['data']['matched'] ] ;
return new JsonResponse($res);
}
#[Route('/api/bank/add',methods: ['POST'], name: 'user_kyc_card')]
public function cardcheck(DocumentManager $doctrine , Request $request ): JsonResponse
{
// $server_token = ($this->getfinotoken())->token;
$user = $this->getUser();
$data = json_decode($request->getContent(), false);
$card_number = $data->number ;
$nid = $user->getNationalid() ;
$birthdate = $user->getBirthday() ;
if(!$nid || !$birthdate ) {
$res = ["status" => "400" , "msg" => "ابتدا اطلاعات هویتی خود را تکمیل نمایید" ];
return new JsonResponse($res);
}
if(strlen($card_number) != 16) {
$res = ["status" => "400" , "msg" => "شماره کارت اشتباه می باشد" ];
return new JsonResponse($res);
}
$card_exist = $doctrine->getRepository(Bank::class)->findOneBy(['number' => $card_number ]);
if($card_exist) {
$res = ["status" => "400" , "msg" => "شماره کارت تکراری می باشد" ];
return new JsonResponse($res);
}
$wall = new Bank();
$wall->setUser($user->getId());
$wall->setName('');
$wall->setBank('');
$wall->setNumber($card_number);
$wall->setIban('');
$wall->setStatus('1');
$doctrine->persist($wall);
$doctrine->flush();
$res = ["status" => "200" , "msg" => "کارت بانکی تایید و اضافه گردید" ] ;
return new JsonResponse($res);
}
#[Route('/api/kyc/idcardcheck',methods: ['POST'], name: 'user_kyc_idcard')]
public function idcardcheck(DocumentManager $doctrine , Request $request ): JsonResponse
{
$server_token = "c736234831ab9805856a7239e5a7acec6145901f";
$user = $this->getUser();
$data = json_decode($request->getContent(), false);
$nid = $user->getNationalid();
$file = $request->files->get('file');
$file2 = $request->files->get('file2');
$temp_file = $request->files->get('file');
$temp_file2 = $request->files->get('file2');
if (!$file) {
$res = ["status" => "400" , "msg" => "id card not found" ] ;
return new JsonResponse($res);
}
$tempFile = $file->getPathname();
$tempFile2 = $file2->getPathname();
$front = $tempFile ;
$back = $tempFile2 ;
// API URL
$url = 'https://service.zohal.io/api/v0/services/inquiry/national_card_ocr';
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$headers = array(
'Content-Type: multipart/form-data',
'Authorization: Bearer ' . $server_token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Prepare file uploads
$postData = [
'national_card_back' => new \CURLFile($back, $temp_file2->getMimeType(), $temp_file2->getClientOriginalName()),
'national_card_front' => new \CURLFile($front, $temp_file->getMimeType(), $temp_file->getClientOriginalName())
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Execute cURL request
$response = curl_exec($ch);
curl_close($ch);
$responseArray = json_decode($response, true);
$flag = false ;
if(isset($responseArray['response_body']['data']['front']) && isset($responseArray['response_body']['data']['front'])) {
$nid_back = $responseArray['response_body']['data']['back']['nationalCode'];
$nid_front = $responseArray['response_body']['data']['front']['nationalCode'];
if ($nid_front === $nid_back && $nid === $nid_front) {
$flag = true ;
}
}
$doc = $this->saveFile($file);
$doc2 = $this->saveFile($file2);
$docs = [$doc,$doc2];
if ($flag){
$user->setLevel("2");
$user->setIdentitydocument(json_encode($docs));
$doctrine->persist($user);
$doctrine->flush();
}
$res = ["status" => "200" , "msg" => "اطلاعات شما جهت بررسی ارسال گردید" , "res" => $flag ] ;
return new JsonResponse($res);
}
#[Route('/api/kyc/videocheck',methods: ['POST'], name: 'user_kyc_video')]
public function videocheck(DocumentManager $doctrine , Request $request ): JsonResponse
{
$server_token = ($this->getfinotoken())->token;
$user = $this->getUser();
$file = $request->files->get('file');
if (!$file) {
$res = ["status" => "200" , "msg" => "video not found" ] ;
return new JsonResponse($res);
}
$videoFile = $this->saveFile($file);
$nid = $user->getNationalid() ;
$birthdate = $user->getBirthday() ;
$serial = "1" ;
$parameters = [
"nid" => $nid ,
"serial" => $serial,
"birth" => $birthdate
];
$postData = http_build_query($parameters);
$url = 'https://trusttether.org/api/finotech/videocheck?'.$postData ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => [],
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data',
'Authorization: Bearer '.$server_token.''
),
));
$response = curl_exec($curl);
curl_close($curl);
$res = ["status" => "200" , "msg" => "video checked" , "res" => json_decode($response) ] ;
return new JsonResponse($res);
}
private function saveFile(UploadedFile $file ): string
{
$directory = 'docss'; // Specify the directory to save the file
try {
$safeFilename = $this->slug->slug($file);
$newFilename = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
$file->move($directory,$newFilename);
return $newFilename;
} catch (Exception $e) {
throw new \RuntimeException('Failed to save file: ' . $e->getMessage());
}
}
#[Route('/api/kyc/nidcheck',methods: ['POST'], name: 'user_kyc_nid')]
public function nidcheck(DocumentManager $doctrine , Request $request ): JsonResponse
{
$server_token = "c736234831ab9805856a7239e5a7acec6145901f";
$user = $this->getUser();
$data = json_decode($request->getContent(), false);
$nid = $data->nid ;
$mobile = $data->mobile ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://service.zohal.io/api/v0/services/inquiry/shahkar',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"national_code" : "'.$nid.'",
"mobile" : "'.$mobile.'"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer '.$server_token.''
),
));
$response = curl_exec($curl);
curl_close($curl);
$responseArray = json_decode($response, true);
if (isset($responseArray['response_body']['data']['matched']) && $responseArray['response_body']['data']['matched'] === true) {
$user->setNationalid($nid);
$doctrine->persist($user);
$doctrine->flush();
}
$res = ["status" => "200" , "msg" => "کد ملی شما جهت بررسی ارسال شد" , "res" =>$responseArray['response_body']['data']['matched'] ] ;
return new JsonResponse($res);
}
#[Route('/api/userkyc',methods: ['POST'], name: 'user_kyc')]
public function userkyc(DocumentManager $doctrine , Request $request ): JsonResponse
{
// $data = json_decode($request->getContent(), false);
// $user = $this->getUser();
// $type_of_kyc = $data->type ;
// switch ($type_of_kyc) {
// case 'mobile':
// # code...
// break;
// case 'email':
// # code...
// break;
// case 'documnet':
// # code...
// break;
// default:
// # code...
// break;
// }
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://apibeta.finnotech.ir/kyc/v2/clients/trusttether/ibanOwnerBirthdateVerification?birthDate=1368%2F06%2F21&nid=0011095891&iban=IR280560611828005163309401',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbklkIjoiOGQzMTViNzAtMzVjMi00YzZlLWE0MmYtNTVlMjI3OWI1ODcwIiwicmVmcmVzaFRva2VuIjoiUXdMa2xudUxFSFhuY2JnbU10Q3VGNFZUOGwwZmRlcFFyYk1oSE42MHdzTkFJSWo3Uk5kbFpMaXZmbmRYWjV3QWc1SmRXY2c1cGxXekJDQm5GQVp4MGpxSmwxczg5aDU5U2xiWTlhSGZ0MjJyT1JWbjFGUjdyYlcyVmViM1k0TFMwUG5hSWVYTlphR2hzdXd6RHpJa3dWNTA4dUIzV3laZ0FHbkxSRkNHTUZqdkZ1WFNZM1BLT0NLZTBRSzhBRDJRVjZ5MG5rd2Y1RURvWk9Ha2hRVjN5TkVuekxtM1VlT1RxSGtJY3N5Wko0emRMZDhyTmdrbHVoWDNZdFpzM1p0UyIsImNyZWF0aW9uRGF0ZSI6IjE0MDIwOTEzMDQ0NDI3IiwibGlmZVRpbWUiOjg2NDAwMDAwMCwiY2xpZW50SWQiOiJ0cnVzdHRldGhlciIsInVzZXJJZCI6IjAwNzU0MjY3MzAiLCJhY3RpdmUiOnRydWUsInNjb3BlcyI6WyJreWM6aWJhbi1vd25lci1iaXJ0aGRhdGUtdmVyaWZpY2F0aW9uOmdldCJdLCJ0eXBlIjoiQ0xJRU5ULUNSRURFTlRJQUwiLCJjbGllbnRJbmZvIjoiVjFqSjFrSExHWDJFZXMzS29ZekF6TlNBWXRKMFUwbmx5MHlMWVVDczlHN0gwc0VsOHlPTndyZ1dqdXFFVzQvWCIsImJhbmsiOiIwNjIiLCJpYXQiOjE3MDE2NTI0NjcsImV4cCI6MTcwMjUxNjQ2N30.dOdZlpimz0rM_uXEFK5moCT3v-cbmz1FYvbfKQtAwIt9O1w0XAWF_6H7tCn2YnV0Z1XmxnupJi5t1oJ4uZhbcz43S7WCxDPbYH9_k58eHzO1UH40WdR7Pw2QW5dx5oj_Bog4SrUvkmdhs0EyXgpQVrfW-zf6CLil-PuGp-J8ao82ouXLG-TwuNwpxqwnf3-TNU4Xj3Yn1nTLWbIa4Mh1YQ-p5osMf-HDZGYLW82Nk3RXnZPvSjSpANxqW2vACBaA8jhebzLzyvWYiLuKrcmNFIg71Grby1vqt0IEgMOQYQtqOuVvlHqiGoh67to-rdhNjfwXALfTarCZTt0rI-6mgg'
),
));
$response = curl_exec($curl);
curl_close($curl);
$res = ["status" => "200" , "msg" => ($response) ] ;
}
}