Execute mysql command after payment received

Discussion in 'PHP' started by dawilster, Aug 31, 2008.

  1. #1
    I have a simple rpg game and i want to offer a way to purchase special points, so is there anyway i can have a paypal button and after payment is received i will execute a command that will top there account up

    thanks
     
    dawilster, Aug 31, 2008 IP
  2. Warden

    Warden Peon

    Messages:
    22
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PayPal has an option of letting you set a "Back to merchant" link that basically sends the visitor back to your page or a "Thank You" page after the payment was received. You could use that to link to a PHP file that updates the database and displays a Thank You message with some eventual instructions or feature list of the new special points. It should be pretty easy.
     
    Warden, Aug 31, 2008 IP
  3. CJ1

    CJ1 Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The above is very insecure. If you go to PayPal Developer you can find ways of using API to check whether a payment has been processed.

    The best way to do this is to use Instant Payment Notification (IPN) so PayPal's servers tell you whether a transaction was processed and valid or not...

    [​IMG]

    You need a Premier or Business account to do this (and you should have one to use Buy Now buttons anyway).

    1. Log in to your Premier or Business PayPal account.
    2. Click Profile.
    3. Click Instant Payment Notification Preferences.
    4. Enter the URL at which you would like to receive IPN posts.

    Example url: http://mywebsite.com/thanks.php

    Now you can use PayPal's servers to see if a payment is valid. You now have the option of these variables...

    You can use these to test for such booleans as:

    1. If the payment was an echeck, display a message which says "Please wait for the echeck to clear before we credit your account".
    2. "It appears your address is different from your PayPal account, please wait for us to verify your payment before we credit your account".

    etc. etc. But you can also use this to check if the payment was valid:

    
    <?php
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }
    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
    if (!$fp) {
    echo "There was an error. Please wait for manual transaction verification";
    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
    
    
                                           // echo the response
                                           echo "The response from IPN was: <b>" .$res ."</b><br><br>";
    
    
                                           //loop through the $_POST array and print all vars to the screen.
                                           foreach($_POST as $key => $value){
                                           echo $key." = ". $value."<br>";
    }
    
    
    }
    else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    
    
                                            // echo the response
                                            echo "The response from IPN was: <b>" .$res ."</b>";
    
    
      }
    
    }
    fclose ($fp);
    }
    ?>
    
    Code (markup):
    Change the massively tabbed parts to your own code. This just echos the response. The "//loop through the $_POST array and print all vars to the screen" just echos the information they entered. You won't really want to display this. Change the tabbed part under "// echo the response" to the code you want to execute if the person's payment is verified.

    PLEASE NOTE: There are 2 options. VERIFIED or INVALID. Currently the script will display whether the payment is verified or invalid, and echo the payment info. You need to change the massively tabbed code!

    For example, you could put:

    
    <?php
    if($res=="VERIFIED") {
    
    // do the code, it's verified payment
    
    } elseif($res=="INVALID") {
    
    // the person tried to pay, but it failed. for example: cancelled payment, invalid funds (i think invalid funds is an option)
    
    } else {
    
    // the person has not tried to pay but just came onto the thankyou page, trying to use the method in the above post to steal
    
    }
    ?>
    
    Code (markup):
    - This script uses PHP function fsockopen(). Make sure your server supports this function call or this script will not work. For more information on fsockopen(), you can visit www.php.net.
    - This script uses the PHP function mail() to send the email. Make sure your server supports this function call or this script will not work. For more information on mail(), you can visit www.php.net.

    For this script to work as intended, you should set the value of the 'return' variable in your button code to the URL of this script.

    Do NOT enable Auto Return or Payment Data Transfer (PDT) or this script will not work.

    I hope this helps.
     
    CJ1, Aug 31, 2008 IP
    chandan123 likes this.
  4. dawilster

    dawilster Active Member

    Messages:
    844
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    great stuff thanks alot this cleared up alot, thanks man
    edit: also where exactly do i add the buy now button
     
    dawilster, Aug 31, 2008 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to get a hold of the actual documentation on this, it can be found here
    https://developer.paypal.com/devscr?cmd=help/main
     
    JAY6390, Aug 31, 2008 IP
  6. dumsky

    dumsky Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks a lot, really great stuff
     
    dumsky, Aug 31, 2008 IP
  7. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #7
    yes the inf is quite usefull
     
    octalsystems, Aug 31, 2008 IP