<?php
namespace App\Controller\Front;
use App\Entity\Picture;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Masjid;
use App\Entity\Community;
use Stripe\Stripe;
use Stripe\Checkout;
use Stripe\PaymentIntent;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Form\DonateType;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PaymentController extends AbstractController
{
public function getErrorMessages(FormInterface $form)
{
$errors = array();
//this part get global form errors (like csrf token error)
foreach ($form->getErrors() as $error) {
$nom =$error->getCause()->getPropertyPath();
$nom = substr($nom, 9);
$nom = substr($nom, 0, -6);
$errors[$nom] = $error->getMessage();
}
return $errors;
}
/**
* @Route("/donate", name="donate")
*/
public function donate(Request $request): Response
{
$form = $this->createForm(DonateType::class);
$form->handleRequest($request);
if ($request->isMethod('POST')) {
$errors = $this->getErrorMessages($form);
//dd(count($errors));
if(is_array($errors) && count($errors) > 0){
return new JsonResponse(array(
'code' => 400,
'message' => 'error',
'errors' => $errors
),400);
}else{
if ($form->isSubmitted() && $form->isValid()) {
// $gateway = $form->get("gateway")->getData();
$email = $form->get("email")->getData();
$price = $form->get("price")->getData();
$other = $form->get("other")->getData();
if($other != ""){
$prix = (float)$other*100;
}else{
$prix = (float)$price*100;
}
$locale = $request->getLocale();
Stripe::setApiKey('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');
$success_url = $this->generateUrl('payment-success',array(),UrlGeneratorInterface::ABSOLUTE_URL);
$cancel_url = $this->generateUrl('payment-cancel',array(),UrlGeneratorInterface::ABSOLUTE_URL);
$checkout_session = Checkout\Session::create([
'customer_email' => $email,
'submit_type' => 'donate',
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'aed',
'unit_amount' => $prix,
'product_data' => [
'name' => 'Donate',
'images' => ["https://sharjah-masjids.com/images/top-logo2.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $success_url ,
'cancel_url' => $cancel_url,
]);
return new JsonResponse(array('id' => $checkout_session->id));
}
}
}
return $this->render('payment/payment_form.html.twig', [
'form' => $form->createView(),]
);
}
/**
* @Route("/payment/success", name="payment-success")
*/
public function success(): Response
{
return $this->render('payment/success.html.twig', []);
}
/**
* @Route("/payment/cancel" , name="payment-cancel")
*/
public function cancel(): Response
{
return $this->render('payment/cancel.html.twig', []);
}
}