/**
  * 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;
 }
Beispiel #2
0
 /**
  * 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;
 }