BrightSide Workbench Full Report + Source Code
RedsysPOS.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.cart;
20 
21 import java.io.UnsupportedEncodingException;
22 import java.security.InvalidAlgorithmParameterException;
23 import java.security.InvalidKeyException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.HashMap;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.crypto.BadPaddingException;
29 import javax.crypto.IllegalBlockSizeException;
30 import javax.crypto.NoSuchPaddingException;
31 import org.turro.string.Strings;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.util.DecimalFormats;
35 import org.turro.marker.ElephantMarker;
36 import sis.redsys.api.ApiMacSha256;
37 
42 public class RedsysPOS {
43 
44  private final HashMap<String, String> properties = new HashMap<>();
45 
46  public void addParameters(ElephantMarker marker, double amount, String order) {
47  ApiMacSha256 apiMacSha256 = new ApiMacSha256();
48 
49  apiMacSha256.setParameter("DS_MERCHANT_TITULAR", Strings.truncateAndWarn(ElephantContext.getSiteName(), 60));
50  apiMacSha256.setParameter("DS_MERCHANT_AMOUNT", DecimalFormats.format(amount * 100, "0"));
51  apiMacSha256.setParameter("DS_MERCHANT_ORDER", order);
52  apiMacSha256.setParameter("DS_MERCHANT_MERCHANTNAME", ElephantContext.getSiteName());
53  apiMacSha256.setParameter("DS_MERCHANT_MERCHANTCODE", ShopContext.getInstance().getMerchantCode());
54  apiMacSha256.setParameter("DS_MERCHANT_CURRENCY", ShopContext.getInstance().getMerchantCurrency());
55  apiMacSha256.setParameter("DS_MERCHANT_TRANSACTIONTYPE", "0");
56  apiMacSha256.setParameter("DS_MERCHANT_TERMINAL", ShopContext.getInstance().getMerchantTerminal());
57  apiMacSha256.setParameter("DS_MERCHANT_MERCHANTURL", ElephantContext.getServerUrl("http") + ShopContext.getInstance().getNotificationPath());
58  apiMacSha256.setParameter("DS_MERCHANT_URLOK", ElephantContext.getServerUrl("http") + ShopContext.getInstance().getOKPath());
59  apiMacSha256.setParameter("DS_MERCHANT_URLKO", ElephantContext.getServerUrl("http") + ShopContext.getInstance().getKOPath());
60  //apiMacSha256.setParameter("DS_MERCHANT_MERCHANTDATA", request.getParameter("data"));
61  apiMacSha256.setParameter("DS_MERCHANT_CONSUMERLANGUAGE", ShopContext.getInstance().getMerchantLanguage());
62 
63  try {
65  marker.put("pos_url", "https://sis-t.redsys.es:25443/sis/realizarPago");
66  } else {
67  marker.put("pos_url", "https://sis.redsys.es/sis/realizarPago");
68  }
69  marker.put("pos_version", ShopContext.getInstance().getMerchantKeyType());
70  marker.put("pos_params", apiMacSha256.createMerchantParameters());
71  marker.put("pos_signature", apiMacSha256.createMerchantSignature(ShopContext.getInstance().getMerchantKey()));
72  } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | IllegalStateException |
73  NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
74  Logger.getLogger(RedsysPOS.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
75  }
76 
77  }
78 
79  /*
80  Ds_SignatureVersion=HMAC_SHA256_V1&Ds_MerchantParameters=eyJEc19EYXRlIjoiMTFcLzA2XC8yMDE2IiwiRHNfSG91ciI6IjA5OjM3IiwiRHNfU2VjdXJlUGF5bWVudCI6IjEiLCJEc19DYXJkX0NvdW50cnkiOiI3MjQiLCJEc19BbW91bnQiOiIyMDAwMCIsIkRzX0N1cnJlbmN5IjoiOTc4IiwiRHNfT3JkZXIiOiIxNjE0MDI3ODMwMjYiLCJEc19NZXJjaGFudENvZGUiOiIzMzU1MDUxNDUiLCJEc19UZXJtaW5hbCI6IjAwMSIsIkRzX1Jlc3BvbnNlIjoiMDAwMCIsIkRzX01lcmNoYW50RGF0YSI6IiIsIkRzX1RyYW5zYWN0aW9uVHlwZSI6IjAiLCJEc19Db25zdW1lckxhbmd1YWdlIjoiMyIsIkRzX0F1dGhvcmlzYXRpb25Db2RlIjoiMzEyMDI5In0=&Ds_Signature=5spWgE9uK6LndK_7RbSeRDW-J91aFjpTJJJmipNEOT4=
81  */
82  public boolean isOnlineNotification(IConstructor constructor) {
83  String version = constructor.getParameter("Ds_SignatureVersion", true),
84  params = constructor.getParameter("Ds_MerchantParameters", true),
85  signature = constructor.getParameter("Ds_Signature", true);
86 
87  if(Strings.isBlank(signature)) {
88  return false;
89  }
90 
91  properties.put("Ds_SignatureVersion", version);
92  properties.put("Ds_MerchantParameters", params);
93  properties.put("Ds_Signature", signature);
94 
95  ApiMacSha256 apiMacSha256 = new ApiMacSha256();
96 
97  try {
98 
99  apiMacSha256.decodeMerchantParameters(params);
100 
101  String calculated = apiMacSha256.createMerchantSignatureNotif(ShopContext.getInstance().getMerchantKey(), params);
102 
103  return calculated.equals(signature);
104 
105  } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | IllegalStateException |
106  NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
107  Logger.getLogger(RedsysPOS.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
108  }
109 
110  return false;
111  }
112 
113  public Cart isAccepted(IConstructor constructor) {
114  ApiMacSha256 apiMacSha256 = new ApiMacSha256();
115 
116  String version = constructor.getParameter("Ds_SignatureVersion", true),
117  params = constructor.getParameter("Ds_MerchantParameters", true),
118  signature = constructor.getParameter("Ds_Signature", true);
119 
120  try {
121 
122  apiMacSha256.decodeMerchantParameters(params);
123 
124  String response = apiMacSha256.getParameter("Ds_Response"),
125  order = apiMacSha256.getParameter("Ds_Order");
126 
127  properties.put("Ds_Order", order);
128 
129  if(!Strings.isBlank(response) && Long.valueOf(response) < 100) {
130  return Cart.deserializeForOrder(order);
131  }
132 
133  } catch (UnsupportedEncodingException ex) {
134  Logger.getLogger(RedsysPOS.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
135  }
136 
137  return null;
138  }
139 
140  public void logProperties() {
141  for(String k : properties.keySet()) {
142  Logger.getLogger(RedsysPOS.class.getName()).log(Level.INFO, k + ":" + properties.get(k));
143  }
144  }
145 
146 }
147 
148 /*
149 <%@page import="java.security.*"%>
150 <%@page import="sis.redsys.api.ApiMacSha256"%>
151 
152 <HTML>
153 <BODY BGCOLOR="WHITE">
154 <form name=compra action="https://sis.sermepa.es/sis/realizarPago" method="POST" style="margin-left:20px">
155  <input type="text" name="Ds_SignatureVersion" value="HMAC_SHA256_V1"/>
156  <input type="text" name="Ds_MerchantParameters" value="<%=params%>"/>
157  <input type="text" name="Ds_Signature" value="<%=signature%>"/>
158 </form>
159 </BODY>
160  <SCRIPT LANGUAGE="JavaScript" >
161  function calc() {
162  document.compra.submit();
163  }
164  calc();
165 </SCRIPT>
166 </HTML>
167 */
static String getServerUrl(String scheme)
static String format(Number value, String pattern)
static Cart deserializeForOrder(String order)
Definition: Cart.java:255
boolean isOnlineNotification(IConstructor constructor)
Definition: RedsysPOS.java:82
void addParameters(ElephantMarker marker, double amount, String order)
Definition: RedsysPOS.java:46
Cart isAccepted(IConstructor constructor)
Definition: RedsysPOS.java:113
Object put(Object key, Object value)