Ejemplo n.º 1
0
 /**
  * Apply promocode for user and get callback
  *
  * @param $code
  * @param $callback
  * @return bool|float
  */
 public function applyCode($code, $callback = null)
 {
     $promocode = Promocode::byCode($code)->fresh()->first();
     // check if exists not used code
     if (!is_null($promocode)) {
         //
         if (!is_null($promocode->user) && $promocode->user->id !== $this->attributes['id']) {
             // callback function with false value
             if (is_callable($callback)) {
                 $callback(false);
             }
             return false;
         }
         // update promocode as it is used
         if ($promocode->update(['is_used' => true])) {
             // callback function with reward value
             if (is_callable($callback)) {
                 $callback($promocode->reward ?: true);
             }
             return $promocode->reward ?: true;
         }
     }
     // callback function with false value
     if (is_callable($callback)) {
         $callback(false);
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * Apply promocode to user that it's used from now
  *
  * @param $code
  *
  * @return bool|float
  */
 public function apply($code)
 {
     $promocode = Promocode::byCode($code)->fresh();
     // check if exists not used code
     if ($promocode->exists()) {
         $record = $promocode->first();
         $record->is_used = true;
         // update promocode as it is used
         if ($record->save()) {
             return $record->reward ?: true;
         }
     }
     return false;
 }