From e0bb3d1118641aca1dcc62bc368b740830becd96 Mon Sep 17 00:00:00 2001 From: baptiste Date: Tue, 20 Sep 2016 14:43:02 +0200 Subject: [PATCH 1/2] return orderId on success response --- Controller/PaymentController.php | 12 +++++++----- Helper/PaymentHelper.php | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Controller/PaymentController.php b/Controller/PaymentController.php index 1388f73..e765cd9 100644 --- a/Controller/PaymentController.php +++ b/Controller/PaymentController.php @@ -199,21 +199,23 @@ public function paymentFinalizeSecureAction(Request $request, $orderId) $this->get('translator')->trans('appventus_mangopay.alert.pre_authorisation.success') ); - return $this->redirect($this->get('appventus_mangopay.payment_helper')->generateSuccessUrl()); + return $this->redirect($this->get('appventus_mangopay.payment_helper')->generateSuccessUrl($orderId)); } /** * @param Request $request The request + * @param int $orderId * * This method shows the congratulations * - * @Route("/success", name="appventus_mangopaybundle_payment_success") + * @Route("/success/{orderId}", name="appventus_mangopaybundle_payment_success") * @return Response */ - public function successAction(Request $request) + public function successAction(Request $request, $orderId) { return $this->render( - 'AppVentusMangopayBundle::success.html.twig' + 'AppVentusMangopayBundle::success.html.twig', + [ 'orderId' => $orderId ] ); } -} +} \ No newline at end of file diff --git a/Helper/PaymentHelper.php b/Helper/PaymentHelper.php index f2cd8c5..aed5874 100644 --- a/Helper/PaymentHelper.php +++ b/Helper/PaymentHelper.php @@ -61,7 +61,7 @@ public function prepareCardRegistrationCallback(User $user, Order $order) ) ); - $successRedirect = $this->generateSuccessUrl(); + $successRedirect = $this->generateSuccessUrl($order->getId()); return array( 'callback' => 'payAjaxOrRedirect("' @@ -199,9 +199,9 @@ public function cancelPreAuthForOrder(Order $order, CardPreAuthorisation $preAut } } - public function generateSuccessUrl() + public function generateSuccessUrl($orderId) { - return $this->router->generate('appventus_mangopaybundle_payment_success'); + return $this->router->generate('appventus_mangopaybundle_payment_success', ['orderId' => $orderId]); } -} +} \ No newline at end of file From f43c15fafa5ee24ed6a3b0714edef803c666f461 Mon Sep 17 00:00:00 2001 From: baptiste Date: Tue, 20 Sep 2016 15:44:21 +0200 Subject: [PATCH 2/2] fix StyleCi --- Controller/PaymentController.php | 67 +++++++++++++--------------- Helper/PaymentHelper.php | 76 +++++++++++++++----------------- 2 files changed, 67 insertions(+), 76 deletions(-) diff --git a/Controller/PaymentController.php b/Controller/PaymentController.php index e765cd9..03a93d6 100644 --- a/Controller/PaymentController.php +++ b/Controller/PaymentController.php @@ -10,21 +10,20 @@ use AppVentus\MangopayBundle\OrderEvents; use MangoPay\CardRegistration; use MangoPay\PayIn; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; /** - * Manage payment + * Manage payment. + * * @Route("/payment") */ class PaymentController extends Controller { - /** - * Create a payment + * Create a payment. * * @Route("/new/{order}", name="appventus_mangopaybundle_payment_new", defaults={"order" = null, "type" = "card"}) **/ @@ -53,28 +52,28 @@ public function newAction(Request $request, $order) return $this->render( 'AppVentusMangopayBundle::cardPayment.html.twig', - array( - 'form' => $form->createView(), + [ + 'form' => $form->createView(), 'order' => $order, - ) + ] ); } /** * @param Request $request The request * @param Reservation $reservation The reservation - * @param integer $cardId The cardId + * @param int $cardId The cardId * * This method is called by paymentAction callback, with the authorized cardId as argument. * It creates a PreAuthorisation with reservation price, and store its id in the Reservation. * When the owner will accept the reservation, we will be able to fetch the PreAuthorisation and create the PayIn * * @Route("/finalize/{orderId}/{cardId}", name="appventus_mangopaybundle_payment_finalize") + * * @return JsonResponse return json */ public function paymentFinalizeAction(Request $request, $orderId, $cardId) { - $em = $this->getDoctrine()->getManager(); $orderRepository = $em->getRepository($this->container->getParameter('appventus_mangopay.order.class')); $order = $orderRepository->findOneById($orderId); @@ -87,16 +86,14 @@ public function paymentFinalizeAction(Request $request, $orderId, $cardId) // Handle error if ((property_exists($updatedCardRegister, 'ResultCode') - && $updatedCardRegister->ResultCode !== "000000") - || $updatedCardRegister->Status == 'ERROR') - { + && $updatedCardRegister->ResultCode !== '000000') + || $updatedCardRegister->Status == 'ERROR') { + $errorMessage = $this->get('translator')->trans('mangopay.error.'.$updatedCardRegister->ResultCode); - $errorMessage = $this->get('translator')->trans('mangopay.error.' . $updatedCardRegister->ResultCode); - - return new JsonResponse(array( + return new JsonResponse([ 'success' => false, - 'message' => $errorMessage - )); + 'message' => $errorMessage, + ]); } // Create a PayIn @@ -104,20 +101,19 @@ public function paymentFinalizeAction(Request $request, $orderId, $cardId) // Handle error if ((property_exists($preAuth, 'Code') && $preAuth->Code !== 200) || $preAuth->Status == 'FAILED') { + $errorMessage = $this->get('translator')->trans('mangopay.error.'.$preAuth->ResultCode); - $errorMessage = $this->get('translator')->trans('mangopay.error.' . $preAuth->ResultCode); - - return new JsonResponse(array( + return new JsonResponse([ 'success' => false, - 'message' => $errorMessage - )); + 'message' => $errorMessage, + ]); } // Handle secure mode if (property_exists($preAuth, 'SecureModeNeeded') && $preAuth->SecureModeNeeded == 1) { - return new JsonResponse(array( - 'success' => true, - 'redirect' => $preAuth->SecureModeRedirectURL - )); + return new JsonResponse([ + 'success' => true, + 'redirect' => $preAuth->SecureModeRedirectURL, + ]); } // store payin transaction @@ -138,10 +134,9 @@ public function paymentFinalizeAction(Request $request, $orderId, $cardId) $this->get('translator')->trans('appventus_mangopay.alert.pre_authorisation.success') ); - return new JsonResponse(array( - 'success' => true - )); - + return new JsonResponse([ + 'success' => true, + ]); } /** @@ -151,11 +146,11 @@ public function paymentFinalizeAction(Request $request, $orderId, $cardId) * This method is called by paymentFinalizeActionif 3dsecure is required. 3DSecure is needed when 250€ are reached * * @Route("/finalize-secure/{orderId}", name="appventus_mangopaybundle_payment_finalize_secure") + * * @return RedirectResponse */ public function paymentFinalizeSecureAction(Request $request, $orderId) { - $em = $this->getDoctrine()->getManager(); $orderRepository = $em->getRepository($this->container->getParameter('appventus_mangopay.order.class')); $order = $orderRepository->findOneById($orderId); @@ -166,11 +161,10 @@ public function paymentFinalizeSecureAction(Request $request, $orderId) $preAuth = $mangopayApi->CardPreAuthorizations->Get($preAuthId); if ((property_exists($preAuth, 'Code') && $preAuth->Code !== 200) || $preAuth->Status != 'SUCCEEDED') { - if (property_exists($preAuth, 'Code')) { $this->get('session')->getFlashBag()->add( 'danger', - $this->get('translator')->trans('mangopay.error.' . $preAuth->Code) + $this->get('translator')->trans('mangopay.error.'.$preAuth->Code) ); } else { $this->get('session')->getFlashBag()->add('error', $preAuth->ResultMessage); @@ -204,18 +198,19 @@ public function paymentFinalizeSecureAction(Request $request, $orderId) /** * @param Request $request The request - * @param int $orderId + * @param int $orderId * * This method shows the congratulations * * @Route("/success/{orderId}", name="appventus_mangopaybundle_payment_success") + * * @return Response */ public function successAction(Request $request, $orderId) { return $this->render( 'AppVentusMangopayBundle::success.html.twig', - [ 'orderId' => $orderId ] + ['orderId' => $orderId] ); } -} \ No newline at end of file +} diff --git a/Helper/PaymentHelper.php b/Helper/PaymentHelper.php index aed5874..ad811f1 100644 --- a/Helper/PaymentHelper.php +++ b/Helper/PaymentHelper.php @@ -1,4 +1,5 @@ UserId = $user->Id; - $cardRegistration->Currency = "EUR"; + $cardRegistration->Currency = 'EUR'; $mangoCardRegistration = $this->mangopayHelper->CardRegistrations->create($cardRegistration); $event = new CardRegistrationEvent($cardRegistration); @@ -55,37 +53,38 @@ public function prepareCardRegistrationCallback(User $user, Order $order) $redirect = $this->router->generate( 'appventus_mangopaybundle_payment_finalize', - array( + [ 'orderId' => $order->getId(), - 'cardId' => $mangoCardRegistration->Id - ) + 'cardId' => $mangoCardRegistration->Id, + ] ); $successRedirect = $this->generateSuccessUrl($order->getId()); - return array( + return [ 'callback' => 'payAjaxOrRedirect("' - . $redirect . '", "' - . $redirect . '", "' - . $cardRegistrationURL . '", "' - . $preregistrationData . '", "' - . $accessKey . '", "' - . $successRedirect . '")', - ); + .$redirect.'", "' + .$redirect.'", "' + .$cardRegistrationURL.'", "' + .$preregistrationData.'", "' + .$accessKey.'", "' + .$successRedirect.'")', + ]; } /** - * Update card registration with token - * @param string $cardId - * @param string $data - * @param string $errorCode + * Update card registration with token. + * + * @param string $cardId + * @param string $data + * @param string $errorCode + * * @return CardRegistration */ public function updateCardRegistration($cardId, $data, $errorCode) { - $cardRegister = $this->mangopayHelper->CardRegistrations->Get($cardId); - $cardRegister->RegistrationData = $data ? "data=" . $data : "errorCode=" . $errorCode; + $cardRegister->RegistrationData = $data ? 'data='.$data : 'errorCode='.$errorCode; $updatedCardRegister = $this->mangopayHelper->CardRegistrations->Update($cardRegister); @@ -104,16 +103,16 @@ public function createPreAuthorisation(CardRegistration $updatedCardRegister, Us $cardPreAuthorisation->AuthorId = $user->getMangoUserId(); $debitedFunds = new Money(); - $debitedFunds->Currency = "EUR"; + $debitedFunds->Currency = 'EUR'; $debitedFunds->Amount = $order->getMangoPrice(); $cardPreAuthorisation->DebitedFunds = $debitedFunds; - $cardPreAuthorisation->SecureMode = "DEFAULT"; + $cardPreAuthorisation->SecureMode = 'DEFAULT'; $cardPreAuthorisation->SecureModeReturnURL = $this->router->generate( 'appventus_mangopaybundle_payment_finalize_secure', - array( + [ 'orderId' => $order->getId(), - ), + ], true ); @@ -126,13 +125,15 @@ public function createPreAuthorisation(CardRegistration $updatedCardRegister, Us return $preAuth; } + /** - * execute a pre authorisation + * execute a pre authorisation. + * * @param CardPreAuthorisation $preAuthorisation * @param UserInterface $buyer * @param Wallet $wallet - * @param integer $feesAmount - * @param integer $amount 0 to 100 + * @param int $feesAmount + * @param int $amount 0 to 100 * * @return PayIn */ @@ -142,9 +143,7 @@ public function executePreAuthorisation( Wallet $wallet, $feesAmount, $amount = null - ) - { - + ) { if (!$amount) { $amount = $preAuthorisation->getDebitedFunds(); } @@ -169,27 +168,25 @@ public function executePreAuthorisation( $payIn = $this->mangopayHelper->PayIns->Create($payIn); - if (property_exists($payIn, 'Status') && $payIn->Status != "FAILED") { + if (property_exists($payIn, 'Status') && $payIn->Status != 'FAILED') { $event = new PayInEvent($payIn); $this->dispatcher->dispatch(AppVentusMangopayEvents::NEW_PAY_IN, $event); - return $payIn; + return $payIn; } $event = new PayInEvent($payIn); $this->dispatcher->dispatch(AppVentusMangopayEvents::ERROR_PAY_IN, $event); throw new MongopayPayInCreationException($this->translator->trans( - 'mangopay.error.'. $payIn->ResultCode, + 'mangopay.error.'.$payIn->ResultCode, [], 'messages' )); - } public function cancelPreAuthForOrder(Order $order, CardPreAuthorisation $preAuth) { - if ($preAuth->getPaymentStatus() == "WAITING") { - + if ($preAuth->getPaymentStatus() == 'WAITING') { $mangoCardPreAuthorisation = $this->mangopayHelper->CardPreAuthorizations->Get($preAuth->getMangoId()); $mangoCardPreAuthorisation->PaymentStatus = 'CANCELED'; $this->mangopayHelper->CardPreAuthorizations->Update($mangoCardPreAuthorisation); @@ -203,5 +200,4 @@ public function generateSuccessUrl($orderId) { return $this->router->generate('appventus_mangopaybundle_payment_success', ['orderId' => $orderId]); } - -} \ No newline at end of file +}