Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Collect payments from wepay in your rails application.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app/controllers/wepay
config
lib
test
.document
.gitignore
Gemfile
Gemfile.lock
LICENSE.txt
README
README.rdoc
Rakefile
VERSION
wepay-rails.gemspec

README.rdoc

IMPORTANT - This gem is under heavy development and is in extreme alpha phase. As soon as it's in a release state (very soon), I will update the MAJOR version to 1. If you are interested in helping, please drop me a message.

wepay-rails

Wepay-Rails allows your rails app to accept payments with Wepay (www.wepay.com).

To install it, add this to your Gemfile

gem 'wepay-rails'

Since Wepay uses Oauth2 to authenticate, wepay-rails has been built to handle this for you. You will need to add a column to one of your models to hold the authentication token. For example, if you have a user model:

Your migration:

add_column :my_model, :wepay_auth_code, :string

You need to also create a new model called WepayCheckoutRecord. It 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.

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

Modify config/wepay.yml.example to your needs. You will need to set the model and column where you are storing your wepay auth code:

Snippet of wepay.yml showing the auth_code_location directive:

production:
  auth_code_location: MyModel.wepay_auth_code

You will have to set up a few new controllers in your rails app to handle callbacks and redirects from wepay. I created one 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 differnt than mine. Do what makes sense to you. For now, here's what I am doing in my rails app to handle it…

app
  |_ controllers
               |_ purchase
                      |_ checkout_controller.rb
                      |_ finalize_controller.rb

The wepay-rails gem comes with an IpnController already built in for handling requests from wepay about a specific checkout. If you wish to override it, you can create an IpnController in your rails app.

class IpnController < Wepay::ApplicationController
    def index
        #Do something with the instant payment notifications back from Wepay
    end
end

wepay.yml will also need these directives. See the wepay.yml.example file in config.

When you include WepayRails::Payments, you get the controller actions you need. For instance, initialize_wepay_access_token(auth_code) which completes the Oauth2 handshake with Wepay and get's the access token for future comunications with Wepay.

Finally, your checkout controller (or some controller that will interact with the Wepay API):

class Purchase::CheckoutController < Purchase::PurchaseController
    before_filter :authenticate_account! # I am using devise - this line depends on your authentication scheme

    # PLEASE READ
    # I am saving the wepay_auth_code in my Profile model. Somewhere in my rails app, I have a method called
    # current_profile which I use to return the Profile object of my user. I pass that object through the
    # init_checkout_and_send_user_to_wepay method along with my checkout parameters.

    # After the customer is sent to wepay, when they are done paying you, they will be redirected
    # back to your application - you will set the location they will be redirected back to using the redirect_uri directive
    # either here - or in wepay.yml. Using it here overrides wepay.yml.

    def index

        cart = current_account.cart # EXAMPLE - get my shopping cart
        tx_id = cart.transaction_id # EXAMPLE - I use a column in my cart to have a way to look up the cart upon the user's return from wepay

        checkout_params = {
            :amount => cart.grand_total,
            :short_description => cart.short_description,
            :long_description => cart.long_description,
            :redirect_uri => purchase_finalize_index_url(:txID => tx_id) # Wepay redirects the user back to this url after purchase
        }

        # Finally, send the user off to wepay so you can get paid! - CASH MONEY
        init_checkout_and_send_user_to_wepay(checkout_params, current_profile)
    end
end

The controller I use for finalizing the checkout - AKA - the controller the user is sent back to after his/her trip to wepay to checkout.

class Purchase::FinalizeController < ApplicationController
    def index
        # Do something - the user has come back from wepay and need an acknowlegement or something.
        # For example - my app does something like:

        cart = Cart.find_by_transaction_id(params[:txID])
        # my redirect_uri included a txID so that I can look up the
        # cart when the customer came back.
    end
end

Example Routes for these:

namespace :purchase do
    resource :cart, :has_many => :purchase_items
    resources :checkout, :only => [:index]
    resources :finalize, :only => [:index]
end

Special Thanks to additional contributers of Wepay-Rails

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 © 2011 Adam Medeiros. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.