【问题标题】:\Stripe\Subscription::retrieve not working giving me test Test webhook error: 500\Stripe\Subscription::retrieve 不工作给我测试测试 webhook 错误:500
【发布时间】:2018-03-21 18:23:52
【问题描述】:
\Stripe\Stripe::setApiKey

此 API 在 webhook 中完美运行并获得完美结果。但是当我使用订阅 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`

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

【问题讨论】:

  • 我厌倦了从客户 ID 中检索详细信息,然后尝试取消订阅,它可以工作

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


【解决方案1】:

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

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

您可以在此处阅读有关错误处理的更多信息:https://stripe.com/docs/api/php#error_handling。

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 2020-08-03
    • 2016-03-09
    • 2016-11-10
    • 2020-01-05
    • 2021-10-24
    • 2023-03-08
    • 2015-04-15
    • 2022-01-17
    相关资源
    最近更新 更多