php - 在Stripe PHP上捕获特定错误以显示自定义消息

标签 php error-handling stripe-payments

我有一个带有信用卡详细信息的Stripe付款网关,可以输入。

它工作正常,但是有一个我不喜欢的错误消息,但是我在PHP中的先进程度不足以解决此问题。

function stripeResponseHandler(status, response) {
            if (response.error) {
                // Enable the submit button
                $('#payBtn').removeAttr("disabled");
                // Display the errors on the form

                $(".payment-status").html('<p>'+response.error.message+'</p>');

            } else {
                var form$ = $("#paymentFrm");
                // Get token id
                var token = response.id;
                // Insert the token into the form
                form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");

                // Submit form to the server
                form$.get(0).submit();
            }
        }

这正在捕获错误并显示response.error.message

这些错误非常大,但如果您在到期日中写入一堆数字而不是mm/yy则除外。

返回的错误是:

The 'exp_year' parameter should be an integer (instead, is 20undefined).



我对这种语言不太了解,但是我想捕获这个特定的错误,只说类似“到期日期不正确,请输入为MM/YY”

我试图做一个if语句来读取response.error.message,但是我不知道如何格式化。我也尝试找到一个error.code而不是要使用的消息,但是出现了代码invalid_number,但是如果我在IF语句中使用它,它引用所有字段的所有无效编号(包括卡号),因此我的到期声明显然不正确这个情况!

最佳答案

那实际上是JavaScript,而不是PHP。您可能将此代码保存在要在服务器上呈现的PHP文件中,但这是将在客户端上运行的JavaScript代码。

听起来您正在使用Stripe.js v2进行集成。不建议使用Stripe.js v2。相反,您应该使用Stripe Elements,它将为您处理所有在此进行的验证。

上面的链接显示了如何使用PaymentIntents创建付款,PaymentIntents是Stripe最新,功能最丰富的API。但是,如果您想继续使用Charge API,可以在此处查看如何将其与Elements一起使用:https://stripe.com/docs/payments/accept-a-payment-charges

关于php - 在Stripe PHP上捕获特定错误以显示自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031694/

相关文章:

php - eBay API 添加跟踪号 - 错误号 10007

javascript - 将函数参数值传递给该函数内部的函数

php - 使用组检索最新记录

python-3.x - Python3中的异常处理

django - Stripe 客户是在没有信用卡信息的情况下创建的

javascript - 使用 javascript 中的append() 到带有 PHP 代码的选择表单

error-handling - 如何使用问号运算符来处理 Tokio future 中的错误?

c++ - 是否可以从 C printf() 捕获错误?

stripe-payments - 购买完成后更改 Stripe 结账按钮

ruby-on-rails - 在更新中从 Stripe 订阅中删除优惠券