Example #1
0
 static function newRecord($token, $payer_id, $transaction_id, $quantity)
 {
     $current_time = DateUtil::getCurrentTime();
     $user = Auth::user();
     $pay = new Payment();
     $pay->user_id = $user->id;
     $pay->method = Payment::METHOD_PAYPAL;
     $pay->mount = Payment::PAY_PRICE_PER_DAY * $quantity;
     $pay->quantity = $quantity;
     $pay->transaction_id = $transaction_id;
     $pay->ip = Util::getIP();
     $pay->user_agent = $_SERVER['HTTP_USER_AGENT'];
     $pay->date = $current_time;
     $pay->token = $token;
     $pay->payer_id = $payer_id;
     $pay->save();
     $user->role = User::ROLE_SUSCRIPTOR_PREMIUM;
     $user->premium_to = is_null($user->premium_to) ? (new DateUtil($current_time))->addDays($quantity) : (new DateUtil($user->premium_to))->addDays($quantity);
     $user->save();
 }
 /** Recibe una peticion ajax, para generar una url de video con una token especial
  * 
  * @param Request $request
  * @return type
  */
 function ajax_getVideoUrl(Request $request)
 {
     if (!$request->ajax()) {
         return;
     }
     //Evita que se un usuario pueda reproducir dos videos al mismo tiempo
     //NOTA: Esta funcion no se puede implementar, porque el evento onbeforeunload de javascript no funciona del todo en Google Chrome (Buscar una alternativa)
     /*
      if (Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_RUNNING, true)->count() > 0)
      return json_encode(array("error" => "<span style='font-size: 60pt;color: red;' class='glyphicon glyphicon-ban-circle'></span><br/>Lo sentimos, pero no puedes reproducir este contenido, por que esta cuenta ya se esta usando."));
     */
     //Tiempo de paja
     //sleep(3);
     $data = $request->all();
     //Verifica que el token no se repite
     do {
         $token = Hash::generateToken(100);
     } while (Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_TOKEN, $token)->count() > 0);
     if (strlen($data["token_video"]) > 2) {
         $parent_play = Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_TOKEN, $data["token_video"])->get();
         if (count($parent_play) > 0) {
             $parent_play = $parent_play[0];
             $parent = $parent_play->pivot->id;
         } else {
             return json_encode(array("error" => "Bad Request"));
         }
     } else {
         $parent = 0;
     }
     //Obtiene la informaciĆ³n del video
     $chapter = Chapter::firstOrNew([Chapter::ATTR_VIDEO => $data["id_video"]]);
     //Genera un registro de reproduccion y token de validacion
     Auth::user()->playbacks()->attach($data["production_id"], array(User::ATTR_PLAYBACKS_PIVOT_CHAPTER_ID => $chapter->id, User::ATTR_PLAYBACKS_PIVOT_IP => Util::getIP(), User::ATTR_PLAYBACKS_PIVOT_DATE => DateUtil::getCurrentTime(), User::ATTR_PLAYBACKS_PIVOT_TOKEN => $token, User::ATTR_PLAYBACKS_PIVOT_PARENT => $parent, User::ATTR_PLAYBACKS_PIVOT_USER_AGENT => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null));
     return json_encode(array("url" => url("get/source/video/" . $token . "/" . $data["id_video"]), "token" => $token));
 }