Paypal优惠券代码结账

时间:2015-11-09 04:43:58

标签: php jquery paypal

我正在尝试为我的网站制作简单的产品结帐系统。我正在使用paypal默认结帐系统。它工作正常,但我也需要优惠券代码设施。我从未在paypal中看到任何选项以获取优惠券代码。这就是为什么我试图通过jquery创建它。

我成功了,但它仅适用于一种产品。当我在页面中添加更多产品时,这是行不通的。我可以理解它关于类和id的问题,但我仍然不知道如何做到这一点。也尝试折扣%明智的当前我的代码只工作硬编码价格。

你们会检查我的代码,看看我的错误在哪里,或者如何做到。或者,如果还有其他方法可以做到

HTML

<h2>  Product 1 - $4.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0001" />
    <input type="hidden" name="amount" class="amount" value="4.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>
 <h2>  Product 2 - $6.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0002" />
    <input type="hidden" name="amount" class="amount" value="6.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>

<h2>  Product 3 - $8.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0003" />
    <input type="hidden" name="amount" class="amount" value="8.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>
 <h2>  Product 4 - $1.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0004" />
    <input type="hidden" name="amount" class="amount" value="1.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>
 <h2>  Product 5 - $6.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0005" />
    <input type="hidden" name="amount" class="amount" value="6.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>

<h2>  Product 6 - $6.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0006" />
    <input type="hidden" name="amount" class="amount" value="6.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>
 <h2>  Product 7 - $6.99</h2> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal" class="frm_paypal">
    <input type="hidden" name="cmd" value="_cart" />
    <input type="hidden" name="business" value="info@soming.com" />
    <input type="hidden" name="lc" value="US" />
    <input type="hidden" name="item_name" value="Chile Powder" />
    <input type="hidden" name="item_number" value="0007" />
    <input type="hidden" name="amount" class="amount" value="6.99" />
    <input type="hidden" name="currency_code" value="USD" />
    <input type="hidden" name="button_subtype" value="products" />
    <input type="hidden" name="no_note" value="0" />
    <input type="hidden" name="tax_rate" value="0.082" />
    <input type="hidden" name="add" value="1" />
    <input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest" />
    <input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://oldstoberfest.com/wp-content/uploads/2015/04/buynow.png" class="btnPaypal" />
    <div class="div_coupon_code">
        <div class="div_msg"></div>If you have any coupon code please enter Otherwise leave it blank and Press checkout.
        <br>
        <input type="text" name="coupon_code" class="coupon_code" value="" placeholder="Coupon Code" />
        <br>
        <input type="button" class="btnCheckOut" name="btnCheckOut" value="Checkout" /> <span class="span_pay">
        <input type="button" class="btnPay" name="btnPay" value="Pay" />
        </span> 
    </div>
    <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />
</form>

JS

jQuery(document).ready(function($){
  $('body').on('click','#btnPaypal', function(e) {
      e.preventDefault();
      $("#div_coupon_code").show("slow");
      return false;
  });

  $('body').on('click','#btnCheckOut', function(e) {
      var coupon=$("#coupon_code").val();
      var go_paypal=1;
      if(coupon=="happy" || coupon=="HAPPY")
      {
        $("#amount").val(5.24);
        $("#div_msg").html("Valid coupon code entered! Click pay to continue").show("slow");
        go_paypal=0;
      }
      else if(coupon!="")
      {
        $("#div_msg").html("Invalid coupon code. Please double check").show("slow");
        go_paypal=1;
      }
      else
        $("#frm_paypal").submit();

      if(go_paypal==0)
      {
        $("#btnCheckOut").hide("slow");
        $("#span_pay").show("fast");
      }
  });

  $('body').on('click','#btnPay', function(e) {
      $("#frm_paypal").submit();
  });
  $("#div_coupon_code").hide();
  $("#span_pay").hide();    
});  

单品演示 http://jsfiddle.net/cyber007/gpLeqgzc/

多产品演示 http://jsfiddle.net/cyber007/3pLv8dy6/

我会在这里继续更新

1 个答案:

答案 0 :(得分:0)

如果您想动态计算多个产品的折扣,那么将所有产品的价格定价,然后在获得总额后应用优惠券折扣

所以,你的代码应该是这样的......

function calcPrice(){
var v = parseInt(document.getElementById("val").value);
var q = parseInt(document.getElementById("quantity").value);
if ((isNaN(q)) || (q == null) || (q <0)) {
alert ("You must enter a positive number");
document.getElementById("quantity").value = "";
return false;
}
var p = calcTotal(v,q);
alert("Total Price = " + p);
}

function calcTotal(v,q) {
var discount = 0;
if (q>=1) {discount = .25}
var price = q * v;
var savings = price * discount;
var total = price - savings;
return total;
}

这是您只需修改HTML

的ID和类所需的参考