2015-12-23 64 views
0

在網絡掛接條紋invoice.payment_succeeded事件的反應顯示總是響應500條紋支付網絡掛接

我的編碼invoice_payment_succeeded事件下面,

<?php 
if (Yii::app()->request->isPostRequest) { 

     try { 
      /* 
      * Initialize API 
      */ 
      Stripe::setApiKey(Yii::app()->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->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->type . ') ====', 'trace', 'stripe'); 
         Yii::log('==================================', 'trace', 'stripe'); 
         $customer_id = $event->data->object->customer;        
         $customer = Stripe_Customer::retrieve($customer_id);        
         $invoice = Stripe_Invoice::retrieve($event->data->object->id); 
         $user = TblPackageUserplan::model()->findByAttributes(array('customer_id' => $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()->db->beginTransaction(); 
         try { 


          Yii::log('Invoce period start: ' . date('Y-m-d H:i:s', $event->data->object->period_start), 'trace', 'stripe'); 
          Yii::log('Invoce period end: ' . date('Y-m-d H:i:s', $event->data->object->period_end), 'trace', 'stripe'); 

          /* Save transaction in database including post data 
          if (!$this->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->type sent by sathis to manage new plan upgrade 
          $this->manageSubscription($customer->subscriptions, $user, $event->type); 

          Yii::log('Commiting db transaction...', 'trace', 'stripe'); 
          /* NO EXCEPTION, everything is ok. Let's commit */ 
          $transaction->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->getMessage(), 'error', 'stripe'); 
          /* Something went wrong, log the error, rollback and continue */ 
          $transaction->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->sendMailToUser($customer, $invoice)) { 
            Yii::log("Mail (to user) sent", 'error', 'stripe'); 
           } else { 
            Yii::log("Mail (to user) not sent", 'error', 'stripe'); 
           } 

           if ($this->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->getMessage(), 'error', 'stripe'); 
          /* Something went wrong, log the error, rollback and continue */ 
          $transaction->rollback(); 
         } 
} 


public function manageSubscription($subscription, $user , $event_type = '') { 

$update = TblPackageUserplan::model()->updateByPk(62, array('create_date'=> '2015-08-08')); 



$period_end = $subscription->current_period_end; 
$period_start = $subscription->current_period_start; 
$plan = $subscription->plan->id; 






    Yii::log('subscription is ' . $subscription->status, 'trace', 'stripe'); 
    if ($subscription->data[0]->status == 'active') { 
    if (!$this->updateCompanyUserExpireDate($user, $period_end,$period_start)) { 
        throw new Exception('User not saved', '400'); 
       } else { 
        Yii::log('User data saved (not commited)', 'trace', 'stripe'); 
       } 

    }   



} 

什麼是錯在我的代碼?爲什麼我得到響應500.I一旦我更改了客戶ID和發票編號,就給出了靜態意味着它的工作狀況良好。如何獲取訂閱詳情$ customer-> subscriptions或 $ customer-> subscription?當前事件時如何檢查發票編號和客戶?

回答

0

在您的來電manageSubscription()

$this->manageSubscription($customer->subscriptions, $user, $event->type); 

你通過$customer->subscriptionssubscriptions屬性是列表subscription objects。但在manageSubscription()方法本身中,您將其視爲單個客戶對象。

如果您的客戶將永遠不會有一個以上的認購,這可能是簡單的電話更改爲:

$this->manageSubscription($customer->subscriptions[0], $user, $event->type); 

否則,您將需要修改方法的身體。

裏面的方法,也是這一行:

if ($subscription->data[0]->status == 'active') { 

如果$subscription是一個單一的訂閱對象,則該行應爲:

if ($subscription->status == 'active') { 

如認購對象沒有一個data屬性。

+0

如何檢查客戶是否有多個訂閱? –

+0

'count($ customer-> subscriptions)'將返回'subscriptions'屬性中的元素數量,即訂閱數量。 – Ywain

+0

爲什麼我不能更新我的數據庫中的manageSubscription函數中的任何數據?plz參考我的上述編碼 –