Exemplo n.º 1
0
 /**
  * Создать счёт на пополнение баланса
  *
  * @ApiDoc(
  *     section="Billing API",
  *     input="Vifeed\PaymentBundle\Form\OrderType",
  *     resource=true,
  *     statusCodes={
  *         201="Returned when successful",
  *         400="Returned when something is wrong",
  *         403="Returned when the user is not authorized to use this method"
  *     }
  * )
  *
  * @Rest\Put("orders")
  *
  * @return Response
  */
 public function putOrderAction()
 {
     if ($this->getUser()->getType() !== User::TYPE_ADVERTISER) {
         throw new AccessDeniedHttpException();
     }
     $request = $this->get('request');
     $orderForm = $this->createForm(new OrderType());
     $orderForm->submit($request);
     if ($orderForm->isValid()) {
         /** @var Order $order */
         $order = $orderForm->getData();
         $method = $request->get('jms_choose_payment_method')['method'];
         if ($method == 'bank_transfer') {
             if (!$this->getUser()->getCompany()) {
                 throw new BadRequestHttpException('Вы не ввели реквизиты компании');
             }
             if (!$this->getUser()->getCompany()->isApproved()) {
                 throw new BadRequestHttpException('Ваша компания проверяется');
             }
         }
         if ($method == 'paypal_express_checkout') {
             $currency = 'RUB';
         } else {
             $currency = 'RUR';
         }
         $form = $this->container->get('form.factory')->create('jms_choose_payment_method', null, ['csrf_protection' => false, 'amount' => $order->getAmount(), 'currency' => $currency, 'predefined_data' => ['qiwi_wallet' => ['return_url' => $this->get('router')->generate('payment_completed', [], true), 'comment' => 'Оплата заказа №' . $order->getId(), 'lifetime' => new \DateTime('+14 day'), 'alarm' => true, 'create' => true], 'paypal_express_checkout' => ['return_url' => $this->get('router')->generate('payment_completed', [], true), 'cancel_url' => $this->get('router')->generate('payment_declined', [], true)]]]);
         $form->submit($request);
         if ($form->isValid()) {
             /** @var PaymentInstruction $instruction */
             $instruction = $form->getData();
             $this->ppc->createPaymentInstruction($instruction);
             $order->setUser($this->getUser())->setPaymentInstruction($instruction);
             $this->em->persist($order);
             $this->em->flush($order);
             return $this->getOrderCompleteAction($order);
         } else {
             $view = new View($form, 400);
         }
     } else {
         $view = new View($orderForm, 400);
     }
     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();
 }