src/Controller/DolyamiController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\Module;
  9. use App\Entity\Client;
  10. use App\Entity\Order;
  11. use App\Service\DolyamiClient;
  12. use GuzzleHttp\Client as HttpClient;
  13. class DolyamiController extends AbstractController
  14. {
  15.     
  16.     /**
  17.      * @Route("/payment/create", name="app_dolyami_create", methods={"POST"})
  18.      */
  19.     public function create(Request $request): Response
  20.     {
  21.         $em $this->getDoctrine()->getManager();
  22.         $clientId $request->request->get('clientId');
  23.         $payment json_decode($request->request->get('create'));
  24.         file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/log/order2.log'$request->getContent()."\n"FILE_APPEND);
  25.         $client $em->getRepository(Client::class)->findOneByCode($clientId);
  26.         $module $client->getModules()->first();
  27.         
  28.         $order = new Order();
  29.         $order->setExternalId($payment->orderId);
  30.         $order->setOrderNumber($payment->orderNumber);
  31.         $order->setUuid($payment->invoiceUuid);
  32.         if(!empty($payment->customer->email)) $order->setEmail($payment->customer->email);
  33.         if(!empty($payment->customer->phone)) $order->setPhone($payment->customer->phone);
  34.         $name '';
  35.         if(!empty($payment->customer->lastName)) $name .= $payment->customer->lastName;
  36.         if(!empty($payment->customer->firstName)) $name .= $payment->customer->firstName;
  37.         if(!empty($payment->customer->patronymic)) $name .= $payment->customer->patronymic;
  38.         $order->setFio($name);
  39.         $order->setReceipt($request->request->get('create'));
  40.         $order->setModule($module);
  41.         $order->setClient($client);
  42.         $order->setAmount($payment->amount);
  43.         $em->persist($order);
  44.         $order->setStatus('new');
  45.         $em->flush();
  46.         
  47.         $dolyami = new DolyamiClient($module->getLogin(), $module->getPassword(), $module->getCert(), $module->getKey());
  48.         $result $dolyami->create($payment$order->getUuid());
  49.         file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/log/dolyami22.log'json_encode($result));
  50.         $order->setLink($result->link);
  51.         $em->persist($order);
  52.         $em->flush();
  53.         
  54.         return new JsonResponse([
  55.             'success' => true,
  56.             'errorMsg' => '',
  57.             'result' => [
  58.                 'paymentId' => $payment->invoiceUuid,
  59.                 'invoiceUrl' => $result->link,
  60.                 'cancellable' => true
  61.             ]
  62.         ]);
  63.     }
  64.     
  65.     /**
  66.      * @Route("/payment/approve", name="app_dolyami_approve", methods={"POST"})
  67.      */
  68.     public function approve(Request $request): Response
  69.     {
  70.         $id json_decode($request->request->get('approve'))->paymentId;
  71.         $em $this->getDoctrine()->getManager();
  72.         $order $em->getRepository(Order::class)->find($id);
  73.         
  74.         $dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
  75.         $result $dolyami->commit(json_decode($order->getReceipt()), $order->getUuid());
  76.         
  77.         return new JsonResponse([
  78.             'success' => true
  79.         ]);
  80.     }
  81.     
  82.     /**
  83.      * @Route("/payment/confirm_all", name="app_dolyami_confirm_all", methods={"GET"})
  84.      */
  85.     public function confirmAll(Request $request): Response
  86.     {
  87.         $id json_decode($request->query->get('number'));
  88.         $em $this->getDoctrine()->getManager();
  89.         $orders $em->getRepository(Order::class)->findByOrderNumber($id);
  90.         dump($orders); die;
  91.         $dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
  92.         foreach($orders as $order){
  93.             $result $dolyami->commit(json_decode($order->getReceipt()), $order->getUuid());
  94.         }
  95.         return new JsonResponse([
  96.             'success' => true
  97.         ]);
  98.     }
  99.     
  100.     /**
  101.      * @Route("/payment/cancel", name="app_dolyami_cancel", methods={"POST"})
  102.      */
  103.     public function cancel(Request $request): Response
  104.     {
  105.         $id json_decode($request->request->get('cancel'))->paymentId;
  106.         $em $this->getDoctrine()->getManager();
  107.         $order $em->getRepository(Order::class)->find($id);
  108.         
  109.         $dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
  110.         $result $dolyami->cancel($order->getUuid());
  111.         
  112.         return new JsonResponse([
  113.             'success' => true
  114.         ]);
  115.     }
  116.     
  117.     /**
  118.      * @Route("/payment/refund", name="app_dolyami_refund", methods={"POST"})
  119.      */
  120.     public function refund(Request $request): Response
  121.     {
  122.         $id json_decode($request->request->get('refund'))->paymentId;
  123.         $em $this->getDoctrine()->getManager();
  124.         $order $em->getRepository(Order::class)->find($id);
  125.         
  126.         $dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
  127.         $result $dolyami->refund(json_decode($order->getReceipt()), $order->getUuid());
  128.         
  129.         $order->setRefundId($result->refund_id);
  130.         $order->setRefundAmount(json_decode($order->getReceipt())->amount);
  131.         file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/wg.log'json_encode($result)."\n"FILE_APPEND);
  132.         
  133.         $em->persist($order);
  134.         $em->flush();
  135.         
  136.         return new JsonResponse([
  137.             'success' => true,
  138.             'result' => [
  139.                 'id' => $result->refund_id,
  140.                 'amount' => json_decode($order->getReceipt())->amount
  141.             ]
  142.         ]);
  143.     }
  144.     
  145.     /**
  146.      * @Route("/webhook", name="app_dolyami_webhook", methods={"POST"})
  147.      */
  148.     public function webhook(Request $request): Response
  149.     {
  150.         file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/wg.log'$request->getContent()."\n"FILE_APPEND);
  151.         $data json_decode($request->getContent(), true);
  152.         $id $data['id'];
  153.         $em $this->getDoctrine()->getManager();
  154.         $order $em->getRepository(Order::class)->find($id);
  155.         $order->setStatus($data['status']);
  156.         $em->persist($order);
  157.         $em->flush();
  158.         $states = [
  159.             //'approved' => 'pending',
  160.             'rejected' => 'canceled',
  161.             'canceled' => 'canceled',
  162.             'committed' => 'succeeded',
  163.             'wait_for_commit' => 'waitingForCapture',
  164.             'completed' => 'succeeded'
  165.         ];
  166.         if(isset($states[$data['status']])){
  167.             $retailClient = new HttpClient([
  168.                 // Base URI is used with relative requests
  169.                 'base_uri' => $order->getModule()->getUrl(),
  170.                 // You can set any number of default request options.
  171.                 'timeout'  => 2.0,
  172.             ]);
  173.             $result $retailClient->post('/api/v5/payment/update-invoice?apiKey='.$order->getModule()->getApiToken(), [
  174.                 'form_params' => [
  175.                     'updateInvoice' => json_encode([
  176.                         'invoiceUuid' => $order->getUuid(),
  177.                         'paymentId' => $order->getUuid(),
  178.                         'amount' => $data['amount'],
  179.                         'status' => $states[$data['status']],
  180.                         'invoiceUrl' => $order->getLink(),
  181.                         'cancellable' => true,
  182.                         'refundable' => true
  183.                     ])
  184.                 ]
  185.             ]);
  186.         }
  187.         return new JsonResponse([
  188.             'success' => true
  189.         ]);
  190.     }
  191. }