支付宝:当面付支付完成异步回调
Demo参考:当面付 DEMO - 支付宝开放平台 (alipay.com)
下载地址: https://gw.alipayobjects.com/os/bmw-prod/fe70c2d4-7ee9-410f-9a01-2617a9be392f.zip
代码:
C# 全选
[RoutePrefix("api/PayNotify")]
public class PayNotifyController : AliAdminTool.WebApi.ControllerBase.ControllerBase
{
    [HttpPost]
    public IHttpActionResult AliPayNotify()
    {
        SortedDictionary<string, string> sPara = GetRequestPost();
        if (sPara.Count > 0)//判断是否有带返回参数
        {
            //Notify aliNotify = new Notify();
            Notify aliNotify = new Notify(HttpContext.Current.Request.Form["charset"], HttpContext.Current.Request.Form["sign_type"], ConfigData.Intance.AliPaySetting.alipay_appID, "https://mapi.alipay.com/gateway.do", ConfigData.Intance.AliPaySetting.alipay_public_key);
            //对异步通知进行验签
            bool verifyResult = aliNotify.Verify(sPara, HttpContext.Current.Request.Form["notify_id"], HttpContext.Current.Request.Form["sign"]);
            //对验签结果
            //bool isSign = Aop.Api.Util.AlipaySignature.RSACheckV2(sPara, Config.alipay_public_key ,Config.charset,Config.sign_type,false );
            if (verifyResult) //验签成功 && 关键业务参数校验成功
            {
                ////商户订单号
                string out_trade_no = sPara["out_trade_no"];
                Req_QueryOrder req_data = new Req_QueryOrder()
                {
                    OrderID = out_trade_no
                };
                ModelRequestAction requestAction = new ModelRequestAction()
                {
                    Data = Newtonsoft.Json.JsonConvert.SerializeObject(req_data)
                };
                BLLPay bllPay = new BLLPay(requestAction);
                var obj = bllPay.QueryOrderBase();
                if (obj.IsPay == false)
                {
                    string body2 = Newtonsoft.Json.JsonConvert.SerializeObject(sPara);
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Headers:");
                    sb.AppendLine("\t" + Newtonsoft.Json.JsonConvert.SerializeObject(Request.Headers));
                    sb.AppendLine("Body:");
                    sb.AppendLine("\t" + body2);
                    sb.AppendLine("回调成功!但是业务处理失败,返回状态还是支付失败!");
                    Logs.Error("签名失败!", null, sb.ToString());
                }
                return Ok();
            }
            else//验证失败
            {
                // 记录日志
                string body2 = Newtonsoft.Json.JsonConvert.SerializeObject(sPara);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("收到支付宝回调:" + Request.RequestUri.ToString());
                sb.AppendLine("Headers:");
                sb.AppendLine("\t" + Newtonsoft.Json.JsonConvert.SerializeObject(Request.Headers));
                sb.AppendLine("Body2:");
                sb.AppendLine("\t" + body2);
                sb.AppendLine("验签结果:" + verifyResult);
                Logs.Info(sb.ToString());
                return BadRequest();
            }
        }
        else
        {
            //Response.Write("无通知参数");
            return Ok();
        }
    }
    /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
    /// </summary>
    /// <returns>request回来的信息组成的数组</returns>
    public SortedDictionary<string, string> GetRequestPost()
    {
        int i = 0;
        SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
        NameValueCollection coll;
        //Load Form variables into NameValueCollection variable.
        coll = HttpContext.Current.Request.Form;
        // Get names of all forms into a string array.
        String[] requestItem = coll.AllKeys;
        for (i = 0; i < requestItem.Length; i++)
        {
            sArray.Add(requestItem[i], HttpContext.Current.Request.Form[requestItem[i]]);
        }
        return sArray;
    }
}版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
OnlinePay 管理员  
