.Net Core 微信小程序支付、退款

近期做了一个商城类微信小程序,涉及支付方便查了一些资料,在此记录一下
一套基于.Net Core的开发支付SDK
引用包:Essensoft.AspNetCore.Payment.WeChatPay
示例链接
包含微信支付、支付宝支付
配置文件
在这里插入图片描述
Startup.cs 注入
在这里插入图片描述
请求参数参考
在这里插入图片描述
在这里插入图片描述

 		//微信统一下单
        public async Task<WeChatPayUnifiedOrderResponse> WeChatPay(string appId, string mchId, string subMchId,
            string openId, string body, string OutTradeNo , string url,
            DateTimeOffset startTime, DateTime expireTime, int totalFee, string key, string nonceStr, string ip,
            IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
        {
            try
            {
                //payment微信下单
                var request = new WeChatPayUnifiedOrderRequest
                {
                    Body = body,//描述
                    OutTradeNo = OutTradeNo ,
                    TotalFee = totalFee,
                    SpBillCreateIp = ip,
                    NotifyUrl = url,
                    TradeType = "JSAPI",
                    OpenId = openId,
                    TimeStart = startTime.ToString("yyyyMMddHHmmss"),
                    TimeExpire = expireTime.ToString("yyyyMMddHHmmss")
                };

                var response = await client.ExecuteAsync(request, optionsAccessor.Value);
                return response;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

		/// <summary>
        ///  微信退款
        /// </summary>
        /// <param name="OutRefundNo">商户退款单号</param> 
        /// <param name="TransactionId">微信支付订单号</param> 
        /// <param name="OutTradeNo">商户订单号</param>
        /// <param name="TotalFee">订单金额</param>
        /// <param name="RefundFee">退款金额</param>
        /// <param name="RefundDesc">退款原因</param>
        /// <param name="NotifyUrl">退款结果通知url</param>
        /// <param name="client"></param>
        /// <param name="optionsAccessor"></param>
        /// <returns></returns>
        public async Task<WeChatPayRefundResponse> Refund(string mchId,string OutRefundNo, string TransactionId,string OutTradeNo,
            int TotalFee,int RefundFee,string RefundDesc,string NotifyUrl,
            IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
        {
            try
            {
                OutRefundNo = GenerateOutTradeNo(mchId);
                var request = new WeChatPayRefundRequest
                {
                    OutRefundNo = OutRefundNo,
                    TransactionId = TransactionId,
                    OutTradeNo = OutTradeNo,
                    TotalFee = TotalFee,
                    RefundFee = RefundFee,
                    RefundDesc = RefundDesc,
                    NotifyUrl = NotifyUrl
                };
                var response = await client.ExecuteAsync(request, optionsAccessor.Value);
                _nlog.Info($"order code:{OutTradeNo}---return info:{response}");
                return response;
            }
            catch (Exception e)
            {
                _nlog.Error($"order code:{OutTradeNo}---ex:{e}");
                return null;
            }
        }
		public string GenerateOutTradeNo(string mchId)
        {
            var ran = new Random();
            return string.Format("{0}{1}{2}", mchId, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值