Esempio n. 1
0
        public void WeiXinPay()
        {
            M_Payment payMod = payBll.SelModelByPayNo(PayNo);

            if (string.IsNullOrEmpty(PayNo))
            {
                function.WriteErrMsg("0x53,支付单号或为空");
            }
            else if (payMod == null)
            {
                function.WriteErrMsg("支付单号不存在");
            }
            else if (payMod.Status != (int)M_Payment.PayStatus.NoPay)
            {
                function.WriteErrMsg("0x14,支付单已付过款,不能重复支付");
            }
            else if (payMod.MoneyReal <= 0)
            {
                function.WriteErrMsg("0x56,支付单金额异常");
            }

            string  code      = Request.QueryString["code"];
            string  resultStr = APIHelper.GetWebResult("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appMod.APPID + "&secret=" + appMod.Secret + "&code=" + code + "&grant_type=authorization_code");
            JObject obj       = (JObject)JsonConvert.DeserializeObject(resultStr);

            WxPayData wxdata = new WxPayData();

            wxdata.SetValue("out_trade_no", payMod.PayNo);
            wxdata.SetValue("body", payMod.Remark);
            wxdata.SetValue("total_fee", (int)(payMod.MoneyReal * 100));
            wxdata.SetValue("trade_type", "JSAPI");
            wxdata.SetValue("notify_url", notifyUrl);
            wxdata.SetValue("product_id", "123");                //必填
            wxdata.SetValue("openid", obj["openid"].ToString()); //公众号支付必填
            wxdata.SetValue("nonce_str", WxAPI.nonce);

            //获取预支付单号
            WxPayData result = WxPayApi.UnifiedOrder(wxdata, appMod);

            if (result.GetValue("return_code").ToString().Equals("FAIL"))
            {
                function.WriteErrMsg("商户" + result.GetValue("return_msg"));
            }

            prepay_id = result.GetValue("prepay_id").ToString();
            payBll.UpdatePlat(payMod.PaymentID, M_PayPlat.Plat.WXPay, appMod.ID.ToString());
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        M_APIResult retMod = new M_APIResult();

        retMod.retcode  = M_APIResult.Failed;
        retMod.callback = Request["callback"];
        if (string.IsNullOrEmpty(PayNo) || string.IsNullOrEmpty(OpenID))
        {
            retMod.retmsg = "0x53,支付单号或OpenID为空"; RepToClient(retMod);
        }
        try
        {
            M_UserInfo mu     = B_User_API.GetLogin(OpenID);
            M_Payment  payMod = payBll.SelModelByPayNo(PayNo);
            if (mu == null)
            {
                retMod.retmsg = "0x59,用户未登录,或登录已失效";
            }
            else if (payMod == null)
            {
                retMod.retmsg = "0x82,支付单号不存在";
            }
            else if (payMod.Status != (int)M_Payment.PayStatus.NoPay)
            {
                retMod.retmsg = "0x14,支付单已付过款,不能重复支付";
            }
            else if (payMod.MoneyReal <= 0)
            {
                retMod.retmsg = "0x56,支付单金额异常";
            }
            //else if (payMod.UserID != mu.UserID) { retMod.retmsg = "x058,该支付单并非你所有"; }
            else
            {
                //*不要看程序内部的报错,直接看Repsonse返回,其才是真正的报错
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                timestr = Convert.ToInt64(ts.TotalSeconds).ToString();
                //------------统一下单,获取预付单号
                WxPayData wxdata = new WxPayData(PlatConfig.WXPay_Key);
                wxdata.SetValue("appid", PlatConfig.WXPay_APPID);
                wxdata.SetValue("attach", "attach"); //test
                wxdata.SetValue("body", "pay");      //不允许为空,中文需要单独处理
                wxdata.SetValue("mch_id", PlatConfig.WXPay_MCHID);
                wxdata.SetValue("nonce_str", nonceStr);
                wxdata.SetValue("notify_url", notifyUrl);
                wxdata.SetValue("out_trade_no", PayNo);                                //支付单号
                wxdata.SetValue("spbill_create_ip", ip);                               //Can be empty
                wxdata.SetValue("total_fee", (payMod.MoneyReal * 100).ToString("f0")); //(payMod.MoneyReal * 100).ToString("f0")
                wxdata.SetValue("trade_type", "APP");

                WxPayData result = UnifiedOrder(wxdata);
                //if (result.GetValue("return_code") != "SUCCESS") { retMod.retmsg = result.GetValue("return_msg").ToString(); RepToClient(retMod); }
                //------------生成返回给APP端的数据
                timestr = Convert.ToInt64(ts.TotalSeconds).ToString();
                WxPayData appdata = new WxPayData(PlatConfig.WXPay_Key);
                appdata.SetValue("appid", PlatConfig.WXPay_APPID);
                appdata.SetValue("partnerid", PlatConfig.WXPay_MCHID);
                appdata.SetValue("package", "Sign=WXPay");
                appdata.SetValue("noncestr", nonceStr);
                appdata.SetValue("timestamp", timestr);
                appdata.SetValue("prepayid", result.GetValue("prepay_id").ToString());
                appdata.SetValue("sign", appdata.MakeSign());

                retMod.result  = "{\"appid\":\"" + PlatConfig.WXPay_APPID + "\",\"partnerid\":\"" + PlatConfig.WXPay_MCHID + "\",\"package\":\"Sign=WXPay\",\"noncestr\":\"" + nonceStr + "\",\"timestamp\":" + timestr + ",\"prepayid\":\"" + result.GetValue("prepay_id").ToString() + "\",\"sign\":\"" + appdata.GetValue("sign") + "\"}";
                retMod.retcode = M_APIResult.Success;
                ZLLog.L("生成微信预付单完成" + retMod.result);
                payBll.UpdatePlat(payMod.PaymentID, M_PayPlat.Plat.WXPay, appMod.ID.ToString());
            }
        }
        catch (Exception ex) { retMod.retcode = M_APIResult.Failed; retMod.retmsg = "server exception:" + ex.Message; ZLLog.L("微信预付单报错" + retMod.retmsg); }
        RepToClient(retMod);
        //正确返回
        //<xml><return_code><![CDATA[SUCCESS]]></return_code>
        //<return_msg><![CDATA[OK]]></return_msg>
        //<appid><![CDATA[wxe4703a7618738bed]]></appid>
        //<mch_id><![CDATA[1342097701]]></mch_id>
        //<nonce_str><![CDATA[LYJFMZbGIaa2LezS]]></nonce_str>
        //<sign><![CDATA[440C7616B9658FD7102805895E6808CC]]></sign>
        //<result_code><![CDATA[SUCCESS]]></result_code>
        //<prepay_id><![CDATA[wx201605160903248f02bc5ab10976835276]]></prepay_id>
        //<trade_type><![CDATA[APP]]></trade_type>
        //</xml>
        //转成Json回发给APP
        //{"appid":"wxb4ba3c02aa476ea1","partnerid":"1305176001","package":"Sign=WXPay","noncestr":"717cbf90679a1f26d6b8211efaed3ffb","timestamp":1463394698,"prepayid":"wx20160516183138c3ac06576e0471509227","sign":"414C383FA311DAC74DC882BA89A5E296"}
    }