先导入jar包UPPapAssistEx
android银联支付分成以下三个部分↓
一、从网络开始,获取交易流水号即TN(通过网络请求从后台获取到TN)
跟接口处商量获取方式
二、通过银联工具类启动支付插件
UpPayUtils.doStartUnionPayPlugin(this,tn,ConstantValues.MODE);
银联支付工具类
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.util.Log; import com.unionpay.UPPayAssistEx; import com.xh.boke.ConstantValues; /** * 银联支付工具类 * Created by Administrator on 2015/10/26 0026. */ public class UpPayUtils { private static final String TAG = "UpPayUtils"; /** * 调用银联支付 * @param activity 活动页 * @param tn 流水号 * @param mode 启动环境 */ public static void doStartUnionPayPlugin(final Activity activity, String tn, String mode) { // mMode参数解释: // 00 - 启动银联正式环境 // 01 - 连接银联测试环境 int ret = UPPayAssistEx.startPay(activity, null, null, tn, mode); if (ret == ConstantValues.PLUGIN_NEED_UPGRADE || ret == ConstantValues.PLUGIN_NOT_INSTALLED) { // 需要重新安装控件 Log.e(TAG, " plugin not found or need upgrade!!!"); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("提示"); builder.setMessage("完成购买需要安装银联支付控件,是否安装?"); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { UPPayAssistEx.installUPPayPlugin(activity); dialog.dismiss(); } }); builder.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } Log.e(TAG, "" + ret); } }
三、处理银联手机支付控件返回的支付结果
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data == null) { return; } /* * 支付控件返回字符串:success、fail、cancel 分别代表支付成功,支付失败,支付取消 */ String str = data.getExtras().getString("pay_result"); if (str.equalsIgnoreCase("success")) { Toast.makeText(this, "支付成功!", Toast.LENGTH_SHORT).show(); } else if (str.equalsIgnoreCase("fail")) { Toast.makeText(this, "支付失败!", Toast.LENGTH_SHORT).show(); } else if (str.equalsIgnoreCase("cancel")) { Toast.makeText(this, "您取消了订单支付!", Toast.LENGTH_SHORT).show(); } }
下面是银联的配置信息
public class ConstantValues { /** 银联支付 */ public static final int PLUGIN_VALID = 0;//已经安装银联插件 public static final int PLUGIN_NOT_INSTALLED = -1;//未安装银联插件 public static final int PLUGIN_NEED_UPGRADE = 2;//需要重新安装银联插件 /** mMode参数解释: "00" - 启动银联正式环境 "01" - 连接银联测试环境 */ public static final String MODE = "01"; }
另外,在Android Manifest.xml 里面添加以下权限与代码
<!-- 银联的权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 银联支付 --> <activity android:name="com.unionpay.uppay.PayActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" android:excludeFromRecents="true" android:windowSoftInputMode="adjustResize"> </activity>
注意:调用银联支付需要银联插件的支持,在assets文件夹中放入银联插件,在代码中已经有检测功能,这样可免下载安装