\ Stripe \ Subscription ::检索不工作给我测试测试webhook错误:500

时间:2017-10-10 11:03:17

标签: php wordpress api stripe-payments payment-gateway

\Stripe\Stripe::setApiKey

此API在webhook中完美运行并获得完美结果。但是当我使用subcription api时:

\Stripe\Subscription::retrieve

它给我测试webhook错误:500。

这是我的代码:

        $stripe_secret_key = '*****'; 
        $dbname="****"; // database name
        $usertable="*****"; // webhook table

        require_once('wp-config.php');
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

        mysql_select_db($dbname, $connection);

        require_once('webhook/vendor/autoload.php');

        // Set your secret key: remember to change this to your live secret key in production

        // See your keys here: https://dashboard.stripe.com/account/apikeys

        \Stripe\Stripe::setApiKey($stripe_secret_key);
        // Retrieve the request's body and parse it as JSON

        $input = @file_get_contents("php://input");

        $event_json = json_decode($input);

        $event_type = $event_json->type;
        $chargeId = $event_json->data->object->charge;
        $subcriptionKey = $event_json->data->object->lines->data[0]->id;
        $customerId = $event_json->data->object->customer;
        $amoutDue = $event_json->data->object->amount_due;
        $paidStatus = $event_json->data->object->paid;
        $interval = $event_json->data->object->lines->data[0]->plan->interval_count;

        //insert query
        $sql = "INSERT INTO epti_webhook ".
               "(subscription_id,charge_id, customer_id,event_type, month_interval, paidStatus, data_response) "."VALUES ".
               "('$subcriptionKey','$chargeId','$customerId','$event_type','$interval','$paidStatus','$input')";
        $retval = mysql_query( $sql, $connection );

        if(! $retval ) {
            die('Could not enter data: ' . mysql_error());
        }

        echo "Entered data successfully\n";

        //count query
        $result=mysql_query("SELECT count(*) as total from epti_webhook where subscription_id = '".$subcriptionKey."'");
        $data=mysql_fetch_assoc($result);
        echo $data['total'].'===';
        if($data['total'] > '1'){

               //issue is coming here

            $sub = \Stripe\Subscription::retrieve("sub_A****");
            $sub->cancel();

        }
        http_response_code(200); // PHP 5.4 or greater`

需要帮助的人。我也更新了最新的条带,但它给了我同样的错误。 提前谢谢

1 个答案:

答案 0 :(得分:0)

\Stripe\Stripe::setApiKey()只是在本地设置API密钥 - 它实际上并不向Stripe的API发送任何请求。

检索订阅时代码可能失败的原因有很多。例如,如果Stripe的API返回错误,则此错误将作为\Stripe\Error\...异常引发。您应该在try/catch块中将所有请求包装到Stripe的API中,以正确处理错误。

您可以在此处详细了解错误处理:https://stripe.com/docs/api/php#error_handling