PayPal amount tampering

只谈情不闲聊 提交于 2019-11-29 11:49:39
Andy

There are a couple ways to prevent this. The first is using PayPal's Instant Payment Notification (IPN). Using this, you would compare the prices that PayPal posts back to you to confirm that they match what you are expecting. If they don't match, you cancel the order.

Example Workflow:

  • User Orders an item and modifies price to $0.01
  • Order is posted to PayPal, which shows price of $0.01
  • User accepts price and pays $0.01
  • PayPal calls your IPN URL and posts transaction details, showing that the user paid $0.01 for a thing
  • Your IPN checks the price that PayPal received ($0.01) verus what you were expecting ( > $0.01). Since they don't match, you cancel the order

Another option, is to use PayPal's Button API, to create dynamic, encrypted buttons. These are embedded into your page and the user clicks it to make their order. Since it is encrypted, the user is unable to reliably modify the source code during the transaction. A nice example of this is available in this answer. Additionally, you are able to combine this with the IPN option listed above to serve as a nice audit of the transaction

What you need to do is implement a simple invoice system. Have a table in your database called invoices (ID, User_Id, Invoice_Value, Payment_Status) (example).

When the user gets to the checkout page, by now you should have inserted an entry in the db table for that user, for the total amount they have to pay and a initial payment status of "Pending"). After inserting the invoice table row, get the last insert id and to a variable called $invoice_id.

Now, you output the html paypal checkout button form and one of the hidden input field should be like this:

<input type="hidden" value="<?php echo $invoice_id; ?>" name="custom">

Now, when paypal responds with the IPN to your return URL, your IPN handler should behave something along this way:

<?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://sandbox.www.paypal.com', 443, $errno, $errstr, 30);  
if (!$fp) {  
    // HTTP ERROR  
}
else
{
    // Make Request To PayPal
    fputs ($fp, $header . $req);  
    while (!feof($fp))
    { 
        // Read Response 
        $res = fgets ($fp, 1024);

        // Check Response  
        if (strcmp ($res, "VERIFIED") == 0)
        {  
            // PAYMENT VALIDATED & VERIFIED!

            // Load the Invoice_Value from invoices table for $_POST['custom']
            // and compare it with paypal posted amount held in $_POST['mc_gross']
            // if it matches, paypal has authenticated the payment and the value has not been tampered with
            // update the invoice table and set the payment status
        }  
        else if (strcmp ($res, "INVALID") == 0)
        {  
            // PAYMENT INVALID & INVESTIGATE MANUALY!  
        }
    }  
    fclose ($fp);  
}  

?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!