This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
forked from spreedly/sample-payment-frame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
189 lines (165 loc) · 6.96 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Spreedly Sample iFrame Payment Page</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<script src="https://core.spreedly.com/iframe/iframe-v1.min.js"></script>
</head>
<body>
<form id="payment-form" accept-charset="UTF-8" class="spf-form" method="POST"
action="#" onsubmit="submitPaymentForm(); return false;">
<input type="hidden" name="payment_method_token" id="payment_method_token" value="" />
<fieldset class="spf-fs-name">
<h2>Name</h2>
<div class="spf-field spf-field-fname">
<label class="internal" for="spf-fname">First Name</label>
<input type="text" class="spf-input-text" id="first_name">
</div>
<div class="spf-field spf-field-lname">
<label class="internal" for="spf-lname">Last Name</label>
<input type="text" class="spf-input-text" id="last_name">
</div>
</fieldset>
<!-- If you want billing address, uncomment -->
<!--
<fieldset class="spf-fs-address">
<h2>Billing Address</h2>
<div class="spf-field spf-field-street">
<label class="internal" for="spf-street">Street Address</label>
<input type="text" class="spf-input-text" id="address1">
</div>
<div class="spf-field spf-field-street-2">
<input type="text" class="spf-input-text" id="address2">
</div>
<div class="spf-field spf-field-city">
<label class="internal" for="spf-city">City</label>
<input type="text" class="spf-input-text" id="city">
</div>
<div class="spf-field spf-field-state">
<label class="internal" for="spf-state">State</label>
<input type="text" class="spf-input-text" id="state">
</div>
<div class="spf-field spf-field-postcode">
<label class="internal" for="spf-postcode">Zip Code</label>
<input type="text" class="spf-input-text" id="zip">
</div>
<div class="spf-field spf-field-country">
<label class="internal" for="spf-country">Country</label>
<input type="text" class="spf-input-text" id="country">
</div>
</fieldset>
-->
<fieldset class="spf-fs-cc">
<h2>Payment Details</h2>
<div class="spf-field">
<label class="spf-field-group spf-number">Credit Card Number</label>
<label class="spf-field-group spf-verification_value">CVV</label>
<div id="spreedly-number-test" class="spf-field-group spf-number spf-field-cc">
</div>
<div id="spreedly-cvv-test" class="spf-field-group spf-verification_value spf-field-cc">
</div>
</div>
<div class="spf-field spf-field-exp">
<label>Expiration Date</label>
<div class="spf-field-group spf-month">
<input type="text" class="spf-input-text spf-exp" id="month" size="3" maxlength="2" placeholder="MM">
<!-- <label class="spf-label-secondary" for="spf-exp-m">Month (MM)</label> -->
</div>
<span class="spf-exp-divider">/</span>
<div class="spf-field-group spf-year">
<input type="text" class="spf-input-text spf-exp" id="year" size="5" maxlength="4" placeholder="YYYY">
<!-- <label class="spf-label-secondary" for="spf-exp-y">Year (YYYY)</label> -->
</div>
</div>
</fieldset>
<fieldset class="spf-field-submit">
<input type="submit" class="button" value="Submit Payment">
<div id="message"></div>
<div id="errors"></div>
</fieldset>
<script
id="spreedly-iframe"
data-environment-key="TbWAYgG7qaagGBh7hPjXQxS4DM4"
data-number-id="spreedly-number-test"
data-cvv-id="spreedly-cvv-test">
</script>
</form>
<script>
Spreedly.init();
Spreedly.on('paymentMethod', function(token, pmData) {
var tokenField = document.getElementById("payment_method_token");
tokenField.setAttribute("value", token);
var masterForm = document.getElementById('payment-form');
// Normally would now submit the form..
// masterForm.submit();
// For demonstration purposes just display the token
var messageEl = document.getElementById('message');
messageEl.innerHTML = "Success! The returned payment method token is: " + token;
});
Spreedly.on('errors', function(errors) {
var messageEl = document.getElementById('errors');
var errorBorder = "1px solid red";
for(var i = 0; i < errors.length; i++) {
var error = errors[i];
if(error["attribute"]) {
var masterFormElement = document.getElementById(error["attribute"]);
if(masterFormElement) {
masterFormElement.style.border = errorBorder
} else {
Spreedly.setStyle(error["attribute"], "border: " + errorBorder + ";");
}
}
messageEl.innerHTML += error["message"] + "<br/>";
}
});
Spreedly.on('ready', function(frame) {
Spreedly.setFieldType('number', 'text');
Spreedly.setFieldType('cvv', 'text');
Spreedly.setNumberFormat('maskedFormat');
Spreedly.setStyle('number','width: 67%; border-radius: 3px; border: 1px solid #ccc; padding: .65em .5em; font-size: 91%;');
Spreedly.setStyle('cvv', 'width: 30%; border-radius: 3px; border: 1px solid #ccc; padding: .65em .5em; font-size: 91%;');
});
Spreedly.on('fieldEvent', function(name, event, activeElement, inputData) {
if (event == 'input') {
if (inputData["validCvv"]){
Spreedly.setStyle('cvv', "background-color: #CDFFE6;")
} else {
Spreedly.setStyle('cvv', "background-color: #FFFFFF;")
}
if (inputData["validNumber"]){
Spreedly.setStyle('number', "background-color: #CDFFE6;")
} else {
Spreedly.setStyle('number', "background-color: #FFFFFF;")
}
}
});
function submitPaymentForm() {
var normalBorder = "1px solid #ccc";
// These are the fields whose values we want to transfer *from* the
// master form *to* the payment frame form. Add the following if
// you're displaying the address:
// ['address1', 'address2', 'city', 'state', 'zip', 'country']
var paymentMethodFields = ['first_name', 'last_name', 'month', 'year']
options = {};
for(var i = 0; i < paymentMethodFields.length; i++) {
var field = paymentMethodFields[i];
// Reset existing styles (to clear previous errors)
var fieldEl = document.getElementById(field);
fieldEl.style.border = normalBorder;
// add value to options
options[field] = fieldEl.value
}
// Reset frame styles
Spreedly.setStyle('number', "border: " + normalBorder + ";");
Spreedly.setStyle('cvv', "border: " + normalBorder + ";");
// Reset previous messages
document.getElementById('errors').innerHTML = "";
document.getElementById('message').innerHTML = "";
// Tokenize!
Spreedly.tokenizeCreditCard(options);
}
</script>
</body>
</html>