Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add callback uri for revoked access tokens, fix bug in generator #32

Merged
merged 9 commits into from

2 participants

@jules27
  • WePay added a new optional parameter in /oauth2/token called callback_uri for receiving IPNs for revoked access tokens for a user. Add this to get_access_token method.
  • Remove the belongs_to associations in WepayCheckoutRecord, as those are not required by default and can be customized.
  • Fix a bug in the install generator where WepayCheckoutRecord is not copied to the correct directory.
  • Convert README from .rdoc to .md and add an example of using the gem to authorize a user.
@adamthedeveloper adamthedeveloper merged commit 0f68b7a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2013
  1. @jules27

    Add callback_uri to get_access_token, which uses WePay's /oauth2/toke…

    jules27 authored
    …n API. The callback_uri is used to receive IPNs when the user revokes an access_token or is deleted.
Commits on Apr 18, 2013
  1. @jules27

    Try changing the generator to copy wepay_checkout_record model to app…

    jules27 authored
    …/models instead of lib/models.
  2. @jules27
  3. @jules27

    Remove empty README as well.

    jules27 authored
  4. @jules27

    Update styling in README.md.

    jules27 authored
  5. @jules27
  6. @jules27
  7. @jules27

    Tried bundle update for the gem, but cannot figure out how to resolve…

    jules27 authored
    … rcov conflict with Ruby 1.9. Adding the empty readme files back.
  8. @jules27
This page is out of date. Refresh to see the latest.
View
202 README.md
@@ -0,0 +1,202 @@
+# wepay-rails
+
+Wepay-Rails allows your rails app to accept payments with [Wepay](http://www.wepay.com).
+
+[![build status][travis]][travis-link]
+[travis]: https://secure.travis-ci.org/adamthedeveloper/wepay-rails.png?branch=master
+[travis-link]: http://travis-ci.org/adamthedeveloper/wepay-rails
+
+## Features
+
+* Added PREAPPROVAL and DELAYED CHARGE ability 09/2012
+* Built in IPN that listens to push notifications from wepay and updates saved checkout records for you
+* Allows you to fetch your access token easily through a web interface
+* Built in API tools to allow you to make all wepay-api calls easily
+* Built in ability to send your customers to wepay to make payments and handles their trip back for you OR use iFrame for checkouts (see Wiki https://github.com/adamthedeveloper/wepay-rails/wiki/Using-the-wepay-iframe)
+* Saves the current state of every checkout
+* Authorize many users to accept payments dynamically (see Wiki https://github.com/adamthedeveloper/wepay-rails/wiki/Authorize-Many-Users-Dynamically)
+* Configurable
+
+## Installation
+
+To install it, add this to your Gemfile
+
+```ruby
+gem 'wepay-rails'
+```
+
+To create your WepayCheckoutRecord model and migration:
+
+```console
+script/rails g wepay_rails:install
+```
+
+This will create 3 files for you, a migration file for a table to hold the checkout results, the model and a wepay.yml.example file. Next run:
+
+```console
+rake db:migrate
+```
+
+WepayCheckoutRecord will be updated by wepay's IPN system as changes to the checkout change - such as the status.
+Wepay-rails handles those IPN notifications for you. You can write observers watching the WepayCheckoutRecord model if you need to have
+something specific occur when the checkout changes. Also, since a model is created for you, you can also track changes to it's state
+through state machine or paper trail or some other gem of your liking. Hook up WepayCheckoutRecord how you like.
+
+Modify config/wepay.yml.example to your needs and copy it to config/wepay.yml.
+
+Assuming that you have:
+
+1. created an account on wepay
+2. created a user to accept the payments
+3. created your application for your account
+4. set your wepay.yml file with the info it needs to talk to the wepay api minus the access_token
+
+You can now get your access token.
+
+To fetch your access_token, open a browser and go to:
+
+```console
+your.railsapp.com/wepay/authorize
+```
+
+Login at the prompt or register. You will be sent back to your app and you should have gotten an access_token. Copy it to your wepay.yml
+file and restart your app.
+
+## Example
+
+I created a controller called finalize_controller and I use it for a landing page when the customer is finished paying
+their order. The other controller I created is a checkout_controller - I send my customers to it when they click checkout
+in the cart. Your app is surely different than mine. Do what makes sense to you.
+For now, here's a small example...
+
+```
+app
+ |_ controllers
+ |_ purchase
+ |_ checkout_controller.rb
+ |_ finalize_controller.rb
+```
+
+Tell wepay-rails where to send the customer after they come back from wepay with a complete payment. Open wepay.yml:
+
+```ruby
+after_checkout_redirect_uri: "http://www.example.com/purchase/finalize"
+```
+
+Create a controller that will send the user to wepay - notice it includes WepayRails::Payments:
+
+```ruby
+class Purchase::CheckoutController < ApplicationController
+ include WepayRails::Payments
+
+ def index
+ cart = current_user.cart # EXAMPLE - get my shopping cart
+
+ checkout_params = {
+ :amount => cart.grand_total,
+ :short_description => cart.short_description,
+ :long_description => cart.long_description,
+ }
+
+ # 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
+```
+
+Finally, the controller I use for finalizing the checkout - AKA - the controller the user is sent back to after his/her trip back from
+wepay. A checkout_id is passed in through params so you can access the WepayCheckoutRecord, make a call to
+wepay to get the checkout info - whatever you want to do (See the wiki for more info on API calls):
+
+```ruby
+class Purchase::FinalizeController < ApplicationController
+ def index
+ # Fetch the WepayCheckoutRecord that was stored for the checkout
+ wcr = WepayCheckoutRecord.find_by_checkout_id(params[:checkout_id])
+
+ # Example: Set the association of the wepay checkout record to my cart - then, on to order.
+ cart = current_account.cart
+ cart.wepay_checkout_record = wcr
+ cart.save!
+
+ # Convert cart to an order?? Move to observer of WepayCheckoutRecord??
+ cart.convert_cart_to_order if wcr.state == 'authorized'
+
+ render :text => "Hooray - you bought some widgets!"
+ end
+end
+```
+
+## Example of WePay Oauth
+
+For reference, please refer to WePay's [documentation on Oauth](https://www.wepay.com/developer/reference/oauth2).
+
+### Setup
+
+As an example, I have the User model, view, and controller.
+
+I have the following routes for WePay in my `config/routes.rb:
+
+```ruby
+match 'wepay_connect', to: 'users#wepay_connect'
+match 'wepay_auth', to: 'users#wepay_auth'
+```
+
+### Controllers
+
+The first method that we will hit in this example is `wepay_connect` in my `Users` controller:
+
+```ruby
+def wepay_connect
+ wepay_gateway = WepayRails::Payments::Gateway.new
+ redirect_to wepay_gateway.auth_code_url( wepay_auth_path(current_user, only_path: false) )
+end
+```
+
+This will send the user to WePay's `/oauth2/authorize` uri to start the Oauth flow.
+
+The response is an authorization code, which is used to get the user's access token.
+
+The next method we will hit is `wepay_auth`:
+
+```ruby
+def wepay_auth
+ wepay_gateway = WepayRails::Payments::Gateway.new
+ access_token = wepay_gateway.get_access_token(params[:code], wepay_auth_path(current_user, :only_path => false) )
+ if current_user.update_attributes(wepay_access_token: access_token, wepay_user_id: wepay_gateway.account_id)
+ flash[:success] = "Your WePay account is now connected!"
+ redirect_to root_path
+ end
+end
+```
+
+### Start Oauth
+
+I have this in the view, assuming that a user is currently signed in:
+
+```ruby
+link_to "Connect To WePay", wepay_connect_path
+```
+
+When the user clicks on this link, he will be prompted to start the WePay Oauth flow.
+
+## Special Thanks to additional contributers of Wepay-Rails
+* lucisferre (Chris Nicola) https://github.com/lucisferre
+* mindeavor (Gilbert) https://github.com/mindeavor
+* ustorf (Bernd Ustorf) https://github.com/ustorf
+* dragonstarwebdesign (Steve Aquino) https://github.com/dragonstarwebdesign
+* jules27 (Julie Mao) https://github.com/jules27
+
+## Contributing to wepay-rails
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
+* Fork the project
+* Start a feature/bugfix branch
+* Commit and push until you are happy with your contribution
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+## Copyright
+
+Copyright (c) 2011 Adam Medeiros. See LICENSE.txt for further details.
View
134 README.rdoc
@@ -1,134 +0,0 @@
-= wepay-rails
-
-Wepay-Rails allows your rails app to accept payments with Wepay (http://www.wepay.com).
-
-{<img src="https://secure.travis-ci.org/adamthedeveloper/wepay-rails.png?branch=master" alt="Build Status" />}[http://travis-ci.org/adamthedeveloper/wepay-rails]
-
-= Features
-
-* Added PREAPPROVAL and DELAYED CHARGE ability 09/2012
-* Built in IPN that listens to push notifications from wepay and updates saved checkout records for you
-* Allows you to fetch your access token easily through a web interface
-* Built in API tools to allow you to make all wepay-api calls easily
-* Built in ability to send your customers to wepay to make payments and handles their trip back for you OR use iFrame for checkouts (see Wiki https://github.com/adamthedeveloper/wepay-rails/wiki/Using-the-wepay-iframe)
-* Saves the current state of every checkout
-* Authorize many users to accept payments dynamically (see Wiki https://github.com/adamthedeveloper/wepay-rails/wiki/Authorize-Many-Users-Dynamically)
-* Configurable
-
-= Installation
-
-To install it, add this to your Gemfile
-
- gem 'wepay-rails'
-
-To create your WepayCheckoutRecord model and migration:
-
- script/rails g wepay_rails:install
-
-This will create 3 files for you, a migration file for a table to hold the checkout results, the model and a wepay.yml.example file. Next run:
-
- rake db:migrate
-
-WepayCheckoutRecord will be updated by wepay's IPN system as changes to the checkout change - such as the status.
-Wepay-rails handles those IPN notifications for you. You can write observers watching the WepayCheckoutRecord model if you need to have
-something specific occur when the checkout changes. Also, since a model is created for you, you can also track changes to it's state
-through state machine or paper trail or some other gem of your liking. Hook up WepayCheckoutRecord how you like.
-
-Modify config/wepay.yml.example to your needs and copy it to config/wepay.yml.
-
-Assuming that you have:
-
-1. created an account on wepay
-2. created a user to accept the payments
-3. created your application for your account
-4. set your wepay.yml file with the info it needs to talk to the wepay api minus the access_token
-
-You can now get your access token.
-
-To fetch your access_token, open a browser and go to:
-
- your.railsapp.com/wepay/authorize
-
-Login at the prompt or register. You will be sent back to your app and you should have gotten an access_token. Copy it to your wepay.yml
-file and restart your app.
-
-= Example
-
-I created a controller called finalize_controller and I use it for a landing page when the customer is finished paying
-their order. The other controller I created is a checkout_controller - I send my customers to it when they click checkout
-in the cart. Your app is surely different than mine. Do what makes sense to you.
-For now, here's a small example...
-
- app
- |_ controllers
- |_ purchase
- |_ checkout_controller.rb
- |_ finalize_controller.rb
-
-Tell wepay-rails where to send the customer after they come back from wepay with a complete payment. Open wepay.yml:
-
- after_checkout_redirect_uri: "http://www.example.com/purchase/finalize"
-
-Create a controller that will send the user to wepay - notice it includes WepayRails::Payments:
-
- class Purchase::CheckoutController < ApplicationController
- include WepayRails::Payments
-
- def index
- cart = current_user.cart # EXAMPLE - get my shopping cart
-
- checkout_params = {
- :amount => cart.grand_total,
- :short_description => cart.short_description,
- :long_description => cart.long_description,
- }
-
- # 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
-
-Finally, the controller I use for finalizing the checkout - AKA - the controller the user is sent back to after his/her trip back from
-wepay. A checkout_id is passed in through params so you can access the WepayCheckoutRecord, make a call to
-wepay to get the checkout info - whatever you want to do (See the wiki for more info on API calls):
-
- class Purchase::FinalizeController < ApplicationController
- def index
- # Fetch the WepayCheckoutRecord that was stored for the checkout
- wcr = WepayCheckoutRecord.find_by_checkout_id(params[:checkout_id])
-
- # Example: Set the association of the wepay checkout record to my cart - then, on to order.
- cart = current_account.cart
- cart.wepay_checkout_record = wcr
- cart.save!
-
- # Convert cart to an order?? Move to observer of WepayCheckoutRecord??
- cart.convert_cart_to_order if wcr.state == 'authorized'
-
- render :text => "Hooray - you bought some widgets!"
- end
- end
-
-
-== Special Thanks to additional contributers of Wepay-Rails
-* lucisferre (Chris Nicola) https://github.com/lucisferre
-* mindeavor (Gilbert) https://github.com/mindeavor
-* ustorf (Bernd Ustorf) https://github.com/ustorf
-* dragonstarwebdesign (Steve Aquino) https://github.com/dragonstarwebdesign
-
-
-== Contributing to wepay-rails
-
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
-* Fork the project
-* Start a feature/bugfix branch
-* Commit and push until you are happy with your contribution
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
-
-== Copyright
-
-Copyright (c) 2011 Adam Medeiros. See LICENSE.txt for
-further details.
-
View
2  lib/generators/wepay_rails/install/install_generator.rb
@@ -21,7 +21,7 @@ def setup_routes
def copy_migrations
migration_template "create_wepay_checkout_records.rb", "db/migrate/create_wepay_checkout_records.rb"
- copy_file "wepay_checkout_record.rb", "lib/models/wepay_checkout_record.rb"
+ copy_file "wepay_checkout_record.rb", "app/models/wepay_checkout_record.rb"
copy_file "wepay.yml", "config/wepay.yml.example"
end
end
View
4 lib/generators/wepay_rails/install/templates/wepay_checkout_record.rb
@@ -1,8 +1,4 @@
class WepayCheckoutRecord < ActiveRecord::Base
- belongs_to :checkout
- belongs_to :preapproval
- belongs_to :ipn
- belongs_to :authorize
attr_accessible :amount,
:short_description,
:access_token,
View
6 lib/wepay-rails.rb
@@ -144,12 +144,14 @@ def call_api(api_path, params={}, timeout=30000)
end
# Fetch the access token from wepay for the auth code
- def get_access_token(auth_code, redirect_uri)
+ def get_access_token(auth_code, redirect_uri, callback_uri = nil)
params = {
:client_id => @wepay_config[:client_id],
:client_secret => @wepay_config[:client_secret],
:redirect_uri => redirect_uri,
- :code => auth_code
+ :code => auth_code,
+ :callback_uri => callback_uri # Optional field in which you will receive IPNs with the user_id
+ # when the user revokes an access_token or is deleted.
}
json = call_api("/oauth2/token", params)
raise WepayRails::Exceptions::AccessTokenError.new("A problem occurred trying to get the access token: #{json.inspect}") unless json.has_key?(:access_token)
Something went wrong with that request. Please try again.