
微信二维码生成
-
2023年2月15日发(作者:)Android微信⽣成⽀付⼆维码URL
⽣成微信⽀付⼆维码URL,让别⼈扫⼀扫收钱,就是微信的收钱码
/***微信⼆维码⽀付**/
publicclassWXPayUtils{
privatestaticStringstrResponse=null;
privatestaticStringurl=\"\";
publicstaticStringnonceStr=\"\";
publicstaticStringoutTradeNo=\"\";
/**
*微信⽣成签名
*/
privatestaticStringgenPackageSign(Listparams){
StringBuildersb=newStringBuilder();
for(inti=0;i<();i++){
((i).getName());
(\'=\');
((i).getValue());
(\'&\');
}
(\"key=\");
();
StringpackageSign=sageDigest(ng().getBytes())
.toUpperCase();
Log.e(\"orion\",packageSign);
returnpackageSign;
}
/***微信获取签名**/
publicstaticStringgenProductArgs(StringnonceStr,StringoutTradeNo,
StringtotalFee){
try{
ListpackageParams=newLinkedList();
(newBasicNameValuePair(\"appid\",
));
(newBasicNameValuePair(\"body\",me));
packageParams
.add(newBasicNameValuePair(\"mch_id\",));
(newBasicNameValuePair(\"nonce_str\",nonceStr));
(newBasicNameValuePair(\"notify_url\",
\"/wxpay/\"));
packageParams
.add(newBasicNameValuePair(\"out_trade_no\",outTradeNo));
(newBasicNameValuePair(\"spbill_create_ip\",
));
(newBasicNameValuePair(\"total_fee\",totalFee));
(newBasicNameValuePair(\"trade_type\",\"NATIVE\"));
Stringsign=genPackageSign(packageParams);
returnsign;
}catch(Exceptione){
leToSMB(eptionInfo(e));
tackTrace();
returnnull;
}
}
/***微信获取签名**/
publicstaticStringQgenProductArgs(StringnonceStr,StringoutTradeNo){
try{
ListpackageParams=newLinkedList();
(newBasicNameValuePair(\"appid\",
));
packageParams
.add(newBasicNameValuePair(\"mch_id\",));
(newBasicNameValuePair(\"nonce_str\",nonceStr));
packageParams
.add(newBasicNameValuePair(\"out_trade_no\",outTradeNo));
Stringsign=genPackageSign(packageParams);
returnsign;
}catch(Exceptione){
leToSMB(eptionInfo(e));
tackTrace();
returnnull;
}
}
/**微信获取随机字符串nonce_str**/
publicstaticStringgetNonceStr(){
Randomrandom=newRandom();
nonceStr=sageDigest(f(
tTimeMillis()+t(10000)).getBytes());
returnnonceStr;
}
/**微信获取随机字符串out_trade_no**/
publicstaticStringgetOutTradNo(){
Randomrandom=newRandom();
outTradeNo=sageDigest(f(
tTimeMillis()+t(10000)).getBytes());
returnoutTradeNo;
}
/****微信⽣成⼆维码URL**/
publicstaticvoidsendWxPayRequest(finalStringbody,finalStringnonceStr,
finalStringoutTradeNo,finalStringtotalFee,
finalHandlerhandler){
newThread(newRunnable(){
@Override
publicvoidrun(){
//构造HTTP请求
HttpClienthttpclient=newHttpClient();
PostMethodpostMethod=newPostMethod(
\"/pay/unifiedorder\");
StringBufferrequestStr=newStringBuffer(\"\");
(\"
();
(\"]]>\");
(\"
(body);
(\"]]>\");
(\"
();
(\"]]>\");
(\"
(nonceStr);
(\"]]>\");
(\"
(\"/wxpay/\");
(\"]]>\");
(\"
(outTradeNo);
(\"]]>\");
(\"
();
(\"]]>\");
(\"
(totalFee);
(\"]]>\");
(\"
(\"NATIVE\");
(\"]]>\");
(\"
(ductArgs(nonceStr,
outTradeNo,totalFee));
(\"]]>\");
(\"\");
//发送请求
try{
RequestEntityentity=newStringRequestEntity(
ng(),\"text/xml\",\"UTF-8\");
uestEntity(entity);
eMethod(postMethod);
strResponse=newString(ponseBody(),
\"utf-8\");
Log.e(\"strResponse\",strResponse);
//File(strResponse);
Messagemsg=();
if(!\"FAIL\".equals((\"CDATA[\")[1].split(\"]]\")[0])){
url=\"weixin\"
+(\"weixin\")[1].split(\"]]\")[0];
=0;
=url;
}else{
=8;
}
ssage(msg);
}catch(HttpExceptione){
leToSMB(eptionInfo(e));
tackTrace();
}catch(IOExceptione){
leToSMB(eptionInfo(e));
tackTrace();
}finally{
eConnection();
}
}
}).start();
}
/****微信查询订单状态**/
publicstaticvoidqueryWxPayRequest(finalStringnonceStr,
finalStringoutTradeNo,finalHandlerhandler){
newThread(newRunnable(){
@Override
publicvoidrun(){
//构造HTTP请求
HttpClienthttpclient=newHttpClient();
PostMethodpostMethod=newPostMethod(
\"/pay/orderquery\");
StringBufferrequestStr=newStringBuffer(\"\");
(\"
();
(\"]]>\");
(\"
();
(\"]]>\");
(\"
(nonceStr);
(\"]]>\");
(\"
(outTradeNo);
(\"]]>\");
(\"
(oductArgs(nonceStr,
outTradeNo));
(\"]]>\");
(\"\");
//发送请求
try{
RequestEntityentity=newStringRequestEntity(requestStr
.toString(),\"text/xml\",\"UTF-8\");
uestEntity(entity);
eMethod(postMethod);
strResponse=newString(ponseBody(),
\"utf-8\");
Log.e(\"strResponse\",strResponse);
Stringstate=\"\";
if(!\"FAIL\".equals((\"CDATA[\")[1].split(\"]]\")[0])){
state=(\"trade_state\")[1]
.split(\"[\")[2].split(\"]]\")[0];
}
Messagemsg=();
=1;
=state;
ssage(msg);
Log.e(\"state\",state);
}catch(Exceptione){
leToSMB(eptionInfo(e));
tackTrace();
}finally{
eConnection();
}
}
}).start();
}
}