Skip to content

jpablobr/active_paypal_adaptive_payment

Repository files navigation

Active PayPal Adaptive Payment

This library is meant to interface with PayPal's Adaptive Payment Gateway.

Active PayPal Adaptive Payment

Supported

  • Payments
  • Preapprovals
  • Refunds
  • Currency conversions
  • getting/setting payment options
  • getting shipping addresses
  • getting a redirect for the embedded pay flow
  • More soon!

Installation

Add the following line to your app Gemfile:

gem "active_paypal_adaptive_payment"

bundle install

Implementation

See iAuction: An Adaptive Payments Tutorial Featuring Parallel Payments tutorial for more info.

Init

gateway =  ActiveMerchant::Billing::PaypalAdaptivePayment.new(
  :login => "acutio_1313133342_biz_api1.gmail.com",
  :password => "1255043567",
  :signature => "Abg0gYcQlsdkls2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf",
  :appid => "APP-80W284485P519543T" )

Pre-approved payment

gateway.preapprove_payment (
  :return_url => "returnURL",
  :cancel_url => "cancelURL",
  :senderEmail =>"email address of sender",
  :start_date => Time.now,
  :end_date => Time.now + (60*60*24) * 30,
  :currency_code =>"currency code",
  :max_amount => "maxTotalAmountOfAllPayments",
  :maxNumberOfPayments => "maxNumberOfPayments" )

  response = gateway.setup_purchase(
    :return_url => url_for(:action => 'action', :only_path => false),
    :cancel_url => url_for(:action => 'action', :only_path => false),
    :notify_url => url_for(:action => 'notify_action', :only_path => false),
    :receiver_list => recipients
  )

# For redirecting the customer to the actual paypal site to finish the payment.
redirect_to (gateway.redirect_pre_approval_url_for(response["payKey"]))

Cancel pre-approved payment

 gateway.cancel_preapproval(:preapproval_key => "preapprovalkey")

Chained payments

def checkout
  recipients = [{:email => 'receiver_email',
                 :amount => some_amount,
                 :primary => true},
                {:email => 'receiver_email',
                 :amount => recipient_amount,
                 :primary => false}
                 ]
  response = gateway.setup_purchase(
    :return_url => url_for(:action => 'action', :only_path => false),
    :cancel_url => url_for(:action => 'action', :only_path => false),
    :ipn_notification_url => url_for(:action => 'notify_action', :only_path => false),
    :receiver_list => recipients
  )

  # For redirecting the customer to the actual paypal site to finish the payment.
  redirect_to (gateway.redirect_url_for(response["payKey"]))
end

Set the :primary flag to false for each recipient for a split payment.

Maybe also check the tests for a sample implementation.

Notes:

In development environment it's very important to define ActiveMerchant environment you will be using to test your app like so:

#config/environments/{environment.rb}
App::Application.configure do
  config.setting...
end
ActiveMerchant::Billing::Base.mode = :test

Setting advanced payment and invoice options

To be able to set additional payment and invoice options, such as item names, shipping costs, custom logos, etc., you must first create a payment, then set the options and then direct the user to the payment page:

purchase = gateway.setup_purchase(
  :action_type => "CREATE", # This is in contrast to the default PAY action
                           # as above
)

gateway.set_payment_options(
  :display_options => {
    :business_name    => "Your Business",
    :header_image_url => "http://cdn.yourbusiness.com/logo-for-paypal.png"
  },
  :pay_key => purchase["payKey"],
  :receiver_options => [
    {
      :description => "Your purchase of XYZ",
      :invoice_data => {
        :item => [
          { :name => "Item #1", :item_count => 1, :item_price => 100, :price => 100 },
          { :name => "Item #2", :item_count => 2, :item_price => 10, :price => 20 }
        ],
        :total_shipping => 5,
        :total_tax => 10
      },
      :receiver => { :email => "receiver1@example.com" }
    },
    {
      :description => "XYZ Processing fee",
      :invoice_data => {
        :item => [{ :name => "Fee", :item_count => 1, :item_price => 10, :price => 10 }]
      },
      :receiver => { :email => "receiver2@example.com" }
    }
  ]
)

redirect_to(gateway.redirect_url_for(purchase["payKey"]))

See the implementation of ActiveMerchant::Billing::PaypalAdaptivePayment#build_adaptive_set_payment_options_request for all available options and Operation SetPaymentOptions API for a description of them.

Testing

First modify the test/fixtures.yml to fit your app credentials (You will need at least a PayPal developer account).

After that you can run them like this:

$ ruby -Ilib test/test_paypal_adaptive_payment.rb

Debugging

Use either gateway.debug or response.debug this gives you the json response, the xml sent and the url it was posted to.

From the Rails console it can be accessed like such:

ActiveMerchant::Billing::PaypalAdaptivePayment

PaypalAdaptivePayment#debug or AdaptivePaymentResponse#debug return the raw xml request, raw json response and the URL of the endpoint.

TODO

  • More/better Documentation.
  • Improve/change the tests implementation by maybe run them off fixture data and use a separate script/test for actually invoking requests to PayPal. This will allow other developers to implement the without being required to set and request the credentials from PayPal process.

Contributors

Other previous contributors where some code was taken from.

Some PayPal Adaptive payment resources.

Note on Patches/Pull Requests:

  • Fork the project.
  • Make your feature addition or bug fix.
  • Send me a pull request. Bonus points for topic branches.

Copyright:

(The MIT License)

Copyright 2011 Jose Pablo Barrantes. MIT Licence, so go for it.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, an d/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.