Skip to content

Instantly share code, notes, and snippets.

@richardsondx
Created July 3, 2012 17:59
Show Gist options
  • Save richardsondx/11be6cef6a97632343b9 to your computer and use it in GitHub Desktop.
Save richardsondx/11be6cef6a97632343b9 to your computer and use it in GitHub Desktop.
Paypa Express Payment in Rails 3.2
#Initializer
ActiveMerchant::Billing::Base.mode = :test
::GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(
:login => "rico_1340337222_biz_api1.gmail.com",
:password => "I_PUT_MY_PASS_HERE",
:signature => "I_PUT_MY_SIGNATURE_HERE",
:allow_guest_checkout => true
)
This transaction is invalid. Please return to the recipient's website to complete your transaction using their regular checkout flow.
Return to merchant
At this time, we are unable to process your request. Please return to and try another option.
---
<%= link_to image_tag('http://img36.imageshack.us/img36/249/buttonpaypal.png'), action: 'checkout', controller: 'orders'%>
class Order < ActiveRecord::Base
attr_accessible :token
def confirm
redirect_to :action => root_path unless params[:token]
details_response = ::GATEWAY.details_for(params[:token])
if !details_response.success?
@message = details_response.message
render :action => 'error'
return
end
@address = details_response.address
end
def new
@order = Order.new(:express_token => params[:token])
end
def complete
purchase = ::GATEWAY.purchase(5000,
:ip => request.remote_ip,
:payer_id => params[:payer_id],
:token => params[:token]
)
if !purchase.success?
@message = purchase.message
render :action => 'error'
return
end
end
end
class OrdersController < ApplicationController
include ActiveMerchant::Billing
def checkout
setup_response = ::GATEWAY.setup_purchase(2000,
:ip => request.remote_ip,
:return_url => url_for('confirm'),
:cancel_return_url => url_for(root_path)
)
redirect_to ::GATEWAY.redirect_url_for(setup_response.token)
end
end
resources :orders do
# Im not sure why 'get :checkout' by itself doesn't work.
get :checkout, :on => :new
get :confirm
get :complete
end
get "pages/index"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment