unit weixinapi;
interface
uses IdHTTP,//indy HttpClient
//使用RealThinClientSDK_v628中的rtcInfo的Utf8Decode函數來(lái)消除中文亂碼問(wèn)題,
//System中自帶的Utf8Decode有問(wèn)題
rtcInfo,
IWNativeXml,//NativeXML
System.Classes,//FileStream
System.Variants,//使用隨機數初始化函數Randomize
System.SysUtils,Xml.XMLDoc,Data.Win.ADODB,//使用XMLDocument、AdoConnection、AdoQuery
//不同版本的SSL會(huì )使idHTTP需要使用到IOHandler參數
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;
function weixinapi_downloadbill(bill_date:string): string;
function weixinapi_orderquery(out_trade_no:string):string;
function weixinapi_refundquery(out_trade_no:string):string;
function MD5_encrypt(str_TEMP:string):string;
const api_id='你的公眾賬號ID';//你的公眾賬號ID
mch_id='你的商戶(hù)號';//你的商戶(hù)號
api_key='你的API密鑰';//你的API密鑰
refundquery_url='https://api.mch.weixin.qq.com/pay/refundquery';//查詢(xún)退款網(wǎng)址
implementation
//微信商戶(hù)對賬功能-查詢(xún)退款詳情
//out_trade_no是需要查詢(xún)的商戶(hù)訂單號,也可以使用微信訂單號來(lái)查詢(xún)
function weixinapi_refundquery(out_trade_no:string):string;
var xmldd:TNativeXml;
fsparams:TFileStream;
idhttp_bill:TIdHTTP;
//Nativexml中沒(méi)有發(fā)現如何賦值已有的xml,只好重新加入系統自帶的xml
sRe_XML:TXMLDocument;
stringA:string;
stringTEMP:string;
nonce_str:string;
sign:string;
sResponse:string;
iohandssl:TIdSSLIOHandlerSocketOpenSSL;
begin
Randomize;
nonce_str:=IntToStr(Random(1000000));//得到隨機數nonce_str
stringA:='appid='+api_id+'&mch_id='+mch_id+'&nonce_str='+nonce_str+'&out_trade_no='+
out_trade_no;
stringTEMP:=stringA+'&key='+api_key;
sign:=MD5_encrypt(stringTEMP);//使用MD5加密函數對stringTEMP進(jìn)行加密,得到sign簽名
try
//創(chuàng )建通過(guò)idhttp POST 的xml文件
xmldd:=TNativeXml.CreateName('xml');
xmldd.EncodingString:='utf-8';
xmldd.XmlFormat:=xfReadable;
xmldd.Root.WriteString('appid',api_id);
xmldd.Root.WriteString('mch_id',mch_id);
xmldd.Root.WriteString('nonce_str',nonce_str);
xmldd.Root.WriteString('out_trade_no',out_trade_no);
xmldd.Root.WriteString('sign',sign);
xmldd.SaveToFile('weixin_refund_'+out_trade_no+'.xml');
except
on e: Exception do
Result:=e.Message;
end;
//根據創(chuàng )建的xml文件創(chuàng )建fsparams文件流,
//試驗過(guò)多種格式的post內容,string、TStrings、標準xml文件,
//最后發(fā)現使用TFileStream POST過(guò)去的內容
//微信接口才識別為xml格式,其他格式都會(huì )報XML Format Error等錯誤
fsparams:=TFileStream.Create('.\weixin_refund_'+out_trade_no+'.xml',fmOpenRead or fmShareDenyWrite);
try
sRe_XML:=TXMLDocument.Create(nil);
//對于不同版本的SSL庫需要加上TIdSSLIOHandlerSocketOpenSSL,
//不然會(huì )報IOHandler value is not valid錯誤
iohandssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
idhttp_bill:=tIdHTTP.Create();
idhttp_bill.Request.ContentType:='text/xml';
idhttp_bill.Request.CharSet:='UTF-8';
idhttp_bill.IOHandler:=iohandssl;
//使用的UTF8Decode函數是rtcInfo里面的,不是system里面的,system中的還是會(huì )亂碼
sRe_XML.XML.Text:=Utf8Decode(idhttp_bill.Post(refundquery_url,fsparams));
sRe_XML.XML.SaveToFile('weixin_refundquery_'+out_trade_no+'.xml');
Result:=sRe_XML.XML.Text;//返回從微信接口收到的數據
FreeAndNil(fsparams);
FreeAndNil(idhttp_bill);
FreeAndNil(sre_XML);
except
on e: Exception do
Result:=e.Message;
end;
end;
//MD5加密,試過(guò)多種方法包括 SQLServer、MD5單元來(lái)進(jìn)行MD5加密,卻都得不到正確的MD5加密值,
//無(wú)奈使用了mysql的MD5函數來(lái)進(jìn)行,有更好更簡(jiǎn)潔的方法嗎
function MD5_encrypt(str_TEMP:string):string;
var adoc_md5:TADOConnection;
adoq_md5:TADOQuery;
begin
try
adoc_md5:=TADOConnection.Create(nil);
adoc_md5.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=my;';
adoc_md5.LoginPrompt:=False;
adoc_md5.Connected;
adoq_md5:=TADOQuery.Create(nil);
adoq_md5.Connection:=adoc_md5;
adoq_md5.SQL.Text:='select upper(md5('''+str_TEMP+''')) as sign_str';
adoq_md5.Open;
Result:=adoq_md5.FieldByName('sign_str').AsString;
finally
FreeAndNil(adoc_md5);
FreeAndNil(adoq_md5);
end;
end;
end.