johnreitano-activemerchant 1.5.5 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,36 @@ module ActiveMerchant #:nodoc:
25
25
  @express ||= PaypalExpressGateway.new(@options)
26
26
  end
27
27
 
28
+ RECURRING_ACTIONS = Set.new([:add, :cancel, :inquiry, :suspend])
29
+
30
+ # For recurring options see https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_api_WPRecurringPayments
31
+
32
+ def recurring(money, credit_card, options = {})
33
+ options[:name] = credit_card.name if options[:name].blank? && credit_card
34
+ request = build_recurring_request(options[:profile_id] ? :modify : :add, money, options) do |xml|
35
+ add_credit_card(xml, credit_card, options[:billing_address], options) if credit_card
36
+ end
37
+ commit('CreateRecurringPaymentsProfile', request)
38
+ end
39
+
40
+ # cancels an existing recurring profile
41
+ def cancel_recurring(profile_id)
42
+ request = build_recurring_request(:cancel, 0, :profile_id => profile_id) {}
43
+ commit('ManageRecurringPaymentsProfileStatus', request)
44
+ end
45
+
46
+ # retrieves information about a recurring profile
47
+ def recurring_inquiry(profile_id, options = {})
48
+ request = build_recurring_request(:inquiry, nil, options.update( :profile_id => profile_id ))
49
+ commit('GetRecurringPaymentsProfileDetails', request)
50
+ end
51
+
52
+ # suspends a recurring profile
53
+ def suspend_recurring(profile_id)
54
+ request = build_recurring_request(:suspend, 0, :profile_id => profile_id) {}
55
+ commit('ManageRecurringPaymentsProfileStatus', request)
56
+ end
57
+
28
58
  private
29
59
 
30
60
  def define_transaction_type(transaction_arg)
@@ -116,6 +146,87 @@ module ActiveMerchant #:nodoc:
116
146
  def build_response(success, message, response, options = {})
117
147
  Response.new(success, message, response, options)
118
148
  end
149
+
150
+ def build_recurring_request(action, money, options)
151
+ unless RECURRING_ACTIONS.include?(action)
152
+ raise StandardError, "Invalid Recurring Profile Action: #{action}"
153
+ end
154
+
155
+ xml = Builder::XmlMarkup.new :indent => 2
156
+ ns2 = 'n2:'
157
+
158
+ if [:add].include?(action)
159
+ xml.tag! 'CreateRecurringPaymentsProfileReq', 'xmlns' => PAYPAL_NAMESPACE do
160
+ xml.tag! 'CreateRecurringPaymentsProfileRequest' do
161
+ xml.tag! 'Version', API_VERSION, 'xmlns' => EBAY_NAMESPACE
162
+ # NOTE: namespace prefix here is critical!
163
+ xml.tag! ns2 + 'CreateRecurringPaymentsProfileRequestDetails ', 'xmlns:n2' => EBAY_NAMESPACE do
164
+
165
+ # credit card and other information goes here
166
+ yield xml
167
+
168
+ xml.tag! ns2 + 'RecurringPaymentsProfileDetails' do
169
+ xml.tag! ns2 + 'BillingStartDate', options[:starting_at]
170
+ end
171
+
172
+ xml.tag! ns2 + 'ScheduleDetails' do
173
+ xml.tag! ns2 + 'Description', options[:comment]
174
+
175
+ unless options[:initial_payment].nil?
176
+ xml.tag! ns2 + 'ActivationDetails' do
177
+ xml.tag! ns2 + 'InitialAmount', amount(options[:initial_payment]), 'currencyID' => options[:currency] || currency(options[:initial_payment])
178
+ xml.tag! ns2 + 'FailedInitAmountAction', 'CancelOnFailure'
179
+ end
180
+ end
181
+
182
+ frequency, period = get_pay_period(options)
183
+ xml.tag! ns2 + 'PaymentPeriod' do
184
+ xml.tag! ns2 + 'BillingPeriod', period
185
+ xml.tag! ns2 + 'BillingFrequency', frequency.to_s
186
+ xml.tag! ns2 + 'TotalBillingCycles', options[:payments] unless options[:payments].nil? || options[:payments] == 0
187
+ xml.tag! ns2 + 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
188
+ end
189
+
190
+ xml.tag! ns2 + 'AutoBillOutstandingAmount', 'AddToNextBilling'
191
+ end
192
+ end
193
+ end
194
+ end
195
+ elsif [:cancel, :suspend].include?(action)
196
+ xml.tag! 'ManageRecurringPaymentsProfileStatusReq', 'xmlns' => PAYPAL_NAMESPACE do
197
+ xml.tag! 'ManageRecurringPaymentsProfileStatusRequest', 'xmlns:n2' => EBAY_NAMESPACE do
198
+ xml.tag! ns2 + 'Version', API_VERSION
199
+ xml.tag! ns2 + 'ManageRecurringPaymentsProfileStatusRequestDetails' do
200
+ xml.tag! 'ProfileID', options[:profile_id]
201
+ xml.tag! ns2 + 'Action', action == :cancel ? 'Cancel' : 'Suspend'
202
+ xml.tag! ns2 + 'Note', 'Canceling the action, no real comment here'
203
+ end
204
+ end
205
+ end
206
+ elsif [:inquiry].include?(action)
207
+ xml.tag! 'GetRecurringPaymentsProfileDetailsReq', 'xmlns' => PAYPAL_NAMESPACE do
208
+ xml.tag! 'GetRecurringPaymentsProfileDetailsRequest', 'xmlns:n2' => EBAY_NAMESPACE do
209
+ xml.tag! ns2 + 'Version', API_VERSION
210
+ xml.tag! 'ProfileID', options[:profile_id]
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ def get_pay_period(options)
217
+ requires!(options, [:periodicity, :bimonthly, :monthly, :biweekly, :weekly, :yearly, :daily, :semimonthly, :quadweekly, :quarterly, :semiyearly])
218
+ case options[:periodicity]
219
+ when :weekly then [1, 'Week']
220
+ when :biweekly then [2, 'Week']
221
+ when :semimonthly then [1, 'SemiMonth']
222
+ when :quadweekly then [4, 'Week']
223
+ when :monthly then [1, 'Month']
224
+ when :quarterly then [3, 'Month']
225
+ when :semiyearly then [6, 'Month'] # broken! i think
226
+ when :yearly then [1, 'Year']
227
+ end
228
+ end
229
+
119
230
  end
120
231
  end
121
232
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: johnreitano-activemerchant
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 5
9
- - 5
10
- version: 1.5.5
9
+ - 7
10
+ version: 1.5.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Reitano
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain:
17
17
  - gem-public_cert.pem
18
- date: 2010-06-28 00:00:00 -07:00
18
+ date: 2010-07-14 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency