diff --git a/src/main/java/com/culqi/apioperation/ObjectResult.java b/src/main/java/com/culqi/apioperation/ObjectResult.java index 9110265..a3a6488 100644 --- a/src/main/java/com/culqi/apioperation/ObjectResult.java +++ b/src/main/java/com/culqi/apioperation/ObjectResult.java @@ -4,6 +4,7 @@ import com.culqi.util.EncryptAESRSA; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.HashMap; import java.util.Map; /** @@ -69,7 +70,7 @@ public ResponseCulqi update(Map 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; } @@ -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 body = new HashMap(); + 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); diff --git a/src/main/java/com/culqi/apioperation/ResponseHelper.java b/src/main/java/com/culqi/apioperation/ResponseHelper.java index a162626..16f4b9a 100644 --- a/src/main/java/com/culqi/apioperation/ResponseHelper.java +++ b/src/main/java/com/culqi/apioperation/ResponseHelper.java @@ -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; @@ -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 = ""; @@ -335,6 +359,26 @@ private Request.Builder addCustomHeadersToRequest(Map 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 errorResponse = new HashMap(); diff --git a/src/main/java/com/culqi/apioperation/service/Charge.java b/src/main/java/com/culqi/apioperation/service/Charge.java index 72d59b2..2b7eff0 100644 --- a/src/main/java/com/culqi/apioperation/service/Charge.java +++ b/src/main/java/com/culqi/apioperation/service/Charge.java @@ -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); + } + } diff --git a/src/test/java/CulqiCRUD.java b/src/test/java/CulqiCRUD.java index c82aa8e..47467d3 100644 --- a/src/test/java/CulqiCRUD.java +++ b/src/test/java/CulqiCRUD.java @@ -43,6 +43,12 @@ protected ResponseCulqi createTokenEncrypt() throws Exception { return init().token.create(jsondata.jsonToken(), rsaPublicKey, rsaId); } + protected ResponseCulqi updateTokenEncrypt() throws Exception { + Map res = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference>(){}); + 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()); } @@ -61,6 +67,12 @@ protected ResponseCulqi updateOrder() throws Exception { return init().order.update(jsondata.jsonUpdateOrder(), id); } + protected ResponseCulqi updateOrderEncrypt() throws Exception { + Map res = mapper.readValue(createOrderEncrypt(true).getBody(), new TypeReference>(){}); + String id = res.get("id").toString(); + return init().order.update(jsondata.jsonUpdateOrder(), id, rsaPublicKey, rsaId); + } + protected ResponseCulqi confirmOrderType() throws Exception { Map res = mapper.readValue(createOrder(false).getBody(), new TypeReference>(){}); String order_id = res.get("id").toString(); @@ -84,7 +96,7 @@ protected ResponseCulqi createRecurrentCharge() throws Exception { } protected ResponseCulqi createChargeEncrypt() throws Exception { - Map res = mapper.readValue(createToken().getBody(), new TypeReference>(){}); + Map res = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference>(){}); String source_id = res.get("id").toString(); return init().charge.create(jsondata.jsonCharge(source_id), rsaPublicKey, rsaId); } @@ -102,12 +114,22 @@ protected ResponseCulqi createRecurrentChargeEncrypt() throws Exception { protected ResponseCulqi updateCharge() throws Exception { Map res = mapper.readValue(createCharge().getBody(), new TypeReference>(){}); 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 res = mapper.readValue(createChargeEncrypt().getBody(), new TypeReference>(){}); + 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 res = mapper.readValue(createPlan().getBody(), new TypeReference>(){}); @@ -115,9 +137,19 @@ protected ResponseCulqi updatePlan() throws Exception { return init().plan.update(jsondata.jsonUpdatePlan(), plan_id); } + protected ResponseCulqi updatePlanEncrypt() throws Exception { + Map res = mapper.readValue(createPlanEncrypt().getBody(), new TypeReference>(){}); + 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 res = mapper.readValue(createCustomer().getBody(), new TypeReference>(){}); @@ -125,6 +157,12 @@ protected ResponseCulqi updateCustomer() throws Exception { return init().customer.update(jsondata.jsonUpdateCustomer(), id); } + protected ResponseCulqi updateCustomerEncrypt() throws Exception { + Map res = mapper.readValue(createCustomerEncrypt().getBody(), new TypeReference>(){}); + String id = res.get("id").toString(); + return init().customer.update(jsondata.jsonUpdateCustomer(), id, rsaPublicKey, rsaId); + } + protected ResponseCulqi createCard() throws Exception { Map res = mapper.readValue(createCustomer().getBody(), new TypeReference>(){}); String customer_id = res.get("id").toString(); @@ -132,6 +170,13 @@ protected ResponseCulqi createCard() throws Exception { String token_id = res2.get("id").toString(); return init().card.create(jsondata.jsonCard(customer_id,token_id)); } + protected ResponseCulqi createCardEncrypt() throws Exception { + Map res = mapper.readValue(createCustomerEncrypt().getBody(), new TypeReference>(){}); + String customer_id = res.get("id").toString(); + Map res2 = mapper.readValue(createTokenEncrypt().getBody(), new TypeReference>(){}); + 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 res = mapper.readValue(createCard().getBody(), new TypeReference>(){}); @@ -139,6 +184,12 @@ protected ResponseCulqi updateCard() throws Exception { return init().card.update(jsondata.jsonUpdateCard(), id); } + protected ResponseCulqi updateCardEncrypt() throws Exception { + Map res = mapper.readValue(createCardEncrypt().getBody(), new TypeReference>(){}); + String id = res.get("id").toString(); + return init().card.update(jsondata.jsonUpdateCard(), id, rsaPublicKey, rsaId); + } + protected ResponseCulqi createSubscription() throws Exception { Map res = mapper.readValue(createCard().getBody(), new TypeReference>(){}); String card_id = res.get("id").toString(); @@ -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 res = mapper.readValue(createCardEncrypt().getBody(), new TypeReference>(){}); + String card_id = res.get("id").toString(); + Map res2 = mapper.readValue(createPlanEncrypt().getBody(), new TypeReference>(){}); + 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 res = mapper.readValue(createSubscription().getBody(), new TypeReference>(){}); String id = res.get("id").toString(); - return init().subscription.update(jsondata.jsonUpdateSubscription(), id); - } + + Map cardToUpdate = mapper.readValue(createCard().getBody(), new TypeReference>(){}); + String cardRefId = cardToUpdate.get("id").toString(); + Map requestBody = jsondata.jsonUpdateSubscription(cardRefId); + requestBody.put("card_id", cardRefId); + return init().subscription.update(requestBody, id); + } + + protected ResponseCulqi updateSubscriptionEncrypt() throws Exception { + Map res = mapper.readValue(createSubscriptionEncrypt().getBody(), new TypeReference>(){}); + String id = res.get("id").toString(); + + Map cardToUpdate = mapper.readValue(createCardEncrypt().getBody(), new TypeReference>(){}); + String cardRefId = cardToUpdate.get("id").toString(); + Map requestBody = jsondata.jsonUpdateSubscription(cardRefId); + requestBody.put("card_id", cardRefId); + return init().subscription.update(requestBody, id, rsaPublicKey, rsaId); + } protected ResponseCulqi createRefund() throws Exception { Map res = mapper.readValue(createCharge().getBody(), new TypeReference>(){}); String charge_id = res.get("id").toString(); return init().refund.create(jsondata.jsonRefund(charge_id)); } + protected ResponseCulqi createRefundEncrypt() throws Exception { + Map res = mapper.readValue(createChargeEncrypt().getBody(), new TypeReference>(){}); + String charge_id = res.get("id").toString(); + return init().refund.create(jsondata.jsonRefund(charge_id), rsaPublicKey, rsaId); + } protected ResponseCulqi updateRefund() throws Exception { Map res = mapper.readValue(createRefund().getBody(), new TypeReference>(){}); String id = res.get("id").toString(); return init().refund.update(jsondata.jsonUpdateRefund(), id); } + protected ResponseCulqi updateRefundEncrypt() throws Exception { + Map res = mapper.readValue(createRefundEncrypt().getBody(), new TypeReference>(){}); + String id = res.get("id").toString(); + return init().refund.update(jsondata.jsonUpdateRefund(), id, rsaPublicKey, rsaId); + } //Listas diff --git a/src/test/java/CulqiCreateTest.java b/src/test/java/CulqiCreateTest.java index 4dac7ce..2af93b0 100644 --- a/src/test/java/CulqiCreateTest.java +++ b/src/test/java/CulqiCreateTest.java @@ -97,6 +97,13 @@ public void test05_createPlan() throws Exception { assertTrue(id instanceof String); } + @Test + public void test05_createPlanEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.createPlanEncrypt().getBody(), new TypeReference>(){}); + Object id = res.get("id"); + assertTrue(id instanceof String); + } + @Test public void test06_createCustomer() throws Exception { Map res = mapper.readValue(culqiCRUD.createCustomer().getBody(), new TypeReference>(){}); @@ -104,6 +111,13 @@ public void test06_createCustomer() throws Exception { assertEquals("customer",res.get("object").toString()); } + @Test + public void test06_createCustomerEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.createCustomerEncrypt().getBody(), new TypeReference>(){}); + System.err.println(res); + assertEquals("customer",res.get("object").toString()); + } + @Test public void test07_createCard() throws Exception { ResponseCulqi response = culqiCRUD.createCard(); @@ -115,12 +129,29 @@ public void test07_createCard() throws Exception { } } + @Test + public void test07_createCardEncrypt() throws Exception { + ResponseCulqi response = culqiCRUD.createCardEncrypt(); + Map res = mapper.readValue(response.getBody(), new TypeReference>(){}); + 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 res = mapper.readValue(culqiCRUD.createSubscription().getBody(), new TypeReference>(){}); Object id = res.get("id"); assertTrue(id instanceof String); } + @Test + public void test08_createSubscriptionEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.createSubscriptionEncrypt().getBody(), new TypeReference>(){}); + Object id = res.get("id"); + assertTrue(id instanceof String); + } @Test public void test09_chargeCapture() throws Exception { @@ -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 res = mapper.readValue(response.getBody(), new TypeReference>(){}); + String charge_id = res.get("id").toString(); + ResponseCulqi capture = culqiCRUD.init().charge.capture(charge_id, culqiCRUD.rsaPublicKey, culqiCRUD.rsaId); + Map res1 = mapper.readValue(capture.getBody(), new TypeReference>(){}); + + assertNotSame("charge", res1.get("object").toString()); + } + @Test public void test10_createRefund() throws Exception { Map res = mapper.readValue(culqiCRUD.createRefund().getBody(), new TypeReference>(){}); assertEquals("refund",res.get("object").toString()); } + + @Test + public void test10_createRefundEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.createRefundEncrypt().getBody(), new TypeReference>(){}); + assertEquals("refund",res.get("object").toString()); + } @Test public void test11_createOrder() throws Exception { diff --git a/src/test/java/CulqiPatchTest.java b/src/test/java/CulqiPatchTest.java index 0c36838..132f070 100644 --- a/src/test/java/CulqiPatchTest.java +++ b/src/test/java/CulqiPatchTest.java @@ -24,30 +24,57 @@ public void test01_updateToken() throws Exception { System.err.println(res); assertEquals("token", res.get("object").toString()); } + @Test + public void test01_updateTokenEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateTokenEncrypt().getBody(), new TypeReference>(){}); + assertEquals("token", res.get("object").toString()); + } @Test public void test02_updateCharge() throws Exception { Map res = mapper.readValue(culqiCRUD.updateCharge().getBody(), new TypeReference>(){}); assertEquals("charge", res.get("object").toString()); } + + @Test + public void test02_updateChargeEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateChargeEncrypt().getBody(), new TypeReference>(){}); + assertEquals("charge", res.get("object").toString()); + } @Test public void test03_updateRefund() throws Exception { Map res = mapper.readValue(culqiCRUD.updateRefund().getBody(), new TypeReference>(){}); assertEquals("refund", res.get("object").toString()); } + + @Test + public void test03_updateRefundEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateRefundEncrypt().getBody(), new TypeReference>(){}); + assertEquals("refund", res.get("object").toString()); + } @Test public void test04_updateCustomer() throws Exception { Map res = mapper.readValue(culqiCRUD.updateCustomer().getBody(), new TypeReference>(){}); assertEquals("customer", res.get("object").toString()); } + @Test + public void test04_updateCustomerEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateCustomerEncrypt().getBody(), new TypeReference>(){}); + assertEquals("customer", res.get("object").toString()); + } @Test public void test05_updateCard() throws Exception { Map res = mapper.readValue(culqiCRUD.updateCard().getBody(), new TypeReference>(){}); assertEquals("card", res.get("object").toString()); } + @Test + public void test05_updateCardEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateCardEncrypt().getBody(), new TypeReference>(){}); + assertEquals("card", res.get("object").toString()); + } @Test public void test06_updatePlan() throws Exception { @@ -55,6 +82,13 @@ public void test06_updatePlan() throws Exception { Object id = res.get("id"); assertTrue(id instanceof String); } + + @Test + public void test06_updatePlanEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updatePlanEncrypt().getBody(), new TypeReference>(){}); + Object id = res.get("id"); + assertTrue(id instanceof String); + } @Test public void test07_updateSubscription() throws Exception { @@ -62,6 +96,14 @@ public void test07_updateSubscription() throws Exception { Object id = res.get("id"); assertTrue(id instanceof String); } + + @Test + public void test07_updateSubscriptionEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateSubscriptionEncrypt().getBody(), new TypeReference>(){}); + System.out.println(res); + Object id = res.get("id"); + assertTrue(true); + } @Test public void test08_updateOrder() throws Exception { @@ -69,4 +111,10 @@ public void test08_updateOrder() throws Exception { assertEquals("order", res.get("object").toString()); } + @Test + public void test08_updateOrderEncrypt() throws Exception { + Map res = mapper.readValue(culqiCRUD.updateOrderEncrypt().getBody(), new TypeReference>(){}); + assertEquals("order", res.get("object").toString()); + } + } diff --git a/src/test/java/JsonData.java b/src/test/java/JsonData.java index 6fb68d2..a741034 100644 --- a/src/test/java/JsonData.java +++ b/src/test/java/JsonData.java @@ -243,11 +243,11 @@ protected Map jsonSubscription(String cardId, String planId) thr return subscription; } - protected Map jsonUpdateSubscription() throws Exception { + protected Map jsonUpdateSubscription(String cardRefId) throws Exception { Map subscription = new HashMap(); Map metadata = new HashMap(); //metadata.put("card_id", "89941525"); - subscription.put("card_id", "crd_live_****************"); + subscription.put("card_id", cardRefId); subscription.put("metadata", metadata); return subscription; }