Android-银联支付
银联支付也是一般比较常用的支付功能,这里简单了介绍android app如果短期快速应用这一方面的东西。直接上代码:
1.导入银联支付的依赖包:
2.在res目录下增加资源包:
3.配置AndroidManifest.xml文件配置打开的activity:
<activity android:name="com.unionpay.uppay.PayActivity" android:configChanges="orientation|keyboardHidden" android:excludeFromRecents="true" android:label="@string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize" />至此,我们的项目环境就搭好了.(以上资料文件,请查看银联测试文档目录的androiddemo);
银联支付代码3步骤:
1.获取TN号 2.请求控件界面(PayActivity)
3.ActivityResult处理支付结果(但该结果并不一定正确 ,因为银联系统通过异步返回给服务器和我们的app是同时异步的)
以下 我以测试接口为例
/** * UnionPay Test * * @author Lean @date:2014-8-8 */ public class MainActivity extends Activity implements Runnable { private String mMode = "01";//设置测试模式:01为测试 00为正式环境 private static final String TN_URL_01 = "http://202.101.25.178:8080/sim/gettn";//自己后台需要实现的给予我们app的tn号接口 private Handler mHandler=new Handler(){ public void handleMessage(android.os.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; doStartUnionPayPlugin(MainActivity.this, tn, mMode); } } }; /** * 启动支付界面 */ public void doStartUnionPayPlugin(Activity activity, String tn, String mode) { UPPayAssistEx.startPayByJAR(activity, PayActivity.class, null, null, tn, mode); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(MainActivity.this).start(); } @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); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data == null) { return; } String msg = ""; /* * 支付控件返回字符串:success、fail、cancel 分别代表支付成功,支付失败,支付取消 */ String str = data.getExtras().getString("pay_result"); Log.v("zftphone", "2 "+data.getExtras().getString("merchantOrderId")); if (str.equalsIgnoreCase("success")) { msg = "支付成功!"; } else if (str.equalsIgnoreCase("fail")) { msg = "支付失败!"; } else if (str.equalsIgnoreCase("cancel")) { msg = "用户取消了支付"; } //支付完成,处理自己的业务逻辑! } }
Tip:做这个的时候,你要保证你有个牛比的支持团队能够接受新事物,同时谴责下银联的客服 太垃圾了说!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。