/**
  * {@inheritdoc}
  */
 public function buildOrderHandoff(CustomerOrder $order)
 {
     $orderUuId = $order->getUuid();
     if (!$orderUuId) {
         throw new \InvalidArgumentException('Order does not exist in entity manager, make sure that it is persisted before handing off');
     }
     $handoff = new OrderHandoff($orderUuId, $this->router->generate($this->handoffUrlRouteName, array('uuid' => $orderUuId), true));
     return $handoff;
 }
 /**
  * Get the refund status
  *
  * @REST\Get("/orders/{uuid}/refundStatus", defaults={"_format"="json"})
  * @REST\View
  */
 public function getRefundStatusAction(CustomerOrder $order)
 {
     if ($order->getUser()->getId() != $this->security->getToken()->getUser()->getId()) {
         throw new AccessDeniedException('Access denied');
     } else {
         if (CustomerOrder::TRANSACTION_TYPE_REFUND != $order->getTransactionType()) {
             throw new InvalidArgumentException('Order was not a refund');
         }
     }
     $refundResponse = $order->getStatusResponse();
     if (!$refundResponse) {
         throw new NotFoundHttpException(sprintf('Payment status for the order "%s" could not be found', $order->getUuid()));
     }
     return $refundResponse;
 }
 /**
  * Action to repeat the payment
  *
  * @param CustomerOrder $order
  * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  * @throws \Exception
  *
  * @Framework\Route("/order/{uuid}/repeat")
  * @Framework\Template
  */
 public function requestRepeatAction(CustomerOrder $order)
 {
     $instruction = new PaymentInstruction($order->getAmount(), 'GBP', 'paypoint_hosted');
     $this->paymentPluginController->createPaymentInstruction($instruction);
     $order->setPaymentInstruction($instruction);
     $this->em->persist($instruction);
     $this->em->flush();
     $pendingTransaction = $instruction->getPendingTransaction();
     if (null === $pendingTransaction) {
         $payment = $this->paymentPluginController->createPayment($instruction->getId(), $instruction->getAmount());
     } else {
         $payment = $pendingTransaction->getPayment();
     }
     $orderId = $order->getId();
     $amount = -abs($order->getAmount());
     /** @var Result $result */
     $result = $this->paymentPluginController->approveAndDeposit($payment->getId(), $amount);
     /** @var FinancialTransaction $transaction */
     $transaction = $result->getFinancialTransaction();
     if (null === $transaction) {
         throw new FinancialException('No financial transaction for repeat');
     }
     /** @var CustomerOrder $order */
     $order = $this->em->getRepository('BarbonPaymentPortalBundle:CustomerOrder')->find($orderId);
     if (!$order) {
         throw new FinancialException('Could not find order');
     }
     /** @var CustomerOrder $parentOrder */
     $parentOrder = $this->em->getRepository('BarbonPaymentPortalBundle:CustomerOrder')->find($order->getParentId());
     if (!$parentOrder) {
         throw new FinancialException('Could not find parent order');
     }
     if ($order->getPaymentStatusCallbackUrl()) {
         $repeatStatus = new OrderStatus();
         if (Result::STATUS_SUCCESS == $result->getStatus() && !$result->isAttentionRequired()) {
             $repeatStatus->setStatus(OrderStatus::STATUS_SUCCESS)->setCode(OrderStatus::CODE_REPEAT_PAYMENT_CAPTURED)->setMessage('Repeat generated');
         } else {
             $message = 'Repeat failed';
             /** @var ExtendedData $extendedData */
             $extendedData = $transaction->getExtendedData();
             if ($extendedData) {
                 if ($extendedData->has('message')) {
                     $message = $extendedData->get('message');
                 }
             }
             $repeatStatus->setStatus(OrderStatus::STATUS_FAILURE)->setCode(OrderStatus::CODE_REPEAT_PAYMENT_FAILED_TO_CAPTURE)->setMessage($message);
         }
         $repeatStatusResponse = new RepeatStatusResponse();
         $repeatStatusResponse->setOriginalTransactionUuId($parentOrder->getTransId())->setRepeatTransactionUuId($transaction->getReferenceNumber())->setOrderUuId($order->getUuid())->setAmount($transaction->getProcessedAmount())->setCurrency($order->getCurrency())->setRepeatStatus($repeatStatus)->setProcessor($result->getPaymentInstruction()->getPaymentSystemName())->setPaymentType(CustomerOrder::PAYMENT_TYPE_CARD_PAYMENT)->setPayload($order->getPaymentStatusCallbackPayload())->setPayer($order->getPayer());
         $order->setStatusResponse($repeatStatusResponse)->setTransId($transaction->getReferenceNumber());
         $this->em->persist($order);
         $this->em->flush();
         $repeatStatusResponse->setPayer($order->getPayer());
         $this->callbackNotifier->notifyCallback($order->getPaymentStatusCallbackUrl(), $repeatStatusResponse);
         $redirectUrl = null;
         if (Result::STATUS_SUCCESS == $result->getStatus()) {
             $redirectUrl = $order->getRedirectOnSuccessUrl();
         } else {
             if (Result::STATUS_FAILED == $result->getStatus()) {
                 $redirectUrl = $order->getRedirectOnFailureUrl();
             }
         }
         if ($redirectUrl) {
             return new Response("<html><head><script>(function () { window.location.replace('{$redirectUrl}') })()</script></head><body><a href=\"{$redirectUrl}\">Proceed to last step&hellp;</a></body></html>");
         }
     }
     return new Response();
 }