/** * Платёж совершён * * @param Order $order * * @Rest\Get("orders/{id}/complete", requirements={"id"="\d+"}) * @ParamConverter("order", class="VifeedPaymentBundle:Order") * @ApiDoc( * section="Billing API", * requirements={ * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="id заказа"} * }, * statusCodes={ * 200="Returned when successful", * 303="Returned when some user action is required by payment system", * 403="Returned when the user is not authorized to use this method", * 404="Returned when order is not found" * } * ) * * @throws \RuntimeException * @throws \JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException * * @return Response */ public function getOrderCompleteAction(Order $order) { if ($this->getUser() !== $order->getUser()) { throw new AccessDeniedHttpException(); } $instruction = $order->getPaymentInstruction(); // если заказ уже оплачен, то нам тут нечего ловить if ($instruction->getAmount() != $instruction->getApprovedAmount()) { /** @var FinancialTransaction $pendingTransaction */ $pendingTransaction = $instruction->getPendingTransaction(); if (null === $pendingTransaction) { $payment = $this->ppc->createPayment($instruction->getId(), $instruction->getAmount() - $instruction->getDepositedAmount()); } else { $payment = $pendingTransaction->getPayment(); } /** @var Result $result */ $result = $this->ppc->approveAndDeposit($payment->getId(), $payment->getTargetAmount()); if (Result::STATUS_PENDING === $result->getStatus()) { $ex = $result->getPluginException(); $order->setStatus(Order::STATUS_PENDING); $this->em->persist($order); $this->em->flush($order); if ($ex instanceof ActionRequiredException) { $action = $ex->getAction(); if ($action instanceof DownloadFile) { return $this->handleView(new View(['url' => $action->getUrl()], 303)); } elseif ($action instanceof VisitUrl) { $data = ['url' => $action->getUrl(), 'orderId' => $order->getId()]; return $this->handleView(new View($data, 303)); } throw $ex; } } } $view = new View(['status' => $order->getStatus()]); return $this->handleView($view); }
/** * 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(); }