アプリケーションの支払い処理業者として Stripe を使用していますが、Stripe の公式ドキュメントのエラー コードの説明に記載されているように、HTTP 経由でエラーを受信するのではなく、Java ライブラリを使用するときにエラー応答を取得することに関していくつか質問があります。
以前にストライプで作成された顧客オブジェクトに基づいてクレジットカードに請求するために使用している方法は次のとおりです。
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 はすべての支払いを USD に換算しますか?
更新: Stripe のサポート チームから受け取ったメールによると、米国以外のカードの発行銀行は、現地通貨から米ドルへのすべての通貨換算を自動的に実行します。請求元のカードに基づいて通貨コードを調整する必要はありません。