【android仿系列进阶篇】android 支付宝手机网页支付
最近在做android~,恩,就说这么多吧
1,准备工作
2,代码相关
/** * 解析远程模拟提交后返回的信息,获得token * @param text 要解析的字符串 * @return 解析结果 * @throws Exception */ public static String getRequestToken(String text) throws Exception { String request_token = ""; //以“&”字符切割字符串 String[] strSplitText = text.split("&"); //把切割后的字符串数组变成变量与数值组合的字典数组 Map<String, String> paraText = new HashMap<String, String>(); for (int i = 0; i < strSplitText.length; i++) { //获得第一个=字符的位置 int nPos = strSplitText[i].indexOf("="); //获得字符串长度 int nLen = strSplitText[i].length(); //获得变量名 String strKey = strSplitText[i].substring(0, nPos); //获得数值 String strValue = strSplitText[i].substring(nPos+1,nLen); //放入MAP类中 paraText.put(strKey, strValue); } if (paraText.get("res_data") != null) { String res_data = paraText.get("res_data"); //解析加密部分字符串(RSA与MD5区别仅此一句) if(AlipayConfig.sign_type.equals("0001")) { res_data = RSA.decrypt(res_data, AlipayConfig.private_key, AlipayConfig.input_charset); } //token从res_data中解析出来(也就是说res_data中已经包含token的内容) Document document = DocumentHelper.parseText(res_data); request_token = document.selectSingleNode( "//direct_trade_create_res/request_token" ).getText(); } return request_token; }
res_data = RSA.decrypt(res_data, AlipayConfig.private_key, AlipayConfig.input_charset);
Document document = DocumentHelper.parseText(res_data); request_token = document.selectSingleNode( "//direct_trade_create_res/request_token" ).getText();
Log.d("res_data", res_data); request_token = res_data.substring(res_data.indexOf("<?")); int startIndex = request_token.indexOf("<request_token>"); if (startIndex > -1) { int temp = "<request_token>".length(); String start = request_token.substring(temp+startIndex, temp+startIndex+22); Log.d("start", start); int endIndex = request_token.indexOf("</request_token>"); String end = request_token.substring(endIndex - 18, endIndex); Log.d("end", end); return start+end; } Log.d("request_token", request_token); return null;
这里说明一下为什么,首先它的这两句无非就是xml的解析,我们没必要这么用,还要加上它的包,另外,我发现解析出来的授权令牌里面有乱码,不管我怎么设置编码都有,本想联系客服问个原因,但是一直都是繁忙中,无奈我只能截取一下,先用着,望有知道的同学告知一下……
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/top_bar" android:layout_width="match_parent" android:layout_height="55dp" android:layout_alignParentTop="true" android:background="@color/light_black" android:padding="4dp"> <TextView android:id="@+id/back_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="4dp android:gravity="center" android:padding="6dp" android:text="@string/home_back" android:textColor="@drawable/top_bar_selector" android:textSize="20sp" /> <TextView android:id="@+id/main_app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="支付宝网页版" android:textColor="@android:color/white" android:textSize="22sp" /> </RelativeLayout> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/top_bar" android:scrollbars="none" /> </RelativeLayout>
webView.getSettings().setJavaScriptEnabled(true);//允许webkit执行js代码; webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } }); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); } }); webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); } public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (progressDialog != null) { progressDialog.dismiss(); } } public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } });
public static String getAliPayWAPOrderBodyInfor(String subject, String price, String refNo, String SUCCESS_URL, String NOTIFY_URL, String userid) { String orderInfor = "<direct_trade_create_req><su" + "bject>" + subject + "</subject><out_trade_no>" + "1282889689801" + "</out_trade_no><total_fee>" + price + "</tot" + "al_fee><seller_account_name>" + AliPayUtil.SELLER + "</seller_account_name><call_" + "back_url>" + SUCCESS_URL + "</call_back_" + "url><notify_url>" + NOTIFY_URL + "</notify_url><out_user>" + userid + "</out_user><pay_expire>3600</pay_expire></direct_trade_create" + "_req>"; return orderInfor; }
public static Map<String, String> getAccessTokenParamsMap() { Map<String, String> sParaTemp = new HashMap<String, String>(); sParaTemp.put("service", "alipay.wap.trade.create.direct"); sParaTemp.put("partner", AliPayUtil.PARTNER);//这里换成你的pid sParaTemp.put("sec_id", "0001");//这是RSA加密 sParaTemp.put("format", "xml"); sParaTemp.put("v", "2.0"); sParaTemp.put("req_id", UtilDate.getOrderNum());//这个方法可以把util包里面的这个UtilDate.java拿过来,当然了没必要的话直接把相关的方法拿过来 return sParaTemp; }
public static Map<String, String> getBillParamsMap() { Map<String, String> sParaTemp = new HashMap<String, String>(); sParaTemp.put("service", "alipay.wap.auth.authAndExecute"); sParaTemp.put("partner", AliPayUtil.PARTNER);//这里换成你的pid sParaTemp.put("sec_id", "0001"); sParaTemp.put("format", "xml"); sParaTemp.put("v", "2.0"); return sParaTemp; }
/** * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串 * @param params 需要排序并参与字符拼接的参数组 * @return 拼接后字符串 */ public static String createLinkString(Map<String, String> params) { List<String> keys = new ArrayList<String>(params.keySet()); Collections.sort(keys); String prestr = ""; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = params.get(key); if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符 prestr = prestr + key + "=" + value; } else { prestr = prestr + key + "=" + value + "&"; } } return prestr; }
public static String buildRequestMysign(Map<String, String> sPara) { String prestr = AlipayCore.createLinkString(sPara); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 String mysign = ""; mysign = sign(prestr, private_key, "utf-8"); return mysign; }
<pre name="code" class="html">Map<String, String> sParaTemp = AliPayUtil.getAccessTokenParamsMap(); sParaTemp.put("req_data", RSA.getAliPayWAPOrderBodyInfor(SUBJECT, price, "1282889689801", SUCCESS_URL, NOTIFY_URL, userid)); //生成签名结果 String mysign = RSA.buildRequestMysign(sParaTemp); //签名结果与签名方式加入请求提交参数组中 sParaTemp.put("sign", mysign);
这样我们的参数map就做好了,当然了 一般我们是用post请求所以好要把它转化一下,像这样:
private List<NameValuePair> generatNameValuePair(Map<String, String> properties) { List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(properties.size()); for (Map.Entry<String, String> entry : properties.entrySet()) { nameValuePair.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } return nameValuePair; }
这样你就可以以post形式发了,post的url是http://wappaygw.alipay.com/service/rest.htm,参数是上面的list
String dec = ""; try { dec = URLDecoder.decode(result, "utf-8");//请将result换成你的请求结果 } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } String ret; try { ret = RSA.getRequestToken(dec); Log.d("ret", ret); if (ret!=null) { wapPay(ret); } else { //message return; } } catch (Exception e) { e.printStackTrace(); return; }
先做一次urldecode,然后再解析数据:
private void wapPay(String token) { Map<String, String> sParaTemp = RSA.getBillParamsMap(); String req_data = "<auth_and_execute_req><request_token>" + token + "</request_token></auth_and_execute_req>"; sParaTemp.put("req_data", req_data); String mysign = RSA.buildRequestMysign(sParaTemp); sParaTemp.put("sign", mysign); List<NameValuePair> paramspost = generatNameValuePair(sParaTemp); String url = "http://wappaygw.alipay.com/service/rest.htm?" + URLEncodedUtils.format(paramspost, "UTF-8"); webView.loadUrl(url); }
好的至此,我们可以看到页面了:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。