Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Last active October 28, 2022 05:12
Show Gist options
  • Save A-pZ/581cb4a3808ed1651c8a6e7fddbd7594 to your computer and use it in GitHub Desktop.
Save A-pZ/581cb4a3808ed1651c8a6e7fddbd7594 to your computer and use it in GitHub Desktop.
決済のキャプチャ
public PaymentIntent capturePaymentIntent(PaymentIntentId paymentIntentId) {
try {
PaymentIntent target = PaymentIntent.retrieve(paymentIntentId.value());
PaymentStatus paymentStatus = PaymentStatus.of(target.getStatus());
if (paymentStatus.requireThreeDSecure()) {
String message = String.format("3DS認証が必要です: %s", paymentIntentId.value());
throw new PaymentRuntimeException(message);
}
if (paymentStatus.alreadyCaptured()) {
String message = String.format("すでにキャプチャ済みです: %s", paymentIntentId.value());
throw new PaymentRuntimeException(message);
}
if (! paymentStatus.capturable()) {
String message = String.format("キャプチャできるステータスではありません: %s, status=%s", paymentIntentId.value(), target.getStatus());
throw new PaymentRuntimeException(message);
}
return target.capture();
} catch(StripeException e) {
throw new PaymentRuntimeException(e);
}
}
@AllArgsConstructor @Getter
public enum PaymentStatus {
/** PaymentMethod(支払い方法)が未設定 */
requires_payment_method("requires_payment_method"),
/** 支払い方法が指定されたが、Stripe側の確認処理が必要 */
requires_confirmation("requires_confirmation"),
/** 顧客による3DS認証が必要 */
requires_action("requires_action"),
/** キャプチャ待ち(オーソリ済みと実質同じで、全額返金したときもこのステータスになる) */
requires_capture("requires_capture"),
/** Stripeで決済処理中 */
processing("processing"),
/** 決済完了 */
succeeded("succeeded"),
/** その他のステータス(未定義) */
none("none"),
;
private String value;
public static PaymentStatus of(String target) {
return Arrays.stream(values())
.filter(value -> Objects.equals(value.getValue(), target))
.findFirst().orElse(none);
}
/**
* 決済確定(キャプチャ)できるステータスがを判定する。
* @return キャプチャできる場合はtrue
*/
public boolean capturable() {
return this == requires_capture;
}
/**
* 3DS(1,2問わず)が必要な決済かを返す。
* @return 3DSが発動する場合はtrue
*/
public boolean requireThreeDSecure() {
return this == requires_action;
}
/**
* すでに決済完了済みかを返す。
* @return 決済完了済みの場合はtrue
*/
public boolean alreadyCaptured() {
return this == succeeded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment