-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmystripe.php
385 lines (313 loc) · 11.2 KB
/
mystripe.php
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
require_once('lib/respond.php');
require_once('lib/p2k12.php');
require_once('lib/stripe-php/lib/Stripe.php');
setlocale (LC_ALL, 'nb_NO.UTF-8');
date_default_timezone_set ('Europe/Oslo');
require_once('db-connect-string.php');
if (false === pg_connect($db_connect_string))
respond('500', 'PostgreSQL connect failed');
Stripe::setApiKey($stripe_apikey);
$membership_infos_res = pg_query("SELECT name, price FROM membership_infos WHERE recurrence = '1 month'::INTERVAL ORDER BY name = 'aktiv' DESC, price DESC");
$membership_types = array();
while ($row = pg_fetch_assoc($membership_infos_res))
$membership_types[$row['name']] = $row['price'];
if (!isset($_GET['id']) || !isset($_GET['signature']))
respond('404', 'Not found');
$account = $_GET['id'];
$provided_signature = $_GET['signature'];
$correct_signature = hash_for_account($account);
if ($provided_signature != $correct_signature)
respond('404', 'Not found');
// Get membership details
if (false === ($member_res = pg_query_params("SELECT price, recurrence, full_name, email, organization, flag, name AS username FROM active_members, accounts WHERE account = $1 AND active_members.account = accounts.id", array($account))))
respond('500', 'PostgreSQL query error');
if (!pg_num_rows($member_res))
respond('404', 'Member does not exist');
$member = pg_fetch_assoc($member_res);
// Update credit card
if (isset($_POST['stripeToken'])) {
// Get stripe customer and set new card
if (false === ($member_res = pg_query_params("SELECT account, id FROM stripe_customer WHERE account = $1", array($account))))
respond('500', 'PostgreSQL query error');
if (pg_num_rows($member_res))
{
$stripe_customer = pg_fetch_assoc($member_res);
try {
$customer = Stripe_Customer::retrieve($stripe_customer['id']);
if (count($customer->cards->data) > 0)
{
// Delete existing card
$cardid = $customer->cards->data[0]->id;
$customer->cards->retrieve($cardid)->delete();
}
$customer->cards->create(array("card" => $_POST['stripeToken']));
} catch (Stripe_Error $e)
{
respond('404', 'Error updating stripe customer: ' . $e->getMessage());
}
}
else // Create new Stripe customer
{
$status = pg_query("BEGIN");
$plan = "medlem" . $member['price'];
try {
$customer = Stripe_Customer::create(array(
"card" => $_POST['stripeToken'],
"plan" => $plan,
"email" => $member['email']));
$status = pg_query_params("INSERT INTO stripe_customer (account, id) VALUES ($1, $2)", array($account, $customer->id));
} catch (Stripe_Error $e) {
respond('500', 'Error creating stripe customer: ' . $e->getMessage());
}
if ($status !== false)
{
$status = pg_query_params("INSERT INTO members (full_name, email, price, account) VALUES ($1, $2, $3, $4)", array($member['full_name'], $member['email'], $member['price'], $account));
}
if ($status !== false)
pg_query("COMMIT");
else {
pg_query("ROLLBACK");
if (isset($customer))
$customer->delete();
syslog(LOG_ERR, pg_last_error());
respond('500', 'PostgreSQL query error while creating Stripe customer');
}
}
}
// Endre medlemstype
if (isset($_POST['membership_price']))
{
$price = $_POST['membership_price'];
$plan = "medlem" . $price;
// Get stripe customer
if (false === ($member_res = pg_query_params("SELECT account, id FROM stripe_customer WHERE account = $1", array($account))))
respond('500', 'PostgreSQL query error');
// Oppdater stripe info hvis det brukes.
if (pg_num_rows($member_res))
{
$stripe_customer = pg_fetch_assoc($member_res);
try {
$customer = Stripe_Customer::retrieve($stripe_customer['id']);
// Create subscription if it does not exist
if (count($customer->subscriptions->data) == 0 && $price != 0) {
$customer->subscriptions->create(array('plan' => $plan));
}
else
{
$subscription = $customer->subscriptions->data[0];
if ($price == 0)
{
// Delete subscription if price==0
$subscription->cancel(array('at_period_end' => true));
}
else {
// Update subscription
$subscription->plan = $plan;
$subscription->save();
}
}
} catch (Stripe_Error $e) {
respond('500', 'Error communicating with stripe: ' . $e->getMessage());
}
}
// Update p2k12
if (false === pg_query_params("INSERT INTO members (full_name, email, price, account) VALUES ($1, $2, $3, $4)", array($member['full_name'], $member['email'], $price, $account)))
respond('500', 'PostgreSQL query error');
respond_303($_SERVER['REQUEST_URI']);
}
// Set some defaults
$invoices = array();
$card_expire = '';
$card_num = 'N/A';
// Get Stripe details
if (false === ($member_res = pg_query_params("SELECT account, id FROM stripe_customer WHERE account = $1", array($account))))
respond('500', 'PostgreSQL query error');
if (pg_num_rows($member_res))
{
$stripe_customer = pg_fetch_assoc($member_res);
$customer = Stripe_Customer::retrieve($stripe_customer['id']);
if (count($customer->cards->data) > 0)
{
$card_num = '**** **** **** ' . $customer->cards->data[0]->last4;
$card_expire = $customer->cards->data[0]->exp_month . '/' . $customer->cards->data[0]->exp_year;
}
else
{
$card_num = 'No card set';
$card_expire = '00/00';
}
if (false === ($res = pg_query_params("SELECT * FROM stripe_payment WHERE account = $1 ORDER BY paid_date DESC", array($account))))
respond('500', 'PostgreSQL query error');
$invoices = array();
while ($row = pg_fetch_assoc($res))
$invoices[] = $row;
}
function html($s)
{
return htmlentities($s, ENT_QUOTES, 'utf-8');
}
?>
<html>
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-WP3CM2M');</script>
<!-- End Google Tag Manager -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
</style>
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WP3CM2M"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div class="container">
<div class="page-header">
<h1>
<img src="small-logo.png" style="margin-right: 20px">Mitt Bitraf-medlemskap</h1>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Medlemskap</h3>
</div>
<div class="panel-body">
<table class="table">
<tr>
<th>Navn:</th>
<td><?=htmlentities($member['full_name'], ENT_QUOTES, 'utf-8')?></td>
</tr>
<tr>
<th>Brukernavn:</th>
<td><?=htmlentities($member['username'], ENT_QUOTES, 'utf-8')?></td>
</tr>
<tr>
<th>Organisasjon:</th>
<td><?=htmlentities($member['organization'], ENT_QUOTES, 'utf-8')?></td>
</tr>
<tr>
<th>E-post:</th>
<td><?=htmlentities($member['email'], ENT_QUOTES, 'utf-8')?></td>
</tr>
<tr>
<th>Medlemspris:</th>
<td><?=htmlentities($member['price'], ENT_QUOTES, 'utf-8')?></td>
</tr>
<tr>
<th>Betalingsmåte:</th>
<td><? if (isset($customer)) print 'Stripe (kredittkort)'; else print 'Bankoverføring';?> </td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Betaling</h3>
</div>
<div class="panel-body">
<table class="table">
<tr>
<th>Kredittkort:</th>
<td> <?=$card_num?> </td>
</tr>
<tr>
<th>Utløpsdato:</th>
<td> <?=$card_expire?> </td>
</tr>
</table>
<div class="row">
<div class="col-md-6">
<form method='post' action='<?=html($_SERVER['REQUEST_URI'])?>'>
<button type="button" class="btn btn-success" id="updateStripe">Oppdater kredittkort</a>
<script>
$('#updateStripe').click(function(){
var token = function(res){
var $input = $('<input type=hidden name=stripeToken />').val(res.id);
$('form').append($input).submit();
};
StripeCheckout.open({
key: '<?=$stripe_pubkey?>',
panelLabel: 'Endre kredittkort',
name: 'Bitraf medlemskap',
token: token
});
return false;
});
</script>
</form>
</div>
<div class="col-md-6">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#endreMedlemskap">Endre Medlemskap</a>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="endreMedlemskap" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form method='post' action='<?=html($_SERVER['REQUEST_URI'])?>'>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Endre medlemsskapstype</h4>
</div>
<div class="modal-body">
<p>Medlemskap</p>
<select class="form-control" name="membership_price">
<? foreach ($membership_types as $membership_type => $price): ?>
<option value="<?=$price?>" <? if ($price===$member['price']) print 'selected'?>><?=html($membership_type)?> (<?=$price?> kr / mnd) </option>
<? endforeach ?>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Lukk</button>
<button type="submit" class="btn btn-primary">Lagre</button>
</div>
</form>
</div>
</div>
</div>
<p>Kredittkortdetaljer blir aldri overført eller lagret av Bitraf og håndteres kun av <a href='http://stripe.com'>stripe.com</a>.</p>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Medlemsperioder</h3>
</div>
<div class="panel-body" style="width: 50%;">
<table class="table">
<tr>
<th>Fra</th>
<th>Til</th>
<th>Beløp</th>
<th>Betalt</th>
</tr>
<?
foreach($invoices as $inv)
{
print "<tr>";
print "<td>" . $inv['start_date'] . "</td>";
print "<td>" . $inv['end_date'] . "</td>";
print "<td>" . $inv['price'] . "</td>";
print "<td>" . $inv['paid_date'] . "</td>";
print "</tr>";
}
?>
</table>
</div>
</div>
</div>
</body>
</html>