接下来就是微信外支付了,先是H5支付把。相关链接: H5支付
public class H5Pay
{
public string GetPayUrl(UnifiedOrderModel unifiedOrderModel, string thip)
{
//Log.Info(this.GetType().ToString(), "H5 pay url is producing...");
WxPayData data = new WxPayData();
data.SetValue("body", unifiedOrderModel.body);
data.SetValue("attach", unifiedOrderModel.attach);
data.SetValue("out_trade_no", unifiedOrderModel.out_trade_no);
data.SetValue("total_fee", unifiedOrderModel.total_fee);
data.SetValue("time_start", unifiedOrderModel.time_start);
data.SetValue("time_expire", unifiedOrderModel.time_expire);
data.SetValue("goods_tag", unifiedOrderModel.goods_tag);
//因H5支付要求商户在统一下单接口中上传用户真实ip地址“spbill_create_ip”
data.SetValue("spbill_create_ip", thip);
data.SetValue("trade_type", "MWEB");//交易类型
WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
if (!result.IsSet("mweb_url"))
{
throw new Exception("H5支付地址获取失败!");
}
string url = result.GetValue("mweb_url").ToString();//获得统一下单接口返回的链接
//Log.Info(this.GetType().ToString(), "Get H5 pay url : " + url);
return url;
}
public string GetIP(HttpRequest request)
{
if (request == null || request.ServerVariables == null)
{
return string.Empty;
}
string CustomerIP = string.Empty;
//CDN加速后取到的IP
CustomerIP = request.Headers["Cdn-Src-Ip"];
if (string.IsNullOrEmpty(CustomerIP))
{
CustomerIP = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
if (string.IsNullOrEmpty(CustomerIP))
{
if (request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(CustomerIP))
{
CustomerIP = request.ServerVariables["REMOTE_ADDR"];
}
}
else
{
CustomerIP = request.ServerVariables["REMOTE_ADDR"];
}
}
if (string.IsNullOrEmpty(CustomerIP))
{
CustomerIP = request.UserHostAddress;
}
if (string.IsNullOrEmpty(CustomerIP))
{
CustomerIP = "0.0.0.0";
}
return CustomerIP;
}
}
测试环境的时候,各种渗透,跳转,几乎获取的都是127.0.0.1 这样肯定就报 网络环境未能通过安全验证,请稍后再试 的问题了,当时是用搜狐的获取公网IP方法,要是有其他的用其他的也行,顺手就好。
<script src="http://pv.sohu.com/cityjson?ie=utf-8" type="text/javascript"></script>
alert(returnCitySN['cip'] + returnCitySN['cname']);
前端代码
function h5Pay() {
$.ajax({
type: "post",
url: "url",
dataType: "json",
data: {
"method": "GetH5PayUrl",
"body": $("#body").val(),
"attach": $("#attach").val(),
"out_trade_no": $("#out_trade_no").val(),
"total_fee": $("#total_fee").val(),
"goods_tag": $("#goods_tag").val(),
"ip": returnCitySN['cip']//测试环境后台获取不到真正的IP才可以使用,正式环境不允许
},
success: function (data) {
var item = eval(data); //转换成json对象访问
alert(data.url);
if (data.url) {
window.location.href = data.url;
} else {
alert(data.error);
}
return;
}
});
}
就遇到两个问题:
网络环境未能通过安全验证,请稍后再试 这个要传外网真实IP就行,写死或者调用外网方法也行。
请在微信外打开订单,进行支付 顾名思义了,要在微信外打开才行