src/Controller/Front/PaymentController.php line 124

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Picture;
  4. use App\Entity\User;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Entity\Masjid;
  10. use App\Entity\Community;
  11. use Stripe\Stripe;
  12. use Stripe\Checkout;
  13. use Stripe\PaymentIntent;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use App\Form\DonateType;
  16. use Symfony\Component\Validator\Validation;
  17. use Symfony\Component\Form\FormInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. class PaymentController extends AbstractController
  20. {
  21.     public function getErrorMessages(FormInterface $form)
  22.     {
  23.         $errors = array();
  24.         //this part get global form errors (like csrf token error)
  25.         foreach ($form->getErrors() as $error) {
  26.             $nom =$error->getCause()->getPropertyPath();
  27.             $nom substr($nom9);
  28.             $nom substr($nom0, -6);
  29.             $errors[$nom] = $error->getMessage();
  30.         }
  31.         return $errors;
  32.     }
  33.     /**
  34.     * @Route("/donate", name="donate")
  35.     */
  36.     public function donate(Request $request): Response
  37.     {
  38.         $form $this->createForm(DonateType::class);
  39.         $form->handleRequest($request);
  40.         if ($request->isMethod('POST')) {
  41.             $errors $this->getErrorMessages($form);
  42.            
  43.             //dd(count($errors));
  44.             if(is_array($errors) && count($errors) > 0){
  45.                 return new JsonResponse(array(
  46.                     'code' => 400,
  47.                     'message' => 'error',
  48.                     'errors' => $errors
  49.                 ),400);
  50.             }else{
  51.            
  52.                 if ($form->isSubmitted() && $form->isValid()) {
  53.             
  54.                    // $gateway = $form->get("gateway")->getData();
  55.                     $email $form->get("email")->getData();
  56.                     $price $form->get("price")->getData();
  57.                     $other $form->get("other")->getData();
  58.                     if($other != ""){
  59.                         $prix = (float)$other*100;
  60.                     }else{
  61.                         $prix = (float)$price*100;
  62.                     }
  63.                     
  64.                         $locale $request->getLocale();
  65.                         Stripe::setApiKey('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');
  66.                         $success_url $this->generateUrl('payment-success',array(),UrlGeneratorInterface::ABSOLUTE_URL);
  67.                         $cancel_url $this->generateUrl('payment-cancel',array(),UrlGeneratorInterface::ABSOLUTE_URL);
  68.                         $checkout_session Checkout\Session::create([
  69.                           'customer_email' => $email,
  70.                           'submit_type' => 'donate',
  71.                           'payment_method_types' => ['card'],
  72.                           'line_items' => [[
  73.                             'price_data' => [
  74.                               'currency' => 'aed',
  75.                               'unit_amount' => $prix,
  76.                               'product_data' => [
  77.                                 'name' => 'Donate',
  78.                                 'images' => ["https://sharjah-masjids.com/images/top-logo2.png"],
  79.                               ],
  80.                             ],
  81.                             'quantity' => 1,
  82.                           ]],
  83.                           'mode' => 'payment',
  84.                           'success_url' => $success_url ,
  85.                           'cancel_url' => $cancel_url,
  86.                         ]);
  87.                         return new JsonResponse(array('id' => $checkout_session->id));
  88.                     
  89.                    
  90.                 }
  91.             }
  92.         }
  93.     
  94.         return $this->render('payment/payment_form.html.twig', [
  95.             'form' => $form->createView(),]
  96.         );
  97.     }
  98.     /**
  99.     * @Route("/payment/success", name="payment-success")
  100.     */
  101.     public function success(): Response
  102.     {
  103.         return $this->render('payment/success.html.twig', []);
  104.     }
  105.     /**
  106.     * @Route("/payment/cancel" , name="payment-cancel")
  107.     */
  108.     public function cancel(): Response
  109.     {
  110.        return $this->render('payment/cancel.html.twig', []);
  111.     }
  112. }