. NET Integrated PayPal demo

Source: Internet
Author: User

Recently the project needs to use PayPal (PayPal) to pay, study the interface, really spit blood, spent an afternoon + a night, repeatedly defeated repeatedly, the sea Amoy a number of documents, finally try to succeed, share, hope to use PayPal friends have helped.

PayPal offers 3 types of external payment interface, which can be consulted: Https://cms.paypal.com/c2/cgi-bin/?cmd=_render-content&content_ID=pages_c2/profile _comparison_new&fli=true

We use the standard Web version here, where the standard version of the Web page is divided into three ways:

1. Button mode, that is, a product a button

2. Shopping cart mode, let PayPal host your shopping cart

3. Your own shopping site has a shopping cart, payment when the shopping cart data submitted to PayPal to pay

Considering the question of freedom, of course we need a third way.

Preparatory work

1. To www.paypal.com to apply for a formal account, note that the card is currently only available with credit and debit cards, and need to support different currencies, or may fail to open.

2. Use the account you just requested to create a developer demo account in https://developer.paypal.com/, why? Because the developer account test does not require a fee

Here you need to create two accounts, buyers and sellers, and the default system will create sellers for you.

After the account has been created, be careful to change the login password for each account and change it in profile.

Oh, forget, create a buyer account, do not forget to recharge oh, recharge the amount of <10000 knife.

3. After setting up, we go to the sandbox to login to the seller account, click the link Enter sandbox site, with business type account login, and then do the following settings

What is the meaning of IPN instant payment notice? The principle of interaction is as follows:

We submit the shopping cart to PayPal, after the completion of payment, PayPal will want us to set up the IPN agent, send a message, so that our own program to know if the payment is successful, for subsequent operation, and this operation is called in the background, its main purpose is to prevent users to actively close the browser, An issue that causes the program order status to not be updated.

Payment automatic return settings, refers to the completion of payment after the jump to our own site interface, this is displayed to the user to see, and this receiving interface, PayPal will send the relevant order data, payment status until this page, that is, "payment data transfer (optional)" This, After the hook on the back of the URL interface to receive the relevant data (after the user payment is completed, the case of malicious shutdown)

4. Of course, the above two values in the account settings are global, you can in the specific submit button inside the set

5. On our shopping site, implement the shopping Cart interface code:

<form action="HTTPS://WWW.SANDBOX.PAYPAL.COM/CGI-BIN/WEBSCR"Method="Post"> <input type="Hidden"Name="cmd"Value="_cart"> <input type="Hidden"Name="Upload"Value="1"> <input type="Hidden"Name=" Business"Value="[email protected]">
<input type="Hidden"Name="Item_name_1"Value="San Francisco Bay (+ ' X32 ')"> <input type="Hidden"Name="amount_1"Value="1.00"> <input type="Hidden"Name="quantity_1"Value="2">
<input type="Hidden"Name="item_name_2"Value="Mount Hamilton (' x15 ')"> <input type="Hidden"Name="amount_2"Value="1.00"> <input type="Hidden"Name="quantity_2"Value="1">
<input type="Hidden"Name="Currency_code"Value="CNY">
<!--rewrite URL here, will overwrite global settings-<input type="Hidden"Name="return"Value="http://127.0.0.1:56508/ok.aspx"> <input type="Hidden"Name="Notify_url"Value="http://127.0.0.1:56508/pp.aspx"/>
<input type="Submit"Value="Upload Cart"alt="Make payments with Paypal-it ' s fastfree and secure!"/> </form>

6. Payment success interface code, i.e. return

stringStrformvalues =Request.Params.ToString (); stringStrnewvalue; stringstrresponse; stringServerURL ="HTTPS://WWW.SANDBOX.PAYPAL.COM/CGI-BIN/WEBSCR"; HttpWebRequest req=(HttpWebRequest) webrequest.create (ServerURL); Req. Method="POST"; Req. ContentType="application/x-www-form-urlencoded"; Strnewvalue="cmd=_notify-validate"; Strnewvalue="cmd=_notify-synch&tx="+ request.querystring["TX"] +"&at=access_token you."; //req. ContentLength = strnewvalue.length;StreamWriter StOut=NewStreamWriter (req.            GetRequestStream (), System.Text.Encoding.ASCII);            Stout.write (Strnewvalue);            Stout.close (); StreamReader stin=NewStreamReader (req. GetResponse ().            GetResponseStream ()); Strresponse=Stin.readtoend ();            Stin.close (); if(Strresponse.startswith ("SUCCESS"))            {            }            Else            {            }

7.IPN Background Data reception

//Post to either sandbox or live        stringStrsandbox ="HTTPS://WWW.SANDBOX.PAYPAL.COM/CGI-BIN/WEBSCR"; stringStrlive ="HTTPS://WWW.PAYPAL.COM/CGI-BIN/WEBSCR"; HttpWebRequest req=(HttpWebRequest) webrequest.create (Strsandbox); //Set values for the request backReq. Method ="POST"; Req. ContentType="application/x-www-form-urlencoded"; byte[] param =Request.BinaryRead (HttpContext.Current.Request.ContentLength); stringStrrequest =Encoding.ASCII.GetString (param); stringIpnpost =strrequest; Strrequest+="&cmd=_notify-validate"; Req. ContentLength=strrequest.length; //For proxy//WebProxy proxy = new WebProxy (The new Uri ("Http://url:p ort# ")); //req.        Proxy = proxy; //Send the request to PayPal and get the responseStreamWriter streamout =NewStreamWriter (req.        GetRequestStream (), System.Text.Encoding.ASCII);        Streamout.write (strrequest);        Streamout.close (); StreamReader StreamIn=NewStreamReader (req. GetResponse ().        GetResponseStream ()); stringStrresponse =Streamin.readtoend ();        Streamin.close (); //logging IPN messages ... be sure so give write//permission to process executing this code        stringLogpathdir = RESOLVEURL (""); stringLogPath =string. Format ("{0}\\{1}.txt", Server.MapPath (Logpathdir), DateTime.Now.Ticks); File.writealltext (LogPath, Ipnpost+" "+strresponse); //        if(Strresponse = ="verified")        {            //Check the payment_status is completed//Check that txn_id have not been previously processed//Check that Receiver_email is your Primary PayPal email//Check that payment_amount/payment_currency is correct//Process Payment        }        Else if(Strresponse = ="INVALID")        {            //Log for manual investigation        }        Else        {            //log RESPONSE/IPN data for manual investigation} Response.Write (strresponse);

8. finally listen to my words, the program is written well, be sure to publish to the public network IIS, and then test, or IPN is always invalid, is this bird problem let me engage in a night, gas dead.

OK, then there is a problem, you can contact me.

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.