银联支付平台实在是难找,百度了好久才找到他们的网站 https://open.unionpay.com/ajweb/index
下载银联支付的sdk和集成文档。
一:在app目录下的libs文件夹中添加 UPPayAssistEx.jar 和 UPPayPluginExPro.jar这两个文件 并分别右键选择 add as library。
二:在app --> src --> main目录下 创建 assets文件夹 并把 data.bin文件拷贝到这个文件夹里
三:在app --> src --> main目录下创建 jniLibs目录 并把所需要的 so文件拷贝到这个目录中 目录结构如下:
jniLibs --> armeabi --> libentryexpro.so 这种结构
四:下面开始代码的添加
/***************************************************************** * mMode参数解释: "00" - 启动银联正式环境 "01" - 连接银联测试环境 *****************************************************************/ private final String mMode = "01"; private static final String TN_URL_01 = "http://101.231.204.84:8091/sim/getacptn"; public static final int PLUGIN_VALID = 0; public static final int PLUGIN_NOT_INSTALLED = -1; public static final int PLUGIN_NEED_UPGRADE = 2;
/** * 获取 tn */
private void getTn() { new Thread(new Runnable() { @Override public void run() { String tn = null; InputStream is; try { String url = TN_URL_01; URL myURL = new URL(url); URLConnection ucon = myURL.openConnection(); ucon.setConnectTimeout(120000); is = ucon.getInputStream(); int i = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((i = is.read()) != -1) { baos.write(i); } tn = baos.toString(); is.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } Message msg = mHandler.obtainMessage(); msg.obj = tn; mHandler.sendMessage(msg); } }).start(); }
private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { String tn = ""; if (msg.obj == null || ((String) msg.obj).length() == 0) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("错误提示"); builder.setMessage("网络连接失败,请重试!"); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } else { tn = (String) msg.obj; /************************************************* * 步骤2:通过银联工具类启动支付插件 ************************************************/ doStartUnionPayPlugin(MainActivity.this, tn, mMode); } } };
public void doStartUnionPayPlugin(Activity activity, String tn, String mode) {
// 判断当前手机是否安装了银联的 apk boolean bInstall = UPPayAssistEx.checkInstalled(activity); if (bInstall) { // mMode参数解释: // 0 - 启动银联正式环境 // 1 - 连接银联测试环境 int ret = UPPayAssistEx.startPay(this, null, null, tn, mode); if (ret == PLUGIN_NEED_UPGRADE || ret == PLUGIN_NOT_INSTALLED) { // 需要重新安装控件 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("完成购买需要安装银联支付控件,是否安装?"); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UPPayAssistEx.installUPPayPlugin(MainActivity.this); dialog.dismiss(); } }); builder.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } } else { UPPayAssistEx.startPay(activity, null, null, tn, mode); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { /************************************************* * 步骤3:处理银联手机支付控件返回的支付结果 ************************************************/ if (data == null) { return; } String msg = ""; /* * 支付控件返回字符串:success、fail、cancel 分别代表支付成功,支付失败,支付取消 */ String str = data.getExtras().getString("pay_result"); if (str.equalsIgnoreCase("success")) { // 支付成功后,extra中如果存在result_data,取出校验 // result_data结构见c)result_data参数说明 if (data.hasExtra("result_data")) { String result = data.getExtras().getString("result_data"); try { JSONObject resultJson = new JSONObject(result); String sign = resultJson.getString("sign"); String dataOrg = resultJson.getString("data"); // 验签证书同后台验签证书 // 此处的verify,商户需送去商户后台做验签 boolean ret = verify(dataOrg, sign, mMode); if (ret) { // 验证通过后,显示支付结果 msg = "支付成功!"; } else { // 验证不通过后的处理 // 建议通过商户后台查询支付结果 msg = "支付失败!"; } } catch (JSONException e) { } } else { // 未收到签名信息 // 建议通过商户后台查询支付结果 msg = "支付成功!"; } } else if (str.equalsIgnoreCase("fail")) { msg = "支付失败!"; } else if (str.equalsIgnoreCase("cancel")) { msg = "用户取消了支付"; } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("支付结果通知"); builder.setMessage(msg); builder.setInverseBackgroundForced(true); // builder.setCustomTitle(); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }
private boolean verify(String msg, String sign64, String mode) { // 此处的verify,商户需送去商户后台做验签 return true; }