예제 #1
0
 public function token()
 {
     if (!Input::has('payment_id')) {
         return Response::json(['errors' => [], 'message' => trans('pay.prepayment.search_card_error')]);
     }
     $paymentId = Input::get('payment_id');
     $target = Input::get('target');
     $order_id = $this->parsePayTarget($target, Input::get('id'));
     // проверим id
     $order_id = (int) $order_id;
     if ($order_id <= 0) {
         return $this->responseErrorMessage('Отсутствует id заказа', 400);
     }
     // проверим существование заказа у данного клиента
     $api = new Api();
     $order = $api->getOrder($order_id);
     if (!$order) {
         if ($target == 'subscription') {
             return $this->responseErrorMessage('Вероятно, эта подписка уже была оплачена', 404);
         }
         return $this->responseErrorMessage('Заказ не найден', 404);
     }
     // проверим, что заказ не находится в процессинге оплаты
     $check = $this->isOrderPayWaiting($order_id);
     if ($check !== true) {
         return $check;
     }
     // проверим, что заказ можно оплатить в api
     try {
         $api->IsGoodOrder($order_id, $api->id());
     } catch (ApiException $e) {
         return $this->responseErrorMessage('Оплата заказа не доступна', 403);
     }
     $token = PaymentCloud::getToken($api->id(), $paymentId);
     if ($token) {
         $result = $api->payByToken($order_id, $token->token, $order['amount'], $order['doc_number']);
         if (!$result->success) {
             return $this->responseErrorMessage($result->message, 200);
         }
         return Response::json(['data' => '', 'message' => $result->message]);
     }
     return Response::json(['errors' => [], 'message' => 'Ошибка! Карта не найдена!']);
 }