-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.rb
215 lines (177 loc) · 6.7 KB
/
app.rb
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'sinatra'
require 'killbill_client'
set :kb_url, ENV['KB_URL'] || 'http://127.0.0.1:8080'
set :publishable_key, ENV['PUBLISHABLE_KEY']
set :admin_password, ENV['ADMIN_PWD'] || 'password'
#
# Kill Bill configuration and helpers
#
KillBillClient.url = settings.kb_url
KillBillClient.disable_ssl_verification = true
# Multi-tenancy and RBAC credentials
options = {
:username => 'admin',
:password => settings.admin_password,
:api_key => 'bob',
:api_secret => 'lazar'
}
# Audit log data
user = 'demo'
reason = 'New subscription'
comment = 'Trigger by Sinatra'
def create_kb_account(user, reason, comment, options)
account = KillBillClient::Model::Account.new
account.name = 'John Doe'
account.currency = 'USD'
account.create(user, reason, comment, options)
end
def create_kb_payment_method(account, session_id, token, user, reason, comment, options)
pm = KillBillClient::Model::PaymentMethod.new
pm.account_id = account.account_id
pm.plugin_name = 'killbill-stripe'
prop = KillBillClient::Model::PluginPropertyAttributes.new
if token.nil?
prop.key = 'sessionId'
prop.value = session_id
else
prop.key = 'token'
prop.value = token
end
options[:pluginProperty] = [prop]
pm.create(true, user, reason, comment, options)
end
def create_subscription(account, user, reason, comment, options)
subscription = KillBillClient::Model::Subscription.new
subscription.account_id = account.account_id
subscription.product_name = 'Sports'
subscription.product_category = 'BASE'
subscription.billing_period = 'MONTHLY'
subscription.price_list = 'DEFAULT'
subscription.price_overrides = []
# For the demo to be interesting, override the trial price to be non-zero so we trigger a charge in Stripe
override_trial = KillBillClient::Model::PhasePriceAttributes.new
override_trial.phase_type = 'TRIAL'
override_trial.fixed_price = 10.0
subscription.price_overrides << override_trial
subscription.create(user, reason, comment, nil, true, options.clone.merge( { :params => { :callCompletion => true, :callTimeoutSec => 20 } }))
end
def create_session(account, options)
# Magic template, see https://stripe.com/docs/payments/checkout/fulfillment#webhooks
success_url = 'http://localhost:4567/charge?kbAccountId=' + @account.account_id + '&sessionId={CHECKOUT_SESSION_ID}'
response = KillBillClient::API.post '/plugins/killbill-stripe/checkout', nil, { :kbAccountId => account.account_id, :successUrl => success_url }, options
JSON.parse(response.body)['formFields'].find {|x| x['key'] == 'id'}['value']
end
def charge(account_id, session_id, token, user, reason, comment, options)
account = KillBillClient::Model::Account.find_by_id(account_id, true, true, options)
# Add a payment method associated with the Stripe token
create_kb_payment_method(account, session_id, token, user, reason, comment, options)
# Add a subscription
create_subscription(account, user, reason, comment, options)
# Retrieve the invoice
invoice = account.invoices(options).first
# And the Stripe authorization
transaction = invoice.payments(true, false, 'NONE', options).first.transactions.first
authorization = (transaction.properties.find { |p| p.key == 'id' }).value
[invoice, authorization]
end
#
# Sinatra handlers
#
get '/' do
erb :index
end
post '/checkout' do
# Create an account
@account = create_kb_account(user, reason, comment, options)
# Create the Stripe session
@session_id = create_session(@account, options)
erb :checkout
end
post '/elements' do
# Create an account
account = create_kb_account(user, reason, comment, options)
@invoice, @authorization = charge(account.account_id, nil, params[:stripeToken], user, reason, comment, options)
erb :charge
end
get '/charge' do
@invoice, @authorization = charge(params[:kbAccountId], params[:sessionId], nil, user, reason, comment, options)
erb :charge
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>
@@index
<span class="image"><img src="https://drive.google.com/uc?&id=0Bw8rymjWckBHT3dKd0U3a1RfcUE&w=960&h=480" alt="uc?&id=0Bw8rymjWckBHT3dKd0U3a1RfcUE&w=960&h=480"></span>
<form id="checkout" action="/checkout" method="post">
<article>
<label class="amount">
<span>Sports car, 30 days trial for only $10.00!</span>
</label>
</article>
</form>
<button type="submit" form="checkout" value="Submit">Buy via Stripe Checkout</button>
<hr/>
<script src="https://js.stripe.com/v3/"></script>
<form action="/elements" method="post" id="payment-form">
<div class="form-row">
<label for="card-element">Credit or debit card</label>
<div id="card-element"></div>
<div id="card-errors" role="alert"></div>
</div>
<button>Buy via Stripe Elements</button>
</form>
<script>
var stripe = Stripe('<%= settings.publishable_key %>');
var elements = stripe.elements();
var card = elements.create('card');
card.mount('#card-element');
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
</script>
@@checkout
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('<%= settings.publishable_key %>');
stripe.redirectToCheckout({
sessionId: '<%= @session_id %>'
}).then(function (result) {
alert(result.error.message);
});
</script>
@@charge
<h2>Thanks! Here is your invoice:</h2>
<ul>
<% @invoice.items.each do |item| %>
<li><%= "subscription_id=#{item.subscription_id}, amount=#{item.amount}, phase=sports-monthly-trial, start_date=#{item.start_date}" %></li>
<% end %>
</ul>
You can verify the payment at <a href="<%= "https://dashboard.stripe.com/test/payments/#{@authorization}" %>"><%= "https://dashboard.stripe.com/test/payments/#{@authorization}" %></a>.