如何使用PayPal API进行总订单数量折扣

时间:2012-10-19 16:38:27

标签: paypal

我暂时更新了我的合唱协会网页,需要对我们使用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),但我们还不清楚是否有一个组合可以满足我的需求。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

在PayPal支持的帮助和大量搜索示例代码的情况下,我想出了以下似乎在测试版中令人满意的工作。 keydown逻辑来自StackOverflow上的另一个问题。

代码会为成人和学生数量创建一对文本框。当客户输入数量以在输入上方显示小计字符串时,将强制执行折扣规则。当客户点击“立即购买”时,总额将作为单个项目发送以创建付款表单。还增加了S& H费用作为总额的百分比以支付交易成本。

为冗长的编码风格道歉。 JavaScript不是我惯用的语言。欢迎提出改进建议。

希望有人发现它有用!

警告 - 此代码易于被CROOKS与计算机技能作弊。不要在没有某种形式的购买后审核的情况下使用它进行有价值的交易。

<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>

答案 1 :(得分: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/