接上篇。这次是商户平台的申请退款功能。此功能要点是商户平台退款功能要求使用HTTPS双向证书,所以通过TIdSSLIOHandlerSocketOpenSSL.SSLOptions.CertFile加载微信颁发的证书文件,TIdSSLIOHandlerSocketOpenSSL.SSLOptions.KeyFile加载证书密钥文件。需要注意的是,该接口在收到客户端发送的正确(格式、数据、证书等)请求后,会立即执行退款操作,没有审核环节,执行成功及开始退款流程,所以需要客户端应用程序把好审核关,防止错退的情况发生。代码如下:
unit weixinapi;
interface
uses IdHTTP,//indy HttpClient
//使用RealThinClientSDK_v628中的rtcInfo的Utf8Decode函数来消除中文乱码问题,
//System中自带的Utf8Decode有问题
rtcInfo,
IWNativeXml,//NativeXML
System.Classes,//FileStream
System.Variants,//使用随机数初始化函数Randomize
System.SysUtils,Xml.XMLDoc,Data.Win.ADODB,//使用XMLDocument、AdoConnection、AdoQuery
//退款功能要求使用颁发的证书文件和密钥文件
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL;
function weixinapi_refund(op_user_id,out_refund_no,out_trade_no,refund_fee,
total_fee,transaction_id:string):string;
function MD5_encrypt(str_TEMP:string):string;
const api_id='你的公众账号ID';//你的公众账号ID
mch_id='你的商户号';//你的商户号
api_key='你的API密钥';//你的API密钥
refund_url='https://api.mch.weixin.qq.com/secapi/pay/refund';//申请退款网址
implementation
//微信商户对账功能-申请退款
//op_user_id:操作员,out_refund_no:商户退款单号,out_trade_no商户订单号,
//refund_fee:退款金额,total_fee:总金额, transaction_id:微信订单号
function weixinapi_refund(op_user_id,out_refund_no,out_trade_no,refund_fee,
total_fee,transaction_id:string):string;
var xmldd:TNativeXml;
fsparams:TFileStream;
idhttp_refund:TIdHTTP;
//Nativexml中没有发现如何赋值已有的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+
'&op_user_id='+op_user_id+'&out_refund_no='+out_refund_no+
'&out_trade_no='+out_trade_no+'&refund_fee='+refund_fee+
'&total_fee='+total_fee+'&transaction_id='+transaction_id;
stringTEMP:=stringA+'&key='+api_key;
sign:=MD5_encrypt(stringTEMP);//使用MD5加密函数对stringTEMP进行加密,得到sign签名
try
//创建通过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('op_user_id',op_user_id);
xmldd.Root.WriteString('out_refund_no',out_refund_no);
xmldd.Root.WriteString('out_trade_no',out_trade_no);
xmldd.Root.WriteString('refund_fee',refund_fee);
xmldd.Root.WriteString('total_fee',total_fee);
xmldd.Root.WriteString('transaction_id',transaction_id);
xmldd.Root.WriteString('sign',sign);
xmldd.SaveToFile('weixin_refundd_'+out_trade_no+'.xml');
except
on e: Exception do
Result:=e.Message;
end;
//根据创建的xml文件创建fsparams文件流,
//试验过多种格式的post内容,string、TStrings、标准xml文件,
//最后发现使用TFileStream POST过去的内容
//微信接口才识别为xml格式,其他格式都会报XML Format Error等错误
fsparams:=TFileStream.Create('.\weixin_refundd_'+out_trade_no+'.xml',fmOpenRead or fmShareDenyWrite);
try
sRe_XML:=TXMLDocument.Create(nil);
//对于不同版本的SSL库需要加上TIdSSLIOHandlerSocketOpenSSL,
//不然会报IOHandler value is not valid错误
//同时退款功能要求使用颁发的证书文件和密钥文件
iohandssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
idhttp_refund:=tIdHTTP.Create();
idhttp_refund.Request.ContentType:='text/xml';
idhttp_refund.Request.CharSet:='UTF-8';
idhttp_refund.IOHandler:=iohandssl;
//加载微信支付商户平台颁发的证书文件
iohandssl.SSLOptions.CertFile:='cert_api/apiclient_cert.pem';
//加载微信支付商户平台颁发的密钥文件
iohandssl.SSLOptions.KeyFile:='cert_api/apiclient_key.pem';
idhttp_refund.HandleRedirects:=True;
// idhttp_refund.IOHandler.Close;
//使用的UTF8Decode函数是rtcInfo里面的,不是system里面的,system中的还是会乱码
sRe_XML.XML.Text:=Utf8Decode(idhttp_refund.Post(refund_url,fsparams));
sRe_XML.XML.SaveToFile('weixin_refunddetial_'+out_trade_no+'.xml');
Result:=sRe_XML.XML.Text;//返回从微信接口收到的数据
FreeAndNil(fsparams);
FreeAndNil(idhttp_refund);
FreeAndNil(sre_XML);
except
on e: Exception do
Result:=e.Message;
end;
end;
//MD5加密,试过多种方法包括 SQLServer、MD5单元来进行MD5加密,却都得不到正确的MD5加密值,
//无奈使用了mysql的MD5函数来进行,有更好更简洁的方法吗
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.