forked from aladrach/craft-apple-pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple-pay-2.html
241 lines (167 loc) · 7.03 KB
/
apple-pay-2.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
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
{# {% if currentUser %}
{% redirect 'checkout/addresses' %}
{% endif %}
#}
{% extends '_layouts/checkout' %}
{% block content %}
<div class="grid-container new-section-break">
<div class="grid-x grid-padding-x">
<div class="medium-6 cell">
<li class="CartCheckout-applePay" id="apple-pay-option" style="display: none;">
<h5 class="CartCheckout-title">
<div id="apple-pay-header"></div>
</h5>
<style>
#apple-pay-button {
display: block;
background-color: black;
background-image: -webkit-named-image(apple-pay-logo-white);
background-size: 100% 100%;
background-origin: content-box;
background-repeat: no-repeat;
width: 100%;
height: 44px;
padding: 10px 0;
border-radius: 10px;
}
</style>
<style>
#apple-pay-header {
display: block;
background-color: transparent;
background-image: -webkit-named-image(apple-pay-logo-black);
background-size: 100% 100%;
background-origin: content-box;
background-repeat: no-repeat;
width: 60px;
height: 44px;
padding: 10px 0;
border-radius: 10px;
}
</style>
{% set statesByAbbreviation = {} %}
{% set statesByName = [] %}
{% for state in craft.commerce.states.allStates if state.countryId == 233 %}
{% set statesByAbbreviation = statesByAbbreviation|merge({(state.abbreviation):(state.id) }) %}
{% set statesByName = statesByName|merge({(state.name):(state.id) }) %}
{% endfor %}
<div class="CartCheckout-details">
<form method="POST" id="ApplePayCheckout--update">
<input type="text" name="action" value="commerce/cart/update-cart"/>
<input type="text" name="orderNumber" value="{{ cart.number }}"/>
<input type="text" name="billingAddressSameAsShipping" value="1">
<input type="text" name="gatewayId" value="4">
{{ csrfInput() }}
</form>
<form method="POST" id="ApplePayCheckout--checkout">
<input type="text" name="action" value="commerce/payments/pay"/>
<input type="hidden" name="email" value="{{ cart.email }}"/>
<input type="text" name="orderNumber" value="{{ cart.number }}"/>
<input type="text" name="gatewayId" value="4">
{{ redirectInput('/checkout/success?number='~cart.number) }}
{{ csrfInput() }}
<a id="apple-pay-button"></a>
</form>
</div>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
{% set publishableKey = '' %}
{% for method in craft.commerce.gateways.allCustomerEnabledGateways %}
{# Replace 'method.id' value with whatever the ID of your Apple Pay/Stripe method is. This can be simplified if you only have one payment method. #}
{% if method.id == 4 %}
{% set publishableKey = method.settings.publishableKey %}
{% endif %}
{% endfor %}
<script>
Stripe.setPublishableKey('{{publishableKey}}');
Stripe.applePay.checkAvailability(function(available) {
if (available) {
document.getElementById('apple-pay-option').style.display = 'block';
}
});
document.getElementById('apple-pay-button').addEventListener('click', beginApplePay);
function beginApplePay() {
var paymentRequest = {
countryCode: 'US',
currencyCode: 'USD',
// Set your required contact fields here to be passed back to Craft.
requiredShippingContactFields: ["postalAddress","name","phone","email"],
total: {
label: '{{ siteName }}',
amount: '{{cart.totalPrice|round(2)}}'
}
};
var session = Stripe.applePay.buildSession(paymentRequest,
function(result, completion) {
var statesByName = {{ statesByName|json_encode|raw}}
var statesByAbbr = {{ statesByAbbreviation|json_encode|raw}}
completion(ApplePaySession.STATUS_SUCCESS);
var $form = $('#ApplePayCheckout--checkout');
var $form2 = $('#ApplePayCheckout--update');
// Map result fields to Craft fields
var theState = result.shippingContact.administrativeArea;
if (theState.length == 2) {
var stateId = statesByAbbr[theState];
} else {
var stateId = statesByName[theState];
}
$('#ApplePayCheckout--update input').each(function() { $(this).attr('type','text'); });
$form2.append($('<input type="text" name="email" />').val(result.shippingContact.emailAddress));
$form2.append($('<input type="text" name="shippingAddress[firstName]" />').val(result.shippingContact.givenName));
$form2.append($('<input type="text" name="shippingAddress[lastName]" />').val(result.shippingContact.familyName));
$form2.append($('<input type="text" name="shippingAddress[address1]" />').val(result.shippingContact.addressLines[0]));
$form2.append($('<input type="text" name="shippingAddress[address2]" />').val(result.shippingContact.addressLines[1]));
$form2.append($('<input type="text" name="shippingAddress[city]" />').val(result.shippingContact.locality));
$form2.append($('<input type="text" name="shippingAddress[stateValue]" />').val(stateId));
$form2.append($('<input type="text" name="shippingAddress[zipCode]" />').val(result.shippingContact.postalCode));
$form2.append($('<input type="text" name="shippingAddress[phone]" />').val(result.shippingContact.phoneNumber));
$form2.append($('<input type="text" name="shippingAddress[countryId]" />').val(233));
$form.append($('<input type="text" name="stripeToken" />').val(result.token.id));
// Submit update cart to add shipping info, then pay form to submit payment info
// $.post('/commerce/cart/update-cart', );
$.ajax({
type: "POST",
dataType: 'json',
headers: {
"X-CSRF-Token" : '{{ craft.app.request.csrfToken }}',
},
url: '/commerce/cart/update-cart',
data: $('#ApplePayCheckout--update').serializeArray(),
success: function(data){
$.ajax({
url: '',
data: {
'{{ craft.config.csrfTokenName|e('js') }}': '{{ craft.request.csrfToken|e('js') }}',
'action': 'commerce/cart/get-cart'
},
success: function(data) {
console.log(data);
var $form = $('#ApplePayCheckout--checkout');
$form.append('<input type="submit" value="Submit" />');
},
dataType: 'json'
});
},
fail: function(data) {
console.log('fail');
console.log(data);
}
});
// $form.get(0).submit();
//$form.get(0).submit();
}, function(error) {
// Return error message to cart page.
console.log(error);
console.log(error.message);
//window.location = "/cart?error=" + error.message;
});
session.oncancel = function() {
//console.log("User hit the cancel button in the payment window");
};
session.begin();
}
</script>
</li>
<hr>
</div>
</div>
{% endblock %}