private static Dictionary <string, string> EntityToDic(VCodePayEntity wei) { Dictionary <string, string> dic = new Dictionary <string, string>(); foreach (System.Reflection.PropertyInfo v in wei.GetType().GetProperties()) { string str = ""; try { if (v.Name.ToLower() == "total_fee") { if (v.GetValue(wei, null).ToString() == "0") { str = ""; } else { str = v.GetValue(wei, null).ToString(); } } else { str = v.GetValue(wei, null).ToString(); } } catch { } if (!string.IsNullOrWhiteSpace(str)) { dic.Add(v.Name.ToLower(), str); } } return(dic); }
/// <summary> /// 创建微信支付 /// </summary> /// <param name="wei"></param> /// <returns></returns> public static VCodePayResponsEntity CreateWeixinPay(VCodePayEntity wei) { Dictionary <string, string> dic = EntityToDic(wei); string sign = GetResultSign(dic, null); dic.Add("sign", sign); string reqtest = BuildXml(dic, false); string msg; VCodePayResponsEntity post = PostData(prepay_id_Url, reqtest, out msg); return(post); }
/// <summary> /// 创建二维码支付请求 返回二维码图片地址 返回"":创建失败 /// </summary> /// <param name="wei">实体</param> /// <returns>返回二维码图片地址 返回"":创建失败</returns> public static string RequestWeixin(VCodePayEntity wei) { Dictionary <string, string> dic = EntityToDic(wei); string sign = GetResultSign(dic, null); dic.Add("sign", sign); string reqtest = BuildXml(dic, false); string msg; VCodePayResponsEntity post = null; int loop = 0; do { post = PostData(prepay_id_Url, reqtest, out msg); if (post != null) { break; } loop++; System.Threading.Thread.Sleep(500); } while (loop < 5); if (post == null) { return(msg); } try { CreateQrcode(post); return(post.sign + ".jpg"); } catch (Exception) { } return(""); }
/// <summary> /// 创建支付二维码(路径) /// </summary> /// <param name="orderInfo">订单信息</param> /// <param name="appId"></param> /// <param name="partnerId"></param> /// <returns></returns> private string CreatePayQRCode(OrderInfo orderInfo, string appId, string partnerId) { string orderId = orderInfo.OrderId; VCodePayEntity vCodePayEntity = new VCodePayEntity(); vCodePayEntity.appid = appId; vCodePayEntity.mch_id = partnerId; vCodePayEntity.body = orderId; vCodePayEntity.nonce_str = VCodePayHelper.CreateRandom(20); vCodePayEntity.out_trade_no = orderId; vCodePayEntity.fee_type = "CNY"; //1 = 1分 vCodePayEntity.total_fee = (int)(orderInfo.GetTotal() * 100); vCodePayEntity.spbill_create_ip = "127.0.0.1"; vCodePayEntity.notify_url = "http://" + base.Context.Request.Url.Host + "/pay/wx_PayQRcode_notify_url.aspx"; vCodePayEntity.product_id = orderId; string vCodePayPath = VCodePayHelper.RequestWeixin(vCodePayEntity); return(vCodePayPath); }