Repository navigation
Expand file tree
/
Copy pathapp.rb
More file actions
160 lines (134 loc) · 5.11 KB
/
Copy pathapp.rb
File metadata and controls
160 lines (134 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'rubygems'
require 'open-uri'
require 'bundler'
Bundler.require
require 'sinatra/content_for'
require 'cgi'
Samurai.options = {
:site => 'https://api.samurai.feefighters.com/v1/',
:merchant_key => 'a1ebafb6da5238fb8a3ac9f6',
:merchant_password => 'ae1aa640f6b735c4730fbb56',
:processor_token => '5a0e1ca1e5a11a2997bbf912'
}
include Samurai::Rails::Helpers
# =============================================================================
get '/' do
erb :index
end
# Payment form for Samurai.js
# ------------------------------
#
# * displays a drop-in payment form from Samurai.js, no extra logic required
get '/samurai_js/payment_form' do
erb :'samurai_js/payment_form'
end
# Purchase action for Samurai.js
# ------------------------------
#
# * payment_method_token is POST'd via AJAX
# * Responds with a JSON transaction object
#
post '/samurai_js/purchase', :provides => 'json' do
@transaction = Samurai::Processor.the_processor.purchase(
params[:payment_method_token],
122.00, # The price for the Samurai.js Katana Sword
{
:descriptor => 'Samurai.js Katana Sword',
:customer_reference => Time.now.to_f,
:billing_reference => Time.now.to_f
}
)
@transaction.to_json
end
# =============================================================================
# Payment form for Transparent Redirect
# ------------------------------
#
# * Displays a payment form using the Samurai Rails helpers bundled in the gem
# * Payment form is initialized with PaymentMethod data, if a token is passed in the params.
# This allows validation & processor-response error messages to be displayed.
#
get '/transparent_redirect/payment_form' do
setup_for_transparent_redirect(params)
erb :'transparent_redirect/payment_form'
end
# Purchase action for Transparent Redirect
# ------------------------------
#
# * This action is requested as the callback from the Samurai.js Transparent Redirect
# * It performs the purchase, and redirects the user to the purchase confirmation page
# * On error, it redirects back to the payment form to display validation/card errors
#
get '/transparent_redirect/purchase' do
load_and_verify_payment_method(params)
unless @payment_method
redirect to('/transparent_redirect/payment_form?payment_method_token='+payment_method_params[:payment_method_token])
end
@transaction = Samurai::Processor.the_processor.purchase(
@payment_method.token,
122.00, # The price for the Transparent Redirect Nunchucks
{
:descriptor => 'Transparent Redirect Nunchucks',
:customer_reference => Time.now.to_f,
:billing_reference => Time.now.to_f
}
)
if @transaction.failed?
redirect to('/transparent_redirect/payment_form?payment_method_token='+payment_method_params[:payment_method_token])
end
redirect to('/transparent_redirect/receipt')
end
# =============================================================================
# Payment form for Server-to-Server API
# -------------------------------------
#
# * Displays a payment form that POSTs to the purchase method below
# * The credit card data is provided directly to this rails server, where it is used to process a
# transaction entirely on the backend.
# * A payment_method_token or reference_id can be provided in the params so that validation errors can be displayed.
#
get '/server_to_server/payment_form' do
unless params[:payment_method_token].nil?
@payment_method = Samurai::PaymentMethod.find params[:payment_method_token]
else
@payment_method = Samurai::PaymentMethod.new :is_sensitive_data_valid => false
end
unless params[:reference_id].nil?
@transaction = Samurai::Transaction.find params[:reference_id]
end
erb :'server_to_server/payment_form'
end
# Purchase action for Server-to-Server API
# ----------------------------------------
#
# * Payment Method details are POST'ed directly to the server, which performs a S2S API call
# * NOTE: This approach is typically not recommended, as it comes with a much greater PCI compliance burden
# In general, it is a good idea to prevent the credit card details from ever touching your server.
#
post '/server_to_server/purchase' do
@payment_method = Samurai::PaymentMethod.create params[:payment_method]
if @payment_method.nil?
redirect to('/server_to_server/payment_form')
end
@transaction = Samurai::Processor.the_processor.purchase(
@payment_method.token,
122.00, # The price for the Server-to-Server Battle Axe + Shipping
{
:descriptor => 'Server-to-Server Battle Axe',
:customer_reference => Time.now.to_f,
:billing_reference => Time.now.to_f
}
)
unless @transaction.success
query = { :payment_method_token => @payment_method.token,
:reference_id => @transaction.reference_id
}.map { |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join('&')
redirect to('/server_to_server/payment_form?'+query)
end
redirect to('/server_to_server/receipt')
end
# Purchase confirmation & receipt page
# ------------------------------------
get %r{/(samurai_js|transparent_redirect|server_to_server)/receipt} do |t|
erb :"#{t}/receipt"
end