1. 程式人生 > >微信 支付寶

微信 支付寶

sig private ram var web char appid pub public

微信支付

https://github.com/842549829/Fund/blob/master/Weixin/CreateRequest.cs

/// <summary>
    /// 創建請求信息
    /// </summary>
    public class CreateRequest
    {
        #region web微信公眾號
        /// <summary>
        /// 創建支付信息(安卓 IOS)
        /// </summary>
        /// <param name="request">
request</param> /// <param name="config">配置</param> /// <returns>結果</returns> public static UnifiedOrderResult CreateDirectPayByUser(UnifiedOrderRequest request, Config config) { // 統一下單 var requestStr = TenpayUtil.GetUnifiedOrderXml(request, config);
var response = HttpHelp.PostXmlToUrl(Config.UnifiedPayUrl, requestStr); var unifiedOrderResult = TenpayUtil.ConvertToUnifiedOrderResult(response); // 調起支付 var parameters = TenpayUtil.GetPaySign(unifiedOrderResult, config); var result = TenpayUtil.CreatePayParameters(parameters, unifiedOrderResult);
return result; } /// <summary> /// 創建支付信息(網頁公眾號) /// </summary> /// <param name="request">request</param> /// <param name="config">配置</param> /// <returns>結果</returns> public static WapUnifiedOrderResult CreateDirectWapPayByUser(WapUnifiedOrderRequest request, Config config) { // 統一下單 var requestStr = TenpayUtil.GetUnifiedWapOrderXml(request, config); var response = HttpHelp.PostXmlToUrl(Config.UnifiedPayUrl, requestStr); var unifiedOrderResult = TenpayUtil.ConvertToWapUnifiedOrderResult(response); // 調起支付 var parameters = TenpayUtil.GetWapPaySign(unifiedOrderResult, config); var result = TenpayUtil.CreatePayParameters(parameters, unifiedOrderResult); return result; } ......

支付通知:https://github.com/842549829/Fund/blob/master/Notify/Controllers/TenpayNoticeController.cs

 /// <summary>
    /// 騰訊通知
    /// </summary>
    public class TenpayNoticeController : BaseController
    {
        /// <summary>
        /// 微信Sdk支付通知
        /// </summary>
        /// <returns>ContentResult</returns>
        public ContentResult PaySdkNotice()
        {
            try
            {
                var requestStr = GetRequestStr();
                var flg = Weixin.Notify.PayNotify(requestStr, new Weixin.Model.Config { /*微信配置信息*/ });
                if (flg.IsSuccess)
                {
                    // 處理業務
                    return new ContentResult { Content = GetResult(true, "OK") };
                }
                else
                {
                    return new ContentResult { Content = GetResult(false, flg.Message) };
                }
            }
            catch (Exception ex)
            {
                return new ContentResult { Content = GetResult(false, "微信支付通知異常") };
            }
        }

支付寶支付

老版本 :https://github.com/842549829/Fund/blob/master/AliPay/CreateRequest.cs

/// <summary>
    /// CreateRequest
    /// 航旅版 :加密參數需要Pid Key 
    /// 螞蟻金服 :全部
    /// </summary>    
    public class CreateRequest
    {
        /// <summary>
        /// 創建PC支付鏈接串(航旅版)
        /// </summary>
        /// <param name="directInfo">支付信息</param>
        /// <param name="config">config</param>
        /// <returns>請求連接</returns>
        public static string CreateDirectGetPcPayByUser(DirectInfo directInfo, Config config)
        {
            var partner = config.GetPid();
            Submit submit = new Submit(config);
            var sParaTemp = new SortedDictionary<string, string>
            {
                {"service", "create_direct_pay_by_user"},
                {"partner", partner},
                {"_input_charset", Config.InputCharset.ToLower()},
                {"payment_type", "1"},
                {"notify_url", directInfo.Notify},
                {"return_url", directInfo.Return},
                {"out_trade_no", directInfo.OutTradeNo},
                {"subject", directInfo.Subject},
                {"total_fee", directInfo.TotalFee.ToString("0.00")},
                {"show_url", directInfo.ShowUrl},
                {"seller_email", Config.SellerEmail},
            };
            string responseResult = submit.BuildRequest(sParaTemp);
            return responseResult;
        }
.................

支付通知:https://github.com/842549829/Fund/blob/master/Notify/Controllers/AliPayNoticeController.cs

/// <summary>
        /// 支付通知
        /// </summary>
        /// <returns>返回結果</returns>
        public ContentResult PayNotice()
        {
            
            var sPara = GetRequestPost();
            if (sPara.Count > 0)
            {
                // 簽名驗證
                var flg = AliPay.Notify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"], new AliPay.Model.Config{ });
                if (flg)
                {
                    //交易狀態
                    string trade_status = Request.Form["trade_status"];

                    //商戶訂單號
                    string out_trade_no = Request.Form["out_trade_no"];

                    //支付寶交易號
                    string trade_no = Request.Form["trade_no"];

                    //獲取總金額
                    decimal total_fee = Convert.ToDecimal(Request.Form["total_fee"]);

                    //買家支付寶賬號
                    //string accocunt = Request.Form["buyer_email"];

                    //買家支付寶賬號ID
                    string accountID = Request.Form["buyer_id"];

                    if (trade_status == "TRADE_SUCCESS")
                    {
                        // 處理業務
                        return new ContentResult { Content = "success" };
                    }
                    else
                    {
                        // 支付失敗的通知直接返回結果不再來通知
                        return new ContentResult { Content = "success" };
                    }
                }
                else//驗證失敗
                {
                    return new ContentResult { Content = "fail" };
                }
            }
            else
            {
                return new ContentResult { Content = "無通知參數" };
            }
        }

新版本:https://github.com/842549829/Fund/blob/master/AliPay/CreateRequest.cs

/// <summary>
        /// 創建Sdk支付鏈接串(螞蟻金服)
        /// </summary>
        /// <param name="directInfo">支付信息</param>
        /// <param name="config">config</param>
        /// <returns>請求連接</returns>
        public static string CreateDirectSdkPayByUser(DirectInfo directInfo, SdkPay.Config config)
        {
            const string url = SdkPay.Config.ServerUrl;
            string appId = config.GetAppId();
            string privateKeyPem = config.GetPrivateKeyPem();
            const string format = SdkPay.Config.Format;
            const string version = SdkPay.Config.Version;
            const string signType = SdkPay.Config.SignType;
            string publicKeyPem = config.GetPublicKeyPem();
            const string charset = SdkPay.Config.Charset;
            const string productCode = SdkPay.Config.ProductCode;
            string pid = config.GetPid();
            IAopClient client = new DefaultAopClient(url, appId, privateKeyPem, format, version, signType, publicKeyPem, charset, false);
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            if (!string.IsNullOrWhiteSpace(directInfo.Notify))
            {
                request.SetNotifyUrl(directInfo.Notify);
            }
            if (!string.IsNullOrWhiteSpace(directInfo.Return))
            {
                request.SetReturnUrl(directInfo.Return);
            }
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel
            {
                Body = directInfo.ExtraCommonParam,
                Subject = directInfo.Subject,
                TotalAmount = directInfo.TotalFee.ToString("0.00"),
                ProductCode = productCode,
                OutTradeNo = directInfo.OutTradeNo,
                TimeoutExpress = "30m",
                SellerId = pid
            };
            request.SetBizModel(model);
            AlipayTradeAppPayResponse response = client.SdkExecute(request);
            var body = response.Body;
            return body;
        }

....................

支付通知: https://github.com/842549829/Fund/blob/master/Notify/Controllers/AliPayNoticeController.cs

 /// <summary>
        /// 支付寶Sdk支付通知
        /// </summary>
        /// <returns>返回結果</returns>
        public ContentResult PaySdkNotice()
        {
            var sPara = GetRequestPost();
            if (sPara.Count > 0)
            {
                // 簽名驗證
                var flg = AliPay.Notify.VerifySdk(sPara, new AliPay.SdkPay.Config { });
                if (flg)
                {
                    //交易狀態
                    string trade_status = Request.Form["trade_status"];

                    //商戶訂單號
                    string out_trade_no = Request.Form["out_trade_no"];

                    //支付寶交易號
                    string trade_no = Request.Form["trade_no"];

                    //獲取總金額
                    decimal total_fee = Convert.ToDecimal(Request.Form["buyer_pay_amount"]);

                    // 訂單金額(用於獲取不到總金額的時候第二次驗證)
                    decimal orderAmount = Convert.ToDecimal(Request.Form["total_amount"]);

                    //買家支付寶賬號
                    string accocunt = Request.Form["buyer_logon_id"];

                    if (trade_status == "TRADE_SUCCESS")
                    {
                        try
                        {
                            if (total_fee <= 0)
                            {
                                total_fee = orderAmount;
                            }
                            // 處理業務
                            return new ContentResult { Content = "success" };
                        }
                        catch (Exception ex)
                        {
                            return new ContentResult { Content = "fail" };
                        }
                    }
                    else
                    {
                        // 支付失敗的通知直接返回結果不再來通知
                        return new ContentResult { Content = "success" };
                    }
                }
                else//驗證失敗
                {
                    return new ContentResult { Content = "fail" };
                }
            }
            else
            {
                return new ContentResult { Content = "無通知參數" };
            }
        }

退款請去github下載源代碼自己擼

github地址 :https://github.com/842549829/Fund

微信 支付寶