Laravel Stripe付款错误重定向无效

4xrmg8kj  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(227)

如果支付成功,它会重定向到“成功”页面,但如果支付失败,它不会重定向到“取消”页面,它看起来像下图。如果你能帮忙,我会很高兴的,谢谢。
ERROR PAGE PHOTO

else if ($payment_method == 'stripe') {

                $stripe = array(
                    "secret_key"      => $stripe_secret_key,
                    "publishable_key" => $stripe_publish_key
                );

                \Stripe\Stripe::setApiKey($stripe['secret_key']);

                $customer = \Stripe\Customer::create(array(
                    'email' => $order_email,
                    'source'  => $token
                ));

                $item_name = $item_names_data;
                $item_price = $amount * 100;
                $currency = $site_currency;
                $order_id = $purchase_token;

                $charge = \Stripe\Charge::create(array(
                    'customer' => $customer->id,
                    'amount'   => $item_price,
                    'currency' => $currency,
                    'description' => $item_name,
                    'metadata' => array(
                        'order_id' => $order_id
                    )
                ));

                $chargeResponse = $charge->jsonSerialize();

                if ($chargeResponse['paid'] == 1 && $chargeResponse['captured'] == 1) {

                    //if the payment is successful they will...

                    return view('success')->with($data_record);
                }
                else
                {
                    return redirect("cancel")->with("error", $response->getMessage()); //NOT WORKING
                }   
            }
but5z9lq

but5z9lq1#

正如@aynber所说,您需要一个PHP try-catch块来处理异常(https://www.phptutorial.net/php-oop/php-try-catch/)。
您还可能希望捕获其他Stripe异常,例如InvalidRequestException、AuthenticationException和ApiConnectionException,并且可能只需要彻底捕获一般异常。API调用并不总是按计划进行。

$messages = []; 

...

try {
       $charge = \Stripe\Charge::create(array(
                'customer' => $customer->id,
                'amount'   => $item_price,
                'currency' => $currency,
                'description' => $item_name,
                'metadata' => array(
                    'order_id' => $order_id
                )
            ));

} catch (\Stripe\Exception\CardException $e) {
        array_push($messages, $e->getMessage());
}

相关问题