Created
July 18, 2016 09:10
-
-
Save burnix/5cd920907d874b86b5274f7940e172f8 to your computer and use it in GitHub Desktop.
liq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gns_it.liqpaytest; | |
import android.net.Uri; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Base64; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.Charset; | |
import java.security.MessageDigest; | |
import java.util.HashMap; | |
import ua.privatbank.paylibliqpay.ErrorCode; | |
import ua.privatbank.paylibliqpay.LiqPay; | |
import ua.privatbank.paylibliqpay.LiqPayCallBack; | |
public class MainActivity extends AppCompatActivity { | |
public static final String PUBILC_KEY = "i10427060373"; | |
public static final String PRIVATE_KEY = "voPGajIUBw5fSw8Fe91A2tGdVuajhuieR6QfCoE3"; | |
public static final String PHONE_TEST = "+38...."; | |
public static final String AMOUNT_TEST = "0.25"; | |
public static final String CURRENCY_TEST = "UAH"; | |
public static final String ORDERID_TEST = "11"; | |
public static final String LANGUAGE_TEST = "en"; | |
public static final String SERVER_URL = "http://itea-api-test.dev.gns-it.com/payment/status"; | |
// public static final String SERVER_URL = "http://e9d088d2.ngrok.io/payment/status"; | |
// public static final String SERVER_URL = "http://e9d088d2.ngrok.io/api/v1/payment/status"; | |
Button bCheckout; | |
Button bPay; | |
private LiqPayCallBack callBack; | |
private String cardToken; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
bCheckout = (Button) findViewById(R.id.bCheckout); | |
bCheckout.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
checkout(); | |
} | |
}); | |
bPay = (Button) findViewById(R.id.bPay); | |
bPay.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
paytoken(); | |
} | |
}); | |
callBack = new LiqPayCallBack() { | |
@Override | |
public void onResponseSuccess(final String resp) { | |
JSONObject object = null; | |
try { | |
object = new JSONObject(resp); | |
if("success".equals(object.optString("status"))){ | |
// успех | |
cardToken = object.optString("card_token"); | |
}else { | |
// ошибка | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onResponceError(final ErrorCode errorCode) { | |
int r = 0; | |
} | |
}; | |
} | |
private void checkout() { | |
JSONObject jo = new JSONObject(); | |
try { | |
jo.put("version", 3); | |
jo.put("public_key", PUBILC_KEY); | |
jo.put("action", "pay"); | |
jo.put("amount", AMOUNT_TEST); | |
jo.put("currency", CURRENCY_TEST); | |
jo.put("description", "test_checkout"); | |
jo.put("order_id", ORDERID_TEST); | |
jo.put("language", LANGUAGE_TEST); | |
jo.put("sandbox", 1); | |
jo.put("server_url", SERVER_URL); | |
jo.put("recurringbytoken", "1"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
final String base64EncodedData = Base64.encodeToString(jo.toString().getBytes(), 2); | |
final String signature = base64Encode(sha1(PRIVATE_KEY + base64EncodedData + PRIVATE_KEY)); | |
// base64EncodedData - результат функции base64_encode( $json_string ) | |
// signature - результат функции base64_encode( sha1( $private_key . $data . $private_key ) ) | |
boolean useSDK = true; | |
if(useSDK) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
LiqPay.checkout(getApplicationContext(), base64EncodedData, signature, callBack); | |
} | |
}).start(); | |
} | |
else{ | |
Log.d("LPTEST", "base64Data:\t" + base64EncodedData); | |
Log.d("LPTEST", "signature:\t\t" + signature); | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("data", base64EncodedData) | |
.appendQueryParameter("signature", signature); | |
String query = builder.build().getEncodedQuery(); | |
new PostAsyncTask("https://www.liqpay.com/api/3/checkout", query) { | |
@Override | |
public void onResult(String result) { | |
Charset cset = Charset.forName("UTF-8"); | |
ByteBuffer buf = cset.encode(result); | |
byte[] b = buf.array(); | |
String str = new String(b); | |
Log.d("LPTEST", "result:\t" + str); | |
} | |
}; | |
} | |
} | |
private void paytoken() { | |
if(cardToken==null) { | |
Toast.makeText(getApplicationContext(), "NO CARD TOKEN!", Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
JSONObject jo = new JSONObject(); | |
try { | |
jo.put("version", 3); | |
// jo.put("public_key", PUBILC_KEY); | |
jo.put("phone", PHONE_TEST); | |
jo.put("amount", AMOUNT_TEST); | |
jo.put("currency", CURRENCY_TEST); | |
jo.put("description", "test_paytoken"); | |
jo.put("order_id", ORDERID_TEST); | |
jo.put("card_token", cardToken); | |
jo.put("sandbox", 1); | |
jo.put("server_url", SERVER_URL); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
final String base64EncodedData = Base64.encodeToString(jo.toString().getBytes(), 2); | |
final String signature = base64Encode(sha1(PRIVATE_KEY + base64EncodedData + PRIVATE_KEY)); | |
// base64EncodedData - результат функции base64_encode( $json_string ) | |
// signature - результат функции base64_encode( sha1( $private_key . $data . $private_key ) ) | |
boolean useSDK = true; | |
if(useSDK) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
LiqPay.api(getApplicationContext(), "pay", base64EncodedData, signature, callBack); | |
} | |
}).start(); | |
} | |
else{ | |
Log.d("LPTEST", "base64Data:\t" + base64EncodedData); | |
Log.d("LPTEST", "signature:\t\t" + signature); | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("data", base64EncodedData) | |
.appendQueryParameter("signature", signature); | |
String query = builder.build().getEncodedQuery(); | |
new PostAsyncTask("https://www.liqpay.com/api/request/pay", query) { | |
@Override | |
public void onResult(String result) { | |
Charset cset = Charset.forName("UTF-8"); | |
ByteBuffer buf = cset.encode(result); | |
byte[] b = buf.array(); | |
String str = new String(b); | |
Log.d("LPTEST", "result:\t" + str); | |
} | |
}; | |
} | |
} | |
protected static String base64Encode(byte[] bytes) { | |
return Base64.encodeToString(bytes, 2); | |
} | |
protected static byte[] sha1(String param) { | |
try { | |
MessageDigest e = MessageDigest.getInstance("SHA-1"); | |
e.reset(); | |
e.update(param.getBytes("UTF-8")); | |
return e.digest(); | |
} catch (Exception var2) { | |
throw new RuntimeException("Can\'t calc SHA-1 hash", var2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment