<?php
namespace App\Controller;
use App\Entity\Payment;
use App\Form\PaymentType;
use App\Repository\PaymentRepository;
use App\Repository\ApplicationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/payment")
*/
class PaymentController extends AbstractController
{
/**
* @Route("/", name="app_payment_index", methods={"GET"})
*/
public function index(PaymentRepository $paymentRepository): Response
{
return $this->render('payment/index.html.twig', [
'payments' => $paymentRepository->findAll(),
]);
}
/**
* @Route("/new", name="app_payment_new", methods={"GET", "POST"})
*/
public function new(Request $request, PaymentRepository $paymentRepository): Response
{
$payment = new Payment();
$form = $this->createForm(PaymentType::class, $payment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$paymentRepository->add($payment, true);
return $this->redirectToRoute('app_payment_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('payment/new.html.twig', [
'payment' => $payment,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_payment_show", methods={"GET"})
*/
public function show(Payment $payment): Response
{
return $this->render('payment/show.html.twig', [
'payment' => $payment,
]);
}
/**
* @Route("/{id}/edit", name="app_payment_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Payment $payment, PaymentRepository $paymentRepository): Response
{
$form = $this->createForm(PaymentType::class, $payment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$paymentRepository->add($payment, true);
return $this->redirectToRoute('app_payment_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('payment/edit.html.twig', [
'payment' => $payment,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_payment_delete", methods={"POST"})
*/
public function delete(Request $request, Payment $payment, PaymentRepository $paymentRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$payment->getId(), $request->request->get('_token'))) {
$paymentRepository->remove($payment, true);
}
return $this->redirectToRoute('app_payment_index', [], Response::HTTP_SEE_OTHER);
}
/**
* Creates a new payment entity.
*
* @Route("/cash/out/user", name="payment_cash_out") *
*
*/
public function cashOutAction(Request $request, PaymentRepository $paymentRepository, ApplicationRepository $applicationRepository)
{
$applicationKey = $request->get('applicationKey');
$amount = $request->get('amount');
$reason = $request->get('reason');
$number = $request->get('number');
$operator = $request->get('operator');// useless
if(!empty($applicationKey) && !empty($amount) && !empty($reason) && !empty($number) && !empty($operator))
{
$app = $applicationRepository->FindOneBy(['applicationPass'=>$applicationKey]);
if(!empty($app) )
{
if($app->getCashOutCumul() < 10000){
$ret = $this->CashInPay($amount, $number, $reason);
$payment = new Payment();
$payment->setOperator($operator);
$payment->setAmount($amount);
$payment->setNumber($number);
$payment->setReason($reason);
$payment->setOperationType("CASHIN");
$payment->setApiResponse($ret);
$payment->setComment($ret['data']['txnmessage']);
$app->setCashOutCumul($app->getCashOutCumul() + $amount);
$paymentRepository->add($payment, true);
$applicationRepository->add($app, true);
$return = array(
'Etat' =>'SUCCESS',
'Message' =>"Dépôt effectué avec succès !!!",
'detail'=>$ret
);
}else
{
$return = array(
'Etat' =>'FAILED',
'Message' =>"Le montant maximal de vos opérations est atteint.",
);
}
}else
{
$return = array(
'Etat' =>'FAILED',
'Message' =>"N'êtes pas autorisé à effectuer cette opération",
'applicationKey' =>$applicationKey,
);
}
}else
{
$return = array(
'Etat' =>'FAILED' ,
'Message' =>'Paramètres manquants',
);
}
$retour = new JsonResponse($return);
return $retour;
}
/**
* Creates a new payment entity.
*
* @Route("/get/orange/user/name", name="payment_get_user_name") *
*
*/
public function getUserNameAction(Request $request, ApplicationRepository $applicationRepository)
{
$applicationKey = $request->get('applicationKey');
$number = $request->get('number');
if(!empty($applicationKey) && !empty($number))
{
$app = $applicationRepository->FindOneBy(['applicationPass'=>$applicationKey]);
if(!empty($app) )
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.wantacm.cesd-sa.com/api/v1.1/TestRefit/OmUserInfo?_phonenumber='.$number,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
$response = json_decode($response,true);
curl_close($curl);
if($response["status"]=="SUCCES"){
$return = array(
'Etat' =>'SUCCESS',
'Message' =>"Vérification de nom effectué avec succès !!!",
'Nom'=>$response['data']
);
}else{
$return = array(
'Etat' =>'FAILED',
'Message' =>"Vérification de nom effectué avec ECHEC !!!",
'Nom'=>"Nom indisponible"
);
}
}else
{
$return = array(
'Etat' =>'FAILED',
'Message' =>"N'êtes pas autorisé à effectuer cette opération",
'applicationKey' =>$applicationKey,
);
}
}else
{
$return = array(
'Etat' =>'FAILED' ,
'Message' =>'Paramètres manquants',
);
}
$retour = new JsonResponse($return);
return $retour;
}
function getAccessToken()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api-s1.orange.cm/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic aGtBcnl1V2ZNSXE3OXFiSjMydUQ2R2Z1YlJrYTpLT1Bfekdpa0lNQWRxWFI0WkJkajhSSjBnWWth',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$response = json_decode($response,true);
curl_close($curl);
$response = $response["access_token"];
return $response;
}
function InitTransaction(){
$accessToken = $this->getAccessToken();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api-s1.orange.cm/omcoreapis/1.0.2/cashin/init',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'X-AUTH-TOKEN: QU1MQVBST0Q6QU1MQVBST0QyMDIw',
'Content-Type: application/json',
'Authorization: Bearer '.$accessToken
),
));
$response = curl_exec($curl);
$response = json_decode($response,true);
curl_close($curl);
$return = array(
'access_token' =>$accessToken ,
'pay_token' =>$response["data"]["payToken"],
);
return $return;
}
function CashInPay($amount, $number, $reason){
$initAndPayToken = $this->InitTransaction();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api-s1.orange.cm/omcoreapis/1.0.2/cashin/pay',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"subscriberMsisdn": "'.$number.'",
"channelUserMsisdn": "691128951",
"amount": "'.$amount.'",
"description": "test",
"orderId": "'.$reason.'",
"pin": "2562",
"payToken": "'.$initAndPayToken['pay_token'].'",
"notifUrl": "https://webhook.site/7ad03c61-5fe1-4436-92e3-61563016efb2"
}',
CURLOPT_HTTPHEADER => array(
'X-AUTH-TOKEN: QU1MQVBST0Q6QU1MQVBST0QyMDIw',
'Content-Type: application/json',
'Authorization: Bearer '.$initAndPayToken['access_token']
),
));
$response = curl_exec($curl);
$response = json_decode($response,true);
curl_close($curl);
return $response;
}
function checkStatus($reference, $token){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api-s1.orange.cm/omcoreapis/1.0.2/cashin/paymentstatus/CI2405107ED3DB2096DD671F3A20',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'X-AUTH-TOKEN: QU1MQVBST0Q6QU1MQVBST0QyMDIw',
'Content-Type: application/json',
'Authorization: Bearer 5610615e-64aa-3d6f-aaeb-b7390d995770'
),
));
$response = curl_exec($curl);
$response = json_decode($response,true);
curl_close($curl);
return $response;
}
}