Advertisement
Guest User

aaa

a guest
Apr 18th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.71 KB | None | 0 0
  1. package br.com.videosoft.totemkoerich.service;
  2.  
  3. import br.com.videosoft.totemkoerich.common.Configuration;
  4. import br.com.videosoft.totemkoerich.common.PaymentMethodEnum;
  5. import br.com.videosoft.totemkoerich.models.BillInformation;
  6. import br.com.videosoft.totemkoerich.models.Session;
  7. import com.sun.jersey.api.client.Client;
  8. import com.sun.jersey.api.client.ClientResponse;
  9. import com.sun.jersey.api.client.WebResource;
  10. import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
  11. import java.math.BigDecimal;
  12. import java.security.KeyManagementException;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.security.cert.X509Certificate;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. import javax.net.ssl.HostnameVerifier;
  20. import javax.net.ssl.HttpsURLConnection;
  21. import javax.net.ssl.SSLContext;
  22. import javax.net.ssl.SSLSession;
  23. import javax.net.ssl.TrustManager;
  24. import javax.net.ssl.X509TrustManager;
  25. import javax.ws.rs.core.HttpHeaders;
  26. import javax.ws.rs.core.MediaType;
  27. import org.apache.log4j.Logger;
  28. import org.json.JSONArray;
  29. import org.json.JSONObject;
  30.  
  31. public class TotemKoerichWebservice {
  32.  
  33. private static final Logger LOGGER = Logger.getLogger(TotemKoerichWebservice.class);
  34.  
  35. private TotemKoerichWebservice() {
  36. }
  37.  
  38. public static TotemKoerichWebservice getInstance() {
  39. return TotemKoerichWebserviceHolder.INSTANCE;
  40. }
  41.  
  42.  
  43.  
  44. private static class TotemKoerichWebserviceHolder {
  45.  
  46. private static final TotemKoerichWebservice INSTANCE = new TotemKoerichWebservice();
  47. }
  48.  
  49.  
  50. //*****************************************TOTEM KOERICH*****************************************//
  51.  
  52. public JSONObject listBillsUnpaid(String method) {
  53.  
  54.  
  55. try {
  56.  
  57. String user = Session.getInstance().getUserInformation().getUserIdentifier();
  58. String filial = Configuration.getInstance().getStoreId();
  59.  
  60. Client client = Client.create();
  61.  
  62. disableSslVerification();
  63. String authorization = getAuthorization();
  64. // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/parcelasCarne/"+filial+"/"+user);
  65. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user);
  66.  
  67. LOGGER.info("Resource [ " + Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user + " ]");
  68.  
  69. String bearerAuthorization = "Bearer "+authorization;
  70.  
  71. ClientResponse response = webResource
  72. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  73. .type(MediaType.APPLICATION_JSON)
  74. .accept(MediaType.APPLICATION_JSON)
  75. .get(ClientResponse.class);
  76.  
  77. if (response.getStatus() != 200) {
  78. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  79. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  80. }
  81.  
  82. String output = response.getEntity(String.class);
  83.  
  84. JSONObject jsonObj = new JSONObject(output);
  85.  
  86. LOGGER.info("JSON listBillsUnpaid return >>" + jsonObj.toString() + "<<");
  87. return jsonObj;
  88.  
  89. } catch (Exception e) {
  90.  
  91. LOGGER.error("Erro na chamada do webservice de consulta dos menus", e);
  92. return null;
  93.  
  94. }
  95. }
  96.  
  97. public JSONObject getMenu() {
  98. try {
  99. String filial = Configuration.getInstance().getStoreId();
  100.  
  101. Client client = Client.create();
  102.  
  103. disableSslVerification();
  104. String authorization = getAuthorization();
  105. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/layout/"+filial+"/");
  106.  
  107. String bearerAuthorization = "Bearer "+authorization;
  108.  
  109. ClientResponse response = webResource
  110. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  111. .type(MediaType.APPLICATION_JSON)
  112. .accept(MediaType.APPLICATION_JSON)
  113. .get(ClientResponse.class);
  114.  
  115. if (response.getStatus() != 200) {
  116. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  117. }
  118.  
  119. String output = response.getEntity(String.class);
  120.  
  121. JSONObject jsonObj = new JSONObject(output);
  122. LOGGER.info("JSON getMenu return >>" + jsonObj.toString() + "<<");
  123. return jsonObj;
  124.  
  125. } catch (Exception e) {
  126.  
  127. LOGGER.error("Erro na chamada do webservice de consulta dos menus", e);
  128. return null;
  129.  
  130. }
  131.  
  132. }
  133.  
  134. public JSONObject getCliente(String cpf){
  135. try {
  136. String filial = Configuration.getInstance().getStoreId();
  137.  
  138. Client client = Client.create();
  139.  
  140. disableSslVerification();
  141. String authorization = getAuthorization();
  142.  
  143. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/cliente/"+filial+"/"+cpf);
  144. String bearerAuthorization = "Bearer "+authorization;
  145.  
  146. ClientResponse response = webResource
  147. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  148. .type(MediaType.APPLICATION_JSON)
  149. .accept(MediaType.APPLICATION_JSON)
  150. .get(ClientResponse.class);
  151.  
  152. if (response.getStatus() != 200) {
  153. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  154. }
  155.  
  156. String output = response.getEntity(String.class);
  157.  
  158. JSONObject jsonObj = new JSONObject(output);
  159. LOGGER.info("JSON getCliente return >>" + jsonObj.toString() + "<<");
  160. return jsonObj;
  161. } catch (Exception e) {
  162.  
  163. LOGGER.error("Erro na chamada do webservice de login do cliente", e);
  164. return null;
  165.  
  166. }
  167. }
  168.  
  169. public JSONObject getValoresSaque(){
  170. try {
  171. String user = Session.getInstance().getUserInformation().getUserIdentifier();
  172. String filial = Configuration.getInstance().getStoreId();
  173.  
  174. Client client = Client.create();
  175.  
  176. disableSslVerification();
  177. String authorization = getAuthorization();
  178.  
  179. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/valoresSaque/"+filial+"/"+user);
  180. String bearerAuthorization = "Bearer "+authorization;
  181. LOGGER.info("JSON getValoresSaque sent >>" + Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/valoresSaque/"+filial+"/"+user + "<<");
  182.  
  183. ClientResponse response = webResource
  184. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  185. .type(MediaType.APPLICATION_JSON)
  186. .accept(MediaType.APPLICATION_JSON)
  187. .get(ClientResponse.class);
  188.  
  189. if (response.getStatus() != 200) {
  190. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  191. }
  192.  
  193. String output = response.getEntity(String.class);
  194.  
  195. JSONObject jsonObj = new JSONObject(output);
  196. LOGGER.info("JSON getValoresSaque return >>" + jsonObj.toString() + "<<");
  197. return jsonObj;
  198. } catch (Exception e) {
  199.  
  200. LOGGER.error("Erro na chamada do webservice de valores de saque do cliente", e);
  201. return null;
  202.  
  203. }
  204. }
  205.  
  206. public JSONObject getProdutosCatalogo(String method){
  207. try {
  208. String user = Session.getInstance().getUserInformation().getUserIdentifier();
  209. String filial = Configuration.getInstance().getStoreId();
  210.  
  211. Client client = Client.create();
  212.  
  213. disableSslVerification();
  214. String authorization = getAuthorization();
  215.  
  216. // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/produtosCatalogo/"+filial+"/"+user);
  217. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user);
  218. String bearerAuthorization = "Bearer "+authorization;
  219.  
  220. LOGGER.info("Resource [ " + Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user + " ]");
  221.  
  222. ClientResponse response = webResource
  223. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  224. .type(MediaType.APPLICATION_JSON)
  225. .accept(MediaType.APPLICATION_JSON)
  226. .get(ClientResponse.class);
  227.  
  228. if (response.getStatus() != 200) {
  229. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  230. }
  231.  
  232. String output = response.getEntity(String.class);
  233.  
  234.  
  235.  
  236. JSONObject jsonObj = new JSONObject(output);
  237. LOGGER.info("JSON getProdutosCatalogo return >>" + jsonObj.toString() + "<<");
  238. return jsonObj;
  239. } catch (Exception e) {
  240.  
  241. LOGGER.error("Erro na chamada do webservice de catalogo de produtos do cliente", e);
  242. return null;
  243. }
  244. }
  245.  
  246.  
  247. public String getDadosCliente(String codigo){
  248. try {
  249. String filial = Configuration.getInstance().getStoreId();
  250.  
  251. Client client = Client.create();
  252.  
  253. disableSslVerification();
  254. String authorization = getAuthorization();
  255. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/dadosCliente/"+filial+"/"+codigo);
  256.  
  257. String bearerAuthorization = "Bearer "+authorization;
  258.  
  259. ClientResponse response = webResource
  260. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  261. .type(MediaType.APPLICATION_JSON)
  262. .accept(MediaType.APPLICATION_JSON)
  263. .get(ClientResponse.class);
  264.  
  265. if (response.getStatus() != 200) {
  266. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  267. }
  268.  
  269. String output = response.getEntity(String.class);
  270.  
  271. JSONObject jsonObj = new JSONObject(output);
  272. LOGGER.info("JSON getDadosCliente return >>" + jsonObj.toString() + "<<");
  273. return jsonObj.getString("htmlInformativo");
  274.  
  275. } catch (Exception e) {
  276.  
  277. LOGGER.error("Erro na chamada do webservice de consulta dos dados do cliente", e);
  278. return null;
  279.  
  280. }
  281. }
  282.  
  283. public JSONObject getContent(String method){
  284. try {
  285. String filial = Configuration.getInstance().getStoreId();
  286.  
  287. Client client = Client.create();
  288.  
  289. disableSslVerification();
  290. String authorization = getAuthorization();
  291. String resource = "";
  292. if((boolean) Session.getInstance().getDetailView().get("requerLogin")){
  293. resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+Session.getInstance().getUserInformation().getUserIdentifier();
  294. }else{
  295. resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
  296. }
  297. LOGGER.info("getContent resource [ " + resource + " ]");
  298. WebResource webResource = client.resource(resource);
  299.  
  300. String bearerAuthorization = "Bearer "+authorization;
  301.  
  302. ClientResponse response = webResource
  303. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  304. .type(MediaType.APPLICATION_JSON)
  305. .accept(MediaType.APPLICATION_JSON)
  306. .get(ClientResponse.class);
  307.  
  308. if (response.getStatus() != 200) {
  309. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  310. }
  311.  
  312. String output = response.getEntity(String.class);
  313.  
  314.  
  315. JSONObject jsonObj = new JSONObject(output);
  316. LOGGER.info("JSON getContent return >>" + jsonObj.toString() + "<<");
  317. return jsonObj;
  318.  
  319. } catch (Exception e) {
  320.  
  321. LOGGER.error("Erro na chamada do webservice de consulta do conteudo", e);
  322. return null;
  323.  
  324. }
  325.  
  326. }
  327.  
  328. public String getActionButton(String method){
  329. return getActionButton(method, (boolean) Session.getInstance().getDetailView().get("requerLogin"), "");
  330. }
  331.  
  332. public String getActionButton(String method, boolean userRequired, String product){
  333. try {
  334. String filial = Configuration.getInstance().getStoreId();
  335.  
  336. Client client = Client.create();
  337.  
  338. disableSslVerification();
  339. String authorization = getAuthorization();
  340. String resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
  341.  
  342. if(userRequired){
  343. resource = resource + Session.getInstance().getUserInformation().getUserIdentifier()+"/";
  344. }
  345.  
  346. if (product != ""){
  347. resource = resource + product;
  348. }
  349.  
  350. LOGGER.info("getActionButton resource [ " + resource + " ]");
  351.  
  352. WebResource webResource = client.resource(resource);
  353.  
  354. String bearerAuthorization = "Bearer "+authorization;
  355.  
  356. ClientResponse response = webResource
  357. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  358. .type(MediaType.APPLICATION_JSON)
  359. .accept(MediaType.APPLICATION_JSON)
  360. .post(ClientResponse.class, "[]");
  361.  
  362. if (response.getStatus() != 200) {
  363. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  364.  
  365. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  366. }
  367.  
  368. String output = response.getEntity(String.class);
  369.  
  370.  
  371. JSONObject jsonObj = new JSONObject(output);
  372. LOGGER.info("JSON getActionButton return >>" + jsonObj.toString() + "<<");
  373. String ReturnAction = "";
  374. if(jsonObj.has("retorno")){
  375. ReturnAction = jsonObj.getString("retorno");
  376. }
  377. return ReturnAction;
  378.  
  379. } catch (Exception e) {
  380.  
  381. LOGGER.error("Erro na chamada do webservice de ação de botão", e);
  382. return null;
  383.  
  384. }
  385. }
  386.  
  387. public String postActionCatalogProductsButton(String method, ArrayList<String> productSelected){
  388. try {
  389. String filial = Configuration.getInstance().getStoreId();
  390.  
  391. Client client = Client.create();
  392.  
  393. JSONObject jsonBody = new JSONObject();
  394.  
  395. JSONArray jsonArrayProducts = new JSONArray();
  396. for(int i=0;i<productSelected.size();i++){
  397. JSONObject product = new JSONObject();
  398. product.put("id", productSelected.get(i));
  399. jsonArrayProducts.put(product);
  400. //ID do produto pega aqui, mas na listagem em horizontal, pode vir mais de um, na vertical SEMPRE vem apenas um, então esse laço n precisa
  401. }
  402. // jsonBody.put("dadosOperacao", jsonArrayProducts);
  403.  
  404.  
  405. disableSslVerification();
  406. String authorization = getAuthorization();
  407. String resource = "";
  408. if((boolean) Session.getInstance().getDetailView().get("requerLogin")){
  409. //ta errado aqui, pq ele ta mandando filial OK, e depois o userIdentifier, é pra mandar o id do produto selecionado
  410. //só que essa função tb é chamada pelo catalogo de produtos na horizontal, que deve ficar como esta aqui
  411. // estranho q eu lembro de ter feito, parece q n mexi, cria uma copia dessa funcao para vertical postActionCatalogVerticalProductsButton
  412. resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+IDdoProduto;
  413. resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+Session.getInstance().getUserInformation().getUserIdentifier();
  414. }else{
  415. resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
  416. }
  417. LOGGER.info("postActionCatalogProductsButton resource [ " + resource + " ]");
  418.  
  419. WebResource webResource = client.resource(resource);
  420.  
  421. String bearerAuthorization = "Bearer "+authorization;
  422.  
  423. LOGGER.info("JSON postActionCatalogProductsButton sent >>" + jsonArrayProducts.toString() + "<<");
  424.  
  425. ClientResponse response = webResource
  426. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  427. .type(MediaType.APPLICATION_JSON)
  428. .accept(MediaType.APPLICATION_JSON)
  429. .post(ClientResponse.class, jsonArrayProducts.toString());
  430.  
  431. if (response.getStatus() != 200) {
  432. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  433.  
  434. throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
  435. }
  436.  
  437. String output = response.getEntity(String.class);
  438.  
  439.  
  440. JSONObject jsonObj = new JSONObject(output);
  441.  
  442. LOGGER.info("JSON postActionCatalogProductsButton return >>" + jsonObj.toString() + "<<");
  443. String ReturnAction = "";
  444. if(jsonObj.has("retorno")){
  445. ReturnAction = jsonObj.getString("retorno");
  446. }
  447. return ReturnAction;
  448.  
  449. } catch (Exception e) {
  450.  
  451. LOGGER.error("Erro na chamada do webservice de ação de botão", e);
  452. return null;
  453.  
  454. }
  455. }
  456.  
  457. public JSONObject postTransacoesStart(List<BillInformation> bill) {
  458. try {
  459. String filial = Configuration.getInstance().getStoreId();
  460.  
  461. Client client = Client.create();
  462.  
  463. disableSslVerification();
  464. String authorization = getAuthorization();
  465. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/start/" + filial + "/" + Session.getInstance().getUserInformation().getUserIdentifier());
  466. // client.addFilter(new HTTPBasicAuthFilter(username, password));
  467.  
  468. String bearerAuthorization = "Bearer " + authorization;
  469.  
  470. ArrayList<JSONObject> documentos = new ArrayList<>();
  471. BigDecimal total = new BigDecimal(0);
  472. for (BillInformation register : bill) {
  473. JSONObject registerBill = new JSONObject();
  474. registerBill.put("documento", register.getBillDocument());
  475. registerBill.put("tipoDocumento", register.getTypeDocument());
  476. if(register.getParcel() != 0){
  477. registerBill.put("referencia", register.getParcel());
  478. }
  479. registerBill.put("valorVencimento", register.getValue());
  480. registerBill.put("valorPagar", register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
  481.  
  482. total = total.add(register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
  483.  
  484. documentos.add(registerBill);
  485. }
  486.  
  487. JSONObject jsonBody = new JSONObject();
  488. jsonBody.put("documentos", documentos);
  489. jsonBody.put("valorAutenticacao", total);
  490.  
  491. LOGGER.info("JSON postTransacoesStart sent >>" + jsonBody.toString() + "<<");
  492.  
  493. ClientResponse response = webResource
  494. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  495. .type(MediaType.APPLICATION_JSON)
  496. .accept(MediaType.APPLICATION_JSON)
  497. .post(ClientResponse.class, jsonBody.toString());
  498. // ClientResponse response = webResource.accept("application/json").post(ClientResponse.class);
  499.  
  500. if (response.getStatus() != 200) {
  501. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  502.  
  503. throw new RuntimeException("Failed : HTTP error code : "
  504. + response.getStatus());
  505. }
  506.  
  507. String output = response.getEntity(String.class);
  508.  
  509.  
  510. JSONObject jsonObj = new JSONObject(output);
  511. LOGGER.info("JSON postTransacoesStart return >>" + jsonObj.toString() + "<<");
  512.  
  513.  
  514. return jsonObj;
  515. } catch (Exception e) {
  516.  
  517. LOGGER.error("Erro na chamada do webservice de post de transação", e);
  518. return null;
  519.  
  520. }
  521. }
  522.  
  523. public boolean postTransacoesCash(List<BillInformation> bill, PaymentMethodEnum paymentMethod, JSONObject resultPayment, String user, int codeMethodPaymentKoerich) {
  524. try{
  525. String filial = Configuration.getInstance().getStoreId();
  526.  
  527. Client client = Client.create();
  528.  
  529. disableSslVerification();
  530. String authorization = getAuthorization();
  531. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/cash/" + filial + "/" + user);
  532. // client.addFilter(new HTTPBasicAuthFilter(username, password));
  533.  
  534. String bearerAuthorization = "Bearer " + authorization;
  535.  
  536. ArrayList<JSONObject> documentos = new ArrayList<>();
  537. BigDecimal total = new BigDecimal(0);
  538. for (BillInformation register : bill) {
  539. JSONObject registerBill = new JSONObject();
  540. registerBill.put("documento", register.getBillDocument());
  541. registerBill.put("tipoDocumento", register.getTypeDocument());
  542. if(register.getParcel() != 0){
  543. registerBill.put("referencia", register.getParcel());
  544. }
  545. registerBill.put("valorVencimento", register.getValue());
  546. registerBill.put("valorPagar", register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
  547. registerBill.put("dataVencimento", register.getExpirationDate().toString());
  548.  
  549. total = total.add(register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
  550.  
  551. documentos.add(registerBill);
  552. }
  553.  
  554. ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
  555.  
  556. JSONObject item;
  557.  
  558. switch(paymentMethod) {
  559. case CASH :
  560.  
  561. item = new JSONObject();
  562. item.put("formaRecebimento", codeMethodPaymentKoerich);
  563. item.put("valor", total);
  564.  
  565. formasRecebidas.add(item);
  566. break;
  567.  
  568. case CARD_DEBIT :
  569. case CARD_DEBIT_BILL :
  570. case CARD_CREDIT :
  571.  
  572. String data = resultPayment.getString("dataHora").substring(0, 10);
  573. String hora = resultPayment.getString("dataHora").substring(11);
  574.  
  575. int modalidade = Integer.valueOf(resultPayment.getString("tipoTransacaoCod").substring(0, 2));
  576. int submodalidade = Integer.valueOf(resultPayment.getString("tipoTransacaoCod").substring(2, 4));
  577.  
  578. item = new JSONObject();
  579. item.put("formaRecebimento", codeMethodPaymentKoerich);
  580. item.put("valor", total);
  581. item.put("tipo", "TEF");
  582.  
  583. JSONObject dadosTef = new JSONObject();
  584. dadosTef.put("data", data);
  585. dadosTef.put("hora", hora);
  586. dadosTef.put("valor", total);
  587. dadosTef.put("nsu", resultPayment.get("nsuSitef"));
  588. dadosTef.put("nsuHost", resultPayment.get("nsuHost"));
  589.  
  590. if (resultPayment.has("autorizacaoCredito")) {
  591. dadosTef.put("autorizacao", resultPayment.get("autorizacaoCredito"));
  592. } else {
  593. dadosTef.put("autorizacao", "0");
  594. }
  595.  
  596. dadosTef.put("rede", Integer.valueOf(resultPayment.getString("autorizadorId")));
  597. dadosTef.put("bandeira", Integer.valueOf(resultPayment.getString("tipoCartaoId")));
  598. dadosTef.put("modalidade", modalidade);
  599. dadosTef.put("submodalidade", submodalidade);
  600. // dadosTef.put("pagamento", 16);
  601.  
  602. item.put("dadosTEF", dadosTef);
  603.  
  604. formasRecebidas.add(item);
  605. break;
  606. }
  607.  
  608. JSONObject jsonBody = new JSONObject();
  609. jsonBody.put("documentos", documentos);
  610. jsonBody.put("valorAutenticacao", total);
  611. jsonBody.put("formasRecebidas", formasRecebidas);
  612.  
  613. LOGGER.info("JSON postTransacoesCash sent >>" + jsonBody.toString() + "<<");
  614.  
  615. ClientResponse response = webResource
  616. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  617. .type(MediaType.APPLICATION_JSON)
  618. .accept(MediaType.APPLICATION_JSON)
  619. .post(ClientResponse.class, jsonBody.toString());
  620.  
  621. if (response.getStatus() != 200) {
  622. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  623.  
  624. throw new RuntimeException("Failed : HTTP error code : "
  625. + response.getStatus());
  626. }
  627.  
  628. String output = response.getEntity(String.class);
  629.  
  630.  
  631. JSONObject jsonObj = new JSONObject(output);
  632. LOGGER.info("JSON postTransacoesCash return >>" + jsonObj.toString() + "<<");
  633.  
  634. return jsonObj.getBoolean("sucesso");
  635. } catch (Exception e) {
  636.  
  637. LOGGER.error("Erro na chamada do webservice de post de transação cash", e);
  638. return false;
  639.  
  640. }
  641. }
  642.  
  643. public boolean postTransacoesRollback(int clientId) {
  644. try{
  645. String filial = Configuration.getInstance().getStoreId();
  646.  
  647. Client client = Client.create();
  648.  
  649. disableSslVerification();
  650. String authorization = getAuthorization();
  651. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/rollback/" + filial + "/" + clientId);
  652.  
  653. String bearerAuthorization = "Bearer " + authorization;
  654.  
  655. ClientResponse response = webResource
  656. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  657. .type(MediaType.APPLICATION_JSON)
  658. .accept(MediaType.APPLICATION_JSON)
  659. .post(ClientResponse.class);
  660.  
  661. if (response.getStatus() != 200) {
  662. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  663.  
  664. throw new RuntimeException("Failed : HTTP error code : "
  665. + response.getStatus());
  666. }
  667.  
  668. String output = response.getEntity(String.class);
  669.  
  670. JSONObject jsonObj = new JSONObject(output);
  671. LOGGER.info("JSON postTransacoesRollback return >>" + jsonObj.toString() + "<<");
  672.  
  673. return jsonObj.getBoolean("sucesso");
  674. } catch (Exception e) {
  675.  
  676. LOGGER.error("Erro na chamada do webservice de post de transação rollback", e);
  677. return false;
  678.  
  679. }
  680. }
  681.  
  682. public boolean postTransacoesVoucher(String valor, Long cupomFiscal) {
  683. try{
  684. JSONObject jsonBody = new JSONObject();
  685. jsonBody.put("valor", Double.parseDouble(valor));
  686. jsonBody.put("cupomFiscal", cupomFiscal);
  687.  
  688. String filial = Configuration.getInstance().getStoreId();
  689.  
  690. Client client = Client.create();
  691.  
  692. disableSslVerification();
  693. String authorization = getAuthorization();
  694.  
  695. WebResource webResource;
  696. if(Session.getInstance().getUserInformation() != null){
  697. webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial + "?cliente=" + Session.getInstance().getUserInformation().getUserIdentifier());
  698. }else{
  699. webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial);
  700. }
  701.  
  702. // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial + "/" + Session.getInstance().getUserInformation().getUserIdentifier());
  703.  
  704. String bearerAuthorization = "Bearer " + authorization;
  705.  
  706.  
  707. LOGGER.info("JSON postTransacoesVoucher sent >>" + jsonBody.toString() + "<<");
  708.  
  709. ClientResponse response = webResource
  710. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  711. .type(MediaType.APPLICATION_JSON)
  712. .accept(MediaType.APPLICATION_JSON)
  713. .post(ClientResponse.class, jsonBody.toString());
  714.  
  715. if (response.getStatus() != 200) {
  716. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  717.  
  718. throw new RuntimeException("Failed : HTTP error code : "
  719. + response.getStatus());
  720. }
  721.  
  722. String output = response.getEntity(String.class);
  723.  
  724. JSONObject jsonObj = new JSONObject(output);
  725. LOGGER.info("JSON postTransacoesVoucher return >>" + jsonObj.toString() + "<<");
  726.  
  727. return jsonObj.getBoolean("sucesso");
  728. } catch (Exception e) {
  729.  
  730. LOGGER.error("Erro na chamada do webservice de post de transação voucher", e);
  731. return false;
  732.  
  733. }
  734. }
  735.  
  736. // public boolean postTransacoesWithdrawal(int total, PaymentMethodEnum paymentMethod, JSONObject resultPayment, String user) {
  737. public boolean postTransacoesWithdrawal(double total, JSONObject resultWithdrawal, String user) {
  738. try{
  739. String filial = Configuration.getInstance().getStoreId();
  740.  
  741. Client client = Client.create();
  742.  
  743. disableSslVerification();
  744. String authorization = getAuthorization();
  745. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/withdrawal/" + filial + "/" + user);
  746.  
  747. String bearerAuthorization = "Bearer " + authorization;
  748.  
  749.  
  750. ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
  751.  
  752. JSONObject item;
  753.  
  754.  
  755. Date dateNow = new Date();
  756. SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
  757. SimpleDateFormat formatHour = new SimpleDateFormat("HH:mm:ss");
  758.  
  759. String data = formatDate.format( dateNow );
  760. String hora = formatHour.format( dateNow );
  761.  
  762.  
  763. int modalidade = Integer.valueOf(resultWithdrawal.getString("tipoTransacaoCod").substring(0, 2));
  764. int submodalidade = Integer.valueOf(resultWithdrawal.getString("tipoTransacaoCod").substring(2, 4));
  765.  
  766. item = new JSONObject();
  767. item.put("valor", total);
  768. item.put("tipo", "TEF");
  769.  
  770. JSONObject dadosTef = new JSONObject();
  771. dadosTef.put("data", data);
  772. dadosTef.put("hora", hora);
  773. dadosTef.put("valor", total);
  774. dadosTef.put("nsu", resultWithdrawal.get("nsuSitef"));
  775. dadosTef.put("nsuHost", resultWithdrawal.get("nsuHost"));
  776.  
  777. if (resultWithdrawal.has("autorizacaoCredito")) {
  778. dadosTef.put("autorizacao", resultWithdrawal.get("autorizacaoCredito"));
  779. } else {
  780. dadosTef.put("autorizacao", "0");
  781. }
  782.  
  783. dadosTef.put("rede", Integer.valueOf(resultWithdrawal.getString("autorizadorId")));
  784. dadosTef.put("bandeira", Integer.valueOf(resultWithdrawal.getString("tipoCartaoId")));
  785. dadosTef.put("modalidade", modalidade);
  786. dadosTef.put("submodalidade", submodalidade);
  787.  
  788. item.put("dadosTEF", dadosTef);
  789.  
  790. formasRecebidas.add(item);
  791.  
  792. JSONObject jsonBody = new JSONObject();
  793. jsonBody.put("valorAutenticacao", total);
  794. jsonBody.put("formasRecebidas", formasRecebidas);
  795.  
  796. System.out.println("JSON postTransacoesWithdrawal sent >>" + jsonBody.toString() + "<<");
  797. LOGGER.info("JSON postTransacoesWithdrawal sent >>" + jsonBody.toString() + "<<");
  798.  
  799. ClientResponse response = webResource
  800. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  801. .type(MediaType.APPLICATION_JSON)
  802. .accept(MediaType.APPLICATION_JSON)
  803. .post(ClientResponse.class, jsonBody.toString());
  804.  
  805. if (response.getStatus() != 200) {
  806. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  807.  
  808. throw new RuntimeException("Failed : HTTP error code : "
  809. + response.getStatus());
  810. }
  811.  
  812. String output = response.getEntity(String.class);
  813.  
  814.  
  815. JSONObject jsonObj = new JSONObject(output);
  816. System.out.println("JSON postTransacoesWithdrawal return >>" + jsonObj.toString() + "<<");
  817. LOGGER.info("JSON postTransacoesWithdrawal return >>" + jsonObj.toString() + "<<");
  818.  
  819. return jsonObj.getBoolean("sucesso");
  820. } catch (Exception e) {
  821.  
  822. LOGGER.error("Erro na chamada do webservice de post de transação withdrawal", e);
  823. return false;
  824.  
  825. }
  826. }
  827.  
  828. private String getAuthorization() throws Exception {
  829. try {
  830. String username = Configuration.getInstance().getWebserviceAuthorizationUser();
  831. String password = Configuration.getInstance().getWebserviceAuthorizationPassword();
  832.  
  833. Client client = Client.create();
  834.  
  835. // URL u = new URL(Configuration.getInstance().getUrlTotemKoerichWebservice()); // this would check for the protocol
  836. // u.getContent();
  837.  
  838. disableSslVerification();
  839. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/autenticacao/autenticar");
  840. client.addFilter(new HTTPBasicAuthFilter(username, password));
  841.  
  842. ClientResponse response = webResource.accept("application/json").post(ClientResponse.class);
  843.  
  844. if (response.getStatus() != 200) {
  845. throw new RuntimeException("Failed : HTTP error code : "
  846. + response.getStatus());
  847. }
  848.  
  849. String output = response.getEntity(String.class);
  850.  
  851.  
  852. JSONObject jsonObj = new JSONObject(output);
  853. LOGGER.info("JSON getAuthorization return >>" + jsonObj.toString() + "<<");
  854. return (String) jsonObj.get("access_token");
  855.  
  856. } catch (Exception e) {
  857. LOGGER.error(e.getMessage(), e);
  858. // e.printStackTrace();
  859. throw e;
  860. // return null;
  861.  
  862. }
  863. }
  864.  
  865. private static void disableSslVerification() {
  866. try
  867. {
  868. // Create a trust manager that does not validate certificate chains
  869. TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
  870. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  871. return null;
  872. }
  873. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  874. }
  875. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  876. }
  877. }
  878. };
  879.  
  880. // Install the all-trusting trust manager
  881. SSLContext sc = SSLContext.getInstance("SSL");
  882. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  883. HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  884.  
  885. // Create all-trusting host name verifier
  886. HostnameVerifier allHostsValid = new HostnameVerifier() {
  887. public boolean verify(String hostname, SSLSession session) {
  888. return true;
  889. }
  890. };
  891.  
  892. // Install the all-trusting host verifier
  893. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  894. } catch (NoSuchAlgorithmException e) {
  895. e.printStackTrace();
  896. } catch (KeyManagementException e) {
  897. e.printStackTrace();
  898. }
  899. }
  900.  
  901. public JSONObject postTransacoesStartRecharge() {
  902. try{
  903. String filial = Configuration.getInstance().getStoreId();
  904.  
  905. Client client = Client.create();
  906.  
  907. disableSslVerification();
  908. String authorization = getAuthorization();
  909. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startRecharge/" + filial);
  910.  
  911. String bearerAuthorization = "Bearer " + authorization;
  912.  
  913. ClientResponse response = webResource
  914. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  915. .type(MediaType.APPLICATION_JSON)
  916. .accept(MediaType.APPLICATION_JSON)
  917. .post(ClientResponse.class);
  918.  
  919. if (response.getStatus() != 200) {
  920. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  921.  
  922. throw new RuntimeException("Failed : HTTP error code : "
  923. + response.getStatus());
  924. }
  925.  
  926. String output = response.getEntity(String.class);
  927.  
  928. JSONObject jsonObj = new JSONObject(output);
  929. LOGGER.info("JSON postTransacoesStartRecharge return >>" + jsonObj.toString() + "<<");
  930.  
  931. return jsonObj;
  932. } catch (Exception e) {
  933.  
  934. LOGGER.error("Erro na chamada do webservice de post de transação inicio de recarga", e);
  935. return null;
  936.  
  937. }
  938. }
  939.  
  940. public boolean postTransacoesRecharge(JSONObject resultRecharge, int codeMethodPaymentKoerich) {
  941. try{
  942. String filial = Configuration.getInstance().getStoreId();
  943.  
  944. Client client = Client.create();
  945.  
  946. disableSslVerification();
  947. String authorization = getAuthorization();
  948. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/recharge/" + filial);
  949. // client.addFilter(new HTTPBasicAuthFilter(username, password));
  950.  
  951. String bearerAuthorization = "Bearer " + authorization;
  952.  
  953. JSONObject jsonBody = new JSONObject();
  954.  
  955. String data = resultRecharge.getString("dataHora").substring(0, 10);
  956. String hora = resultRecharge.getString("dataHora").substring(11);
  957.  
  958. jsonBody.put("nsu", resultRecharge.get("nsuSitef"));
  959. jsonBody.put("nsuHost", resultRecharge.get("nsuHost"));
  960. jsonBody.put("valor", resultRecharge.getDouble("valor"));
  961. jsonBody.put("data", data);
  962. jsonBody.put("hora", hora);
  963. jsonBody.put("parcelas", 1);
  964. jsonBody.put("operadora", resultRecharge.get("operadoraNome"));
  965. jsonBody.put("celular", resultRecharge.get("telefone"));
  966. jsonBody.put("submodalidade", 0);
  967. // jsonBody.put("usuario", 999993);
  968. // jsonBody.put("filial", Integer.valueOf(filial));
  969. // jsonBody.put("cliente", "");
  970. // jsonBody.put("maquina", "");
  971. // jsonBody.put("intervensao", "");
  972. jsonBody.put("saque", false);
  973.  
  974. if (resultRecharge.has("pagamento")) {
  975.  
  976. JSONObject pagamento = resultRecharge.getJSONObject("pagamento");
  977.  
  978. ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
  979. JSONObject item;
  980.  
  981. switch(pagamento.getString("tipo")) {
  982. case "Dinheiro" :
  983.  
  984. item = new JSONObject();
  985. item.put("formaRecebimento", codeMethodPaymentKoerich);
  986. item.put("valor", resultRecharge.getDouble("valor"));
  987. item.put("tipoRecebimento", "VISTA");
  988.  
  989. formasRecebidas.add(item);
  990. break;
  991.  
  992. case "Cartão de Débito" :
  993. case "Cartão de Crédito" :
  994.  
  995. int modalidade = Integer.valueOf(pagamento.getString("tipoTransacaoCod").substring(0, 2));
  996. int submodalidade = Integer.valueOf(pagamento.getString("tipoTransacaoCod").substring(2, 4));
  997.  
  998. item = new JSONObject();
  999.  
  1000. item.put("formaRecebimento", codeMethodPaymentKoerich);
  1001.  
  1002. item.put("valor", resultRecharge.getDouble("valor"));
  1003. item.put("tipo", "TEF");
  1004.  
  1005. JSONObject dadosTef = new JSONObject();
  1006. dadosTef.put("data", data);
  1007. dadosTef.put("hora", hora);
  1008. dadosTef.put("valor", resultRecharge.getDouble("valor"));
  1009. dadosTef.put("nsu", pagamento.get("nsuSitef"));
  1010. dadosTef.put("nsuHost", pagamento.get("nsuHost"));
  1011.  
  1012. if (pagamento.has("autorizacaoCredito")) {
  1013. dadosTef.put("autorizacao", pagamento.get("autorizacaoCredito"));
  1014. } else {
  1015. dadosTef.put("autorizacao", "0");
  1016. }
  1017.  
  1018. dadosTef.put("rede", Integer.valueOf(pagamento.getString("autorizadorId")));
  1019. dadosTef.put("bandeira", Integer.valueOf(pagamento.getString("tipoCartaoId")));
  1020. dadosTef.put("modalidade", modalidade);
  1021. dadosTef.put("submodalidade", submodalidade);
  1022. // dadosTef.put("pagamento", 16);
  1023.  
  1024. item.put("dadosTEF", dadosTef);
  1025.  
  1026. formasRecebidas.add(item);
  1027. break;
  1028. }
  1029.  
  1030. jsonBody.put("formasRecebimentoTef", formasRecebidas);
  1031. }
  1032.  
  1033. LOGGER.info("JSON postTransacoesRecharge sent >>" + jsonBody.toString() + "<<");
  1034.  
  1035. ClientResponse response = webResource
  1036. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  1037. .type(MediaType.APPLICATION_JSON)
  1038. .accept(MediaType.APPLICATION_JSON)
  1039. .post(ClientResponse.class, jsonBody.toString());
  1040.  
  1041. if (response.getStatus() != 200) {
  1042. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  1043.  
  1044. throw new RuntimeException("Failed : HTTP error code : "
  1045. + response.getStatus());
  1046. }
  1047.  
  1048. String output = response.getEntity(String.class);
  1049.  
  1050.  
  1051. JSONObject jsonObj = new JSONObject(output);
  1052.  
  1053. LOGGER.info("JSON postTransacoesRecharge return >>" + jsonObj.toString() + "<<");
  1054.  
  1055. return jsonObj.getBoolean("sucesso");
  1056. } catch (Exception e) {
  1057.  
  1058. LOGGER.error("Erro na chamada do webservice de post de transação de recarga", e);
  1059. return false;
  1060.  
  1061. }
  1062. }
  1063.  
  1064. public JSONArray postTransacoesStartWithdrawal(int valueWithdrawal, String user) {
  1065. try{
  1066. String filial = Configuration.getInstance().getStoreId();
  1067.  
  1068. Client client = Client.create();
  1069.  
  1070. disableSslVerification();
  1071. String authorization = getAuthorization();
  1072. LOGGER.info("JSON postTransacoesStartWithdrawal sent >>" + Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startWithdrawal/" + filial + "/" + valueWithdrawal + "/" + user + "<<");
  1073. WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startWithdrawal/" + filial + "/" + valueWithdrawal + "/" + user);
  1074.  
  1075. String bearerAuthorization = "Bearer " + authorization;
  1076.  
  1077. ClientResponse response = webResource
  1078. .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
  1079. .type(MediaType.APPLICATION_JSON)
  1080. .accept(MediaType.APPLICATION_JSON)
  1081. .post(ClientResponse.class);
  1082.  
  1083. if (response.getStatus() != 200) {
  1084. LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
  1085.  
  1086. throw new RuntimeException("Failed : HTTP error code : "
  1087. + response.getStatus());
  1088. }
  1089.  
  1090. String output = response.getEntity(String.class);
  1091.  
  1092. JSONArray jsonArray = new JSONArray(output);
  1093. // String teste = jsonArray.getJSONArray(0).getString(0);
  1094. // String teste1 = jsonArray.getString(1);
  1095. // JSONObject jsonObj = new JSONObject(output);
  1096. LOGGER.info("JSON postTransacoesStartWithdrawal return >>" + jsonArray.toString() + "<<");
  1097.  
  1098. return jsonArray;
  1099. } catch (Exception e) {
  1100.  
  1101. LOGGER.error("Erro na chamada do webservice de post de transação inicio parcelamento de saque", e);
  1102. return null;
  1103.  
  1104. }
  1105. }
  1106.  
  1107. public String mountUrlImage(String urlImage) {
  1108.  
  1109. if (urlImage.toLowerCase().startsWith("http")) {
  1110. return urlImage;
  1111. }
  1112.  
  1113. return Configuration.getInstance().getUrlTotemKoerichWebservice() + urlImage;
  1114. }
  1115.  
  1116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement