Module: PayPal
- Defined in:
- lib/sinatra/paypal.rb
Defined Under Namespace
Modules: Helpers
Class Method Summary collapse
Instance Method Summary collapse
- #_paypal_method_name(key) ⇒ Object
- #_paypal_register_callback(key, &block) ⇒ Object
- #_paypal_register_default_callbacks ⇒ Object
- #_paypal_valid_blocks ⇒ Object
-
#payment(name, &block) ⇒ Object
Register a payment callback.
Class Method Details
.registered(app) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 |
Instance Method Details
#_paypal_method_name(key) ⇒ Object
107 108 109 |
# File 'lib/sinatra/paypal.rb', line 107 def _paypal_method_name(key) "payment_event_#{key}".to_sym end |
#_paypal_register_callback(key, &block) ⇒ Object
99 100 101 |
# 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 ⇒ Object
93 94 95 96 97 |
# 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 ⇒ Object
103 104 105 |
# File 'lib/sinatra/paypal.rb', line 103 def _paypal_valid_blocks [:complete, :finish, :validate!, :repeated?] end |
#payment(name, &block) ⇒ Object
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
88 89 90 91 |
# 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 |