码迷,mamicode.com
首页 > 移动开发 > 详细

Android-银联支付

时间:2014-08-08 12:46:25      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:银联   android   支付   

银联支付也是一般比较常用的支付功能,这里简单了介绍android app如果短期快速应用这一方面的东西。直接上代码:

1.导入银联支付的依赖包:

bubuko.com,布布扣

2.在res目录下增加资源包:

bubuko.com,布布扣

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是同时异步的)

bubuko.com,布布扣

以下 我以测试接口为例

/**
 *		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:做这个的时候,你要保证你有个牛比的支持团队能够接受新事物,同时谴责下银联的客服 太垃圾了说!

Android-银联支付,布布扣,bubuko.com

Android-银联支付

标签:银联   android   支付   

原文地址:http://blog.csdn.net/qq285016127/article/details/38435585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!