Skip to content

[CC-1389] add paypage v2 #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[CC-1389] Add Paypage v2 support:
- Add fetch method for paypage v2.
  • Loading branch information
Ryouzanpaku committed Sep 4, 2024
commit 7ba643a49ec8c23da4c1c3446f10f67c38dd1f2d
11 changes: 11 additions & 0 deletions src/main/java/com/unzer/payment/Unzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,17 @@ public PaypageV2 createPaypage(PaypageV2 paypage) {
return paypageV2Service.create(paypage);
}

/**
* Fetch a Paypage v2. Respons contains list of payments created wit the
* given paypage.
*
* @param paypage paypage id
* @return
*/
public PaypageV2 fetchPaypagePayments(String paypage) {
return paypageV2Service.fetch(paypage);
}

/**
* Initiates a paypage and returns the redirectUrl and an id to the paypage. The id will be
* used for embedded paypage within Javascript components, the redirectUrl will be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ public static String addAuthentication(String privateKey) {

@Override
public String httpGet(String url, String privateKey) throws HttpCommunicationException {
return httpGet(url, privateKey, ApiConfigs.PAYMENT_API);
}

@Override
public String httpGet(String url, String privateKey, ApiConfig apiClientConfig) throws HttpCommunicationException {
Objects.requireNonNull(url);
return this.execute(createRequest(url, UnzerHttpMethod.GET), privateKey, EMPTY_JSON);
return this.execute(createRequest(url, UnzerHttpMethod.GET), privateKey, EMPTY_JSON, apiClientConfig);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public interface UnzerRestCommunication {
*/
String httpGet(String url, String privateKey) throws HttpCommunicationException;

/**
* Executes a GET Request to the given {@code url} authenticated with the given
* {@code privateKey}.
*
* @param url - the url to be called
* @param privateKey - the private key of the key-pair to used
* @return - the Response as application/json, UTF-8
* @throws HttpCommunicationException - thrown for any problems occurring in http-communication
*/
String httpGet(String url, String privateKey, ApiConfig apiClientConfig) throws HttpCommunicationException;

/**
* Executes a POST Request to the given {@code url} authenticated with the given
* {@code privateKey}.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/unzer/payment/models/paypage/PaypagePayment.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package com.unzer.payment.models.paypage;

import com.unzer.payment.communication.json.JsonMessage;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

@Data
@Setter(AccessLevel.PRIVATE)
public class PaypagePayment {
private String paymentId;
private String transactionStatus;
private String creationDate;
private JsonMessage[] messages;
}
16 changes: 9 additions & 7 deletions src/main/java/com/unzer/payment/resources/PaypageV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import com.unzer.payment.models.paypage.*;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.HashMap;

@Data
@NoArgsConstructor
public class PaypageV2 extends BaseResource {

private static String URI = "/v2/merchant/paypage";
Expand Down Expand Up @@ -70,16 +72,17 @@ public String getId() {
}

public enum MethodName {
DEFAULT("default"),
CARDS("cards"),
PAYPAL("paypal"),
PAYLATERINSTALLMENT("paylaterInstallment"),
PAYLATER_INSTALLMENT("paylaterInstallment"),
GOOGLEPAY("googlepay"),
APPLEPAY("applepay"),
KLARNA("klarna"),
SEPADIRECTDEBIT("sepaDirectDebit"),
SEPA_DIRECT_DEBIT("sepaDirectDebit"),
EPS("eps"),
PAYLATERINVOICE("paylaterInvoice"),
PAYLATERDIRECTDEBIT("paylaterDirectDebit"),
PAYLATER_INVOICE("paylaterInvoice"),
PAYLATER_DIRECT_DEBIT("paylaterDirectDebit"),
PREPAYMENT("prepayment"),
PAYU("payu"),
IDEAL("ideal"),
Expand All @@ -89,16 +92,15 @@ public enum MethodName {
BANCONTACT("bancontact"),
PFCARD("pfcard"),
PFEFINANCE("pfefinance"),
TWINT("twint"),
DEFAULT("default");
TWINT("twint");

private final String name;

MethodName(String name) {
this.name = name;
}

public String get() {
public String value() {
return name;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/unzer/payment/service/v2/PaypageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public PaypageV2 create(PaypageV2 paypage, String url) throws HttpCommunicationE

return new JsonParser().fromJson(response, PaypageV2.class);
}

public PaypageV2 fetch(String paypageId) {
unzer.prepareJwtToken();
String response = restCommunication.httpGet(
urlUtil.getUrl(new PaypageV2()) + "/" + paypageId,
unzer.getJwtToken(),
ApiConfigs.PAYPAGE_API
);
return new JsonParser().fromJson(response, PaypageV2.class);
}
}
70 changes: 70 additions & 0 deletions src/test/java/com/unzer/payment/business/PaypageV2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.unzer.payment.business;


import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.unzer.payment.communication.JsonParser;
import com.unzer.payment.communication.json.JsonMessage;
import com.unzer.payment.models.paypage.PaypagePayment;
import com.unzer.payment.resources.PaypageV2;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

@WireMockTest(httpPort = 8080)
class PaypageV2Test {
private static String getResponse(String response) {
return new Scanner(Objects.requireNonNull(
PaypageV2Test.class.getResourceAsStream("/api-response/paypage-v2/" + response))).useDelimiter("\\A")
.next();
}

@ParameterizedTest(name = "Paypage creation with {0}")
@MethodSource("paypageFetchResponseProvider")
void paypagePaymentGetMappedCorrectlyFromJson(Integer paymentCount, String JsonResponse) {
PaypageV2 fetchedPaypage = new JsonParser().fromJson(JsonResponse, PaypageV2.class);
assertEquals(paymentCount, fetchedPaypage.getTotal());
assertEquals(paymentCount, fetchedPaypage.getPayments().length);

if (paymentCount == 0) {
return;
}

PaypagePayment paypagePayment = fetchedPaypage.getPayments()[0];
assertEquals("s-pay-0", paypagePayment.getPaymentId());

JsonMessage[] messages = paypagePayment.getMessages();
assertEquals(1, messages.length);

assertEquals("COR.000.000.000", messages[0].getCode());
assertEquals("Your payments have been successfully processed.", messages[0].getCustomer());
assertEquals("Transaction succeeded", messages[0].getMerchant());

if (paymentCount == 1) {
return;
}

PaypagePayment paypagePaymentTwo = fetchedPaypage.getPayments()[1];
assertEquals("s-pay-1", paypagePaymentTwo.getPaymentId());

JsonMessage[] messagesTwo = paypagePaymentTwo.getMessages();
assertEquals(1, messagesTwo.length);

assertEquals("COR.000.000.000", messagesTwo[0].getCode());
assertEquals("Your payments have been successfully processed.", messagesTwo[0].getCustomer());
assertEquals("Transaction succeeded", messagesTwo[0].getMerchant());
}

public static Stream<Arguments> paypageFetchResponseProvider() {
return Stream.of(
Arguments.of(0, getResponse("fetch-response-without-payments.json")),
Arguments.of(1, getResponse("fetch-response-one-payment.json")),
Arguments.of(2, getResponse("fetch-response-with-multiple-payments.json"))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public String httpGet(String url, String privateKey) {
return execute(new HttpGet(proxyUrl(url)), null);
}

@Override
public String httpGet(String url, String privateKey, ApiConfig apiClientConfig) throws HttpCommunicationException {
return execute(new HttpGet(proxyUrl(url)), null);
}

@Override
public String httpPost(String url, String privateKey, Object data) {
return execute(new HttpPost(proxyUrl(url)), data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ void minimumPaypageCreation() {
testPaypageCreation(paypage);
}

@Test
void fetchPaypageById() {
PaypageV2 paypage = new PaypageV2(new BigDecimal("9.99"), "EUR", "charge");
assertNull(paypage.getPayments());
assertNull(paypage.getTotal());

// when
PaypageV2 paypageResponse = testPaypageCreation(paypage);
PaypageV2 fetchedPaypage = unzer.fetchPaypagePayments(paypageResponse.getId());

// Paypage initially has no payments
assertEquals(fetchedPaypage.getTotal(), 0);
assertNotNull(fetchedPaypage.getPayments());
assertArrayEquals(new PaypagePayment[0], fetchedPaypage.getPayments());
}

@Test
void paypageCreationWitOptionalFields() {
PaypageV2 paypage = new PaypageV2(new BigDecimal("9.99"), "EUR", "charge");
Expand Down Expand Up @@ -131,12 +147,13 @@ void createPaypageWithRiskData() {
testPaypageCreation(paypage);
}

private void testPaypageCreation(PaypageV2 paypage) {
private PaypageV2 testPaypageCreation(PaypageV2 paypage) {
// when
PaypageV2 response = unzer.createPaypage(paypage);

// then
assertCreatedPaypage(response);
return response;
}

@ParameterizedTest(name = "Paypage can be created with \"{0}\"")
Expand Down Expand Up @@ -186,27 +203,27 @@ public static Stream<Arguments> getPaymentMethodsConfigs() {
withPaylaterConfig.put("cards", paylaterConfig);

HashMap<String, PaymentMethodConfig> withEnumMethodNames = new HashMap<>();
withEnumMethodNames.put(PaypageV2.MethodName.CARDS.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYPAL.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATERINSTALLMENT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.GOOGLEPAY.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.APPLEPAY.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.KLARNA.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.SEPADIRECTDEBIT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.EPS.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATERINVOICE.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATERDIRECTDEBIT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PREPAYMENT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYU.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.IDEAL.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PRZELEWY24.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.ALIPAY.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.WECHATPAY.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.BANCONTACT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PFCARD.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PFEFINANCE.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.TWINT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.DEFAULT.get(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.CARDS.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYPAL.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATER_INSTALLMENT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.GOOGLEPAY.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.APPLEPAY.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.KLARNA.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.SEPA_DIRECT_DEBIT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.EPS.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATER_INVOICE.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYLATER_DIRECT_DEBIT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PREPAYMENT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PAYU.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.IDEAL.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PRZELEWY24.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.ALIPAY.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.WECHATPAY.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.BANCONTACT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PFCARD.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.PFEFINANCE.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.TWINT.value(), enabledConfig);
withEnumMethodNames.put(PaypageV2.MethodName.DEFAULT.value(), enabledConfig);

return Stream.of(
Arguments.of("Empty", emptyConfig),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"payments": [
{
"paymentId": "s-pay-0",
"transactionStatus": "success",
"creationDate": "2024-09-04T15:08:23.223Z",
"messages": [
{
"code": "COR.000.000.000",
"customer": "Your payments have been successfully processed.",
"merchant": "Transaction succeeded"
}
]
}
],
"total": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"payments": [
{
"paymentId": "s-pay-0",
"transactionStatus": "failure",
"creationDate": "2024-09-04T15:08:23.223Z",
"messages": [
{
"code": "COR.000.000.000",
"customer": "Your payments have been successfully processed.",
"merchant": "Transaction succeeded"
}
]
},
{
"paymentId": "s-pay-1",
"transactionStatus": "success",
"creationDate": "2024-09-04T15:08:23.223Z",
"messages": [
{
"code": "COR.000.000.000",
"customer": "Your payments have been successfully processed.",
"merchant": "Transaction succeeded"
}
]
}
],
"total": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"payments": [],
"total": 0
}
Loading