<?php
namespace App\Controller;
use App\Entity\ClientPayment;
use App\Entity\Invoice;
use App\Entity\Payment;
use App\Entity\User;
use App\Enum\Payment\Status;
use App\Form\Type\LoginType;
use App\Library\Payment\Tinkoff\TinkoffMerchantAPI;
use App\Service\Machine\MachineService;
use App\Service\ThemeService;
use Doctrine\DBAL\Types\TextType;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\VarDumper\VarDumper;
class MainController extends AbstractController
{
public function index(Request $request, Security $security)
{
if ($security->getUser()) {
return $this->redirectToRoute('admin_index');
}
return $this->redirectToRoute('login');
//return $this->render('base.html.twig');
}
public function getAddress(Request $request, MachineService $machineService)
{
$machine = $machineService->getByMachine((int)$request->get('id', 0));
if ($machine) {
return $this->json(['address' => $machine->getAddress() . ', пост ' . $machine->getPostId()]);
}
return $this->json(['address' => '']);
}
public function reportQr(Request $request, MachineService $machineService)
{
return $this->handleQrPayRequest((int)$request->get('id', 0), (int)$request->get('amount', 0),
null, null, $machineService);
}
private function handleQrPayRequest(int $machineId, int $amount, ?string $userPhone, ?string $orderId, MachineService $machineService)
{
$machine = $machineService->getByMachine($machineId);
if ($machine) {
if ($amount > 0) {
//save payment, todo: gateway id?
$machineService->newRemotePayment(['machine' => $machine, 'amount' => $amount, 'userPhone' => $userPhone, 'orderId' => $orderId]);
return $this->json(['status' => 'ok']);
}
}
return $this->json(['status' => 'error']);
}
/**
* Callback оплаты по статическому QR О!Деньги (Кыргызстан)
*/
public function reportQrOdengi(Request $request, MachineService $machineService, LoggerInterface $odengiLogger)
{
if (getenv('COUNTRY') != 'KG') {
return new Response('', 400);
}
$post = json_decode($request->getContent(), true);
$post["fields_other"] = json_decode($post["fields_other"], true);
$odengiLogger->info($request->getContent());
if ($post["status_pay"] == 3) { //3 - оплачено
$machineId = (int)$post["fields_other"]['fields_other_1'];
$amount = (int)($post["amount"] / 100);
$phone = $post["mobile"] ?? null;
$orderId = $post["invoice_id"] ?? null;
return $this->handleQrPayRequest($machineId, $amount, $phone, $orderId, $machineService);
}
return new Response('', 400);
}
public function login(Request $request, Security $security)
{
if ($security->getUser()) {
return $this->redirectToRoute('admin_index');
}
$user = new User();
$loginForm = $this->createForm(LoginType::class, $user);
$loginForm->handleRequest($request);
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR);
if ($error) {
$request->getSession()->set(Security::AUTHENTICATION_ERROR, null);
}
if ($error && trim($error) != '') {
$this->addFlash('errors', $error);
}
return $this->render('login.html.twig', [
'loginForm' => $loginForm->createView(),
]);
}
public function venstapayReport(Request $request)
{
$id = (int)$request->get('id');
/** @var $payment */
$payment = $this->getDoctrine()->getManager()->getRepository(Payment::class)->find($id);
if ($payment && $payment instanceof Payment) {
$amount = $request->get('amount') + 1;
if ($amount >= $payment->getAmount()) {
if ($payment->getStatus() != Status::STATUS_SUCCESS) {
$payment->setStatus(Status::STATUS_SUCCESS);
$this->getDoctrine()->getManager()->flush();
//set payment to invoice
/** @var Invoice $invoice */
foreach ($payment->getCompany()->getInvoices() as $invoice) {
if ($invoice->getStatus() == \App\Enum\Invoice\Status::STATUS_NEW && $payment->getAmount() == $invoice->getAmount()) {
$invoice->setStatus(\App\Enum\Invoice\Status::STATUS_PAID);
$this->getDoctrine()->getManager()->flush();
break;
}
}
}
echo 'OK';
exit;
}
}
}
public function robokassaReport(Request $request)
{
/*pass1md5lIve
pa_s2md5liVe
pas_1md5Dev
pass2md5dEv*/
$id = (int)$request->get('InvId');
/** @var $payment */
$payment = $this->getDoctrine()->getManager()->getRepository(Payment::class)->find($id);
if ($payment && $payment instanceof Payment) {
$amount = $request->get('OutSum') + 1;
if ($amount >= $payment->getAmount()) {
///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
$sign = strtoupper(md5($request->get('OutSum', '') . ':' . $request->get('InvId', '') . ':' . 'pa_s2md5liVe'));
if ($sign == strtoupper($request->get('SignatureValue'))) {
if ($payment->getStatus() != Status::STATUS_SUCCESS) {
$payment->setStatus(Status::STATUS_SUCCESS);
$this->getDoctrine()->getManager()->flush();
//set payment to invoice
/** @var Invoice $invoice */
foreach ($payment->getCompany()->getInvoices() as $invoice) {
if ($invoice->getStatus() == \App\Enum\Invoice\Status::STATUS_NEW && $payment->getAmount() == $invoice->getAmount()) {
$invoice->setStatus(\App\Enum\Invoice\Status::STATUS_PAID);
$this->getDoctrine()->getManager()->flush();
break;
}
}
}
echo 'OK' . $id;
exit;
}
}
}
echo 'ERROR' . $id;
exit;
}
public function tinkoffReport(Request $request)
{//todo remove
$api = new TinkoffMerchantAPI(
// '1625752046979DEMO', //Ваш Terminal_Key
// '2pn431lr67yqp0wl' //Ваш Secret_Key
'1625752046979', //Ваш Terminal_Key
'ytz81yeopihj8gwm' //Ваш Secret_Key
);
//file_get_contents('https://tpk.uno/T/?/' . urlencode($request->getContent()));
$payments = $this->getDoctrine()->getManager()->getRepository(Payment::class)->getUnpaid();
try {
$report = json_decode($request->getContent(), true);
} catch (\Exception $exception) {
$report = [];
}
if (isset($report['PaymentId'])) {
$tmpPayment = $this->getDoctrine()->getManager()->getRepository(Payment::class)->findOneBy(['idGate' => $report['PaymentId']]);
if ($tmpPayment instanceof Payment) {
$payments[] = $tmpPayment;
}
}
//check reports
/** @var Payment $payment */
foreach ($payments as $payment) {
$params = [
'PaymentId' => $payment->getIdGate(),
];
$api->getState($params);
if ($api->status == 'CONFIRMED') {
$payment->setStatus(Status::STATUS_SUCCESS);
$this->getDoctrine()->getManager()->flush();
} else if ($api->status == 'REJECTED') {
$payment->setStatus(Status::STATUS_ERROR);
$this->getDoctrine()->getManager()->flush();
} else if ($api->status == 'REFUNDED' || $api->status == 'PARTIAL_REFUNDED') {
$payment->setStatus(Status::STATUS_NEW);
$this->getDoctrine()->getManager()->flush();
}
}
echo 'OK';
exit;
//return $this->json(['status' => 'success']);
}
public function mobileAppPaymentReport(Request $request)
{
$id = (int)$request->get('id');
/** @var ClientPayment $payment */
$payment = $this->getDoctrine()->getManager()->getRepository(ClientPayment::class)->find($id);
if ($payment && $payment instanceof ClientPayment) {
$amount = $request->get('amount') + 1;
if ($amount >= $payment->getAmount()) {
if ($payment->getStatus() != \App\Enum\ClientPayment\Status::STATUS_SUCCESS) {
$payment->setStatus(\App\Enum\ClientPayment\Status::STATUS_SUCCESS);
$this->getDoctrine()->getManager()->flush();
$client = $payment->getClient();
$newClientAmount = $client->getAmount() + (int)$request->get('amount');
$client->setAmount($newClientAmount);
$this->getDoctrine()->getManager()->flush();
}
echo 'OK';
exit;
}
}
}
public function main()
{
return $this->render(ThemeService::getLoginBaseTemplatePath());
}
public function admin()
{
return $this->json(['erwer' => 'wer']);
}
public function logout()
{
//
}
}