php - Error connecting stripe api on laravel project StripeExceptionApiConnectionException
I have to connect Stripe api to my laravel project, but when starting the process i face this error message: 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))
. I find on stackoverflow a guy with the same error and the answer was upgrading the TLS 1.0 to TLS 1.2. I tried here but nothing happened. Also i run the test for reachability here, and in the end a disabled HTTP/2 before making any Stripe API calls as follows $curl = new \Stripe\HttpClient\CurlClient(); $curl->setEnableHttp2(false); \Stripe\ApiRequestor::setHttpClient($curl);
. Please i don't know what to do more.
checkout.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>
Answer
Solution:
After a long research the solution was forcing HTTP/1.1:
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1]);
$curl->setEnableHttp2(false);
\Stripe\ApiRequestor::setHttpClient($curl);
Source