Paypal Setup


1. Setup and configure your receiver paypal account.
2. Create your simple "SandBox" Buy Now order page in ASP.
3. Create your "SandBox" IPN processing page in ASP.
4. Create your "SandBox" PDT processing page in ASP.
5. Setup and configure your "SandBox" paypal accounts.
6. Successfully Testing with the PayPal "SandBox".
7. Finally, getting everything to work live.

1. Set up and configure your receiver paypal account.
a) Go to www.paypal.com and create your account. EASY! (Make sure you do all the account verification stuff.)
b) Log in to the account and go to My Account...Profile
c) Under "Selling Preferences" on the right, click "Instant Payment Notification Preferences".
d) Check the "Instant Payment Notification integrates..." checkbox.
e) Enter the URL of what will be your IPN processing page. (Ex. http://www.yourdomain.com/pp_ipn.asp)
f) Click Save.
g) Click "Back to Profile Summary".
h) Under "Selling Preferences" on the right, click "Website Payment Preferences".
i) Check "On" for "Auto Return".
j) Enter the URL of what will be your PDT processing page. (Ex. http://www.yourdomain.com/pp_pdt.asp)
k) Under "Payment Data Transfer (optional)" check "On" for "Payment Data Transfer".
l) If your identity token is listed there, copy it and paste it somewhere you won't lose it.
m) The rest of the options on this page are optional, do as you please then click Save.
n) If your identity token wasn't listed before it will be displayed now. Copy and save for later (be sure you identify this token as your live PayPal Ident token.)


2. Create your simple "SandBox" Buy Now order page in ASP.
a) It will be up to you to build your forms/buttons however you like, but you can use this code for reference:

'---- HTML/ASP CODE -----













ItemDescriptionCost
PRODUCT NAME HEREDESCRIPTION HEREPRICE HERE





















'---- END HTML/ASP CODE -----

b)notice the "form" action... www.sandbox.paypal.... all you have to do make this form go to PayPal live (When you're ready) is remove the ".sandbox" from the URL.


3. Create your "SandBox" IPN processing page in ASP (which will insert a new row to a mySQL database table, assuming you've created one. No tutorial here for mySQL ;P)
a) Here's the basic ASP code:

'---- ASP CODE -----

<%
Set Conn = Server.CreateObject("ADODB.Connection")

'Choose a connectionstring method (DSN or DSNless)
'DSN = "DRIVER={mysql};SERVER=your DB IP;DATABASE=theDBName;User ID=theUserID;Password=thePassword"
DSN = "DSN=theDSNName"
Conn.open DSN

Dim Item_name, Item_number, Payment_status, Payment_amount
Dim Txn_id, Receiver_email, Payer_email
Dim objHttp, str

' read post from PayPal system and add 'cmd'
str = Request.Form & "&cmd=_notify-validate"

' post back to PayPal system to validate, choose an XMLHttp method.
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
' set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.4.0")
' set objHttp = Server.CreateObject("Microsoft.XMLHTTP")

'To Go Live, uncomment the first line below and comment out the second. (When Ready)
'objHttp.open "POST", "https://www.paypal.com/cgi-bin/webscr", false
objHttp.open "POST", "https://www.sandbox.paypal.com/cgi-bin/webscr", false
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.Send str

' assign posted variables to local variables
Item_name = Request.Form("item_name")
Item_number = Request.Form("item_number")
Payment_status = Request.Form("payment_status")
Payment_amount = Request.Form("mc_gross")
Payment_currency = Request.Form("mc_currency")
Txn_id = Request.Form("txn_id")
Receiver_email = Request.Form("receiver_email")
Payer_email = Request.Form("payer_email")

'---------VERY IMPORTANT----------
'Your custom option variables will NOT be named anything you'd expect. They will come as: "option_selection#"
myCustomOption = Request.Form("option_selection1")

'You can uncomment this if you want to store All the Form data in your DB.
'allForm = Request.Form

'--------The SQL Inserts assume you have a table with an auto inc. ID and 4 other fields (TXN_ID, USERID, PRODUCTID, STATUS)

' Check notification validation
if (objHttp.status <> 200 ) then
' HTTP error handling
sqlIns = "INSERT INTO paypal (txn_id, uid, pid, status) VALUES ('0', 0, 0, 'ERROR 200')"
Conn.Execute(sqlIns)
Conn.Close()
elseif (objHttp.responseText = "VERIFIED") then
'You can do a bunch of stuff here to ensure everything is kosher (prevent double orders, etc...), but I leave that to you.
' check that Payment_status=Completed
' check that Txn_id has not been previously processed
' check that Receiver_email is your Primary PayPal email
' check that Payment_amount/Payment_currency are correct
' process payment

sqlIns = "INSERT INTO paypal (txn_id, uid, pid, status) VALUES ('" & Txn_id & "', " & userid & ", " & Item_number & ", '" & Payment_status & "')"
Conn.Execute(sqlIns)
Conn.Close()
elseif (objHttp.responseText = "INVALID") then
sqlIns = "INSERT INTO paypal (txn_id, uid, pid, status) VALUES ('" & Txn_id & "', " & userid & ", " & Item_number & ", 'INVALID')"
Conn.Execute(sqlIns)
Conn.Close()
else
Response.write("ERROR")
' error
end if
set objHttp = nothing
%>

'---- END ASP CODE -----

b) Voila you've got an IPN processing page. If you finish this tutorial, this page will insert a row into your table with some basic info about the order.



4. Create your "SandBox" PDT processing page in ASP.
a) Use this code to get started building your PDT return page:

'---- ASP CODE -----
<%
Dim authToken, txToken
Dim query
Dim objHttp
Dim sQuerystring
Dim sParts, iParts, aParts
Dim sResults, sKey, sValue
Dim i, result
Dim firstName, lastName, itemName, mcGross, mcCurrency

'----------NOTICE THIS-----------
'These are a good place to store the Identity Tokens, one each for Sandbox and Live. (The ones below aren't real, use your own!)
'Sandbox
'AKSJDLKJASDLKJASUFLKJASFKJHLDKJFHSDSDF
'Live
'LKADUJFKLAJF:LKJASL:FKJALKJSDFKLASJDLKASJD

'Just put the appropriate token in for what your'e doing.
authToken = "0LVKu6lDhAjedNtazwpi0Nl2YVKuxbs_B45itcaWcmr0fJUmJ2N2bQCwB4e"
txToken = Request.Querystring("tx")

query = "cmd=_notify-synch&tx=" & txToken & "&at=" & authToken

'set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")

'To Go Live, uncomment the first line below and comment out the second. (When Ready)
'objHttp.open "POST", "http://www.paypal.com/cgi-bin/webscr", false
objHttp.open "POST", "http://www.sandbox.paypal.com/cgi-bin/webscr", false
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.Send query

sQuerystring = objHttp.responseText

If Mid(sQuerystring,1,7) = "SUCCESS" Then
sQuerystring = Mid(sQuerystring,9)
sParts = Split(sQuerystring, vbLf)
iParts = UBound(sParts) - 1
ReDim sResults(iParts, 1)
For i = 0 To iParts
aParts = Split(sParts(i), "=")
sKey = aParts(0)
sValue = aParts(1)
sResults(i, 0) = sKey
sResults(i, 1) = sValue

Select Case sKey
Case "first_name"
firstName = sValue
Case "last_name"
lastName = sValue
Case "item_name"
itemName = sValue
Case "mc_gross"
mcGross = sValue
Case "mc_currency"
mcCurrency = sValue
End Select

Next
OrderOutput = ""
OrderOutput = OrderOutput & "Thank you, " & firstName & " " & lastName & ", for your order! As soon as our system is notified that the payment of $" & mcGross & " " & mcCurrency & " has been completed, your " & Replace(itemName, "+", " ") & " will be added to your account.

Please note that this may already have happened or may take a few moments depending on the type of payment method you have chosen.

If there is any problem with your order, please contact us immediately and we will resolve the matter."
Else
'log for manual investigation
OrderOutput = "There has been an error processing your payment. This may have only been an issue with the redirect to our site. If this is the case, your product should already be added to your account. If this is not the case, please contact us to have us investigate the issue."
End If

Response.write (OrderOutput)
%>

'---- END ASP CODE ------

b) Voila! A PDT Processing page. This will spit out a conf. to the user that payment was received.


5. Setup and configure your "SandBox" paypal accounts.
a) This is tricky, but doable...
c) Here we go...
d) Go to https://developer.paypal.com/
e) Click Sign Up Now
f) Click Fill in all the info they ask for.
g) Click Sign Up.
h) Go to your email and follow instructions.
i) Once you can log in to the Developer Central, click "SandBox, create and manage Sandbox accounts"
j) The account you created will be your business one. You can create another to use as a test user.
k) Once you go through all the steps to create a test user, log in to that test user sandbox paypal account.
l) To add a credit card go to "My Account...Profile", then click "Credit Cards" under "Financial Information" to get your self a bogus credit card number for testing.
k) once the test user is verified, you can start testing!!!
l) Make sure you follow all the steps in #1 at the top of this tutorial for your new sandbox business account as well. You'll need to make sure the accounts IPN and PDT are set up and that you've recorded your Identity Token for the PDT Processing page.

6. Successfully Testing with the PayPal "SandBox".
a) Now if you've done everything above:
all your PayPal accounts (One sandbox biz, one sandbox user, one live biz)
are setup (with IPN and PDT activated and configured)
and you should have three ASP pages (orderform.asp, pp_ipn.asp and pp_pdt.asp)
and you should have two identity tokens (one sandbox and one live)
b) If you've basically copied and pasted the code for the asp pages, they should all be set up to use sandbox. If so, GREAT!
c) Load your orderform.asp in your browser. Click your Buy Now button.
d) You should be brought to a PayPal SandBox site with the order info and login request.
e) Log in using your test user account.
f) click pay.
g) You should then be brought back to your pp_pdt.asp page and it should show your order information with some other text. If not, you messed up somewhere.
h) Now if you check your database, you should see a new row with the basic data that the pp_ipn.asp inserted invisibly by the hidden PayPal server communication.

7. Finally, getting everything to work live.
a) Swap all URLs in all files from www.sandbox.paypal.com... to www.paypal.com...
b) Swap the identity tokens in the pp_pdt.asp file for the live ones.
c) THAT'S IT!!! Make some $$$$$$$$$

If you followed this tutorial to a T, and I didn't screw something up somewhere, you should now have a pretty darned good starting point for your PayPal enabled site.

Comments

Popular posts from this blog

input stream or file to X509Certificate

How to rebase chain of git changes on master ?