【问题标题】:Stripe Payment Webhook条纹支付网络钩子
【发布时间】:2016-03-29 13:20:22
【问题描述】:

在 webhook 响应中的条带 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。我在更改客户 ID 和发票 ID 后检查了测试响应,给出静态意味着它的工作正常。如何获取订阅详细信息 $customer->subscriptions 或 $客户->订阅?时事时如何查看发票编号和客户?

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    在您致电manageSubscription()

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

    你通过$customer-&gt;subscriptionssubscriptions 属性是subscription objects列表。但在 manageSubscription() 方法本身中,您将其视为单个客户对象。

    如果您的客户永远不会拥有多个订阅,则将调用更改为:

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

    否则,您需要修改方法的主体。

    在方法内部,还有这行:

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

    如果$subscription 是单个订阅对象,那么该行应该是:

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

    因为订阅对象没有data 属性。

    【讨论】:

    • 如何查看客户是否有多次订阅?
    • count($customer-&gt;subscriptions) 将返回subscriptions 属性中的元素数,即订阅数。
    • 为什么我不能更新数据库中 manageSubscription 函数中的任何数据?请参考我上面的代码
    猜你喜欢
    • 2019-04-28
    • 2015-10-11
    • 2021-10-08
    • 2020-10-14
    • 2022-08-19
    • 2017-09-14
    • 2020-11-28
    • 2021-08-02
    • 2020-12-30
    相关资源
    最近更新 更多