Module: Paypal::Helpers

Defined in:
lib/helper.rb

Overview

This is a collection of helpers which aid in the creation of paypal buttons

Example:

<%= form_tag Paypal::Notification.ipn_url %>

  <%= paypal_setup "Item 500", Money.us_dollar(50000), "[email protected]" %>  
  Please press here to pay $500US using paypal. <%= submit_tag %>

<% end_form_tag %>

For this to work you have to include these methods as helpers in your rails application. One way is to add “include Paypal::Helpers” in your application_helper.rb See Paypal::Notification for information on how to catch payment events.

Instance Method Summary collapse

Instance Method Details

#paypal_form_tag(url = Paypal::Notification.ipn_url) ⇒ Object

Convenience helper. Can replace <%= form_tag Paypal::Notification.ipn_url %> takes optional url parameter, default is Paypal::Notification.ipn_url



20
21
22
# File 'lib/helper.rb', line 20

def paypal_form_tag(url = Paypal::Notification.ipn_url)
  form_tag(url)
end

#paypal_setup(item_number, amount, business, options = {}) ⇒ Object

This helper creates the hidden form data which is needed for a paypal purchase.

  • item_number – The first parameter is the item number. This is for your personal organization and can be arbitrary. Paypal will sent the item number back with the IPN so its a great place to store a user ID or a order ID or something like this.

  • amount – should be a parameter of type Money ( see leetsoft.com/api/money ) but can also be a string of type “50.00” for 50$. If you use the string syntax make sure you set the current currency as part of the options hash. The default is USD

  • business – This is your paypal account name ( a email ). This needs to be a valid paypal business account.

The last parameter is a options hash. You can override several things:

  • :notify_url – default is nil. Supply an url which paypal will send its IPN notification to once a purchase is made, canceled or any other status changes occure.

  • :return_url – default is nil. If provided paypal will redirect a user back to this url after a successful purchase. Useful for a kind of thankyou page.

  • :cancel_url – default is nil. If provided paypal will redirect a user back to this url when the user cancels the purchase.

  • :item_name – default is ‘Store purchase’. This is the name of the purchase which will be displayed on the paypal page.

  • :no_shipping – default is ‘1’. By default we tell paypal that no shipping is required. Usually the shipping address should be collected in our application, not by paypal.

  • :no_note – default is ‘1’

  • :currency – default is ‘USD’

  • :tax – the tax for the store purchase. Same format as the amount parameter but optional

  • :invoice – Unique invoice number. User will never see this. optional

  • :custom – Custom field. User will never see this. optional

  • :no_utf8 – if set to false this prevents the charset = utf-8 hidden field. (I don’t know why you would want to disable this… )

Examples:

<%= paypal_setup @order.id, Money.us_dollar(50000), "[email protected]" %>  
<%= paypal_setup @order.id, '50.00', "[email protected]", :currency => 'USD' %>  
<%= paypal_setup @order.id, '50.00', "[email protected]", :currency => 'USD', :notify_url => url_for(:only_path => false, :action => 'paypal_ipn') %>  
<%= paypal_setup @order.id, Money.ca_dollar(50000), "[email protected]", :item_name => 'Snowdevil shop purchase', :return_url => paypal_return_url, :cancel_url => paypal_cancel_url, :notify_url => paypal_ipn_url  %>


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/helper.rb', line 63

def paypal_setup(item_number, amount, business, options = {})

  params = {
    :item_name => 'Store purchase',
    :no_shipping => '1',
    :no_note => '1',
    :currency => 'USD',
    :return_url => nil
  }.merge(options)

  # We accept both, strings and money objects as amount    
  amount = amount.cents.to_f / 100.0 if amount.respond_to?(:cents)
  amount = sprintf("%.2f", amount)
  
  # same for tax
  tax = params[:tax]
  if tax
    tax = tax.cents.to_f / 100.0 if tax.respond_to?(:cents)
    tax = sprintf("%.2f", tax)
  end
  
  # Build the form 
  returning button = [] do
    button << tag(:input, :type => 'hidden', :name => 'cmd', :value => "_xclick")
    button << tag(:input, :type => 'hidden', :name => 'quantity', :value => 1)
    button << tag(:input, :type => 'hidden', :name => 'business', :value => business)
    button << tag(:input, :type => 'hidden', :name => 'amount', :value => amount)
    button << tag(:input, :type => 'hidden', :name => 'item_number', :value => item_number)
    button << tag(:input, :type => 'hidden', :name => 'item_name', :value => params[:item_name])
    button << tag(:input, :type => 'hidden', :name => 'no_shipping', :value => params[:no_shipping])
    button << tag(:input, :type => 'hidden', :name => 'no_note', :value => params[:no_note])
    button << tag(:input, :type => 'hidden', :name => 'return', :value => params[:return_url]) if params[:return_url]
    button << tag(:input, :type => 'hidden', :name => 'notify_url', :value => params[:notify_url]) if params[:notify_url]
    button << tag(:input, :type => 'hidden', :name => 'cancel_return', :value => params[:cancel_url]) if params[:cancel_url]
    button << tag(:input, :type => 'hidden', :name => 'tax', :value => tax) if tax
    button << tag(:input, :type => 'hidden', :name => 'invoice', :value => params[:invoice]) if params[:invoice]
    button << tag(:input, :type => 'hidden', :name => 'custom', :value => params[:custom]) if params[:custom]

    # if amount was a object of type money or something compatible we will use its currency, 
    # otherwise get the currency from the options. default is USD
    button << tag(:input, :type => 'hidden', :name => 'currency_code', :value => amount.respond_to?(:currency) ? amount.currency : params[:currency])
    button << tag(:input, :type => 'hidden', :name => 'charset', :value => 'utf-8') unless params[:no_utf8]
  end.join("\n")
end