Amass

Perl’s Ternary Operator

July 26th, 2007 Luke

I always seem to forget exactly how Perl’s ternary operator works, and I always have trouble finding something on Google that explains how it works. So here’s how Perl’s Ternary Operator works:

my $var = $boolean_condition ? $true_value : $false_value;

Here’s some sample code:

#!/usr/bin/perl -w use strict; my $test = 0; my $ternary = $test?'true':'false'; print $ternary . "\n"; $test = 1; $ternary = $test?'true':'false'; print $ternary . "\n";

Which, when run under my console, returns this:

Luke@Wolverine:~ $ perl test.pl false true Luke@Wolverine:~ $

Implementing donation gifts using Paypal

July 20th, 2007 Luke

Have you ever seen that generic Paypal “Make a Donation” button? I see it a lot.

On some websites, they offer you gifts for donating. Kingdom of Loathing, for example, offers a special item to players who donate; this item then becomes immensely valuable to the other players because they either have to spend real-world money to get it, or pay an exorbitant amount of in-game money to get the item from the player who did spend real-world money to get it.

Have you ever wondered how they did it? It’s actually a lot simpler than you’d think. All you need is a Paypal account, and an authentication system for whatever you’re going to be taking donations for.

Creating a donate button

Creating the donate button is actually the simplest step in a series of simple steps. Once you have logged in to your Paypal account, just go to “Merchant Tools”. On that page you should see a box with the heading “Key Features”; click on “Donations”.

This is where you make your donation button. You even have the option of choosing a custom graphic if you have one(or can make one - I can’t). Virtually every piece of information you need to put in to the form is optional. I usually fill in the Donation Name/Service box, and leave the Donation Amount box blank so that users can donate as much or as little as they want to. If you want to force a user to donate a certain amount, you can enter that amount into the Donation Amount box.

Once you’ve filled in the boxes to your satisfaction, move to the buttons at the bottom of the form. Click on “Add More Options” to move on.

Setting up payment result pages

On this page, the first thing you’ll see are two boxes for URL’s. The first is for where a user should be redirected to if a payment is processed successfully, and the second is where they should be redirected if the payment fails. If all you’re interested in is setting up donation gifts, you will only need a successful payment URL. You’ll want to enter the URL to your donation gift script here.

Getting users their donation gift

When a user gets redirected to your successful payment script, they’ll (hopefully) still be logged in. If that’s the case, you can use whatever information you’re using to mark who is logged in to identify them and give them their free gift. Here’s some pseudo-code for your successful donation page:

if(userIsLoggedIn) { var userID = retrieveUserID(); var itemID = retrieveItemID(donationGift); giveUserItem(userID,itemID); ## VERY IMPORTANT redirect('your/homepage/or/something'); } else { ## User is not logged in -- display a message, ## and give them a support address to e-mail ## about their gift. }

The redirect once the user has gained their gift is important because it prevents users from seeing the URL they were originally redirected to. If they saw the URL they were sent to, it would be an easy job to just log in and copy/paste the URL that got them their gift over and over. Redirecting the user right after our processing has been finished fixes that problem.

And that’s all there is to it. Hopefully this mini-tutorial has helped you figure out how to start building donation gifts into your own websites; I’m pretty sure I’ve included enough information to at least get the ball rolling. If you’re having trouble, leave a comment.

Good Luck!