-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
147 lines (110 loc) · 4.81 KB
/
index.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
<?
// comment this out to allow http:
if($_SERVER['SERVER_PORT'] == 80){
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
die();
}
$key_publishable = '';
$key_secret = '';
$note_prefix = 'Terminal'; //this will be used in the charge's description
$title = 'Terminal';
$currency = 'usd';
$currency_symbol = '$';
$demo_mode = false;
if(!$key_publishable || !$key_secret) die('Please set stripe API keys');
if($_POST){
require_once 'stripe-php/lib/Stripe.php';
$note_parts = array();
if($note_prefix) $note_parts[] = $note_prefix;
if($_POST['note']) $note_parts[] = $_POST['note'];
$params = array(
'amount' => $_POST['amount'],
'currency' => $currency,
'card' => $_POST['token'],
'description' => implode(' - ', $note_parts)
);
$response = array(
'success' => false
);
try{
Stripe::setApiKey($key_secret);
$charge = Stripe_Charge::create($params);
$response['success'] = true;
$response['id'] = $charge->id;
$response['amount'] = number_format($charge->amount / 100, 2);
$response['fee'] = number_format($charge->fee / 100, 2);
$response['card_type'] = $charge->card->type;
$response['card_last4'] = $charge->card->last4;
}catch (Exception $e) {
$response['error'] = $e->getMessage();
}
echo json_encode($response);
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?= $title ?></title>
<link rel="stylesheet" type="text/css" media="screen" href="layout.css?<?= filemtime('layout.css') ?>" />
<meta name="viewport" content="width=480px" />
<meta name="viewport" content="user-scalable=no" />
</head>
<body>
<div id="wrap">
<h2><?= $title ?></h2>
<? if($demo_mode){ ?>
<div id="demo_warning">
This is a <b>DEMO</b>. Please don't enter real payment info. You can use <b>4242424242424242</b> as a valid card number. <a href="https://stripe.com/docs/testing">Stripe Testing FAQ</a>
</div>
<? } ?>
<form action="" method="POST" id="payment_form">
<label>Amount</label>
<div class="form_input">
<input id="input_amount" type="text" placeholder="Ex: <?=$currency_symbol?>19.99" />
</div>
<div class="clear"></div>
<div id="error_amount" class="error_wrapper"></div>
<label>Card Number</label>
<div class="form_input">
<input id="input_number" type="text" pattern="[0-9]*"/>
</div>
<div class="clear"></div>
<div id="error_number" class="error_wrapper"></div>
<label>CVC</label>
<div class="form_input">
<input id="input_cvc" type="text" pattern="[0-9]*" />
</div>
<div class="clear"></div>
<div id="error_cvc" class="error_wrapper"></div>
<label>Expiration</label>
<div class="form_input">
<input id="input_exp_month" type="text" pattern="[0-9]*" placeholder="MM" />
<span> / </span>
<input id="input_exp_year" type="text" pattern="[0-9]*" placeholder="YYYY" />
</div>
<div class="clear"></div>
<div id="error_exp_month" class="error_wrapper"></div>
<div id="error_exp_year" class="error_wrapper"></div>
<label>Note</label>
<div class="form_input">
<input id="input_note" type="text" placeholder="A short note" />
</div>
<div class="clear"></div>
<div id="error_note" class="error_wrapper"></div>
<div id="transaction_error"></div>
<div class="left" id="progress_message"></div>
<div class="right" style="margin-top: 10px;">
<button id="submit_button" type="submit">Submit</button>
</div>
<div class="clear"></div>
</form>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript" src="form.js?<?= filemtime('form.js') ?>"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<?= $key_publishable ?>');
</script>
</body>
</html>