/// <summary> /// 退款入口 /// </summary> /// <param name="para"></param> /// <returns></returns> public override RefundFeeReturnModel ProcessRefundFee(PaymentPara para) { //退款网关 string gateway = "https://mapi.alipay.com/gateway.do"; //数据初始 RefundFeeReturnModel paymentInfo = new RefundFeeReturnModel(); paymentInfo.RefundResult = RefundState.Failure; paymentInfo.RefundMode = RefundRunMode.Async; string refund_batch_no = para.out_refund_no; string strResult = string.Empty; Config _config = GetConfig(); if (_config.Sign_type.ToLower() == "md5") { //多种签名机制参与参数不一样 if (string.IsNullOrEmpty(_config.Key)) { throw new PluginException("未设置Key"); } } else { if (string.IsNullOrEmpty(_config.Private_key)) { throw new PluginException("未设置私钥"); } } Dictionary <string, string> dicPara = new Dictionary <string, string>(); //整理基础数据 dicPara.Add("service", "refund_fastpay_by_platform_pwd"); //服务固定,退款 dicPara.Add("partner", _config.Partner); //合作者ID,支付宝提供 dicPara.Add("_input_charset", _config.Input_charset); dicPara.Add("notify_url", para.notify_url); //整理业务数据 dicPara.Add("seller_user_id", _config.Partner); dicPara.Add("refund_date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); dicPara.Add("batch_no", refund_batch_no); dicPara.Add("batch_num", "1"); dicPara.Add("detail_data", para.pay_trade_no.Trim() + "^" + para.refund_fee.ToString("F2").Trim() + "^平台协商退款".Trim()); //要去除非格式时用“^”、“|”、“$”、“#” string refundurl = Submit.BuildRequestUrl(dicPara, _config, gateway); paymentInfo.RefundResult = RefundState.Success; paymentInfo.ResponseContentWhenFinished = refundurl; paymentInfo.RefundNo = refund_batch_no; return(paymentInfo); }
public override RefundFeeReturnModel ProcessRefundFee(PaymentPara para) { //创建请求对象 RequestHandler reqHandler = new RequestHandler(); RefundFeeReturnModel paymentInfo = new RefundFeeReturnModel(); paymentInfo.RefundMode = RefundRunMode.Sync; paymentInfo.RefundResult = RefundState.Failure; string strResult = string.Empty; Config payConfig = Utility <Config> .GetConfig(WorkDirectory); if (string.IsNullOrEmpty(payConfig.AppId)) { throw new PluginException("未设置AppId"); } if (string.IsNullOrEmpty(payConfig.MCHID)) { throw new PluginException("未设置MCHID"); } if (string.IsNullOrWhiteSpace(payConfig.pkcs12)) { throw new PluginConfigException("未设置商户证书"); } //----------------------------- //设置请求参数 //----------------------------- reqHandler.SetKey(payConfig.Key); var nonceStr = TenPayUtil.GetNoncestr(); reqHandler.SetParameter("out_trade_no", para.out_trade_no); reqHandler.SetParameter("out_refund_no", para.out_refund_no); reqHandler.SetParameter("total_fee", Convert.ToInt32((para.total_fee * 100)).ToString()); reqHandler.SetParameter("refund_fee", Convert.ToInt32((para.refund_fee * 100)).ToString()); reqHandler.SetParameter("op_user_id", payConfig.MCHID); reqHandler.SetParameter("appid", payConfig.AppId); reqHandler.SetParameter("mch_id", payConfig.MCHID); reqHandler.SetParameter("nonce_str", nonceStr); //从哪个帐号退款(余额、未结算帐号) reqHandler.SetParameter("refund_account", REFUND_SOURCE_RECHARGE_FUNDS); //REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款/基本账户 string sign = reqHandler.CreateMd5Sign("key", payConfig.Key); //按约定规则生成MD5,规则参考接口文档 reqHandler.SetParameter("sign", sign); var pkcs12 = WorkDirectory + "\\" + payConfig.pkcs12; if (!System.IO.File.Exists(pkcs12)) { throw new PluginException("未找到商户证书文件"); } string strXml = reqHandler.ParseXML(); string result = string.Empty; try { result = TenPayV3.Refund(strXml, pkcs12, payConfig.MCHID);//调用统一接口 } catch (Exception ex) { throw new PluginException("原路返回退款时出错:" + ex.Message); } XDocument xmlDocument = XDocument.Parse(result); if (xmlDocument == null) { throw new PluginException("原路返回退款时出错:" + strXml); } XElement e_return = xmlDocument.Element("xml").Element("return_code"); XElement e_result = xmlDocument.Element("xml").Element("return_msg"); if (e_return == null) { throw new PluginException("原路返回退款时,返回参数异常"); } //处理返回时先判断协议错误码,再业务,最后交易状态 if (e_return.Value == "SUCCESS") { e_result = xmlDocument.Element("xml").Element("result_code"); XElement e_errdes = xmlDocument.Element("xml").Element("err_code_des"); if (e_result.Value == "SUCCESS") { //微信退款单号 string refund_id = xmlDocument.Element("xml").Element("refund_id").Value; //商户退款单号 string out_refund_no = xmlDocument.Element("xml").Element("out_refund_no").Value; //业务处理 paymentInfo.RefundResult = RefundState.Success; paymentInfo.OrderId = long.Parse(out_refund_no); paymentInfo.RefundNo = refund_id; paymentInfo.RefundTime = DateTime.Now; } else { throw new PluginException("原路返回退款时,接口返回异常:" + e_errdes.Value); } } else { throw new PluginException("原路返回退款时,接口返回异常:" + e_result.Value); } return(paymentInfo); }