Android生成支付二维码URL

Android⽣成⽀付⼆维码URL ⽣成⽀付⼆维码URL,让别⼈扫⼀扫收钱,就是的收钱码
/***⼆维码⽀付**/
public class WXPayUtils {
private static String strResponse = null;
private static String url = "";
public static String nonceStr = "";
public static String outTradeNo = "";
/**
* ⽣成签名
*/
private static String genPackageSign(List<NameValuePair> params) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < params.size(); i++) {
sb.(i).getName());
sb.append('=');
sb.(i).getValue());
sb.append('&');
}
sb.append("key=");
sb.append(AlipayUtils.key);
String packageSign = String().getBytes())
.toUpperCase();
Log.e("orion", packageSign);
return packageSign;
}
/*** 获取签名 **/
public static String genProductArgs(String nonceStr, String outTradeNo,
String totalFee) {
try {
List<NameValuePair> packageParams = new LinkedList<>();
packageParams.add(new BasicNameValuePair("appid",
AlipayUtils.appid));
packageParams.add(new BasicNameValuePair("body", AlipayUtils.bodyname));
packageParams
.add(new BasicNameValuePair("mch_id", hid));
packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));
packageParams.add(new BasicNameValuePair("notify_url",
"www.weixin.qq/wxpay/pay.php"));
packageParams
.add(new BasicNameValuePair("out_trade_no", outTradeNo));
packageParams.add(new BasicNameValuePair("spbill_create_ip",
AlipayUtils.ip));
packageParams.add(new BasicNameValuePair("total_fee", totalFee));
packageParams.add(new BasicNameValuePair("trade_type", "NATIVE"));
String sign = genPackageSign(packageParams);
return sign;
} catch (Exception e) {
LogUtils.ExceptionInfo(e));
e.printStackTrace();
return null;
}
}
public static String QgenProductArgs(String nonceStr, String outTradeNo) { try {
List<NameValuePair> packageParams = new LinkedList<>();
packageParams.add(new BasicNameValuePair("appid",
AlipayUtils.appid));
packageParams
.add(new BasicNameValuePair("mch_id", hid));
packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));            packageParams
.add(new BasicNameValuePair("out_trade_no", outTradeNo));
String sign = genPackageSign(packageParams);
return sign;
} catch (Exception e) {
LogUtils.ExceptionInfo(e));
e.printStackTrace();
return null;
}
}
/** 获取随机字符串nonce_str **/
public static String getNonceStr() {
Random random = new Random();
nonceStr = MessageDigest(String.valueOf(
System.currentTimeMillis() + Int(10000)).getBytes());
return nonceStr;
}
/** 获取随机字符串out_trade_no **/
public static String getOutTradNo() {
Random random = new Random();
outTradeNo = MessageDigest(String.valueOf(
System.currentTimeMillis() + Int(10000)).getBytes());
return outTradeNo;
}
/**** ⽣成⼆维码URL **/
public static void sendWxPayRequest(final String body, final String nonceStr,
final String outTradeNo, final String totalFee,二维码支付
final Handler handler) {
new Thread(new Runnable() {
@Override
public void run() {
// 构造HTTP请求
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod(
"h.weixin.qq/pay/unifiedorder");
StringBuffer requestStr = new StringBuffer("<xml>");
requestStr.append("<appid><![CDATA[");
requestStr.append(AlipayUtils.appid);
requestStr.append("]]></appid>");
requestStr.append("<body><![CDATA[");
requestStr.append(body);
requestStr.append("]]></body>");
requestStr.append("<mch_id><![CDATA[");
requestStr.hid);
requestStr.append("]]></mch_id>");
requestStr.append("<nonce_str><![CDATA[");
requestStr.append(nonceStr);
requestStr.append("]]></nonce_str>");
requestStr.append("www.weixin.qq/wxpay/pay.php");                requestStr.append("]]></notify_url>");
requestStr.append("<out_trade_no><![CDATA[");
requestStr.append(outTradeNo);
requestStr.append("]]></out_trade_no>");
requestStr.append("<spbill_create_ip><![CDATA[");
requestStr.append(AlipayUtils.ip);
requestStr.append("]]></spbill_create_ip>");
requestStr.append("<total_fee><![CDATA[");
requestStr.append(totalFee);
requestStr.append("]]></total_fee>");
requestStr.append("<trade_type><![CDATA[");
requestStr.append("NATIVE");
requestStr.append("]]></trade_type>");
requestStr.append("<sign><![CDATA[");
requestStr.ProductArgs(nonceStr,
outTradeNo, totalFee));
requestStr.append("]]></sign>");
requestStr.append("</xml>");
// 发送请求
try {
RequestEntity entity = new StringRequestEntity(
postMethod.setRequestEntity(entity);
strResponse = new ResponseBody(),
"utf-8");
Log.e("strResponse", strResponse);
//                  LogUtils.saveToFile(strResponse);
Message msg = Message.obtain();
if (!"FAIL".equals(strResponse.split("CDATA\\[")[1].split("]]")[0])){                        url = "weixin"
+ strResponse.split("weixin")[1].split("]]")[0];
msg.what = 0;
msg.obj = url;
}else {
msg.what = 8;
}
handler.sendMessage(msg);
} catch (HttpException e) {
LogUtils.ExceptionInfo(e));
e.printStackTrace();
} catch (IOException e) {
LogUtils.ExceptionInfo(e));
e.printStackTrace();
} finally {
}
}
}).start();
}
/**** 查询订单状态 **/
public static void queryWxPayRequest(final String nonceStr,
final String outTradeNo, final Handler handler) { new Thread(new Runnable() {
@Override
public void run() {
// 构造HTTP请求
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod(
"h.weixin.qq/pay/orderquery");
StringBuffer requestStr = new StringBuffer("<xml>");
requestStr.append(AlipayUtils.appid);
requestStr.append("]]></appid>");
requestStr.append("<mch_id><![CDATA[");
requestStr.hid);
requestStr.append("]]></mch_id>");
requestStr.append("<nonce_str><![CDATA[");
requestStr.append(nonceStr);
requestStr.append("]]></nonce_str>");
requestStr.append("<out_trade_no><![CDATA[");
requestStr.append(outTradeNo);
requestStr.append("]]></out_trade_no>");
requestStr.append("<sign><![CDATA[");
requestStr.append(WXPayUtils.QgenProductArgs(nonceStr,
outTradeNo));
requestStr.append("]]></sign>");
requestStr.append("</xml>");
// 发送请求
try {
RequestEntity entity = new StringRequestEntity(requestStr
.toString(), "text/xml", "UTF-8");
postMethod.setRequestEntity(entity);
strResponse = new ResponseBody(),
"utf-8");
Log.e("strResponse", strResponse);
String state = "";
if (!"FAIL".equals(strResponse.split("CDATA\\[")[1].split("]]")[0])){                        state = strResponse.split("trade_state")[1]
.split("\\[")[2].split("]]")[0];
}
Message msg = Message.obtain();
msg.what = 1;
msg.obj = state;
handler.sendMessage(msg);
Log.e("state", state);
} catch (Exception e) {
LogUtils.ExceptionInfo(e));
e.printStackTrace();
} finally {
}
}
}).start();
}
}

本文发布于:2024-09-24 11:26:47,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/357780.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:微信   请求   获取
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议