Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Collect payments from wepay in your rails application.

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 config
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 wepay-rails.gemspec
README.rdoc

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 :users, :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: User.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

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

class Purchase::CheckoutController < Purchase::PurchaseController
    before_filter :authenticate_user! # 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_user.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,
        }

        # Finally, send the user off to wepay so you can get paid! - CASH MONEY
        init_checkout_and_send_user_to_wepay(checkout_params, current_user)
    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 < Wepay::ApplicationController
  def index
    wcr  = WepayCheckoutRecord.find_by_checkout_id(params[:checkout_id])

    # 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 => wcr.inspect
  end
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.