예제 #1
0
 public function handleSubscription(Subscription $subscription, $coupon = null)
 {
     $user = $subscription->getUserEntity();
     $customer = $this->em->getRepository(Customer::getEntityClassByUser($user))->findOneBy(['user' => $user]);
     $stripeCustomer = $this->getStripeCustomer($customer);
     if ($subscription->getStripeId()) {
         try {
             $ss = $stripeCustomer->subscriptions->retrieve($subscription->getStripeId());
             $ss->plan = $subscription->getPlanId();
             if ($coupon) {
                 $ss->coupon = $coupon;
             }
             $ss->save();
         } catch (Error\InvalidRequest $e) {
             if (404 === $e->getHttpStatus()) {
                 $subscription->stripeReset();
             }
             $ss = $stripeCustomer->subscriptions->create(['plan' => $subscription->getPlanId(), 'coupon' => $coupon]);
         }
     } else {
         $ss = $stripeCustomer->subscriptions->create(['plan' => $subscription->getPlanId(), 'coupon' => $coupon]);
     }
     $subscription->syncStripeData($ss);
     $this->em->persist($subscription);
     $this->em->flush($subscription);
     return $subscription;
 }
 public function getPlanId()
 {
     $this->__load();
     return parent::getPlanId();
 }