src/Controller/StripeController.php line 361

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Model\DataObject\User;
  4. use App\Security\AppAuthenticator;
  5. use App\Security\OauthAuthenticator;
  6. use App\Service\CodelocksService;
  7. use App\Service\PriceCalculator;
  8. use App\Service\PromoService;
  9. use Carbon\Carbon;
  10. use Exception;
  11. use Pimcore\Controller\FrontendController;
  12. use Pimcore\Log\ApplicationLogger;
  13. use Pimcore\Model\DataObject\Folder;
  14. use Pimcore\Model\DataObject\Payment;
  15. use Pimcore\Model\DataObject\PromoUser;
  16. use Pimcore\Model\Document\Email;
  17. use Pimcore\Model\WebsiteSetting;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Stripe\Exception\SignatureVerificationException;
  22. use Stripe\Exception\UnexpectedValueException;
  23. use Stripe\Stripe;
  24. use Pimcore\Mail;
  25. use Pimcore\Model\DataObject;
  26. use Pimcore\Model\DataObject\Order;
  27. use Pimcore\Model\DataObject\Price;
  28. use Pimcore\Model\DataObject\Address;
  29. use Pimcore\Model\DataObject\CreditHistory;
  30. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  31. use Symfony\Component\Security\Core\Security as FrontSecurity;
  32. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  33. class StripeController extends FrontendController
  34. {
  35.     private $stripe null;
  36.     public function __construct()
  37.     {
  38.         \Stripe\Stripe::setApiKey(\Pimcore\Model\WebsiteSetting::getByName('stripeSecretKey')->getData());
  39.         $this->stripe = new \Stripe\StripeClient(\Pimcore\Model\WebsiteSetting::getByName('stripeSecretKey')->getData());
  40.     }
  41.     /**
  42.      * @route("/pay/{method}/{order}", name="pay_with_method", methods={"GET"})
  43.      */
  44.     public function payWithMethod(Request $requestFrontSecurity $security)
  45.     {
  46.         $securityUser $security->getUser();
  47.         if(!$securityUser){
  48.             return $this->json(["error" => "not authenticated"]);
  49.         }
  50.         $user User::getByUsername($securityUser->getUserIdentifier())->load()[0];
  51.         $order Order::getById($request->get("order"));
  52.         if(!$order) {
  53.             throw new Exception("Order doesn't exist"1);
  54.         }
  55.         $amount 0;
  56.         if($order->getOrderType() == "pack"){
  57.             $amount $order->getPack()->getPrice();
  58.         }else{
  59.             foreach($order->getBookings() as $booking){
  60.                 $amount += PriceCalculator::getTotal($booking);
  61.             }
  62.         }
  63.         $order->setTotalNoPromo($amount);
  64.         $promoService = new PromoService();
  65.         if($order->getPromo()){
  66.             $check $promoService->submit($order->getPromo(), $orderfalse);
  67.             if(!$check["status"]){
  68.                 $order->setTotalPrice($amount);
  69.                 $order->setPromo(null);
  70.                 $order->setPromoCode("");
  71.                 $order->setPromoReduc("");
  72.                 $order->setPromoReduc("");
  73.                 $order->save();
  74.             }
  75.             $amount $order->getTotalPrice();
  76.         }else{
  77.             $order->setTotalPrice($amount);
  78.         }
  79.         if($order->getUsingCredits() && $order->getOrderType() != "pack"){
  80.             if($amount){
  81.                 if($user->getCredit() >= $amount){
  82.                     $order->setCredits($amount);
  83.                     $amount 0;
  84.                 }else{
  85.                     $amount $amount $user->getCredit();
  86.                     $order->setCredits($user->getCredit());
  87.                 }
  88.             }else{
  89.                 $order->setUsingCredits(false);
  90.             }
  91.         }
  92.         $order->setStatus("new");
  93.         $order->save();
  94.         $amount *= 100;
  95.         $user User::getByUsername($securityUser->getUserIdentifier())->load()[0];
  96.         $url $this->generateUrl('checkoutSuccess',["order"=>$order->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
  97.         $paymentIntent \Stripe\PaymentIntent::create([
  98.             'customer' => $user->getStripeCustomerId(),
  99.             'payment_method' => $request->get('method'),
  100.             'amount' => $amount,
  101.             'currency' => 'eur',
  102.             'payment_method_types' => ['card'],
  103.         ]);
  104.         $payment = new Payment();
  105.         $payment->setStripePaymentIntent($paymentIntent->id);
  106.         $payment->setTotalPrice($amount 100.0);
  107.         $payment->setKey('Payment_'.uniqid());
  108.         $payment->setParent($order);
  109.         $payment->setPublished(true);
  110.         $payment->save();
  111.         $order->setPayment($payment);
  112.         $order->save();
  113.         return $this->json(["paymentIntent" => $paymentIntent]);
  114.         // return $this->redirectToRoute('checkoutSuccess', ["order"=>$order->getID()]);
  115.     }
  116.     /**
  117.      * @Template
  118.      * @param Request $request
  119.      * @route("/{_locale}/checkout-success/{studiotag}/{order}", name="checkoutSuccess", methods={"GET"})
  120.      * @return array
  121.      */
  122.     public function successAction(Request $requestFrontSecurity $security,
  123.                                   GuardAuthenticatorHandler $guard,
  124.                                   AppAuthenticator $formAuthenticator)
  125.     {
  126.         $orderId $request->get('order');
  127.         $order Order::getById($request->get('order'));
  128.         if($order->getOrderType() == "pack"){
  129.             $prod $order->getPack();
  130.             $duration 0;
  131.         }else{
  132.             $prods $order->getBookings();
  133.             $prod $prods[0];
  134.             $duration $prod->getBookedEnd()->diffInHours($prod->getBookedStart());
  135.         }
  136.         $token $request->get("validation_token");
  137.         if($token){
  138.             $user User::getByPaymentToken($token)->load();
  139.             if($user){
  140.                 $user $user[0];
  141.                 //login automatique du user
  142.                 $guard->authenticateUserAndHandleSuccess(
  143.                     $user,
  144.                     $request,
  145.                     $formAuthenticator,
  146.                     'app_admin'
  147.                 );
  148.             }else{
  149.                 return $this->redirectToRoute("studioList");
  150.             }
  151.         }else{
  152.             $securityUser $security->getUser();
  153.             if (!$securityUser) {
  154.                 return $this->redirectToRoute("studioList");
  155.             }
  156.             $user User::getByUsername($securityUser->getUserIdentifier())->load()[0];
  157.         }
  158.         $order->setStripeReturnDate(Carbon::now());
  159.         $order->setStripeReturnUrl($request->getRequestUri());
  160.         if(($request->get("redirect_status") == "failed" && ($order->getStatus() != "validated" && $order->getStatus() != "waiting")) || $order->getStatus() == "abandonned" || $order->getStatus() == "canceled"){
  161.             $order->setStatus("canceled");
  162.             $order->setLogidriveStatus(true);
  163.             $order->save();
  164.             if($order->getOrderType() != "pack"){
  165.                 $tag "credit-pack";
  166.                 $prod->setLogidriveStatus(true);
  167.                 $prod->save();
  168.             }else{
  169.                 $tag $prod->getBookedStudio()->getCategorie()->getTagUrl();
  170.             }
  171.             return $this->redirectToRoute("checkoutFailure", ["order" => $orderId"studiotag" => $tag]);
  172.         }
  173.         $linkedUser $order->getLinkedUser();
  174.         if($user->getId() !== $linkedUser->getId()){
  175.             return $this->redirectToRoute("studioList");
  176.         }
  177.         $payment $order->getPayment();
  178.         $checkPayment Payment::getByStripePaymentIntent($request->get('payment_intent'))->load();
  179.         if($checkPayment){
  180.             $checkPayment $checkPayment[0];
  181.             if($payment->getId() !== $checkPayment->getId()){
  182.                 $order->setPayment($checkPayment);
  183.                 $order->save();
  184.             }
  185.         }else{
  186.             if($payment->getStripePaymentIntent() !== $request->get('payment_intent')){
  187.                 $payment->setStripePaymentIntent($request->get('payment_intent'));
  188.                 $payment->save();
  189.             }
  190.         }
  191.         if($order->getStatus() == "new"){
  192.             $order->setStatus("waiting");
  193.         }
  194.         $order->save();
  195.         if($order->getOrderType() != "pack" && !$prod->getLockCode() && !$prod->getPtiCode()){
  196.             $clService = new CodelocksService();
  197.             $code $clService->generateCode($prod);
  198.             if($code){
  199.                 $prod->setLockCode($code);
  200.                 $prod->setPtiCode($code);
  201.                 $prod->save();
  202.             }
  203.         }
  204.         $session $request->getSession();
  205.         $checkRefresh $session->get("success_orderid");
  206.         $credFolder \Pimcore\Model\DataObject\Folder::getByPath('/credit-history');
  207.         if (!$credFolder) {
  208.             $credFolder = new \Pimcore\Model\DataObject\Folder();
  209.             $credFolder->setKey('credit-history');
  210.             $credFolder->setParentId(1);
  211.             $credFolder->save();
  212.         }
  213.         if($checkRefresh != $orderId){
  214.             $session->set("success_orderid"$orderId);
  215.             $creditGifting WebsiteSetting::getByName('credit_gifting')->getData();
  216.             $creditAmount = (float) WebsiteSetting::getByName('credit_gifting_amount')->getData();
  217.             if($order->getOrderType() != "pack" && $creditGifting){
  218.                 $user $order->getLinkedUser();
  219.                 $userOrders = new Order\Listing();
  220.                 $userOrders->setCondition("(status = 'validated' OR status = 'canceled') AND linkedUser__id = ".$user->getId()." AND oo_id != ".$order->getId());
  221.                 $userOrders $userOrders->load();
  222.                 if(!$userOrders){
  223.                     $history = new CreditHistory();
  224.                     $history->setCredit($order->getCredits());
  225.                     $history->setMouvement("add");
  226.                     $history->setTransactionTime(Carbon::now());
  227.                     $history->setOrder($order);
  228.                     $history->setTransactionType("Credit Gift");
  229.                     $history->setKey("History_gift".$user->getUsername()."_".Carbon::now()->format("YmdHis")."_".uniqid());
  230.                     $history->setPublished(true);
  231.                     $history->setParent($credFolder);
  232.                     $history->save();
  233.                     $histlist $user->getCreditHistory();
  234.                     $histlist[] = $history;
  235.                     $user->setCreditHistory($histlist);
  236.                     $user->setCredit(((float) $user->getCredit())+$creditAmount);
  237.                     $user->save();
  238.                     $data = [
  239.                         "credit" => $creditAmount
  240.                     ];
  241.                     $mail = new Mail();
  242.                     $mail->setDocument('/emails/'.$prod->getOrder()->getLang().'/credit-gifting');
  243.                     $mail->to($user->getEmail());
  244.                     $mail->setParams($data);
  245.                     $mail->send();
  246.                 }
  247.             }
  248.             if($order->getUsingCredits()){
  249.                 $credCheck = new CreditHistory\Listing();
  250.                 $credCheck->setCondition("mouvement = 'remove' AND order__id = ".$order->getId());
  251.                 $credCheck $credCheck->load();
  252.                 if(!$credCheck){
  253.                     $creditsLeft 0;
  254.                     if(((float) $user->getCredit()) >= ((float) $order->getCredits())){
  255.                         $creditsLeft = ((float) $user->getCredit()) - ((float) $order->getCredits());
  256.                     }
  257.                     $history = new CreditHistory();
  258.                     $history->setCredit((float) $order->getCredits());
  259.                     $history->setMouvement("remove");
  260.                     $history->setTransactionTime(Carbon::now());
  261.                     $history->setOrder($order);
  262.                     $history->setTransactionType("Studio Reservation");
  263.                     $history->setKey("History_".$user->getUsername()."_".Carbon::now()->format("YmdHis")."_".uniqid());
  264.                     $history->setPublished(true);
  265.                     $history->setParent($credFolder);
  266.                     $history->save();
  267.                     $histlist $user->getCreditHistory();
  268.                     $histlist[] = $history;
  269.                     $user->setCreditHistory($histlist);
  270.                     $user->setCredit($creditsLeft);
  271.                     $user->save();
  272.                 }
  273.             }
  274.         }
  275.         if($order->getOrderType() == "pack"){
  276.             return $this->render('stripe/order_pack_success.html.twig', ["order" => $order]);
  277.         }
  278.         return  ["order" => $order"payment" => $payment"prod" => $prod"duration" => $duration];
  279.     }
  280.     /**
  281.      * @Template
  282.      * @param Request $request
  283.      * @route("/{_locale}/checkout-failure/{studiotag}/{order}", name="checkoutFailure", methods={"GET"})
  284.      * @return array
  285.      */
  286.     public function failureAction(Request $request)
  287.     {
  288.         $orderId $request->get('order');
  289.         $order Order::getById($orderId);
  290.         return $this->render('stripe/failure.html.twig', ["order" => $order]);
  291.     }
  292.     /**
  293.      *
  294.      * @route("/stripe/webhook",name="stripe_webhook",methods={"POST"})
  295.      * @return array
  296.      * @throws Exception
  297.      */
  298.     public function webhookAction(Request $request)
  299.     {
  300.         $webhookSecret \Pimcore\Model\WebsiteSetting::getByName('stripeWebhookSecret')->getData();
  301.         if ($webhookSecret) {
  302.             try {
  303.                 $event \Stripe\Webhook::constructEvent(
  304.                     $request->getContent(),
  305.                     $request->headers->get('stripe-signature'),
  306.                     $webhookSecret
  307.                 );
  308.             } catch (SignatureVerificationException $e) {
  309.                 \Pimcore\Log\Simple::log("StripeWebhook""Error ".$e->getMessage()." sig : ".$request->headers->get('stripe-signature')." at ".date("Y-m-d H:i:s"));
  310.                 return $this->json([ 'error' => $e->getMessage() ], 401);
  311.             }catch (UnexpectedValueException $e) {
  312.                 \Pimcore\Log\Simple::log("StripeWebhook""Error ".$e->getMessage()." sig : ".$request->headers->get('stripe-signature')." at ".date("Y-m-d H:i:s"));
  313.                 return $this->json([ 'error' => $e->getMessage() ], 402);
  314.             }catch(Exception $e){
  315.                 \Pimcore\Log\Simple::log("StripeWebhook""Error ".$e->getMessage()." sig : ".$request->headers->get('stripe-signature')." at ".date("Y-m-d H:i:s"));
  316.                 return $this->json([ 'error' => $e->getMessage() ], 403);
  317.             }
  318.         } else {
  319.             $event $request->request->all();
  320.         }
  321.         \Pimcore\Log\Simple::log("StripeWebhook""Data Received, sig : ".$request->headers->get('stripe-signature')." at ".date("Y-m-d H:i:s"));
  322.         \Pimcore\Log\Simple::log("StripeWebhook""Data Received, data : ".json_encode($event));
  323.         $object $event->data->object;
  324.         switch ($event->type) {
  325.             case 'payment_intent.succeeded':
  326.                 $payment Payment::getByStripePaymentIntent($object->id);
  327.                 $payment $payment->load()[0];
  328.                 if($payment){
  329.                     $payment->setStatus($object->status);
  330.                     if($object->charges->data[0]->payment_method_details->type == "card"){
  331.                         $payment->setCardBrand($object->charges->data[0]->payment_method_details->card->brand);
  332.                         $payment->setCardDigit($object->charges->data[0]->payment_method_details->card->last4);
  333.                     }else{
  334.                         $payment->setCardBrand("bancontact");
  335.                         $payment->setCardDigit($object->charges->data[0]->payment_method_details->bancontact->iban_last4);
  336.                     }
  337.                     $payment->setStripePaymentMethod($object->charges->data[0]->payment_method);
  338.                     $payment->save();
  339.                     sleep(15);
  340.                     $order $payment->getOrder();
  341.                     if(!$order){
  342.                         $order Order::getByPayment($payment);
  343.                         $order $order->load()[0];
  344.                         $payment->setOrder($order);
  345.                         $payment->save();
  346.                     }else{
  347.                         $order->setPayment($payment);
  348.                         $order->save();
  349.                     }
  350.                     $user $order->getLinkedUser();
  351.                     $flagProcess false;
  352.                     switch($user->getStatus()){
  353.                         case 'unverified':
  354.                             $order->setStatus('waiting');
  355.                             break;
  356.                         case 'verified':
  357.                             $flagProcess true;
  358.                             $order->setStatus('validated');
  359.                             if($order->getOrderType() != "pack"){
  360.                                 $order->setBrevoFlag(true);
  361.                             }
  362.                             break;
  363.                     }
  364.                     $order->save();
  365.                     $request->setLocale($order->getLang());
  366.                     if ($flagProcess) {
  367.                         if($order->getOrderType() == "pack"){
  368.                             $creditAmount $order->getPack()->getPrice() + $order->getPack()->getBonus();
  369.                             $userCredit = (float) $user->getCredit();
  370.                             $user->setCredit($creditAmount+$userCredit);
  371.                             $credFolder \Pimcore\Model\DataObject\Folder::getByPath('/credit-history');
  372.                             if (!$credFolder) {
  373.                                 $credFolder = new \Pimcore\Model\DataObject\Folder();
  374.                                 $credFolder->setKey('credit-history');
  375.                                 $credFolder->setParentId(1);
  376.                                 $credFolder->save();
  377.                             }
  378.                             $history1 = new CreditHistory();
  379.                             $history1->setCredit($creditAmount);
  380.                             $history1->setMouvement("add");
  381.                             $history1->setTransactionTime(Carbon::now());
  382.                             $history1->setOrder($order);
  383.                             $history1->setTransactionType($order->getPack()->getTitle($order->getLang()));
  384.                             $history1->setKey("History_".$user->getUsername()."_".Carbon::now()->format("YmdHis")."_".uniqid());
  385.                             $history1->setPublished(true);
  386.                             $history1->setParent($credFolder);
  387.                             $history1->save();
  388.                             $histlist1 $user->getCreditHistory();
  389.                             $histlist1[] = $history1;
  390.                             $user->setCreditHistory($histlist1);
  391.                             $user->save();
  392.                         }else{
  393.                             $data['user'] = $user;
  394.                             $booking $order->getBookings();
  395.                             $data['order'] = $order;
  396.                             $data['lockCode'] = $booking[0]->getLockCode();
  397.                             $data['ptiCode'] = $booking[0]->getPtiCode();
  398.                             if(!$data['lockCode'] || !$data['ptiCode']){
  399.                                 $clService = new CodelocksService();
  400.                                 $code $clService->generateCode($booking[0]);
  401.                                 if($code){
  402.                                     $booking[0]->setLockCode($code);
  403.                                     $booking[0]->setPtiCode($code);
  404.                                     $booking[0]->save();
  405.                                     $data['lockCode'] = $code;
  406.                                     $data['ptiCode'] = $code;
  407.                                 }
  408.                             }
  409.                             if($data['lockCode'] && $data['ptiCode'] && !$booking[0]->getMailSent()){
  410.                                 //Mail au client
  411.                                 $mail = new Mail();
  412.                                 $mail->setDocument('/emails/'.$order->getLang().'/payment-confirmation');
  413.                                 $mail->to($user->getEmail());
  414.                                 $mail->setParams($data);
  415.                                 $mail->send();
  416.                                 $booking[0]->setMailSent(true);
  417.                                 $booking[0]->save();
  418.                             }
  419.                             $onboarding \Pimcore\Model\WebsiteSetting::getByName('studio_mail_onboarding')?->getData();
  420.                             $studioCat $booking[0]->getBookedStudio()->getCategorie();
  421.                             $studios explode("|"$user->getTypeStudios());
  422.                             if($onboarding && $studioCat->getTagBrevo() && $studioCat->getMailOnboarding($order->getLang()) && !in_array($studioCat->getTagBrevo(), $studios)){
  423.                                 $mailDoc $studioCat->getMailOnboarding($order->getLang());
  424.                                 $mail = new Mail();
  425.                                 $mail->setDocument($mailDoc->getId());
  426.                                 $mail->to($user->getEmail());
  427.                                 $mail->setParams($data);
  428.                                 $mail->send();
  429.                             }
  430.                         }
  431.                         if($promo $order->getPromo()){
  432.                             $promoService = new PromoService();
  433.                             $promoService->success($promo$user$order);
  434.                         }
  435.                         if($order->getUsingCredits()){
  436.                             $credCheck = new CreditHistory\Listing();
  437.                             $credCheck->setCondition("mouvement = 'remove' AND order__id = ".$order->getId());
  438.                             $credCheck $credCheck->load();
  439.                             if(!$credCheck){
  440.                                 $creditsLeft 0;
  441.                                 if(((float) $user->getCredit()) >= ((float) $order->getCredits())){
  442.                                     $creditsLeft = ((float) $user->getCredit()) - ((float) $order->getCredits());
  443.                                 }
  444.                                 $credFolder \Pimcore\Model\DataObject\Folder::getByPath('/credit-history');
  445.                                 if (!$credFolder) {
  446.                                     $credFolder = new \Pimcore\Model\DataObject\Folder();
  447.                                     $credFolder->setKey('credit-history');
  448.                                     $credFolder->setParentId(1);
  449.                                     $credFolder->save();
  450.                                 }
  451.                                 $history = new CreditHistory();
  452.                                 $history->setCredit((float) $order->getCredits());
  453.                                 $history->setMouvement("remove");
  454.                                 $history->setTransactionTime(Carbon::now());
  455.                                 $history->setOrder($order);
  456.                                 $history->setTransactionType("Studio Reservation");
  457.                                 $history->setKey("History_".$user->getUsername()."_".Carbon::now()->format("YmdHis")."_".uniqid());
  458.                                 $history->setPublished(true);
  459.                                 $history->setParent($credFolder);
  460.                                 $history->save();
  461.                                 $histlist $user->getCreditHistory();
  462.                                 $histlist[] = $history;
  463.                                 $user->setCreditHistory($histlist);
  464.                                 $user->setCredit($creditsLeft);
  465.                                 $user->save();
  466.                             }
  467.                         }
  468.                         $orderList Order::getList();
  469.                         $orderList->setCondition("status = 'validated' AND linkedUser__id = ? ", [$user->getId()]);
  470.                         $orderL $orderList->load();
  471.                         $reservationCount count($orderL);
  472.                         if ($user->getStaffStatus() !== 'monitor' && $user->getStaffStatus() !== 'approved') {
  473.                             if ($reservationCount == null || $reservationCount <= 1) {
  474.                                 $user->setStaffStatus('new');
  475.                             } elseif ($reservationCount >= 3) {
  476.                                 $user->setStaffStatus('approved');
  477.                             } else {
  478.                                 $user->setStaffStatus(null);
  479.                             }
  480.                             $user->save();
  481.                         }
  482.                     }
  483.                 }
  484.                 break;
  485.             case 'payment_intent.payment_failed':
  486.                 $payment Payment::getByStripePaymentIntent($object->id);
  487.                 $payment $payment->load()[0];
  488.                 if($payment){
  489.                     $payment->setStatus($object->status);
  490.                     $payment->save();
  491.                     $order Order::getByPayment($payment);
  492.                     $order $order->load()[0];
  493.                     $order->setStatus('new');
  494.                     $order->save();
  495.                     $booking $order->getBookings()[0];
  496.                     if($booking){
  497.                         $booking->setLockCode(null);
  498.                         $booking->setPtiCode(null);
  499.                         $booking->save();
  500.                     }
  501.                 }
  502.                 break;
  503.             default:
  504.                 break;
  505.         }
  506.         return $this->json([ 'status' => 'success' ]);
  507.     }
  508.     /**
  509.      * @route("/stripe/deposit/capture", name="capture_deposit", methods={"POST"})
  510.      */
  511.     public function captureDeposit(Request $request){
  512.         if(!$request->get('amount') || !$request->get('message') || !$request->get('order')){
  513.             return $this->json([ 'error' => 'missing parameters' ]);
  514.         }
  515.         $order Order::getById($request->get('order'));
  516.         if($order->getStatusDeposit() !== null &&  $order->getStatusDeposit() !== ''){
  517.             return $this->json([ 'error' => 'deposit already '.$order->getStatusDeposit() ]);
  518.         }
  519.         $deposit $order->getCaution();
  520.         if( $request->get('amount') > $deposit){
  521.             return $this->json([ 'error' => 'capture amount is more than deposit' ]);
  522.         }
  523.         $payment $order->getPaymentDeposit();
  524.         $intent \Stripe\PaymentIntent::retrieve($payment->getStripePaymentIntent());
  525.         try{
  526.             $intent->capture(['amount_to_capture' => $request->get('amount')*100]);
  527.         }catch(Exception $e){
  528.             return $this->json([ 'error' => $e->getMessage() ]);
  529.         }
  530.         $order->setStatusDeposit('captured');
  531.         $order->save();
  532.         $data['order'] = $order;
  533.         $data['user'] = $order->getLinkedUser();
  534.         //Mail au client
  535.         $mail = new Mail();
  536.         $mail->setDocument('/emails/'.$request->getLocale().'/payment-released');
  537.         $mail->to($order->getLinkedUser()->getEmail());
  538.         $mail->setParams($data);
  539.         $mail->send();
  540.         return $this->json(["status" => "success"]);
  541.     }
  542.     /**
  543.      * @route("/stripe/deposit/release", name="release_deposit", methods={"POST"})
  544.      */
  545.     public function releaseDeposit(Request $request){
  546.         if(!$request->get('order')){
  547.             return $this->json([ 'error' => 'missing parameters' ]);
  548.         }
  549.         $order Order::getById($request->get('order'));
  550.         if($order->getStatusDeposit() !== null){
  551.             return $this->json([ 'error' => 'deposit already '.$order->getStatusDeposit() ]);
  552.         }
  553.         $payment $order->getPaymentDeposit();
  554.         try{
  555.             $this->stripe->paymentIntents->cancel($payment->getStripePaymentIntent(),[]);
  556.         }catch(Exception $e){
  557.             return $this->json([ 'error' => $e->getMessage() ]);
  558.         }
  559.         $order->setStatusDeposit('released');
  560.         $order->save();
  561.         //TODO - Done: faire une mail vers le client
  562.         $data['order'] = $order;
  563.         $data['user'] = $order->getLinkedUser();
  564.         //Mail au client
  565.         $mail = new Mail();
  566.         $mail->setDocument('/emails/'.$request->getLocale().'/payment-released');
  567.         $mail->to($order->getLinkedUser()->getEmail());
  568.         $mail->setParams($data);
  569.         $mail->send();
  570.         return $this->json(["status" => "success"]);
  571.     }
  572. }