在 PayPal 支持和大量谷歌搜索示例代码的帮助下,我想出了以下内容,这些内容似乎在 beta 版中运行良好。keydown 逻辑来自 StackOverflow 上的另一个问题。
该代码为成人和学生数量创建了一对文本框。当客户输入数量以在输入上方显示小计字符串时,就会执行折扣规则。当客户点击“立即购买”时,总额将作为单个项目发送以创建付款表单。S&H 费用也被添加为总额的百分比,以支付交易成本。
为冗长的编码风格道歉。JavaScript 不是我常用的语言。欢迎提出改进建议。
希望有人觉得它有用!
警告——此代码很容易被具有计算机技能的骗子作弊。不要将其用于没有某种形式的购买后审计的有价值的交易。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><script type="text/javascript">// <![CDATA[
$(document).ready(function() {
// Enforce numeric values only.
$('#nadult, #nstudent').keydown(function(event) {
// Allow: backspace, delete, tab, escape, and enter
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
event.preventDefault();
}
}
});
$('#nadult, #nstudent').keyup(function(event) {
compute_form_inputs(false);
});
});
function compute_form_inputs(is_submit) {
// called when buyer click "Buy now" after filling in quantities of adult and student tickets
var base_adult_price = 20.00;
var disc_adult_price = 16.50;
var student_discount = 0.50;
var handling_fee = 0.05 // Five percent to cover cost of transaction;
var nadult = $('#nadult').val();
var nstudent = $('#nstudent').val();
var iadult = parseInt(nadult);
if (isNaN(iadult)) { iadult = 0 }
var istudent = parseInt(nstudent);
if (isNaN(istudent)) { istudent = 0 }
var itickets = istudent + iadult;
if (is_submit && itickets <= 0) {
// call off the purchase
alert("Please specify at least one adult or student ticket to purchase!");
return false;
}
else if (itickets < 3) {
var adult_price = base_adult_price;
}
else {
var adult_price = disc_adult_price;
}
var student_price = adult_price * student_discount;
// Build string components that show how many of each were purchased.
// These go into the PayPal item_name field.
if (iadult > 0) {
var adult_info = nadult + " Adult @ $" + adult_price.toFixed(2) + " ea.";
}
else {
var adult_info = "";
}
if (istudent > 0) {
var student_info = nstudent + " Student @ $" + student_price.toFixed(2) +"ea.";
}
else {
var student_info = "";
}
if (iadult>0 && istudent>0) {
var separator = " + ";
}
else {
var separator = "";
}
var item_name_string = "ACS Flex tickets: " + adult_info + separator + student_info;
var amount = iadult * adult_price + istudent * student_price;
//alert(amount);
var handling = (amount * handling_fee).toFixed(2);
//alert(handling);
$('input[name="item_name"]').val(item_name_string);
$('input[name="amount"]').val(amount);
$('input[name="handling"]').val(handling);
$('input[name="custom"]').val(item_name_string);
// alert("about to change label")
$('#subtotal').text(adult_info + separator + student_info + " = $" + amount.toFixed(2))
return true;
};
// ]]></script>
<form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" onsubmit=compute_form_inputs(true) method="post">
<div style="color: green; margin-bottom: 10px;">
<strong>Subtotal: <label id="subtotal" value=""></strong><super>*</super><br />
</div>
<div>
<input type="text" id="nadult" value="0" size="3"> <strong> Adult </strong>
<input type="text" id="nstudent" value="0" size="3"> <strong> Student </strong>
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="someone@mychoralsociety.org">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="item_name" value="">
<input type="hidden" name="handling" value="">
<input type="hidden" name="amount" value="">
<input type="hidden" name="custom" value="">
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</div>
</form>
<br />
<small> * 5% transaction fee will be added to online and credit card purchase to cover bank fees. </small>