Skip to content

Commit

Permalink
Merge pull request #19 from BFoucher/return-order-on-success
Browse files Browse the repository at this point in the history
return orderId on success response
  • Loading branch information
paulandrieux authored Sep 20, 2016
2 parents c20b3f1 + f43c15f commit 4152810
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 79 deletions.
71 changes: 34 additions & 37 deletions Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
**/
Expand Down Expand Up @@ -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);
Expand All @@ -87,37 +86,34 @@ 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
$preAuth = $paymentHelper->createPreAuthorisation($updatedCardRegister, $this->getUser(), $order);

// 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
Expand All @@ -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,
]);
}

/**
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -199,21 +193,24 @@ 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]
);
}
}
80 changes: 38 additions & 42 deletions Helper/PaymentHelper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace AppVentus\MangopayBundle\Helper;

use AppVentus\MangopayBundle\AppVentusMangopayEvents;
Expand All @@ -21,9 +22,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
*
* ref: appventus_mangopay.payment_helper
*
* ref: appventus_mangopay.payment_helper.
**/
class PaymentHelper
{
Expand All @@ -40,10 +39,9 @@ public function __construct(MangopayHelper $mangopayHelper, Router $router, Even

public function prepareCardRegistrationCallback(User $user, Order $order)
{

$cardRegistration = new CardRegistration();
$cardRegistration->UserId = $user->Id;
$cardRegistration->Currency = "EUR";
$cardRegistration->Currency = 'EUR';
$mangoCardRegistration = $this->mangopayHelper->CardRegistrations->create($cardRegistration);

$event = new CardRegistrationEvent($cardRegistration);
Expand All @@ -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();
$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);

Expand All @@ -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
);

Expand All @@ -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
*/
Expand All @@ -142,9 +143,7 @@ public function executePreAuthorisation(
Wallet $wallet,
$feesAmount,
$amount = null
)
{

) {
if (!$amount) {
$amount = $preAuthorisation->getDebitedFunds();
}
Expand All @@ -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);
Expand All @@ -199,9 +196,8 @@ 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]);
}

}

0 comments on commit 4152810

Please sign in to comment.