add ippanel to sms services

This commit is contained in:
Hesabix 2024-12-28 06:46:10 +00:00
parent 070effd314
commit 98e0ad2d18
5 changed files with 460 additions and 68 deletions

View file

@ -0,0 +1,218 @@
<?php
namespace App\Controller;
use App\Entity\Settings;
use App\Entity\Support;
use App\Entity\User;
use App\Service\Explore;
use App\Service\Extractor;
use App\Service\Jdate;
use App\Service\Notification;
use App\Service\Provider;
use App\Service\registryMGR;
use App\Service\SMS;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
class SupportController extends AbstractController
{
/**
* function to generate random strings
* @param int $length number of characters in the generated string
* @return string a new string is created with random characters of the desired length
*/
private function RandomString($length = 32)
{
return substr(str_shuffle(str_repeat($x = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
}
#[Route('/api/admin/support/list', name: 'app_admin_support_list')]
public function app_admin_support_list(Extractor $extractor, EntityManagerInterface $entityManager): JsonResponse
{
$items = $entityManager->getRepository(Support::class)->findBy(['main' => 0], ['id' => 'DESC']);
$res = [];
foreach ($items as $item) {
$res[] = Explore::ExploreSupportTicket($item, $this->getUser());
}
return $this->json($extractor->operationSuccess($res));
}
#[Route('/api/admin/support/view/{id}', name: 'app_admin_support_view')]
public function app_admin_support_view(Extractor $extractor, Jdate $jdate, EntityManagerInterface $entityManager, string $id = ''): JsonResponse
{
$item = $entityManager->getRepository(Support::class)->find($id);
if (!$item)
throw $this->createNotFoundException();
$replays = $entityManager->getRepository(Support::class)->findBy(['main' => $item->getId()]);
$res = [];
foreach ($replays as $replay) {
if ($replay->getSubmitter() == $this->getUser())
$replay->setState(1);
else
$replay->setState(0);
$res[] = Explore::ExploreSupportTicket($replay, $this->getUser());
}
return $this->json(
$extractor->operationSuccess([
'item' => Explore::ExploreSupportTicket($item, $this->getUser()),
'replays' => $res
])
);
}
#[Route('/api/admin/support/mod/{id}', name: 'app_admin_support_mod')]
public function app_admin_support_mod(registryMGR $registryMGR, SMS $SMS, Request $request, EntityManagerInterface $entityManager, Notification $notifi, string $id = ''): JsonResponse
{
$params = [];
if ($content = $request->getContent()) {
$params = json_decode($content, true);
}
$item = $entityManager->getRepository(Support::class)->find($id);
if (!$item)
$this->createNotFoundException();
if (array_key_exists('body', $params)) {
$support = new Support();
$support->setDateSubmit(time());
$support->setTitle('0');
$support->setBody($params['body']);
$support->setState('0');
$support->setMain($item->getId());
$support->setSubmitter($this->getUser());
$entityManager->persist($support);
$entityManager->flush();
$item->setState('پاسخ داده شده');
$entityManager->persist($support);
$entityManager->flush();
//send sms to customer
if ($item->getSubmitter()->getMobile()) {
$SMS->send(
[$item->getId()],
$registryMGR->get('sms', 'ticketReplay'),
$item->getSubmitter()->getMobile()
);
}
//send notification to user
$settings = $entityManager->getRepository(Settings::class)->findAll()[0];
$url = '/profile/support-view/' . $item->getId();
$notifi->insert("به درخواست پشتیبانی پاسخ داده شد", $url, null, $item->getSubmitter());
return $this->json([
'error' => 0,
'message' => 'successful'
]);
}
return $this->json([
'error' => 999,
'message' => 'تمام موارد لازم را وارد کنید.'
]);
}
#[Route('/api/support/list', name: 'app_support_list')]
public function app_support_list(Jdate $jdate, EntityManagerInterface $entityManager): JsonResponse
{
$items = $entityManager->getRepository(Support::class)->findBy(
[
'submitter' => $this->getUser(),
'main' => 0
],
[
'id' => 'DESC'
]
);
foreach ($items as $item) {
$item->setDateSubmit($jdate->jdate('Y/n/d', $item->getDateSubmit()));
}
return $this->json($items);
}
#[Route('/api/support/mod/{id}', name: 'app_support_mod')]
public function app_support_mod(registryMGR $registryMGR, SMS $SMS, Request $request, EntityManagerInterface $entityManager, string $id = ''): JsonResponse
{
$params = [];
if ($content = $request->getContent()) {
$params = json_decode($content, true);
}
if ($id == '') {
if (array_key_exists('title', $params) && array_key_exists('body', $params)) {
$item = new Support();
$item->setBody($params['body']);
$item->setTitle($params['title']);
$item->setDateSubmit(time());
$item->setSubmitter($this->getUser());
$item->setMain(0);
$item->setCode($this->RandomString(8));
$item->setState('در حال پیگیری');
$entityManager->persist($item);
$entityManager->flush();
//send sms to manager
$SMS->send(
[$item->getId()],
$registryMGR->get('sms', 'ticketRec'),
$registryMGR->get('ticket', 'managerMobile')
);
return $this->json([
'error' => 0,
'message' => 'ok',
'url' => $item->getId()
]);
}
} else {
if (array_key_exists('body', $params)) {
$item = new Support();
$upper = $entityManager->getRepository(Support::class)->find($id);
if ($upper)
$item->setMain($upper->getid());
$item->setBody($params['body']);
$item->setTitle($upper->getTitle());
$item->setDateSubmit(time());
$item->setSubmitter($this->getUser());
$item->setState('در حال پیگیری');
$entityManager->persist($item);
$entityManager->flush();
$upper->setState('در حال پیگیری');
$entityManager->persist($upper);
$entityManager->flush();
//send sms to manager
$SMS->send(
[$item->getId()],
$registryMGR->get('sms', 'ticketRec'),
$registryMGR->get('ticket', 'managerMobile')
);
return $this->json([
'error' => 0,
'message' => 'ok',
'url' => $item->getId()
]);
}
}
return $this->json([
'error' => 999,
'message' => 'تمام موارد لازم را وارد کنید.'
]);
}
#[Route('/api/support/view/{id}', name: 'app_support_view')]
public function app_support_view(Jdate $jdate, EntityManagerInterface $entityManager, string $id = ''): JsonResponse
{
$item = $entityManager->getRepository(Support::class)->find($id);
if (!$item)
throw $this->createNotFoundException();
if ($item->getSubmitter() != $this->getUser())
throw $this->createAccessDeniedException();
$replays = $entityManager->getRepository(Support::class)->findBy(['main' => $item->getId()]);
$replaysArray = [];
foreach ($replays as $replay) {
$replaysArray[] = Explore::ExploreSupportTicket($replay, $this->getUser());
}
return $this->json([
'item' => Explore::ExploreSupportTicket($item, $this->getUser()),
'replays' => $replaysArray
]);
}
}

View file

@ -0,0 +1,129 @@
<?php
namespace App\Entity;
use App\Repository\SupportRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Ignore;
#[ORM\Entity(repositoryClass: SupportRepository::class)]
class Support
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $main = null;
#[ORM\ManyToOne(inversedBy: 'supports')]
#[ORM\JoinColumn(nullable: false)]
#[Ignore]
private ?User $submitter = null;
#[ORM\Column(length: 255)]
private ?string $dateSubmit = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $body = null;
#[ORM\Column(length: 255)]
private ?string $state = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $code = null;
public function getId(): ?int
{
return $this->id;
}
public function getMain(): ?string
{
return $this->main;
}
public function setMain(?string $main): self
{
$this->main = $main;
return $this;
}
public function getSubmitter(): ?User
{
return $this->submitter;
}
public function setSubmitter(?User $submitter): self
{
$this->submitter = $submitter;
return $this;
}
public function getDateSubmit(): ?string
{
return $this->dateSubmit;
}
public function setDateSubmit(string $dateSubmit): self
{
$this->dateSubmit = $dateSubmit;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getBody(): ?string
{
return $this->body;
}
public function setBody(string $body): self
{
$this->body = $body;
return $this;
}
public function getState(): ?string
{
return $this->state;
}
public function setState(string $state): self
{
$this->state = $state;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): static
{
$this->code = $code;
return $this;
}
}

View file

@ -41,9 +41,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Business::class, orphanRemoval: true)] #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Business::class, orphanRemoval: true)]
private Collection $businesses; private Collection $businesses;
#[ORM\OneToMany(mappedBy: 'submitter', targetEntity: StackContent::class, orphanRemoval: true)]
private Collection $stackContents;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Log::class)] #[ORM\OneToMany(mappedBy: 'user', targetEntity: Log::class)]
private Collection $logs; private Collection $logs;
@ -114,7 +111,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
{ {
$this->userTokens = new ArrayCollection(); $this->userTokens = new ArrayCollection();
$this->businesses = new ArrayCollection(); $this->businesses = new ArrayCollection();
$this->stackContents = new ArrayCollection();
$this->logs = new ArrayCollection(); $this->logs = new ArrayCollection();
$this->permissions = new ArrayCollection(); $this->permissions = new ArrayCollection();
$this->hesabdariDocs = new ArrayCollection(); $this->hesabdariDocs = new ArrayCollection();
@ -288,36 +284,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this; return $this;
} }
/**
* @return Collection<int, StackContent>
*/
public function getStackContents(): Collection
{
return $this->stackContents;
}
public function addStackContent(StackContent $stackContent): self
{
if (!$this->stackContents->contains($stackContent)) {
$this->stackContents->add($stackContent);
$stackContent->setSubmitter($this);
}
return $this;
}
public function removeStackContent(StackContent $stackContent): self
{
if ($this->stackContents->removeElement($stackContent)) {
// set the owning side to null (unless already changed)
if ($stackContent->getSubmitter() === $this) {
$stackContent->setSubmitter(null);
}
}
return $this;
}
/** /**
* @return Collection<int, Log> * @return Collection<int, Log>
*/ */

View file

@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Support;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Support>
*
* @method Support|null find($id, $lockMode = null, $lockVersion = null)
* @method Support|null findOneBy(array $criteria, array $orderBy = null)
* @method Support[] findAll()
* @method Support[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class SupportRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Support::class);
}
public function save(Support $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Support $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Support[] Returns an array of Support objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('s.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Support
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View file

@ -18,7 +18,7 @@ class SMS
/** /**
* @param EntityManagerInterface $entityManager * @param EntityManagerInterface $entityManager
*/ */
public function __construct(EntityManagerInterface $entityManager,registryMGR $registryMGR) public function __construct(EntityManagerInterface $entityManager, registryMGR $registryMGR)
{ {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->registryMGR = $registryMGR; $this->registryMGR = $registryMGR;
@ -26,54 +26,67 @@ class SMS
} }
public function send(array $params,$bodyID,$to): void public function send(array $params, $bodyID, $to): void
{ {
if($this->registryMGR->get('sms','plan') == 'melipayamak'){ if ($this->registryMGR->get('sms', 'plan') == 'melipayamak') {
try{ try {
$username = $this->registryMGR->get('sms','username'); $username = $this->registryMGR->get('sms', 'username');
$password = $this->registryMGR->get('sms','password'); $password = $this->registryMGR->get('sms', 'password');
$api = new MelipayamakApi($username,$password); $api = new MelipayamakApi($username, $password);
$sms = $api->sms('soap'); $sms = $api->sms('soap');
$response = $sms->sendByBaseNumber($params,$to,$bodyID); $response = $sms->sendByBaseNumber($params, $to, $bodyID);
$json = json_decode($response); $json = json_decode($response);
}catch(\Exception $e){
echo $e->getMessage();
die();
}
} } catch (\Exception $e) {
elseif($this->registryMGR->get('sms','plan') == 'idepayam'){ echo $e->getMessage();
die();
}
} elseif ($this->registryMGR->get('sms', 'plan') == 'idepayam') {
ini_set("soap.wsdl_cache_enabled", "0"); ini_set("soap.wsdl_cache_enabled", "0");
//create next //create next
$pt = []; $pt = [];
foreach($params as $param){ foreach ($params as $param) {
$pt['{' . strval(array_search($param,$params)) . '}'] = $param; $pt['{' . strval(array_search($param, $params)) . '}'] = $param;
} }
$soap = new \SoapClient("http://185.112.33.61/wbs/send.php?wsdl"); $soap = new \SoapClient("http://185.112.33.61/wbs/send.php?wsdl");
$soap->token = $this->registryMGR->get('sms','token'); $soap->token = $this->registryMGR->get('sms', 'token');
$soap->fromNum = $this->registryMGR->get('sms','fromNum'); $soap->fromNum = $this->registryMGR->get('sms', 'fromNum');
$soap->toNum = array($to); $soap->toNum = array($to);
$soap->patternID = $bodyID; $soap->patternID = $bodyID;
$soap->Content = json_encode($pt,JSON_UNESCAPED_UNICODE); $soap->Content = json_encode($pt, JSON_UNESCAPED_UNICODE);
$soap->Type = 0; $soap->Type = 0;
$array = $soap->SendSMSByPattern($soap->fromNum, $soap->toNum, $soap->Content, $soap->patternID, $soap->Type, $soap->token); $array = $soap->SendSMSByPattern($soap->fromNum, $soap->toNum, $soap->Content, $soap->patternID, $soap->Type, $soap->token);
} elseif ($this->registryMGR->get('sms', 'plan') == 'ippanel') {
$toArray = [$to];
$username = $this->registryMGR->get('sms', 'username');
$password = $this->registryMGR->get('sms', 'password');
$from = $this->registryMGR->get('sms', 'fromNum');
$input_data = [];
foreach ($params as $param) {
$input_data['%' . strval(array_search($param, $params)) . '%'] = $param;
}
$url = "https://ippanel.com/patterns/pattern?username=" . $username . "&password=" . urlencode($password) . "&from=$from&to=" . json_encode($toArray) . "&input_data=" . urlencode(json_encode($input_data)) . "&pattern_code=$bodyID";
$handler = curl_init($url);
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $input_data);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handler);
} }
} }
public function sendByBalance(array $params,$bodyID,$to,Business $business,User $user,$balance = 500): int public function sendByBalance(array $params, $bodyID, $to, Business $business, User $user, $balance = 500): int
{ {
if($business->getSmsCharge() < ($balance * $this->smsPrice)) if ($business->getSmsCharge() < ($balance * $this->smsPrice))
return 2; return 2;
$this->send($params,$bodyID,$to); $this->send($params, $bodyID, $to);
$business->setSmsCharge($business->getSmsCharge() - ($balance * $this->smsPrice)); $business->setSmsCharge($business->getSmsCharge() - ($balance * $this->smsPrice));
$this->entityManager->persist($business); $this->entityManager->persist($business);
$this->entityManager->flush(); $this->entityManager->flush();
//save logs //save logs
$log = new \App\Entity\Log(); $log = new \App\Entity\Log();
$log->setBid($business); $log->setBid($business);
$log->setDateSubmit(time()); $log->setDateSubmit(time());
@ -82,7 +95,7 @@ class SMS
$log->setDes('ارسال پیامک به طول ' . $balance . ' پیامک به شماره ' . $to . ' با شماره الگو ' . $bodyID . ' هزینه: ' . ($this->smsPrice * $balance) . ' ریال '); $log->setDes('ارسال پیامک به طول ' . $balance . ' پیامک به شماره ' . $to . ' با شماره الگو ' . $bodyID . ' هزینه: ' . ($this->smsPrice * $balance) . ' ریال ');
$this->entityManager->persist($log); $this->entityManager->persist($log);
$this->entityManager->flush(); $this->entityManager->flush();
return 1; return 1;
} }
} }