Skip to content

Commit

Permalink
Klarna PayIn integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviana Ghita committed Sep 13, 2023
1 parent 7b7918f commit d6cde0a
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/mangopay/core/APIs/ApiBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected MangoPayApi getRoot() {
put("payins_applepay-direct_create", new String[]{"/payins/applepay/direct", RequestType.POST.toString()});
put("payins_googlepay-direct_create", new String[]{"/payins/googlepay/direct", RequestType.POST.toString()});
put("payins_mbway-direct_create", new String[]{"/payins/payment-methods/mbway", RequestType.POST.toString()});
put("payins_klarna-web_create", new String[]{"/payins/payment-methods/klarna", RequestType.POST.toString()});
put("payins_paypal-direct_create", new String[]{"/payins/payment-methods/paypal", RequestType.POST.toString()});
put("payin_get_refunds", new String[]{"/payins/%s/refunds", RequestType.GET.toString()});
put("payins_recurring_registration", new String[]{"/recurringpayinregistrations", RequestType.POST.toString()});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ public PayIn deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
}
payIn.setPaymentDetails(payInPaymentDetailsPayPal);
break;
case KLARNA:
PayInPaymentDetailsKlarna payInPaymentDetailsKlarna = new PayInPaymentDetailsKlarna();
if (object.has("LineItems") && !object.get("LineItems").isJsonNull()) {
Type listType = new TypeToken<ArrayList<LineItem>>(){}.getType();
payInPaymentDetailsKlarna.setLineItems((List<LineItem>) context.deserialize(object.get("LineItems"), listType));
}
if (object.has("Shipping") && !object.get("Shipping").isJsonNull())
payInPaymentDetailsKlarna.setShipping((Shipping) context.deserialize(object.get("Shipping"), Shipping.class));
if (object.has("Billing") && !object.get("Billing").isJsonNull())
payInPaymentDetailsKlarna.setBilling((Billing) context.deserialize(object.get("Billing"), Billing.class));
if (object.has("PaymentMethod") && !object.get("PaymentMethod").isJsonNull())
payInPaymentDetailsKlarna.setPaymentMethod(object.get("PaymentMethod").getAsString());
if (object.has("Country") && !object.get("Country").isJsonNull())
payInPaymentDetailsKlarna.setCountry(object.get("Country").getAsString());
if (object.has("Culture") && !object.get("Culture").isJsonNull())
payInPaymentDetailsKlarna.setCulture(object.get("Culture").getAsString());
if (object.has("AdditionalData") && !object.get("AdditionalData").isJsonNull())
payInPaymentDetailsKlarna.setAdditionalData(object.get("AdditionalData").getAsString());
if (object.has("Phone") && !object.get("Phone").isJsonNull())
payInPaymentDetailsKlarna.setPhone(object.get("Phone").getAsString());
if (object.has("Email") && !object.get("Email").isJsonNull())
payInPaymentDetailsKlarna.setEmail(object.get("Email").getAsString());
if (object.has("MerchantOrderId") && !object.get("MerchantOrderId").isJsonNull())
payInPaymentDetailsKlarna.setMerchantOrderId(object.get("MerchantOrderId").getAsString());

payIn.setPaymentDetails(payInPaymentDetailsKlarna);
break;
case PAYCONIQ:
PayInPaymentDetailsPayconiq payInPaymentDetailsPayconiq = new PayInPaymentDetailsPayconiq();
if (object.has("ExpirationDate") && !object.get("ExpirationDate").isJsonNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ public enum PayInPaymentType {
/**
* MBWAY payment type
*/
MBWAY
MBWAY,

/**
* KLARNA payment type
*/
KLARNA
}
12 changes: 12 additions & 0 deletions src/main/java/com/mangopay/core/serializer/PayInSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public JsonElement serialize(PayIn src, Type typeOfSrc, JsonSerializationContext
object.add("Shipping", context.serialize(((PayInPaymentDetailsPayPal) src.getPaymentDetails()).getShipping()));
object.add("LineItems", context.serialize(((PayInPaymentDetailsPayPal) src.getPaymentDetails()).getLineItems()));
break;
case "PayInPaymentDetailsKlarna":
object.add("LineItems", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getLineItems()));
object.add("Shipping", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getShipping()));
object.add("Billing", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getShipping()));
object.add("PaymentMethod", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getPaymentMethod()));
object.add("Country", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getCountry()));
object.add("Culture", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getCulture()));
object.add("AdditionalData", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getAdditionalData()));
object.add("Phone", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getPhone()));
object.add("Email", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getEmail()));
object.add("MerchantOrderId", context.serialize(((PayInPaymentDetailsKlarna) src.getPaymentDetails()).getMerchantOrderId()));
break;
case "PayInPaymentDetailsPayconiq":
object.add("Country", context.serialize(((PayInPaymentDetailsPayconiq) src.getPaymentDetails()).getCountry()));
break;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/mangopay/entities/PayIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,18 @@ public Map<String, Map<String, Map<String, Class<?>>>> getDependentObjects() {
put("PaymentDetails", PayInPaymentDetailsGooglePay.class);
}}
);
put("PAYCONIQ", new HashMap<String, Class<?>>(){{
put("PAYCONIQ", new HashMap<String, Class<?>>() {{
put("PaymentDetails", PayInPaymentDetailsPayconiq.class);
}}
);
put("MBWAY", new HashMap<String, Class<?>>() {{
put("PaymentDetails", PayInPaymentDetailsMbway.class);
}}
);
);
put("KLARNA", new HashMap<String, Class<?>>() {{
put("PaymentDetails", PayInPaymentDetailsKlarna.class);
}}
);
// ...and more in future...
}}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.mangopay.entities.subentities;

import com.google.gson.annotations.SerializedName;
import com.mangopay.core.Billing;
import com.mangopay.core.Dto;
import com.mangopay.core.LineItem;
import com.mangopay.core.Shipping;
import com.mangopay.core.interfaces.PayInPaymentDetails;

import java.util.List;

/**
* Class representing the KLARNA type for mean of payment in PayIn entity.
*/
public class PayInPaymentDetailsKlarna extends Dto implements PayInPaymentDetails {

@SerializedName("LineItems")
private List<LineItem> lineItems;

@SerializedName("Shipping")
private Shipping shipping;

@SerializedName("Billing")
private Billing billing;

@SerializedName("PaymentMethod")
private String paymentMethod;

@SerializedName("Country")
private String country;

@SerializedName("Culture")
private String culture;

@SerializedName("AdditionalData")
private String additionalData;

@SerializedName("Phone")
private String phone;

@SerializedName("Email")
private String email;

@SerializedName("MerchantOrderId")
private String merchantOrderId;

public List<LineItem> getLineItems() {
return lineItems;
}

public void setLineItems(List<LineItem> lineItems) {
this.lineItems = lineItems;
}

public Shipping getShipping() {
return shipping;
}

public void setShipping(Shipping shipping) {
this.shipping = shipping;
}

public Billing getBilling() {
return billing;
}

public void setBilling(Billing billing) {
this.billing = billing;
}

public String getPaymentMethod() {
return paymentMethod;
}

public void setPaymentMethod(String paymentMethod) {
this.paymentMethod = paymentMethod;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getCulture() {
return culture;
}

public void setCulture(String culture) {
this.culture = culture;
}

public String getAdditionalData() {
return additionalData;
}

public void setAdditionalData(String additionalData) {
this.additionalData = additionalData;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMerchantOrderId() {
return merchantOrderId;
}

public void setMerchantOrderId(String merchantOrderId) {
this.merchantOrderId = merchantOrderId;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/com/mangopay/core/mangopay.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Fri Jul 28 16:19:09 EEST 2023
#Wed Sep 13 11:15:01 EEST 2023
version=2.30.0
91 changes: 74 additions & 17 deletions src/test/java/com/mangopay/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ protected UserLegal getMatrix() throws Exception {
}

/**
Current optional fields are:
HeadquartersAddress, LegalRepresentativeAddress, LegalRepresentativeEmail, CompanyNumber
*/
* Current optional fields are:
* HeadquartersAddress, LegalRepresentativeAddress, LegalRepresentativeEmail, CompanyNumber
*/
protected UserLegal getMatrixWithoutOptionalFields() throws Exception {
if (BaseTest.MATRIX == null) {
UserNatural john = this.getJohn();
Expand Down Expand Up @@ -483,8 +483,8 @@ private PayInExecutionDetailsWeb getPayInExecutionDetailsWeb() {
BaseTest.PAYIN_EXECUTION_DETAILS_WEB.setSecureMode(SecureMode.DEFAULT);
BaseTest.PAYIN_EXECUTION_DETAILS_WEB.setCulture(CultureCode.FR);
BaseTest.PAYIN_EXECUTION_DETAILS_WEB.setReturnUrl("https://test.com");
if(BaseTest.PAYIN_TEMPLATE_URL_OPTIONS == null) {

if (BaseTest.PAYIN_TEMPLATE_URL_OPTIONS == null) {
BaseTest.PAYIN_TEMPLATE_URL_OPTIONS = new PayInTemplateURLOptions();
BaseTest.PAYIN_TEMPLATE_URL_OPTIONS.PAYLINE = "https://www.maysite.com/payline_template/";
BaseTest.PAYIN_TEMPLATE_URL_OPTIONS.PAYLINEV2 = "https://www.maysite.com/payline_template/";
Expand Down Expand Up @@ -556,6 +556,12 @@ protected PayIn getNewPayInMbwayDirect(String userId) throws Exception {
return this.api.getPayInApi().create(payIn);
}

protected PayIn getNewPayInKlarnaWeb(String userId) throws Exception {
PayIn payIn = getPayInKlarnaWeb(userId);

return this.api.getPayInApi().create(payIn);
}

private PayIn getPayInCardDirect(String userId) throws Exception {

Wallet wallet = this.getJohnsWalletWithMoney();
Expand Down Expand Up @@ -632,6 +638,57 @@ private PayIn getPayInMbwayDirect(String userId) throws Exception {
return payIn;
}

private PayIn getPayInKlarnaWeb(String userId) throws Exception {
Wallet wallet = this.getJohnsWalletWithMoney();

if (userId == null) {
UserNatural user = this.getJohn();
userId = user.getId();
}
// create pay-in KLARNA WEB
PayIn payIn = new PayIn();
payIn.setAuthorId(userId);
payIn.setCreditedWalletId(wallet.getId());
payIn.setDebitedFunds(new Money());
payIn.getDebitedFunds().setAmount(1000);
payIn.getDebitedFunds().setCurrency(CurrencyIso.EUR);
payIn.setFees(new Money());
payIn.getFees().setAmount(100);
payIn.getFees().setCurrency(CurrencyIso.EUR);
payIn.setTag("Klarna test");

// payment type as KLARNA

PayInPaymentDetailsKlarna payInPaymentDetailsKlarna = new PayInPaymentDetailsKlarna();
List<LineItem> lineItems = new ArrayList<>();
lineItems.add(new LineItem(
"test",
1,
1000,
0,
"test descr"
));
payInPaymentDetailsKlarna.setLineItems(lineItems);
payInPaymentDetailsKlarna.setCountry(CountryIso.FR.name());
payInPaymentDetailsKlarna.setCulture(CountryIso.FR.name());
payInPaymentDetailsKlarna.setPhone("33#607080900");
payInPaymentDetailsKlarna.setEmail("[email protected]");
payInPaymentDetailsKlarna.setAdditionalData("{}");
payInPaymentDetailsKlarna.setMerchantOrderId("afd48-879d-48fg");

payInPaymentDetailsKlarna.setBilling(getNewBilling());
payInPaymentDetailsKlarna.setShipping(getNewShipping());
payIn.setPaymentDetails(payInPaymentDetailsKlarna);


// execution type as WEB
PayInExecutionDetailsWeb executionDetails = new PayInExecutionDetailsWeb();
executionDetails.setReturnUrl("http://www.my-site.com/returnURL");
payIn.setExecutionDetails(executionDetails);

return this.api.getPayInApi().create(payIn);
}

protected PayIn getNewPayInCardDirectWithRequested3DSVersion() throws Exception {
PayIn payIn = getPayInCardDirect(null);

Expand Down Expand Up @@ -673,15 +730,15 @@ protected BrowserInfo getNewBrowserInfo() {
return browserInfo;
}

protected PayIn getNewPayInCardDirectWithBrowserInfo() throws Exception{
protected PayIn getNewPayInCardDirectWithBrowserInfo() throws Exception {
PayIn payIn = getPayInCardDirect(null);

((PayInPaymentDetailsCard) payIn.getPaymentDetails()).setBrowserInfo(getNewBrowserInfo());

return this.api.getPayInApi().create(payIn);
}

protected PayIn getNewPayInCardDirectWithIpAddress() throws Exception{
protected PayIn getNewPayInCardDirectWithIpAddress() throws Exception {
PayIn payIn = getPayInCardDirect(null);

((PayInPaymentDetailsCard) payIn.getPaymentDetails()).setIpAddress("2001:0620:0000:0000:0211:24FF:FE80:C12C");
Expand Down Expand Up @@ -933,10 +990,10 @@ protected String getPaylineCorrectRegistartionDataForDeposit(CardRegistration ca
protected String getPaylineCorrectRegistartionData3DSecureForCardNumber(CardRegistration cardRegistration, String cardNumber) throws MalformedURLException, IOException, Exception {

String data = "data=" + cardRegistration.getPreregistrationData() +
"&accessKeyRef=" + cardRegistration.getAccessKey() +
"&cardNumber=" + cardNumber +
"&cardExpirationDate=1224" +
"&cardCvx=123";
"&accessKeyRef=" + cardRegistration.getAccessKey() +
"&cardNumber=" + cardNumber +
"&cardExpirationDate=1224" +
"&cardCvx=123";

URL url = new URL(cardRegistration.getCardRegistrationUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Expand Down Expand Up @@ -1102,12 +1159,12 @@ protected Deposit createNewDeposit() throws Exception {
Money debitedFunds = new Money(CurrencyIso.EUR, 1000);

CreateDeposit dto = new CreateDeposit(
john.getId(),
debitedFunds,
cardRegistration.getCardId(),
"http://lorem",
"2001:0620:0000:0000:0211:24FF:FE80:C12C",
getNewBrowserInfo()
john.getId(),
debitedFunds,
cardRegistration.getCardId(),
"http://lorem",
"2001:0620:0000:0000:0211:24FF:FE80:C12C",
getNewBrowserInfo()
);

return this.api.getDepositApi().create(dto, null);
Expand Down
Loading

0 comments on commit d6cde0a

Please sign in to comment.