Jelajahi Sumber

新增 交易关闭异步通知 及相关示例 #81

Roc 5 tahun lalu
induk
melakukan
2465def340

+ 1 - 0
samples/WebApplicationSample/Controllers/AlipayController.cs

@@ -306,6 +306,7 @@ namespace WebApplicationSample.Controllers
 
             var req = new AlipayTradeCloseRequest();
             req.SetBizModel(model);
+            req.SetNotifyUrl(viewMode.NotifyUrl);
 
             var response = await _client.CertificateExecuteAsync(req, _optionsAccessor.Value);
             ViewData["response"] = response.Body;

+ 24 - 0
samples/WebApplicationSample/Controllers/AlipayNotifyController.cs

@@ -213,6 +213,30 @@ namespace WebApplicationSample.Controllers
             }
         }
 
+        /// <summary>
+        /// 交易关闭异步通知
+        /// </summary>
+        [Route("close")]
+        [HttpPost]
+        public async Task<IActionResult> Close()
+        {
+            try
+            {
+                var notify = await _client.CertificateExecuteAsync<AlipayTradeCloseNotify>(Request, _optionsAccessor.Value);
+                if (notify.TradeStatus == AlipayTradeStatus.Wait)
+                {
+                    Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
+
+                    return AlipayNotifyResult.Success;
+                }
+                return NoContent();
+            }
+            catch
+            {
+                return NoContent();
+            }
+        }
+
         private string MakeVerifyGWResponse(bool isSuccess, string certPublicKey, string appPrivateKey, string charset, string signType)
         {
             var xmlDoc = new XmlDocument(); //创建实例

+ 3 - 0
samples/WebApplicationSample/Models/AlipayViewModel.cs

@@ -175,6 +175,9 @@ namespace WebApplicationSample.Models
 
         [Display(Name = "trade_no")]
         public string TradeNo { get; set; }
+
+        [Display(Name = "notify_url")]
+        public string NotifyUrl { get; set; }
     }
 
     public class AlipayTransferViewModel

+ 4 - 0
samples/WebApplicationSample/Views/Alipay/Close.cshtml

@@ -20,6 +20,10 @@
             <div class="form-group">
                 <label asp-for="TradeNo"></label>
                 <input type="text" class="form-control" asp-for="TradeNo" />
+            </div>           
+            <div class="form-group">
+                <label asp-for="NotifyUrl"></label>
+                <input type="text" class="form-control" asp-for="NotifyUrl" value="http://*/alipay/notify/close" />
             </div>
             <button type="submit" class="btn btn-primary">提交请求</button>
         </form>

+ 132 - 0
src/Essensoft.AspNetCore.Payment.Alipay/Notify/AlipayTradeCloseNotify.cs

@@ -0,0 +1,132 @@
+using System.Text.Json.Serialization;
+
+namespace Essensoft.AspNetCore.Payment.Alipay.Notify
+{
+    /// <summary>
+    /// 统一收单交易关闭异步通知
+    /// 更新时间:2020-03-03
+    /// https://opendocs.alipay.com/apis/api_1/alipay.trade.close
+    /// </summary>
+    public class AlipayTradeCloseNotify : AlipayNotify
+    {
+        /// <summary>
+        /// 通知时间
+        /// </summary>
+        [JsonPropertyName("notify_time")]
+        public string NotifyTime { get; set; }
+
+        /// <summary>
+        /// 通知类型
+        /// </summary>
+        [JsonPropertyName("notify_type")]
+        public string NotifyType { get; set; }
+
+        /// <summary>
+        /// 通知校验ID
+        /// </summary>
+        [JsonPropertyName("notify_id")]
+        public string NotifyId { get; set; }
+
+        /// <summary>
+        /// 支付宝分配给开发者的应用Id
+        /// </summary>
+        [JsonPropertyName("app_id")]
+        public string AppId { get; set; }
+
+        /// <summary>
+        /// 授权方应用id
+        /// </summary>
+        [JsonPropertyName("auth_app_id")]
+        public string AuthAppId { get; set; }
+
+        /// <summary>
+        /// 编码格式
+        /// </summary>
+        [JsonPropertyName("charset")]
+        public string Charset { get; set; }
+
+        /// <summary>
+        /// 接口版本
+        /// </summary>
+        [JsonPropertyName("version")]
+        public string Version { get; set; }
+
+        /// <summary>
+        /// 签名类型
+        /// </summary>
+        [JsonPropertyName("sign_type")]
+        public string SignType { get; set; }
+
+        /// <summary>
+        /// 签名
+        /// </summary>
+        [JsonPropertyName("sign")]
+        public string Sign { get; set; }
+
+        /// <summary>
+        /// 支付宝交易号
+        /// </summary>
+        [JsonPropertyName("trade_no")]
+        public string TradeNo { get; set; }
+
+        /// <summary>
+        /// 商户订单号
+        /// </summary>
+        [JsonPropertyName("out_trade_no")]
+        public string OutTradeNo { get; set; }
+
+        /// <summary>
+        /// 买家支付宝用户号
+        /// </summary>
+        [JsonPropertyName("buyer_id")]
+        public string BuyerId { get; set; }
+
+        /// <summary>
+        /// 买家支付宝账号
+        /// </summary>
+        [JsonPropertyName("buyer_logon_id")]
+        public string BuyerLogonId { get; set; }
+
+        /// <summary>
+        /// 卖家支付宝用户号
+        /// </summary>
+        [JsonPropertyName("seller_id")]
+        public string SellerId { get; set; }
+
+        /// <summary>
+        /// 卖家支付宝账号
+        /// </summary>
+        [JsonPropertyName("seller_email")]
+        public string SellerEmail { get; set; }
+
+        /// <summary>
+        /// 交易状态
+        /// </summary>
+        [JsonPropertyName("trade_status")]
+        public string TradeStatus { get; set; }
+
+        /// <summary>
+        /// 订单金额
+        /// </summary>
+        [JsonPropertyName("total_amount")]
+        public string TotalAmount { get; set; }
+
+        /// <summary>
+        /// 订单标题
+        /// </summary>
+        [JsonPropertyName("subject")]
+        public string Subject { get; set; }
+
+        /// <summary>
+        /// 商品描述
+        /// </summary>
+        [JsonPropertyName("body")]
+        public string Body { get; set; }
+
+        /// <summary>
+        /// 交易创建时间
+        /// </summary>
+        [JsonPropertyName("gmt_create")]
+        public string GmtCreate { get; set; }
+    }
+}