Charging card with Stripe - php

Everything is working fine in Stripe - the token is generated, written in the "log" part in my dashboard, etc. However there is no charge done. I got no error from Stripe or my code even if I did all the error handling given by the stripe documentation. How can I correct this?
require_once ("vendor/autoload.php");
if ($_POST) {
echo "catch if";
// 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
StripeStripe::setApiKey("myApyKey");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create a charge: this will charge the user's card
try {
echo "charging";
$charge = StripeCharge::create(array(
"amount" => 1000, // Amount in cents
"currency" => "eur",
"source" => $token,
"description" => "Example charge"
));
}
catch(StripeErrorCard $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print ('Status is:' . $e->getHttpStatus() . "\n");
print ('Type is:' . $err['type'] . "\n");
print ('Code is:' . $err['code'] . "\n");
// param is '' in this case
print ('Param is:' . $err['param'] . "\n");
print ('Message is:' . $err['message'] . "\n");
}
catch(StripeErrorRateLimit $e) {
// Too many requests made to the API too quickly
}
catch(StripeErrorInvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
}
catch(StripeErrorAuthentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
}
catch(StripeErrorApiConnection $e) {
// Network communication with Stripe failed
}
catch(StripeErrorBase $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
}
catch(Exception $e) {
// Something else happened, completely unrelated to Stripe
}

You are probably getting an error, and your code is handling that error correctly, but you error handling code doesn't actually do anything for many of the cases.
You should probably add something (such as a print call) in each catch block to see exactly what type of issue is being returned.
Alternatively, your Stripe Dashboard has a way to view your accounts Live Mode and Test Mode logs at https://dashboard.stripe.com/logs which will contain an entry for every request (successful or otherwise) that hit Stripe's servers.

Try to use below code find error issue
try {
echo "charging";
$charge = StripeCharge::create(array(
"amount" => 1000, // Amount in cents
"currency" => "eur",
"source" => $token,
"description" => "Example charge"
));
}
catch(StripeErrorCard $e) {
$error = $e->getJsonBody();
<pre>print_r($error);</pre>
//then you can handle like that
if($error == "Your card was declined."){
echo "Your credit card was declined. Please try again with an alternate card.";
}
}

Related

How can I prevent duplicate charge using Stripe and PHP?

Recently i got duplicated payments with Stripe.
They told me to use Idempotent Requests.
I made some tests and i see the error message if i tried to refresh the browser for example.
But i don't know how to make the "next step" in case of error.
I mean if my client make a payment and there is a network issue, how to do with Stripe to retry and continu the process or how to display an error message and came back to the payment's page ?
My code for now:
\Stripe\Stripe::setApiKey("xxxxxx");
$charge = \Stripe\Charge::create([
'customer' => $customer->id,
'amount' => $total_payment,
'currency' => $currency,
'description' => $description
], ["idempotency_key" => $idempotency,]);
$chargeJson = $charge->jsonSerialize();
$status = $chargeJson['status'];
if($status=="succeeded") {...
Thank you for your help, if you can just give me some information and then it will help me to improve my code ^^
Here is a functional demo of the try/catch with all the possible errors adding "Idempotent Requests field", just add your own functionality in each catch:
try {
// Use Stripe's library to make requests...
$charge = \Stripe\Charge::create([
'amount' => $amount,
'currency' => "usd",
'description' => $description,
"receipt_email" => $mail,
], [
'idempotency_key' => $uniqueID
]);
} catch(\Stripe\Exception\CardException $e) {
// Since it's a decline, \Stripe\Exception\CardException will be caught
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
// Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
// Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}

Handling pure PHP errors in CodeIgniter

I have a mobile application with an API developed with CodeIgniter, and this API performs payments by Stripe.
These payments are recursive, so once a month I charge my users. But some users cannot be charged (for an example if an user has insufficient funds on his card), and in this case Stripe throws an exception.
I'd like to be able to perform a try/catch in my controller. For now I have this code :
try {
$charge = \Stripe\Charge::create(array(
"amount" => xxx,
"currency" => "eur",
"customer" => xxx)
);
} catch (Exception $e) {
// If user has insufficient funds, perfom this code
}
I recently saw that the piece of code in the catch was never executed, and I saw that CI had its own error handling system, and I could see in my logs that errors from controllers run by cli were displayed on this view : application/views/errors/cli/error_php.php. So how can I simply detect if my Stripe code returns an exception ?
Thanks in advance for any answer :)
Try using the global namespace for your exception like this:
catch (\Exception $e) //note the backslash
Please refer Stripe's API document carefully.
They have described error handling in a very detailed manner. You can read about it here: https://stripe.com/docs/api/php#error_handling
try {
// Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
// Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}

Stripe payment with php

I am developing a website that is front end made with angularjs and backend with laravel.
I want to integrate stripe payment. I am facing difficulty to add a angularjs api to laravel for stripe payment. So, I am trying to solve it with only procedural php. here is my code.
<?php
require_once('/stripe-php/lib/Stripe.php');
require_once('/stripe-php/lib/Charge.php');
Stripe::setApiKey('my-secret-key');
$charge = Charge::create(array('amount' => 2000, 'currency' => 'usd', 'source' => $token ));
if ($charge) {
echo "Payment successcul";
}
else {
echo "Not success";
}
?>
I can get the token. But the payment is not processing and I see a blank page.
Follow this step to stripe payment.
1. download stripe folder from https://stripe.com/docs/libraries#php
2. Include the folder in your www folder in your project.
3. Make new PHP file and put this code into file.
<?php
{
require_once "./Stripe-php/init.php"; //include your stripe folder
function __construct()
{
// put your keys here
\Stripe\Stripe::setApiKey("Secret-Key");
}
//put your code into function.
$token = $_POST['stripeToken']; // Token ID
$charge = \Stripe\Charge::create([
'amount' => $20 * 100,
'currency' => 'usd',
'description' => 'Message',
'source' => $token,
]);
if($charge['status'] == "succeeded"){ // if success
$status=1;
$post['charge_id']=$charge['id'];
$post['amount']=$charge['amount'];
$post['funding']=$charge['source']['funding'];
$post['holder_name']=$charge['source']['name'];
$post['brand']=$charge['source']['brand'];
$Message="Payment Success";
}
else{
$status=2; // failed
$post=null;
$Message="Payment Failed";
}
$data["status"] = ($status > 1) ? FAILED : SUCCESS;
$data["message"]=$Message;
$data["PaymentData"]=$post;
return $data; // JSON Returen
}
?>
Include Stripes error handling in the page itself.
try {
// Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
// Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}

Stripe error: you cannot use a stripe token more than once

I keep receiving a 400 error while creating a charge with Stripe. The strange thing is that sometimes it works fine but most of the time it doesn't work at all. The payments go through each time as well. Regardless of the error. Is this php script correct for processing payments?
Note: I've checked and each time I create a charge the token is unique from the last one.
<?php
require_once('stripe.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("key");
$token = $_POST['stripeToken'];
$email = $_POST['email'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
// Create a Customer:
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token,
));
// Charge the Customer instead of the card:
$charge = \Stripe\Charge::create(array(
"amount" => 1000,
"currency" => "usd",
"customer" => $customer->id
));
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - Rate Limit.";
echo json_encode($response);
} catch (\Stripe\Error\InvalidRequest $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - Invalid request.";
echo json_encode($response);
} catch (\Stripe\Error\Authentication $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - Authentication.";
echo json_encode($response);
} catch (\Stripe\Error\ApiConnection $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - API Connection.";
echo json_encode($response);
} catch (\Stripe\Error\Base $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - Base.";
echo json_encode($response);
} catch (Exception $e) {
$response["error"] = TRUE;
$response["error_msg"] = "Error processing payment - Exception.";
echo json_encode($response);
}
?>
You are using the token twice. Once for the charge and the other to create the customer. You don't have to use a token to create a customer.
$customer = \Stripe\Customer::create(array(
"email" => $email,
// "source" => $token, //remove
));
The most likely cause is that the Token is being POSTed more than once from your client-side code - either because the customer is clicking more than once, or because the code is causing the data to be submitted more than once.

Stripe how to handle InvalidRequest error

I want to handle this error, however I can't get it to work in my catch. I've tried using multiple error phrases such as Stripe\Error\InvalidRequest and invalid_request_error, but none of them work.
Note: I have only included the necessary code, my payment system works fine.
Here's my code:
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => strtolower($active_user->currency->currency_id)
));
}
catch (Stripe\Error\InvalidRequest $e) {
$msg = "Sorry, you cannot make the same payment twice.";
}
From the Stripe API Docs section on errors:
catch (\Stripe\Error\InvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}
Could be a namespacing issue. Try referencing your exception class with respect to the global scope by using a leading slash:
catch (\Stripe\Error\InvalidRequest $e) {
Failing that, add a catch for the base PHP Exception class, and see what you can find out about what is being thrown.
Example from stripe:
Are your responses set to return json, If so, in your } catch (... $e) {
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");

Categories