Issue 76 * August 6 2009

PayPal Integration with On-Rev
let Paypal and on-Rev do the heavy lifting for you

by Mark Wieder

I needed to set up a web page to accept PayPal payments for my new tool, PowerTools. I also wanted to automate the process of generating a license code for the user after the payment was accepted and to send out a confirmation email to the user as well. In hindsight, it's pretty easy to do, thanks to simple on-rev scripts, although doing the legwork of finding out how to pass information to and from PayPal took a bit of doing.

The first thing to do is to go to http://www.paypal.com and set up a merchant account so that you can accept payments. Then you'll want to set up a payment button (log into your account, click on "Merchant Services" and select "Buy Now Button").

There are several options when you go to PayPal to create a button. I opted for the easiest one, having PayPal host the button for me. I don't have a need to host all that stuff on my own server and to deal with the payment mechanism myself, so I'm throwing all the heavy lifting onto PayPal. After you've created a hosted button on PayPal it's saved on their system and you can go back and edit it or clone it to create a similar one (log into your account, click on Profile, then under "Selling Preferences" select "My Saved Buttons").

When you go to create a hosted button there are three sections of info you can fill out. After you've done the first one (defining product, price, etc.), open up the third section ("Step 3:Customize Advanced Features") and there you get to define two urls, one for when the user cancels the transaction and one for a payment confirmation.

The payment confirmation part is what kept tripping me up. I had to create a sandbox account on PayPal and post actual credit card info in order to get any info posted back to my confirmation page.

So... this step is optional, but I highly recommend that you create a PayPal sandbox account on http://developer.paypal.com. Paypal will send you back a confirmation email, and once you've confirmed you can go ahead and post real financial transactions in the sandbox area without worrying about any repercussions.

Put a form in your payment page. Here's the one I use.

I've got three fields of my own in there: the field names are significant because PayPal uses them to autofill its payment form. It's not a big deal if that doesn't happen, but it's a nice feature and keeps the user from having to enter that info twice. Note: the "6887034" is the id of the hosted button I created on my account. You'll have to change that to the ide of the button you create if you want to receive the payment instead of having it all go to me.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="paypal">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="6887034">
<table align="top">
<tr>
<td>
<!-- <table width="400px" border="0" cellspacing="0" cellpadding="3"> -->
<table>
<tr>
	<td>First Name</td>
	<td><input name="first_name" type="text"></td>
</tr>
<tr>
	<td>Last Name</td>
	<td><input name="last_name" type="text"></td>
</tr>
<tr>
	<td>Email Address</td>
	<td><input name="email" type="text"></td>
</tr>
<tr>
<input type="hidden" name="rm" value="2"><p />
</table>
<tr>
<td>
<?rev
-- show the paypal link
	put "<input type=" & q("image") \
		&& "src=" & q("https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif") \
		&& "border=" & q(0) \
		&& "name=" & q("submit") \
		&& "alt=" & q("PayPal - The safer, easier way to pay online!") & ">"
	put "<img alt=" & quote & quote \
		&& "border=" & q(0) \
		&& "src=" & q("https://www.paypal.com/en_US/i/scr/pixel.gif") \
		&& "width=" & q(1) \
		&& "height=" & q(1) && ">"
?>
</td>
</tr>
</td>
</tr>
</table>
</form>
</p>


So I have a cancellation page ("nosale.irev") that just says "You canceled the transaction". But if a payment goes through then I bring up the "paymentconfirmation.irev" page. It loads the "license.irev" page from the cgi-bin folder (where I stashed it away to keep it safe from prying eyes), generates a license code, emails the license code in a cover letter to the user's email address, and prints the code on the confirmation page. Here's the only section of runrev code that matters on the "paymentconfirmation.irev" page:

<?rev
include "../cgi-bin/license.irev"
?>

There's debugging code commented out on the license.irev page that enabled me to see what info was coming back from PayPal. That was *very* important information, since I didn't see "payer_email" documented anywhere. You're also supposed to be able to send yourself an item of data as a passthrough, but I didn't get that to work successfully, and then I stopped trying when I got the email address field to come back properly. PayPal actually posts a lot of information back to you on a confirmed payment - I'm only interested in three fields here.

<?rev

function GenerateLicense pFirstName, pLastName, pEmailAddress, pMonth, pYear
	-- Generate a license code here
end GenerateLicense

-----------------------------------------------------
-- debugging: display what we got back from PayPal
-----------------------------------------------------
--put "POST:" && the keys of $_POST & "<br/>"
--put "GET:" && the keys of $_GET & "<br/>"
--put "SERVER:" && the keys of $_SERVER & "<br/>"
-----------------------------------------------------

	put $_POST["first_name"] into tFirstName
	put $_POST["last_name"] into tLastName
	put $_POST["payer_email"] into tEmailAddress
	put "<h3>"
	put "First Name:" && tFirstName & "<br/>"
	put "Last Name:" && tLastName & "<br/>"
	put "Email:" && tEmailAddress & "<br/>"

	local tDate, tMonth, tYear
	local tEmail
	local tTempFile
	local tDontSend
	local tContactEmail

	-- obfuscate the contact email address to discourage bots
	put "inf" & "o@" & "ahso" & "ftwa" & "re.net" into tContactEmail

	put the short english date into tDate
	put char -2 to -1 of tDate into tYear
	add 1 to tYear -- make a one-year license
	put char 1 of tDate into tMonth
	------------------------------------------
	-- Generate a license code here
	------------------------------------------
	put GenerateLicense(tFirstName, tLastName, tEmailAddress, tMonth, tYear) into tRegCode

	if "invalid" is not in tRegCode then
		put tRegCode & "<br/>"
		put "expires on" && line tMonth of the monthnames & " / "
		if tYear < 100 then
			put "20"
		else
			put "2"
		end if
		put tYear & "<br/>"

		------------------------------------------
		-- generate the confirmation email message text
		------------------------------------------
		put "Greetings" && tFirstName && tLastName & cr & cr into tEmail
		put "Thanks for your purchase of <YourProductHere>." & cr after tEmail
		put "Your <YourProductHere> registration code is:" & cr after tEmail
		put tRegCode & cr after tEmail
		put cr & "Your license to use <YourProductHere> will never expire, but" & cr after tEmail
		put "unless you renew your subscription before the expiration date" & cr after tEmail
		put "you will no longer be able to get updates after" after tEmail
		put space & line tMonth of the monthnames && "20" & tYear & cr after tEmail
		put cr & "-- " & cr after tEmail
		put tContactEmail & cr after tEmail
		-- save the text into a temporary file so the mail command can pick it up
		put tempname() into tTempFile
		put tEmail into url ("file:" & tTempFile)

		put true into tDontSend
		------------------------------------------
		-- comment this to avoid sending the email for testing
		put false into tDontSend
		------------------------------------------
		if tDontSend then
		-- send the confirmation email
		get shell("mail -s" \
			&& quote & "<YourProductHere> registration" & quote \
			&& tEmailAddress \
			&& "-- -f" & tContactEmail \
			&& "<" && tTempFile)
		end if
		if the result is not empty and the result is not 0 then
			put "there was an error emailing this information to your email address<br>"
		else
			put "(this information has been mailed to your email address)<br/>"
		end if
		delete file tTempFile
	else
		-- show the invalid license code
		put word 2 to -1 of tRegCode && tEmailAddress & "<br/>"
	end if
	put "</h3>"
?>

I hope this article was helpful to you! If you know of additional tips and tricks to use with Paypal, feel free to contact editor@runrev.com and perhaps they can be published here!

About the Author

Mark Wieder is a software developer with Ah, Software in Berkeley, California. You can find him at http://www.ahsoftware.net


Main Menu

What's New

tRev Editor