Last active
October 28, 2022 05:12
-
-
Save A-pZ/581cb4a3808ed1651c8a6e7fddbd7594 to your computer and use it in GitHub Desktop.
決済のキャプチャ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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