PayPal payment function In fact has been updating the document and interface, here is a simple payment function about the flow of the following
1, in the Web site checkout page, set up a submit to the PayPal site form, which has some amount of money, merchandise name, merchant account receivable, after the successful return of the URL and so on,
2, the user checkout, by clicking the ' Use PayPal Checkout ' button to reach the PayPal checkout page, enter their own PayPal username and password and confirm payment
3,paypal will be based on whether or not to pay success to decide which page to return to the site, and in the background of the site to launch a POST request, this action called IPN, told the site to the payment of the account situation, such as completed is to complete the payment
4, the site received PayPal's notify notice, you can give the user shipping or other processing logic
Here's a picture to explain
A simpler flowchart
We need to complete the process, in fact, only two pages to deal with
- checkout.php This page is used to display shopping cart information, and let the user click the button to navigate to PayPal for payment
- notify.php This page is used to receive the IPN information PayPal, to determine whether the user's payment to the account and other status, and deal with the site after the business logic of collection
Log The code:
checkout.php This page can actually be HTML
Copy Code code as follows:
<form action= "HTTPS://WWW.PAYPAL.COM/CGI-BIN/WEBSCR" method= "POST" ><input type= "hidden" name= "EV_CSRF" Value= "9878824EB2CF4F1075DFA43C216D7CEC" > <input type= "hidden" name= "cmd" value= "_cart" > <input type= " Hidden "name=" Upload "value=" 1 "> <input type=" hidden "name=" CharSet "value=" Utf-8 "> <input type=" hidden " Name= "Currency_code" value= "USD" > <input type= "hidden" name= "Business" value=sales@test.com> <input "Hidden" name= "Cancel_return" value= "http://www.test.com/checkout.html" > <input type= "hidden" name= "return" Value= "http://www.test.com/thanks.html" > <input type= hidden "name=" Notify_url "value=" http://www.test.com/ notify.php "> <input type=" hidden "name=" Custom "value=" userid:31;ip:182.114.240.221 "> <input type=" Hidden "name=" Item_Number "value=" ARO0101 "> <input type=" hidden "name=" Item_name "value=" ad182m "> <input Type= "hidden" name= "Quantity" value= "1" > <input type= "hidden" name= "Amount" value= > <input type= "Submit" value= "Checkout with PayPal" > </form>
This form contains some items that PayPal pays to be added, Note that the notify.php is PayPal will be called in the background notify.php this page has two functions, one is to receive PayPal post content and tag return, one is to receive the authentication information PayPal after the site's internal logic processing
Copy Code code as follows:
$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) {
HTTP ERROR
} else {//http OK
Fputs ($FP, $header. $req);
while (!feof ($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "verified") = = 0) {
Process Business of website
}
else if (strcmp ($res, "INVALID") = = 0) {
Log for manual investigation
}
}
Fclose ($FP);
}