This gem is a lightweight wrapper for the paypal adaptive payments API.

This is very much a work in progress! Use at your own risk or submit bug fixes :)

Before you need start, read the API manual and check out the forums on It'll be invaluable for parameters and error messages. The gem keeps the request/response as hashes so you will have to read the manual to make the proper calls. I made a few test cases for further examples at

Changes to the Ross Hale Version

This branch adds support for logging paypal requests and responses. Also "rake test" should work now. See the example below and PreapprovalTest#test_invalid_preapproval_with_logger.

Changes to the Lottay Version

This branch adds support for PayPal Adaptive Accounts. An Adaptive Accounts request looks very similar to the Pay Request. You simply post a hash of signup parameters to PayPal using the "create_account" method, and receive a URL to which you redirect the user to finish signup.

See the unit tests in "create_account_test.rb" for a good example.

Installation Instructions

Install the gem: sudo gem install astrails-paypal_adaptive --source In environment.rb: require 'paypal_adaptive'


Create paypal_adaptive.yml to your config folder: development: environment: "sandbox" username: "sandbox_username" password: "sandbox_password" signature: "sandbox_signature" application_id: "sandbox_app_id" sandbox_email: "paypal_account_email_with_your_sandbox_account"

  environment: "sandbox"
  username: "sandbox_username"
  password: "sandbox_password"
  signature: "sandbox_signature"
  application_id: "sandbox_app_id"
  sandbox_email: "paypal_account_email_with_your_sandbox_account"

  environment: "production"
  username: "my_production_username"
  password: "my_production_password"
  signature: "my_production_signature"
  application_id: "my_production_app_id"

Make the payment request:

pay_request =

data = {
"returnUrl" => "", 
"requestEnvelope" => {"errorLanguage" => "en_US"},
"receiverList"=>{"receiver"=>[{"email"=>"", "amount"=>"10.00"}]},

pay_response =

if pay_response.success?
  redirect_to pay_response.approve_paypal_payment_url
  puts pay_response.errors.first['message']
  redirect_to failed_payment_url

To log the request and response: class Logger def request(path, request_data, headers) # TODO: save to db/file/whatever end

  def response(status, response_data)
    # TODO: save to db/file/whatever


logger =
pay_response = => logger))

Once the user goes to pay_response.approve_paypal_payment_url, they will be prompted to login to Paypal for payment.

Upon payment completion page, they will be redirected to

They can also click cancel to go to

The actual payment details will be sent to your server via "ipnNotificationUrl" You have to create a listener to receive POST messages from paypal. I added a Rails metal template in the templates folder which handles the callback.

Additionally, you can make calls to Paypal Adaptive's other APIs: payment_details, preapproval, preapproval_details, cancel_preapproval, convert_currency, refund

Input is just a Hash just like the pay method. Refer to the Paypal manual for more details.


0.1.1 Added logging support.

0.1.0 Fixed IPN rails metal template by sending the correct params back: ipn.send_back(env['rack.request.form_vars']) Thanks to for fixing this.

0.0.5 Added Preapproval preapproval_paypal_payment_url along with test case.

0.0.4 Preapproval now returns a PaypalAdaptive::Response class. Added preapproval test cases.

0.0.3 Renamed PayRequest, PayResponse into Request, Response since other api calls use the class as well.

0.0.2 Fixed initialized constant warning.

0.0.1 First release.


Copyright (c) 2009 Tommy Chheng. See LICENSE for details.