Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion src/main/java/com/culqi/apioperation/ObjectResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.culqi.util.EncryptAESRSA;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ public ResponseCulqi update(Map<String, Object> body, String url, String id, Str
EncryptAESRSA encryptAESRSA = new EncryptAESRSA();
jsonData = encryptAESRSA.getJsonEncryptAESRSA(jsonData, rsaPublicKey);

ResponseCulqi response = new ResponseHelper().update(url, jsonData, id);
ResponseCulqi response = new ResponseHelper().update(url, jsonData, id, rsaId);
return response;
}

Expand All @@ -82,6 +83,17 @@ public ResponseCulqi capture(String url, String id) throws Exception {
ResponseCulqi response = new ResponseHelper().capture(url, id);
return response;
}

public ResponseCulqi capture(String url, String id, String rsaPublicKey, String rsaId) throws Exception {
Map<String, Object> body = new HashMap<String, Object>();
String jsonData = mapper.writeValueAsString(body);

EncryptAESRSA encryptAESRSA = new EncryptAESRSA();
jsonData = encryptAESRSA.getJsonEncryptAESRSA(jsonData, rsaPublicKey);

ResponseCulqi response = new ResponseHelper().capture(url, id, jsonData, rsaId);
return response;
}

public ResponseCulqi confirm(String url, String id) throws Exception {
ResponseCulqi response = new ResponseHelper().confirm(url, id);
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/com/culqi/apioperation/ResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public ResponseCulqi list(String url, String params) {
return responseCulqi(GENERIC_ERROR, result);
}

public ResponseCulqi create(String url, String jsonData) {
System.out.println("jsonData "+jsonData);
public ResponseCulqi create(String url, String jsonData) {System.out.println("jsonData "+jsonData);
String result = "";
try {
String api_key = url.contains("tokens") || url.contains("confirm") ? Culqi.public_key : Culqi.secret_key;
Expand Down Expand Up @@ -301,6 +300,31 @@ public ResponseCulqi capture(String url, String id) throws Exception {
}
return responseCulqi(GENERIC_ERROR, result);
}
public ResponseCulqi capture(String url, String id, String jsonData, String rsaId) throws Exception {
String result = "";
try {
String env = Config.X_CULQI_ENV_TEST;
if(Culqi.secret_key.contains("live")) {
env = Config.X_CULQI_ENV_LIVE;
}
RequestBody body = RequestBody.create(JSON, jsonData);
Request.Builder builder = new Request.Builder();
builder.url(config.API_BASE + url + id + "/capture/");
builder.header("Authorization", "Bearer " + Culqi.secret_key)
.header("x-culqi-env", env)
.header("x-culqi-client", Config.X_CULQI_CLIENT)
.header("x-culqi-rsa-id", rsaId)
.header("x-culqi-client-version", Config.X_CULQI_CLIENT_VERSION)
.header("x-api-version", Config.X_API_VERSION);
builder.post(body);
Request request = builder.build();
Response response = client.newCall(request).execute();
return responseCulqi(response.code(), response.body().string());
} catch (IOException e) {
result = exceptionError();
}
return responseCulqi(GENERIC_ERROR, result);
}

public ResponseCulqi confirm(String url, String id) throws Exception {
String result = "";
Expand Down Expand Up @@ -335,6 +359,26 @@ private Request.Builder addCustomHeadersToRequest(Map<String, String> customHead
return builder;
}

private String generateCurlCommand(Request request, String jsonData) {
StringBuilder curlCmd = new StringBuilder("curl -X ").append(request.method().toUpperCase() + " ");

// Añadimos la URL
curlCmd.append("\"").append(request.url().toString()).append("\" ");

// Añadimos los headers
for (String headerName : request.headers().names()) {
String headerValue = request.header(headerName);
curlCmd.append("-H \"").append(headerName).append(": ").append(headerValue).append("\" ");
}

// Añadimos el body (si es necesario)
if (jsonData != null && !jsonData.isEmpty()) {
curlCmd.append("-d '").append(jsonData).append("' ");
}

return curlCmd.toString();
}

private String exceptionError() {
String result = "";
Map<String, Object> errorResponse = new HashMap<String, Object>();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/culqi/apioperation/service/Charge.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public ResponseCulqi capture(String id) throws Exception {
return new ObjectResult().capture(this.URL, id);
}

public ResponseCulqi capture(String id, String rsaPublicKey, String rsaId) throws Exception {
return new ObjectResult().capture(this.URL, id, rsaPublicKey, rsaId);
}

}
93 changes: 89 additions & 4 deletions src/test/java/CulqiCRUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ protected ResponseCulqi createTokenEncrypt() throws Exception {
return init().token.create(jsondata.jsonToken(), rsaPublicKey, rsaId);
}

protected ResponseCulqi updateTokenEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().token.update(jsondata.jsonUpdateToken(), id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createTokenYape() throws Exception {
return init().token.createYape(jsondata.jsonTokenYape());
}
Expand All @@ -61,6 +67,12 @@ protected ResponseCulqi updateOrder() throws Exception {
return init().order.update(jsondata.jsonUpdateOrder(), id);
}

protected ResponseCulqi updateOrderEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createOrderEncrypt(true).getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().order.update(jsondata.jsonUpdateOrder(), id, rsaPublicKey, rsaId);
}

protected ResponseCulqi confirmOrderType() throws Exception {
Map<String, Object> res = mapper.readValue(createOrder(false).getBody(), new TypeReference<HashMap<String, Object>>(){});
String order_id = res.get("id").toString();
Expand All @@ -84,7 +96,7 @@ protected ResponseCulqi createRecurrentCharge() throws Exception {
}

protected ResponseCulqi createChargeEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createToken().getBody(), new TypeReference<HashMap<String, Object>>(){});
Map<String, Object> res = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String source_id = res.get("id").toString();
return init().charge.create(jsondata.jsonCharge(source_id), rsaPublicKey, rsaId);
}
Expand All @@ -102,43 +114,82 @@ protected ResponseCulqi createRecurrentChargeEncrypt() throws Exception {
protected ResponseCulqi updateCharge() throws Exception {
Map<String, Object> res = mapper.readValue(createCharge().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().token.update(jsondata.jsonUpdateCharge(), id);
return init().charge.update(jsondata.jsonUpdateCharge(), id);
}

protected ResponseCulqi updateChargeEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createChargeEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().charge.update(jsondata.jsonUpdateCharge(), id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createPlan() throws Exception {
return init().plan.create(jsondata.jsonPlan());
}

protected ResponseCulqi createPlanEncrypt() throws Exception {
return init().plan.create(jsondata.jsonPlan(), rsaPublicKey, rsaId);
}

protected ResponseCulqi updatePlan() throws Exception {
Map<String, Object> res = mapper.readValue(createPlan().getBody(), new TypeReference<HashMap<String, Object>>(){});
String plan_id = res.get("id").toString();
return init().plan.update(jsondata.jsonUpdatePlan(), plan_id);
}

protected ResponseCulqi updatePlanEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createPlanEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String plan_id = res.get("id").toString();
return init().plan.update(jsondata.jsonUpdatePlan(), plan_id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createCustomer() throws Exception {
return init().customer.create(jsondata.jsonCustomer());
}

protected ResponseCulqi createCustomerEncrypt() throws Exception {
return init().customer.create(jsondata.jsonCustomer(), rsaPublicKey, rsaId);
}

protected ResponseCulqi updateCustomer() throws Exception {
Map<String, Object> res = mapper.readValue(createCustomer().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().customer.update(jsondata.jsonUpdateCustomer(), id);
}

protected ResponseCulqi updateCustomerEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createCustomerEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().customer.update(jsondata.jsonUpdateCustomer(), id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createCard() throws Exception {
Map<String, Object> res = mapper.readValue(createCustomer().getBody(), new TypeReference<HashMap<String, Object>>(){});
String customer_id = res.get("id").toString();
Map<String, Object> res2 = mapper.readValue(createToken().getBody(), new TypeReference<HashMap<String, Object>>(){});
String token_id = res2.get("id").toString();
return init().card.create(jsondata.jsonCard(customer_id,token_id));
}
protected ResponseCulqi createCardEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createCustomerEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String customer_id = res.get("id").toString();
Map<String, Object> res2 = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String token_id = res2.get("id").toString();
return init().card.create(jsondata.jsonCard(customer_id,token_id), rsaPublicKey, rsaId);
}

protected ResponseCulqi updateCard() throws Exception {
Map<String, Object> res = mapper.readValue(createCard().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().card.update(jsondata.jsonUpdateCard(), id);
}

protected ResponseCulqi updateCardEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createCardEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().card.update(jsondata.jsonUpdateCard(), id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createSubscription() throws Exception {
Map<String, Object> res = mapper.readValue(createCard().getBody(), new TypeReference<HashMap<String, Object>>(){});
String card_id = res.get("id").toString();
Expand All @@ -147,23 +198,57 @@ protected ResponseCulqi createSubscription() throws Exception {
return init().subscription.create(jsondata.jsonSubscription(card_id, plan_id));
}

protected ResponseCulqi createSubscriptionEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createCardEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String card_id = res.get("id").toString();
Map<String, Object> res2 = mapper.readValue(createPlanEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String plan_id = res2.get("id").toString();
return init().subscription.create(jsondata.jsonSubscription(card_id, plan_id), rsaPublicKey, rsaId);
}

protected ResponseCulqi updateSubscription() throws Exception {
Map<String, Object> res = mapper.readValue(createSubscription().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().subscription.update(jsondata.jsonUpdateSubscription(), id);
}

Map<String, Object> cardToUpdate = mapper.readValue(createCard().getBody(), new TypeReference<HashMap<String, Object>>(){});
String cardRefId = cardToUpdate.get("id").toString();
Map<String, Object> requestBody = jsondata.jsonUpdateSubscription(cardRefId);
requestBody.put("card_id", cardRefId);
return init().subscription.update(requestBody, id);
}

protected ResponseCulqi updateSubscriptionEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createSubscriptionEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();

Map<String, Object> cardToUpdate = mapper.readValue(createCardEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String cardRefId = cardToUpdate.get("id").toString();
Map<String, Object> requestBody = jsondata.jsonUpdateSubscription(cardRefId);
requestBody.put("card_id", cardRefId);
return init().subscription.update(requestBody, id, rsaPublicKey, rsaId);
}

protected ResponseCulqi createRefund() throws Exception {
Map<String, Object> res = mapper.readValue(createCharge().getBody(), new TypeReference<HashMap<String, Object>>(){});
String charge_id = res.get("id").toString();
return init().refund.create(jsondata.jsonRefund(charge_id));
}
protected ResponseCulqi createRefundEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createChargeEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String charge_id = res.get("id").toString();
return init().refund.create(jsondata.jsonRefund(charge_id), rsaPublicKey, rsaId);
}

protected ResponseCulqi updateRefund() throws Exception {
Map<String, Object> res = mapper.readValue(createRefund().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().refund.update(jsondata.jsonUpdateRefund(), id);
}
protected ResponseCulqi updateRefundEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(createRefundEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
String id = res.get("id").toString();
return init().refund.update(jsondata.jsonUpdateRefund(), id, rsaPublicKey, rsaId);
}

//Listas

Expand Down
48 changes: 48 additions & 0 deletions src/test/java/CulqiCreateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,27 @@ public void test05_createPlan() throws Exception {
assertTrue(id instanceof String);
}

@Test
public void test05_createPlanEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createPlanEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
Object id = res.get("id");
assertTrue(id instanceof String);
}

@Test
public void test06_createCustomer() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createCustomer().getBody(), new TypeReference<HashMap<String, Object>>(){});
System.err.println(res);
assertEquals("customer",res.get("object").toString());
}

@Test
public void test06_createCustomerEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createCustomerEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
System.err.println(res);
assertEquals("customer",res.get("object").toString());
}

@Test
public void test07_createCard() throws Exception {
ResponseCulqi response = culqiCRUD.createCard();
Expand All @@ -115,12 +129,29 @@ public void test07_createCard() throws Exception {
}
}

@Test
public void test07_createCardEncrypt() throws Exception {
ResponseCulqi response = culqiCRUD.createCardEncrypt();
Map<String, Object> res = mapper.readValue(response.getBody(), new TypeReference<HashMap<String, Object>>(){});
if (response.getStatusCode()==200) {
assertEquals("REVIEW",res.get("action_code").toString());
}else if (response.getStatusCode()==201) {
assertEquals("card",res.get("object").toString());
}
}

@Test
public void test08_createSubscription() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createSubscription().getBody(), new TypeReference<HashMap<String, Object>>(){});
Object id = res.get("id");
assertTrue(id instanceof String);
}
@Test
public void test08_createSubscriptionEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createSubscriptionEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
Object id = res.get("id");
assertTrue(id instanceof String);
}

@Test
public void test09_chargeCapture() throws Exception {
Expand All @@ -133,11 +164,28 @@ public void test09_chargeCapture() throws Exception {
assertNotSame("charge", res1.get("object").toString());
}

@Test
public void test09_chargeCaptureEncrypt() throws Exception {
ResponseCulqi response = culqiCRUD.createChargeEncrypt();
Map<String, Object> res = mapper.readValue(response.getBody(), new TypeReference<HashMap<String, Object>>(){});
String charge_id = res.get("id").toString();
ResponseCulqi capture = culqiCRUD.init().charge.capture(charge_id, culqiCRUD.rsaPublicKey, culqiCRUD.rsaId);
Map<String, Object> res1 = mapper.readValue(capture.getBody(), new TypeReference<HashMap<String, Object>>(){});

assertNotSame("charge", res1.get("object").toString());
}

@Test
public void test10_createRefund() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createRefund().getBody(), new TypeReference<HashMap<String, Object>>(){});
assertEquals("refund",res.get("object").toString());
}

@Test
public void test10_createRefundEncrypt() throws Exception {
Map<String, Object> res = mapper.readValue(culqiCRUD.createRefundEncrypt().getBody(), new TypeReference<HashMap<String, Object>>(){});
assertEquals("refund",res.get("object").toString());
}

@Test
public void test11_createOrder() throws Exception {
Expand Down
Loading