微信支付官方給出的解決方案
自己看吧,這個是最基本的問題排查,不贅述,上鏈接 官方解決方案 ↓↓↓↓↓↓↓↓
一定會往這里看的,如果上面的就已經解決了,說明也太不認真了。
以下的方案包含網上和微信官網給出的一些方案總結,自己親測可用
常見問題
端口問題
阿里雲、騰訊雲、天翼雲等各種雲服務器或者自己的服務器防火牆請確認是否開啟了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);