简体   繁体   中英

Laravel Omnipay/Stripe unable to send request on linux

currently I have integrated omnipay/stripe to my laravel project, it was ok on my local, but when I test it on server then it returned "Invalid request: unsupported Content-Type. If error persists and you need assistance, please contact support@stripe.com." when trying to send the request, please help.

$response = $gateway->purchase([
'amount' => $amount,
'currency' => $currency,
'token' => $token,
'confirm' => true,
'description' => auth()->user()->name
])->send();

The exact text of the error Content-Type. indicates the request has an empty Content-Type header (the value is included before the period). While I cannot explain why this is happening, something is interfering with the request.

You can try making basic curl calls (like the below) from your server to help isolate whether this is network-level interference or in the application stack.

curl --request POST \
  --url 'https://api.stripe.com/v1/customers' \
  -u sk_test_123: \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data 'description=test cust'

You can try with and without the explicit header to see if this make a difference.

Finally I found a workaround, I will post it here incase any future reference. I have consulted the Stripe's technical support and their respond gave me an idea.

The causes/issue found by Stripe's support: The request is being sent with an empty Content-Type header.

I have gone through the library and try to debug what the request header was being send out but found out actually in the log it was only the "Authorization" header as default, have no idea on how the Content-Type is added in, so based on their respond, I added in 'Content-Type' => 'application/x-www-form-urlencoded' to omnipay/stripe/src/Message/AbstractRequest.php at sendData($data) . Of course you could also placed it in the more structured way than I did.

Surprisingly it works !

And they have also suggested one of the way you could try it as well, but I didn't.

$request = $gateway->purchase([
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'amount' => $amount,
'currency' => $currency,
'token' => $token,
'confirm' => true,
'description' => auth()->user()->name
])->send();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM