ActiveMerchant is a plugin that interfaces with payment gateways. I'm using it to connect to, so all information presented here is based on that premise. It's easy to use, but there are a few details to be aware of.



ActiveMerchant operates in a special test mode. Configure your Rails app accordingly:

unless RAILS_ENV == 'production'
  ActiveMerchant::Billing::Base.mode = :test 

Put this code in your config/environment.rb file.

You're now ready to follow the instructions from the sites mentioned above (under Sources). First, however, we're going to make a small modification to ActiveMerchant. By default, ActiveMerchant tries to communicate with paypal through ssl. In Paypal's sandbox however, ssl only seems to work through port 80 (instead of the default 443 ssl port). Let's adjust ActiveMerchant accordingly:

In the file vendor/plugins/activemerchant/lib/active_merchant/lib/posts_data.rb, look for the method ssl_post. Now change the method definition to look like this:

    def ssl_post(url, data, headers = {})
      uri   = URI.parse(url)

      mode = ActiveMerchant::Billing::Base.mode
      case mode                  
      when :test
        port = 80 # Apparently paypal uses port 80 for testing purposes
        port = uri.port
      #http =, uri.port)
      http =, port)

Here, we change the http port to 80 if we're in test mode.

We can take this one step further: why not communicate in plain text, while we're testing? In the same file, add this method:

    def plain_post(url, data, headers = {})
      uri   = URI.parse(url)      
      http =, 80), data, headers).body   

This is just a trimmed down post method. If you want to, you can copy the error handling over from def ssl_post. Now, move to vendor/plugins/activemerchant/lib/active_merchant/billing/integrations/paypal/notification.rb. In this file, modify the acknowledge method:

  def acknowledge(method = 'ssl_post')
    payload =  raw            
    response = self.send(method, Paypal.service_url + '?cmd=_notify-validate', payload,
      'Content-Length' => "#{payload.size}",
      'User-Agent'     => "Active Merchant --")
    raise"Faulty paypal result: #{response}") unless ["VERIFIED", "INVALID"].include?(response)  
    response == "VERIFIED"

Here, we call ssl_post by default (so ActiveMerchant's default behavior is not in any way altered). But if you call the acknowledge method in your payment_controller, you can now supply the argument plain_post if you're in test mode and don't want to use ssl at all:

  def notify    
    notification =   

    if notification.acknowledge('plain_post')
      ... (etc.) ...

Exposing IPN Variables

If you use Paypal's Instant Payment Notification (IPN) service, Paypal will keep you informed about the details of the transaction by posting the IPN variables to a url on your server. ActiveMerchant exposes these variables for you, so you can do stuff like:

      transaction = => notification.status,
                                    :order_id =>,
                                    :user_id => game.user_id,
                                    :invoice => notification.invoice.to_s,
                                    :currency_code => notification.currency.to_s,
                                    :amount => notification.amount.to_s,
                                    :invoice_date =>,
                                    :raw_paypal_data => request.raw_post)

The IPN variables are stored in an object of class Notification. By default, the custom field (which you can use to store your own value) is mapped to item_id because PayPal doesn‘t return item_number in dispute notifications. That's great, but what if we really need to send along our own custom variable? Here's a fix, applied in ActiveMerchant::Billing::Integrations::Notification:

  # Expose the custom variable separately (added by Onno Schuit, o.schuit residing_at_or_around
  def custom

Personal Tools