Ruby on Rails
Active Merchant PayPal Tips (Version #11)

There is not a lot of documentation for Active Merchant, so here are a few tips to help you out when using it for PayPal Payments Pro:

  • Go here to learn how to install Active Merchant
  • ActiveMerchant uses the Money library. The API is here
  • You will need to add the following code to your production.rb and/or development.rb environments. The after_initialize block is required as of Rails 1.2 to make sure your code runs only after plugins are initialized.

config.after_initialize do
	ActiveMerchant::Billing::PaypalGateway.pem_file =
		File.read(File.dirname(__FILE__) + '/../paypal/paypal.pem')
end

  • To get your PEM file, see these instructions on the PayPal website. Note that if you already requested credentials without a certificate, you will have to remove them and do a new request.
  • Run your development server on port 80 so the return url from the PayPal website works correctly
  • To create your payment gateway, use code similar to this:
    
    gateway = ActiveMerchant::Billing::Base.gateway(:paypal).new(
      :login => 'my_api_login',
      :password => 'MY_API_PASSWORD'
    )
    
  • For credit card processing, you will do something similar to the Authorize.Net examples.
  • For express payment where the user goes offsite, you will need to do something like this: (This code is based on Tom’s blog article with some bug of my own bug fixes)
  • This would go in an action method for signing them into their account
    response = gateway.setup_express_purchase(
    @order.total_price_in_cents, # Also accepts Money objects
    :order_id => @order.id,
    :return_url => url_for(:action => "paypal_success"),
    :cancel_return_url => url_for(:action => "paypal_cancel"),
    :description => @order.description
    )
    if response.success?
    url = ActiveMerchant::Billing::PaypalGateway.redirect_url_for(response.params“token”)
    redirect_to url
    end
  1. This would go in paypal_success
    response = gateway.get_express_details(params“token”)
    if response.success?
    @cart = Cart.find(response.params‘invoice_id’)
    # Now show some kind of confirmation screen
    # After user clicks ‘Confirm payment’ execute something like
    # this to secure the funds:
    response = paypal_gateway.purchase(
    @order.total_price_in_cents,
    nil,
    :express => true,
    :token => params“token”,
    :payer_id => params“PayerID”
    )

    if response.success?
    # Sing and dance, etc.
    end
    end
  • Use response.message to view error/status messages

Using the sandbox mode

To use Active Merchant in the sandbox mode, you have to add this line

      ActiveMerchant::Billing::Base.gateway_mode = :test
either in your environment.rb or your production.rb / development.rb or your controller file.

Issues with having both direct payment and paypal express:

If you initialized the paypal PEM file in environment.rb [or other environment file], then either direct payment or express payment fails with a “Security header is invalid” error. The root cause is initializing the PEM file in the environment files does not work correctly. Instead initialize the PEM file when you create the gateway like this:


@@pem_file = File.read(File.join(RAILS_ROOT, 'config', 'paypal', 'paypal.pem')) @@gateway = ActiveMerchant::Billing::Base.gateway(:paypal).new( :login => login, :password => pass, :pem => @@pem_file


—-

If you need to implement just the paypal express checkout function you may also use the PayPal Plugin (release by JadedPixel) that is better documented than ActiveMerchant. I wrote a simple tutorial on how using Payapal Express Checkout with Ruby on Rails that could be useful.

There is not a lot of documentation for Active Merchant, so here are a few tips to help you out when using it for PayPal Payments Pro:

  • Go here to learn how to install Active Merchant
  • ActiveMerchant uses the Money library. The API is here
  • You will need to add the following code to your production.rb and/or development.rb environments. The after_initialize block is required as of Rails 1.2 to make sure your code runs only after plugins are initialized.

config.after_initialize do
	ActiveMerchant::Billing::PaypalGateway.pem_file =
		File.read(File.dirname(__FILE__) + '/../paypal/paypal.pem')
end

  • To get your PEM file, see these instructions on the PayPal website. Note that if you already requested credentials without a certificate, you will have to remove them and do a new request.
  • Run your development server on port 80 so the return url from the PayPal website works correctly
  • To create your payment gateway, use code similar to this:
    
    gateway = ActiveMerchant::Billing::Base.gateway(:paypal).new(
      :login => 'my_api_login',
      :password => 'MY_API_PASSWORD'
    )
    
  • For credit card processing, you will do something similar to the Authorize.Net examples.
  • For express payment where the user goes offsite, you will need to do something like this: (This code is based on Tom’s blog article with some bug of my own bug fixes)
  • This would go in an action method for signing them into their account
    response = gateway.setup_express_purchase(
    @order.total_price_in_cents, # Also accepts Money objects
    :order_id => @order.id,
    :return_url => url_for(:action => "paypal_success"),
    :cancel_return_url => url_for(:action => "paypal_cancel"),
    :description => @order.description
    )
    if response.success?
    url = ActiveMerchant::Billing::PaypalGateway.redirect_url_for(response.params“token”)
    redirect_to url
    end
  1. This would go in paypal_success
    response = gateway.get_express_details(params“token”)
    if response.success?
    @cart = Cart.find(response.params‘invoice_id’)
    # Now show some kind of confirmation screen
    # After user clicks ‘Confirm payment’ execute something like
    # this to secure the funds:
    response = paypal_gateway.purchase(
    @order.total_price_in_cents,
    nil,
    :express => true,
    :token => params“token”,
    :payer_id => params“PayerID”
    )

    if response.success?
    # Sing and dance, etc.
    end
    end
  • Use response.message to view error/status messages

Using the sandbox mode

To use Active Merchant in the sandbox mode, you have to add this line

      ActiveMerchant::Billing::Base.gateway_mode = :test
either in your environment.rb or your production.rb / development.rb or your controller file.

Issues with having both direct payment and paypal express:

If you initialized the paypal PEM file in environment.rb [or other environment file], then either direct payment or express payment fails with a “Security header is invalid” error. The root cause is initializing the PEM file in the environment files does not work correctly. Instead initialize the PEM file when you create the gateway like this:


@@pem_file = File.read(File.join(RAILS_ROOT, 'config', 'paypal', 'paypal.pem')) @@gateway = ActiveMerchant::Billing::Base.gateway(:paypal).new( :login => login, :password => pass, :pem => @@pem_file


—-

If you need to implement just the paypal express checkout function you may also use the PayPal Plugin (release by JadedPixel) that is better documented than ActiveMerchant. I wrote a simple tutorial on how using Payapal Express Checkout with Ruby on Rails that could be useful.