我在应用程序中使用 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 支持团队收到的一封电子邮件,非美国卡的发卡银行将自动执行从本地到美元的所有货币转换。我不必根据所收费卡的来源调整货币代码。