Skip to content
Browse files

Charge Successful, clean up gem code

  • Loading branch information...
1 parent c4ea780 commit de86c8af926e07d9a50f037322c3c7887f64c38b @lvxn0va lvxn0va committed
View
51 app/controllers/wepay/charge_controller.rb
@@ -1,24 +1,43 @@
class Wepay::ChargeController < Wepay::ApplicationController
+ include WepayRails::Payments
+
def index
- conds = {
- :security_token => params[:security_token],
- :preapproval_id => params[:preapproval_id],
- :checkout_id => params[:checkout_id],
- }
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
- record = WepayCheckoutRecord.where(conds).first
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
+ charge = wepay_gateway.lookup_preapproval(record.preapproval_id)
- if record.present?
- wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
- charge = wepay_gateway.lookup_preapproval(record.preapproval_id)
+ # remove unnecessary attributes
+ charge.delete_if {|k,v| !record.attributes.include? k.to_s}
- # remove unnecessary attributes
- charge.delete_if {|k,v| !record.attributes.include? k.to_s}
+ record.update_attributes(charge)
+ redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
+ else
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
+ end
+ end
+
+ def success
+ response = WepayCheckoutRecord.find(:last)
+ wepay_gateway = WepayRails::Payments::Gateway.new( response.access_token )
+ charge = wepay_gateway.lookup_checkout(response.checkout_id)
- record.update_attributes(charge)
- redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
- else
- raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
- end
+ response.update_attributes(charge)
+ logger.info params
+ render :text => "Wepay charge OK from #{response.payer_email} with Checkout ID # #{response.checkout_id} from the Pre-approval ID # #{response.preapproval_id}."
+ end
+
+ def new
+ # The following is from the Wepay API sample checkout call
+ # create the checkout
+ charge_params = {
+ :amount => '50.00',
+ :short_description => 'A Checkout on the Wepay PreApproval.',
+ :type => 'GOODS',
+ :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.
+ }
+ # Finally, send user to charge on the preapproval.
+ init_charge_and_return_success(charge_params)
end
end
View
21 app/controllers/wepay/checkout_controller.rb
@@ -1,11 +1,8 @@
class Wepay::CheckoutController < Wepay::ApplicationController
+ include WepayRails::Payments
+
def index
- conds = {
- :security_token => params[:security_token],
- :checkout_id => params[:checkout_id],
- }
-
- record = WepayCheckoutRecord.where(conds).first
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
if record.present?
wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
@@ -20,4 +17,16 @@ def index
raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
end
end
+
+ def new
+ # create the checkout - This is TEST info from Wepay only
+ checkout_params = {
+ :amount => '255.00',
+ :short_description => 'A transaction for WePay Testing',
+ :type => 'GOODS'
+ }
+ # Finally, send the user off to wepay so you can get paid! - CASH MONEY
+ init_checkout_and_send_user_to_wepay(checkout_params)
+ end
+
end
View
54 app/controllers/wepay/preapproval_controller.rb
@@ -1,21 +1,37 @@
-def index
- conds = {
- :security_token => params[:security_token],
- :preapproval_id => params[:preapproval_id],
- }
-
- record = WepayCheckoutRecord.where(conds).first
-
- if record.present?
- wepay_gateway = WepayRails::Payments::Gateway.new ( record.access_token )
- preapproval = wepay_gateway.lookup_preapproval(record.preapproval_id)
-
- #remove unneccesary attributes
- preapproval.delete_if {|k,v| !record.attributes.include? k.to_s}
+class Wepay::PreapprovalController < Wepay::ApplicationController
+ include WepayRails::Payments
+
+ def index
+ record = WepayCheckoutRecord.find_by_preapproval_id_and_security_token(params[:preapproval_id],params[:security_token])
- record.update_attributes(preapproval)
- redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?preapproval_id=#{params[:preapproval_id]}"
- else
- raise StandardError.new("Wepay IPN: No record found for preapproval_id #{params[:preapproval_id]} and security_token #{params[:security_token]}")
- end
+
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new ( record.access_token )
+ preapproval = wepay_gateway.lookup_preapproval(record.preapproval_id)
+
+ #remove unneccesary attributes
+ #preapproval.delete_if {|k,v| !record.attributes.include? k.to_s}
+
+ record.update_attributes(preapproval)
+ redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?preapproval_id=#{params[:preapproval_id]}"
+ else
+ raise StandardError.new("Wepay IPN: No record found for preapproval_id #{params[:preapproval_id]} and security_token #{params[:security_token]}")
+ end
+ end
+
+ def new
+ # create the preapproval - This is TEST info
+ preapproval_params = {
+ :period => 'once',
+ :end_time => '2013-12-25',
+ :amount => '50.00',
+ :mode => 'regular',
+ :short_description => 'A Preapproval for MyApp.',
+ :app_fee => "10",
+ :fee_payer => 'payee',
+ :payer_email_message => "You just approved MyApp to charge you for a some money later. You have NOT been charged at this time!"
+ }
+ # Finally, send the user off to wepay for the preapproval
+ init_preapproval_and_send_user_to_wepay(preapproval_params)
+ end
end
View
2 lib/helpers/controller_helpers.rb
@@ -128,7 +128,7 @@ def init_charge(params, access_token=nil)
WepayCheckoutRecord.create(params)
end
- def init_charge_and_return_ipn(params, access_token=nil)
+ def init_charge_and_return_success(params, access_token=nil)
record = init_charge(params, access_token)
redirect_to charge_success_url and return record
end
View
3 lib/wepay-rails.rb
@@ -43,6 +43,9 @@ def routes(rails_router)
resources :preapproval
resources :charge
end
+ match '/preapproval/success' => 'wepay/preapproval#success'
+ match '/preapproval/charge' => 'wepay/charge#new'
+ match '/charge/success' => 'wepay/charge#success'
end
end
end

0 comments on commit de86c8a

Please sign in to comment.
Something went wrong with that request. Please try again.