我在应用程序中使用Stripe作为支付处理器,并且有一些关于在使用Java库时获取错误响应而不是通过HTTP接收错误的问题,如description of errors codes on the official Stripe documentation
我用来根据以前用条纹创建的客户对象来收取信用卡的方法是:
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对象以获取该数据?
调用充电方法的方法示例可能类似于:
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会将所有付款转换为美元,因为这是货币存入我的帐户的货币吗? < / p>
更新 根据我从Stripe的支持团队收到的电子邮件,非美国卡的发卡银行将自动执行从本地到美元的所有货币转换。我不必根据被收费卡的来源调整货币代码。
答案 0 :(得分:4)
似乎CardException
会为您提供与卡相关的(无效的CVC)和与付款相关(拒绝)的错误。
From the docs,此处如何使用Charge.create()
方法:
如果充电成功,则返回充电对象。错误将是 如果出现问题,我会退回一个常见的错误来源是 卡无效或过期,或有效卡不足 平衡。
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.
至于货币,有一个货币值在地图中传递给Charge.create()
方法,这似乎表示您希望收费的货币。我不知道这与结算有什么关系