Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile Can you get javascript to read through the URL to find strings?

    I want javascript to read through a url on page load and search for strings that are inside the url for example item_name="wolf" and payment_ status="completed" and once it has found these print out the number inside the string quantity="1" using .substring(10)?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by maxmitch View Post
    I want javascript to read through a url on page load and search for strings that are inside the url for example item_name="wolf" and payment_ status="completed" and once it has found these print out the number inside the string quantity="1" using .substring(10)?
    I think we need a clearer example of an url. But the answer is yes. Is the number always just a single digit?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    maxmitch (08-11-2013)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    The url is just letters. It is an IPN by paypal!:
    Code:
    txn_type = virtual_terminal&payment_date = 17:11:42 Jul 15, 2008 PDT&last_name =&receipt_id = 3075-7371-4622-1677
    Yes it is always one digit!

    Thank you!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Ummm...but I saw nothing in that example you gave that had either item_name or payment_status included.

    Can you give a concrete example?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    maxmitch (08-11-2013)

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile

    Sorry I just showed you a small sample. https://www.paypal.com/uk/cgi-bin/we...20dd5a9b6ad0e3 this is the kind of idea. I admit it... I don't quite understand it but it says that PayPal sends IPN messages to the URL that you specify. And according to other links, such as https://www.paypalobjects.com/websta...f/ipnguide.pdf It ends up as just a long line of code. With scripts such as item_name="....." etc... I basically just want a way to notify my html/javascript when someone confirms to buy an item. So that I can notify customers if the item is out of stock. any ideas?

    Thank you. It is very kind of you to spend time on this!!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Okay, that's a standard querystring format. So let's just parse it as a query string and go from there.
    Code:
    var ipn = "...that ipn stuff..."
    
    var ipnInfo = [ ];
    var pairs = ipn.split("&");
    for ( var p = 0; p < pairs.length; ++p )
    {
        var pair = pairs[p].split("=");
        ipnInfo[ pair[0] ] = decodeURIComponent( pair[1] );
    }
    Now, once you have done that, it is easy to look for one of those key names.

    For example:
    Code:
    if ( ipnInfo["item_name"] == "wolf" ) && ipnInfo["payment_status"] == "completed" )
    {
        var wolf_quantity = Number( ipnInfo["quantity"] );
    }
    In other words, the code at that top there converts the big long messy string into an easy-to-use lookup table.

    You can also test to see if a given name even exists by using
    Code:
    if ( ipnInfo["frambozzle"] != null )
    {
        alert("How about that!  We even have a frambozzle in there");
    } else {
        alert("So sad.  No frambozzling today for me");
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    maxmitch (08-11-2013)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    But I just realized: you can *NOT* do this with HTML/JavaScript.

    You *MUST* use server-side coding to use PayPal's IPN!

    That's because if you are going to accept IPN messages, you must accept them via a URL *ON YOUR SITE*. Further, you *MUST* acknowledge the messages via server-side coding, as those docs clearly state.

    So, sorry, you are in the wrong forum for this. You will need to do this in PHP/ASP/JSP/etc.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    maxmitch (08-11-2013)

  • #8
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi thank you very much! Can you explain why you can only use this with PHP?
    Is there anyway of then linking the PHP back to the javascript inside the HTML?
    And can you point me in the direction of someone who might be able to help me do this using php?

  • #9
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by Old Pedant View Post
    Further, you *MUST* acknowledge the messages via server-side coding, as those docs clearly state.
    So does this mean I could use javscript to look inside a php file? And do the kind of things I was stating at first?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    I don't think you understand the documentation for IPN.

    IPN is going to call *A PAGE ON YOUR WEBSITE* any old time it feels like. It might call it 5 seconds after you do a transaction; it might call it at 3:43AM when it finds that the credit card transaction was declined by your user's financial institution, it might... Well, the point is, you can NOT predict when it will call that page on your site.

    IT DOES NOT CALL YOUR BROWSER!!! IPN itself is, from the viewpoint of your site, the browser!

    If you have never done any web-based client-server programming (that is, if you have never coded a PHP page--or an ASP or JSP or CGI page--then you probably don't understand the protocol.

    HTTP (Hyper Test Transfer Protocol) is defined such that a client (most typically a web browser such as MSIE, Chrome, FireFox, Safari, etc., but NOT necessarily!) makes *ONE* request of a server. And then the server makes *ONE* response back to the client. And then those two "ends" (client and server) forget about each other.

    So when you, using your client browser hit a web page (example http://www.codingforums.com/forumdisplay.php?f=2 ), some code on the server (in the case of that URL, PHP code...because of the ".php" extension) reads your request (i.e., you requested the page named "forumdisplay.php" and you asked, further, for "f=2"). And so then that server-side code (php, here) says "Oh, he wants to see forum number 2, which is the JavaScript forum, so let me go show him one page of posts." And, indeed, it sends back a page worth of posts as HTML and your browser parses that and displays it.

    NOW...

    With IPN, the PayPal IPN code is the client. JUST EXACTLY as if it were a browser, it makes an HTTP request of the server web page that you told PayPal to use for you. And your server code is expected to SEND BACK a message in a particular format to verify that it received that IPN message.

    NOWHERE in all of that is there ANY browser involved. Nowhere in any of that is there any place FOR a browser to be involved.

    Even if you had a browser up and running on your desktop computer, there is NO MECHANISM for your server to tell your browser that it has received an IPN message. (There are ways to do that outside of a browser, but are you really wanting the server to send YOUR computer a message? Or some other client computer? And there are ways for your browser to "poll" the server every NN seconds and ask "has anything happened?" but I also don't see how that is relevant or appropriate to this situation.

    In short, if you don't understand the IPN documentation and what it is implying that you need to code, then I doubt seriously that you are ready to use IPN. Stick with what PayPal gives you in the browser-based environment.
    Last edited by Old Pedant; 08-11-2013 at 10:04 PM.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    IPN is going to call *A PAGE ON YOUR WEBSITE* any old time it feels like. It might call it 5 seconds after you do a transaction;
    Just to expand on the above since 5 seconds is not necessarily a realistic timeframe for the processing - often it will be a much longer time period than that.

    With payment via eCheck the IPN script gets called twice per transaction - the first time just after the transaction is processed with a flag of "pending" and a second time about a week later with a flag of "completed". The user will be long gone from your web site by the time the second call to complete the payment processing is done. If you want the script to interact with the person making the payment then that interaction needs to be done by creating and sending an email - it can't normally be done by updating anything on a web page because they may not have had their computer turned on for as much as a week by the time that the IPN is called to complete the processing.

    The only time it could be done by updating a web page is if the person creates an account with you prior to making the payment. That gives them a username and password that they can use to identify themselves for access to a part of your web site. That part of the site displays the various electronic products available to purchase with entries in a database tracking which ones that have been purchased through each account. When someone makes a purchase and Paypal tells your IPN script that the payment is complete you then update the database for that account to indicate that item has been purchased. The next time the person logs in they will then see the updated web page that provides the link to download what they bought - this web page might be displayed months after the payment is completed.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Thanks for the support and additional info, Felgall. I've never actually used IPN. (We use Authorize.Net, instead, and from them we get a "feed" once a day about 4PM with a summary of all changes, rather than getting changes one at a time.)

    I hadn't even thought about eCheck. An excellent example of why none of this makes sense without server-side processing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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