/** * 生成扫描支付模式一URL * @param productId 商品ID * @return 模式一URL */ public string GetPrePayUrl(string productId) { WebApiConfig.log.Info(this.GetType().ToString(), "Native pay mode 1 url is producing..."); WxPayData data = new WxPayData(); data.SetValue("appid", WxPayConfig.APPID); //公众帐号id data.SetValue("mch_id", WxPayConfig.MCHID); //商户号 data.SetValue("time_stamp", WxPayApi.GenerateTimeStamp()); //时间戳 data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 data.SetValue("product_id", productId); //商品ID data.SetValue("sign", data.MakeSign()); //签名 string str = ToUrlParams(data.GetValues()); //转换为URL串 string url = "weixin://wxpay/bizpayurl?" + str; WebApiConfig.log.Info(this.GetType().ToString(), "Get native pay mode 1 url : " + url); return(url); }
/** * 调用统一下单,获得下单结果 * @return 统一下单结果 * @失败时抛异常WxPayException */ public static JObject GetUnifiedOrderResult(string out_trade_no, string total_fee, string subject) { //统一下单 WxPayData data = new WxPayData(); data.SetValue("body", subject); data.SetValue("out_trade_no", out_trade_no); data.SetValue("total_fee", total_fee); data.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//通知地址 data.SetValue("trade_type", "APP"); //data.SetValue("trade_type", "JSAPI"); WxPayData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || string.IsNullOrEmpty(result.GetValue("prepay_id").ToString())) { WebApiConfig.log.Error("微信统一下单出错!" + result.GetValue("return_msg") ?? string.Empty); throw new Exception("微信统一下单出错 !"); } WxPayData res = new WxPayData(); res.SetValue("prepayid", result.GetValue("prepay_id")); res.SetValue("appid", result.GetValue("appid")); res.SetValue("partnerid", result.GetValue("mch_id")); res.SetValue("package", "Sign=WXPay"); res.SetValue("noncestr", result.GetValue("nonce_str")); var timestamp = WxPayApi.GenerateTimeStamp(); res.SetValue("timestamp", timestamp); var sign = res.MakeSign(); var value = new JObject( new JProperty("prepay_id", result.GetValue("prepay_id")), new JProperty("appid", result.GetValue("appid")), new JProperty("partnerid", result.GetValue("mch_id")), new JProperty("package", "Sign=WXPay"), new JProperty("noncestr", result.GetValue("nonce_str")), new JProperty("timestamp", timestamp), new JProperty("sign", sign)); return(value); }