コード例 #1
0
ファイル: PayController.cs プロジェクト: yanlipingyan/wechat
        /// <summary>
        /// 扫码支付回调URL---扫码支付模式一
        /// </summary>
        /// <returns></returns>
        public string CallbackUrl()
        {
            var result = Pay.GetNotifyData();

            if (string.IsNullOrEmpty(result["openid"].ToString()) || string.IsNullOrEmpty(result["product_id"].ToString()))
            {
                return(Common.SortedDictionaryToXml(Pay.ErrorInfo("回调数据异常")));
            }

            //商户根据productid【在生成二维码时传的是订单号就是订单号,是商品号就是商品号,要对应起来】生成商户系统的订单
            //:TODO

            //统一下单
            var orderParams = new SortedDictionary <string, object>();

            orderParams.Add("appid", ApiModel.AppID);
            orderParams.Add("attach", "微信扫码支付");
            orderParams.Add("body", "扫码支付一测试");
            orderParams.Add("mch_id", ApiModel.MchID);
            orderParams.Add("nonce_str", Common.GetNonceStr());
            orderParams.Add("notify_url", "http://www.liblog.cn/test/pay/payresultnotify"); //支付成功后的回调URl
            orderParams.Add("openid", result["openid"].ToString());
            orderParams.Add("product_id", result["product_id"].ToString());                 //trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
            orderParams.Add("out_trade_no", Pay.GetOutTradeNo(ApiModel.MchID));             //result["product_id"].ToString()
            orderParams.Add("spbill_create_ip", "171.8.215.143");
            orderParams.Add("total_fee", 1);
            orderParams.Add("trade_type", "NATIVE");
            orderParams.Add("sign", Pay.GetSign(orderParams, ApiModel.MchAPISecret));

            var orderResult = Pay.UnifiedOrder(orderParams);

            //统一下单失败,返回错误结果给微信平台
            if (orderResult["return_code"].ToString() != "SUCCESS")
            {
                return(Common.SortedDictionaryToXml(Pay.ErrorInfo("统一下单失败")));
            }

            //统一下单成功,则返回成功结果给微信支付后台
            var resultParams = new SortedDictionary <string, object>();

            resultParams.Add("return_code", "SUCCESS");
            resultParams.Add("return_msg", "OK");
            resultParams.Add("appid", ApiModel.AppID);
            resultParams.Add("mch_id", ApiModel.MchID);
            resultParams.Add("nonce_str", Common.GetNonceStr());
            resultParams.Add("prepay_id", orderResult["prepay_id"].ToString());
            resultParams.Add("result_code", "SUCCESS");
            resultParams.Add("err_code_des", "OK");
            resultParams.Add("sign", Pay.GetSign(resultParams, ApiModel.MchAPISecret));

            return(Common.SortedDictionaryToXml(resultParams));
        }
コード例 #2
0
ファイル: PayController.cs プロジェクト: yanlipingyan/wechat
        /// <summary>
        /// 扫码支付模式二生成二维码
        /// </summary>
        /// <param name="id">商品id</param>
        public void NativePayTwo(string id)
        {
            //商户根据productid【在生成二维码时传的是订单号就是订单号,是商品号就是商品号,要对应起来】生成商户系统的订单
            //:TODO

            //统一下单
            SortedDictionary <string, object> orderParams = new SortedDictionary <string, object>();

            orderParams.Add("appid", ApiModel.AppID);
            orderParams.Add("attach", "微信扫码支付"); //附加数据
            orderParams.Add("body", "扫码支付二测试");  //商品描述
            orderParams.Add("mch_id", ApiModel.MchID);
            orderParams.Add("nonce_str", Common.GetNonceStr());
            orderParams.Add("notify_url", "http://www.liblog.cn/test/pay/payresultnotify"); //支付成功后的回调URl
            orderParams.Add("product_id", id);                                              //商品标记
            orderParams.Add("out_trade_no", Pay.GetOutTradeNo(ApiModel.MchID));             //随机字符串
            orderParams.Add("spbill_create_ip", "171.8.215.143");
            orderParams.Add("total_fee", 1);                                                //总金额
            orderParams.Add("trade_type", "NATIVE");                                        //商品订单号
            orderParams.Add("sign", Pay.GetSign(orderParams, ApiModel.MchAPISecret));

            //直接支付URL
            var url = Pay.GetPayUrlForNativeTwo(orderParams);

            //初始化二维码生成工具
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

            qrCodeEncoder.QRCodeEncodeMode   = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            qrCodeEncoder.QRCodeVersion      = 0;
            qrCodeEncoder.QRCodeScale        = 4;

            //将字符串生成二维码图片
            Bitmap image = qrCodeEncoder.Encode(url, Encoding.Default);

            //保存为PNG到内存流
            MemoryStream ms = new MemoryStream();

            image.Save(ms, ImageFormat.Png);

            //输出二维码图片
            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
        }
コード例 #3
0
ファイル: PayController.cs プロジェクト: yanlipingyan/wechat
        public string JsPay(string title, string description, int price)
        {
            //统一下单
            SortedDictionary <string, object> orderParams = new SortedDictionary <string, object>();

            orderParams.Add("appid", ApiModel.AppID);
            orderParams.Add("attach", title);
            orderParams.Add("body", description);
            orderParams.Add("mch_id", ApiModel.MchID);
            orderParams.Add("nonce_str", Common.GetNonceStr());
            orderParams.Add("notify_url", "http://www.liblog.cn/test/pay/payresultnotify");
            orderParams.Add("openid", System.Web.HttpContext.Current.Request.Cookies["OpenId"].Value.ToString());
            orderParams.Add("out_trade_no", Pay.GetOutTradeNo(ApiModel.MchID));
            orderParams.Add("spbill_create_ip", "171.8.215.143");
            orderParams.Add("total_fee", price);
            orderParams.Add("trade_type", "JSAPI");
            orderParams.Add("sign", Pay.GetSign(orderParams, ApiModel.MchAPISecret));

            WechatLog.Info("", "1" + ApiModel.AppID + "2" + title + "3" + description + "4" + ApiModel.MchID + "5" + Common.GetNonceStr() + "6" + System.Web.HttpContext.Current.Request.Cookies["OpenId"].Value.ToString() + "7" + Pay.GetOutTradeNo(ApiModel.MchID) + "8" + Pay.GetSign(orderParams, ApiModel.MchAPISecret));
            var result = Pay.UnifiedOrder(orderParams);

            //统一下单失败,返回错误结果给微信平台
            if (result["return_code"].ToString() != "SUCCESS")
            {
                return(JsonConvert.SerializeObject(Pay.ErrorInfo("统一下单失败")));
            }

            //获取H5调起JS API参数
            SortedDictionary <string, object> apiParams = new SortedDictionary <string, object>();

            apiParams.Add("appId", ApiModel.AppID);
            apiParams.Add("timeStamp", Common.GetTimeStamp());
            apiParams.Add("nonceStr", Common.GetNonceStr());
            apiParams.Add("package", "prepay_id=" + result["prepay_id"].ToString());
            apiParams.Add("signType", "MD5");
            apiParams.Add("paySign", Pay.GetSign(apiParams, ApiModel.MchAPISecret));

            return(JsonConvert.SerializeObject(apiParams));
        }