-
Notifications
You must be signed in to change notification settings - Fork 155
One time payments
To start selling products, just include Payola::Sellable
in your model. For example, if you have a Book
model:
class Book < ActiveRecord::Base
include Payola::Sellable
end
Each sellable model requires three attributes:
-
price:integer
, (attribute) an amount in the format that Stripe expects. For USD this is cents. -
permalink:string
, (attribute) a human-readable slug that is exposed in the URL -
name:string
, (attribute) a human-readable name exposed on product pages
For example, to create a 'Book' model:
$ rails g model Book price:integer permalink:string name:string
$ rake db:migrate
Add the concern:
class Book < ActiveRecord::Base
include Payola::Sellable
end
Finally, add a book to the database:
$ rails console
irb(main):001:0> Book.create(name: 'The Book', permalink: 'the-book', price: 1000)
There are two optional methods you can implement on your sellable:
-
redirect_path
takes the sale as an argument and returns a path. The buyer's browser will be redirected to that path after a successful sale. This defaults to/
. -
currency
returns the currency for this product. Payola will default tousd
, which can be changed with thedefault_currency
config setting.
When people buy your product, Payola records information in Payola::Sale
records and will record history if you have the paper_trail
gem installed. It is highly recommended to install paper_trail.
To sell a product, use the checkout
partial like this:
<%= render 'payola/transactions/checkout', sellable: YourProductClass.first %>
This will insert a Stripe Checkout button. The checkout
partial has a bunch of options:
-
sellable
: The product to sell. Required. -
button_text
: What to put on the button. Defaults to "Pay Now" -
button_class
: What class to put on the actual button. Defaults to "stripe-button-el". -
name
: What to put at the top of the Checkout popup. Defaults toproduct.name
. -
description
: What to show as the description in the popup. Defaults to product name + the formatted price. -
product_image_path
: An image to insert into the Checkout popup. Defaults to blank. -
panel_label
: The label of the button in the Checkout popup. -
allow_remember_me
: Whether to show the Remember me checkbox. Defaults to true. -
email
: Email address to pre-fill. Defaults to blank. -
custom_fields
: Data to pass to thecharge_verifier
(see Configuration Options)
Payola's custom form support is basic but functional. Setting up a custom form has two steps. First, include the stripe_header
partial in your layout's <head>
tag:
<%= render 'payola/transactions/stripe_header' %>
Now, to set up your form, give you need to give it the class payola-payment-form
and set a few data attributes:
<%= form_for @whatever,
html: {
class: 'payola-payment-form',
'data-payola-base-path' => main_app.payola_path,
'data-payola-product' => @product.product_class,
'data-payola-permalink' => @product.permalink
} do |f| %>
<span class="payola-payment-error"></span>
Email:<br>
<input type="email" name="stripeEmail"
data-payola="email"></input><br>
Card Number<br>
<input type="text" data-stripe="number"></input><br>
Exp Month<br>
<input type="text" data-stripe="exp_month"></input><br>
Exp Year<br>
<input type="text" data-stripe="exp_year"></input><br>
CVC<br>
<input type="text" data-stripe="cvc"></input><br>
<input type="submit"></input>
<% end %>
You need to set these three data attributes:
-
data-payola-base-path
: should always be set tomain_app.payola_path
-
data-payola-product
: theproduct_class
of the sellable you're selling -
data-payola-permalink
: the permalink for this specific sellable
In addition, you should mark up the email
input in your form with data-payola="email"
in order for it to be set up in your Payola::Sale
properly.
After that, you should mark up your card fields as laid out in the Stripe docs. Ensure that these fields do not have name
attributes because you do not want them to be submitted to your application.