src/Controller/MainController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ClientPayment;
  4. use App\Entity\Invoice;
  5. use App\Entity\Payment;
  6. use App\Entity\User;
  7. use App\Enum\Payment\Status;
  8. use App\Form\Type\LoginType;
  9. use App\Library\Payment\Tinkoff\TinkoffMerchantAPI;
  10. use App\Service\Machine\MachineService;
  11. use App\Service\ThemeService;
  12. use Doctrine\DBAL\Types\TextType;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Component\VarDumper\VarDumper;
  20. class MainController extends AbstractController
  21. {
  22.     public function index(Request $requestSecurity $security)
  23.     {
  24.         if ($security->getUser()) {
  25.             return $this->redirectToRoute('admin_index');
  26.         }
  27.         return $this->redirectToRoute('login');
  28.         //return $this->render('base.html.twig');
  29.     }
  30.     public function getAddress(Request $requestMachineService $machineService)
  31.     {
  32.         $machine $machineService->getByMachine((int)$request->get('id'0));
  33.         if ($machine) {
  34.             return $this->json(['address' => $machine->getAddress() . ', пост ' $machine->getPostId()]);
  35.         }
  36.         return $this->json(['address' => '']);
  37.     }
  38.     public function reportQr(Request $requestMachineService $machineService)
  39.     {
  40.         return $this->handleQrPayRequest((int)$request->get('id'0), (int)$request->get('amount'0),
  41.             nullnull$machineService);
  42.     }
  43.     private function handleQrPayRequest(int $machineIdint $amount, ?string $userPhone, ?string $orderIdMachineService $machineService)
  44.     {
  45.         $machine $machineService->getByMachine($machineId);
  46.         if ($machine) {
  47.             if ($amount 0) {
  48.                 //save payment, todo: gateway id?
  49.                 $machineService->newRemotePayment(['machine' => $machine'amount' => $amount'userPhone' => $userPhone'orderId' => $orderId]);
  50.                 return $this->json(['status' => 'ok']);
  51.             }
  52.         }
  53.         return $this->json(['status' => 'error']);
  54.     }
  55.     /**
  56.      * Callback оплаты по статическому QR О!Деньги (Кыргызстан)
  57.      */
  58.     public function reportQrOdengi(Request $requestMachineService $machineServiceLoggerInterface $odengiLogger)
  59.     {
  60.         if (getenv('COUNTRY') != 'KG') {
  61.             return new Response(''400);
  62.         }
  63.         $post json_decode($request->getContent(), true);
  64.         $post["fields_other"] = json_decode($post["fields_other"], true);
  65.         $odengiLogger->info($request->getContent());
  66.         if ($post["status_pay"] == 3) { //3 - оплачено
  67.             $machineId = (int)$post["fields_other"]['fields_other_1'];
  68.             $amount = (int)($post["amount"] / 100);
  69.             $phone $post["mobile"] ?? null;
  70.             $orderId $post["invoice_id"] ?? null;
  71.             return $this->handleQrPayRequest($machineId$amount$phone$orderId$machineService);
  72.         }
  73.         return new Response(''400);
  74.     }
  75.     public function login(Request $requestSecurity $security)
  76.     {
  77.         if ($security->getUser()) {
  78.             return $this->redirectToRoute('admin_index');
  79.         }
  80.         $user = new User();
  81.         $loginForm $this->createForm(LoginType::class, $user);
  82.         $loginForm->handleRequest($request);
  83.         $error $request->getSession()->get(Security::AUTHENTICATION_ERROR);
  84.         if ($error) {
  85.             $request->getSession()->set(Security::AUTHENTICATION_ERRORnull);
  86.         }
  87.         if ($error && trim($error) != '') {
  88.             $this->addFlash('errors'$error);
  89.         }
  90.         return $this->render('login.html.twig', [
  91.             'loginForm' => $loginForm->createView(),
  92.         ]);
  93.     }
  94.     public function venstapayReport(Request $request)
  95.     {
  96.         $id = (int)$request->get('id');
  97.         /** @var  $payment */
  98.         $payment $this->getDoctrine()->getManager()->getRepository(Payment::class)->find($id);
  99.         if ($payment && $payment instanceof Payment) {
  100.             $amount $request->get('amount') + 1;
  101.             if ($amount >= $payment->getAmount()) {
  102.                 if ($payment->getStatus() != Status::STATUS_SUCCESS) {
  103.                     $payment->setStatus(Status::STATUS_SUCCESS);
  104.                     $this->getDoctrine()->getManager()->flush();
  105.                     //set payment to invoice
  106.                     /** @var Invoice $invoice */
  107.                     foreach ($payment->getCompany()->getInvoices() as $invoice) {
  108.                         if ($invoice->getStatus() == \App\Enum\Invoice\Status::STATUS_NEW && $payment->getAmount() == $invoice->getAmount()) {
  109.                             $invoice->setStatus(\App\Enum\Invoice\Status::STATUS_PAID);
  110.                             $this->getDoctrine()->getManager()->flush();
  111.                             break;
  112.                         }
  113.                     }
  114.                 }
  115.                 echo 'OK';
  116.                 exit;
  117.             }
  118.         }
  119.     }
  120.     public function robokassaReport(Request $request)
  121.     {
  122.         /*pass1md5lIve
  123.         pa_s2md5liVe
  124.         pas_1md5Dev
  125.         pass2md5dEv*/
  126.         $id = (int)$request->get('InvId');
  127.         /** @var  $payment */
  128.         $payment $this->getDoctrine()->getManager()->getRepository(Payment::class)->find($id);
  129.         if ($payment && $payment instanceof Payment) {
  130.             $amount $request->get('OutSum') + 1;
  131.             if ($amount >= $payment->getAmount()) {
  132.                 ///robokassa-report?out_summ=10.000000&OutSum=10.000000&inv_id=78&InvId=78&crc=6FD7C1766137D7A0ABBB549CB730B428&SignatureValue=6FD7C1766137D7A0ABBB549CB730B428&PaymentMethod=BankCard&IncSum=10.000000&IncCurrLabel=GooglePayPSR&EMail=aleksey@telitsyn.com&Fee=0.290000
  133.                 $sign strtoupper(md5($request->get('OutSum''') . ':' $request->get('InvId''') . ':' 'pa_s2md5liVe'));
  134.                 if ($sign == strtoupper($request->get('SignatureValue'))) {
  135.                     if ($payment->getStatus() != Status::STATUS_SUCCESS) {
  136.                         $payment->setStatus(Status::STATUS_SUCCESS);
  137.                         $this->getDoctrine()->getManager()->flush();
  138.                         //set payment to invoice
  139.                         /** @var Invoice $invoice */
  140.                         foreach ($payment->getCompany()->getInvoices() as $invoice) {
  141.                             if ($invoice->getStatus() == \App\Enum\Invoice\Status::STATUS_NEW && $payment->getAmount() == $invoice->getAmount()) {
  142.                                 $invoice->setStatus(\App\Enum\Invoice\Status::STATUS_PAID);
  143.                                 $this->getDoctrine()->getManager()->flush();
  144.                                 break;
  145.                             }
  146.                         }
  147.                     }
  148.                     echo 'OK' $id;
  149.                     exit;
  150.                 }
  151.             }
  152.         }
  153.         echo 'ERROR' $id;
  154.         exit;
  155.     }
  156.     public function tinkoffReport(Request $request)
  157.     {//todo remove
  158.         $api = new TinkoffMerchantAPI(
  159. //            '1625752046979DEMO',  //Ваш Terminal_Key
  160. //            '2pn431lr67yqp0wl'   //Ваш Secret_Key
  161.             '1625752046979',  //Ваш Terminal_Key
  162.             'ytz81yeopihj8gwm'   //Ваш Secret_Key
  163.         );
  164.         //file_get_contents('https://tpk.uno/T/?/' . urlencode($request->getContent()));
  165.         $payments $this->getDoctrine()->getManager()->getRepository(Payment::class)->getUnpaid();
  166.         try {
  167.             $report json_decode($request->getContent(), true);
  168.         } catch (\Exception $exception) {
  169.             $report = [];
  170.         }
  171.         if (isset($report['PaymentId'])) {
  172.             $tmpPayment $this->getDoctrine()->getManager()->getRepository(Payment::class)->findOneBy(['idGate' => $report['PaymentId']]);
  173.             if ($tmpPayment instanceof Payment) {
  174.                 $payments[] = $tmpPayment;
  175.             }
  176.         }
  177.         //check reports
  178.         /** @var Payment $payment */
  179.         foreach ($payments as $payment) {
  180.             $params = [
  181.                 'PaymentId' => $payment->getIdGate(),
  182.             ];
  183.             $api->getState($params);
  184.             if ($api->status == 'CONFIRMED') {
  185.                 $payment->setStatus(Status::STATUS_SUCCESS);
  186.                 $this->getDoctrine()->getManager()->flush();
  187.             } else if ($api->status == 'REJECTED') {
  188.                 $payment->setStatus(Status::STATUS_ERROR);
  189.                 $this->getDoctrine()->getManager()->flush();
  190.             } else if ($api->status == 'REFUNDED' || $api->status == 'PARTIAL_REFUNDED') {
  191.                 $payment->setStatus(Status::STATUS_NEW);
  192.                 $this->getDoctrine()->getManager()->flush();
  193.             }
  194.         }
  195.         echo 'OK';
  196.         exit;
  197.         //return $this->json(['status' => 'success']);
  198.     }
  199.     public function mobileAppPaymentReport(Request $request)
  200.     {
  201.         $id = (int)$request->get('id');
  202.         /** @var ClientPayment $payment */
  203.         $payment $this->getDoctrine()->getManager()->getRepository(ClientPayment::class)->find($id);
  204.         if ($payment && $payment instanceof ClientPayment) {
  205.             $amount $request->get('amount') + 1;
  206.             if ($amount >= $payment->getAmount()) {
  207.                 if ($payment->getStatus() != \App\Enum\ClientPayment\Status::STATUS_SUCCESS) {
  208.                     $payment->setStatus(\App\Enum\ClientPayment\Status::STATUS_SUCCESS);
  209.                     $this->getDoctrine()->getManager()->flush();
  210.                     $client $payment->getClient();
  211.                     $newClientAmount $client->getAmount() + (int)$request->get('amount');
  212.                     $client->setAmount($newClientAmount);
  213.                     $this->getDoctrine()->getManager()->flush();
  214.                 }
  215.                 echo 'OK';
  216.                 exit;
  217.             }
  218.         }
  219.     }
  220.     public function main()
  221.     {
  222.         return $this->render(ThemeService::getLoginBaseTemplatePath());
  223.     }
  224.     public function admin()
  225.     {
  226.         return $this->json(['erwer' => 'wer']);
  227.     }
  228.     public function logout()
  229.     {
  230.         //
  231.     }
  232. }