lib/helpers/controller_helpers.rb in wepay-rails-0.1.116 vs lib/helpers/controller_helpers.rb in wepay-rails-0.2.0
- old
+ new
@@ -33,11 +33,24 @@
# &code=[the code you got in step one]
#
# Response
# {"user_id":"123456","access_token":"1337h4x0rzabcd12345","token_type":"BEARER"} Example
def initialize_wepay_access_token(wepayable_object)
- session[unique_wepay_access_token_key] = wepay_gateway.access_token(wepayable_object)
+ return if wepay_access_token_exists?
+ begin
+ # check to see if they have an auth code
+ # If not, send them to wepay to get one
+ wepayable_column = WepayRails::Configuration.wepayable_column
+ raise unless wepayable_object.send(wepayable_column.to_sym).present?
+
+ # It's possible that we raise an exception here - probably the auth code
+ # was too old and they need an updated one. Send them to wepay to
+ # get a new one if a raise happens while we run the following line of code.
+ session[unique_wepay_access_token_key] = wepay_gateway.access_token(wepayable_object)
+ rescue
+ redirect_to_wepay_for_auth(wepayable_object)
+ end
return
end
# Since we are saving the access token in the session,
# ensure key uniqueness. Might be a good idea to have this
@@ -93,10 +106,11 @@
# :callback_uri No The uri that will receive any Instant Payment Notifications sent. Needs to be a full uri (ex https://www.wepay.com )
# :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
# :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
# :shipping_fee No The amount that you want to charge for shipping.
# :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.
- def init_checkout_and_send_user_to_wepay(params)
+ def init_checkout_and_send_user_to_wepay(params, wepayable_object=nil)
+ initialize_wepay_access_token(wepayable_object) if wepayable_object.present?
response = wepay_gateway.perform_checkout(params)
checkout = WepayCheckoutRecord.create(params.merge({ checkout_id: response['checkout_id'] }))
raise WepayRails::Exceptions::InitializeCheckoutError.new("A problem occurred while trying to checkout. Wepay didn't send us back a checkout uri. Response was: #{response.inspect}, Params were: #{params}, Token was: #{wepay_access_token}") unless response && response.has_key?('checkout_uri')
redirect_to response['checkout_uri'] and return
end