module PayPal

Public Class Methods

registered(app) click to toggle source
# File lib/sinatra/paypal.rb, line 38
def self.registered(app)
        app.helpers PayPal::Helpers
        app._paypal_register_default_callbacks

        app.set :paypal, OpenStruct.new({
                :return_url => '/payment/complete',
                :notify_url => '/payment/validate',
                :sandbox? => app.settings.development?,
                :email => nil
        })

        app.post '/payment/validate' do
                paypal_helper = PaypalHelper.new(app.settings.paypal.sandbox?)
                paypal_request = PaypalRequest.new(params)
                
                # first we check the request against paypal to make sure it is ok
                if settings.production?
                        halt 500, 'request could not be validated' if !paypal_helper.ipn_valid? params
                end
                
                # check transaction log to make sure this not a replay attack
                if _call_paypal_method(:repeated?, paypal_request)
                        # we also want to return 200, because if it is paypal sending this, it will send
                        # it again and again until it gets a 200 back
                        halt 200, 'already processed'
                end

                _call_paypal_method(:validate!, paypal_request)
                
                # check that the payment is complete. we still return 200 if not, but
                # we don't need to do anymore processing (except for marking it as accountable, if it is)
                if paypal_request.complete?
                        _call_paypal_method(:complete, paypal_request)
                end

                _call_paypal_method(:finish, paypal_request)

                return 200
        end
end

Public Instance Methods

_paypal_method_name(key) click to toggle source
# File lib/sinatra/paypal.rb, line 107
def _paypal_method_name(key)
        "payment_event_#{key}".to_sym
end
_paypal_register_callback(key, &block) click to toggle source
# File lib/sinatra/paypal.rb, line 99
def _paypal_register_callback(key, &block)
        self.send :define_method, _paypal_method_name(key), &block
end
_paypal_register_default_callbacks() click to toggle source
# File lib/sinatra/paypal.rb, line 93
def _paypal_register_default_callbacks
        _paypal_valid_blocks.each do |key|
                _paypal_register_callback(key) { |p| }
        end
end
_paypal_valid_blocks() click to toggle source
# File lib/sinatra/paypal.rb, line 103
def _paypal_valid_blocks
        [:complete, :finish, :validate!, :repeated?]
end
payment(name, &block) click to toggle source

Register a payment callback. All callbacks are called with a single argument of the type PaypalRequest containing all the data for the notification.

payment :complete do |p|
        # process the payment here
        # don't forget to check that the price is correct!
end
# File lib/sinatra/paypal.rb, line 88
def payment(name, &block)
        raise "#{name.to_s} is not a valid payment callback" if !_paypal_valid_blocks.include? name
        _paypal_register_callback name, &block
end