php - 在 laravel 项目 Stripe\Exception\ApiConnectionException 上连接 strip api 时出错

标签 php laravel error-handling stripe-payments

我必须将 Stripe api 连接到我的 laravel 项目,但是在启动该过程时,我遇到了以下错误消息:Unexpected error communicating with Stripe. If this problem persists, let us know at support@stripe.com. (Network error [errno 92]: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)) .我在 stackoverflow 上发现一个人有同样的错误,答案是将 TLS 1.0 升级到 TLS 1.2。我试过here但什么也没发生。我还运行了可达性测试here , 最后在进行任何 Stripe API 调用之前禁用 HTTP/2,如下 $curl = new \Stripe\HttpClient\CurlClient(); $curl->setEnableHttp2(false); \Stripe\ApiRequestor::setHttpClient($curl); .请我不知道该怎么做更多。
结帐.js:

Stripe.setPublishableKey('pk_test_51H3t5vAzmeZmoL9jwK8YCeFMxvDnuC6FsV0Rg0XvMejoZziTbS6SMQlAIkOjLj68lGqkE57Yu46jF51zg6HrnyBK00Yg8Iyy45');

var $form = $('#checkout-form');

$form.submit(function(event) {
    $('#charge-error').addClass('hidden');
    $form.find('button').prop('disabled', true);

    Stripe.card.createToken({
      number: $('#card-number').val(),
      cvc: $('#card-cvc').val(),
      exp_month: $('#card-expiry-month').val(),
      exp_year: $('#card-expiry-year').val(),
      name: $('#card-name').val()
    }, stripeResponseHandler);
    return false;
});

function stripeResponseHandler(status, response) {
    if (response.error) {
        $('#charge-error').removeClass('hidden');
        $('#charge-error').text(response.error.message);
        $form.find('button').prop('disalbed', false);
    } else {// Token was created!

    // Get the token ID:
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server:
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));

        // Submit the form:
        $form.get(0).submit();
    }
}
Controller .php:
public function postCheckout(Request $request)
    {
        $curl = new \Stripe\HttpClient\CurlClient();
        $curl->setEnableHttp2(false);
        \Stripe\ApiRequestor::setHttpClient($curl);

          //require_once("vendor/autoload.php");
          //require_once("/path/to/stripe-php/init.php");



        if (!Session::has('cart')) {
            return redirect()->route('shoppingCart');
        }
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);



        Stripe::setApiKey('sk_test_?????
');
        try {
            Charge::create(array(
            "amount" => $cart->totalPrice * 100,
            "currency" => "usd",
            "source" => $request->input('stripeToken'), // obtained with Stripe.js
            "description" => "My First Test Charge"
        ));

       } catch (\Exeption $e) {
           return redirect()->route('checkout')->with('error', $e->getMessage());
       }

       Session::forget('cart');
       return redirect()->route('home')->with('succes', 'Successfully purchaised!');
    }

}
checkout.blade.php:
<span>
    <h4>Total (USD)</h4>
        </span>
          <strong>${{ $total }}</strong>
        </li>
      </ul>
      <div id="charge-error" class="alert alert-danger {{ !Session::has('error') ? 'hidden' : '' }} "> {{ Session::get('error') }} 
    </div>

最佳答案

经过长时间的研究,解决方案强制使用 HTTP/1.1:

$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1]);
$curl->setEnableHttp2(false);
\Stripe\ApiRequestor::setHttpClient($curl);

关于php - 在 laravel 项目 Stripe\Exception\ApiConnectionException 上连接 strip api 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62886114/

相关文章:

php - 使用 php 检查目录是否为空的最佳方法

php - 如何向mysql php结果添加增量

mysql - SQL : Multiple left joins with similar tables

vba - VBA对象变量或带有 block 变量的未设置错误-Web抓取

python - 第8行: ValueError: invalid literal for int() with base 10: ' '

php - 如何从数据库获取值并显示在下拉列表中

php - Laravel 模块用于更新联属网站中的价格、库存和评论

php - Laravel 5.2 Eloquent 地在同一 id 上保存自动增量 pgsql 异常

Laravel 5.5 PHPunit测试- "A facade root has not been set."

parsing - 处理 Antlr 语法错误或如何针对意外标记提供更好的消息