由于在支付前需要对数据库进行操作。所以我需要在数据操作完成后才进行支付。。
而我在后台使用两种方法来调用前台的callpay()方法,都无法使用。
ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>callpay();</script>");
Response.Write("<script type='text/javascript'>function callpay()</script>");
请问我该如何处理,采用什么方式才能从后台调用前台JS的callpay()方法。
我很笨,所以需要能清楚的指定,该如何处理。。。
我现在把两个文件的代码贴出来,请大家看看。。
pay.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pay.aspx.cs" Inherits="Sczhw.pay" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>眉山鑫本科技--易捷通-综合生活服务平台</title>
<meta name="viewport" content="width=640px, maximum-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="/scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="/scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="/scripts/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript" src="/scripts/jQuery.md5.js"></script>
<script type="text/javascript">
$(function () {
$(window).bind('resize load', function() {
$("body").css("zoom", $(window).width() / 640);
$("body").css("display", "block");
$("body").css("zoom", $(window).width() / 640);
});
})
</script>
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<%=wxJsApiParam%>,//josn串
function (res)
{
if (res.err_msg == "get_brand_wcpay_request:ok")
{
var OrderId=$("#OrderId").val();
var orderProductName=$("#orderProductName").val();
var orderMoneySum=$("#orderMoneySum").val();
window.location.href="http://msxinben.cn/fk.aspx?OrderId="+OrderId+"&orderMoneySum="+orderMoneySum+"&orderProductName="+orderProductName;//支付成功后的跳转页面
}else
{
WeixinJSBridge.call('closeWindow');
}
}
);
}
//function yzbd()
//{
// var i = 0;
// if(i == 0)
// {
// callpay();
// }
//}
function callpay()
{
if (typeof WeixinJSBridge == "undefined")
{
if (document.addEventListener)
{
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}
else if (document.attachEvent)
{
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}
else
{
jsApiCall();
}
}
</script>
<style type="text/css">
.font20{font-size:20px; width:300px;}
</style>
</head>
<body style="margin:0 0 0 0;">
<form id="form1" runat="server">
<div style="width:640px; height:308px; background:url(/images/newhead.jpg);"></div>
<div style="width:640px;">
<table style="font-size:14px; width:640px; vertical-align:middle; font-size:20px; font-weight:bold;">
<tr style="height:30px;">
<td style="text-align:right;">公司名称:</td>
<td style="text-align:left;"><asp:TextBox runat="Server" ID="companyText" CssClass="font20"></asp:TextBox>(必填)</td>
</tr>
<tr style="height:20px;">
<td colspan="2"><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="companyText"
ErrorMessage="请输入公司名称!"></asp:RequiredFieldValidator></td>
</tr>
<tr style="height:30px;">
<td style="text-align:right;">纳税人识别号:</td>
<td style="text-align:left;"><asp:TextBox runat="Server" ID="noText" CssClass="font20"></asp:TextBox>(必填)</td>
</tr>
<tr style="height:20px;">
<td colspan="2"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="noText"
ErrorMessage="请输入纳税人识别号!"></asp:RequiredFieldValidator></td>
</tr>
<tr style="height:30px;">
<td style="text-align:right;">联系人:</td>
<td style="text-align:left;"><asp:TextBox runat="Server" ID="menText" CssClass="font20"></asp:TextBox>(必填)</td>
</tr>
<tr style="height:20px;">
<td colspan="2"><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="menText"
ErrorMessage="请输入联系人!"></asp:RequiredFieldValidator></td>
</tr>
<tr style="height:30px;">
<td style="text-align:right;">联系人手机号码:</td>
<td style="text-align:left;"><asp:TextBox runat="Server" ID="menphoneText" CssClass="font20"></asp:TextBox>(必填)</td>
</tr>
<tr style="height:20px;">
<td colspan="2"><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="menphoneText"
ErrorMessage="请输入联系人电话!"></asp:RequiredFieldValidator></td>
</tr>
<tr style="height:30px;">
<td style="text-align:right;">联系人邮箱:</td>
<td style="text-align:left;"><asp:TextBox runat="Server" ID="emailText" CssClass="font20"></asp:TextBox></td>
</tr>
<tr style="background-image:url(/images/midbg.jpg); height:259px;">
<td colspan="2"></td>
</tr>
<tr>
<td style="text-align:center;" colspan="2"><asp:ImageButton ImageUrl="/images/quedingbtn.jpg" runat="server" ID="submitbtn" OnClick="submitBtn_Click" AlternateText="确定" /></td>
</tr>
</table>
</div>
<div style="width:640px; height:77px; background:url(/images/newfoot.jpg);"></div>
</form>
</body>
</html>
pay.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
namespace Sczhw
{
public partial class pay : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
JsApiPay jsApiPay = new JsApiPay(this);
try
{
string total_fee = "0.01";// Request["total_fee"];
orderMoneySum = total_fee;
string ParkName = "服务费";// Request["ParkName"];
orderProductName = ParkName+"停车费用";
OrderId = "145448545";// Request["OrderId"];
if (string.IsNullOrWhiteSpace(total_fee)||total_fee=="0")
{
throw new WxPayException("<span style='color:#FF0000;font-size:20px'>" + "费用为零,请求参数错误" + "</span>");
}
jsApiPay.total_fee =int.Parse((Convert.ToDouble(total_fee)*100).ToString());
Money = (Convert.ToDouble(jsApiPay.total_fee)/100).ToString();
jsApiPay.orderid = OrderId;
//JSAPI支付预处理
try
{
Common common = new Common(Context);
jsApiPay.openid = common.GetOpenId();
if (Common.OpenId == "Openid")
{
throw new WxPayException("OpenId为空无法下单!");
}
jsApiPay.access_token = Common.access_token;
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(ParkName);//ParkName不能是空,这是一个大坑
wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数
}
catch (Exception ex)
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试:" + ex.InnerException.Message + "</span>");
}
}
catch (Exception ex)
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试:" + ex.Message + "</span>");
}
}
}
public string wxJsApiParam { get; set; } //H5调起JS API参数
public string Money { get; set; }
public string OrderId { get; set; }
public string orderMoneySum { get; set; }//商品金额
public string orderProductName { get; set; }//商品名称
protected void submitBtn_Click(object sender, ImageClickEventArgs e)
{
Session["companyVar"] = companyText.Text.ToString();
Session["noVar"] = noText.Text.ToString();
Session["menVar"] = menText.Text.ToString();
Session["menphoneVar"] = menphoneText.Text.ToString();
Session["emailVar"] = emailText.Text.ToString();
Session["dateVar"] = DateTime.Now.ToString();
//ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>callpay();</script>");
Response.Write("<script type='text/javascript'>function callpay()</script>");
}
}
}