skip to Main Content

I am creating a shopping cart using PHP and HTML and that all works fine. But I would appreciate some help in creating a PayPal(PP) button that caters for a variable amount (i.e., the total in the cart) and changing the currency to Australian dollars (AUD). When I submit the code below it all works fine as long as I do not try to change the currency which defaults to US$. I also need to set up a return ‘thanks’ URL and a ‘cancel’ URL. I have Googled for hours but so far have not found a solution that works for me.

Any help will be most appreciated and thanking you in anticipation.

<!-- now the button -->
        <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_blank">
           <input type="hidden" name="cmd" value="_cart">
           <input type="hidden" name="business" value="[email protected]">
           <input type="hidden" name="item_name" value="Item Description">
           <input type="hidden" name="item_number" value="">
           <input type="hidden" name="amount" value="$total">
           <input type="hidden" name="handling" value="0.00">
           <input type="hidden" name="shipping" value="0.00">
           <!-- <input type="hidden" name="currency_code" value="AUD"> -->
           <input type="hidden" name="lc" value="AU">
           <input type="hidden" name="bn" value="PP-BuyNowBF">
           <input type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1">
           <input type="hidden" name="add" value="1">
        </form>

Thanks to the advice from Preston PHX my code now looks like this which is fine but clicking the PayPal link, even though it shows the hand icon, does not go anywhere. Please tell me what I am doing wrong or missing. And thank you.

echo <<<DD1
        <!-- DEVELOPMENT TEST!!!-->
        
            <div id="smart-button-container" style="width: 60%; margin-left: 21%;">
            <div style="text-align: left"><label class="checkout_form_mt_right v_align_top" for="description">Item:&nbsp;&nbsp;&nbsp;</label><input type="text" name="descriptionInput" id="description" size="40" value="Text here"></div>
              <p id="descriptionError" style="visibility: hidden; color:red; text-align: center;">Please enter a description</p>
            <div style="text-align: left"><label  class="checkout_form_mt_right v_align_top" for="amount">Amount:&nbsp;&nbsp;</label><input name="amountInput" type="string" id="amount" value="$total"><span> AUD</span><span class='v_align_top white_text'><small><i>&nbsp;&nbsp;Australian dollars</i></small></span></div>
              <p id="priceLabelError" style="visibility: hidden; color:red; text-align: center;">Please enter a price</p>
            <div id="invoiceidDiv" style="text-align: center; display: none;"><label for="invoiceid"> </label><input name="invoiceid" maxlength="127" type="text" id="invoiceid" value="" ></div>
              <p id="invoiceidError" style="visibility: hidden; color:red; text-align: center;">Please enter an Invoice ID</p>
            <div style="text-align: center; margin-top: 0.625rem;" id="paypal-button-container"></div>
          </div>
      
      <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=AUD" data-sdk-integration-source="button-factory"></script>
      
      <script>
      function initPayPalButton() {
        var description = document.querySelector('#smart-button-container #description');
        var amount = document.querySelector('#smart-button-container #amount');
        var descriptionError = document.querySelector('#smart-button-container #descriptionError');
        var priceError = document.querySelector('#smart-button-container #priceLabelError');
        var invoiceid = document.querySelector('#smart-button-container #invoiceid');
        var invoiceidError = document.querySelector('#smart-button-container #invoiceidError');
        var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv');
    
        var elArr = [description, amount];
    
        if (invoiceidDiv.firstChild.innerHTML.length > 1) {
          invoiceidDiv.style.display = "block";
        }
    
        var purchase_units = [];
        purchase_units[0] = {};
        purchase_units[0].amount = {};
    
        function validate(event) {
          return event.value.length > 0;
        }
    
        paypal.Buttons({
          style: {
            color: 'gold',
            shape: 'rect',
            label: 'paypal',
            layout: 'vertical',
            
          },
    
          onInit: function (data, actions) {
            actions.disable();
    
            if(invoiceidDiv.style.display === "block") {
              elArr.push(invoiceid);
            }
    
            elArr.forEach(function (item) {
              item.addEventListener('keyup', function (event) {
                var result = elArr.every(validate);
                if (result) {
                  actions.enable();
                } else {
                  actions.disable();
                }
              });
            });
          },
    
          onClick: function () {
            if (description.value.length < 1) {
              descriptionError.style.visibility = "visible";
            } else {
              descriptionError.style.visibility = "hidden";
            }
    
            if (amount.value.length < 1) {
              priceError.style.visibility = "visible";
            } else {
              priceError.style.visibility = "hidden";
            }
    
            if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {
              invoiceidError.style.visibility = "visible";
            } else {
              invoiceidError.style.visibility = "hidden";
            }
    
            purchase_units[0].description = description.value;
            purchase_units[0].amount.value = amount.value;
    
            if(invoiceid.value !== '') {
              purchase_units[0].invoice_id = invoiceid.value;
            }
          },
    
          createOrder: function (data, actions) {
            return actions.order.create({
              purchase_units: purchase_units,
            });
          },
    
          onApprove: function (data, actions) {
            return actions.order.capture().then(function (details) {
              alert('Transaction completed by ' + details.payer.name.given_name + '!');
            });
          },
    
          onError: function (err) {
            console.log(err);
          }
        }).render('#paypal-button-container');
      }
      initPayPalButton();
      </script>
     
        <br /><br />

DD1;
} // end development

Is the PayPal bar supposed to be the submit button? If so the way I have it set up it goes nowhere. I do have a developer API key but don’t know if/how/where to incorporate it. I removed the form – thanks for the advice.

Again, any advice will be most appreciated.

Here’s a screen scrape of the code that is generated for the yellow PayPal button. As mentioned I cannot see any HTML <a links in it?

[Screen Scrape]

Please note that at the moment I am trying to get into the sandbox – maybe there is a different link to generate the buttons for the sandbox though I note that the link that Preston provided includes ‘developer’ ?

2

Answers


  1. Chosen as BEST ANSWER

    After many hours, this works :-)

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_blank">
                   <input type="hidden" name="cmd" value="_xclick">
                   <input type="hidden" name="business" value="[email protected]">
                   <input type="hidden" name="item_name" value="Item Description">
                   <input type="hidden" name="item_number" value="">
                   <input type="hidden" name="amount" value="$total">
                   <input type="hidden" name="handling" value="0.00">
                   <input type="hidden" name="shipping" value="0.00">
                   <input type="hidden" name="currency_code" value="AUD">
                   <input type="hidden" name="lc" value="AU">
                   <input type="hidden" name="bn" value="PP-BuyNowBF">
                   <input type="hidden" name="add" value="1">
                   <input type="image" src="./images/PayPal_Button.jpg" alt="Submit" width="100"> <!-- the submit button -->
                </form>
    

    Many thanks for Preston's help.


Please signup or login to give your own answer.
Back To Top
Search