0

我已经暂时更新了我的合唱协会的网页,需要改变我们对 PayPal API 的使用。这是我需要完成的。

我们想以 1-2 美元的价格出售 20 美元(成人)和 10 美元(学生)的音乐会门票。对于 3 名及以上成人和/或学生的任意组合。价格应该是 16.50 美元(成人)和 8.25 美元(学生)。

Examples (just to be absolutely clear):
1 adult ticket : $20
2 adult tickets: $40
3 adult tickets: $49.50
4 adult tickets: $66
etc ...

1 student ticket: $10
2 student tickets: $20
3 student tickets $24.75
4 student tickets: $33
etc ...

1 adult + 1 student: $30
1 adult + 2 student: $33
2 adult + 1 student: $41.25
etc ...

以前,我们的网站有用于各种组合的“添加到购物车”按钮。这让客户感到困惑并且也不可靠,因为 PayPal 提供了可以更改数量的自己的表格。

我想用一个“立即购买”按钮替换我们当前所有的“添加到购物车”按钮,该按钮会指向一个页面,客户可以在该页面中指定任意数量的成人票和学生票(只要两者都不为零)并继续付款与上述定价。

我查看了PayPal API并看到了与折扣相关的几个字段(在表 3 中),但完全不清楚是否有一种组合可以满足我的需求。

任何帮助表示赞赏。

4

2 回答 2

0

在 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:&nbsp;<label id="subtotal" value=""></strong><super>*</super><br />
</div>
<div>
<input type="text" id="nadult" value="0" size="3"> <strong> Adult </strong>&nbsp;
<input type="text" id="nstudent" value="0" size="3"> <strong> Student </strong>&nbsp;
<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>
于 2012-10-20T00:53:06.360 回答
0

将这些行添加到您的按钮代码中。贴现率为 17.5%。添加第一行为零的行,以便折扣不适用于第一个项目。

<input type="hidden" name="discount_rate" value="0">
<input type="hidden" name="discount_rate2" value="17.5">

或者,如果您的按钮是 PayPal 托管的,请将这些添加到第 3 部分的高级变量中

discount_rate=0
discount_rate2=17.5

来源:https ://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/

于 2016-01-10T09:16:52.930 回答