如何在VB ASP.NET Stripe Checkout中动态更新<script>标签属性
解决方案:后端动态更新Stripe Checkout脚本属性
以下方案适配你使用ASP.NET Web Form控件的场景,可直接落地:
步骤1:后端先完成查询字符串参数赋值
在页面Page_Load事件中先获取查询字符串值,赋值给对应Label控件,同时完成Stripe要求的金额单位转换(Stripe金额单位为对应货币的最小单位,比如人民币是分、美元是美分):
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 处理金额参数 if (Request.QueryString["amount"] != null) { // 示例逻辑:查询字符串传值为元单位,转成Stripe要求的分单位 decimal originAmount = decimal.Parse(Request.QueryString["amount"]); Label1.Text = ((int)(originAmount * 100)).ToString(); } // 加兜底默认值,避免参数为空报错 else { Label1.Text = "500"; } // 处理描述参数 if (Request.QueryString["description"] != null) { Label2.Text = Request.QueryString["description"]; } // 加兜底默认值 else { Label2.Text = "Sample Charge"; } } }
步骤2:直接绑定属性到Stripe脚本
修改你原有脚本代码,将data-amount和data-description的值替换为Label控件的Text属性绑定即可:
<script src="https://checkout.stripe.com/checkout.js" id="PayScript" class="stripe-button" data-key="<%= stripePublishableKey %>" data-amount="<%= Label1.Text %>" data-name="Stripe.com" data-description="<%= Label2.Text %>" data-locale="auto" data-zip-code="true"> </script>
可选优化:如果需要对属性做更复杂的逻辑判断,可给script标签加
runat="server"标识,直接在后端代码给脚本设置属性:PayScript.Attributes["data-amount"] = Label1.Text; PayScript.Attributes["data-description"] = Label2.Text;
内容的提问来源于stack exchange,提问作者Sajid H. A. Rashid
相关产品推荐
相关产品推荐

