Ejemplo n.º 1
0
        public string WebPay(string subject, string body, string notifyUrl, string returnUrl, PayLog payLog, Payment payment,
                             Dictionary <string, string> param = null)
        {
            //统一下单
            var config = _configService.Get <WeiXinConfig>();

            WxRequestHandler packageReqHandler = new WxRequestHandler(null);
            var nonceStr = TenPayV3Util.GetNoncestr();
            //商品Id,用户自行定义
            string productId = DateTime.Now.ToString("yyyyMMddHHmmss");

            //创建请求统一订单接口参数
            packageReqHandler.SetParameter("appid", config.AppId);
            packageReqHandler.SetParameter("mch_id", config.MchId);
            packageReqHandler.SetParameter("nonce_str", nonceStr);
            packageReqHandler.SetParameter("body", body);
            packageReqHandler.SetParameter("out_trade_no", payLog.TransactionNo);
            packageReqHandler.SetParameter("total_fee", Convert.ToInt32(payLog.OrderAmount * 100).ToString());
            packageReqHandler.SetParameter("spbill_create_ip", "127.0.0.1");
            packageReqHandler.SetParameter("notify_url", notifyUrl);
            packageReqHandler.SetParameter("trade_type", TenPayV3Type.NATIVE.ToString());
            packageReqHandler.SetParameter("product_id", productId);
            string sign = packageReqHandler.CreateMd5Sign("key", config.Key);

            packageReqHandler.SetParameter("sign", sign);
            string data = packageReqHandler.ParseXML();

            //调用统一订单接口
#pragma warning disable 618
            var result = TenPayV3.Unifiedorder(data);
#pragma warning restore 618
            var    unifiedorderRes = XDocument.Parse(result);
            string codeUrl         = unifiedorderRes.Element("xml")?.Element("code_url")?.Value;
            return(codeUrl);
        }
Ejemplo n.º 2
0
        public string Transfer(List <CrashApply> applys, Payment payment, string notifyUrl)
        {
            int successCount = 0;

            foreach (var crashApply in applys)
            {
                if (crashApply.ApplyState != ApplyState.ApplyPassed)
                {
                    Logger.Operation($"提现转账失败,扣除冻结金额处理失败,TransactionNo:{crashApply.TransactionNo},原因:提现转账申请状态不合法", PaymentProcessModule.Instance, Security.SecurityLevel.Danger);
                    continue;
                }
                //判断是否已经存在
                var oauth = _currencyService.GetSingleByConditon <UserOAuth>(
                    o => o.OAuthType == OAuthType.WeiXin && o.MemberId.Equals(crashApply.MemberId, StringComparison.OrdinalIgnoreCase));
                if (oauth == null)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        //其他操作
                        string error;

                        if (_walletService.Thaw(crashApply.MemberId, WalletType.Cash, crashApply.Money, "提现失败", out error))
                        {
                            crashApply.ApplyState = ApplyState.Failure;
                            _currencyService.Update(crashApply);
                            //提交
                            scope.Complete();
                        }
                        else
                        {
                            Logger.Operation($"提现转账失败,扣除冻结金额处理失败,TransactionNo:{crashApply.TransactionNo},原因:{error}", PaymentProcessModule.Instance, Security.SecurityLevel.Danger);
                        }
                    }
                    continue;
                }

                //创建支付应答对象
                WxRequestHandler packageReqHandler = new WxRequestHandler(HttpContext.Current);
                //初始化
                packageReqHandler.Init();

                var nonceStr = TenPayV3Util.GetNoncestr();
                //设置package订单参数
                packageReqHandler.SetParameter("mch_appid", AppId);                                                //公众账号ID
                packageReqHandler.SetParameter("mchid", MchId);                                                    //商户号
                packageReqHandler.SetParameter("nonce_str", nonceStr);                                             //随机字符串
                packageReqHandler.SetParameter("partner_trade_no", crashApply.TransactionNo);
                packageReqHandler.SetParameter("openid", oauth.OAuthId);
                packageReqHandler.SetParameter("check_name", "OPTION_CHECK");
                packageReqHandler.SetParameter("re_user_name", crashApply.RealName);
                packageReqHandler.SetParameter("amount", (crashApply.Money * 100).ToString());
                packageReqHandler.SetParameter("desc", "余额提现");
                packageReqHandler.SetParameter("spbill_create_ip", RemoteIP);

                string sign = packageReqHandler.CreateMd5Sign("key", Key);
                packageReqHandler.SetParameter("sign", sign);   //签名

                string data = packageReqHandler.ParseXML();
                Logger.Debug(data);
                var urlFormat = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

                var          formDataBytes = data == null ? new byte[0] : Encoding.UTF8.GetBytes(data);
                MemoryStream ms            = new MemoryStream();
                ms.Write(formDataBytes, 0, formDataBytes.Length);
                ms.Seek(0, SeekOrigin.Begin);//设置指针读取位置
                X509Certificate2 cer = new X509Certificate2($"{AppDomain.CurrentDomain.BaseDirectory}/Config/weixin_apiclient_cert.p12", MchId);

                try
                {
                    var result = RequestUtility.HttpPost(urlFormat, null, ms, timeOut: 10000, cer: cer, checkValidationResult: true);
                    //                    @"<xml>
                    //<return_code><![CDATA[SUCCESS]]></return_code>
                    //<return_msg><![CDATA[]]></return_msg>
                    //<mch_appid><![CDATA[wx8526f7a589c11f7f]]></mch_appid>
                    //<mchid><![CDATA[1393035002]]></mchid>
                    //<device_info><![CDATA[]]></device_info>
                    //<nonce_str><![CDATA[06138BC5AF6023646EDE0E1F7C1EAC75]]></nonce_str>
                    //<result_code><![CDATA[SUCCESS]]></result_code>
                    //<partner_trade_no><![CDATA[2016101312011511532]]></partner_trade_no>
                    //<payment_no><![CDATA[1000018301201610130371564966]]></payment_no>
                    //<payment_time><![CDATA[2016-10-13 19:48:11]]></payment_time>
                    //</xml>"

                    //                    @"<xml>
                    //<return_code><![CDATA[SUCCESS]]></return_code>
                    //<return_msg><![CDATA[付款金额不能小于最低限额.]]></return_msg>
                    //<result_code><![CDATA[FAIL]]></result_code>
                    //<err_code><![CDATA[AMOUNT_LIMIT]]></err_code>
                    //<err_code_des><![CDATA[付款金额不能小于最低限额.]]></err_code_des>
                    //</xml>"
                    var res           = XDocument.Parse(result);
                    var resultCodeXml = res.Element("xml")?.Element("result_code");
                    if (resultCodeXml != null && resultCodeXml.Value.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
                    {
                        successCount++;
                        //提交成功
                        crashApply.ApplyState   = ApplyState.Transferred;
                        crashApply.TransferTime = DateTime.Now;
                        crashApply.Description  = "微信提现成功";
                        _currencyService.Update(crashApply);
                        string error;
                        if (
                            !_walletService.Draw(crashApply.MemberId, WalletType.Cash, crashApply.Money, "提现支出",
                                                 out error, null, null, true))
                        {
                            Logger.Operation($"提现转账成功,扣除冻结金额处理失败,TransactionNo:{crashApply.TransactionNo},原因:{error}",
                                             PaymentProcessModule.Instance, Security.SecurityLevel.Danger);
                        }

                        _systemMessageService.CreatePushSystemMessage("提现打款", "您申请的提现已打款,请注意查收", "您申请的提现已打款,请注意查收",
                                                                      crashApply.MemberId, null, null, "CrashApply", WalletModule.Key, MessageCategory.Personal);
                    }
                    else
                    {
                        var returnMsgXml = res.Element("xml")?.Element("return_msg")?.Value;
                        Logger.Operation($"提现转账失败,TransactionNo:{crashApply.TransactionNo},原因:{returnMsgXml}", PaymentProcessModule.Instance, Security.SecurityLevel.Danger);
                    }

                    Logger.Debug(result);
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "微信企业付款");
                    Logger.Operation($"提现转账失败,TransactionNo:{crashApply.TransactionNo},未知异常:{ex.Message}", PaymentProcessModule.Instance, Security.SecurityLevel.Danger);

                    continue;
                }
            }

            var resultJson = new DataJsonResult();

            if (successCount < applys.Count)
            {
                resultJson.ErrorMessage = "部分条目处理失败,请查看系统日志";
            }
            resultJson.Success = true; //重置回True
            return(resultJson.ToJson());
        }