ruby-paypal 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ruby-paypal/paypal.rb +292 -11
  2. metadata +14 -10
@@ -6,7 +6,7 @@ require 'ruby-paypal/credit_card_checks'
6
6
 
7
7
  SANDBOX_SERVER = 'api-3t.sandbox.paypal.com/nvp'
8
8
  PRODUCTION_SERVER = 'api-3t.paypal.com/nvp'
9
- API_VERSION = '3.2'
9
+ API_VERSION = '56.0'
10
10
 
11
11
  module Net
12
12
  #
@@ -59,13 +59,16 @@ end
59
59
 
60
60
 
61
61
  =begin rdoc
62
- Author:: Chang Sau Sheong (mailto:sausheong.chang@gmail.com)
63
- Copyright:: Copyright (c) 2007 Chang Sau Sheong
62
+ Author:: Chang Sau Sheong (mailto:sausheong.chang@gmail.com)
63
+ Author:: Philippe F. Monnet (mailto:pfmonnet@gmail.com)
64
+ Copyright:: Copyright (c) 2007-2009 Chang Sau Sheong & Philippe F. Monnet
64
65
  License:: Distributes under the same terms as Ruby
65
- Version:: 0.0.2
66
+ Version:: 0.0.5
67
+
68
+ :main: Paypal
66
69
 
67
70
  =Installing Ruby-PayPal
68
- A lightweight ruby wrapper for PayPal NVP APIs. To install type the following
71
+ A lightweight ruby wrapper for PayPal NVP (Name-Value Pair) APIs. To install type the following
69
72
  at the command line:
70
73
 
71
74
  $ gem install ruby-paypal
@@ -74,13 +77,18 @@ at the command line:
74
77
  =Using Ruby-PayPal
75
78
  It's critical that you understand how PayPal works and how the PayPal NVP API
76
79
  works. You should be relatively well-versed in the NVP API Developer Guide and
77
- Reference (https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/index.html).
80
+ Reference - see:
81
+ - https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/index.html
82
+ - https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_api_soap_NVPAPIOverview
83
+
78
84
  You should also visit and register yourself with the PayPal Developer Network
79
85
  and get a Sandbox account with in the PayPal Development Central
80
86
  (https://developer.paypal.com/).
81
87
 
82
88
  Note that this library only supports the API signature method of securing the API credentials.
83
89
 
90
+ By setting Paypal.debug=true, the API will "pretty-print" the PayPal parameters to the console.
91
+
84
92
  ==Direct Payment
85
93
  To use credit card payment through PayPal, you need to use the DoDirectPayment APIs:
86
94
 
@@ -114,14 +122,144 @@ To use the customer's PayPal account for payment, you will need to use the Expre
114
122
 
115
123
  <to be documented>
116
124
 
117
- =More information
118
- I will be continually providing updates in my blog at http://blog.saush.com
125
+ ==PayPal Subscriptions
126
+
127
+ PayPal Subscriptions is a service offering allowing you to sell subscriptions, consisting of an initial payment followed by
128
+ several recurring payments. For a good technical overview, review the following guide:
129
+ - https://www.paypal.com/en_US/ebook/PP_ExpressCheckout_IntegrationGuide/RecurringPayments.html
130
+
131
+ Using the subscriptions service involve understanding the series of exchanges to perform using the NVP API.
132
+ There are 3 key phases of a subscription:
133
+ 1. Creating a subscription request (and button for the customer)
134
+ 2. Customer review and confirmation on the PayPal site
135
+ 3. Processing of a subscription agreement
136
+ Each phase involves specific APIs.
137
+
138
+ ===Phase 1 - Subscription Request
139
+ In this phase, the do_set_express_checkout method will be called. PayPal will return a token we can use in subsequent API calls.
140
+
141
+ Let's create a subcription request with the details of our subscription:
142
+
143
+ subscription_request = create_monthly_subscription_request(
144
+ name='_Why's Ruby Camping Adventures',
145
+ id='MNWRCA',
146
+ description='_Why's Ruby Camping Adventures - Monthly Tips And Tricks For Camping Development',
147
+ invoice_number='INV20091122',
148
+ amount='5.00')
149
+
150
+ Let's call do_set_express_checkout to get a token back:
151
+
152
+ response = paypal.do_set_express_checkout(
153
+ return_url='http://www.yoursite.com/subscription-confirmed',
154
+ cancel_url='http://www.yoursite.com/subscription-aborted',
155
+ amount='5.00',
156
+ other_params=subscription_request)
157
+
158
+ token = (response.ack == 'Success') ? response['TOKEN'] : ''
159
+
160
+ Let's use the token to create a PayPal button to request payment via the sandbox:
161
+
162
+ form( { :method => 'post' ,
163
+ :action => 'https://www.sandbox.paypal.com/cgi-bin/webscr' #sandbox
164
+ } ) do
165
+
166
+ input :id => 'cmd', :name => 'cmd', :type => 'hidden',
167
+ :value => "_express-checkout";
168
+
169
+ input :id => 'token', :name => 'token', :type => 'hidden',
170
+ :value => "#{token}";
171
+
172
+ input :id => 'submit_subscription_request', :name => 'submit', :type => 'submit',
173
+ :value => 'Subscribe Via PayPal'
174
+ end #form
175
+
176
+ ===Phase 2 - Customer Review and Confirmation
177
+
178
+ The customer will see the details of the subscription agreement we created previously.
179
+ Upon confirmation, PayPal will redirect the customer to the return_url we specified passing
180
+ the token back as well as the payerid.
119
181
 
182
+ ===Phase 3 = Subscription Processing
183
+
184
+ First we will retrieve the details of the check-out:
185
+
186
+ response = paypal.do_get_express_checkout_details(token)
187
+
188
+ Then we will execute the actual payment:
189
+
190
+ response = paypal.do_express_checkout_payment(token=token,
191
+ payment_action='Sale',
192
+ payer_id=payerid,
193
+ amount='5.00')
194
+
195
+ transaction_id = response['TRANSACTIONID']
196
+
197
+ Now we can create the actual PayPal subscription
198
+
199
+ response = @paypal.do_create_recurring_payments_profile(token,
200
+ start_date='2009-11-22 14:30:10',
201
+ profile_reference='INV20091122',
202
+ description='_Why's Ruby Camping Adventures - Monthly Tips And Tricks For Camping Development',
203
+ billing_period='Month',
204
+ billing_frequency=1,
205
+ total_billing_cycles=11,
206
+ amount='5.00',
207
+ currency='USD')
208
+
209
+ profile_id = @response['PROFILEID']
210
+
211
+ The profile_id can then be used in the future to access the details of the subscription,
212
+ suspend it, reactivate it or cancel it using the following methods:
213
+ - do_get_recurring_payments_profile_details
214
+
215
+ response = paypal.do_get_recurring_payments_profile_details(profile_id)
216
+
217
+ - do_manage_recurring_payments_profile_status
218
+
219
+ # Suspend
220
+ response = paypal.do_manage_recurring_payments_profile_status(profile_id,
221
+ action='Suspend',
222
+ note='The subscription is being suspended due to payment cancellation by the customer')
223
+
224
+ # Re-Activate
225
+ response = paypal.do_manage_recurring_payments_profile_status(profile_id,
226
+ action='Reactivate',
227
+ note='The subscription is being reactivated due to new payment by the customer')
228
+
229
+ # Cancel
230
+ response = paypal.do_manage_recurring_payments_profile_status(profile_id,
231
+ action='Cancel',
232
+ note='The subscription is being cancelled due to cancellation of the account by the customer')
233
+
234
+ The customer information associated with the subscription can be retrieved using:
235
+ - do_get_billing_agreement_customer_details
236
+
237
+ response = paypal.do_get_billing_agreement_customer_details(token)
238
+
239
+ Note: all subscriptions methods also accept an optional other_params hash for any other NVP you need to pass.
240
+
241
+
242
+ =More information
243
+ Check for updates in our blogs:
244
+ - http://blog.saush.com
245
+ - http://blog.monnet-usa.com
120
246
  =end
121
247
 
122
248
  class Paypal
123
249
  include CreditCardChecks
124
250
 
251
+ @@debug = false
252
+
253
+ def self.debug
254
+ @@debug
255
+ end
256
+
257
+ # Controls whether or not PP debug statements will be produced and sent to the console
258
+ #
259
+ def self.debug=(val) #:doc:
260
+ @@debug = val
261
+ end
262
+
125
263
  # Create a new object with the given user name, password and signature. To enable production
126
264
  # access to PayPal change the url to the live PayPal server. Set url to <tt>:production</tt> to change
127
265
  # access to PayPal production servers.
@@ -193,6 +331,10 @@ class Paypal
193
331
  #
194
332
  # Equivalent of SetExpressCheckout.
195
333
  #
334
+ def do_set_express_checkout(return_url, cancel_url, amount, other_params={})
335
+ return set_express_checkout(return_url, cancel_url, amount, other_params)
336
+ end
337
+
196
338
  def set_express_checkout(return_url, cancel_url, amount, other_params={})
197
339
  params = {
198
340
  'METHOD' => 'SetExpressCheckout',
@@ -231,8 +373,6 @@ class Paypal
231
373
  }
232
374
 
233
375
  params.merge! other_params
234
- pp "DO EXPRESS CHECKOUT PAYMENT"
235
- pp params
236
376
  make_nvp_call(params)
237
377
  end
238
378
 
@@ -294,7 +434,8 @@ class Paypal
294
434
  # Makes the call to the PayPal NVP API. This is the workhorse method for the other method calls.
295
435
  #
296
436
  def make_nvp_call(params)
297
- pp params
437
+ pp params if @@debug
438
+
298
439
  @api_parameters.merge! params
299
440
  parameters = URI.escape(@api_parameters.to_a.collect {|pair| pair.join('=')}.join('&'))
300
441
  response = Net::HTTPS.post_form(URI.parse("https://#{@paypal_url}"), @api_parameters)
@@ -341,6 +482,146 @@ class Paypal
341
482
  make_nvp_call(params)
342
483
  end
343
484
 
485
+ # techarch> Subscription APIs
486
+
487
+ # Creates a payment subscription based on a start date, billing period, frequency, number of periods and amount
488
+ #
489
+ # Equivalent to CreateRecurringPaymentsProfile
490
+ #
491
+ def do_create_recurring_payments_profile(token, start_date, profile_reference, description, billing_period, billing_frequency, total_billing_cycles, amount, currency, other_params={})
492
+ params = {
493
+ 'METHOD' => 'CreateRecurringPaymentsProfile',
494
+ 'TOKEN' => token,
495
+ 'PROFILESTARTDATE' => start_date,
496
+ 'PROFILEREFERENCE' => profile_reference,
497
+ 'DESC' => description,
498
+ 'BILLINGPERIOD' => billing_period,
499
+ 'BILLINGFREQUENCY' => billing_frequency,
500
+ 'TOTALBILLINGCYCLES' => total_billing_cycles,
501
+ 'AMT' => amount,
502
+ 'CURRENCYCODE' => currency
503
+ }
504
+ params.merge! other_params
505
+
506
+ make_nvp_call(params)
507
+ end
508
+
509
+ # Retrieves the details of a payment subscription for a given profile id
510
+ # Will return for e.g. the start date, billing period, frequency, number of periods and amount
511
+ #
512
+ # Equivalent to GetRecurringPaymentsProfileDetails
513
+ #
514
+ def do_get_recurring_payments_profile_details (profile_id, other_params={})
515
+ params = {
516
+ 'METHOD' => 'GetRecurringPaymentsProfileDetails',
517
+ 'PROFILEID' => profile_id }
518
+ params.merge! other_params
519
+
520
+ make_nvp_call(params)
521
+ end
522
+
523
+ # Manages a recurring subscription profile in terms of status:
524
+ # - Cancel
525
+ # - Suspend
526
+ # - Reactivate
527
+ # Equivalent to ManageRecurringPaymentsProfileStatus
528
+ #
529
+ def do_manage_recurring_payments_profile_status(profile_id, action, note='', other_params={})
530
+ params = {
531
+ 'METHOD' => 'ManageRecurringPaymentsProfileStatus',
532
+ 'PROFILEID' => profile_id,
533
+ 'ACTION' => action,
534
+ 'NOTE' => note
535
+ }
536
+ params.merge! other_params
537
+
538
+ make_nvp_call(params)
539
+ end
540
+
541
+ # Retrieves the customer details for the billing agreement associated with the current token
542
+ # Equivalent to GetBillingAgreementCustomerDetails
543
+ #
544
+ def do_get_billing_agreement_customer_details(token, other_params={})
545
+ params = {
546
+ 'METHOD' => 'GetBillingAgreementCustomerDetails',
547
+ 'TOKEN' => token
548
+ }
549
+ params.merge! other_params
550
+
551
+ make_nvp_call(params)
552
+ end
553
+
554
+ # Initiates the creation of a billing agreement
555
+ # Equivalent to SetCustomerBillingAgreement
556
+ #
557
+ def do_set_billing_agreement_customer_details(return_url, cancel_url, billing_desc, billing_type='RecurringPayments', payment_type='', custom='', other_params={})
558
+ params = {
559
+ 'METHOD' => 'SetCustomerBillingAgreement',
560
+ 'RETURNURL' => return_url,
561
+ 'CANCELURL' => cancel_url,
562
+ 'L_BILLINGAGREEMENTDESCRIPTION0' => billing_desc,
563
+ 'L_BILLINGTYPE0' => billing_type,
564
+ 'L_PAYMENTTYPE0' => payment_type,
565
+ 'L_BILLINGAGREEMENTCUSTOM0' => custom
566
+ }
567
+ params.merge! other_params
568
+
569
+ make_nvp_call(params)
570
+ end
571
+
572
+ # Retrieves the details of a transaction for a given transaction id
573
+ #
574
+ # Equivalent to GetTransactionDetails
575
+ #
576
+ def do_get_transaction_details (transaction_id, other_params={})
577
+ params = {
578
+ 'METHOD' => 'GetTransactionDetails',
579
+ 'TRANSACTIONID' => transaction_id }
580
+ params.merge! other_params
581
+
582
+ make_nvp_call(params)
583
+ end
584
+
585
+ # Retrieves the details of a express checkout for a given token
586
+ #
587
+ # Equivalent to GetExpressCheckoutDetails
588
+ #
589
+ def do_get_express_checkout_details (token, other_params={})
590
+ params = {
591
+ 'METHOD' => 'GetExpressCheckoutDetails',
592
+ 'TOKEN' => token }
593
+ params.merge! other_params
594
+
595
+ make_nvp_call(params)
596
+ end
597
+
598
+ # Search transactions between payee and payer
599
+ # Equivalent to TransactionSearch
600
+ #
601
+ def do_transaction_search(start_date,payee_email, payer_email='', payer_first='', payer_middle='', payer_last='',
602
+ transaction_class='Subscription', other_params={})
603
+ params = {
604
+ 'METHOD' => 'TransactionSearch',
605
+ 'STARTDATE' => start_date,
606
+ 'RECEIVER' => payee_email,
607
+ 'TRANSACTIONCLASS' => transaction_class
608
+ }
609
+
610
+ if !payer_email.nil? && !payer_email.empty?
611
+ params['EMAIL'] = payer_email
612
+ else
613
+ params['FIRSTNAME'] = payer_first !payer_first.nil? && !payer_first.empty?
614
+ params['MIDDLENAME'] = payer_middle !payer_middle.nil? && !payer_middle.empty?
615
+ params['LASTNAME'] = payer_last !payer_last.nil? && !payer_last.empty?
616
+ end
617
+
618
+ params.merge! other_params
619
+
620
+ make_nvp_call(params)
621
+ end
622
+
623
+ # --------------------------------------------------------------------------------------------------------------------------------------
624
+
344
625
  private
345
626
 
346
627
  #
metadata CHANGED
@@ -1,20 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-paypal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chang Sau Sheong
8
+ - Philippe F. Monnet
8
9
  autorequire: ruby-paypal
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-07-19 00:00:00 +08:00
13
+ date: 2009-11-27 00:00:00 -07:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
16
- description:
17
- email: sausheong.chang@gmail.com
17
+ description: " ruby-paypal is a lightweight wrapper for the PayPal Name-Value Pair API providing:\n - direct payments\n - subscription and recurring payments\n"
18
+ email:
19
+ - sausheong.chang@gmail.com
20
+ - pfmonnet@gmail.com
18
21
  executables: []
19
22
 
20
23
  extensions: []
@@ -22,14 +25,15 @@ extensions: []
22
25
  extra_rdoc_files:
23
26
  - README
24
27
  files:
25
- - lib/ruby-paypal
26
28
  - lib/ruby-paypal/credit_card_checks.rb
27
29
  - lib/ruby-paypal/paypal.rb
28
30
  - lib/ruby-paypal.rb
29
31
  - test/ts_ruby-paypal.rb
30
32
  - README
31
33
  has_rdoc: true
32
- homepage: http://blog.saush.com
34
+ homepage: http://rubyforge.org/projects/ruby-paypal/
35
+ licenses: []
36
+
33
37
  post_install_message:
34
38
  rdoc_options: []
35
39
 
@@ -49,10 +53,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
53
  version:
50
54
  requirements: []
51
55
 
52
- rubyforge_project:
53
- rubygems_version: 1.0.1
56
+ rubyforge_project: ruby-paypal
57
+ rubygems_version: 1.3.5
54
58
  signing_key:
55
- specification_version: 2
56
- summary: A lightweight Ruby wrapper for PayPal NVP API
59
+ specification_version: 3
60
+ summary: A lightweight Ruby wrapper for the PayPal NVP API providing payment and subscriptions capabilities
57
61
  test_files:
58
62
  - test/ts_ruby-paypal.rb