Skip to content

Commit

Permalink
Added support for whole DbGenerateRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Grzelak committed Jun 23, 2020
1 parent a7eeffa commit 2be9325
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Data
public class DbGenerateRequest {
private String service_id;
private String serviceId;
private String control;
private String complete;
private String failure;
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/pl/simpay/api/payments/DirectBilling.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import pl.simpay.api.model.generic.IPResponse;
import pl.simpay.api.utils.Hashing;
import pl.simpay.api.utils.HttpService;
import pl.simpay.api.utils.Reflections;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@Data
public class DirectBilling {
Expand Down Expand Up @@ -62,7 +64,7 @@ public DirectBilling(String apiKey) {

// https://docs.simpay.pl/#generowanie-transakcji
public DbGenerateResponse generateTransaction(@NonNull DbGenerateRequest request) {
if (request.getService_id() == null) request.setService_id(serviceId);
if (request.getServiceId() == null) request.setServiceId(serviceId);

String amount = "";

Expand All @@ -74,15 +76,12 @@ public DbGenerateResponse generateTransaction(@NonNull DbGenerateRequest request

FormBody.Builder builder = new FormBody.Builder();

builder.add("serviceId", request.getService_id());
builder.add("amount", request.getAmount());
builder.add("control", request.getControl());

System.out.println(decimalFormat.format(Double.valueOf(amount)));

request.setSign(Hashing.sha256hex(this.serviceId + "" + decimalFormat.format(Double.valueOf(amount)).replace(',', '.') + "" + request.getControl() + "" + this.apiKey));

builder.add("sign", request.getSign());
for (Map.Entry<String, String> entry : Reflections.serializeObject(request).entrySet()) {
System.out.println(entry);
builder.add(entry.getKey(), entry.getValue());
}

return service.sendPost(API_URL, builder.build(), DbGenerateResponse.class);
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/pl/simpay/api/utils/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ private Gson gson() {

String x = response.body().string();

System.out.println(x);

return x;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/pl/simpay/api/utils/Reflections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pl.simpay.api.utils;

import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Reflections {
@SneakyThrows public static Map<String, String> serializeObject(Object o) {
HashMap<String, String> map = new HashMap<>();

for (Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object val = field.get(o);
if (val == null) continue;
map.put(field.getName(), String.valueOf(val));
}

return map;
}
}

0 comments on commit 2be9325

Please sign in to comment.