Your cart
Choose your method of payment
PayPal Website Standard
This is the most basic method of payment. You will be navigated to PayPal where you can enter your
credit card information or sign in to your account.
Show Code
- app/views/paypal/_website_standard.haml
%form{:action => PaypalController::FORM_URL, :method => "post"}
= hidden_field_tag :cmd,"_cart"
= hidden_field_tag :upload,1
= hidden_field_tag :business, PaypalController::ACCOUNT
= hidden_field_tag :currency_code, "USD"
= hidden_field_tag :no_shipping, 1
-
- i = 1
- for purchasable in @cart.to_a
= hidden_field_tag "item_name_#{i}", purchasable.human_name
= hidden_field_tag "amount_#{i}", fmt_money(purchasable.unit_price)
= hidden_field_tag "quantity_#{i}", purchasable.quantity
- i += 1
= hidden_field_tag :notify_url, notify_url
= hidden_field_tag :cancel_return, url_for(:action=>"checkout",:only_path=>false)
= hidden_field_tag :return, url_for(:action=>"purchase_success",:only_path=>false)
= paypal_submit
PayPal Website Standard from ActiveMerchant
This method is the same as above except the html form was generated using ActiveMerchant.
Show Code
- app/views/paypal/_website_standard_active_merchant.haml
/- website_standard_active_merchant do
/ = paypal_submit
- payment_service_for 1000, PaypalController::ACCOUNT, |
:currency => 'USD', |
:service => :paypal, |
:html => { :id => 'payment-form' } do |service| |
- service.cmd "_cart"
- service.add_field 'upload', "1"
- service.no_shipping '1'
-
- service.form_fields.delete('item_name')
- service.form_fields.delete('item_number')
- service.form_fields.delete('quantity')
-
- i = 1
- for purchasable in @cart.to_a
- service.add_field "item_name_#{i}",purchasable.human_name
- service.add_field "amount_#{i}",fmt_money(purchasable.unit_price)
- service.add_field "quantity_#{i}",purchasable.quantity
- i += 1
- service.notify_url notify_url
- service.return_url url_for(:action=>"purchase_success",:only_path=>false)
- service.cancel_return_url url_for(:action=>"checkout",:only_path=>false)
= paypal_submit
Google Checkout
A Google Checkout button using the HTML API. Buyers are navigated to Google where they must sign in or create a new account.
Show Code
- app/views/google/_checkout.haml
%form{:action => "https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/#{ENV['GOOGLE_CHECKOUT_MERCHANT_ID']}", :method => "post", :accept_charset => "utf-8"}
- i = 1
- for purchasable in @cart.to_a
= hidden_field_tag "item_name_#{i}", purchasable.human_name
= hidden_field_tag "item_description_#{i}", purchasable.short_description
= hidden_field_tag "item_price_#{i}", fmt_money(purchasable.unit_price)
= hidden_field_tag "item_quantity_#{i}", purchasable.quantity
- i += 1
= hidden_field_tag "_charset_"
- image_src = "http://checkout.google.com/buttons/checkout.gif?merchant_id=#{ENV['GOOGLE_CHECKOUT_MERCHANT_ID']}&w=180&h=46&style=white&variant=text&loc=en_US"
%input{:type=>"image", :name=>"Google Checkout", :alt=>"Fast checkout through Google", :src=>image_src, :height=>"46", :width=>"180" }
Amazon Checkout
FIXED
An Amazon Checkout button using the HTML API. Buyers are navigated to Amazon.
Show Code
- app/views/amazon/_checkout.haml
/<script type=text/javascript src=https://images-na.ssl-images-amazon.com/images/G/01/cba/js/jquery.js></script>
/<script type=text/javascript src=https://images-na.ssl-images-amazon.com/images/G/01/cba/js/widget/widget.js></script>
- merchant_id = "A1TDXY6XJ7JAMX"
%form{:action => "http://payments.amazon.com/checkout/
- i = 1
- for purchasable in @cart.to_a
= hidden_field_tag "item_merchant_id_#{i}", merchant_id
= hidden_field_tag "item_title_#{i}", purchasable.human_name
= hidden_field_tag "item_price_#{i}", fmt_money(purchasable.unit_price)
= hidden_field_tag "item_quantity_#{i}", purchasable.quantity
/= hidden_field_tag "item_description_#{i}", purchasable.short_description
- i += 1
= hidden_field_tag "currency_code", "USD"
%input{:alt=>"Checkout with Amazon Payments", :src=>"https://payments.amazon.com/gp/cba/button?ie=UTF8&color=orange&background=white&size=medium", :type=>"image"}
Pay using a credit card on this site
This uses ActiveMerchant and the Trust Commerce gateway. No off-site navigation is needed.
Show Code
- app/controllers/billing_controller.rb
class BillingController < RailsVendorController
def new
@billing = RailsVendor::Billing.new
end
def create
begin
raise RailsVendor::Exception.new("Invalid card") unless @cart && @cart.total_price_cents && @cart.total_price_cents > 0
RailsVendor::Billing.transaction do
@billing = RailsVendor::Billing.new(params[:billing])
@billing.save!
active_merchant_credit_card = @billing.active_merchant_credit_card
login = ENV['TRUST_COMMERCE_LOGIN'].to_s
password = ENV['TRUST_COMMERCE_PASSWORD'].to_s
gateway=ActiveMerchant::Billing::TrustCommerceGateway.new(:login => login,:password => password)
cents = @cart.total_price_cents
response = gateway.authorize(cents, active_merchant_credit_card)
raise RailsVendor::Exception.new(response.message) unless response.success?
authorization = response.authorization
response2 = gateway.capture(cents, authorization, {})
raise RailsVendor::Exception.new(response2.message) unless response2.success?
end
redirect_to :controller => "rails_vendor", :action => "purchase_success"
rescue ActiveRecord::RecordInvalid => exc
render :action => "new"
rescue RailsVendor::Exception => exc
flash.now[:note] = exc.to_s
render :action => "new"
end
end
end