PHP Consolidated PayPal Payment

Source: Internet
Author: User
Tags php class vars

Simply tidy up the PHP project to integrate PayPal payment function.

First, the form of the construction:

1 2 3 4 5 6 7 8 9 10 11-12 <form method= "POST" name= "form" action= "HTTPS://WWW.PAYPAL.COM/CGI-BIN/WEBSCR" > <input type= "hidden" RM "value=" 2 "/> <input type=" hidden "name=" cmd "value=" _xclick "/> <input type=" hidden "name=" business " Value= "merchant account"/> <input type= "hidden" "name=" return "value=" returns to the URL "/> <input type=" hidden "name=" Cancel_return "Value=" cancels the return url "/> <input type=" hidden "name=" Notify_url "value=" is used to receive PayPal-sent instant payment notification URLs that must be valid URLs "/> < Input type= "hidden" name= "Item_name" value= "Item name"/> <input type= "hidden" name= "Item_Number" Can be used to track the delivery variables purchased or donated, and return the price of the "/> <input type=" hidden "name=" Amount "value=" items (the total price of all items in the shopping cart) at the completion of the payment/> Type= "hidden" name= "Currency_code" value= "Currency"/> </form>

Second, IPN verification part

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 <?php class PayPal {var $ipn _data = Array ();//array contains the POST values fo R IPN var $fields = array (); Submit a form parameter to PayPal, example: Item_name=iphone 6,QUANTITY=1,AMOUNT=499,CURRENCY_CODE=USD all form information,//before calling the validation IPN First need to initialize good var $paypal _url = ' https://www.paypal.com/cgi-bin/webscr ';//sandbox:https://www.sandbox.paypal.com/cgi-bin /WEBSCR function VALIDATE_IPN () {//Verify IPN//Parse the PayPal URL $url _par=parse_url ($this->paypal_url)       ; Generate the post string from the _post VARs aswell as load the//_post VARs to a arry so we can play with th       EM from the calling//script.       $post _str = ';         foreach ($_post as $field => $value) {$this->ipn_data["$field"] = $value; $post _str. = $field. ' = '. UrlEncode (Stripslashes ($value)). '       & '; } $post _str.= "Cmd=_notify-validate"; Append IPN command//Open the connection to PayPal $fp = Fsockopen ($url _par[host], "No", $errnum, $errstr, 30);         if (! $fp) {//Could not open the connection.       return false; else {//post the data back to PayPal fputs ($fp, "post". $url _par[path]. "         Http/1.1rn "); Fputs ($FP, "Host:". $url _par[host]. "         RN ");         Fputs ($fp, "content-type:application/x-www-form-urlencodedrn"); Fputs ($FP, "Content-length:". strlen ($post _str). "         RN ");         Fputs ($fp, "connection:closernrn"); Fputs ($fp, $post _str.         "Rnrn"); Loop through the response from the "server and append to variable" while (!feof ($fp)) {$this->valida         Te_ipn_response. = Fgets ($fp, 1024); } fclose ($FP);     Close connection} if (Eregi ("verified", $this->validate_ipn_response)) {return true;     else {return false; }}?>

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.