156 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.erp.preferences;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
17 |
import org.openconcerto.sql.Configuration;
|
|
|
18 |
import org.openconcerto.sql.preferences.SQLPreferences;
|
|
|
19 |
import org.openconcerto.ui.DefaultGridBagConstraints;
|
|
|
20 |
import org.openconcerto.ui.JLabelBold;
|
|
|
21 |
import org.openconcerto.ui.preferences.JavaPrefPreferencePanel;
|
|
|
22 |
import org.openconcerto.ui.preferences.PrefView;
|
|
|
23 |
import org.openconcerto.utils.Base64;
|
|
|
24 |
import org.openconcerto.utils.JImage;
|
|
|
25 |
import org.openconcerto.utils.PrefType;
|
|
|
26 |
|
|
|
27 |
import java.awt.Desktop;
|
|
|
28 |
import java.awt.GridBagConstraints;
|
|
|
29 |
import java.awt.GridBagLayout;
|
|
|
30 |
import java.awt.event.ActionEvent;
|
|
|
31 |
import java.awt.event.ActionListener;
|
|
|
32 |
import java.io.IOException;
|
|
|
33 |
import java.io.InputStream;
|
|
|
34 |
import java.io.InputStreamReader;
|
|
|
35 |
import java.io.OutputStream;
|
|
|
36 |
import java.net.HttpURLConnection;
|
|
|
37 |
import java.net.URI;
|
|
|
38 |
import java.net.URISyntaxException;
|
|
|
39 |
import java.net.URL;
|
|
|
40 |
import java.net.URLConnection;
|
|
|
41 |
import java.net.URLEncoder;
|
|
|
42 |
import java.nio.charset.StandardCharsets;
|
|
|
43 |
import java.util.HashMap;
|
|
|
44 |
import java.util.Map;
|
|
|
45 |
import java.util.StringJoiner;
|
|
|
46 |
|
|
|
47 |
import javax.swing.JButton;
|
|
|
48 |
import javax.swing.JCheckBox;
|
|
|
49 |
import javax.swing.JComponent;
|
|
|
50 |
import javax.swing.JLabel;
|
|
|
51 |
import javax.swing.JOptionPane;
|
|
|
52 |
import javax.swing.JPanel;
|
|
|
53 |
import javax.swing.SwingConstants;
|
|
|
54 |
|
|
|
55 |
public class PayPalPreferencePanel extends JavaPrefPreferencePanel {
|
|
|
56 |
|
|
|
57 |
public static final String PAYPAL_INVOICE = "paypal.invoice";
|
|
|
58 |
public static final String PAYPAL_CLIENTID = "paypal.clientid";
|
|
|
59 |
public static final String PAYPAL_SECRET = "paypal.secret";
|
|
|
60 |
public static final String PAYPAL_INVOICE_X = "paypal.invoice.x";
|
|
|
61 |
public static final String PAYPAL_INVOICE_Y = "paypal.invoice.y";
|
|
|
62 |
|
|
|
63 |
// Usage :
|
|
|
64 |
// SQLPreferences prefs = SQLPreferences.getMemCached(comptaConf.getRootSociete());
|
|
|
65 |
// prefs.getBoolean(PayPalPreferencePanel.PAYPAL_INVOICE, false));
|
|
|
66 |
|
|
|
67 |
public PayPalPreferencePanel() {
|
|
|
68 |
super("Paypal", null);
|
|
|
69 |
|
|
|
70 |
setPrefs(new SQLPreferences(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete()));
|
|
|
71 |
this.removeAll();
|
|
|
72 |
|
|
|
73 |
this.setLayout(new GridBagLayout());
|
|
|
74 |
GridBagConstraints c = new DefaultGridBagConstraints();
|
|
|
75 |
// Logo
|
|
|
76 |
c.gridwidth = 3;
|
|
|
77 |
c.weightx = 1;
|
|
|
78 |
c.fill = GridBagConstraints.NONE;
|
|
|
79 |
c.anchor = GridBagConstraints.WEST;
|
|
|
80 |
final JImage img = new JImage(PayPalPreferencePanel.class.getResource("logo_paypal_106x29.png"));
|
|
|
81 |
try {
|
|
|
82 |
img.setHyperLink(new URI("https://www.paypal.fr"));
|
|
|
83 |
} catch (URISyntaxException e2) {
|
|
|
84 |
// No hyperlink
|
|
|
85 |
}
|
|
|
86 |
this.add(img, c);
|
|
|
87 |
c.gridy++;
|
|
|
88 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
89 |
c.gridwidth = 3;
|
|
|
90 |
this.add(new JLabel("Intégration du lien de paiement PayPal dans les factures PDF"), c);
|
|
|
91 |
c.gridy++;
|
|
|
92 |
this.add(new JLabel("Cette fonctionnalité nécessite la création d'un compte professionnel (gratuit)."), c);
|
|
|
93 |
// Account creation
|
|
|
94 |
final JButton b = new JButton("Créer votre compte maintenant");
|
|
|
95 |
b.addActionListener(new ActionListener() {
|
|
|
96 |
|
|
|
97 |
@Override
|
|
|
98 |
public void actionPerformed(ActionEvent e) {
|
|
|
99 |
if (Desktop.isDesktopSupported()) {
|
|
|
100 |
final String url = "https://www.paypal.com/fr/merchantsignup/create";
|
|
|
101 |
try {
|
|
|
102 |
Desktop.getDesktop().browse(new URI(url));
|
|
|
103 |
} catch (IOException | URISyntaxException e1) {
|
|
|
104 |
JOptionPane.showMessageDialog(PayPalPreferencePanel.this, url, "Erreur d'ouverture du navigateur", JOptionPane.WARNING_MESSAGE);
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
}
|
|
|
109 |
});
|
|
|
110 |
c.gridy++;
|
|
|
111 |
c.fill = GridBagConstraints.NONE;
|
|
|
112 |
this.add(b, c);
|
|
|
113 |
|
|
|
114 |
c.gridy++;
|
|
|
115 |
this.add(new JLabel(" "), c);
|
|
|
116 |
c.gridy++;
|
|
|
117 |
this.add(new JLabelBold("Paramètres de l'API"), c);
|
|
|
118 |
c.gridy++;
|
|
|
119 |
this.add(new JLabel("à créer depuis https://developer.paypal.com"), c);
|
|
|
120 |
|
|
|
121 |
// Client ID
|
|
|
122 |
c.gridx = 0;
|
|
|
123 |
c.gridy++;
|
|
|
124 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
125 |
c.gridwidth = 1;
|
|
|
126 |
c.weightx = 0;
|
|
|
127 |
this.add(new JLabel("Client ID", SwingConstants.RIGHT), c);
|
|
|
128 |
c.gridwidth = 2;
|
|
|
129 |
c.gridx++;
|
|
|
130 |
c.weightx = 1;
|
|
|
131 |
PrefView<String> clientIdPrefView = new PrefView<>(PrefType.STRING_TYPE, "Client ID", PAYPAL_CLIENTID);
|
|
|
132 |
this.addView(clientIdPrefView);
|
|
|
133 |
this.add(clientIdPrefView.getVW().getComp(), c);
|
|
|
134 |
|
|
|
135 |
// Secret
|
|
|
136 |
c.gridx = 0;
|
|
|
137 |
c.gridy++;
|
|
|
138 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
139 |
c.gridwidth = 1;
|
|
|
140 |
c.weightx = 0;
|
|
|
141 |
this.add(new JLabel("Secret", SwingConstants.RIGHT), c);
|
|
|
142 |
c.gridwidth = 2;
|
|
|
143 |
c.gridx++;
|
|
|
144 |
c.weightx = 1;
|
|
|
145 |
PrefView<String> secretPrefView = new PrefView<>(PrefType.STRING_TYPE, "Secret", PAYPAL_SECRET);
|
|
|
146 |
this.addView(secretPrefView);
|
|
|
147 |
this.add(secretPrefView.getVW().getComp(), c);
|
|
|
148 |
|
|
|
149 |
// Enable
|
|
|
150 |
c.gridx = 0;
|
|
|
151 |
c.gridy++;
|
|
|
152 |
c.fill = GridBagConstraints.NONE;
|
|
|
153 |
c.gridwidth = 3;
|
|
|
154 |
c.weightx = 0;
|
|
|
155 |
c.anchor = GridBagConstraints.EAST;
|
|
|
156 |
final JButton bVerify = new JButton("Vérifier les paramètres");
|
|
|
157 |
this.add(bVerify, c);
|
|
|
158 |
// Enable
|
|
|
159 |
c.gridx = 1;
|
|
|
160 |
c.gridy++;
|
|
|
161 |
c.fill = GridBagConstraints.NONE;
|
|
|
162 |
c.gridwidth = 1;
|
|
|
163 |
c.weightx = 0;
|
|
|
164 |
c.anchor = GridBagConstraints.EAST;
|
|
|
165 |
PrefView<Boolean> enabledPrefView = new PrefView<>(PrefType.BOOLEAN_TYPE, "intégration", PAYPAL_INVOICE);
|
|
|
166 |
this.addView(enabledPrefView);
|
|
|
167 |
final JComponent comp = enabledPrefView.getVW().getComp();
|
|
|
168 |
((JCheckBox) comp).setText("intégrer le lien de paiement dans les factures PDF");
|
|
|
169 |
this.add(comp, c);
|
|
|
170 |
|
|
|
171 |
// Position
|
|
|
172 |
c.gridy++;
|
|
|
173 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
174 |
this.add(new JLabel(" "), c);
|
|
|
175 |
c.gridy++;
|
|
|
176 |
c.gridx = 0;
|
|
|
177 |
c.gridwidth = 3;
|
|
|
178 |
this.add(new JLabelBold("Position du logo sur le PDF"), c);
|
|
|
179 |
|
|
|
180 |
c.gridx = 0;
|
|
|
181 |
c.gridy++;
|
|
|
182 |
c.gridwidth = 1;
|
|
|
183 |
c.weightx = 0;
|
|
|
184 |
this.add(new JLabel("X (0 - 440)", SwingConstants.RIGHT), c);
|
|
|
185 |
c.gridx++;
|
|
|
186 |
PrefView<Integer> xPrefView = new PrefView<>(PrefType.INT_TYPE, "X", PAYPAL_INVOICE_X);
|
|
|
187 |
xPrefView.setDefaultValue(10);
|
|
|
188 |
this.addView(xPrefView);
|
|
|
189 |
this.add(xPrefView.getVW().getComp(), c);
|
|
|
190 |
|
|
|
191 |
c.gridx = 0;
|
|
|
192 |
c.gridy++;
|
|
|
193 |
c.gridwidth = 1;
|
|
|
194 |
c.weightx = 0;
|
|
|
195 |
this.add(new JLabel("Y (0 - 780)", SwingConstants.RIGHT), c);
|
|
|
196 |
c.gridx++;
|
|
|
197 |
PrefView<Integer> yPrefView = new PrefView<>(PrefType.INT_TYPE, "Y", PAYPAL_INVOICE_Y);
|
|
|
198 |
yPrefView.setDefaultValue(10);
|
|
|
199 |
this.addView(yPrefView);
|
|
|
200 |
this.add(yPrefView.getVW().getComp(), c);
|
|
|
201 |
c.gridy++;
|
|
|
202 |
c.weighty = 1;
|
|
|
203 |
JPanel spacer = new JPanel();
|
|
|
204 |
this.add(spacer, c);
|
|
|
205 |
|
|
|
206 |
bVerify.addActionListener(new ActionListener() {
|
|
|
207 |
|
|
|
208 |
@Override
|
|
|
209 |
public void actionPerformed(ActionEvent e) {
|
|
|
210 |
boolean ok = false;
|
|
|
211 |
try {
|
|
|
212 |
ok = sendAuth(clientIdPrefView.getVW().getValue(), secretPrefView.getVW().getValue());
|
|
|
213 |
} catch (IOException e1) {
|
|
|
214 |
e1.printStackTrace();
|
|
|
215 |
}
|
|
|
216 |
if (ok) {
|
|
|
217 |
JOptionPane.showMessageDialog(PayPalPreferencePanel.this, "Paramètres OK", "API Paypal", JOptionPane.INFORMATION_MESSAGE);
|
|
|
218 |
} else {
|
|
|
219 |
JOptionPane.showMessageDialog(PayPalPreferencePanel.this, "Paramètres incorrects", "API Paypal", JOptionPane.ERROR_MESSAGE);
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
});
|
|
|
223 |
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
@Override
|
|
|
227 |
protected void addViews() {
|
|
|
228 |
// Nothing to do here, panel is built in the constructor
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
public static void main(String[] args) throws IOException {
|
|
|
232 |
String id = "Abb2mKqK0TSU6Jgf71CJjFx0u5x6_NmEzHduuvsCXjYCfYxRg9GZ7B6ptx3pijriuq9Apx9Jp-VTFveF";
|
|
|
233 |
String s = "ENR1EyKreAlPbLRI0ofm2NbW6nyk8W5cJUZ7mvaBMgoQ6gB6VBivZpQW6B96toCtLZ3ClCUVutN-Gal2";
|
|
|
234 |
sendAuth(id, s + "");
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
static boolean sendAuth(String clientId, String secret) throws IOException {
|
|
|
238 |
final URL url = new URL("https://api.paypal.com/v1/oauth2/token");
|
|
|
239 |
final URLConnection con = url.openConnection();
|
|
|
240 |
final HttpURLConnection http = (HttpURLConnection) con;
|
|
|
241 |
http.setRequestMethod("POST");
|
|
|
242 |
http.setDoOutput(true);
|
|
|
243 |
http.setDefaultUseCaches(false);
|
|
|
244 |
String authString = clientId + ":" + secret;
|
|
|
245 |
String authStringEnc = Base64.encodeBytes(authString.getBytes(), Base64.DONT_BREAK_LINES);
|
|
|
246 |
con.setRequestProperty("Authorization", "Basic " + authStringEnc);
|
|
|
247 |
|
|
|
248 |
// x-www-form-urlencoded
|
|
|
249 |
final Map<String, String> arguments = new HashMap<>();
|
|
|
250 |
arguments.put("grant_type", "client_credentials");
|
|
|
251 |
final StringJoiner sj = new StringJoiner("&");
|
|
|
252 |
for (Map.Entry<String, String> entry : arguments.entrySet()) {
|
|
|
253 |
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"));
|
|
|
254 |
}
|
|
|
255 |
byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8);
|
|
|
256 |
int length = out.length;
|
|
|
257 |
http.setFixedLengthStreamingMode(length);
|
|
|
258 |
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
259 |
http.connect();
|
|
|
260 |
try (OutputStream os = http.getOutputStream()) {
|
|
|
261 |
os.write(out);
|
|
|
262 |
}
|
|
|
263 |
if (http.getResponseCode() == 401) {
|
|
|
264 |
return false;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
InputStream is = http.getInputStream();
|
|
|
268 |
InputStreamReader isr = new InputStreamReader(is);
|
|
|
269 |
|
|
|
270 |
int numCharsRead;
|
|
|
271 |
char[] charArray = new char[1024];
|
|
|
272 |
StringBuilder sb = new StringBuilder();
|
|
|
273 |
while ((numCharsRead = isr.read(charArray)) > 0) {
|
|
|
274 |
sb.append(charArray, 0, numCharsRead);
|
|
|
275 |
}
|
|
|
276 |
String result = sb.toString();
|
|
|
277 |
return result.contains("access_token");
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
}
|