先吐槽下微信的文档和demo,重要的步骤信息没有强调清楚,.net的demo就没有跑成功过。
1.微信扫码登录
2.微信pc端支付
几经摸索才走通这个退款功能。下面介绍下微信支付退款功能的开发步骤:
一、下载证书并导入到系统
微信退款是需要证书的,这个证书不是官方demo中的证书,而是需要自己在微信商户平台中的api安全栏下载的证书,在官方的证书使用实例的一个word文档看到下面话:c#有一点需要注意,除了在代码中使用apiclient_cert.p12之外还需要将该证书导入操作系统才能使用,1、代码中使用、;2、导入操作系统,二者缺一不可。.net版本需要大于2.0 之前就是不知道这两步,浪费了太多时间。所以先下载证书:
下载的时候需要手机验证及登录密码。下载后找到apiclient_cert.p12这个证书,双击导入,导入的时候提示输入密码,这个密码就是商户id,且必须是在自己的商户平台下载的证书。否则会出现密码错误的提示:
导入正确的提示:
二、代码退款
这个地方可以直接用官方demo中的代码,demo下载
需要修改wxpayconfig中的几个参数:
public const string appid = wxf6dd794bcexxxx; public const string mchid = xxxx; public const string key = xxxxx849ba56abbe56e05xxxxx; public const string appsecret = ---; //=======【证书路径设置】===================================== /* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要) */ public const string sslcert_path = /wxpayapi/cert/apiclient_cert.p12; public const string sslcert_password = 131xxxx;
上面的sslcert_password就是mchid,也就是商户id,sslcert_password错误会出现指定的网络密码不正确的提示:
接下来在控制器中增加一个退款方法,包含微信订单号、商户订单号、总金额和退款金额。商户订单号和微信订单号二选一。详细参数
public actionresult dorefund() { string result = refund.run(,131667780120trade_no, 1, 1); return content(result); }
refund类的run方法:
/*** * 申请退款完整业务流程逻辑 * @param transaction_id 微信订单号(优先使用) * @param out_trade_no 商户订单号 * @param total_fee 订单总金额 * @param refund_fee 退款金额 * @return 退款结果(xml格式) */ public static string run(string transaction_id, string out_trade_no, string total_fee, string refund_fee) { logger.info(refund is processing...); wxpaydata data = new wxpaydata(); if (!string.isnullorempty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准 { data.setvalue(transaction_id, transaction_id); } else//微信订单号不存在,才根据商户订单号去退款 { data.setvalue(out_trade_no, out_trade_no); } data.setvalue(total_fee, int.parse(total_fee));//订单总金额 data.setvalue(refund_fee, int.parse(refund_fee));//退款金额 data.setvalue(out_refund_no, out_trade_no);//随机生成商户退款单号 data.setvalue(op_user_id, wxpayconfig.mchid);//操作员,默认为商户号 wxpaydata result = wxpayapi.refund(data);//提交退款申请给api,接收返回数据 logger.info(refund process complete, result : + result.toxml()); return result.toprintstr(); }
refund:方法
/** * * 申请退款 * @param wxpaydata inputobj 提交给申请退款api的参数 * @param int timeout 超时时间 * @throws wxpayexception * @return 成功时返回接口调用结果,其他抛异常 */ public static wxpaydata refund(wxpaydata inputobj, int timeout = 6) { string url = https://api.mch.weixin.qq.com/secapi/pay/refund; //检测必填参数 if (!inputobj.isset(out_trade_no) && !inputobj.isset(transaction_id)) { throw new wxpayexception(退款申请接口中,out_trade_no、transaction_id至少填一个!); } else if (!inputobj.isset(out_refund_no)) { throw new wxpayexception(退款申请接口中,缺少必填参数out_refund_no!); } else if (!inputobj.isset(total_fee)) { throw new wxpayexception(退款申请接口中,缺少必填参数total_fee!); } else if (!inputobj.isset(refund_fee)) { throw new wxpayexception(退款申请接口中,缺少必填参数refund_fee!); } else if (!inputobj.isset(op_user_id)) { throw new wxpayexception(退款申请接口中,缺少必填参数op_user_id!); } inputobj.setvalue(appid, wxpayconfig.appid);//公众账号id inputobj.setvalue(mch_id, wxpayconfig.mchid);//商户号 inputobj.setvalue(nonce_str, guid.newguid().tostring().replace(-, ));//随机字符串 inputobj.setvalue(sign, inputobj.makesign());//签名 string xml = inputobj.toxml(); var start = datetime.now; log.debug(wxpayapi, refund request : + xml); string response = httpservice.post(xml, url, true, timeout);//调用http通信接口提交数据到api log.debug(wxpayapi, refund response : + response); var end = datetime.now; int timecost = (int)((end - start).totalmilliseconds);//获得接口耗时 //将xml格式的结果转换为对象以返回 wxpaydata result = new wxpaydata(); result.fromxml(response); reportcosttime(url, timecost, result);//测速上报 return result; }
生产环境中记得修改成自己的参数。如果参数都正确,将会返回:
而且,微信马上回收到退款通知:
小结:至此,退款功能已经走通,其实如果参数和流程对了,这个地方还是很简单的,微信的规定是可以申请一年内交易的退款。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ss3的渐变如何使用
jquery、angular、node中的promise详解
h5的视频播放库video.js详解
以上就是微信支付的退款功能开发的详细内容。
