c#版在pc端发起微信扫码支付的实例

2019-12-30 14:45:56于海丽

 不使用代理要注释HttpService.cs里面post和get方法的下面代码:
 


//设置代理服务器
    //WebProxy proxy = new WebProxy();       //定义一个网关对象
    //proxy.Address = new Uri(Config.PROXY_URL);    //网关服务器端口:端口
    //request.Proxy = proxy;

统一下单:


/**
  * 
  * 统一下单
  * @param WxPaydata inputObj 提交给统一下单API的参数
  * @param int timeOut 超时时间
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
  public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)
  {
   string url = "https://www.easck.com/pay/unifiedorder";
   //检测必填参数
   if (!inputObj.IsSet("out_trade_no"))
   {
    throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
   }
   else if (!inputObj.IsSet("body"))
   {
    throw new WxPayException("缺少统一支付接口必填参数body!");
   }
   else if (!inputObj.IsSet("total_fee"))
   {
    throw new WxPayException("缺少统一支付接口必填参数total_fee!");
   }
   else if (!inputObj.IsSet("trade_type"))
   {
    throw new WxPayException("缺少统一支付接口必填参数trade_type!");
   }

   //关联参数
   if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
   {
    throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
   }
   if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
   {
    throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
   }

   //异步通知url未设置,则使用配置文件中的url
   if (!inputObj.IsSet("notify_url"))
   {
    inputObj.SetValue("notify_url", Config.NOTIFY_URL);//异步通知url
   }

   inputObj.SetValue("appid", Config.APPID);//公众账号ID
   inputObj.SetValue("mch_id", Config.MCHID);//商户号
   inputObj.SetValue("spbill_create_ip", Config.IP);//终端ip    
   inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串

   //签名
   inputObj.SetValue("sign", inputObj.MakeSign());
   string xml = inputObj.ToXml();

   var start = DateTime.Now;

   Log.Debug("WxPayApi", "UnfiedOrder request : " + xml);
   string response = HttpService.Post(xml, url, false, timeOut);
   Log.Debug("WxPayApi", "UnfiedOrder response : " + response);

   var end = DateTime.Now;
   int timeCost = (int)((end - start).TotalMilliseconds);

   WxPayData result = new WxPayData();
   result.FromXml(response);

   ReportCostTime(url, timeCost, result);//测速上报

   return result;
  }