Skip to content

Instantly share code, notes, and snippets.

@igreench
Last active July 7, 2017 16:00
Show Gist options
  • Select an option

  • Save igreench/a7dbcf85f7f66ef1bd9ed57698dc3552 to your computer and use it in GitHub Desktop.

Select an option

Save igreench/a7dbcf85f7f66ef1bd9ed57698dc3552 to your computer and use it in GitHub Desktop.
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class QiwiService {
private static Gson gson = new Gson();
class AuthorizeResponse {
private String code;
public String getCode() {
return code;
}
AuthorizeResponse() {
}
}
class AccessTokenResponse {
private String access_token;
private String expires_in;
private String phone;
private String refresh_token;
private String token_type;
public String getAccess_token() {
return access_token;
}
public String getExpires_in() {
return expires_in;
}
public String getPhone() {
return phone;
}
public String getRefresh_token() {
return refresh_token;
}
public String getToken_type() {
return token_type;
}
AccessTokenResponse() {
}
}
private static final String uriAuthorize = "https://w.qiwi.com/oauth/authorize";
private static final String uriAccessToken = "https://w.qiwi.com/oauth/access_token";
private static final String uriPay = "https://sinap.qiwi.com/api/terms/99/payments";
public static String authorize(String number) throws IOException {
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("client_id", "qw-fintech"));
params.add(new BasicNameValuePair("client_secret", "Xghj!bkjv64"));
params.add(new BasicNameValuePair("client-software", "qw-%20fintech-0.0.1"));
params.add(new BasicNameValuePair("response_type", "code"));
params.add(new BasicNameValuePair("username", number));
String response = HttpService.invoke(uriAuthorize, params);
AuthorizeResponse authorizeResponse = gson.fromJson(response, AuthorizeResponse.class);
return authorizeResponse.getCode();
}
public static String getAccessToken(String token, String code) throws IOException {
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("client_id", "qw-fintech"));
params.add(new BasicNameValuePair("client_secret", "Xghj!bkjv64"));
params.add(new BasicNameValuePair("client-software", "qw-fintech-0.0.1"));
params.add(new BasicNameValuePair("grant_type", "urn:qiwi:oauth:grant-type:vcode"));
params.add(new BasicNameValuePair("code", token));
params.add(new BasicNameValuePair("vcode", code));
String response = HttpService.invoke(uriAccessToken, params);
AccessTokenResponse accessTokenResponse = gson.fromJson(response, AccessTokenResponse.class);
return accessTokenResponse.getAccess_token();
}
public static String pay(String accessToken, String numberSender, String numberReceiver, Long sum) throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/vnd.qiwi.v2+json");
headers.put("Accept-Encoding", "gzip, deflate, compress");
headers.put("Content-Type", "application/json");
headers.put("Authorization", getAuthorizationHash(numberSender, accessToken));
headers.put("User-Agent", "HTTPie/0.3.0");
String body = getJson(numberReceiver, sum, String.valueOf(System.currentTimeMillis()));
return HttpService.invoke(uriPay, headers, body);
}
private static String getJson(String account, Long sum, String id) {
JsonObject jsonObject = new JsonObject();
JsonObject fieldsObject = new JsonObject();
fieldsObject.addProperty("account", account);
fieldsObject.addProperty("prvId", "99");
JsonObject paymentMethodObject = new JsonObject();
paymentMethodObject.addProperty("type", "Account");
paymentMethodObject.addProperty("accountId", "643");
JsonObject sumObject = new JsonObject();
sumObject.addProperty("amount", String.valueOf(sum));
sumObject.addProperty("currency", "643");
jsonObject.add("fields", fieldsObject);
jsonObject.addProperty("id", id);
jsonObject.add("paymentMethod", paymentMethodObject);
jsonObject.add("sum", sumObject);
return jsonObject.toString();
}
private static String getAuthorizationHash(String numberSender, String accessToken) {
String based = numberSender + ":" + accessToken;
byte[] encodedBytes = Base64.encodeBase64(based.getBytes());
return "Token " + new String(encodedBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment