SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Price calculator integration with Paypal

    Hi I have made a price calculator which calculates the deposit due by players of my service, how can I link this to paypal so that I have a paypal 'Pay now' button that would open a page with correct amount passed in Euros and option to pay by credit card or paypal? Thanks

    Frontend.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script language="javascript" src="ajax.js"></script>
    </head>
    
    <body>
    	<p style="display: inline;">Number of players:</p>
    	<input type="text" id="num1" size="2">
    	<input type="button" value=" = " onClick="doMath();">
    	<div id="answer" style="display: inline; font-size: 16px"></div>
    </body>
    </html>
    Ajax.php
    Code:
    http = getHTTPObject();
     
    function getHTTPObject(){
      var xmlhttp;
     
      /*@cc_on
     
      @if (@_jscript_version >= 5)
        try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
          try{
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
          xmlhttp = false;
        }
      }
      @else
        xmlhttp = false;
      @end @*/
     
      if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try {
          xmlhttp = new XMLHttpRequest();
        }catch(e){
          xmlhttp = false;
        }
      }
     
      return xmlhttp;
    }
     
    function doMath(){
      var url = "backend.php?num1=" + document.getElementById('num1').value;
     
      http.open("GET", url, true);
      http.onreadystatechange = handleHttpResponse;
     
      http.send(null);
    }
     
    function handleHttpResponse(){
      if(http.readyState == 4){
        document.getElementById('answer').innerHTML = http.responseText;
      }
    }
    Backend.php
    Code:
    <?php
      if($_GET['num1']==""){
        echo "Please enter number of players";
        exit;
      }
     
      if(!is_numeric($_GET['num1'])){
        echo "Error: Invalid numbers.";
        exit;
      }
     
       echo $_GET['num1'] * 7;
    
    
    ?>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's a bit complicated now, but you can go through the Pay Now button generator and uncheck the "host this button on PayPal" and every option that says something about encryption, to get a plain text link to a payment page. Then you can simply change the amount in the link to create a button for any amount.

    Here's an example of how you might use that to create a page which redirects to PayPal with the appropriate amount. You'd link your 'buy' or 'checkout' button to that page:

    PHP Code:
    $purl "https://www.paypal.com/cgi-bin/webscr?cmd=_ext-enter&redirect_cmd=_xclick&business=" urlencode($YOUR_PAYPAL_EMAIL);
    $purl .= "&item_name=" urlencode($ITEM_DESCRIPTION);
    $purl .= "&item_number=" $YOUR_REFERENCE_NUMBER;
    $purl .= "&currency_code=USD";
    $purl .= "&amount=" $AMOUNT
    $purl 
    .= "&shipping=0&tax=0";
    $purl .= "&return=" urlencode($YOUR_THANK_YOU_PAGE_URL);
    $purl .= "&cancel_return=" urlencode($PAGE_TO_GO_TO_UPON_CANCELLING);
    $purl .= "&notify_url=" urlencode($YOUR_IPN_PROCESSOR_URL);

    //Redirect to PayPal
    header("Location: $purl"); 

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With my script however the total amount is not stored as a variable, how can I change it to be able to pass it as a variable to paypal?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sure it is. http.responseText is the amount to be paid, you could use that to construct your payment button.

    But what you're doing is somewhat ridiculous. You don't need to make an asynchronous HTTP request to a backend PHP script to multiply a form input by 7. It's like calling up NASA and having them run your calculation through a supercomputer when you could just press the 7 button on the calculator in your hand. JavaScript could've done that client side on its own in one line.

    I suggest simply having your form submit to a PHP page, normally, without AJAX, which multiplies the input by 7 and redirects to PayPal.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •