Skip to content

Commit

Permalink
Added support for initial amount billing; You can skip the request_pa…
Browse files Browse the repository at this point in the history
…yment by providing :initial_amount and :initial_amount_action options while calling PayPal::Recurring::Base#create_recurring_profile method.
  • Loading branch information
fnando committed Jul 1, 2011
1 parent 2c9284c commit 1ecba15
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 52 deletions.
32 changes: 18 additions & 14 deletions lib/paypal/recurring/base.rb
Expand Up @@ -5,8 +5,11 @@ class Base
attr_accessor :cancel_url
attr_accessor :currency
attr_accessor :description
attr_accessor :email
attr_accessor :failed
attr_accessor :frequency
attr_accessor :initial_amount
attr_accessor :initial_amount_action
attr_accessor :ipn_url
attr_accessor :outstanding
attr_accessor :payer_id
Expand All @@ -16,7 +19,6 @@ class Base
attr_accessor :return_url
attr_accessor :start_at
attr_accessor :token
attr_accessor :email

def initialize(options = {})
options.each {|name, value| send("#{name}=", value)}
Expand Down Expand Up @@ -104,24 +106,26 @@ def request_payment
# Create a recurring billing profile.
#
# ppr = PayPal::Recurring.new({
# :amount => "9.00",
# :currency => "USD",
# :description => "Awesome - Monthly Subscription",
# :ipn_url => "http://example.com/paypal/ipn",
# :frequency => 1,
# :token => "EC-05C46042TU8306821",
# :period => :monthly,
# :reference => "1234",
# :payer_id => "WTTS5KC2T46YU",
# :start_at => Time.now,
# :failed => 1,
# :outstanding => :next_billing
# :amount => "9.00",
# :initial_amount => "9.00",
# :initial_amount_action => :cancel,
# :currency => "USD",
# :description => "Awesome - Monthly Subscription",
# :ipn_url => "http://example.com/paypal/ipn",
# :frequency => 1,
# :token => "EC-05C46042TU8306821",
# :period => :monthly,
# :reference => "1234",
# :payer_id => "WTTS5KC2T46YU",
# :start_at => Time.now,
# :failed => 1,
# :outstanding => :next_billing
# })
#
# response = ppr.create_recurring_profile
#
def create_recurring_profile
params = collect(:amount, :currency, :description, :payer_id, :token, :reference, :start_at, :failed, :outstanding, :ipn_url, :frequency, :period, :email)
params = collect(:amount, :initial_amount, :initial_amount_action, :currency, :description, :payer_id, :token, :reference, :start_at, :failed, :outstanding, :ipn_url, :frequency, :period, :email)
request.run(:create_profile, params)
end

Expand Down
61 changes: 36 additions & 25 deletions lib/paypal/recurring/request.rb
Expand Up @@ -10,6 +10,11 @@ class Request
:manage_profile => "ManageRecurringPaymentsProfileStatus"
}

INITIAL_AMOUNT_ACTIONS = {
:cancel => "CancelOnFailure",
:continue => "ContinueOnFailure"
}

ACTIONS = {
:cancel => "Cancel",
:suspend => "Suspend",
Expand All @@ -28,31 +33,33 @@ class Request
}

ATTRIBUTES = {
:action => "ACTION",
:amount => ["PAYMENTREQUEST_0_AMT", "AMT"],
:billing_type => "L_BILLINGTYPE0",
:cancel_url => "CANCELURL",
:currency => ["PAYMENTREQUEST_0_CURRENCYCODE", "CURRENCYCODE"],
:description => ["DESC", "PAYMENTREQUEST_0_DESC", "L_BILLINGAGREEMENTDESCRIPTION0"],
:email => "EMAIL",
:failed => "MAXFAILEDPAYMENTS",
:frequency => "BILLINGFREQUENCY",
:ipn_url => ["PAYMENTREQUEST_0_NOTIFYURL", "NOTIFYURL"],
:method => "METHOD",
:no_shipping => "NOSHIPPING",
:outstanding => "AUTOBILLOUTAMT",
:password => "PWD",
:payer_id => "PAYERID",
:payment_action => "PAYMENTREQUEST_0_PAYMENTACTION",
:period => "BILLINGPERIOD",
:profile_id => "PROFILEID",
:reference => "PROFILEREFERENCE",
:return_url => "RETURNURL",
:signature => "SIGNATURE",
:start_at => "PROFILESTARTDATE",
:token => "TOKEN",
:username => "USER",
:version => "VERSION",
:action => "ACTION",
:amount => ["PAYMENTREQUEST_0_AMT", "AMT"],
:billing_type => "L_BILLINGTYPE0",
:cancel_url => "CANCELURL",
:currency => ["PAYMENTREQUEST_0_CURRENCYCODE", "CURRENCYCODE"],
:description => ["DESC", "PAYMENTREQUEST_0_DESC", "L_BILLINGAGREEMENTDESCRIPTION0"],
:email => "EMAIL",
:failed => "MAXFAILEDPAYMENTS",
:frequency => "BILLINGFREQUENCY",
:initial_amount => "INITAMT",
:initial_amount_action => "FAILEDINITAMTACTION",
:ipn_url => ["PAYMENTREQUEST_0_NOTIFYURL", "NOTIFYURL"],
:method => "METHOD",
:no_shipping => "NOSHIPPING",
:outstanding => "AUTOBILLOUTAMT",
:password => "PWD",
:payer_id => "PAYERID",
:payment_action => "PAYMENTREQUEST_0_PAYMENTACTION",
:period => "BILLINGPERIOD",
:profile_id => "PROFILEID",
:reference => "PROFILEREFERENCE",
:return_url => "RETURNURL",
:signature => "SIGNATURE",
:start_at => "PROFILESTARTDATE",
:token => "TOKEN",
:username => "USER",
:version => "VERSION",
}

CA_FILE = File.dirname(__FILE__) + "/cacert.pem"
Expand Down Expand Up @@ -152,6 +159,10 @@ def build_outstanding(value) # :nodoc:
def build_action(value) # :nodoc:
ACTIONS.fetch(value.to_sym, value) if value
end

def build_initial_amount_action(value) # :nodoc:
INITIAL_AMOUNT_ACTIONS.fetch(value.to_sym, value) if value
end
end
end
end
3 changes: 2 additions & 1 deletion lib/paypal/recurring/response/profile.rb
Expand Up @@ -20,7 +20,8 @@ class Profile < Base
:period => :BILLINGPERIOD,
:frequency => :BILLINGFREQUENCY,
:currency => :CURRENCYCODE,
:amount => :AMT
:amount => :AMT,
:initial_amount => :AGGREGATEOPTIONALAMT
)

OUTSTANDING = {
Expand Down
9 changes: 9 additions & 0 deletions spec/paypal/request_spec.rb
Expand Up @@ -98,5 +98,14 @@
subject.normalize_params(:action => :suspend).should == {:ACTION => "Suspend"}
subject.normalize_params(:action => :reactivate).should == {:ACTION => "Reactivate"}
end

it "normalizes initial amount" do
subject.normalize_params(:initial_amount => "9.00").should == {:INITAMT => "9.00"}
end

it "normalizes initial amount action" do
subject.normalize_params(:initial_amount_action => :cancel).should == {:FAILEDINITAMTACTION => "CancelOnFailure"}
subject.normalize_params(:initial_amount_action => :continue).should == {:FAILEDINITAMTACTION => "ContinueOnFailure"}
end
end
end
26 changes: 14 additions & 12 deletions spec/paypal/response/create_recurring_profile_spec.rb
Expand Up @@ -6,18 +6,20 @@

subject {
ppr = PayPal::Recurring.new({
:amount => "9.00",
:currency => "USD",
:description => "Awesome - Monthly Subscription",
:ipn_url => "http://example.com/paypal/ipn",
:frequency => 1,
:token => "EC-2UK36172XH723314S",
:period => :monthly,
:reference => "1234",
:payer_id => "WTTS5KC2T46YU",
:start_at => Time.now,
:failed => 1,
:outstanding => :next_billing
:amount => "9.00",
:initial_amount => "9.00",
:initial_amount_action => :cancel,
:currency => "USD",
:description => "Awesome - Monthly Subscription",
:ipn_url => "http://example.com/paypal/ipn",
:frequency => 1,
:token => "EC-2UK36172XH723314S",
:period => :monthly,
:reference => "1234",
:payer_id => "WTTS5KC2T46YU",
:start_at => Time.now,
:failed => 1,
:outstanding => :next_billing
})
ppr.create_recurring_profile
}
Expand Down
1 change: 1 addition & 0 deletions spec/paypal/response/profile_spec.rb
Expand Up @@ -27,6 +27,7 @@
its(:frequency) { should == "1" }
its(:currency) { should == "USD" }
its(:amount) { should == "9.00" }
its(:initial_amount) { should == "0.00" }
end

context "when failure" do
Expand Down

0 comments on commit 1ecba15

Please sign in to comment.