/**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     if ('local' != Config::get('laravel-billing::default')) {
         return $this->error('Not configured to use the "local" driver.');
     }
     // Init gateway.
     Facades\Billing::customer();
     $code = $this->ask('What is the coupon Code (eg. 20percentoff)?');
     $percent_off = $this->ask('What is the coupon Percent Off (enter for none)?');
     $amount_off = $this->ask('What is the coupon Amount Off (in cents) (enter for none)?');
     $duration_in_months = $this->ask('How many months should this coupon last (press enter for unlimited)?');
     $coupon = Gateways\Local\Models\Coupon::create(array('code' => $code, 'percent_off' => $percent_off ? $percent_off : null, 'amount_off' => $amount_off ? $amount_off : null, 'duration_in_months' => $duration_in_months ? $duration_in_months : null));
     $this->info('Coupon created successfully: ' . $coupon->id);
 }
 /**
  * Update a customer.
  *
  * @param array $properties
  *
  * @return Customer
  */
 public function update(array $properties = array())
 {
     $this->info();
     if (!empty($properties['description'])) {
         $this->local_customer->description = $properties['description'];
     }
     if (!empty($properties['email'])) {
         $this->local_customer->email = $properties['email'];
     }
     if (!empty($properties['coupon'])) {
         if ($coupon_model = Models\Coupon::where('code', $properties['coupon'])->first()) {
             $this->local_customer->coupon_id = $coupon_model->id;
         }
     }
     $this->local_customer->save();
     if (!empty($properties['card_token'])) {
         $token = $properties['card_token'];
         if ($card = $this->local_customer->cards->first()) {
             $this->card($card)->update(json_decode($token, true));
         } else {
             $this->card()->create($token);
         }
     }
     $this->gateway->apiDelay();
     $this->local_subscription = null;
     return $this;
 }
 /**
  * Create a new subscription.
  *
  * @param mixed $plan
  * @param array $properties
  *
  * @return Subscription
  */
 public function create($plan, array $properties = array())
 {
     $card_id = empty($properties['card']) ? null : $properties['card'];
     if (!empty($properties['card_token'])) {
         $card_id = $this->gateway->customer($this->local_customer)->card()->create($properties['card_token'])->id();
     }
     if (!$card_id) {
         $card_id = $this->local_customer->cards->first()->id;
     }
     $coupon_id = null;
     if ($coupon = Arr::get($properties, 'coupon')) {
         $coupon_id = Models\Coupon::where('code', $coupon)->first()->id;
     }
     $plan = Models\Plan::where('key', $plan)->first();
     $trial_ends_at = null;
     if (!empty($properties['trial_ends_at'])) {
         $trial_ends_at = date('Y-m-d H:i:s', strtotime((string) $properties['trial_ends_at']));
         if (strtotime($trial_ends_at) <= time()) {
             $trial_ends_at = date('Y-m-d H:i:s');
         }
     } else {
         if ($plan->trial_period_days) {
             $trial_ends_at = (string) Carbon::now()->addDays($plan->trial_period_days);
         }
     }
     $this->local_subscription = Models\Subscription::create(array('customer_id' => $this->local_customer->id, 'plan_id' => $plan->id, 'card_id' => Models\Card::find($card_id)->id, 'coupon_id' => $coupon_id, 'quantity' => Arr::get($properties, 'quantity', 1), 'trial_ends_at' => $trial_ends_at, 'cancel_at' => null));
     $this->gateway->apiDelay();
     $this->id = $this->local_subscription->id;
     $this->local_subscription->process();
     return $this;
 }