Jump to content

How to catch exception. Stripe Payment.


Go to solution Solved by sKunKbad,

Recommended Posts

 Hi Guys,
Can someone please help me.
I have following PHP code that works fine. The only issue I am facing is, if there is a payment thats not sucessfull I just get a blank screen, and do not receive error on the screen.

However the error is dispalyed in execption, I just can catch it.
 



CODE:
<?PHP

require './lib/Stripe.php';


$ccnum=$_GET['ccNum'];
$ccmon=$_GET['ccExpMo'];
$ccyr=$_GET['ccExpYr'];
$amount=$_GET['amt'];
$jobid=$_GET['JobId'];
$multiply=100;
$amountcents=$amount*$multiply;
$cvc=172;
$description='something';





if(isset($_POST)) {
Stripe::setApiKey("stripe_key_here");
$payment = Stripe_Charge::create(array(
'amount'        => $amountcents,
'currency'      => 'gbp',
'card'          => array(
'number'=> $ccnum,
'exp_month' => $ccmon,
'exp_year'=> $ccyr,
'name'=> 'some name',
'cvc'=> $cvc),
'description'   => $description)); }

print_r($payment);

?>

Following is the error (or any other error) which I cant seem to catch as an execption.

error:
    message: "Your card has insufficient funds."
    type: "card_error"
    code: "card_declined"
    charge: ch_12asdasweAAFdgooXDUd2tvKJgW

Thanks a lot again for any advise.

Regards,

Link to comment
https://forums.phpfreaks.com/topic/295594-how-to-catch-exception-stripe-payment/
Share on other sites

  • Solution

Try something like this, maybe:

// Attempt charging the card
try
{
	// Load Stripe!
	require_once APPPATH . 'libraries/stripe/stripe-php/lib/Stripe.php';

	// Set the private key
	$mode = config_item('stripe_payments_mode');
	Stripe::setApiKey( config_item( $mode . '_secret_key') );

	// Make sure the amount if pre-formatted
	$cart_data = $this->cart_manager->get_all_cart_data();
	$amount = number_format( $cart_data['total'], 2, '.', '' );

	// Create the order description for Stripe
	$description = '';
	foreach( $cart_data['products'] as $ID => $product )
	{
		$description .= 'Qty.' . $product['cart_product_qty'] . 
			' of ' . $product['cart_product_model'] . 
			' by ' . $product['cart_product_brand'] . 
			' @ $' . number_format( $product['cart_product_price'], 2, '.', '' ) . ' USD each.' . 
			' (PN:' . $product['cart_product_part_number'] . '|ID:' . $ID . ') ';
	}

	// Charge the order:
	$charge = Stripe_Charge::create([
		'amount'      => preg_replace( '/[^0-9]/', '', $amount ),
		'currency'    => "usd",
		'card'        => $post['stripe_token'],
		'description' => $description,
		'capture'     => FALSE
	]);

	// Charge was paid !
	if( $charge->paid == TRUE )
	{
		$order_id = $this->_create_order( $post, $charge, $cart_data );
	}

	// Charge was not paid!	
	else
	{
		$errors[] = 'Payment System Error: Your payment could NOT be processed (i.e., you have not been charged) because the payment system rejected the transaction. You can try again or use another card.';
	}
}
catch( Stripe_CardError $e )
{
    // Card was declined.
	$e_json = $e->getJsonBody();
	$err = $e_json['error'];
	$errors[] = $err['message'];
}
catch( Stripe_ApiConnectionError $e )
{
	// Network connection error
    $errors[] = 'Network problem. Please reload the page and try again.';
}
catch( Stripe_InvalidRequestError $e )
{
    // You screwed up in your programming. Shouldn't happen!
    $errors[] = 'Invalid Request. Please reload the page and try again. If problems persist, please contact us.';
}
catch( Stripe_ApiError $e )
{
    // Stripe's servers are down!
    $errors[] = 'Payment server not available. Please wait, then reload the page and try again.';
}
catch( Stripe_CardError $e )
{
    // Something else that's not the customer's fault.
    $errors[] = 'Card processing error. Please reload the page and try again.';
}
  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.