title: Stripe Payment Webhook
body: <p>In stripe invoice.payment_succeeded  events in webhooks response showing always Response 500.</p>
  
  <p>My coding invoice_payment_succeeded event Below,</p>
  
  <pre><code>&lt;?php 
   if (Yii::app()-&gt;request-&gt;isPostRequest) {
  
          try {
              /*
               * Initialize API
               */
              Stripe::setApiKey(Yii::app()-&gt;params['secret_key']);
  
              /*
               * Get and decode data
               */
              $postdata = file_get_contents("php://input");
              $event = json_decode($postdata);
  
  
  
  
              /*
               * Find the event in our database or save it if doesn't exist
               */
  
  
  
              /*
               * If the event hasn't been processed yet...
               */
  
  
  
                  /*
                   * We don't manage all Stripe events but only relevant events
                   * 
                   */
  
  
                  switch ($event-&gt;type) {
  
                                          /*
                       * When a payment success we get a invoice.payment_succeeded
                       */
                      case 'invoice.payment_succeeded':
  
                          Yii::log('', 'trace', 'stripe');
                          Yii::log('==================================', 'trace', 'stripe');
                          Yii::log('==== Event (' . $event-&gt;type . ') ====', 'trace', 'stripe');
                          Yii::log('==================================', 'trace', 'stripe');
                          $customer_id = $event-&gt;data-&gt;object-&gt;customer;                            
                          $customer = Stripe_Customer::retrieve($customer_id);                            
                          $invoice = Stripe_Invoice::retrieve($event-&gt;data-&gt;object-&gt;id);
                          $user = TblPackageUserplan::model()-&gt;findByAttributes(array('customer_id' =&gt; $customer_id));
  
  
  
                          /* Customer, invoice and user must exist */
                          if (!$customer || !$invoice || !$user) {
                              throw new Exception('No customer, invoice or user object retrieved');
                          }
  
                          $saved = false;
  
                          /* Begin db transaction, we will commit/rollback depending on possible exceptions */
                          $transaction = Yii::app()-&gt;db-&gt;beginTransaction();
                          try {
  
  
                             Yii::log('Invoce period start: ' . date('Y-m-d H:i:s', $event-&gt;data-&gt;object-&gt;period_start), 'trace', 'stripe');
                              Yii::log('Invoce period end: ' . date('Y-m-d H:i:s', $event-&gt;data-&gt;object-&gt;period_end), 'trace', 'stripe');
  
                              /* Save transaction in database including post data 
                              if (!$this-&gt;saveTransaction($user, $event, $postdata)) {
                                  throw new Exception('Transaction not saved', '400');
                              } else {
                                  Yii::log('Payment transaction saved (not commited)', 'trace', 'stripe');
                              }*/
  
                              /* We updated local status here using lines. Now we use subscription's customer */
                              //$event-&gt;type sent by sathis to manage new plan upgrade
                              $this-&gt;manageSubscription($customer-&gt;subscriptions, $user, $event-&gt;type);
  
                              Yii::log('Commiting db transaction...', 'trace', 'stripe');
                              /* NO EXCEPTION, everything is ok. Let's commit */
                              $transaction-&gt;commit();
                              Yii::log('Done', 'trace', 'stripe');
                              /* This flag let us know if we have to send a email or not */
                              $saved = true;
                          } catch (Exception $e) {
                              Yii::log("Rolling back transaction", 'error', 'stripe');
                              Yii::log("Exception: " . $e-&gt;getMessage(), 'error', 'stripe');
                              /* Something went wrong, log the error, rollback and continue */
                              $transaction-&gt;rollback();
                          }
                          if ($saved) {
                              /* If everything ok, send an email */
                              Yii::log("Checking if user must receive email", 'error', 'stripe');
  
                                  Yii::log("Sending", 'error', 'stripe');
                                  if ($this-&gt;sendMailToUser($customer, $invoice)) {
                                      Yii::log("Mail (to user) sent", 'error', 'stripe');
                                  } else {
                                      Yii::log("Mail (to user) not sent", 'error', 'stripe');
                                  }
  
                                  if ($this-&gt;sendMailToAdmin($user, $invoice)) {
                                      Yii::log("Mail (to admin) sent", 'error', 'stripe');
                                  } else {
                                      Yii::log("Mail (to admin) not sent", 'error', 'stripe');
                                  }
  
                          }
                  }
          }
  
          catch (Exception $e) {
                              Yii::log("Rolling back transaction", 'error', 'stripe');
                              Yii::log("Exception: " . $e-&gt;getMessage(), 'error', 'stripe');
                              /* Something went wrong, log the error, rollback and continue */
                              $transaction-&gt;rollback();
                          }
   }
  
  
  public function manageSubscription($subscription, $user , $event_type = '') {
  
  $update  =   TblPackageUserplan::model()-&gt;updateByPk(62, array('create_date'=&gt; '2015-08-08'));
  
  
  
   $period_end = $subscription-&gt;current_period_end;
   $period_start = $subscription-&gt;current_period_start;
   $plan = $subscription-&gt;plan-&gt;id;
  
  
  
  
  
  
      Yii::log('subscription is ' . $subscription-&gt;status, 'trace', 'stripe');
       if ($subscription-&gt;data[0]-&gt;status == 'active') {
      if (!$this-&gt;updateCompanyUserExpireDate($user, $period_end,$period_start)) {
                      throw new Exception('User not saved', '400');
                  } else {
                      Yii::log('User data saved (not commited)', 'trace', 'stripe');
                  }
  
      }           
  
  
  
   }
  </code></pre>
  
  <p>What's wrong in my code?Why i got response 500.I checked in Test Response once i changed that customer id and invoice id give static mean its working fine.How to take subscription details $customer->subscriptions or 
  $customer->subscription? How to check invoice id and customer when current events?</p>