diff --git a/src/main/java/com/checkout/common/MarketplaceData.java b/src/main/java/com/checkout/common/MarketplaceData.java index 1154160d..17df3dba 100644 --- a/src/main/java/com/checkout/common/MarketplaceData.java +++ b/src/main/java/com/checkout/common/MarketplaceData.java @@ -7,14 +7,29 @@ import java.util.List; +/** + * The marketplace data of a payment. Not part of the API specification. + * + * @deprecated Not part of the API specification. Use {@link AmountAllocations} instead. This class will + * be removed in the next major version. + */ +@Deprecated @Data @Builder @NoArgsConstructor @AllArgsConstructor public final class MarketplaceData { + /** + * The sub-entity that the payment is being processed on behalf of. + * [Optional] + */ private String subEntityId; + /** + * The sub-entities that the payment is being processed on behalf of. + * [Optional] + */ private List subEntities; } diff --git a/src/main/java/com/checkout/common/MarketplaceDataSubEntity.java b/src/main/java/com/checkout/common/MarketplaceDataSubEntity.java index 4433a0cc..fb7b485a 100644 --- a/src/main/java/com/checkout/common/MarketplaceDataSubEntity.java +++ b/src/main/java/com/checkout/common/MarketplaceDataSubEntity.java @@ -5,28 +5,66 @@ import lombok.Data; import lombok.NoArgsConstructor; +/** + * A sub-entity of the marketplace data of a payment. Not part of the API specification. + * + * @deprecated Not part of the API specification. Use {@link AmountAllocations} instead. This class will + * be removed in the next major version. + */ +@Deprecated @Data @Builder @NoArgsConstructor @AllArgsConstructor public final class MarketplaceDataSubEntity { + /** + * The id of the sub-entity. + * [Optional] + */ private String id; + /** + * The split amount, in the minor currency unit. + * [Optional] + */ private Long amount; + /** + * A reference you can later use to identify this split. + * [Optional] + */ private String reference; + /** + * The commission to collect from this split. + * [Optional] + */ private MarketplaceCommission commission; + /** + * The commission of a marketplace sub-entity. Not part of the API specification. + * + * @deprecated Not part of the API specification. Use {@link Commission} instead. This class will be + * removed in the next major version. + */ + @Deprecated @Data @Builder @NoArgsConstructor @AllArgsConstructor public static final class MarketplaceCommission { + /** + * The fixed amount of commission to collect, in the minor currency unit. + * [Optional] + */ private Long amount; + /** + * The percentage of commission to collect. + * [Optional] + */ private Double percentage; } diff --git a/src/main/java/com/checkout/payments/CaptureRequest.java b/src/main/java/com/checkout/payments/CaptureRequest.java index 1aed3c5d..6b57f336 100644 --- a/src/main/java/com/checkout/payments/CaptureRequest.java +++ b/src/main/java/com/checkout/payments/CaptureRequest.java @@ -12,40 +12,97 @@ import java.util.List; import java.util.Map; +/** + * Request to capture a payment (POST /payments/{id}/captures). + */ @Data @Builder @NoArgsConstructor @AllArgsConstructor public final class CaptureRequest { + /** + * The amount to capture. If not specified, the full payment amount will be captured. + * [Optional] + * Format: int64 + * min 0 + * max 9999999999 + */ private Long amount; + /** + * The type of capture. If set to Final, the remaining available-to-capture balance will be voided. + * [Optional] + */ private CaptureType captureType; + /** + * A reference you can later use to identify this capture request. For Amex, the string limit is 30 characters. + * [Optional] + * max 80 characters + */ private String reference; + /** + * The customer's details. Required if source.type is tamara. + * [Optional] + */ private PaymentCustomerRequest customer; + /** + * A description of the payment. + * [Optional] + * max 100 characters + */ private String description; + /** + * An optional description that is displayed on the customer's statement identifying a purchase. + * [Optional] + */ private BillingDescriptor billingDescriptor; + /** + * The shipping details. + * [Optional] + */ private ShippingDetails shipping; + /** + * The order's line items. + * [Optional] + */ private List items; /** - * @deprecated This property will be removed in the future, and should be used - * {@link CaptureRequest#amountAllocations} instead + * The marketplace data of the capture. Not part of the API specification. + * [Optional] + * + * @deprecated Not part of the API specification. Use {@link CaptureRequest#amountAllocations} + * instead. This property will be removed in the next major version. */ @Deprecated private MarketplaceData marketplace; + /** + * The sub-entities that the payment is being processed on behalf of. + * [Optional] + */ private List amountAllocations; + /** + * Use the processing object to influence or override the data sent during card processing. + * [Optional] + */ private ProcessingSettings processing; + /** + * A set of key-value pairs to attach to the authorization request. You can use this to store additional + * information in a structured format. The metadata object only supports primitive data types. Objects and + * arrays are not supported. + * [Optional] + */ @Builder.Default private Map metadata = new HashMap<>(); -} \ No newline at end of file +} diff --git a/src/main/java/com/checkout/payments/PaymentAction.java b/src/main/java/com/checkout/payments/PaymentAction.java index 890d5e10..38c6352a 100644 --- a/src/main/java/com/checkout/payments/PaymentAction.java +++ b/src/main/java/com/checkout/payments/PaymentAction.java @@ -12,42 +12,110 @@ import java.util.List; import java.util.Map; +/** + * An action of a payment, as returned by GET /payments/{id}/actions. + */ @Data @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public final class PaymentAction extends Resource { + /** + * The unique identifier of the payment action. + * [Required] + * Pattern: ^(act)_(\w{26})$ + * max 30 characters + * min 30 characters + */ private String id; + /** + * The type of action. + * [Required] + */ private ActionType type; + /** + * The date/time the action was processed. + * [Required] + * Format: date-time + */ private Instant processedOn; + /** + * The action amount. + * [Required] + */ private Long amount; + /** + * Whether the action was successful. + * [Optional] + */ private Boolean approved; + /** + * The issuer authorization code for cards. Only applicable for payins. + * [Optional] + */ private String authCode; + /** + * The Gateway response code. + * [Required] + */ private String responseCode; + /** + * The Gateway response summary. + * [Optional] + */ private String responseSummary; + /** + * The authorization type. Only applicable for payins. + * [Optional] + */ private AuthorizationType authorizationType; + /** + * Your reference for the action. Only applicable for payins. + * [Optional] + */ private String reference; /** - * @deprecated This property will be removed in the future, and should be used - * {@link PaymentAction#amountAllocations} instead + * The marketplace data of the action. Not part of the API specification. + * [Optional] + * + * @deprecated Not part of the API specification. Use {@link PaymentAction#amountAllocations} + * instead. This property will be removed in the next major version. */ @Deprecated private MarketplaceData marketplace; + /** + * The sub-entities that the payment is being processed on behalf of. Only applicable for payins. + * [Optional] + */ private List amountAllocations; + /** + * Returns information related to the processing of the payment. Only applicable for payins. + * [Optional] + */ private Processing processing; + /** + * The order's line item or product that is being purchased. Only applicable for payins. + * [Optional] + */ + private List items; + + /** + * A set of key-value pairs that you can attach to an action. Only applicable for payins. + * [Optional] + */ private Map metadata = new HashMap<>(); -} \ No newline at end of file +} diff --git a/src/main/java/com/checkout/payments/request/PaymentRequest.java b/src/main/java/com/checkout/payments/request/PaymentRequest.java index 6c93a7f1..5acac2ca 100644 --- a/src/main/java/com/checkout/payments/request/PaymentRequest.java +++ b/src/main/java/com/checkout/payments/request/PaymentRequest.java @@ -227,9 +227,12 @@ public final class PaymentRequest { private PaymentRecipient recipient; /** - * The marketplace data for the payment. No longer in the API specification. + * The marketplace data for the payment. Not part of the API specification: the API rejects a payment + * request that carries it (422 marketplace_not_supported). * [Optional] - * @deprecated Use {@link PaymentRequest#amountAllocations} instead. + * + * @deprecated Not part of the API specification and rejected by the API (422 marketplace_not_supported). Use + * {@link PaymentRequest#amountAllocations} instead. This property will be removed in the next major version. */ @Deprecated private MarketplaceData marketplace; diff --git a/src/main/java/com/checkout/payments/response/GetPaymentResponse.java b/src/main/java/com/checkout/payments/response/GetPaymentResponse.java index 7df74592..23b18c5a 100644 --- a/src/main/java/com/checkout/payments/response/GetPaymentResponse.java +++ b/src/main/java/com/checkout/payments/response/GetPaymentResponse.java @@ -199,11 +199,11 @@ public final class GetPaymentResponse extends Resource { private String paymentIp; /** - * Not part of the get payment details schema in the API reference. + * The marketplace data of the payment. Not part of the API specification. * [Optional] * - * @deprecated This property will be removed in the future, and should be used - * {@link GetPaymentResponse#amountAllocations} instead + * @deprecated Not part of the API specification. Use {@link GetPaymentResponse#amountAllocations} + * instead. This property will be removed in the next major version. */ @Deprecated private MarketplaceData marketplace; diff --git a/src/test/java/com/checkout/OAuthTestIT.java b/src/test/java/com/checkout/OAuthTestIT.java index e3e34997..a46a2e6e 100644 --- a/src/test/java/com/checkout/OAuthTestIT.java +++ b/src/test/java/com/checkout/OAuthTestIT.java @@ -3,9 +3,9 @@ import com.checkout.common.AccountHolderIdentification; import com.checkout.common.AccountHolderIdentificationType; import com.checkout.common.Address; +import com.checkout.common.AmountAllocations; import com.checkout.common.CountryCode; import com.checkout.common.Currency; -import com.checkout.common.MarketplaceData; import com.checkout.payments.request.PaymentRequest; import com.checkout.payments.request.source.RequestCardSource; import com.checkout.payments.sender.PaymentIndividualSender; @@ -13,6 +13,7 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.Collections; import java.util.UUID; import static java.net.URI.create; @@ -63,7 +64,13 @@ void shouldMakeOAuthCall() { .amount(10L) .currency(Currency.EUR) .processingChannelId("pc_a6dabcfa2o3ejghb3sjuotdzzy") - .marketplace(MarketplaceData.builder().subEntityId("ent_ocw5i74vowfg2edpy66izhts2u").build()) + // The OAuth client's entity must allocate the payment to a sub-entity; the sandbox refuses + // the former marketplace object (marketplace_not_supported) and a payment without an + // allocation (amount_allocations_required). + .amountAllocations(Collections.singletonList(AmountAllocations.builder() + .id("ent_ocw5i74vowfg2edpy66izhts2u") + .amount(10L) + .build())) .build(); assertNotNull(blocking(() -> checkoutApi.paymentsClient().requestPayment(request))); diff --git a/src/test/java/com/checkout/payments/PaymentsSerializationTest.java b/src/test/java/com/checkout/payments/PaymentsSerializationTest.java new file mode 100644 index 00000000..73f516d2 --- /dev/null +++ b/src/test/java/com/checkout/payments/PaymentsSerializationTest.java @@ -0,0 +1,288 @@ +package com.checkout.payments; + +import com.checkout.GsonSerializer; +import com.checkout.common.Address; +import com.checkout.common.AmountAllocations; +import com.checkout.common.Commission; +import com.checkout.common.CountryCode; +import com.checkout.common.MarketplaceData; +import com.checkout.common.MarketplaceDataSubEntity; +import com.checkout.common.Phone; +import com.checkout.payments.request.ItemSubType; +import com.checkout.payments.request.ItemType; +import com.checkout.payments.request.PaymentCustomerRequest; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.LocalDate; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PaymentsSerializationTest { + + private final GsonSerializer serializer = new GsonSerializer(); + + // CaptureRequest (POST /payments/{id}/captures) + + @Test + void shouldSerializeEmptyCaptureRequest() { + final String json = serializer.toJson(CaptureRequest.builder().build()); + + assertEquals("{\"metadata\":{}}", json); + } + + @Test + void shouldRoundTripCaptureRequestWithAllProperties() { + final CaptureRequest original = createFullCaptureRequest(); + + final String json = serializer.toJson(original); + final JsonObject wire = JsonParser.parseString(json).getAsJsonObject(); + for (final String name : new String[]{"amount", "capture_type", "reference", "customer", "description", + "billing_descriptor", "shipping", "items", "amount_allocations", "processing", "metadata"}) { + assertTrue(wire.has(name), name); + } + assertEquals("NonFinal", wire.get("capture_type").getAsString()); + assertEquals("ent_w4jelhppmfiufdnatam37wrfc4", + wire.getAsJsonArray("amount_allocations").get(0).getAsJsonObject().get("id").getAsString()); + assertFalse(wire.has("marketplace")); + + final CaptureRequest deserialized = serializer.fromJson(json, CaptureRequest.class); + + assertEquals(original.getAmount(), deserialized.getAmount()); + assertEquals(original.getCaptureType(), deserialized.getCaptureType()); + assertEquals(original.getReference(), deserialized.getReference()); + assertEquals(original.getCustomer(), deserialized.getCustomer()); + assertEquals(original.getDescription(), deserialized.getDescription()); + assertEquals(original.getBillingDescriptor(), deserialized.getBillingDescriptor()); + assertEquals(original.getShipping(), deserialized.getShipping()); + assertEquals(original.getItems(), deserialized.getItems()); + assertEquals(original.getAmountAllocations(), deserialized.getAmountAllocations()); + assertEquals(original.getProcessing(), deserialized.getProcessing()); + assertEquals(original.getMetadata(), deserialized.getMetadata()); + } + + @Test + @SuppressWarnings("deprecation") + void shouldKeepSerializingDeprecatedMarketplaceInCaptureRequest() { + final CaptureRequest request = CaptureRequest.builder() + .marketplace(MarketplaceData.builder().subEntityId("ent_w4jelhppmfiufdnatam37wrfc4").build()) + .build(); + + final String json = serializer.toJson(request); + + assertEquals("{\"marketplace\":{\"sub_entity_id\":\"ent_w4jelhppmfiufdnatam37wrfc4\"},\"metadata\":{}}", json); + assertEquals("ent_w4jelhppmfiufdnatam37wrfc4", + serializer.fromJson(json, CaptureRequest.class).getMarketplace().getSubEntityId()); + } + + // PaymentAction (GET /payments/{id}/actions) + + @Test + void shouldDeserializePaymentActionWithAllProperties() { + final String json = "{" + + "\"id\":\"act_y3oqhf46pyzuxjbcn2giaqnb44\"," + + "\"type\":\"Authorization\"," + + "\"processed_on\":\"2018-01-17T09:30:48Z\"," + + "\"amount\":6540," + + "\"approved\":true," + + "\"auth_code\":\"643381\"," + + "\"response_code\":\"10000\"," + + "\"response_summary\":\"Approved\"," + + "\"authorization_type\":\"Estimated\"," + + "\"reference\":\"ORD-5023-4E89\"," + + "\"amount_allocations\":[{\"id\":\"ent_w4jelhppmfiufdnatam37wrfc4\",\"amount\":6540," + + "\"reference\":\"ORD-5023-4E89\",\"commission\":{\"amount\":1000,\"percentage\":1.125}}]," + + "\"processing\":{\"retrieval_reference_number\":\"909913440644\"," + + "\"acquirer_reference_number\":\"24021219099007452440793\"," + + "\"acquirer_transaction_id\":\"440644309099499894406\"}," + + "\"items\":[{\"name\":\"Kevlar batterang\",\"quantity\":2,\"unit_price\":50," + + "\"reference\":\"858818ac\",\"total_amount\":100}]," + + "\"metadata\":{\"coupon_code\":\"NY2018\"}," + + "\"_links\":{\"self\":{\"href\":\"https://api.checkout.com/payments/pay_1/actions\"}}" + + "}"; + + final PaymentAction action = serializer.fromJson(json, PaymentAction.class); + + assertEquals("act_y3oqhf46pyzuxjbcn2giaqnb44", action.getId()); + assertEquals(ActionType.AUTHORIZATION, action.getType()); + assertEquals(Instant.parse("2018-01-17T09:30:48Z"), action.getProcessedOn()); + assertEquals(6540L, action.getAmount()); + assertEquals(Boolean.TRUE, action.getApproved()); + assertEquals("643381", action.getAuthCode()); + assertEquals("10000", action.getResponseCode()); + assertEquals("Approved", action.getResponseSummary()); + assertEquals(AuthorizationType.ESTIMATED, action.getAuthorizationType()); + assertEquals("ORD-5023-4E89", action.getReference()); + assertEquals(1, action.getAmountAllocations().size()); + final AmountAllocations allocation = action.getAmountAllocations().get(0); + assertEquals("ent_w4jelhppmfiufdnatam37wrfc4", allocation.getId()); + assertEquals(6540L, allocation.getAmount()); + assertEquals("ORD-5023-4E89", allocation.getReference()); + assertEquals(1000L, allocation.getCommission().getAmount()); + assertEquals(1.125, allocation.getCommission().getPercentage()); + assertEquals("909913440644", action.getProcessing().getRetrievalReferenceNumber()); + assertEquals("24021219099007452440793", action.getProcessing().getAcquirerReferenceNumber()); + assertEquals("440644309099499894406", action.getProcessing().getAcquirerTransactionId()); + assertEquals(1, action.getItems().size()); + final ProductResponse item = action.getItems().get(0); + assertEquals("Kevlar batterang", item.getName()); + assertEquals(2L, item.getQuantity()); + assertEquals(50L, item.getUnitPrice()); + assertEquals("858818ac", item.getReference()); + assertEquals(100L, item.getTotalAmount()); + assertEquals("NY2018", action.getMetadata().get("coupon_code")); + assertNotNull(action.getLink("self")); + + final PaymentAction roundTripped = serializer.fromJson(serializer.toJson(action), PaymentAction.class); + assertEquals(action, roundTripped); + } + + @Test + void shouldDeserializePaymentActionsSwaggerExample() { + // The spec example sends auth_code, response_code and the processing identifiers as JSON numbers + // although the schema types them as strings; the SDK reads them as strings. + final String json = "[{\"id\":\"act_fd3h6evhpn3uxdoqbuu3lqnqbm\",\"type\":\"Refund\"," + + "\"processed_on\":\"2018-01-20T10:30:48.0000000+00:00\",\"amount\":1000,\"approved\":true," + + "\"response_code\":10000,\"response_summary\":\"Approved\"}," + + "{\"id\":\"act_gefycn3jcvuupboxfmqrhk2aym\",\"type\":\"Capture\"," + + "\"processed_on\":\"2018-01-17T10:30:48.0000000+00:00\",\"amount\":6540,\"approved\":true," + + "\"response_code\":10000,\"response_summary\":\"Approved\"," + + "\"processing\":{\"acquirer_reference_number\":2.402121909900745E+22," + + "\"acquirer_transaction_id\":745244079},\"metadata\":{\"shipping_ref\":\"MQIBN2\"}}," + + "{\"id\":\"act_y3oqhf46pyzuxjbcn2giaqnb44\",\"type\":\"Authorization\"," + + "\"processed_on\":\"2018-01-17T09:30:48.0000000+00:00\",\"amount\":6540,\"approved\":true," + + "\"auth_code\":643381,\"response_code\":10000,\"response_summary\":\"Approved\"," + + "\"reference\":\"ORD-5023-4E89\",\"processing\":{\"retrieval_reference_number\":909913440644," + + "\"acquirer_transaction_id\":440644309099499900000}}]"; + + final PaymentAction[] actions = serializer.fromJson(json, PaymentAction[].class); + + assertEquals(3, actions.length); + assertEquals("act_fd3h6evhpn3uxdoqbuu3lqnqbm", actions[0].getId()); + assertEquals(ActionType.REFUND, actions[0].getType()); + assertEquals(Instant.parse("2018-01-20T10:30:48Z"), actions[0].getProcessedOn()); + assertEquals(1000L, actions[0].getAmount()); + assertEquals("10000", actions[0].getResponseCode()); + assertNull(actions[0].getProcessing()); + assertEquals(ActionType.CAPTURE, actions[1].getType()); + assertEquals("745244079", actions[1].getProcessing().getAcquirerTransactionId()); + assertEquals("MQIBN2", actions[1].getMetadata().get("shipping_ref")); + assertEquals(ActionType.AUTHORIZATION, actions[2].getType()); + assertEquals("643381", actions[2].getAuthCode()); + assertEquals("ORD-5023-4E89", actions[2].getReference()); + assertEquals("909913440644", actions[2].getProcessing().getRetrievalReferenceNumber()); + } + + @Test + @SuppressWarnings("deprecation") + void shouldKeepDeserializingDeprecatedMarketplaceInPaymentAction() { + final PaymentAction action = serializer.fromJson( + "{\"marketplace\":{\"sub_entity_id\":\"ent_w4jelhppmfiufdnatam37wrfc4\"}}", PaymentAction.class); + + assertEquals("ent_w4jelhppmfiufdnatam37wrfc4", action.getMarketplace().getSubEntityId()); + } + + @Test + @SuppressWarnings("deprecation") + void shouldReadSameAllocationFromMarketplaceAndAmountAllocationsInPaymentAction() { + final String json = "{\"id\":\"act_y3oqhf46pyzuxjbcn2giaqnb44\",\"type\":\"Authorization\"," + + "\"marketplace\":{\"sub_entity_id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"," + + "\"sub_entities\":[{\"amount\":10,\"id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"}]}," + + "\"amount_allocations\":[{\"amount\":10,\"id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"}]}"; + + final PaymentAction action = serializer.fromJson(json, PaymentAction.class); + + final MarketplaceDataSubEntity subEntity = action.getMarketplace().getSubEntities().get(0); + final AmountAllocations allocation = action.getAmountAllocations().get(0); + assertEquals("ent_ocw5i74vowfg2edpy66izhts2u", action.getMarketplace().getSubEntityId()); + assertEquals(allocation.getId(), subEntity.getId()); + assertEquals(allocation.getAmount(), subEntity.getAmount()); + assertEquals(10L, allocation.getAmount()); + } + + private static CaptureRequest createFullCaptureRequest() { + final Map metadata = new HashMap<>(); + metadata.put("coupon_code", "NY2018"); + final List items = Collections.singletonList(ProductRequest.builder() + .type(ItemType.DIGITAL) + .subType(ItemSubType.BLOCKCHAIN) + .name("Kevlar batterang") + .quantity(2L) + .unitPrice(50L) + .reference("858818ac") + .commodityCode("DEF123") + .unitOfMeasure("metres") + .totalAmount(100L) + .taxRate(2000L) + .taxAmount(20L) + .discountAmount(10L) + .wxpayGoodsId("1001") + .imageUrl("https://example.com/image.png") + .url("https://example.com/item") + .sku("SKU-1") + .serviceEndsOn(LocalDate.parse("2026-12-31")) + .purchaseCountry(CountryCode.GB) + .foreignRetailerAmount(5L) + .build()); + return CaptureRequest.builder() + .amount(6540L) + .captureType(CaptureType.NON_FINAL) + .reference("ORD-5023-4E89") + .customer(PaymentCustomerRequest.builder() + .id("cus_y3oqhf46pyzuxjbcn2giaqnb44") + .email("brucewayne@gmail.com") + .name("Bruce Wayne") + .taxNumber("1350693505279") + .phone(Phone.builder().countryCode("+1").number("415 555 2671").build()) + .build()) + .description("Set of 3 masks") + .billingDescriptor(BillingDescriptor.builder() + .name("SUPERHEROES.COM") + .city("GOTHAM") + .reference("ORD-5023-4E89") + .build()) + .shipping(ShippingDetails.builder() + .firstName("Bruce") + .lastName("Wayne") + .email("brucewayne@gmail.com") + .address(Address.builder() + .addressLine1("Checkout.com") + .addressLine2("90 Tottenham Court Road") + .city("London") + .state("London") + .zip("W1T 4TJ") + .country(CountryCode.GB) + .build()) + .phone(Phone.builder().countryCode("+1").number("415 555 2671").build()) + .fromAddressZip("10014") + .build()) + .items(items) + .amountAllocations(Collections.singletonList(AmountAllocations.builder() + .id("ent_w4jelhppmfiufdnatam37wrfc4") + .amount(6540L) + .reference("ORD-5023-4E89") + .commission(Commission.builder().amount(1000L).percentage(1.125).build()) + .build())) + .processing(ProcessingSettings.builder() + .orderId("123456789") + .taxAmount(3000.0) + .discountAmount(0.0) + .dutyAmount(0.0) + .shippingAmount(300.0) + .shippingTaxAmount(100.0) + .build()) + .metadata(metadata) + .build(); + } + +} diff --git a/src/test/java/com/checkout/payments/request/PaymentRequestSerializationTest.java b/src/test/java/com/checkout/payments/request/PaymentRequestSerializationTest.java index 6173764a..7a14fd5a 100644 --- a/src/test/java/com/checkout/payments/request/PaymentRequestSerializationTest.java +++ b/src/test/java/com/checkout/payments/request/PaymentRequestSerializationTest.java @@ -6,6 +6,7 @@ import com.checkout.common.Address; import com.checkout.common.CountryCode; import com.checkout.common.Currency; +import com.checkout.common.MarketplaceData; import com.checkout.common.PaymentSourceType; import com.checkout.common.Phone; import com.checkout.payments.AuthorizationType; @@ -234,6 +235,22 @@ void shouldRoundTripSerialize() { assertEquals(Boolean.TRUE, deserialized.getCapture()); } + @Test + @SuppressWarnings("deprecation") + void shouldKeepSerializingDeprecatedMarketplace() { + final PaymentRequest request = PaymentRequest.builder() + .currency(Currency.EUR) + .marketplace(MarketplaceData.builder().subEntityId("ent_w4jelhppmfiufdnatam37wrfc4").build()) + .build(); + + final String json = serializer.toJson(request); + + assertEquals("{\"currency\":\"EUR\",\"payment_type\":\"Regular\"," + + "\"marketplace\":{\"sub_entity_id\":\"ent_w4jelhppmfiufdnatam37wrfc4\"},\"metadata\":{}}", json); + assertEquals("ent_w4jelhppmfiufdnatam37wrfc4", + serializer.fromJson(json, PaymentRequest.class).getMarketplace().getSubEntityId()); + } + @Test void shouldNotSerializeNullOptionalFields() { final PaymentRequest request = PaymentRequest.builder() diff --git a/src/test/java/com/checkout/payments/response/GetPaymentResponseSerializationTest.java b/src/test/java/com/checkout/payments/response/GetPaymentResponseSerializationTest.java new file mode 100644 index 00000000..be30f019 --- /dev/null +++ b/src/test/java/com/checkout/payments/response/GetPaymentResponseSerializationTest.java @@ -0,0 +1,43 @@ +package com.checkout.payments.response; + +import com.checkout.GsonSerializer; +import com.checkout.common.AmountAllocations; +import com.checkout.common.MarketplaceData; +import com.checkout.common.MarketplaceDataSubEntity; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class GetPaymentResponseSerializationTest { + + private final GsonSerializer serializer = new GsonSerializer(); + + // GetPaymentResponse (GET /payments/{id}): deprecated marketplace next to amount_allocations + + @Test + @SuppressWarnings("deprecation") + void shouldReadSameAllocationFromMarketplaceAndAmountAllocations() { + // The API still returns the former marketplace object next to amount_allocations. + final String json = "{\"id\":\"pay_mbabizu24mvu3mela5njyhpit4\",\"amount\":10,\"currency\":\"EUR\"," + + "\"marketplace\":{\"sub_entity_id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"," + + "\"sub_entities\":[{\"amount\":10,\"id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"}]}," + + "\"amount_allocations\":[{\"amount\":10,\"id\":\"ent_ocw5i74vowfg2edpy66izhts2u\"}]}"; + + final GetPaymentResponse response = serializer.fromJson(json, GetPaymentResponse.class); + + final MarketplaceData marketplace = response.getMarketplace(); + assertEquals("ent_ocw5i74vowfg2edpy66izhts2u", marketplace.getSubEntityId()); + assertEquals(1, marketplace.getSubEntities().size()); + assertEquals(1, response.getAmountAllocations().size()); + final MarketplaceDataSubEntity subEntity = marketplace.getSubEntities().get(0); + final AmountAllocations allocation = response.getAmountAllocations().get(0); + assertEquals("ent_ocw5i74vowfg2edpy66izhts2u", allocation.getId()); + assertEquals(10L, allocation.getAmount()); + assertEquals(allocation.getId(), subEntity.getId()); + assertEquals(allocation.getAmount(), subEntity.getAmount()); + assertNull(allocation.getReference()); + assertNull(subEntity.getReference()); + } + +}