diff --git a/lib/paypal/recurring.rb b/lib/paypal/recurring.rb index 430da07..6c3fc91 100644 --- a/lib/paypal/recurring.rb +++ b/lib/paypal/recurring.rb @@ -11,6 +11,7 @@ module Recurring autoload :Request, "paypal/recurring/request" autoload :Response, "paypal/recurring/response" autoload :Version, "paypal/recurring/version" + autoload :Utils, "paypal/recurring/utils" ENDPOINTS = { :sandbox => { diff --git a/lib/paypal/recurring/notification.rb b/lib/paypal/recurring/notification.rb index 97735c9..9f3014e 100644 --- a/lib/paypal/recurring/notification.rb +++ b/lib/paypal/recurring/notification.rb @@ -1,8 +1,24 @@ module PayPal module Recurring class Notification + extend PayPal::Recurring::Utils + attr_reader :params + mapping({ + :type => :txn_type, + :transaction_id => :txn_id, + :fee => [:mc_fee, :payment_fee], + :reference => [:rp_invoice_id, :custom, :invoice], + :payment_id => :recurring_payment_id, + :amount => [:amount, :mc_gross, :payment_gross], + :currency => :mc_currency, + :status => :payment_status, + :payment_date => [:time_created, :payment_date], + :seller_id => :receiver_id, + :email => :receiver_email + }) + def initialize(params = {}) self.params = params end @@ -13,10 +29,6 @@ def params=(params) end end - def type - params[:txn_type] - end - def express_checkout? type == "express_checkout" end @@ -36,47 +48,19 @@ def response end def valid? - completed? && verified? && params[:receiver_email] == PayPal::Recurring.email && params[:receiver_id] == PayPal::Recurring.seller_id + completed? && verified? && email == PayPal::Recurring.email && seller_id == PayPal::Recurring.seller_id end def completed? status == "Completed" end - def transaction_id - params[:txn_id] - end - - def fee - params[:mc_currency] - end - - def reference - params[:rp_invoice_id] - end - - def payment_id - params[:recurring_payment_id] - end - def next_payment_date Time.parse(params[:next_payment_date]) if params[:next_payment_date] end def paid_at - Time.parse params[:time_created] if params[:time_created] - end - - def amount - params[:amount] - end - - def currency - params[:mc_currency] - end - - def status - params[:payment_status] + Time.parse(payment_date) if payment_date end def verified? diff --git a/lib/paypal/recurring/response/base.rb b/lib/paypal/recurring/response/base.rb index 14a98a2..007c401 100644 --- a/lib/paypal/recurring/response/base.rb +++ b/lib/paypal/recurring/response/base.rb @@ -2,24 +2,9 @@ module PayPal module Recurring module Response class Base - attr_accessor :response + extend PayPal::Recurring::Utils - def self.mapping(options = {}) - options.each do |to, from| - class_eval <<-RUBY - def #{to} - @#{to} ||= begin - from = [#{from.inspect}].flatten - name = from.find {|name| params[name]} - value = nil - value = params[name] if name - value = send("build_#{to}", value) if respond_to?("build_#{to}", true) - value - end - end - RUBY - end - end + attr_accessor :response mapping( :token => :TOKEN, diff --git a/lib/paypal/recurring/utils.rb b/lib/paypal/recurring/utils.rb new file mode 100644 index 0000000..d11e5ce --- /dev/null +++ b/lib/paypal/recurring/utils.rb @@ -0,0 +1,22 @@ +module PayPal + module Recurring + module Utils + def mapping(options = {}) + options.each do |to, from| + class_eval <<-RUBY + def #{to} + @#{to} ||= begin + from = [#{from.inspect}].flatten + name = from.find {|name| params[name]} + value = nil + value = params[name] if name + value = send("build_#{to}", value) if respond_to?("build_#{to}", true) + value + end + end + RUBY + end + end + end + end +end diff --git a/spec/paypal/notification_spec.rb b/spec/paypal/notification_spec.rb index f5c930a..1302f5e 100644 --- a/spec/paypal/notification_spec.rb +++ b/spec/paypal/notification_spec.rb @@ -11,6 +11,23 @@ subject.should be_recurring_payment end + describe "#reference" do + it "returns from recurring IPN" do + subject.params[:rp_invoice_id] = "abc" + subject.reference.should == "abc" + end + + it "returns from custom field" do + subject.params[:custom] = "abc" + subject.reference.should == "abc" + end + + it "returns from invoice field" do + subject.params[:invoice] = "abc" + subject.reference.should == "abc" + end + end + context "when successful" do use_vcr_cassette "notification/success"