李昊 4 gadi atpakaļ
vecāks
revīzija
3f8da44aba

+ 1 - 1
gx_api/GxPress/Api/GxPress.Api/WebControllers/AlipayController.cs

@@ -14,7 +14,7 @@ namespace GxPress.Api.WebControllers
         /// <summary>
         /// 回调地址
         /// </summary>
-        [HttpGet("callback")]
+        [HttpGet("notify")]
         [AllowAnonymous]
         public void Callback()
         {

+ 91 - 3
gx_api/GxPress/Api/GxPress.Api/WebControllers/WxpayController.cs

@@ -1,3 +1,7 @@
+using System;
+using System.Collections.Generic;
+using Alipay.AopSdk.Core.Util;
+using GxPress.Common.AliPay;
 using GxPress.Common.WechatPay;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
@@ -19,11 +23,95 @@ namespace GxPress.Api.WebControllers
         /// <summary>
         /// 回调地址
         /// </summary>
-        [HttpGet("callback")]
+        [HttpGet("notify")]
         [AllowAnonymous]
-        public string ProcessNotify()
+        public string Callback()
         {
-            return "true";
+            /* 实际验证过程建议商户添加以下校验。
+             1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
+             2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
+             3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
+             4、验证app_id是否为该商户本身。
+             */
+
+            Dictionary<string, string> sArray = GetRequestGet();
+            //检查openid和product_id是否返回
+            if (!sArray.Keys.Equals("openid") || !sArray.Keys.Equals("product_id"))
+            {
+                WxPayData res = new WxPayData();
+                res.SetValue("return_code", "FAIL");
+                res.SetValue("return_msg", "回调数据异常");
+                return (res.ToXml());
+            }
+
+            //调统一下单接口,获得下单结果
+            //string openid = notifyData.GetValue("openid").ToString();
+            sArray.TryGetValue("openid", out var openid);
+            //string product_id = notifyData.GetValue("product_id").ToString();
+            sArray.TryGetValue("product_id", out var product_id);
+            WxPayData unifiedOrderResult = new WxPayData();
+            try
+            {
+                unifiedOrderResult = GxPress.Common.WechatPay.WxPayApi.UnifiedOrder(openid, product_id);
+            }
+            catch (Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台
+            {
+                WxPayData res = new WxPayData();
+                res.SetValue("return_code", "FAIL");
+                res.SetValue("return_msg", "统一下单失败");
+                return (res.ToXml());
+            }
+
+            //若下单失败,则立即返回结果给微信支付后台
+            if (!unifiedOrderResult.IsSet("appid") || !unifiedOrderResult.IsSet("mch_id") || !unifiedOrderResult.IsSet("prepay_id"))
+            {
+                WxPayData res = new WxPayData();
+                res.SetValue("return_code", "FAIL");
+                res.SetValue("return_msg", "统一下单失败");
+                //Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml());
+                return (res.ToXml());
+                //page.Response.End();
+            }
+          
+            //统一下单成功,则返回成功结果给微信支付后台
+            WxPayData data = new WxPayData();
+            data.SetValue("return_code", "SUCCESS");
+            data.SetValue("return_msg", "OK");
+            data.SetValue("appid", WxPayConfig.GetConfig().GetAppID());
+            data.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID());
+            data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
+            data.SetValue("prepay_id", unifiedOrderResult.GetValue("prepay_id"));
+            data.SetValue("result_code", "SUCCESS");
+            data.SetValue("err_code_des", "OK");
+            data.SetValue("sign", data.MakeSign());
+
+            // Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + data.ToXml());
+            return data.ToXml();
+        }
+        private Dictionary<string, string> GetRequestPost()
+        {
+            Dictionary<string, string> sArray = new Dictionary<string, string>();
+
+            ICollection<string> requestItem = Request.Form.Keys;
+            foreach (var item in requestItem)
+            {
+                sArray.Add(item, Request.Form[item]);
+
+            }
+            return sArray;
+
+        }
+        private Dictionary<string, string> GetRequestGet()
+        {
+            Dictionary<string, string> sArray = new Dictionary<string, string>();
+
+            ICollection<string> requestItem = Request.Query.Keys;
+            foreach (var item in requestItem)
+            {
+                sArray.Add(item, Request.Query[item]);
+            }
+            return sArray;
+
         }
     }
 }

+ 18 - 5
gx_api/GxPress/Infrastructure/GxPress.Common/WechatPay/WxPayApi.cs

@@ -1,14 +1,27 @@
 using System;
-using System.Collections.Generic;
-using System.Web;
-using System.Net;
-using System.IO;
-using System.Text;
 
 namespace GxPress.Common.WechatPay
 {
     public class WxPayApi
     {
+
+        public static WxPayData UnifiedOrder(string openId,string productId)
+        {
+            //统一下单
+            WxPayData req = new WxPayData();
+            req.SetValue("body", "test");
+            req.SetValue("attach", "test");
+            req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
+            req.SetValue("total_fee", 1);
+            req.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
+            req.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
+            req.SetValue("goods_tag", "test");
+            req.SetValue("trade_type", "NATIVE");
+            req.SetValue("openid", openId);
+            req.SetValue("product_id", productId);
+            WxPayData result = WxPayApi.UnifiedOrder(req);
+            return result;
+        }
         /**
         * 提交被扫支付API
         * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,