Skip to content

Commit

Permalink
Extracted mapping method to be used on IPN class.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jul 5, 2011
1 parent 79e6f3b commit 300b7d7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
1 change: 1 addition & 0 deletions lib/paypal/recurring.rb
Expand Up @@ -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 => {
Expand Down
52 changes: 18 additions & 34 deletions 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
Expand All @@ -13,10 +29,6 @@ def params=(params)
end
end

def type
params[:txn_type]
end

def express_checkout?
type == "express_checkout"
end
Expand All @@ -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?
Expand Down
19 changes: 2 additions & 17 deletions lib/paypal/recurring/response/base.rb
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions spec/paypal/notification_spec.rb
Expand Up @@ -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"

Expand Down

0 comments on commit 300b7d7

Please sign in to comment.