Ruby on Rails with Paypal

I couldn’t find a good example of a Ruby on Rails Paypal Website Payments Standard implementation that I could open the hood and dig around in. The majority of the ones I found were partially implemented, geared at Website Payments Pro ($30/month), or were commercial products. So I decided to write a test to see how it all worked.

DISCLAIMER[0]: I am NOT a good Ruby on Rails coder. I am from a Java world and still find the world of closures and dynamically typed variables a little disorienting. That said, things could definitely be cleaned up and made to work better. I am open to house cleaning suggestions.

DISCLAMER[1]: This was done over the past couple months in my spare time only so there are likely bugs. I have only tested this in the Paypal Sandbox and have NOT used it in any production capacity. If you plan on using this in a production environment DO NOT assume that it all works correctly. TEST! Let me know if you find any bugs.

Let’s jump in.

The first thing you should know is that accessing the cart automatically and randomly generates and stores down inventory so you don’t have to worry about it.

This test covers the following scenarios:

  • Basic connection using form variables and posting in the same window.
    This is the most straight forward scenario. The page has hidden fields that contain all the information needed to start the payment process. When the user click the checkout button they are directed over to Paypal for payment. They then have the option of returning to the site after the payment process completes or if they decide to cancel.
  • Return URL order detail validation.
    When the user clicks “Return to Paypal Test Site” on the payment confirmation page within Paypal this site then validates the data submitted from that click in order to ensure payment and order details are correct. NOTE: This is not secure and was just the first step of the test. Don’t do this in real life!
  • Payment data transfer (PDT) order detail validation.
    PDT basically sends an encrypted token back which can then be posted to Paypal to get payment and order details. This allows the server to verify details about the transaction, removing the ability for users to change the validation data. Note that PDT only happens if the user returns to the site from the payment confirmation page within Paypal. All but the first scenario use PDT and IPN together.
  • Instant payment notification (IPN) order detail validation.
    IPN is the same as PDT, only it happens regardless of whether the user returns to the site. All but the first scenario use PDT and IPN together.
  • Page level redirection to Paypal.
    This hides the paypal form tags on a redirect page that is only displayed briefly. This moves the Paypal form variables off the cart page, where they tempt people to try to change them, off into a briefly displayed redirect page. This by no means offers any real security, however it does obscure the process a little bit, making it less tempting to play with. IPN and PDT are in place for this option as well.
  • Controller level redirection – not fully working.
    The idea behind this one is that it passes all the Paypal form fields across at the server level, removing the ability for users to interact with or change them. This uses the Net::HTTP code to do some funky POSTs and redirects but is failing at the moment. I have the code so that it submits via Net:HTTP in the controller and follows the redirects, however it is not transferring cookie or form data correctly (not sure which/either), which causes Paypal to redirect to an error page. I would be very interested to see if anyone can get this one working.
  • DHTML popup window payments.
    This is the same basic concept as the standard flow with page level redirection only the Paypal site is displayed in a centered popup window. Cancelling the paypal transaction simply closes the popup, leaving you still at the shopping cart. Completing the transaction redirects the entire window to the payment confirmation page.

Changes that you will need to make to get this working:

  1. Update models/util.rb to point to your email addresses and Paypal sandbox info.
  2. Update config/environments/development.rb to point to your SMTP server.
  3. Update the controllers/website_payment_standard_controller.rb PDT_IDENTITY_TOKEN variable to point to match your PDT identity token.

NOTE: I removed the Test folders to lighten the load and quickly remove a bunch of SVN folders but didn’t try it out after this. If you are having any errors revolving around tests, create a new project then copy the test folder and its contents over to this one.

The files:  paypal.zip

Enjoy!


The Impending Auto Bubble

Right now the big 3 US car makers, GM, Ford, and Chrysler, are hurting. Bad. In fact, they are hurting so bad that they are telling congress that they need a portion of the bailout money in order to stay afloat.

So what happens if they go under? My prediction is that we will see a very short bubble in the auto industry. The reason for this is basic supply and demand. Let’s explore this with a simple example.

Right now let’s say that we have Ford, GM, Chrysler, Toyota, Honda, and Mazda operating in the United States and no others. Each one can produce 100 cars per day at maximum capacity but demand only has them producing at 75% capacity. So right now things are in harmony where demand equals supply with 450 cars per day being produced.

But now we are shutting three of them down because they have been mismanaged for years (or any other of the 1,000 reasons why they are hurting now) and we don’t want to bail them out. So now we must remove their daily production and capacity from the mix – to keep it simple we will assume that none of them are bought by the other auto makers. This takes the supply down to 225 cars per day yet the demand still sits at 400 cars per day. To accommodate this reduced supply the remaining car makers max their output, raising it up to 300 cars per day.

This is still a supply deficit though. The demand is still 100 cars per day higher than the supply is able to accommodate. This will cause two changes.

First, the remaining auto makers will increase production capacity to account for the variance. This will take time and will be a long term solution.

Second, the auto makers will raise prices. In order to try to reach an equillibrium where they are producing at optimal efficiency they will raise the prices to increase profits and lower demand. Likewise, the lessened availability of cars in the market will reduce the purchasing power of consumers, allowing dealerships to raise the prices without an immediate penalty for doing so.

From an economic standpoint this will have a number of effects. First, the dealerships and auto makers will increase their short term profits. Second, the auto makers will grow larger. Third, autos may actually appreciate slightly, at least for the short term. Finally, autos will be overvalued, creating a severe negative equity situation.

The first and second points are pretty straight forward. As the prices rise but costs remain relatively stable the auto makers and dealerships will recognize an increased profit. This increased profit will help to fuel growth, which must occur to stabilize the supply and demand disparity, which will cause the companies to grow.

The third point, that autos may appreciate slightly, comes from the same supply and demand concept that caused the prices to rise. This doesn’t mean that the new car you bought will be worth more than you paid for it. It simply means that the car you have had for a while that has severely depreciated may come back up a bit. For example, if you bought a car at $30,000 and it was valued at $17,000 after two years you may find that this bubble causes the depreciation to lessen slightly, making the value of the car $18,000.

The last point is the most scary from an economic viewpoint. As prices rise they will reach artificial levels that would not stand in the typical economy where supply and demand were fairly close to one another. These artificially high prices mean that, at least in the short term, people will have to pay more for vehicles. This additional purchase price will cause people to have higher payments for less real value. Once the market corrects itself the real value of the vehicles will shift downward, leaving a large chunk of negative equity. This will eventually cause hardships like we hae seen with the housing bubble, albeit not of the same magnitude.

Let’s use an example here. Keep in mind that the numbers are illustrative only and will not add up correctly.

My car breaks down and dies during the height of the auto bubble. I go and buy a brand new BMW for $45,000. This car would ordinarily be priced at $35,000 but the bubble has artificially inflated the price. I am forced to take a monthly payment of $650 per month (I had no money to put down) in order to own the car. As the bubble breaks the price of the car drops back down to its standard price of $35,000. So the car that I bought at $45,000 is now instantly worth $10,000 les than it was. In addition, the payments of $650 per month are not reflective of the value of the car so no banks will finance a refinance loan (at least that will drop the payments) because of the extreme amount of negative equity. Likewise, I can’t sell the car on the used market because I need more to pay it off than what you can buy a new car for. The payments eventually become too much – perhaps I lost my job, made some bad decisions, got sick, etc. – and I default on my loan. To recoup some losses the bank turns around and sells my car at a high discount from the current market price, in order to liquify it quickly, and takes a huge loss. Now magnify this situation across the United States.

Sound familiar? It should – this is exactly what happened with the housing bubble. If the big 3 go under and are not purchased by competitors then we could see such a situation. It is our responsibility to be vigalent and prevent another bubble from hurting ours or the world economy. The solution is pretty simple: don’t by things for more than they are worth. If a car is worth $20,000 today and $25,000 tomorrow, that should raise a red flag. Just be a smart consumer and the bubble will remain small, isolated, and insignificant.


Tipping the Scales

Every organization needs procedures. What needs to happen when I file a bug? How do I notify clients that their feature is in production? Who do I bill this work to? All of these are simplified by procedure. Instead of having to figure out the issue and devise a solution every time the problem arises, procedures allow us to base our actions on past thought and designs.

Procedures can be good but they can also create more problems than they are worth. When procedures reach a point to where you are spending more time trying to find the right procedure than you would if you just figured out the problem from scratch, they are a burden.

A happy medium should be targeted. This middle ground should ultimately save time – otherwise it is a wasteful venture. If you have to spend long amounts of time searching for or through documents just to figure out what is supposed to happen then the system is a failure.

Next time you are going to add a procedure you should stop to ask yourself the following:

1. Does this solve a tough problem?
Does this procedure save people an inordinate amount of time or money? Does this procedure address an issue that is difficult to figure out on a case by case basis? If either of these are true then the procedure is likely to be a good addition.

2. Is this going to simplify things or add undue process?
Will people have to take a lot of extra steps just for the sake of procedure? If any part of the procedure is there just for the sake of it and doesn’t produce a tangible output then the overall process is diminished.

3. How easy will this be to remember?
If a procedure is hard to remember then people will need to look it up each time. This, by itself, doesn’t necessarily negate usefulness. However, if people have to look it up and it is tough to find then the process becomes much less useful and less likely to succeed.

4. How often is this likely to happen and what is the importance when it happens?
If the item in question rarely happens and/or doesn’t have a lot of value associated with it then there is likely not a need for detailed procedures. For example, declaring war doesn’t happen that often but has a high cost so a procedure is needed. Conversely, working showstoppers happens far more frequently but has a relatively low cost.