Example of integrating ThinkPHP into PayPal payment

Source: Internet
Author: User
Tags strcmp

A project needs to use the paypal payment function. The first time I learned about APIs outside China, I had a really bad time. I had to figure it out for two days. During this time, I also found a lot of information. I was confused at the beginning, go to the official website to read the documents. All the documents are in English. I can use translation tools to translate them a little bit. I don't know what the translation is.

 

It is relatively simple to submit a payment form. The important step is to interact with paypal to verify this. If you understand it, you will have other problems. At first, you will not understand the ipn, so I went around a big circle, miserable, and hard-to-use. Well, let's not talk much about other nonsense. First, let's look at what ipn is:

 

What is instant payment notification IPN?
When you receive a new payment transaction or the status of a payment transaction changes, PayPal will be asynchronous (that is, not as part of the website payment process) send payment details to the URL you specified so that you can understand the buyer's payment details and respond accordingly. This process is called an instant payment notification (IPN ). The diagram is as follows:

 

 

& Bull; the customer clicks & ldquo; Payment & rdquo; to pay for your account;
& Bull; after receiving the payment from the customer, PayPal sends IPN to the URL specified by your server through POST;
& Bull; after your server receives the IPN, you must return the received POST information to PayPal for verification. PayPal can use this method to protect your defense

 

Fan spoofing or & ldquo; man-in-the-middle & rdquo; attack (the verification process for IPN information is called notification confirmation );
& Bull; PayPal returns verification information. If it passes verification, it is VERIFIED. If it fails, it is INVALD;
& Bull; process payment details based on verification information;
Note: You may receive more than one IPN information each time you make a payment until the payment status in the received IPN information is Completed.

 

The IPN data contains detailed information about the entire payment process. You can:
& Bull; custom website: you can notify the customer of the payment status by email or other means;
& Bull; automatically perform related operations: When you receive IPN data and confirm that the payment status has been completed, you can immediately start the delivery process to the buyer, you can also recharge the virtual currency for the buyer or send the card number and password of the virtual goods to the buyer in some way;
& Bull; record transaction information to your database;
& Hellip;. The process is like this. These are on the official website:

 

Http://www.ebay.cn/public/paypal/integrationcenter/list__resource_2.html

 

Variable submitted by form

 

Http://www.ebay.cn/public/paypal/integrationcenter/list__resource_7.html

 

Now, let's briefly describe ipn here. Here we will go directly to the paypal class. Finally, we will share some of the reference manual for paypal.

<? Phpnamespace Home \ Controller; use Home \ Father; header ('content-Type: text/html; charset = utf-8 '); // Paypal payment class PaypalController extends FatherController {public function _ empty () {header ("HTTP/1.0 404 Not Found"); $ this-> display ('Common: 404 ');} // submit the form public function buildForm () {$ paypal_email = 'AAA @ paypal.com'; // seller account $ order_sn = '123 '; // Order No. $ custom = '12'; // custom additional parameter $ currency_code = 'USD'; // goods Currency type $ charset = 'utf-8'; // code $ item_name = 'Order name'; $ amount = 200; // amount $ formurl = 'https: // www.sandbox.paypal.com/cgi-bin/webscr'#//test delivery address //?formurl = 'https: // www.paypal.com/cgi-bin/webscr'#// I am trying to submit the address #weburl = 'http ://'. $ _ SERVER ['http _ host']. _ APP __; $ yyurl = $ weburl. '/Paypal/yyurl'; // IPN address $ cancel_return = $ weburl; // address returned after the user cancels the payment $ return = $ weburl. '/Paypal/returnurl'; // address returned after successful payment // for more form variables, visit payp Official website of al // http://www.ebay.cn/public/paypal/integrationcenter/list?resource_7.html#html = '<form id = "paypalsubmit" action = "'. $ formurl. '"method =" post ">'; $ html. = '<input type = "hidden" name = "cmd" value = "_ xclick"> <input type = "hidden" name = "charset" value = "'. $ charset. '"> <input type =" hidden "name =" business "value = "'. $ paypal_email. '"> <input type =" hidden "name =" receiver_email "value = "'. $ paypal_email. '"> <Indium Ut type = "hidden" name = "item_name" value = "'. $ item_name. '"> <input type =" hidden "name =" item_number "value = "'. $ order_sn. '"> <input type =" hidden "name =" currency_code "value = "'. $ currency_code. '"> <input type =" hidden "name =" custom "value = "'. $ custom. '"> <input type =" hidden "name =" amount "value = "'. $ amount. '"> <input type =" hidden "name =" yy_url "value = "'. $ yyurl. '"/> <input type =" hidden "name =" cancel_retu Rn "value = "'. $ cancel_return. '"/> <input type =" hidden "name =" return "value = "'. $ return. '"/>'; $ html. = '</form> <script> document. forms ["paypalsubmit"]. submit (); </script> '; echo $ html;}/* IPN instant notification */public function policyurl () {// if it is not POST mode if (! IS_POST) exit ('invalid request'); // record the POST variable in the local variable $ data = array (); $ req = 'cmd = _ policy-validate '; foreach ($ _ POST as $ key =>$ value) {$ data [$ key] = $ value; $ value = urlencode (stripslashes ($ value); $ req. = "& $ key = $ value";} // POST the information to paypal for verification $ header = "POST/cgi-bin/webscr HTTP/1.0 \ r \ n "; $ header. = "Content-Type: application/x-www-form-urlencoded \ r \ n"; // use sandbox $ header for testing. = "Host: www.sandbox.paypal.com \ r \ n ";// $ Header. = "Host: www.paypal.com \ r \ n"; $ header. = "Content-Length :". strlen ($ req ). "\ r \ n"; $ header. = "Connection: close \ r \ n"; // In the case of sandobx, set: $ fp = fsockopen ('SSL: // www.sandbox.paypal.com ', 443, $ errno, $ errstr, 30); // $ fp = fsockopen ('SSL: // www.paypal.com ', 443, $ errno, $ errstr, 30 ); // Determine whether the reply POST is successfully created if (! $ Fp) {fclose ($ fp); return false;} else {// write the reply POST information to the socket Port fputs ($ fp, $ header. $ req); // file_put_contents('data.txt ', http_build_query ($ data), FILE_USE_INCLUDE_PATH); // start to accept the authentication information of paypal for replying to POST information while (! Feof ($ fp) {$ res = fgets ($ fp, 1024); // file_put_contents('aa.txt ', $ res, FILE_APPEND ); // if (strcmp ($ res, 'verified ') = 0) {// Check the payment status // check whether txn_id has been processed // check whether receiver_email is the email address in your paypal account // check whether the payment amount and currency unit are correct // this payment, update order status fclose ($ fp); return false;} else if (strcmp ($ res, 'invalid') = 0) {// echo 'error '; // The POST information fclose ($ fp); return false ;}} fclose ($ fp); return false ;}} /* page jump handling method * After the payment is complete, go to the address */public function returnurl () {$ tx = I ('Get. tx ', ''); // paypal transaction No. $ st = I ('Get. st', ''); // payment status $ amt = I ('Get. amt ', ''); // payment amount $ cc = I ('Get. cc ', ''); // currency Type $ cm = I ('Get. cm ', ''); // additional custom parameter $ order_sn = I ('Get. item_number ', ''); // Order No. // here ..... perform other operations, you know. }}?>

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.