Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to send an email when a "payment received" comes from paypal.

Status
Not open for further replies.

wvdba

IS-IT--Management
Jun 3, 2008
465
0
0
US
hi.
i have an asp (classic) page that takes a customer's orders, places it in a database and sends an email that their order has been received and confirmed. no problems so far. my questions is: is there anyway to check the emails coming every so many minutes to see if a payment has been received on paypal? when a payment is received from paypal, an email is sent to the recipient. any ideas would be appreciated.
thanks.
 
Paypal IPN (Instant Payment Notification) can be set up so that once a payment has been received it will look for a page on your server, and call the code.

Im using a off the shelf ecommerce package in asp, and it does exactly that.

So maybe your best seeing how to deal with Paypal IPN in asp.

 
thanks lagc.
the code that i looked at for IPN is in php. i need to get the asp version. is there anyway to get this code in asp?
thanks.
 
lagc,
thanks for your posts.
my difficulty is this: these codes that you posted, are for a form on the website that takes the customer to paypal to pay for the item via your website. but, sometimes the customers justs place the order and pay for it later. so, after the order is placed in the database and an email is sent to the customer that their order has been confirmed, 3 days later, the customer goes to paypal and pays an amount using our email in paypal. then paypal sends us an email "instant payment notification". what i need to do is when this email is received, to send the customer an email that their payment has been received and update the payment status in the database. how can this be done? by checking email? by polling paypal account? how?
 
Yes I see.

Well I can do for you my friend is post the exact same code that was in the package I bought that deals with the IPN.

It will obviosuly be set up for my system, but maybe you can pick the bones out of it and use some of it for yourself. Otherwise I cant help you anymore I'm sorry.

Code:
<%
'Work Fields
dim strReply
dim objHttp
dim httpStatus
dim httpResponseText
dim formattedDateTime
dim ordertotal

'Values sent by PayPal
dim payment_status
dim receiver_email
dim item_number
dim amount

'Database
dim mySQL
dim conntemp
dim rstemp
dim rstemp2

'Session
dim idOrder
dim idCust

'************************************************************************
 
'Open Database Connection
call openDb()

'Store Configuration
if loadConfig() = false then
	call errorDB(LangText("ErrConfig",""),"")
end if

'Get a Date and Time formatted to the user's specifications
formattedDateTime = formatTheDate(currDateTime("DT",timeOffSet)) & " " & currDateTime("T",timeOffSet)

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

'Extract some info we will need to update the Database if "VERIFIED"
payment_status	= trim(Request.Form("payment_status"))
receiver_email	= trim(Request.Form("receiver_email"))
item_number		= trim(Request.Form("item_number"))
amount			= trim(Request.Form("mc_gross"))

'Check the item_number and Order Status
if len(item_number) = 0 or not isNumeric(item_number) then
	Response.Clear
	Response.Write "PayPal IPN : Invalid value in Item_Number field."
	Response.End
else
	'Check that the order status is "Pending"
	mySQL = "SELECT orderStatus, total " _
		  & "FROM   cartHead " _
		  & "WHERE  idOrder = " & validSQL(item_number,"I")
	set rsTemp = openRSexecute(mySQL)
	if rsTemp.eof then
		Response.Clear
		Response.Write "PayPal IPN : Order could not be located."
		Response.End
	else
		if rsTemp("orderStatus") <> "0" then 
			Response.Clear
			Response.Write "PayPal IPN : Order status must be pending."
			Response.End
		end if
	end if
	OrderTotal = rsTemp("total")
	call closeRS(rsTemp)
end if

'Create XML object
on error resume next
set objHttp = server.Createobject(MSXMLprogID)
if err.number <> 0 then
	Response.Clear
	Response.Write "PayPal IPN : Could not create XML HTTP object."
	Response.End
end if
on error goto 0

'Open connection to LIVE server (PayPal)
if payModePayPal = "0" then
	objHttp.open "POST", "[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr",[/URL] false
else
	objHttp.open "POST", "[URL unfurl="true"]https://www.sandbox.paypal.com/cgi-bin/webscr",false[/URL]
end if


'Send reply
objHttp.Send strReply

'Get response
httpStatus		 = objHttp.status
httpResponseText = UCase(trim(objHttp.responseText))
set objHttp		 = nothing

'Validate response
if httpStatus <> 200 then
	call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : HTTP Error " & httpStatus)
	Response.Write "PayPal IPN : HTTP Error " & httpStatus
else
	if httpResponseText = "VERIFIED" then
		if lCase(payPalMemberID) = lCase(receiver_email) then
			if lCase(payment_status) = "completed" then
				if request.form("test_ipn") = 1 then item_number = -item_number
				if CCur(OrderTotal) = CCur(amount) then
					call updOrderStatus(item_number,"1","Y","Y","DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : Status = " & payment_status)
					Response.Write "PayPal IPN : Status = " & payment_status
				else
					call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : Amount: " & amount & vbCrLf & "Order: " & ordertotal)
					Response.Write "PayPal IPN : Amount (" & amount & ") does not equal order total (" & ordertotal & ").<br>"
				end if
			else
				call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : Status = " & payment_status)
				Response.Write "PayPal IPN : Status = " & payment_status
			end if
		else
			call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : Invalid Email = " & receiver_email)
			Response.Write "PayPal IPN : Invalid Email = " & receiver_email
		end if
	else
		if httpResponseText = "INVALID" then
			call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : INVALID Response")
			Response.Write "PayPal IPN : INVALID Response"
		else
			call updOrderPrivate(item_number,"DATE : " & formattedDateTime & vbCrLf & "PayPal IPN : ERROR Response")
			Response.Write "PayPal IPN : ERROR Response"
		end if
	end if
end if

'Close Database connection
call closeDB()
%>
 
Hi guys,

I´ve been searching for a solution for an HTTP-API call using VBScript in Classic ASP.

Thanks to this post I´ve resolved my issue.

Thank you!!

Frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top