|
30 | 30 | import com.adyen.model.checkout.*; |
31 | 31 | import com.adyen.service.checkout.*; |
32 | 32 | import com.fasterxml.jackson.databind.JsonNode; |
| 33 | +import java.io.IOException; |
33 | 34 | import java.lang.reflect.Field; |
34 | 35 | import java.time.OffsetDateTime; |
35 | 36 | import java.util.*; |
| 37 | +import java.util.stream.Stream; |
36 | 38 | import org.junit.jupiter.api.Assertions; |
37 | 39 | import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.Arguments; |
| 42 | +import org.junit.jupiter.params.provider.MethodSource; |
| 43 | +import org.junit.jupiter.params.provider.ValueSource; |
38 | 44 | import org.mockito.ArgumentCaptor; |
39 | 45 |
|
40 | 46 | public class CheckoutTest extends BaseTest { |
@@ -942,4 +948,141 @@ public void testForwardCardDetails() throws Exception { |
942 | 948 | assertEquals(200, (int) response.getResponse().getStatus()); |
943 | 949 | assertTrue(response.getResponse().getBody().contains("PAYMENT_METHOD_ID")); |
944 | 950 | } |
| 951 | + |
| 952 | + private static Stream<Arguments> redirectPaymentMethods() { |
| 953 | + return Stream.of( |
| 954 | + Arguments.of( |
| 955 | + new CheckoutPaymentMethod(new AuPayDetails().type(AuPayDetails.TypeEnum.AUPAY)), |
| 956 | + "mocks/checkout/paymentResponseAuPay.json", |
| 957 | + "881567437271705K", |
| 958 | + "aupay"), |
| 959 | + Arguments.of( |
| 960 | + new CheckoutPaymentMethod(new DBaraiDetails().type(DBaraiDetails.TypeEnum.DBARAI)), |
| 961 | + "mocks/checkout/paymentResponseDBarai.json", |
| 962 | + "881567437271706L", |
| 963 | + "dbarai")); |
| 964 | + } |
| 965 | + |
| 966 | + /** Should make a payment with a redirect payment method */ |
| 967 | + @ParameterizedTest |
| 968 | + @MethodSource("redirectPaymentMethods") |
| 969 | + public void testPaymentRedirectSuccess( |
| 970 | + CheckoutPaymentMethod paymentMethod, |
| 971 | + String mockFile, |
| 972 | + String expectedPspReference, |
| 973 | + String expectedPaymentMethodType) |
| 974 | + throws Exception { |
| 975 | + Client client = createMockClientFromFile(mockFile); |
| 976 | + PaymentRequest paymentRequest = |
| 977 | + new PaymentRequest() |
| 978 | + .merchantAccount("YOUR_MERCHANT_ACCOUNT") |
| 979 | + .reference("YOUR_REFERENCE") |
| 980 | + .amount(new Amount().currency("EUR").value(1000L)) |
| 981 | + .returnUrl("https://your-company.example.org/checkout?shopperOrder=12xy..") |
| 982 | + .paymentMethod(paymentMethod); |
| 983 | + |
| 984 | + PaymentsApi checkout = new PaymentsApi(client); |
| 985 | + PaymentResponse paymentResponse = checkout.payments(paymentRequest); |
| 986 | + |
| 987 | + assertEquals(expectedPspReference, paymentResponse.getPspReference()); |
| 988 | + assertEquals(PaymentResponse.ResultCodeEnum.REDIRECTSHOPPER, paymentResponse.getResultCode()); |
| 989 | + assertEquals( |
| 990 | + CheckoutRedirectAction.TypeEnum.REDIRECT, |
| 991 | + paymentResponse.getAction().getCheckoutRedirectAction().getType()); |
| 992 | + assertEquals( |
| 993 | + expectedPaymentMethodType, |
| 994 | + paymentResponse.getAction().getCheckoutRedirectAction().getPaymentMethodType()); |
| 995 | + |
| 996 | + verify(client.getHttpClient()) |
| 997 | + .request( |
| 998 | + String.format("https://checkout-test.adyen.com/v%s/payments", PaymentsApi.API_VERSION), |
| 999 | + paymentRequest.toJson(), |
| 1000 | + client.getConfig(), |
| 1001 | + false, |
| 1002 | + null, |
| 1003 | + ApiConstants.HttpMethod.POST, |
| 1004 | + null); |
| 1005 | + } |
| 1006 | + |
| 1007 | + @ParameterizedTest |
| 1008 | + @ValueSource( |
| 1009 | + strings = { |
| 1010 | + "{\"type\":\"notARealPaymentMethod\"}", // type matches no known schema |
| 1011 | + "{\"storedPaymentMethodId\":\"M5N7TQ4TG5PFWR50\"}" // no type discriminator at all |
| 1012 | + }) |
| 1013 | + public void testCheckoutPaymentMethodDeserializationFailure(String json) { |
| 1014 | + IOException exception = |
| 1015 | + assertThrows(IOException.class, () -> CheckoutPaymentMethod.fromJson(json)); |
| 1016 | + assertTrue(exception.getMessage().contains("Failed deserialization for CheckoutPaymentMethod")); |
| 1017 | + } |
| 1018 | + |
| 1019 | + @Test |
| 1020 | + public void testPaymentRequestThirdPartyTokenRedundancyInfo() throws Exception { |
| 1021 | + PaymentRequest paymentRequest = |
| 1022 | + new PaymentRequest() |
| 1023 | + .merchantAccount("YOUR_MERCHANT_ACCOUNT") |
| 1024 | + .reference("YOUR_REFERENCE") |
| 1025 | + .amount(new Amount().currency("EUR").value(1000L)) |
| 1026 | + .thirdPartyTokenRedundancyInfo( |
| 1027 | + new ThirdPartyTokenRedundancyInfo() |
| 1028 | + .requestTemplateCode("TEMPLATE_CODE") |
| 1029 | + .putRequestParametersItem("key", "value")); |
| 1030 | + |
| 1031 | + String json = paymentRequest.toJson(); |
| 1032 | + assertTrue(json.contains("thirdPartyTokenRedundancyInfo")); |
| 1033 | + |
| 1034 | + PaymentRequest parsed = PaymentRequest.fromJson(json); |
| 1035 | + assertEquals( |
| 1036 | + "TEMPLATE_CODE", parsed.getThirdPartyTokenRedundancyInfo().getRequestTemplateCode()); |
| 1037 | + assertEquals( |
| 1038 | + "value", parsed.getThirdPartyTokenRedundancyInfo().getRequestParameters().get("key")); |
| 1039 | + } |
| 1040 | + |
| 1041 | + @Test |
| 1042 | + public void testCreateSessionShopperConversionId() throws Exception { |
| 1043 | + CreateCheckoutSessionRequest sessionRequest = |
| 1044 | + new CreateCheckoutSessionRequest() |
| 1045 | + .merchantAccount("YOUR_MERCHANT_ACCOUNT") |
| 1046 | + .amount(new Amount().currency("EUR").value(1000L)) |
| 1047 | + .reference("YOUR_PAYMENT_REFERENCE") |
| 1048 | + .shopperConversionId("SHOPPER_CONVERSION_ID"); |
| 1049 | + |
| 1050 | + String json = sessionRequest.toJson(); |
| 1051 | + assertTrue(json.contains("shopperConversionId")); |
| 1052 | + |
| 1053 | + CreateCheckoutSessionRequest parsed = CreateCheckoutSessionRequest.fromJson(json); |
| 1054 | + assertEquals("SHOPPER_CONVERSION_ID", parsed.getShopperConversionId()); |
| 1055 | + } |
| 1056 | + |
| 1057 | + @Test |
| 1058 | + public void testPaypalUpdateOrderRequestSerialization() throws Exception { |
| 1059 | + PaypalUpdateOrderRequest request = |
| 1060 | + new PaypalUpdateOrderRequest() |
| 1061 | + .pspReference("DZ4DPSHB4WD2WN82") |
| 1062 | + .paymentData("po7XZ...") |
| 1063 | + .amount(new Amount().currency("EUR").value(12000L)) |
| 1064 | + .deliveryMethods( |
| 1065 | + Arrays.asList( |
| 1066 | + new DeliveryMethod() |
| 1067 | + .reference("1") |
| 1068 | + .description("Express Shipping") |
| 1069 | + .type(DeliveryMethod.TypeEnum.SHIPPING) |
| 1070 | + .amount(new Amount().currency("EUR").value(1000L)) |
| 1071 | + .selected(true), |
| 1072 | + new DeliveryMethod() |
| 1073 | + .reference("2") |
| 1074 | + .description("Standard Ground") |
| 1075 | + .type(DeliveryMethod.TypeEnum.SHIPPING) |
| 1076 | + .amount(new Amount().currency("EUR").value(500L)) |
| 1077 | + .selected(false))); |
| 1078 | + |
| 1079 | + String json = request.toJson(); |
| 1080 | + PaypalUpdateOrderRequest parsed = PaypalUpdateOrderRequest.fromJson(json); |
| 1081 | + assertEquals("DZ4DPSHB4WD2WN82", parsed.getPspReference()); |
| 1082 | + assertEquals("po7XZ...", parsed.getPaymentData()); |
| 1083 | + assertEquals(12000L, parsed.getAmount().getValue().longValue()); |
| 1084 | + assertEquals(2, parsed.getDeliveryMethods().size()); |
| 1085 | + assertEquals("Express Shipping", parsed.getDeliveryMethods().get(0).getDescription()); |
| 1086 | + assertEquals(true, parsed.getDeliveryMethods().get(0).getSelected()); |
| 1087 | + } |
945 | 1088 | } |
0 commit comments