<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Module;
use App\Entity\Client;
use App\Entity\Order;
use App\Service\DolyamiClient;
use GuzzleHttp\Client as HttpClient;
class DolyamiController extends AbstractController
{
/**
* @Route("/payment/create", name="app_dolyami_create", methods={"POST"})
*/
public function create(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$clientId = $request->request->get('clientId');
$payment = json_decode($request->request->get('create'));
file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/log/order2.log', $request->getContent()."\n", FILE_APPEND);
$client = $em->getRepository(Client::class)->findOneByCode($clientId);
$module = $client->getModules()->first();
$order = new Order();
$order->setExternalId($payment->orderId);
$order->setOrderNumber($payment->orderNumber);
$order->setUuid($payment->invoiceUuid);
if(!empty($payment->customer->email)) $order->setEmail($payment->customer->email);
if(!empty($payment->customer->phone)) $order->setPhone($payment->customer->phone);
$name = '';
if(!empty($payment->customer->lastName)) $name .= $payment->customer->lastName;
if(!empty($payment->customer->firstName)) $name .= $payment->customer->firstName;
if(!empty($payment->customer->patronymic)) $name .= $payment->customer->patronymic;
$order->setFio($name);
$order->setReceipt($request->request->get('create'));
$order->setModule($module);
$order->setClient($client);
$order->setAmount($payment->amount);
$em->persist($order);
$order->setStatus('new');
$em->flush();
$dolyami = new DolyamiClient($module->getLogin(), $module->getPassword(), $module->getCert(), $module->getKey());
$result = $dolyami->create($payment, $order->getUuid());
file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/log/dolyami22.log', json_encode($result));
$order->setLink($result->link);
$em->persist($order);
$em->flush();
return new JsonResponse([
'success' => true,
'errorMsg' => '',
'result' => [
'paymentId' => $payment->invoiceUuid,
'invoiceUrl' => $result->link,
'cancellable' => true
]
]);
}
/**
* @Route("/payment/approve", name="app_dolyami_approve", methods={"POST"})
*/
public function approve(Request $request): Response
{
$id = json_decode($request->request->get('approve'))->paymentId;
$em = $this->getDoctrine()->getManager();
$order = $em->getRepository(Order::class)->find($id);
$dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
$result = $dolyami->commit(json_decode($order->getReceipt()), $order->getUuid());
return new JsonResponse([
'success' => true
]);
}
/**
* @Route("/payment/confirm_all", name="app_dolyami_confirm_all", methods={"GET"})
*/
public function confirmAll(Request $request): Response
{
$id = json_decode($request->query->get('number'));
$em = $this->getDoctrine()->getManager();
$orders = $em->getRepository(Order::class)->findByOrderNumber($id);
dump($orders); die;
$dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
foreach($orders as $order){
$result = $dolyami->commit(json_decode($order->getReceipt()), $order->getUuid());
}
return new JsonResponse([
'success' => true
]);
}
/**
* @Route("/payment/cancel", name="app_dolyami_cancel", methods={"POST"})
*/
public function cancel(Request $request): Response
{
$id = json_decode($request->request->get('cancel'))->paymentId;
$em = $this->getDoctrine()->getManager();
$order = $em->getRepository(Order::class)->find($id);
$dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
$result = $dolyami->cancel($order->getUuid());
return new JsonResponse([
'success' => true
]);
}
/**
* @Route("/payment/refund", name="app_dolyami_refund", methods={"POST"})
*/
public function refund(Request $request): Response
{
$id = json_decode($request->request->get('refund'))->paymentId;
$em = $this->getDoctrine()->getManager();
$order = $em->getRepository(Order::class)->find($id);
$dolyami = new DolyamiClient($order->getModule()->getLogin(), $order->getModule()->getPassword(), $order->getModule()->getCert(), $order->getModule()->getKey());
$result = $dolyami->refund(json_decode($order->getReceipt()), $order->getUuid());
$order->setRefundId($result->refund_id);
$order->setRefundAmount(json_decode($order->getReceipt())->amount);
file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/wg.log', json_encode($result)."\n", FILE_APPEND);
$em->persist($order);
$em->flush();
return new JsonResponse([
'success' => true,
'result' => [
'id' => $result->refund_id,
'amount' => json_decode($order->getReceipt())->amount
]
]);
}
/**
* @Route("/webhook", name="app_dolyami_webhook", methods={"POST"})
*/
public function webhook(Request $request): Response
{
file_put_contents('/var/www/dolyami2_qua_usr/data/www/dolyami2.quadruple.dev/var/wg.log', $request->getContent()."\n", FILE_APPEND);
$data = json_decode($request->getContent(), true);
$id = $data['id'];
$em = $this->getDoctrine()->getManager();
$order = $em->getRepository(Order::class)->find($id);
$order->setStatus($data['status']);
$em->persist($order);
$em->flush();
$states = [
//'approved' => 'pending',
'rejected' => 'canceled',
'canceled' => 'canceled',
'committed' => 'succeeded',
'wait_for_commit' => 'waitingForCapture',
'completed' => 'succeeded'
];
if(isset($states[$data['status']])){
$retailClient = new HttpClient([
// Base URI is used with relative requests
'base_uri' => $order->getModule()->getUrl(),
// You can set any number of default request options.
'timeout' => 2.0,
]);
$result = $retailClient->post('/api/v5/payment/update-invoice?apiKey='.$order->getModule()->getApiToken(), [
'form_params' => [
'updateInvoice' => json_encode([
'invoiceUuid' => $order->getUuid(),
'paymentId' => $order->getUuid(),
'amount' => $data['amount'],
'status' => $states[$data['status']],
'invoiceUrl' => $order->getLink(),
'cancellable' => true,
'refundable' => true
])
]
]);
}
return new JsonResponse([
'success' => true
]);
}
}