Czy byłoby możliwe dodanie API dla płatności, np. SMS? Chodzi mi o to, że mam np. swoją stronę na VPS'ie i chciałbym dodać tam automatyczną płatność SMS. Obecnie korzystam z ręcznego rozwiązania, ale jest ono bardzo unsafe.
private static final HashMap COOKIES = new HashMap<>();
private static final Pattern KOD = Pattern.compile("[A-Z0-9]{8}");
private static final String USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36";
//cloudflare blokuje jave
public static class PaymentsResponse {
public int error;
public double amount = 0;
public String wallet_formatted;
}
private static String getRequestCookieString(Map req) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Map.Entry cookie : req.entrySet()) {
if (!first)
sb.append("; ");
else
first = false;
sb.append(cookie.getKey()).append('=').append(cookie.getValue());
}
return sb.toString();
}
private boolean zaloguj() throws ServletException, IOException {
Response res = Jsoup.connect("https://lvlup.pro/auth/login").userAgent(USER_AGENT).execute();
COOKIES.putAll(res.cookies());
FormElement el = (FormElement) res.parse().getElementsByTag("form").get(0);
for (Element e : el.elements()) {
if (!e.tag().isFormSubmittable())
continue;
if (e.hasAttr("disabled"))
continue;
if (e.attr("name").equals("username"))
e.val("jakis tam nasz email");
else if (e.attr("name").equals("password"))
e.val("jakies tam nasze haslo");
}
res = el.submit().userAgent(USER_AGENT).cookies(COOKIES).execute();
COOKIES.putAll(res.cookies());
if (res.body().contains("Zaloguj si") || !res.body().contains("Wyloguj")) {
return false;
}
return true;
}
private double sprawdzKod(String kod, int proby) throws IOException, ServletException {
if (!KOD.matcher(kod).matches())
return -2;
HttpURLConnection conn = (HttpURLConnection) new URL("https://lvlup.pro/panel/profile/payments")
.openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("x-requested-with", "XMLHttpRequest");
conn.setRequestMethod("POST");
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);
conn.addRequestProperty("Cookie", getRequestCookieString(COOKIES));
conn.getOutputStream().write(("{\"code\":\"" + kod + "\"}").getBytes(StandardCharsets.UTF_8));
char[] b = new char[8192];
int n;
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
while ((n = isr.read(b)) != -1)
sb.append(b, 0, n);
if (sb.length() == 0) {
if (proby == 3) {
throw new ServletException(
"Nie mozna sie zalogowac. Zglos blad administratorowi i zalacz ta wiadomosc: "
+ conn.getResponseCode() + " " + conn.getResponseMessage() + " " + kod + " "
+ kod.hashCode());
}
zaloguj();
return sprawdzKod(kod, proby + 1);
}
PaymentsResponse pr = new Gson().fromJson(sb.toString(), PaymentsResponse.class);
if (pr == null) {
if (proby == 3) {
throw new ServletException(
"Nie mozna sie zalogowac. Zglos blad administratorowi i zalacz ta wiadomosc: " + sb.toString());
}
zaloguj();
return sprawdzKod(kod, proby + 1);
}
return pr.amount;
}
private HashMap kody = new HashMap<>();