Satanand Tiwari
Satanand Tiwari

Reputation: 496

Stripe charge card get fatal error with php rest api

I am using stripe charge card api for card payment. Token successfully created and this information goes to stripe server also. But when go for charge payment it give fatal error.

I am using amazone ubuntu instance for publish my website.

My php code is :-

$token=$this->input->post('stripeToken');
var_dump($token);

\Stripe\Stripe::setApiKey("xxx");

// Get the credit card details submitted by the form
//$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
try {
    $charge = \Stripe\Charge::create(array(
    "amount" => 100, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
));
    } catch(\Stripe\Error\Card $e) {
    // The card has been declined
}

It give fatal error.

Fatal error: Uncaught exception 'Stripe\Error\ApiConnection' with message 'Could not connect to Stripe (https://api.stripe.com/v1/charges). Please check your internet connection and try again. If this problem persists, you should check Stripe's service status at https://twitter.com/stripestatus, or let us know at [email protected]. (Network error [errno 28]: Connection timed out after 30055 milliseconds)' in /opt/lampp/htdocs/devbeacon/application/libraries/stripe/lib/HttpClient/CurlClient.php:216 Stack trace: #0 /opt/lampp/htdocs/devbeacon/application/libraries/stripe/lib/HttpClient/CurlClient.php(178): Stripe\HttpClient\CurlClient->handleCurlError('https://api.str...', 28, 'Connection time...') #1 /opt/lampp/htdocs/devbeacon/application/libraries/stripe/lib/ApiRequestor.php(184): Stripe\HttpClient\CurlClient->request('post', 'https://api.str...', Array, Array, false) #2 /opt/lampp/htdocs/devbeacon/application/libraries/stripe/lib/ApiRequestor.php(59): Stripe\ApiRequestor->_requestRaw('post', '/v1/charges', Array, Ar in /opt/lampp/htdocs/devbeacon/application/libraries/stripe/lib/HttpClient/CurlClient.php on line 216

I don't know the reason behind that.

Upvotes: 2

Views: 2287

Answers (1)

Rodrigo Murillo
Rodrigo Murillo

Reputation: 13632

The message shows Network error [errno 28], which is a curl error, defined as a CURLE_OPERATION_TIMEDOUT error. This is a connection error.

Connection errors are usually due to security group settings and not allowing outbound https, in this case to the API endpoint (https://api.stripe.com/v1/charges).

Can you try to curl https://api.stripe.com/v1/charges and confirm you can connect?

You should get the following:

{
  "error": {
    "type": "invalid_request_error",
    "message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/d
ocs/api#authentication for details, or we can help at https://support.stripe.com/."
  }
}

If you do not get that response the command line on your server, then your code will not be able to connect either. Review your outgoing security group settings, and allow outbound HTTPS to the IP of the endpoint, or 0.0.0.0/0.

Upvotes: 3

Related Questions