wepay-rails 2.5.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ gem 'httparty'
8
8
  # Include everything needed to run rake, tests, features, etc.
9
9
  group :development do
10
10
  gem "rails", "3.1.0"
11
+ gem "rake"
11
12
  gem 'turn', '0.8.2', :require => false
12
13
  gem 'thor'
13
14
  gem "bundler"
@@ -110,6 +110,7 @@ DEPENDENCIES
110
110
  httparty
111
111
  jeweler (~> 1.6.4)
112
112
  rails (= 3.1.0)
113
+ rake
113
114
  rcov
114
115
  thor
115
116
  turn (= 0.8.2)
@@ -6,6 +6,7 @@ Wepay-Rails allows your rails app to accept payments with Wepay (http://www.wepa
6
6
 
7
7
  = Features
8
8
 
9
+ * Added PREAPPROVAL and DELAYED CHARGE ability 09/2012
9
10
  * Built in IPN that listens to push notifications from wepay and updates saved checkout records for you
10
11
  * Allows you to fetch your access token easily through a web interface
11
12
  * Built in API tools to allow you to make all wepay-api calls easily
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.5.0
1
+ 2.5.1
Binary file
@@ -0,0 +1,43 @@
1
+ class Wepay::ChargeController < Wepay::ApplicationController
2
+ include WepayRails::Payments
3
+
4
+ def index
5
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
6
+
7
+ if record.present?
8
+ wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
9
+ charge = wepay_gateway.lookup_preapproval(record.preapproval_id)
10
+
11
+ # remove unnecessary attributes
12
+ charge.delete_if {|k,v| !record.attributes.include? k.to_s}
13
+
14
+ record.update_attributes(charge)
15
+ redirect_to "#{wepay_gateway.configuration[:after_charge_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
16
+ else
17
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
18
+ end
19
+ end
20
+
21
+ def success
22
+ response = WepayCheckoutRecord.find(:last)
23
+ wepay_gateway = WepayRails::Payments::Gateway.new( response.access_token )
24
+ charge = wepay_gateway.lookup_checkout(response.checkout_id)
25
+
26
+ response.update_attributes(charge)
27
+ logger.info params
28
+ render :text => "Wepay charge OK from #{response.payer_email} with Checkout ID # #{response.checkout_id} from the Pre-approval ID # #{response.preapproval_id}."
29
+ end
30
+
31
+ def new
32
+ # The following is from the Wepay API sample checkout call
33
+ # create the checkout
34
+ charge_params = {
35
+ :amount => '50.00',
36
+ :short_description => 'A Checkout on the Wepay PreApproval.',
37
+ :type => 'GOODS',
38
+ :preapproval_id => xxxxx # To test, be sure to manually add the preapproval_id from the preapproval response which will skip having to go to Wepay to add CC info.
39
+ }
40
+ # Finally, send user to charge on the preapproval.
41
+ init_charge_and_return_success(charge_params)
42
+ end
43
+ end
@@ -1,18 +1,32 @@
1
1
  class Wepay::CheckoutController < Wepay::ApplicationController
2
+ include WepayRails::Payments
3
+
2
4
  def index
3
- record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
5
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
4
6
 
5
- if record.present?
6
- wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
7
- checkout = wepay_gateway.lookup_checkout(record.checkout_id)
8
-
9
- # remove unnecessary attributes
10
- checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
11
-
12
- record.update_attributes(checkout)
13
- redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
14
- else
15
- raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
16
- end
7
+ if record.present?
8
+ wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
9
+ checkout = wepay_gateway.lookup_checkout(record.checkout_id)
10
+
11
+ # remove unnecessary attributes
12
+ checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
13
+
14
+ record.update_attributes(checkout)
15
+ redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
16
+ else
17
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
18
+ end
19
+ end
20
+
21
+ def new
22
+ # create the checkout - This is TEST info from Wepay only
23
+ checkout_params = {
24
+ :amount => '255.00',
25
+ :short_description => 'A transaction for WePay Testing',
26
+ :type => 'GOODS'
27
+ }
28
+ # Finally, send the user off to wepay so you can get paid! - CASH MONEY
29
+ init_checkout_and_send_user_to_wepay(checkout_params)
17
30
  end
31
+
18
32
  end
@@ -1,20 +1,22 @@
1
1
  class Wepay::IpnController < Wepay::ApplicationController
2
- def create
2
+ include WepayRails::Payments
3
+ def update
4
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
3
5
 
4
- record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
6
+ if record.present?
7
+ wepay_gateway = WepayRails::Payments::Gateway.new
5
8
 
6
- if record.present?
7
- wepay_gateway = WepayRails::Payments::Gateway.new
8
- checkout = wepay_gateway.lookup_checkout(record.checkout_id)
9
-
10
- # remove unnecessary attributes
11
- checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
12
-
13
- record.update_attributes(checkout)
14
- render :text => "ok"
15
- else
16
- raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
17
- end
9
+ if record.checkout_id.present?
10
+ checkout = wepay_gateway.lookup_checkout(record.checkout_id)
11
+ else
12
+ checkout = wepay_gateway.lookup_preapproval(record.preapproval_id)
13
+ end
14
+ checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
15
+ record.update_attributes(checkout)
16
+ render :text => "ok"
17
+ else
18
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
19
+ end
18
20
 
19
- end
21
+ end
20
22
  end
@@ -0,0 +1,47 @@
1
+ class Wepay::PreapprovalController < Wepay::ApplicationController
2
+ include WepayRails::Payments
3
+
4
+ def index
5
+ record = WepayCheckoutRecord.find_by_preapproval_id_and_security_token(params[:preapproval_id],params[:security_token])
6
+
7
+
8
+ if record.present?
9
+ wepay_gateway = WepayRails::Payments::Gateway.new ( record.access_token )
10
+ preapproval = wepay_gateway.lookup_preapproval(record.preapproval_id)
11
+
12
+ #remove unneccesary attributes
13
+ preapproval.delete_if {|k,v| !record.attributes.include? k.to_s}
14
+
15
+ record.update_attributes(preapproval)
16
+ redirect_to "#{wepay_gateway.configuration[:after_preapproval_redirect_uri]}?preapproval_id=#{params[:preapproval_id]}"
17
+ else
18
+ raise StandardError.new("Wepay IPN: No record found for preapproval_id #{params[:preapproval_id]} and security_token #{params[:security_token]}")
19
+ end
20
+ end
21
+
22
+ def success
23
+ response = WepayCheckoutRecord.find(:last)
24
+ wepay_gateway = WepayRails::Payments::Gateway.new( response.access_token )
25
+ charge = wepay_gateway.lookup_preapproval(response.preapproval_id)
26
+
27
+ response.update_attributes(charge)
28
+ logger.info params
29
+ render :text => "PRE-APPROVAL OK from #{response.payer_email} with Pre-approval ID # #{response.preapproval_id}. You can use this Pre-approval Id# to run a charge at a later time."
30
+ end
31
+
32
+ def new
33
+ # create the preapproval - This is TEST info
34
+ preapproval_params = {
35
+ :period => 'once',
36
+ :end_time => '2013-12-25',
37
+ :amount => '50.00',
38
+ :mode => 'regular',
39
+ :short_description => 'A Preapproval for MyApp.',
40
+ :app_fee => "10",
41
+ :fee_payer => 'payee',
42
+ :payer_email_message => "You just approved MyApp to charge you for a some money later. You have NOT been charged at this time!"
43
+ }
44
+ # Finally, send the user off to wepay for the preapproval
45
+ init_preapproval_and_send_user_to_wepay(preapproval_params)
46
+ end
47
+ end
@@ -0,0 +1,113 @@
1
+ require 'digest/sha2'
2
+ module WepayRails
3
+ module Api
4
+ module ChargeMethods
5
+ # Many of the settings you pass in here are already factored in from
6
+ # the wepay.yml file and only need to be overridden if you insist on doing
7
+ # so when this method is called. The following list of key values are pulled
8
+ # in for you from your wepay.yml file:
9
+ #
10
+ # Note: @wepay_config is your wepay.yml as a Hash
11
+ # :callback_uri => @wepay_config[:ipn_callback_uri],
12
+ # :redirect_uri => @wepay_config[:checkout_redirect_uri],
13
+ # :fee_payer => @wepay_config[:fee_payer],
14
+ # :type => @wepay_config[:checkout_type],
15
+ # :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
16
+ # :app_fee => @wepay_config[:app_fee],
17
+ # :auto_capture => @wepay_config[:auto_capture] ? 1 : 0,
18
+ # :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
19
+ # :shipping_fee => @wepay_config[:shipping_fee],
20
+ # :charge_tax => @wepay_config[:charge_tax],
21
+ # :account_id => wepay_user['account_id'] # wepay-rails goes and gets this for you, but you can override it if you want to.
22
+ #
23
+ #
24
+ # params hash key values possibilities are:
25
+ # Parameter: Required: Description:
26
+ # :account_id Yes The unique ID of the account you want to create a checkout for.
27
+ # :short_description Yes A short description of what is being paid for.
28
+ # :long_description No A long description of what is being paid for.
29
+ # :type Yes The the checkout type (one of the following: GOODS, SERVICE, DONATION, or PERSONAL)
30
+ # :reference_id No The unique reference id of the checkout (set by the application in /checkout/create
31
+ # :amount Yes The amount that the payer will pay.
32
+ # :app_fee No The amount that the application will receive in fees.
33
+ # :fee_payer No Who will pay the fees (WePay's fees and any app fees). Set to "Payer" to charge fees to the person paying (Payer will pay amount + fees, payee will receive amount). Set to "Payee" to charge fees to the person receiving money (Payer will pay amount, Payee will receive amount - fees). Defaults to "Payer".
34
+ # :redirect_uri No The uri the payer will be redirected to after paying.
35
+ # :callback_uri No The uri that will receive any Instant Payment Notifications sent. Needs to be a full uri (ex https://www.wepay.com )
36
+ # :auto_capture No A boolean value (0 or 1). Default is 1. If set to 0 then the payment will not automatically be released to the account and will be held by WePay in payment state 'reserved'. To release funds to the account you must call /checkout/capture
37
+ # :require_shipping No A boolean value (0 or 1). If set to 1 then the payer will be asked to enter a shipping address when they pay. After payment you can retrieve this shipping address by calling /checkout
38
+ # :shipping_fee No The amount that you want to charge for shipping.
39
+ # :charge_tax No A boolean value (0 or 1). If set to 1 and the account has a relevant tax entry (see /account/set_tax), then tax will be charged.
40
+ def perform_charge(params)
41
+ security_token = Digest::SHA2.hexdigest("#{Rails.env.production? ? rand(4) : 1}#{Time.now.to_i}") # Make less random during tests
42
+
43
+ # add the security token to any urls that were passed in from the app
44
+ if params[:callback_uri]
45
+ params[:callback_uri] = apply_security_token( params[:callback_uri], security_token )
46
+ end
47
+
48
+ if params[:redirect_uri]
49
+ params[:redirect_uri] = apply_security_token( params[:redirect_uri], security_token )
50
+ end
51
+
52
+ defaults = {
53
+ :callback_uri => ipn_callback_uri(security_token),
54
+ :redirect_uri => checkout_redirect_uri(security_token),
55
+ :fee_payer => @wepay_config[:fee_payer],
56
+ :type => @wepay_config[:checkout_type],
57
+ :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
58
+ :app_fee => @wepay_config[:app_fee],
59
+ :auto_capture => @wepay_config[:auto_capture] ? 1 : 0,
60
+ :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
61
+ :shipping_fee => @wepay_config[:shipping_fee],
62
+ :account_id => @wepay_config[:account_id]
63
+ }.merge(params)
64
+
65
+ resp = self.call_api("/checkout/create", defaults)
66
+ resp.merge({:security_token => security_token})
67
+ end
68
+
69
+ def lookup_checkout(checkout_id)
70
+ co = self.call_api("/checkout", {:checkout_id => checkout_id})
71
+ co.delete(:type)
72
+ co
73
+ end
74
+
75
+ def lookup_preapproval(preapproval_id)
76
+ co = self.call_api("/preapproval", {:preapproval_id => preapproval_id})
77
+ co.delete("type")
78
+ co
79
+ end
80
+
81
+ def ipn_callback_uri(security_token)
82
+ uri = if @wepay_config[:ipn_callback_uri].present?
83
+ @wepay_config[:ipn_callback_uri]
84
+ else
85
+ "#{@wepay_config[:root_callback_uri]}/wepay/ipn"
86
+ end
87
+ apply_security_token(uri, security_token)
88
+ end
89
+
90
+ def charge_redirect_uri(security_token)
91
+ uri = if @wepay_config[:ipn_callback_uri].present?
92
+ @wepay_config[:charge_redirect_uri]
93
+ else
94
+ "#{@wepay_config[:root_callback_uri]}/wepay/charge"
95
+ end
96
+ apply_security_token(uri, security_token)
97
+ end
98
+
99
+ def checkout_redirect_uri(security_token)
100
+ uri = if @wepay_config[:ipn_callback_uri].present?
101
+ @wepay_config[:checkout_redirect_uri]
102
+ else
103
+ "#{@wepay_config[:root_callback_uri]}/wepay/checkout"
104
+ end
105
+ apply_security_token(uri, security_token)
106
+ end
107
+
108
+ def apply_security_token(uri, security_token)
109
+ uri += (uri =~ /\?/ ? '&' : '?') + "security_token=#{security_token}"
110
+ end
111
+ end
112
+ end
113
+ end
@@ -71,6 +71,12 @@ module WepayRails
71
71
  co.delete(:type)
72
72
  co
73
73
  end
74
+
75
+ def lookup_preapproval(preapproval_id)
76
+ co = self.call_api("/preapproval", {:preapproval_id => preapproval_id})
77
+ co.delete("type")
78
+ co
79
+ end
74
80
 
75
81
  def ipn_callback_uri(security_token)
76
82
  uri = if @wepay_config[:ipn_callback_uri].present?
@@ -0,0 +1,97 @@
1
+ require 'digest/sha2'
2
+ module WepayRails
3
+ module Api
4
+ module PreapprovalMethods
5
+ # Many of the settings you pass in here are already factored in from
6
+ # the wepay.yml file and only need to be overridden if you insist on doing
7
+ # so when this method is called. The following list of key values are pulled
8
+ # in for you from your wepay.yml file:
9
+ #
10
+ # Note: @wepay_config is your wepay.yml as a Hash
11
+ # :callback_uri => @wepay_config[:ipn_callback_uri],
12
+ # :redirect_uri => @wepay_config[:checkout_redirect_uri],
13
+ # :fee_payer => @wepay_config[:fee_payer],
14
+ # :type => @wepay_config[:checkout_type],
15
+ # :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
16
+ # :app_fee => @wepay_config[:app_fee],
17
+ # :auto_capture => @wepay_config[:auto_capture] ? 1 : 0,
18
+ # :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
19
+ # :shipping_fee => @wepay_config[:shipping_fee],
20
+ # :charge_tax => @wepay_config[:charge_tax],
21
+ # :account_id => wepay_user['account_id'] # wepay-rails goes and gets this for you, but you can override it if you want to.
22
+ #
23
+ #
24
+ # params hash key values possibilities are:
25
+ # Parameter: Required: Description:
26
+ # :account_id Yes The unique ID of the account you want to create a checkout for.
27
+ # :short_description Yes A short description of what is being paid for.
28
+ # :long_description No A long description of what is being paid for.
29
+ # :type Yes The the checkout type (one of the following: GOODS, SERVICE, DONATION, or PERSONAL)
30
+ # :reference_id No The unique reference id of the checkout (set by the application in /checkout/create
31
+ # :amount Yes The amount that the payer will pay.
32
+ # :app_fee No The amount that the application will receive in fees.
33
+ # :fee_payer No Who will pay the fees (WePay's fees and any app fees). Set to "Payer" to charge fees to the person paying (Payer will pay amount + fees, payee will receive amount). Set to "Payee" to charge fees to the person receiving money (Payer will pay amount, Payee will receive amount - fees). Defaults to "Payer".
34
+ # :redirect_uri No The uri the payer will be redirected to after paying.
35
+ # :callback_uri No The uri that will receive any Instant Payment Notifications sent. Needs to be a full uri (ex https://www.wepay.com )
36
+ # :auto_capture No A boolean value (0 or 1). Default is 1. If set to 0 then the payment will not automatically be released to the account and will be held by WePay in payment state 'reserved'. To release funds to the account you must call /checkout/capture
37
+ # :require_shipping No A boolean value (0 or 1). If set to 1 then the payer will be asked to enter a shipping address when they pay. After payment you can retrieve this shipping address by calling /checkout
38
+ # :shipping_fee No The amount that you want to charge for shipping.
39
+ # :charge_tax No A boolean value (0 or 1). If set to 1 and the account has a relevant tax entry (see /account/set_tax), then tax will be charged.
40
+ def perform_preapproval(params)
41
+ security_token = Digest::SHA2.hexdigest("#{Rails.env.production? ? rand(4) : 1}#{Time.now.to_i}") # Make less random during tests
42
+
43
+ # add the security token to any urls that were passed in from the app
44
+ if params[:callback_uri]
45
+ params[:callback_uri] = apply_security_token( params[:callback_uri], security_token )
46
+ end
47
+
48
+ if params[:redirect_uri]
49
+ params[:redirect_uri] = apply_security_token( params[:redirect_uri], security_token )
50
+ end
51
+
52
+ defaults = {
53
+ :callback_uri => ipn_callback_uri(security_token),
54
+ :redirect_uri => preapproval_redirect_uri(security_token),
55
+ :fee_payer => @wepay_config[:fee_payer],
56
+ :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
57
+ :app_fee => @wepay_config[:app_fee],
58
+ :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
59
+ :shipping_fee => @wepay_config[:shipping_fee],
60
+ :account_id => @wepay_config[:account_id]
61
+ }.merge(params)
62
+
63
+ resp = self.call_api("/preapproval/create", defaults)
64
+ resp.merge({:security_token => security_token})
65
+ end
66
+
67
+ def lookup_preapproval(preapproval_id)
68
+ co = self.call_api("/preapproval", {:preapproval_id => preapproval_id})
69
+ co.delete("type")
70
+ co
71
+ end
72
+
73
+ def ipn_callback_uri(security_token)
74
+ uri = if @wepay_config[:ipn_callback_uri].present?
75
+ @wepay_config[:ipn_callback_uri]
76
+ else
77
+ "#{@wepay_config[:root_callback_uri]}/wepay/ipn"
78
+ end
79
+ apply_security_token(uri, security_token)
80
+ end
81
+
82
+
83
+ def preapproval_redirect_uri(security_token)
84
+ uri = if @wepay_config[:ipn_callback_uri].present?
85
+ @wepay_config[:preapproval_redirect_uri]
86
+ else
87
+ "#{@wepay_config[:root_callback_uri]}/preapproval/success" #redirect to success action with text - ALT: "#{@wepay_config[:root_callback_uri]}/wepay/preapproval"
88
+ end
89
+ apply_security_token(uri, security_token)
90
+ end
91
+
92
+ def apply_security_token(uri, security_token)
93
+ uri += (uri =~ /\?/ ? '&' : '?') + "security_token=#{security_token}"
94
+ end
95
+ end
96
+ end
97
+ end
@@ -3,6 +3,7 @@ class CreateWepayCheckoutRecords < ActiveRecord::Migration
3
3
 
4
4
  create_table :wepay_checkout_records do |t|
5
5
  t.integer :checkout_id
6
+ t.integer :preapproval_id
6
7
  t.integer :account_id
7
8
  t.string :state
8
9
  t.string :short_description
@@ -17,6 +18,7 @@ class CreateWepayCheckoutRecords < ActiveRecord::Migration
17
18
  t.text :redirect_uri
18
19
  t.text :callback_uri
19
20
  t.text :checkout_uri
21
+ t.text :preapproval_uri
20
22
  t.string :payer_email
21
23
  t.string :payer_name
22
24
  t.text :cancel_reason
@@ -28,11 +30,19 @@ class CreateWepayCheckoutRecords < ActiveRecord::Migration
28
30
  t.string :security_token
29
31
  t.string :access_token
30
32
  t.string :mode
33
+ t.string :period
34
+ t.integer :frequency
35
+ t.datetime :start_time
36
+ t.datetime :end_time
37
+ t.string :manage_uri
38
+ t.datetime :create_time
39
+ t.boolean :auto_recur
31
40
 
32
41
  t.timestamps
33
42
  end
34
43
 
35
44
  add_index :wepay_checkout_records, :checkout_id
45
+ add_index :wepay_checkout_records, :preapproval_id
36
46
  end
37
47
 
38
48
  def self.down
@@ -5,7 +5,9 @@ production:
5
5
  access_token: <your access token that you received when you went to http://your.domain.com/wepay/authorize>
6
6
  root_callback_uri: "http://www.example.com"
7
7
  after_checkout_redirect_uri: "http://www.example.com/purchase/finalize"
8
- scope: [refund_payments,collect_payments,view_balance,view_user]
8
+ after_preapproval_redirect_uri: "http://www.example.com/preapproval/success"
9
+ after_charge_redirect_uri: "http://www.example.com/charge/success"
10
+ scope: [manage_accounts,refund_payments,collect_payments,view_balance,view_user,preapprove_payments]
9
11
  wepay_ui_endpoint: "https://www.wepay.com/v2"
10
12
  wepay_api_endpoint: "https://wepayapi.com/v2"
11
13
  fee_payer: Payee
@@ -23,7 +25,9 @@ development:
23
25
  access_token: <your access token that you received when you went to http://your.domain.com/wepay/authorize>
24
26
  root_callback_uri: "http://www.example.com"
25
27
  after_checkout_redirect_uri: "http://www.example.com/purchase/finalize"
26
- scope: [refund_payments,collect_payments,view_balance,view_user]
28
+ after_preapproval_redirect_uri: "http://www.example.com/preapproval/success"
29
+ after_charge_redirect_uri: "http://www.example.com/charge/success"
30
+ scope: [manage_accounts,refund_payments,collect_payments,view_balance,view_user,preapprove_payments]
27
31
  wepay_ui_endpoint: "https://stage.wepay.com/v2"
28
32
  wepay_api_endpoint: "https://stage.wepayapi.com/v2"
29
33
  fee_payer: Payee
@@ -41,7 +45,9 @@ test:
41
45
  access_token: <your access token that you received when you went to http://your.domain.com/wepay/authorize>
42
46
  root_callback_uri: "http://www.example.com"
43
47
  after_checkout_redirect_uri: "http://www.example.com/purchase/finalize"
44
- scope: [refund_payments,collect_payments,view_balance,view_user]
48
+ after_preapproval_redirect_uri: "http://www.example.com/preapproval/success"
49
+ after_charge_redirect_uri: "http://www.example.com/charge/success"
50
+ scope: [manage_accounts,refund_payments,collect_payments,view_balance,view_user,preapprove_payments]
45
51
  wepay_ui_endpoint: "https://stage.wepay.com/v2"
46
52
  wepay_api_endpoint: "https://stage.wepayapi.com/v2"
47
53
  fee_payer: Payee
@@ -1,2 +1,36 @@
1
1
  class WepayCheckoutRecord < ActiveRecord::Base
2
+ belongs_to :checkout
3
+ belongs_to :preapproval
4
+ belongs_to :ipn
5
+ belongs_to :authorize
6
+ attr_accessible :amount,
7
+ :short_description,
8
+ :access_token,
9
+ :checkout_id,
10
+ :security_token,
11
+ :checkout_uri,
12
+ :account_id,
13
+ :currency,
14
+ :fee_payer,
15
+ :state,
16
+ :redirect_uri,
17
+ :auto_capture,
18
+ :app_fee,
19
+ :gross,
20
+ :fee,
21
+ :callback_uri,
22
+ :tax,
23
+ :payer_email,
24
+ :payer_name,
25
+ :mode,
26
+ :preapproval_id,
27
+ :preapproval_uri,
28
+ :reference_id,
29
+ :period,
30
+ :frequency,
31
+ :start_time,
32
+ :end_time,
33
+ :auto_recur,
34
+ :create_time,
35
+ :manage_uri
2
36
  end
@@ -59,9 +59,80 @@ module WepayRails
59
59
 
60
60
  def init_checkout_and_send_user_to_wepay(params, access_token=nil)
61
61
  record = init_checkout(params, access_token)
62
- redirect_to record.checkout_uri and return
62
+ redirect_to record.checkout_uri and return record
63
63
  end
64
+
65
+ #Parameter Required Format Description
66
+ #account_id Yes Number The WePay account where the money will go when you use this pre-approval to execute a payment.
67
+ #amount No Number The amount for the pre-approval. The API application can charge up to this amount every period.
68
+ #short_description Yes String A short description of what the payer is paying for.
69
+ #period Yes String Can be: hourly, daily, weekly, biweekly, monthly, bimonthly, quarterly, yearly, or once. The API application can charge the payer every period.
70
+ #reference_id No String The reference id of the pre-approval. Can be any string, but must be unique for the application/user pair.
71
+ #app_fee No Number The application fee that will go to the API application's account.
72
+ #fee_payer No String Who will pay the WePay fees and app fees (if set). Can be payee or payer. Defaults to payer.
73
+ #redirect_uri No String The uri the payer will be redirected to after approving the pre-approval.
74
+ #callback_uri No String The uri that any instant payment notifications will be sent to. Needs to be a full uri (ex https://www.wepay.com ) and must NOT be localhost or 127.0.0.1 or include wepay.com. Max 2083 chars.
75
+ #require_shipping No Boolean Defaults to false. If set to true then the payer will be require to enter their shipping address when they approve the pre-approval.
76
+ #shipping_fee No Number The dollar amount of shipping fees that will be charged.
77
+ #charge_tax No Boolean Defaults to false. If set to true then any applicable taxes will be charged.
78
+ #payer_email_message No String A short message that will be included in the payment confirmation email to the payer.
79
+ #payee_email_message No String A short message that will be included in the payment confirmation email to the payee.
80
+ #long_description No String An optional longer description of what the payer is paying for.
81
+ #frequency No Number How often per period the API application can charge the payer.
82
+ #start_time No Number or String When the API application can start charging the payer. Can be a unix_timestamp or a parse-able date-time.
83
+ #end_time No Number or String The last time the API application can charge the payer. Can be a unix_timestamp or a parse-able date-time. The default value is five (5) years from the preapproval creation time.
84
+ #auto_recur No Boolean Set to true if you want the payments to automatically execute every period. Useful for subscription use cases. Default value is false. Only the following periods are allowed if you set auto_recur to true: Weekly, Biweekly, Monthly, Quarterly, Yearly
85
+ #mode No String What mode the pre-approval confirmation flow will be displayed in. The options are 'iframe' or 'regular'. Choose 'iframe' if this is an iframe pre-approval. Mode defaults to 'regular'.
86
+ #prefill_info No Object A JSON object that lets you pre fill certain fields in the pre-approval flow. Allowed fields are 'name', 'email', 'phone_number', 'address', 'city', 'state', 'zip', Pass the prefill-info as a JSON object like so: {"name":"Bill Clerico","phone_number":"855-469-3729"}
87
+ #funding_sources No String What funding sources you want to accept for this checkout. Options are: "bank,cc" to accept both bank and cc payments, "cc" to accept just credit card payments, and "bank" to accept just bank payments.
88
+
89
+
90
+ def init_preapproval(params, access_token=nil)
91
+ wepay_gateway = WepayRails::Payments::Gateway.new(access_token)
92
+ response = wepay_gateway.perform_preapproval(params)
93
+
94
+ if response[:preapproval_uri].blank?
95
+ raise WepayRails::Exceptions::WepayPreapprovalError.new("An error occurred: #{response.inspect}")
96
+ end
97
+
98
+ params.merge!({
99
+ :access_token => wepay_gateway.access_token,
100
+ :preapproval_id => response[:preapproval_id],
101
+ :security_token => response[:security_token],
102
+ :preapproval_uri => response[:preapproval_uri]
103
+ })
104
+
105
+ params.delete_if {|k,v| !WepayCheckoutRecord.attribute_names.include? k.to_s}
64
106
 
107
+ WepayCheckoutRecord.create(params)
108
+ end
109
+
110
+ def init_preapproval_and_send_user_to_wepay(params, access_token=nil)
111
+ record = init_preapproval(params, access_token)
112
+ redirect_to record.preapproval_uri and return record
113
+ end
114
+
115
+ def init_charge(params, access_token=nil)
116
+ wepay_gateway = WepayRails::Payments::Gateway.new(access_token)
117
+ response = wepay_gateway.perform_charge(params)
118
+
119
+ params.merge!({
120
+ :access_token => wepay_gateway.access_token,
121
+ :preapproval_id => response[:preapproval_id],
122
+ :checkout_id => response[:checkout_id],
123
+ :security_token => response[:security_token],
124
+ })
125
+
126
+ params.delete_if {|k,v| !WepayCheckoutRecord.attribute_names.include? k.to_s}
127
+
128
+ WepayCheckoutRecord.create(params)
129
+ end
130
+
131
+ def init_charge_and_return_success(params, access_token=nil)
132
+ record = init_charge(params, access_token)
133
+ redirect_to charge_success_url and return record
134
+ end
135
+
65
136
 
66
137
  end
67
138
  end
@@ -2,6 +2,8 @@ require 'active_record'
2
2
  require 'helpers/controller_helpers'
3
3
  require 'api/account_methods'
4
4
  require 'api/checkout_methods'
5
+ require 'api/preapproval_methods'
6
+ require 'api/charge_methods'
5
7
  require 'httparty'
6
8
 
7
9
  module WepayRails
@@ -37,7 +39,13 @@ module WepayRails
37
39
  resources :ipn
38
40
  resources :authorize
39
41
  resources :checkout
42
+ resources :finalize
43
+ resources :preapproval
44
+ resources :charge
40
45
  end
46
+ match '/preapproval/success' => 'wepay/preapproval#success'
47
+ match '/preapproval/charge' => 'wepay/charge#new'
48
+ match '/charge/success' => 'wepay/charge#success'
41
49
  end
42
50
  end
43
51
  end
@@ -49,6 +57,8 @@ module WepayRails
49
57
  class AuthorizationError < StandardError; end
50
58
  class WepayCheckoutError < StandardError; end
51
59
  class WepayApiError < StandardError; end
60
+ class WepayPreapprovalError < StandardError; end
61
+ class WepayChargeError < StandardError; end
52
62
  end
53
63
 
54
64
  module Payments
@@ -149,6 +159,8 @@ module WepayRails
149
159
 
150
160
  include WepayRails::Api::AccountMethods
151
161
  include WepayRails::Api::CheckoutMethods
162
+ include WepayRails::Api::PreapprovalMethods
163
+ include WepayRails::Api::ChargeMethods
152
164
  end
153
165
 
154
166
  include WepayRails::Exceptions
@@ -99,4 +99,9 @@ class ActiveSupport::TestCase
99
99
  { "checkout_id" => "6789",
100
100
  "checkout_uri" => "http://stage.wepay.com/api/checkout/6789" }.merge(options).to_json
101
101
  end
102
+
103
+ def sample_preapproval_response(options={})
104
+ { "preapproval_id" => "6789",
105
+ "preapproval_uri" => "http://stage.wepay.com/api/preapproval/6789" }.merge(options).to_json
106
+ end
102
107
  end
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
3
  class TestWepayRailsCheckoutMethods < ActiveSupport::TestCase
4
- include WepayRails::Helpers::ControllerHelpers
4
+ include WepayRails::Helpers::ControllerHelpers
5
5
 
6
6
  def setup
7
7
  create_wepay_config_file(false, true)
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestWepayRailsPreapprovalMethods < ActiveSupport::TestCase
4
+ include WepayRails::Helpers::ControllerHelpers
5
+
6
+ def setup
7
+ create_wepay_config_file(false, true)
8
+ initialize_wepay_config
9
+ @checkout_params = {
10
+ :amount => 300,
11
+ :period => "once",
12
+ :short_description => "This is a checkout test!",
13
+ :account_id => "12345"
14
+ }
15
+ end
16
+
17
+ def teardown
18
+ delete_wepay_config_file
19
+ end
20
+
21
+ test "should create a new WePay preapproval object" do
22
+ stub_request(:post, "https://stage.wepayapi.com/v2/preapproval/create").
23
+ with(:headers => wepay_gateway.wepay_auth_header).
24
+ to_return(:status => 200, :body => sample_preapproval_response, :headers => {})
25
+
26
+ @response = wepay_gateway.perform_preapproval(@checkout_params)
27
+ assert_equal "6789", @response[:preapproval_id]
28
+ assert_equal "http://stage.wepay.com/api/preapproval/6789", @response[:preapproval_uri]
29
+ assert_not_nil @response[:security_token]
30
+ end
31
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "wepay-rails"
8
- s.version = "2.5.0"
8
+ s.version = "2.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Medeiros"]
12
- s.date = "2012-09-16"
12
+ s.date = "2013-04-11"
13
13
  s.description = "Rails gem that interfaces with the WePay API"
14
14
  s.email = "adammede@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  "README.rdoc"
19
19
  ]
20
20
  s.files = [
21
+ ".DS_Store",
21
22
  ".document",
22
23
  ".travis.yml",
23
24
  "Gemfile",
@@ -27,13 +28,18 @@ Gem::Specification.new do |s|
27
28
  "README.rdoc",
28
29
  "Rakefile",
29
30
  "VERSION",
31
+ "app/.DS_Store",
30
32
  "app/controllers/wepay/application_controller.rb",
31
33
  "app/controllers/wepay/authorize_controller.rb",
34
+ "app/controllers/wepay/charge_controller.rb",
32
35
  "app/controllers/wepay/checkout_controller.rb",
33
36
  "app/controllers/wepay/ipn_controller.rb",
37
+ "app/controllers/wepay/preapproval_controller.rb",
34
38
  "build",
35
39
  "lib/api/account_methods.rb",
40
+ "lib/api/charge_methods.rb",
36
41
  "lib/api/checkout_methods.rb",
42
+ "lib/api/preapproval_methods.rb",
37
43
  "lib/generators/wepay_rails/install/install_generator.rb",
38
44
  "lib/generators/wepay_rails/install/templates/create_wepay_checkout_records.rb",
39
45
  "lib/generators/wepay_rails/install/templates/wepay.yml",
@@ -45,6 +51,7 @@ Gem::Specification.new do |s|
45
51
  "test/test_wepay_rails_authorize.rb",
46
52
  "test/test_wepay_rails_checkout_methods.rb",
47
53
  "test/test_wepay_rails_initialize.rb",
54
+ "test/test_wepay_rails_preapproval_methods.rb",
48
55
  "wepay-rails.gemspec"
49
56
  ]
50
57
  s.homepage = "http://github.com/adamthedeveloper/wepay-rails"
@@ -59,6 +66,7 @@ Gem::Specification.new do |s|
59
66
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
67
  s.add_runtime_dependency(%q<httparty>, [">= 0"])
61
68
  s.add_development_dependency(%q<rails>, ["= 3.1.0"])
69
+ s.add_development_dependency(%q<rake>, [">= 0"])
62
70
  s.add_development_dependency(%q<turn>, ["= 0.8.2"])
63
71
  s.add_development_dependency(%q<thor>, [">= 0"])
64
72
  s.add_development_dependency(%q<bundler>, [">= 0"])
@@ -68,6 +76,7 @@ Gem::Specification.new do |s|
68
76
  else
69
77
  s.add_dependency(%q<httparty>, [">= 0"])
70
78
  s.add_dependency(%q<rails>, ["= 3.1.0"])
79
+ s.add_dependency(%q<rake>, [">= 0"])
71
80
  s.add_dependency(%q<turn>, ["= 0.8.2"])
72
81
  s.add_dependency(%q<thor>, [">= 0"])
73
82
  s.add_dependency(%q<bundler>, [">= 0"])
@@ -78,6 +87,7 @@ Gem::Specification.new do |s|
78
87
  else
79
88
  s.add_dependency(%q<httparty>, [">= 0"])
80
89
  s.add_dependency(%q<rails>, ["= 3.1.0"])
90
+ s.add_dependency(%q<rake>, [">= 0"])
81
91
  s.add_dependency(%q<turn>, ["= 0.8.2"])
82
92
  s.add_dependency(%q<thor>, [">= 0"])
83
93
  s.add_dependency(%q<bundler>, [">= 0"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wepay-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-16 00:00:00.000000000 Z
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - '='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 3.1.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: turn
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +164,7 @@ extra_rdoc_files:
148
164
  - README
149
165
  - README.rdoc
150
166
  files:
167
+ - .DS_Store
151
168
  - .document
152
169
  - .travis.yml
153
170
  - Gemfile
@@ -157,13 +174,18 @@ files:
157
174
  - README.rdoc
158
175
  - Rakefile
159
176
  - VERSION
177
+ - app/.DS_Store
160
178
  - app/controllers/wepay/application_controller.rb
161
179
  - app/controllers/wepay/authorize_controller.rb
180
+ - app/controllers/wepay/charge_controller.rb
162
181
  - app/controllers/wepay/checkout_controller.rb
163
182
  - app/controllers/wepay/ipn_controller.rb
183
+ - app/controllers/wepay/preapproval_controller.rb
164
184
  - build
165
185
  - lib/api/account_methods.rb
186
+ - lib/api/charge_methods.rb
166
187
  - lib/api/checkout_methods.rb
188
+ - lib/api/preapproval_methods.rb
167
189
  - lib/generators/wepay_rails/install/install_generator.rb
168
190
  - lib/generators/wepay_rails/install/templates/create_wepay_checkout_records.rb
169
191
  - lib/generators/wepay_rails/install/templates/wepay.yml
@@ -175,6 +197,7 @@ files:
175
197
  - test/test_wepay_rails_authorize.rb
176
198
  - test/test_wepay_rails_checkout_methods.rb
177
199
  - test/test_wepay_rails_initialize.rb
200
+ - test/test_wepay_rails_preapproval_methods.rb
178
201
  - wepay-rails.gemspec
179
202
  homepage: http://github.com/adamthedeveloper/wepay-rails
180
203
  licenses:
@@ -191,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
214
  version: '0'
192
215
  segments:
193
216
  - 0
194
- hash: 1100875515219871170
217
+ hash: -3118389177307710448
195
218
  required_rubygems_version: !ruby/object:Gem::Requirement
196
219
  none: false
197
220
  requirements: