微信支付回調通知收不到(不通知)的常見問題以及解決辦法

微信支付官方給出的解決方案

自己看吧,這個是最基本的問題排查,不贅述,上鍊接 官方解決方案 ↓↓↓↓↓↓↓↓
一定會往這裏看的,如果上面的就已經解決了,說明也太不認真了。

以下的方案包含網上和微信官網給出的一些方案總結,自己親測可用

常見問題

端口問題

阿里雲、騰訊雲、天翼雲等各種雲服務器或者自己的服務器防火牆請確認是否開啓了25端口。這個是很多人會忽略的。
具體開房的地址參考微信支付官方文檔文檔地址最下面的第三點

權限問題

開發webapi的時候經常會在基類裏面做了鑑權認證,導致回調接口也繼承了基類的鑑權,所以請把回調地址設置爲所有人都可以訪問,無需鑑權。

配置問題

在商戶上配置授權地址的時候一定是以 /結束的。比如 https://aaa.com/
回調地址儘量保持爲 https://aaa/com/回調方法,不要在增加深層目錄
錯誤示範: https://aaa.com/api/v2/回調方法
備註:沒有驗證過錯誤示範的是否能夠可以,但是正確示例一定可以。

webapi的配置問題

以.netcore爲例
.netcore 默認是支持context-type:json/application格式的。
但是微信支付V2版本的回調函數是以xml方式回調的,所以請配置webapi支持xml的解析。
參考代碼

using Microsoft.AspNetCore.Mvc.Formatters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Light.PhoneNumber.WebApi
{
    public class ResultTextFormatter : TextInputFormatter
    {
        public ResultTextFormatter()
        {
            SupportedMediaTypes.Insert(0, "text/plain");
            SupportedMediaTypes.Insert(0, "text/xml");
            SupportedEncodings.Add(Encoding.UTF8);
            SupportedEncodings.Add(Encoding.Default);
            SupportedEncodings.Add(Encoding.Unicode);
        }
        public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            using (var reader = context.ReaderFactory(context.HttpContext.Request.Body, encoding))
            {
                var stringContent = await reader.ReadToEndAsync();
                var type = context.ModelType;
                var contextType = context.HttpContext.Request.ContentType;
                if (contextType == "text/plain")
                {
                    return await Deserialize(stringContent, type);
                }
                else if (contextType == "text/xml" || context.HttpContext.Request.Path.Value.ToLower().Contains("/payed"))
                {
                    return await DeserializeXML(stringContent, type);
                }
                return await InputFormatterResult.FailureAsync();
            }
        }

        public static async Task<InputFormatterResult> DeserializeXML(string xml, Type type)
        {
            using (var reader = new StringReader(xml))
            {
                var serializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
                var result = serializer.Deserialize(reader);
                return await InputFormatterResult.SuccessAsync(result);
            }
        }

        private static async Task<InputFormatterResult> Deserialize(string stringContent, Type type)
        {
            var result = JsonConvert.DeserializeObject(stringContent, type);
            return await InputFormatterResult.SuccessAsync(result);
        }
    }
}

Startup.cs

     services.AddMvc(config =>
                {
                    config.InputFormatters.Add(new ResultTextFormatter());
                    config.Filters.Add<LogFilter>();

                }
             );

.netcore3.0+版本不支持同步導致無法回調

因爲上一個問題的寫法導致不支持同步會回調異常
處理辦法

 services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
              .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章