3

我在应用程序中使用 Stripe 作为支付处理器,并且有一些关于在使用 Java 库时获得错误响应的问题,而不是通过 HTTP 接收错误,如官方 Stripe 文档上的错误代码描述中所述

我用来根据之前使用条带创建的客户对象向信用卡收费的方法是:

public void charge(BigDecimal amount) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {
    //Convert amount to cents
    NumberFormat usdCostFormat = NumberFormat.getCurrencyInstance(Locale.US);
    usdCostFormat.setMinimumFractionDigits(1);
    usdCostFormat.setMaximumFractionDigits(2);
    double chargeAmountDollars = Double.valueOf(usdCostFormat.format(amount.doubleValue()));
    int chargeAmountCents = (int) chargeAmountDollars * 100;
    Map<String, Object> chargeParams = new HashMap<String, Object>();
    chargeParams.put("amount", chargeAmountCents);
    chargeParams.put("currency", "usd");
    chargeParams.put("customer", subscription.getCustomerId());
    Charge charge = Charge.create(chargeParams);
    //Should I be inspecting the returned charge object and throwing my own errors here?
}

该方法会引发各种异常。CardException 似乎可以为我提供有关付款错误的详细信息,但它实际上是用来检测诸如拒绝和无效卡参数之类的事情?这些异常会告诉我诸如“信用卡被拒绝”或“cvc 代码不正确”之类的信息,还是我应该检查返回的 Charge 对象以获取该数据?

调用 Charge 方法的方法示例可能类似于:

BigDecimal discount = cost.multiply(BigDecimal.valueOf(discountPercentage).setScale(2, RoundingMode.HALF_EVEN));
cost = cost.subtract(discount);

if(cost.compareTo(BigDecimal.ZERO) > 0) {
    //Charge the credit card.
    try {
        paymentManager.charge(cost);
                    //Everything went ok, return success to user.
    } catch (AuthenticationException e) {
        //Authentication with API failed. Log error.
    } catch (InvalidRequestException e) {
        //Invalid parameters, log error.
    } catch (APIConnectionException e) {
        //Network communication failure. Try again.
    } catch (CardException e) {
        String errorCode = e.getCode();
        String errorMsg = e.getParam();
        if(errorCode.equals("incorrect_number")) {
            //Tell the user the cc number is incorrect.
        } else if(errorCode.equals("invalid_cvc")) {
            //Tell the user the cvc is wrong.
        }
        //This is a sample, production will check all possible errors.
    } catch (APIException e) {
        //Something went wrong on Stripes end.
    }
}

其次,我应该担心来自美国以外的付款还是 Stripe 会为我处理所有这些?我是否需要根据用户的语言环境检测货币并设置正确的货币代码,或者 Stripe 是否会将所有付款转换为美元,因为这是存入我账户的货币?

更新: 根据我从 Stripe 支持团队收到的一封电子邮件,非美国卡的发卡银行将自动执行从本地到美元的所有货币转换。我不必根据所收费卡的来源调整货币代码。

4

1 回答 1

4

似乎CardException会给您提供与卡相关的(无效的 CVC)和与付款相关的(拒绝)错误。

从文档中,这里如何使用该Charge.create()方法:

如果收费成功,则返回一个收费对象。如果出现问题,将返回错误。常见的错误来源是无效或过期的卡,或可用余额不足的有效卡。

条纹 Java 文档

Card Errors
Type: card_error

Code                  Details
incorrect_number      The card number is incorrect
invalid_number        The card number is not a valid credit card number
invalid_expiry_month  The card's expiration month is invalid
invalid_expiry_year   The card's expiration year is invalid
invalid_cvc           The card's security code is invalid
expired_card          The card has expired
incorrect_cvc         The card's security code is incorrect
card_declined         The card was declined.
missing               There is no card on a customer that is being charged.
processing_error      An error occurred while processing the card.

至于货币,有一个货币值在 Map 中传递给该Charge.create()方法,该方法似乎表明您想要收取费用的货币。不过,我不确定这与结算有何关系。

于 2013-03-23T13:43:17.857 回答