I am very new to stripe payment gateway integration. I am facing a problem on catching errors. I saw their official documentation for catching errors, but nothing is working. My payment is working fine and when I enter the correct information stripe takes my payment and successfully redirect to the success page, but the problem happens when I tried with wrong information it doesn't give me any error just shows HTTP 500 error (In production environment). When I set error_reporting(E_ALL) then it shows me the actual errors but I can't catch those errors on Try Catch.
Here is my controller
//check whether stripe token is not empty
if (!empty($_POST['stripeToken'])) {
$stripe = array(
"secret_key" => "secret key",
"publishable_key" => "public key"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
//add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $itemPrice * 100,
'currency' => $currency,
'description' => $itemNumber,
'metadata' => array(
'item_id' => $itemNumber
)
));
$chargeJson = $charge->jsonSerialize();
} 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) {
echo "I am this error";
$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\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
}
here is the error if I turn on error_reporting(E_ALL)
PHP error. According to the Strip's documentation I should catch the errors in Try Catch block, but I am failing to catch the errors. It is in local server but in live server it is same.
Related
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
}
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
}
I am currently developing the " payment " of my site and I use Symfony2 and Stripe . When I make a payment with a "valid" map all goes well but when I test a payment error I get an error 500. I would like to access the response of the payment order to decide on the continuation of the process but I don ' unable to do so. Symfony blocking me with his error 500 and despite all feasible var_dump I can not debug the application ...
Could anyone help me ? Thank you in advance !
Here is my code :
/**
* #Route("/confirm/{offerId}", name="order_confirm")
* #Template()
*/
public function chargeAction(Request $request, $offerId)
{
$em = $this->getDoctrine()->getManager();
$offer = $em->getRepository("CoreBundle:Offer")->findOneById($offerId);
$stripe = array(
'secret_key' => '**********',
'publishable_key' => '*****'
);
$apiKey = new Stripe();
$apiKey->setApiKey($stripe['secret_key']);
$order = new MemberOrder();
$form = $this->createForm(new MemberOrderType(),$order);
$order->setDate(new \DateTime("now"));
$order->setState("created");
if ($request->getMethod() == 'POST') {
$form->getData();
$order->setAmount($offer->getPriceDisplayed());
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => $this->getUser()->getEmail(),
'source' => $token
));
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'currency' => 'eur',
'amount' => $offer->getPrice()
));
var_dump($charge);die();
} 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
}
}
return $this->render('OrderBundle:Order:charge.html.twig', array(
'title_controller' => "contact",
'title_action' => "Validez votre paiement",
'form' => $form->createView(),
'offer' => $offer
));
}
The answer on Symfony's page project :
Symfony
Erreur 500
Your card was declined.
Retour à l'accueil
For Charging customers I built this code and get a PCI Compliance server.
Everything works as expected when user enters right cc information but when user inputs any wrong information like cc expiry or incorrect card number it fails to catch error and throw error on page like this
Fatal error: Uncaught exception 'Stripe_CardError' with message 'Your card number is incorrect.' in /home/treehouse/workspace/lib/Stripe/ApiRequestor.php:148 Stack trace: #0 /home/treehouse/workspace/lib/Stripe/ApiRequestor.php(254): Stripe_ApiRequestor->handleApiError('{? "error": {?...', 402, Array) #1 /home/treehouse/workspace/lib/Stripe/ApiRequestor.php(104): Stripe_ApiRequestor->_interpretResponse('{? "error": {?...', 402) #2 /home/treehouse/workspace/lib/Stripe/ApiResource.php(126): Stripe_ApiRequestor->request('post', '/v1/tokens', Array) #3 /home/treehouse/workspace/lib/Stripe/Token.php(26): Stripe_ApiResource::_scopedCreate('Stripe_Token', Array, NULL) #4 /home/treehouse/workspace/Proccess-Card.php(54): Stripe_Token::create(Array) #5 {main} thrown in /home/treehouse/workspace/lib/Stripe/ApiRequestor.php on line 148
<?php
$ccn = $_POST['ccn'];
$ccm = $_POST['ccm'];
$ccy = $_POST['ccy'];
$cvv = $_POST['cvv'];
$api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
$amount = '$_POST['amount'];
require_once('./lib/Stripe.php');
Stripe::setApiKey($api);
$token = Stripe_Token::create(#array(
"card" => array(
"number" => $ccn,
"exp_month" => $ccm,
"exp_year" => $ccy,
"cvc" => $cvv
)
));
try {
$charge = Stripe_Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser#example.com")
);
} catch(Stripe_CardError $e) {
// Since it's a decline, Stripe_CardError 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_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
} catch (Stripe_Error $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
}
?>
I also tried doing all these things with latest stripe library 3.4 and use code to charge stated on the site.
<?php
$ccn = $_POST['ccn'];
$ccm = $_POST['ccm'];
$ccy = $_POST['ccy'];
$cvv = $_POST['cvv'];
$api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
$amount = '$_POST['amount'];
require_once('./stripe/init.php');
Stripe::setApiKey($api);
$token = \Stripe\Token::create(#array(
"card" => array(
"number" => $ccn,
"exp_month" => $ccm,
"exp_year" => $ccy,
"cvc" => $cvv
)
));
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser#example.com")
);
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, Stripe_CardError 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\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\ $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
}
?>
Use Try Catch for Stripe_Charge::create
<?php
$ccn = $_POST['ccn'];
$ccm = $_POST['ccm'];
$ccy = $_POST['ccy'];
$cvv = $_POST['cvv'];
$api = 'sk_test_yEW8dYhX4xiAzpaRiWCr7UbC';
$amount = '$_POST['amount'];
require_once('./stripe/init.php');
Stripe::setApiKey($api);
try{
$token = \Stripe\Token::create(#array(
"card" => array(
"number" => $ccn,
"exp_month" => $ccm,
"exp_year" => $ccy,
"cvc" => $cvv
)
));
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => "payinguser#example.com")
);
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, Stripe_CardError 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\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\ $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
}
?>
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");