/**
  * @param string   $transactionId
  * @param Response $response
  *
  * @return $this
  */
 public function processMangoPayReturn($transactionId, Response $response)
 {
     $payIn = $this->mangoPayPayInsApi->Get($transactionId);
     if (!$payIn instanceof PayIn) {
         throw new BadMangoReturnException('Error, return for ' . $transactionId . ' from Mango is invalid');
     }
     $session = $this->storageService->get(self::SESSION_PREFIX . $transactionId);
     if ('SUCCEEDED' === $payIn->Status) {
         $this->eventDispatched->dispatch(MangoPayEvents::SECURE_FLOW_SUCCESS, new SecureFlowEvent($payIn, $response, $session));
     } else {
         $this->eventDispatched->dispatch(MangoPayEvents::SECURE_FLOW_ERROR, new SecureFlowEvent($payIn, $response, $session));
     }
 }
 /**
  * To prepare the request, call mangopay to retrieve url and token to submit the card registration form
  * and register registration session's data in the storage, then prepare the url of return.
  *
  *
  * @param UserInterface                $user
  * @param string                       $cardType
  * @param null|CardRegistrationSession $session
  *
  * @return CardRegistrationResult
  */
 public function prepare(UserInterface $user, CardRegistrationSession $session, $cardType = 'CB_VISA_MASTERCARD')
 {
     if (empty($user->getMangoPayId())) {
         throw new BadMangoEntityException('Error, the user has not a valid mango pay id');
     }
     $apiResult = $this->getCardRegistrationData($user, $cardType);
     $session->setCardRegistrationId($apiResult->Id)->setUser($user);
     $sessionId = $session->getSessionId();
     $this->storageService->set(self::SESSION_PREFIX . $sessionId, $session);
     $result = new CardRegistrationResult($user);
     $result->setAccessKeyRef($apiResult->AccessKey);
     $result->setData($apiResult->PreregistrationData);
     $result->setCardRegistrationUrl($apiResult->CardRegistrationURL);
     $result->setId($apiResult->Id);
     $result->setReturnUrl($this->router->generate($this->returnRouteName, ['registrationSessionId' => $sessionId], UrlGeneratorInterface::ABSOLUTE_URL));
     return $result;
 }