Пример #1
0
 public function start($order_id, $customer_id)
 {
     if (!$this->isNotPaidOrder($customer_id, $order_id)) {
         return Response::json(['message' => 'Заказ находится в процессе оплаты'], 500);
     }
     $customer = Customer::instance()->initByExternalId($customer_id);
     if (!$customer) {
         return Response::json(['message' => 'Клиент не найден'], 500);
     }
     $autopayCard = PaymentCloud::getCustomerAutopayCard($customer_id);
     if (!$autopayCard) {
         return Response::json(['message' => 'Автоплатежи клиента выключены'], 500);
     }
     $key = 'users.agbis.sessions.customer' . $customer_id;
     $sessionId = $this->initSessionKey($customer, $customer->get()->credential->agbis_password, $key);
     if (!$sessionId) {
         return Response::json(['message' => 'Сессия клиента не найдена'], 500);
     }
     $api = new Api();
     $order = $api->getOrder($order_id, $sessionId);
     if (!$order) {
         return Response::json(['message' => 'Заказ клиента не найден'], 500);
     }
     if ($autopayCard->token == '') {
         return Response::json(['message' => 'Токен привязанной карты не найден'], 500);
     }
     OrderAutopay::unguard();
     $pay = OrderAutopay::create(['order_id' => $order_id, 'customer_id' => $customer_id, 'state' => 0]);
     $result = $api->payByToken($order_id, $autopayCard->token, $order['amount'], $order['doc_number'], $sessionId);
     $pay->comment = $result->message;
     $pay->save();
     if ($result->success) {
         $pay->state = 1;
         $pay->save();
         Mailer::succesAutoPay($customer, $order, $autopayCard);
         return Response::json(['amount' => $order['amount']]);
     }
     Mailer::errorAutoPay($customer, $order, $autopayCard);
     return Response::json(['message' => $result->message], 500);
 }