progress in multi pay gateways

This commit is contained in:
Hesabix 2025-02-08 01:27:57 +00:00
parent 474ec05a53
commit af3d3a9c06
5 changed files with 412 additions and 402 deletions

View file

@ -340,23 +340,25 @@ class AdminController extends AbstractController
}
#[Route('/api/admin/settings/system/info', name: 'admin_settings_system_info')]
public function admin_settings_system_info(Jdate $jdate, #[CurrentUser] ?User $user, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Request $request): Response
public function admin_settings_system_info(registryMGR $registryMGR, Jdate $jdate, #[CurrentUser] ?User $user, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Request $request): Response
{
$item = $entityManager->getRepository(Settings::class)->findAll()[0];
$resp = [];
$resp['keywords'] = $item->getSiteKeywords();
$resp['description'] = $item->getDiscription();
$resp['scripts'] = $item->getScripts();
$resp['zarinpal'] = $item->getZarinpalMerchant();
$resp['zarinpal'] = $registryMGR->get('system', key: 'zarinpalKey');;
$resp['footerScripts'] = $item->getFooterScripts();
$resp['appSite'] = $item->getAppSite();
$resp['footer'] = $item->getFooter();
$resp['activeGateway'] = $registryMGR->get('system', key: 'activeGateway');
$resp['parsianGatewayAPI'] = $registryMGR->get('system', key: 'parsianGatewayAPI');
return $this->json($resp);
}
#[Route('/api/admin/settings/system/info/save', name: 'admin_settings_system_info_save')]
public function admin_settings_system_info_save(EntityManagerInterface $entityManager, Request $request): Response
public function admin_settings_system_info_save(registryMGR $registryMGR, EntityManagerInterface $entityManager, Request $request): Response
{
$params = [];
if ($content = $request->getContent()) {
@ -367,10 +369,12 @@ class AdminController extends AbstractController
$item->setSiteKeywords($params['keywords']);
$item->setDiscription($params['description']);
$item->setScripts($params['scripts']);
$item->setZarinpalMerchant($params['zarinpal']);
$registryMGR->update('system', 'zarinpalKey', $params['zarinpal']);
$item->setFooterScripts($params['footerScripts']);
$item->setAppSite($params['appSite']);
$item->setFooter($params['footer']);
$registryMGR->update('system', 'activeGateway', $params['activeGateway']);
$registryMGR->update('system', 'parsianGatewayAPI', $params['parsianGatewayAPI']);
$entityManager->persist($item);
$entityManager->flush();
return $this->json(['result' => 1]);

View file

@ -9,6 +9,7 @@ use App\Service\Access;
use App\Service\Jdate;
use App\Service\Log;
use App\Service\Notification;
use App\Service\PayMGR;
use App\Service\Provider;
use App\Service\twigFunctions;
use Doctrine\ORM\EntityManagerInterface;
@ -25,7 +26,8 @@ use Symfony\Component\String\Slugger\SluggerInterface;
class ArchiveController extends AbstractController
{
private function getArchiveInfo(EntityManagerInterface $entityManager,array $acc){
private function getArchiveInfo(EntityManagerInterface $entityManager, array $acc)
{
$orders = $entityManager->getRepository(ArchiveOrders::class)->findBy([
'bid' => $acc['bid'],
'status' => 100
@ -68,7 +70,7 @@ class ArchiveController extends AbstractController
}
#[Route('/api/archive/order/submit', name: 'app_archive_order_submit')]
public function app_archive_order_submit(twigFunctions $functions,Request $request,Access $access,Log $log,EntityManagerInterface $entityManager,$code = 0): JsonResponse
public function app_archive_order_submit(PayMGR $payMGR, twigFunctions $functions, Request $request, Access $access, Log $log, EntityManagerInterface $entityManager, $code = 0): JsonResponse
{
$acc = $access->hasRole('join');
if (!$acc)
@ -77,97 +79,42 @@ class ArchiveController extends AbstractController
if ($content = $request->getContent()) {
$params = json_decode($content, true);
}
$settings = $functions->systemSettings();
$order = new ArchiveOrders();
$order->setBid($acc['bid']);
$order->setSubmitter($this->getUser());
$order->setDateSubmit(time());
$order->setGatePay('zarinpal');
$order->setDes('خرید سرویس فضای ابری به مقدار ' . $params['space'] . ' گیگابایت به مدت ' . $params['month'] . ' ماه ');
$settings = $functions->systemSettings();
if(array_key_exists('space',$params) && array_key_exists('month',$params)){
$order->setPrice($params['space'] * $params['month'] * $settings->getStoragePrice());
$order->setDes('خرید سرویس فضای ابری به مقدار ' . $params['space'] . ' گیگابایت به مدت ' . $params['month'] . ' ماه ');
$order->setOrderSize($params['space']);
$order->setMonth($params['month']);
}
else
throw $this->createAccessDeniedException();
$data = array("merchant_id" => $settings->getZarinpalMerchant(),
"amount" => $order->getPrice(),
"callback_url" => $this->generateUrl('api_archive_buy_verify',[],UrlGeneratorInterface::ABSOLUTE_URL),
"description" => 'خرید سرویس فضای ابری',
);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);
if ($err) {
throw $this->createAccessDeniedException($err);
} else {
if (empty($result['errors'])) {
if ($result['data']['code'] == 100) {
$order->setStatus(0);
$order->setVerifyCode($result['data']['authority']);
$entityManager->persist($order);
$entityManager->flush();
$result = $payMGR->createRequest($order->getPrice(), $this->generateUrl('api_archive_buy_verify', ["id"=>$order->getId()], UrlGeneratorInterface::ABSOLUTE_URL), 'خرید فضای ابری');
if ($result['Success']) {
$order->setGatePay($result['gate']);
$entityManager->persist($order);
$entityManager->flush();
$log->insert('سرویس فضای ابری', 'صدور فاکتور سرویس فضای ابری به مقدار ' . $params['space'] . ' گیگابایت به مدت ' . $params['month'] . ' ماه ', $this->getUser(), $acc['bid']);
return $this->json([
'authority' => $result['data']["authority"]
]);
}
}
}
throw $this->createAccessDeniedException();
return $this->json($result);
}
#[Route('/api/archive/buy/verify', name: 'api_archive_buy_verify')]
public function api_archive_buy_verify(twigFunctions $functions,Notification $notification,Request $request,EntityManagerInterface $entityManager,Log $log): Response
#[Route('/api/archive/buy/verify/{id}', name: 'api_archive_buy_verify')]
public function api_archive_buy_verify(string $id, PayMGR $payMGR, twigFunctions $functions, Notification $notification, Request $request, EntityManagerInterface $entityManager, Log $log): Response
{
$Authority = $request->get('Authority');
$req = $entityManager->getRepository(ArchiveOrders::class)->findOneBy(['verifyCode'=>$Authority]);
//get system settings
$settings = $functions->systemSettings();
$data = array("merchant_id" => $settings->getZarinpalMerchant(), "authority" => $Authority, "amount" => $req->getPrice());
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$req = $entityManager->getRepository(ArchiveOrders::class)->find($id);
if (!$req)
throw $this->createNotFoundException('');
$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);
//-----------------------------------
//-----------------------------------
if ($err) {
$res = $payMGR->verify($req->getPrice(), $id, $request);
if ($res['Success'] == false) {
$log->insert('سرویس فضای ابری', 'پرداخت ناموفق سرویس فضای ابری', $this->getUser(), $req->getBid());
return $this->render('buy/fail.html.twig', ['results'=>$result]);
return $this->render('buy/fail.html.twig', ['results' => $res]);
} else {
if(array_key_exists('code',$result['data'])){
if ($result['data']['code'] == 100) {
$req->setStatus(100);
$req->setRefID($result['data']['ref_id']);
$req->setCardPan($result['data']['card_pan']);
$req->setRefID($res['refID']);
$req->setCardPan($res['card_pan']);
$req->setExpireDate(time() + ($req->getMonth() * 30 * 24 * 60 * 60));
$entityManager->persist($req);
$entityManager->flush();
@ -181,11 +128,6 @@ class ArchiveController extends AbstractController
return $this->render('buy/success.html.twig', ['req' => $req]);
}
}
$notification->insert('پرداخت فاکتور فضای ابری ناموفق بود','/',$req->getBid(),$req->getSubmitter());
$log->insert('سرویس پیامک','پرداخت ناموفق فاکتور فضای ابری' ,$this->getUser(),$req->getBid());
return $this->render('buy/fail.html.twig', ['results'=>$result]);
}
}
#[Route('/api/archive/list/{cat}', name: 'app_archive_list')]
public function app_archive_list(string $cat, Jdate $jdate, Provider $provider, Request $request, Access $access, Log $log, EntityManagerInterface $entityManager, $code = 0): JsonResponse
@ -253,7 +195,8 @@ class ArchiveController extends AbstractController
$uploadedFile->move(
$this->getParameter('archiveTempMediaDir'),
$newFilename
);} catch (FileException $e) {
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
return $this->json("error");
}

View file

@ -9,9 +9,12 @@ use App\Entity\Settings;
use App\Service\Access;
use App\Service\Jdate;
use App\Service\Log;
use App\Service\PayMGR;
use App\Service\twigFunctions;
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\Routing\Generator\UrlGeneratorInterface;
@ -19,7 +22,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PluginController extends AbstractController
{
#[Route('/api/plugin/get/info/{id}', name: 'api_plugin_get_info')]
public function api_plugin_get_info(String $id,Access $access, Jdate $jdate, EntityManagerInterface $entityManager,Log $log): JsonResponse
public function api_plugin_get_info(string $id, Access $access, Jdate $jdate, EntityManagerInterface $entityManager, Log $log): JsonResponse
{
$acc = $access->hasRole('join');
if (!$acc)
@ -31,7 +34,7 @@ class PluginController extends AbstractController
}
#[Route('/api/plugin/insert/{id}', name: 'api_plugin_insert')]
public function api_plugin_insert(String $id,Access $access,EntityManagerInterface $entityManager): Response
public function api_plugin_insert(string $id, Log $log, twigFunctions $twigFunctions, PayMGR $payMGR, Access $access, EntityManagerInterface $entityManager): Response
{
$acc = $access->hasRole('join');
if (!$acc)
@ -40,38 +43,11 @@ class PluginController extends AbstractController
if (!$pp)
throw $this->createNotFoundException('plugin not found');
//get system settings
$settings = $entityManager->getRepository(Settings::class)->findAll()[0];
$data = array("merchant_id" => $settings->getZarinpalMerchant(),
"amount" => ($pp->getPrice() * 109)/10,
"callback_url" => $this->generateUrl('api_plugin_buy_verify',[],UrlGeneratorInterface::ABSOLUTE_URL),
"description" => $pp->getName(),
);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);
if ($err) {
throw $this->createAccessDeniedException($err);
} else {
if (empty($result['errors'])) {
if ($result['data']['code'] == 100) {
$settings = $twigFunctions->systemSettings();
$plugin = new Plugin();
$plugin->setBid($acc['bid']);
$plugin->setSubmitter($this->getUser());
$plugin->setDateSubmit(time());
$plugin->setGatePay('zarinpal');
$plugin->setVerifyCode($result['data']['authority']);
$plugin->setStatus(0);
$plugin->setDes($pp->getName());
$plugin->setName($pp->getCode());
@ -79,51 +55,36 @@ class PluginController extends AbstractController
$plugin->setDateExpire(time() + $pp->getTimestamp());
$entityManager->persist($plugin);
$entityManager->flush();
return $this->json([
'authority'=> $result['data']["authority"]
]);
$result = $payMGR->createRequest(($pp->getPrice() * 109) / 10, $this->generateUrl('api_plugin_buy_verify', ['id' => $plugin->getId()], UrlGeneratorInterface::ABSOLUTE_URL), 'خرید فضای ابری');
if ($result['Success']) {
$plugin->setGatePay($result['gate']);
$plugin->setVerifyCode($result['authkey']);
$entityManager->persist($plugin);
$entityManager->flush();
$entityManager->persist($plugin);
$entityManager->flush();
$log->insert('بازار افزونه‌ها', 'صدور فاکتور افزونه ' . $pp->getName(), $this->getUser(), $acc['bid']);
}
} else {
throw $this->createAccessDeniedException();
return $this->json($result);
}
}
}
#[Route('/api/plugin/buy/verify', name: 'api_plugin_buy_verify')]
public function api_plugin_buy_verify(\Symfony\Component\HttpFoundation\Request $request,EntityManagerInterface $entityManager,Log $log): Response
#[Route('/api/plugin/buy/verify/{id}', name: 'api_plugin_buy_verify')]
public function api_plugin_buy_verify(string $id, twigFunctions $twigFunctions, PayMGR $payMGR, Request $request, EntityManagerInterface $entityManager, Log $log): Response
{
$Authority = $request->get('Authority');
$status = $request->get('Status');
$req = $entityManager->getRepository(Plugin::class)->findOneBy(['verifyCode'=>$Authority]);
//get system settings
$settings = $entityManager->getRepository(Settings::class)->findAll()[0];
$data = array("merchant_id" => $settings->getZarinpalMerchant(), "authority" => $Authority, "amount" => $req->getPrice());
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);
//-----------------------------------
//-----------------------------------
if ($err) {
return $this->render('buy/fail.html.twig', ['results'=>$result]);
$req = $entityManager->getRepository(Plugin::class)->find($id);
$res = $payMGR->verify($req->getPrice(), $id, $request);
if ($res['Success'] == false) {
$log->insert(
'بازار افزونه‌ها' . $req->getName(),
'پرداخت ناموفق صورت‌حساب خرید افزونه',
$req->getSubmitter(),
$req->getBid()
);
return $this->render('buy/fail.html.twig', ['results' => $res]);
} else {
if(array_key_exists('code',$result['data'])){
if ($result['data']['code'] == 100) {
$req->setStatus(100);
$req->setRefID($result['data']['ref_id']);
$req->setCardPan($result['data']['card_pan']);
$req->setRefID($res['refID']);
$req->setCardPan($res['card_pan']);
$entityManager->persist($req);
$entityManager->flush();
$log->insert(
@ -135,9 +96,6 @@ class PluginController extends AbstractController
return $this->render('buy/success.html.twig', ['req' => $req]);
}
}
return $this->render('buy/fail.html.twig', ['results'=>$result]);
}
}
#[Route('/api/plugin/get/actives', name: 'api_plugin_get_actives')]
public function api_plugin_get_actives(Access $access, Jdate $jdate, EntityManagerInterface $entityManager, Log $log): JsonResponse

View file

@ -11,10 +11,12 @@ use App\Service\Access;
use App\Service\Jdate;
use App\Service\Log;
use App\Service\Notification;
use App\Service\PayMGR;
use App\Service\PluginService;
use App\Service\Provider;
use App\Service\registryMGR;
use App\Service\SMS;
use App\Service\twigFunctions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -104,7 +106,7 @@ class SMSController extends AbstractController
}
#[Route('/api/sms/charge', name: 'api_sms_charge')]
public function api_sms_charge(Log $log, Notification $notification, Request $request, Access $access, EntityManagerInterface $entityManager): JsonResponse
public function api_sms_charge(PayMGR $payMGR, Log $log, registryMGR $registryMGR, Request $request, Access $access, EntityManagerInterface $entityManager): JsonResponse
{
$acc = $access->hasRole('owner');
if (!$acc)
@ -116,34 +118,6 @@ class SMSController extends AbstractController
if (!array_key_exists('price', $params))
throw $this->createAccessDeniedException('price not set');
//get system settings
$settings = $entityManager->getRepository(Settings::class)->findAll()[0];
$data = array(
"merchant_id" => $settings->getZarinpalMerchant(),
"amount" => $params['price'],
"callback_url" => $this->generateUrl('api_sms_buy_verify', [], UrlGeneratorInterface::ABSOLUTE_URL),
"description" => 'افزایش اعتبار سرویس پیامک',
);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);
if ($err) {
throw $this->createAccessDeniedException($err);
} else {
if (empty($result['errors'])) {
if ($result['data']['code'] == 100) {
$smsPay = new SMSPays();
$smsPay->setBid($acc['bid']);
$smsPay->setDateSubmit(time());
@ -151,57 +125,32 @@ class SMSController extends AbstractController
$smsPay->setDes('افزایش اعتبار سرویس پیامک');
$smsPay->setPrice($params['price']);
$smsPay->setStatus(0);
$smsPay->setVerifyCode($result['data']['authority']);
$smsPay->setGatePay('zarinpal');
$entityManager->persist($smsPay);
$entityManager->flush();
$result = $payMGR->createRequest($params['price'], $this->generateUrl('api_sms_buy_verify', ['id' => $smsPay->getId()], UrlGeneratorInterface::ABSOLUTE_URL), 'افزایش اعتبار سرویس پیامک');
if ($result['Success']) {
$smsPay->setVerifyCode($result['authkey']);
$smsPay->setGatePay($result['gate']);
$entityManager->persist($smsPay);
$entityManager->flush();
$log->insert('سرویس پیامک', 'صدور فاکتور شارژ سرویس پیامک', $this->getUser(), $acc['bid']);
return $this->json([
'authority' => $result['data']["authority"]
]);
}
}
}
throw $this->createAccessDeniedException();
return $this->json($result);
}
#[Route('/api/sms/buy/verify', name: 'api_sms_buy_verify')]
public function api_sms_buy_verify(Notification $notification, Request $request, EntityManagerInterface $entityManager, Log $log): Response
#[Route('/api/sms/buy/verify/{id}', name: 'api_sms_buy_verify')]
public function api_sms_buy_verify(string $id, PayMGR $payMGR, twigFunctions $twigFunctions, Notification $notification, Request $request, EntityManagerInterface $entityManager, Log $log): Response
{
$Authority = $request->get('Authority');
$status = $request->get('Status');
$req = $entityManager->getRepository(SMSPays::class)->findOneBy(['verifyCode' => $Authority]);
//get system settings
$settings = $entityManager->getRepository(Settings::class)->findAll()[0];
$data = array("merchant_id" => $settings->getZarinpalMerchant(), "authority" => $Authority, "amount" => $req->getPrice());
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);
//-----------------------------------
//-----------------------------------
if ($err) {
$req = $entityManager->getRepository(SMSPays::class)->find($id);
$res = $payMGR->verify($req->getPrice(), $id, $request);
if ($res['Success'] == false) {
$log->insert('سرویس پیامک', 'پرداخت ناموفق شارژ سرویس پیامک', $this->getUser(), $req->getBid());
return $this->render('buy/fail.html.twig', ['results' => $result]);
return $this->render('buy/fail.html.twig', ['results' => $res]);
} else {
if (array_key_exists('code', $result['data'])) {
if ($result['data']['code'] == 100) {
$req->setStatus(100);
$req->setRefID($result['data']['ref_id']);
$req->setCardPan($result['data']['card_pan']);
$req->setRefID($res['refID']);
$req->setCardPan($res['card_pan']);
$req->getBid()->setSmsCharge($req->getBid()->getSmsCharge() + ($req->getPrice() / 1.09));
$entityManager->persist($req);
$entityManager->flush();
@ -215,11 +164,6 @@ class SMSController extends AbstractController
return $this->render('buy/success.html.twig', ['req' => $req]);
}
}
$notification->insert('پرداخت فاکتور شارژ سرویس پیامک ناموفق بود', '/', $req->getBid(), $req->getSubmitter());
$log->insert('سرویس پیامک', 'پرداخت ناموفق شارژ سرویس پیامک', $this->getUser(), $req->getBid());
return $this->render('buy/fail.html.twig', ['results' => $result]);
}
}
/**
* @throws \ReflectionException

View file

@ -0,0 +1,161 @@
<?php
namespace App\Service;
use App\Entity\APIToken;
use App\Entity\Business;
use App\Entity\Money;
use App\Entity\Permission;
use App\Entity\UserToken;
use App\Entity\Year;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class PayMGR
{
protected Business|string $bid;
function __construct(
private EntityManagerInterface $entityManager,
private registryMGR $registry
) {
}
public function createRequest($price, $callback_url, $des = '', $orderID = 0): array
{
$res = [
'Success' => false,
];
$activeGateway = $this->registry->get('system', 'activeGateway');
if ($activeGateway == 'zarinpal') {
$data = array(
"merchant_id" => $this->registry->get('system', 'zarinpalKey'),
"amount" => $price,
"callback_url" => $callback_url,
"description" => $des,
);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);
if ($err) {
} else {
if (empty($result['errors'])) {
if ($result['data']['code'] == 100) {
$res['code'] = 100;
$res['Success'] = true;
$res['gate'] = 'zarinpal';
$res['message'] = $result['data']['message'];
$res['authkey'] = $result['data']['authority'];
$res['targetURL'] = 'https://www.zarinpal.com/pg/StartPay/' . $result['data']['authority'];
}
}
}
} elseif ($activeGateway == 'pec') {
ini_set("soap.wsdl_cache_enabled", "0");
$url = "https://pec.shaparak.ir/NewIPGServices/Sale/SaleService.asmx?WSDL";
$params = array(
"LoginAccount" => $this->registry->get('system', 'parsianGatewayAPI'),
"Amount" => $price,
"OrderId" => $orderID,
"CallBackUrl" => $callback_url,
"AdditionalData" => '',
"Originator" => ''
);
$client = new \SoapClient($url);
try {
$result = $client->SalePaymentRequest(array(
"requestData" => $params
));
if ($result->SalePaymentRequestResult->Token && $result->SalePaymentRequestResult->Status === 0) {
$res['code'] = 100;
$res['Success'] = true;
$res['gate'] = 'zarinpal';
$res['message'] = 'OK';
$res['authkey'] = $result->SalePaymentRequestResult->Token;
$res['targetURL'] = 'https://pec.shaparak.ir/NewIPG/?Token=' . $result->SalePaymentRequestResult->Token;
}
} catch (\Exception $ex) {
}
}
return $res;
}
public function verify($price,$token, Request $request): array
{
$res = [
'Success' => false
];
$activeGateway = $this->registry->get('system', 'activeGateway');
if ($activeGateway == 'zarinpal') {
$Authority = $request->get('Authority');
$data = array("merchant_id" => $this->registry->get('system', 'zarinpalKey'), "authority" => $Authority, "amount" => $price);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$result = json_decode($result, true);
if ($err) {
return $res;
} else {
if (array_key_exists('code', $result['data'])) {
if ($result['data']['code'] == 100) {
$res['Success'] = true;
$res['status'] = 100;
$res['refID'] = $result['data']['ref_id'];
$res['card_pan'] = $result['data']['card_pan'];
return $res;
}
}
}
} elseif ($activeGateway == 'pec') {
$confirmUrl = 'https://pec.shaparak.ir/NewIPGServices/Confirm/ConfirmService.asmx?WSDL';
$params = array(
"LoginAccount" => $this->registry->get('system', 'parsianGatewayAPI'),
"Token" => $token
);
$client = new \SoapClient($confirmUrl);
$result = $client->ConfirmPayment(array(
"requestData" => $params
));
if ($result->ConfirmPaymentResult->Status == '0') {
$res['Success'] = true;
$res['status'] = 100;
$res['refID'] = $_POST ["RRN"];
$res['card_pan'] = $result->CardNumberMasked;
}
}
return $res;
}
}