From 736d277c64f8336bb42c57c5fbc006d09552b5e8 Mon Sep 17 00:00:00 2001 From: Lukas Rosenfeldt Date: Thu, 1 Aug 2024 11:41:23 +0200 Subject: [PATCH 1/4] chore: add demo for single and multistep checkout flow --- .../com/payone/commerce/platform/app/App.java | 265 ++++++++++++++++-- 1 file changed, 246 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/payone/commerce/platform/app/App.java b/app/src/main/java/com/payone/commerce/platform/app/App.java index 2b68941..1fbc8ca 100644 --- a/app/src/main/java/com/payone/commerce/platform/app/App.java +++ b/app/src/main/java/com/payone/commerce/platform/app/App.java @@ -3,35 +3,262 @@ */ package com.payone.commerce.platform.app; +import java.io.IOException; import java.security.InvalidKeyException; +import java.util.List; -import com.payone.commerce.platform.app.examples.CheckoutApiExample; -import com.payone.commerce.platform.app.examples.CommerceCaseApiExample; import com.payone.commerce.platform.lib.CommunicatorConfiguration; +import com.payone.commerce.platform.lib.endpoints.CheckoutApiClient; +import com.payone.commerce.platform.lib.endpoints.CommerceCaseApiClient; +import com.payone.commerce.platform.lib.endpoints.OrderManagementCheckoutActionsApiClient; +import com.payone.commerce.platform.lib.errors.ApiErrorResponseException; +import com.payone.commerce.platform.lib.errors.ApiException; +import com.payone.commerce.platform.lib.models.APIError; +import com.payone.commerce.platform.lib.models.Address; +import com.payone.commerce.platform.lib.models.AddressPersonal; +import com.payone.commerce.platform.lib.models.AmountOfMoney; +import com.payone.commerce.platform.lib.models.BankAccountInformation; +import com.payone.commerce.platform.lib.models.CartItemInput; +import com.payone.commerce.platform.lib.models.CartItemInvoiceData; +import com.payone.commerce.platform.lib.models.CheckoutReferences; +import com.payone.commerce.platform.lib.models.CheckoutResponse; +import com.payone.commerce.platform.lib.models.ContactDetails; +import com.payone.commerce.platform.lib.models.CreateCheckoutRequest; +import com.payone.commerce.platform.lib.models.CreateCommerceCaseRequest; +import com.payone.commerce.platform.lib.models.CreateCommerceCaseResponse; +import com.payone.commerce.platform.lib.models.Customer; +import com.payone.commerce.platform.lib.models.DeliverRequest; +import com.payone.commerce.platform.lib.models.DeliverResponse; +import com.payone.commerce.platform.lib.models.DeliverType; +import com.payone.commerce.platform.lib.models.MandateRecurrenceType; +import com.payone.commerce.platform.lib.models.OrderLineDetailsInput; +import com.payone.commerce.platform.lib.models.OrderRequest; +import com.payone.commerce.platform.lib.models.OrderType; +import com.payone.commerce.platform.lib.models.PatchCheckoutRequest; +import com.payone.commerce.platform.lib.models.PaymentChannel; +import com.payone.commerce.platform.lib.models.PaymentMethodSpecificInput; +import com.payone.commerce.platform.lib.models.PersonalInformation; +import com.payone.commerce.platform.lib.models.PersonalName; +import com.payone.commerce.platform.lib.models.ProcessingMandateInformation; +import com.payone.commerce.platform.lib.models.ProductType; +import com.payone.commerce.platform.lib.models.References; +import com.payone.commerce.platform.lib.models.SepaDirectDebitPaymentMethodSpecificInput; +import com.payone.commerce.platform.lib.models.SepaDirectDebitPaymentProduct771SpecificInput; +import com.payone.commerce.platform.lib.models.Shipping; +import com.payone.commerce.platform.lib.models.ShoppingCartInput; public class App { - public static void main(String[] args) throws InvalidKeyException { + private final String MERCHANT_ID; + private final CommunicatorConfiguration config; + private final CommerceCaseApiClient commerceCaseClient; + private final CheckoutApiClient checkoutClient; + private final OrderManagementCheckoutActionsApiClient orderManagementCheckoutClient; - String API_KEY = System.getenv("API_KEY"); - String API_SECRET = System.getenv("API_SECRET"); + public App(String API_KEY, String API_SECRET, String MERCHANT_ID) { + this.MERCHANT_ID = MERCHANT_ID; + this.config = new CommunicatorConfiguration(API_KEY, API_SECRET, "preprod.commerce-api.payone.com"); + try { + this.commerceCaseClient = new CommerceCaseApiClient(config); + this.checkoutClient = new CheckoutApiClient(config); + this.orderManagementCheckoutClient = new OrderManagementCheckoutActionsApiClient(config); + } catch (InvalidKeyException e) { + throw new RuntimeException("Expected key to be valid", e); + } + } + + private static App initFromEnv() { + String apiKey = System.getenv("API_KEY"); + String apiSecret = System.getenv("API_SECRET"); + String merchantId = System.getenv("MERCHANT_ID"); + + if (apiKey == null) { + throw new RuntimeException("required environment variable API_KEY is not set"); + } + if (apiSecret == null) { + throw new RuntimeException("required environment variable API_SECRET is not set"); + } + if (merchantId == null) { + throw new RuntimeException("required environment variable MERCHANT_ID is not set"); + } + + return new App(apiKey, apiSecret, merchantId); + } + + private void runSingleStepCheckout(String commerceCaseMerchantReference) + throws IOException, ApiException { + CreateCommerceCaseRequest payload = new CreateCommerceCaseRequest(); + + Customer customer = new Customer(); + PersonalInformation personalInformation = new PersonalInformation() + .dateOfBirth("19840505") + .name(new PersonalName().firstName("Rich").surname("Harris")); + ContactDetails contactDetails = new ContactDetails().emailAddress("mail@mail.com"); + Address address = new Address() + .countryCode("DE") + .zip("40474") + .city("Düsseldorf") + .street("Cecilienallee") + .houseNumber("2"); + customer.setPersonalInformation(personalInformation); + customer.setContactDetails(contactDetails); + customer.billingAddress(address); + + CreateCheckoutRequest checkout = new CreateCheckoutRequest(); + + CheckoutReferences checkoutReferences = new CheckoutReferences() + .merchantReference("c-" + commerceCaseMerchantReference); + AmountOfMoney amountOfMoney = new AmountOfMoney() + .amount(5199L) + .currencyCode("EUR"); + Shipping shipping = new Shipping() + .address(new AddressPersonal() + .countryCode("DE") + .zip("40474") + .city("Düsseldorf") + .street("Cecilienallee").houseNumber("2")); + ShoppingCartInput shoppingCart = new ShoppingCartInput(); + CartItemInput cartItem = new CartItemInput() + .invoiceData(new CartItemInvoiceData() + .description("Hoodie - Scaleshape Logo - L")) + .orderLineDetails(new OrderLineDetailsInput() + .productPrice(5199L) + .quantity(1L) + .productType(ProductType.GOODS)); + shoppingCart.addItemsItem(cartItem); - CommunicatorConfiguration config = new CommunicatorConfiguration(API_KEY, API_SECRET, - "preprod.commerce-api.payone.com"); + OrderRequest orderRequest = new OrderRequest() + .orderReferences(new References().merchantReference("o-" + commerceCaseMerchantReference)) + .paymentMethodSpecificInput(new PaymentMethodSpecificInput() + .sepaDirectDebitPaymentMethodSpecificInput(new SepaDirectDebitPaymentMethodSpecificInput() + .paymentProductId(771) + .paymentProduct771SpecificInput(new SepaDirectDebitPaymentProduct771SpecificInput() + .mandate(new ProcessingMandateInformation() + .bankAccountIban(new BankAccountInformation() + .iban("DE75512108001245126199") + .accountHolder("Rich Harris")) + .dateOfSignature("20240730") + .recurrenceType(MandateRecurrenceType.UNIQUE) + .uniqueMandateReference("m-" + commerceCaseMerchantReference) + .creditorId("DE98ZZZ09999999999"))))); + + checkout + .references(checkoutReferences) + .amountOfMoney(amountOfMoney) + .shipping(shipping) + .shoppingCart(shoppingCart) + .orderRequest(orderRequest); + + payload.setMerchantReference(commerceCaseMerchantReference); + payload.checkout(checkout); + payload.customer(customer); + + CreateCommerceCaseResponse commerceCase = commerceCaseClient.createCommerceCaseRequest(MERCHANT_ID, + payload); + System.out.println(commerceCase); + } + + private void runMultiStepCheckout(String commerceCaseMerchantReference) + throws IOException, ApiException { + // create the commercase + CreateCommerceCaseRequest createCommerceCaseRequest = new CreateCommerceCaseRequest() + .customer(new Customer() + .businessRelation("B2C") + .locale("de") + .personalInformation(new PersonalInformation().dateOfBirth("19840604") + .name(new PersonalName().firstName("Rich").surname("Harris"))) + .contactDetails(new ContactDetails().emailAddress("mail@mail.com")) + .billingAddress(new Address() + .countryCode("DE") + .zip("40474") + .city("Düsseldorf") + .street("Cecilienallee") + .houseNumber("2"))) + .checkout(new CreateCheckoutRequest() + .shoppingCart(new ShoppingCartInput() + .items(List.of(new CartItemInput() + .invoiceData(new CartItemInvoiceData() + .description("Frankenstein - Mary Shelley - Hardcover")) + .orderLineDetails(new OrderLineDetailsInput() + .productCode("shelley-42") + .productPrice(1999L) + .quantity(1L) + .productType(ProductType.GOODS) + .taxAmount(19L)))))); + CreateCommerceCaseResponse initialCommerceCase = this.commerceCaseClient.createCommerceCaseRequest(MERCHANT_ID, + createCommerceCaseRequest); + // add shipping information + PatchCheckoutRequest patchCheckoutRequest = new PatchCheckoutRequest() + .shipping(new Shipping() + .address(new AddressPersonal() + .countryCode("DE") + .zip("40474") + .city("Düsseldorf") + .street("Cecilienallee") + .houseNumber("2"))); + this.checkoutClient.updateCheckoutRequest( + MERCHANT_ID, + initialCommerceCase.getCommerceCaseId().toString(), + initialCommerceCase.getCheckout().getCheckoutId().toString(), + patchCheckoutRequest); + + // confirm the order + OrderRequest orderRequest = new OrderRequest() + .orderType(OrderType.FULL) + .orderReferences(new References().merchantReference("o-" + commerceCaseMerchantReference)) + .paymentMethodSpecificInput(new PaymentMethodSpecificInput() + .paymentChannel(PaymentChannel.ECOMMERCE) + .sepaDirectDebitPaymentMethodSpecificInput(new SepaDirectDebitPaymentMethodSpecificInput() + .paymentProductId(771) + .paymentProduct771SpecificInput(new SepaDirectDebitPaymentProduct771SpecificInput() + .mandate(new ProcessingMandateInformation() + .bankAccountIban(new BankAccountInformation() + .iban("DE75512108001245126199") + .accountHolder("Rich Harris")) + .dateOfSignature("20240730") + .recurrenceType(MandateRecurrenceType.UNIQUE) + .uniqueMandateReference("m-" + commerceCaseMerchantReference) + .creditorId("DE98ZZZ09999999999"))))); + this.orderManagementCheckoutClient.createOrder( + MERCHANT_ID, + initialCommerceCase.getCommerceCaseId().toString(), + initialCommerceCase.getCheckout().getCheckoutId().toString(), + orderRequest); + + CheckoutResponse finalCheckout = this.checkoutClient.getCheckoutRequest( + MERCHANT_ID, + initialCommerceCase.getCommerceCaseId().toString(), + initialCommerceCase.getCheckout().getCheckoutId().toString()); + System.out.println(finalCheckout); + + // items are ready for shipment, the delivery can be performed to capture the + // money from the reservation + DeliverResponse delivery = this.orderManagementCheckoutClient.deliverOrder( + MERCHANT_ID, + initialCommerceCase.getCommerceCaseId().toString(), + initialCommerceCase.getCheckout().getCheckoutId().toString(), + new DeliverRequest().deliverType(DeliverType.FULL).isFinal(true)); + System.out.println(delivery); + } + + public static void main(String[] args) { + App app = initFromEnv(); try { - CommerceCaseApiExample commerceCaseApiExample = new CommerceCaseApiExample(config); - commerceCaseApiExample.runPostOne(); - commerceCaseApiExample.runGetAll(); - commerceCaseApiExample.runGetOne(); - commerceCaseApiExample.runUpdateOne(); - - CheckoutApiExample checkoutApiExample = new CheckoutApiExample(config); - checkoutApiExample.runPostOne(); - checkoutApiExample.runGetAll(); - checkoutApiExample.runGetOne(); - checkoutApiExample.runUpdateOne(); - checkoutApiExample.runDeleteOne(); + app.runMultiStepCheckout("comc1a1"); + } catch (ApiErrorResponseException e) { + for (APIError error : e.getErrors()) { + System.err.println(error.getMessage()); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + try { + app.runSingleStepCheckout("comc1a1"); + } catch (ApiErrorResponseException e) { + for (APIError error : e.getErrors()) { + System.err.println(error.getMessage()); + } } catch (Exception e) { throw new RuntimeException(e); } From fb6d51a8521b63ced3d81abbc830f8a8e48558ef Mon Sep 17 00:00:00 2001 From: Lukas Rosenfeldt Date: Thu, 1 Aug 2024 11:41:42 +0200 Subject: [PATCH 2/4] chore: remove unused example code --- .../app/examples/CheckoutApiExample.java | 75 ------------------- .../app/examples/CommerceCaseApiExample.java | 72 ------------------ ...derManagementCheckoutActionApiExample.java | 70 ----------------- .../examples/PaymentExecutionApiExample.java | 73 ------------------ 4 files changed, 290 deletions(-) delete mode 100644 app/src/main/java/com/payone/commerce/platform/app/examples/CheckoutApiExample.java delete mode 100644 app/src/main/java/com/payone/commerce/platform/app/examples/CommerceCaseApiExample.java delete mode 100644 app/src/main/java/com/payone/commerce/platform/app/examples/OrderManagementCheckoutActionApiExample.java delete mode 100644 app/src/main/java/com/payone/commerce/platform/app/examples/PaymentExecutionApiExample.java diff --git a/app/src/main/java/com/payone/commerce/platform/app/examples/CheckoutApiExample.java b/app/src/main/java/com/payone/commerce/platform/app/examples/CheckoutApiExample.java deleted file mode 100644 index 8141e54..0000000 --- a/app/src/main/java/com/payone/commerce/platform/app/examples/CheckoutApiExample.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.payone.commerce.platform.app.examples; - -import java.io.IOException; -import java.security.InvalidKeyException; - -import com.payone.commerce.platform.lib.CommunicatorConfiguration; -import com.payone.commerce.platform.lib.endpoints.CheckoutApiClient; -import com.payone.commerce.platform.lib.errors.ApiException; -import com.payone.commerce.platform.lib.models.AmountOfMoney; -import com.payone.commerce.platform.lib.models.CheckoutResponse; -import com.payone.commerce.platform.lib.models.CheckoutsResponse; -import com.payone.commerce.platform.lib.models.CreateCheckoutRequest; -import com.payone.commerce.platform.lib.models.CreateCheckoutResponse; -import com.payone.commerce.platform.lib.models.PatchCheckoutRequest; -import com.payone.commerce.platform.lib.queries.GetCheckoutsQuery; - -public class CheckoutApiExample { - CheckoutApiClient client; - private final String merchantId = System.getenv("MERCHANT_ID"); - private final String commerceCaseId = System.getenv("COMMERCE_CASE_ID"); - private final String checkoutId = System.getenv("CHECKOUT_ID"); - private String createdCheckoutId; - - public CheckoutApiExample(CommunicatorConfiguration config) throws InvalidKeyException { - this.client = new CheckoutApiClient(config); - } - - public void runPostOne() throws ApiException, IOException { - CreateCheckoutRequest payload = new CreateCheckoutRequest(); - AmountOfMoney amount = new AmountOfMoney(); - amount.setAmount(1000l); - amount.setCurrencyCode("EUR"); - payload.amountOfMoney(amount); - CreateCheckoutResponse res2 = client.createCheckoutRequest(merchantId, - commerceCaseId, payload); - - createdCheckoutId = res2.getCheckoutId().toString(); - - System.out.println(res2); - - } - - public void runGetAll() throws ApiException, IOException { - GetCheckoutsQuery query = new GetCheckoutsQuery(); - CheckoutsResponse res = client.getCheckoutsRequest(merchantId, - query); - - System.out.println(res.toString()); - - } - - public void runGetOne() throws ApiException, IOException { - CheckoutResponse res = client.getCheckoutRequest(merchantId, commerceCaseId, - checkoutId); - - System.out.println(res.toString()); - } - - public void runUpdateOne() throws ApiException, IOException { - PatchCheckoutRequest patch = new PatchCheckoutRequest(); - AmountOfMoney amount = new AmountOfMoney(); - - amount.setCurrencyCode("EUR"); - amount.setAmount(1500l); - patch.amountOfMoney(amount); - client.updateCheckoutRequest(merchantId, - commerceCaseId, - createdCheckoutId, patch); - - } - - public void runDeleteOne() throws ApiException, IOException { - client.removeCheckoutRequest(merchantId, commerceCaseId, createdCheckoutId); - } -} diff --git a/app/src/main/java/com/payone/commerce/platform/app/examples/CommerceCaseApiExample.java b/app/src/main/java/com/payone/commerce/platform/app/examples/CommerceCaseApiExample.java deleted file mode 100644 index f1e04a9..0000000 --- a/app/src/main/java/com/payone/commerce/platform/app/examples/CommerceCaseApiExample.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.payone.commerce.platform.app.examples; - -import java.io.IOException; -import java.security.InvalidKeyException; -import java.util.List; - -import com.payone.commerce.platform.lib.CommunicatorConfiguration; -import com.payone.commerce.platform.lib.endpoints.CommerceCaseApiClient; -import com.payone.commerce.platform.lib.errors.ApiException; -import com.payone.commerce.platform.lib.models.Address; -import com.payone.commerce.platform.lib.models.CommerceCaseResponse; -import com.payone.commerce.platform.lib.models.CreateCommerceCaseRequest; -import com.payone.commerce.platform.lib.models.CreateCommerceCaseResponse; -import com.payone.commerce.platform.lib.models.Customer; -import com.payone.commerce.platform.lib.queries.GetCommerceCasesQuery; - -public class CommerceCaseApiExample { - CommerceCaseApiClient client; - private final String merchantId = System.getenv("MERCHANT_ID"); - private final String commerceCaseId = System.getenv("COMMERCE_CASE_ID"); - - public CommerceCaseApiExample(CommunicatorConfiguration config) throws InvalidKeyException { - this.client = new CommerceCaseApiClient(config); - } - - public void runPostOne() throws ApiException, IOException { - - CreateCommerceCaseResponse res = client.createCommerceCaseRequest(merchantId, - new CreateCommerceCaseRequest()); - - System.out.println(res.toString()); - - } - - public void runGetAll() throws ApiException, IOException { - GetCommerceCasesQuery query = new GetCommerceCasesQuery(); - query.setOffset(2); - query.setSize(1); - // Set status = Set.of(StatusCheckout.COMPLETED); - // query.setIncludeCheckoutStatus(status); - - List res = client.getCommerceCasesRequest(merchantId, query); - - System.out.println(res.toString()); - - } - - public void runGetOne() throws ApiException, IOException { - CommerceCaseResponse res = client.getCommerceCaseRequest(merchantId, - commerceCaseId); - - System.out.println(res.toString()); - } - - public void runUpdateOne() throws ApiException, IOException { - CommerceCaseResponse responseGetOne = client.getCommerceCaseRequest(merchantId, - commerceCaseId); - - Customer modifiedCustomer = responseGetOne.getCustomer(); - Address modifiedBillingAddress = modifiedCustomer.getBillingAddress(); - modifiedBillingAddress.setCity("Kerken"); - modifiedCustomer.setBillingAddress(modifiedBillingAddress); - - client.updateCommerceCaseRequest(merchantId, - commerceCaseId, modifiedCustomer); - - CommerceCaseResponse res = client.getCommerceCaseRequest(merchantId, - commerceCaseId); - - System.out.println(res.toString()); - } -} diff --git a/app/src/main/java/com/payone/commerce/platform/app/examples/OrderManagementCheckoutActionApiExample.java b/app/src/main/java/com/payone/commerce/platform/app/examples/OrderManagementCheckoutActionApiExample.java deleted file mode 100644 index e883e4d..0000000 --- a/app/src/main/java/com/payone/commerce/platform/app/examples/OrderManagementCheckoutActionApiExample.java +++ /dev/null @@ -1,70 +0,0 @@ -// package com.payone.commerce.platform.app.examples; - -// import java.io.IOException; -// import java.security.InvalidKeyException; - -// import com.payone.commerce.platform.lib.CommunicatorConfiguration; -// import com.payone.commerce.platform.lib.endpoints.CheckoutApiClient; -// import -// com.payone.commerce.platform.lib.endpoints.OrderManagementCheckoutActionsApiClient; -// import com.payone.commerce.platform.lib.errors.ApiException; -// import com.payone.commerce.platform.lib.models.AmountOfMoney; -// import com.payone.commerce.platform.lib.models.CreateCheckoutRequest; -// import com.payone.commerce.platform.lib.models.CreateCheckoutResponse; -// import com.payone.commerce.platform.lib.models.OrderRequest; -// import com.payone.commerce.platform.lib.models.OrderResponse; -// import com.payone.commerce.platform.lib.models.OrderType; -// import com.payone.commerce.platform.lib.models.References; - -// public class OrderManagementCheckoutActionApiExample { -// OrderManagementCheckoutActionsApiClient client; -// CheckoutApiClient checkoutClient; - -// private final String merchantId = System.getenv("MERCHANT_ID"); -// private final String commerceCaseId = System.getenv("COMMERCE_CASE_ID"); -// private final String checkoutId = System.getenv("CHECKOUT_ID"); - -// public OrderManagementCheckoutActionApiExample(CommunicatorConfiguration -// config) throws InvalidKeyException { -// this.client = new OrderManagementCheckoutActionsApiClient(config); -// this.checkoutClient = new CheckoutApiClient(config); -// } - -// public void runCreateOrderAndExecutePayment() throws ApiException, -// IOException { - -// // create checkout -// CreateCheckoutRequest payload = new CreateCheckoutRequest(); -// AmountOfMoney amount = new AmountOfMoney(); -// amount.setAmount(1000l); -// amount.setCurrencyCode("EUR"); -// payload.setAmountOfMoney(amount); - -// CreateCheckoutResponse res2 = -// checkoutClient.createCheckoutRequest(merchantId, -// commerceCaseId, payload); -// String createdCheckoutId = res2.getCheckoutId().toString(); - -// System.out.println(res2.toString()); - -// // execute payment -// OrderRequest payload2 = new OrderRequest(); -// payload2.setOrderType(OrderType.FULL); -// References references = new References(); -// references.setMerchantReference("1234"); - -// payload2.setOrderReferences(references); - -// OrderResponse res = client.createOrder(merchantId, commerceCaseId, -// createdCheckoutId, -// payload2); - -// System.out.println(res.toString()); - -// // remove checkout -// checkoutClient.removeCheckoutRequest(merchantId, commerceCaseId, -// createdCheckoutId); - -// } - -// } diff --git a/app/src/main/java/com/payone/commerce/platform/app/examples/PaymentExecutionApiExample.java b/app/src/main/java/com/payone/commerce/platform/app/examples/PaymentExecutionApiExample.java deleted file mode 100644 index 6d28274..0000000 --- a/app/src/main/java/com/payone/commerce/platform/app/examples/PaymentExecutionApiExample.java +++ /dev/null @@ -1,73 +0,0 @@ -// package com.payone.commerce.platform.app.examples; - -// import java.io.IOException; -// import java.security.InvalidKeyException; - -// import com.payone.commerce.platform.lib.CommunicatorConfiguration; -// import com.payone.commerce.platform.lib.endpoints.CheckoutApiClient; -// import com.payone.commerce.platform.lib.endpoints.PaymentExecutionApiClient; -// import com.payone.commerce.platform.lib.errors.ApiException; -// import com.payone.commerce.platform.lib.models.AmountOfMoney; -// import com.payone.commerce.platform.lib.models.CreateCheckoutRequest; -// import com.payone.commerce.platform.lib.models.CreateCheckoutResponse; -// import com.payone.commerce.platform.lib.models.CreatePaymentResponse; -// import com.payone.commerce.platform.lib.models.PaymentExecutionRequest; -// import com.payone.commerce.platform.lib.models.PaymentExecutionSpecificInput; -// import com.payone.commerce.platform.lib.models.References; - -// public class PaymentExecutionApiExample { -// PaymentExecutionApiClient client; -// CheckoutApiClient checkoutClient; - -// private final String merchantId = System.getenv("MERCHANT_ID"); -// private final String commerceCaseId = System.getenv("COMMERCE_CASE_ID"); -// private final String checkoutId = System.getenv("CHECKOUT_ID"); - -// public PaymentExecutionApiExample(CommunicatorConfiguration config) throws -// InvalidKeyException { -// this.client = new PaymentExecutionApiClient(config); -// this.checkoutClient = new CheckoutApiClient(config); -// } - -// public void runCreatePayment() throws ApiException, IOException { - -// // create checkout -// CreateCheckoutRequest payload = new CreateCheckoutRequest(); -// AmountOfMoney amount = new AmountOfMoney(); -// amount.setAmount(1000l); -// amount.setCurrencyCode("EUR"); -// payload.setAmountOfMoney(amount); - -// CreateCheckoutResponse res2 = -// checkoutClient.createCheckoutRequest(merchantId, -// commerceCaseId, payload); -// String createdCheckoutId = res2.getCheckoutId().toString(); - -// System.out.println(res2.toString()); - -// try { -// // execute payment -// PaymentExecutionSpecificInput paymentExecutionSpecificInput = new -// PaymentExecutionSpecificInput(); -// paymentExecutionSpecificInput.setAmountOfMoney(amount); -// References references = new References(); -// references.setMerchantReference("12345678764"); -// paymentExecutionSpecificInput.setPaymentReferences(references); -// PaymentExecutionRequest payload2 = new PaymentExecutionRequest(); -// payload2.setPaymentExecutionSpecificInput(paymentExecutionSpecificInput); - -// CreatePaymentResponse res = client.createPayment(merchantId, commerceCaseId, -// createdCheckoutId, -// payload2); - -// System.out.println(res.toString()); -// } catch (Exception e) { -// System.out.println(e.getMessage()); -// // remove checkout -// checkoutClient.removeCheckoutRequest(merchantId, commerceCaseId, -// createdCheckoutId); -// } - -// } - -// } From e704f7d70556bdce487a04d571ce506f435eabb2 Mon Sep 17 00:00:00 2001 From: Lukas Rosenfeldt Date: Thu, 1 Aug 2024 12:23:05 +0200 Subject: [PATCH 3/4] chore: add demo for payment execution --- .../com/payone/commerce/platform/app/App.java | 105 +++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/payone/commerce/platform/app/App.java b/app/src/main/java/com/payone/commerce/platform/app/App.java index 1fbc8ca..297f1e0 100644 --- a/app/src/main/java/com/payone/commerce/platform/app/App.java +++ b/app/src/main/java/com/payone/commerce/platform/app/App.java @@ -5,12 +5,13 @@ import java.io.IOException; import java.security.InvalidKeyException; -import java.util.List; +import java.util.Arrays; import com.payone.commerce.platform.lib.CommunicatorConfiguration; import com.payone.commerce.platform.lib.endpoints.CheckoutApiClient; import com.payone.commerce.platform.lib.endpoints.CommerceCaseApiClient; import com.payone.commerce.platform.lib.endpoints.OrderManagementCheckoutActionsApiClient; +import com.payone.commerce.platform.lib.endpoints.PaymentExecutionApiClient; import com.payone.commerce.platform.lib.errors.ApiErrorResponseException; import com.payone.commerce.platform.lib.errors.ApiException; import com.payone.commerce.platform.lib.models.APIError; @@ -26,6 +27,7 @@ import com.payone.commerce.platform.lib.models.CreateCheckoutRequest; import com.payone.commerce.platform.lib.models.CreateCommerceCaseRequest; import com.payone.commerce.platform.lib.models.CreateCommerceCaseResponse; +import com.payone.commerce.platform.lib.models.CreatePaymentResponse; import com.payone.commerce.platform.lib.models.Customer; import com.payone.commerce.platform.lib.models.DeliverRequest; import com.payone.commerce.platform.lib.models.DeliverResponse; @@ -36,6 +38,8 @@ import com.payone.commerce.platform.lib.models.OrderType; import com.payone.commerce.platform.lib.models.PatchCheckoutRequest; import com.payone.commerce.platform.lib.models.PaymentChannel; +import com.payone.commerce.platform.lib.models.PaymentExecutionRequest; +import com.payone.commerce.platform.lib.models.PaymentExecutionSpecificInput; import com.payone.commerce.platform.lib.models.PaymentMethodSpecificInput; import com.payone.commerce.platform.lib.models.PersonalInformation; import com.payone.commerce.platform.lib.models.PersonalName; @@ -53,6 +57,7 @@ public class App { private final CommerceCaseApiClient commerceCaseClient; private final CheckoutApiClient checkoutClient; private final OrderManagementCheckoutActionsApiClient orderManagementCheckoutClient; + private final PaymentExecutionApiClient paymentExecutionClient; public App(String API_KEY, String API_SECRET, String MERCHANT_ID) { this.MERCHANT_ID = MERCHANT_ID; @@ -61,6 +66,7 @@ public App(String API_KEY, String API_SECRET, String MERCHANT_ID) { this.commerceCaseClient = new CommerceCaseApiClient(config); this.checkoutClient = new CheckoutApiClient(config); this.orderManagementCheckoutClient = new OrderManagementCheckoutActionsApiClient(config); + this.paymentExecutionClient = new PaymentExecutionApiClient(config); } catch (InvalidKeyException e) { throw new RuntimeException("Expected key to be valid", e); } @@ -84,6 +90,90 @@ private static App initFromEnv() { return new App(apiKey, apiSecret, merchantId); } + private void runCheckoutWithPaymentExecution(String commerceCaseMerchantReference) + throws IOException, ApiException { + // Create a commerce case, add customer data, put something into the shopping + // cart + CreateCommerceCaseRequest createCommerceCaseRequest = new CreateCommerceCaseRequest(); + + Customer customer = new Customer(); + PersonalInformation personalInformation = new PersonalInformation() + .dateOfBirth("19991112") + .name(new PersonalName().firstName("Ryan").surname("Carniato")); + ContactDetails contactDetails = new ContactDetails().emailAddress("mail@mail.com"); + Address address = new Address() + .countryCode("DE") + .zip("24937") + .city("Flensburg") + .street("Rathausplatz") + .houseNumber("1"); + customer.setPersonalInformation(personalInformation); + customer.setContactDetails(contactDetails); + customer.billingAddress(address); + + CreateCheckoutRequest checkoutRequest = new CreateCheckoutRequest(); + AmountOfMoney amountOfMoney = new AmountOfMoney() + .amount(3599L) + .currencyCode("EUR"); + Shipping shipping = new Shipping() + .address(new AddressPersonal() + .countryCode("DE") + .zip("24937") + .city("Flensburg") + .street("Rathausplatz").houseNumber("1")); + ShoppingCartInput shoppingCart = new ShoppingCartInput(); + CartItemInput cartItem = new CartItemInput() + .invoiceData(new CartItemInvoiceData() + .description("T-Shirt - Scaleshape Logo - S")) + .orderLineDetails(new OrderLineDetailsInput() + .productPrice(3599L) + .quantity(1L) + .productType(ProductType.GOODS)); + shoppingCart.addItemsItem(cartItem); + + checkoutRequest.amountOfMoney(amountOfMoney) + .shoppingCart(shoppingCart) + .shipping(shipping); + + createCommerceCaseRequest + .merchantReference(commerceCaseMerchantReference) + .customer(customer) + .checkout(checkoutRequest); + + CreateCommerceCaseResponse commerceCase = commerceCaseClient.createCommerceCaseRequest( + MERCHANT_ID, + createCommerceCaseRequest); + + PaymentExecutionRequest paymentExecutionRequest = new PaymentExecutionRequest() + .paymentExecutionSpecificInput(new PaymentExecutionSpecificInput() + .paymentReferences(new References().merchantReference("p-" + commerceCaseMerchantReference)) + .amountOfMoney(new AmountOfMoney() + .amount(3599L) + .currencyCode("EUR"))) + .paymentMethodSpecificInput(new PaymentMethodSpecificInput() + .sepaDirectDebitPaymentMethodSpecificInput(new SepaDirectDebitPaymentMethodSpecificInput() + .paymentProductId(771) + .paymentProduct771SpecificInput(new SepaDirectDebitPaymentProduct771SpecificInput() + .mandate(new ProcessingMandateInformation() + .bankAccountIban(new BankAccountInformation() + .iban("DE75512108001245126199") + .accountHolder("Ryan Carniato")) + .dateOfSignature("20240730") + .recurrenceType(MandateRecurrenceType.UNIQUE) + .uniqueMandateReference("m-" + commerceCaseMerchantReference) + .creditorId("DE98ZZZ09999999999"))))); + CreatePaymentResponse paymentResponse = this.paymentExecutionClient.createPayment(MERCHANT_ID, + commerceCase.getCommerceCaseId().toString(), + commerceCase.getCheckout().getCheckoutId().toString(), paymentExecutionRequest); + System.out.println(paymentResponse); + + CheckoutResponse finalCheckout = this.checkoutClient.getCheckoutRequest( + MERCHANT_ID, + commerceCase.getCommerceCaseId().toString(), + commerceCase.getCheckout().getCheckoutId().toString()); + System.out.println(finalCheckout); + } + private void runSingleStepCheckout(String commerceCaseMerchantReference) throws IOException, ApiException { CreateCommerceCaseRequest payload = new CreateCommerceCaseRequest(); @@ -175,7 +265,7 @@ private void runMultiStepCheckout(String commerceCaseMerchantReference) .houseNumber("2"))) .checkout(new CreateCheckoutRequest() .shoppingCart(new ShoppingCartInput() - .items(List.of(new CartItemInput() + .items(Arrays.asList(new CartItemInput() .invoiceData(new CartItemInvoiceData() .description("Frankenstein - Mary Shelley - Hardcover")) .orderLineDetails(new OrderLineDetailsInput() @@ -243,6 +333,17 @@ private void runMultiStepCheckout(String commerceCaseMerchantReference) public static void main(String[] args) { App app = initFromEnv(); + try { + app.runCheckoutWithPaymentExecution("comc1a5"); + } catch (ApiErrorResponseException e) { + for (APIError error : e.getErrors()) { + System.err.println(error.getMessage()); + } + System.exit(1); + } catch (Exception e) { + throw new RuntimeException(e); + } + try { app.runMultiStepCheckout("comc1a1"); } catch (ApiErrorResponseException e) { From c50916520ba602a232d976ba6b405266618c6ba8 Mon Sep 17 00:00:00 2001 From: Lukas Rosenfeldt Date: Thu, 1 Aug 2024 12:41:43 +0200 Subject: [PATCH 4/4] chore: explain different demos and link documentation --- app/src/main/java/com/payone/commerce/platform/app/App.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/com/payone/commerce/platform/app/App.java b/app/src/main/java/com/payone/commerce/platform/app/App.java index 297f1e0..e9990f4 100644 --- a/app/src/main/java/com/payone/commerce/platform/app/App.java +++ b/app/src/main/java/com/payone/commerce/platform/app/App.java @@ -333,6 +333,7 @@ private void runMultiStepCheckout(String commerceCaseMerchantReference) public static void main(String[] args) { App app = initFromEnv(); + // creates a checkout and executes the payment in one go try { app.runCheckoutWithPaymentExecution("comc1a5"); } catch (ApiErrorResponseException e) { @@ -344,6 +345,8 @@ public static void main(String[] args) { throw new RuntimeException(e); } + // see: https://docs.payone.com/pcp/checkout-flows/step-by-step-checkout + // not that the given reference must be unique and has to renewed after each run try { app.runMultiStepCheckout("comc1a1"); } catch (ApiErrorResponseException e) { @@ -354,6 +357,8 @@ public static void main(String[] args) { throw new RuntimeException(e); } + // see: https://docs.payone.com/pcp/checkout-flows/one-step-checkout + // not that the given reference must be unique and has to renewed after each run try { app.runSingleStepCheckout("comc1a1"); } catch (ApiErrorResponseException e) {