-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d_secure_guest.html
418 lines (393 loc) · 11.3 KB
/
3d_secure_guest.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.1.js" crossorigin="anonymous"></script>
<script src="https://js.stripe.com/v3/"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<style type="text/css">
textarea {
width: 600px;
height: 200px;
}
input[type="text"] {
width: 400px;
}
button {
margin-top: 1em;
}
</style>
</head>
<body id="app" class="container">
<h1>3D Secure example for guest customers</h1>
<p class="alert alert-warning" role="alert">NOTE: Place this file inside your Magento pub/ directory to avoid CORS restrictions.</p>
<h2 class="mt-4">Configuration</h2>
<div class="col-md-6 mb-3">
<label for="base_url">Base Magento URL</label>
<input class="form-control" type="text" id="base_url" name="base_url" value="" onchange="save('base_url')">
</div>
<div class="col-md-6 mb-3">
<label>Product SKU to purchase</label>
<input class="form-control" type="text" id="sku" value="24-MB02" onchange="save('sku')">
</div>
<div class="col-md-6 mb-3">
<label>Stripe Publishable API Key</label>
<input class="form-control" type="text" id="pk" value="pk_test_xyz" onchange="save('pk')">
</div>
<h2 class="mt-4">Step 1: Create a guest cart</h2>
<textarea class="form-control" id="create_guest_cart_data">
mutation {
createEmptyCart
}
</textarea>
<button onclick="createGuestCart()" class="btn btn-primary">POST</button>
<pre id="create_guest_cart_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 2: Add product to cart</h2>
<textarea class="form-control" id="add_product_data">
mutation {
addSimpleProductsToCart(
input: {
cart_id: "CART_ID"
cart_items: [
{
data: {
quantity: 1
sku: "SIMPLE_PRODUCT_SKU"
}
}
]
}
) {
cart {
items {
id
product {
sku
stock_status
}
quantity
}
}
}
}
</textarea>
<button onclick="post('add_product')" class="btn btn-primary">POST</button>
<pre id="add_product_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 3: Set guest email address</h2>
<textarea class="form-control" id="set_guest_email_data">
mutation {
setGuestEmailOnCart(input: {
cart_id: "CART_ID"
email: "[email protected]"
}) {
cart {
email
}
}
}
</textarea>
<button onclick="post('set_guest_email')" class="btn btn-primary">POST</button>
<pre id="set_guest_email_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 4: Set shipping address</h2>
<textarea class="form-control" id="set_shipping_data">
mutation {
setShippingAddressesOnCart(
input: {
cart_id: "CART_ID"
shipping_addresses: [
{
address: {
firstname: "John"
lastname: "Doe"
company: "Company Name"
street: ["3320 N Crescent Dr", "Beverly Hills"]
city: "Los Angeles"
region: "CA"
region_id: 12
postcode: "90210"
country_code: "US"
telephone: "123-456-0000"
save_in_address_book: false
}
}
]
}
) {
cart {
shipping_addresses {
firstname
lastname
company
street
city
region {
code
label
}
postcode
telephone
country {
code
label
}
available_shipping_methods{
carrier_code
carrier_title
method_code
method_title
}
}
}
}
}
</textarea>
<button onclick="post('set_shipping')" class="btn btn-primary">POST</button>
<pre id="set_shipping_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 5: Set billing address</h2>
<textarea class="form-control" id="set_billing_data">
mutation {
setBillingAddressOnCart(
input: {
cart_id: "CART_ID"
billing_address: {
address: {
firstname: "John"
lastname: "Doe"
company: "Company Name"
street: ["64 Strawberry Dr", "Beverly Hills"]
city: "Los Angeles"
region: "CA"
region_id: 12
postcode: "90210"
country_code: "US"
telephone: "123-456-0000"
save_in_address_book: true
}
}
}
) {
cart {
billing_address {
firstname
lastname
company
street
city
region{
code
label
}
postcode
telephone
country {
code
label
}
}
}
}
}
</textarea>
<button onclick="post('set_billing')" class="btn btn-primary">POST</button>
<pre id="set_billing_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 6: Set shipping method</h2>
<textarea class="form-control" id="set_shipping_method_data">
mutation {
setShippingMethodsOnCart(input: {
cart_id: "CART_ID"
shipping_methods: [
{
carrier_code: "flatrate"
method_code: "flatrate"
}
]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
carrier_title
method_title
}
}
}
}
}
</textarea>
<button onclick="post('set_shipping_method')" class="btn btn-primary">POST</button>
<pre id="set_shipping_method_response" class="alert alert-light"></pre>
<h2 class="mt-4">Step 7: Set payment method</h2>
<textarea class="form-control" id="set_payment_method_data">
mutation {
setPaymentMethodOnCart(input: {
cart_id: "CART_ID"
payment_method: {
code: "stripe_payments"
stripe_payments: {
cc_stripejs_token: "pm_card_authenticationRequired"
save_payment_method: true
}
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
</textarea>
<button onclick="post('set_payment_method')" class="btn btn-primary">POST</button>
<pre id="set_payment_method_response" class="alert alert-light"></pre>
<h2 class="mt-4">Place order</h2>
<textarea class="form-control" id="place_order_data">
mutation {
placeOrder(input: {cart_id: "CART_ID"}) {
order {
order_number
}
}
}
</textarea>
<button onclick="placeOrder()" class="btn btn-primary">POST</button>
<pre id="place_order_response" class="alert alert-light"></pre>
<br>
<br>
<br>
<script>
var customerToken = null;
var cartId = null;
var clientSecret = null;
var stripe = null;
var $ = jQuery;
if (localStorage.getItem("base_url"))
{
$("#base_url").val(localStorage.getItem("base_url"));
}
else
{
$("#base_url").val(window.location.protocol + "//" + window.location.hostname);
}
if (localStorage.getItem("sku"))
{
$("#sku").val(localStorage.getItem("sku").toString());
}
if (localStorage.getItem("pk"))
{
$("#pk").val(localStorage.getItem("pk"));
}
var post = function(step, onSuccess)
{
var endpoint = $("#base_url").val() + '/graphql';
var sku = $("#sku").val();
var data = JSON.stringify({
query: $("#" + step + "_data").val().replace("CART_ID", cartId).replace("SIMPLE_PRODUCT_SKU", sku)
});
$( "#" + step + "_response" ).html("");
$.ajax({
url: endpoint,
type: "POST",
data: data,
contentType:"application/json; charset=utf-8",
dataType:"json",
beforeSend: function (xhr)
{
if (customerToken)
{
xhr.setRequestHeader('Authorization', 'Bearer ' + customerToken);
}
},
success: function(response)
{
$( "#" + step + "_response" ).html( JSON.stringify(response, null, 2) );
if (typeof onSuccess != "undefined")
{
onSuccess(response);
}
}
});
};
var createGuestCart = function()
{
post("create_guest_cart", function(response)
{
cartId = response.data.createEmptyCart;
});
};
var placeOrder = function()
{
post("place_order", function(response)
{
if (response.errors)
{
var message = response.errors[0].message;
if (message && message.indexOf("Authentication Required: ") === 0)
{
clientSecret = message.substring("Authentication Required: ".length);
stripe = Stripe($("#pk").val());
authenticate(clientSecret);
}
}
});
}
var authenticate = function(pi_client_secret)
{
stripe.retrievePaymentIntent(pi_client_secret).then(function(result)
{
if (result.error)
alert(result.error.message);
else if (result.paymentIntent.confirmation_method == "manual")
return handleCardAction(pi_client_secret, placeAuthenticatedOrder);
else
return handleCardPayment(pi_client_secret, placeAuthenticatedOrder);
});
};
var handleCardPayment = function(pi_client_secret, done)
{
try
{
stripe.handleCardPayment(pi_client_secret).then(function(result)
{
if (result.error)
return done(result.error.message);
return done();
});
}
catch (e)
{
done(e.message);
}
};
var handleCardAction = function(pi_client_secret, done)
{
try
{
stripe.handleCardAction(pi_client_secret).then(function(result)
{
if (result.error)
return done(result.error.message);
return done();
});
}
catch (e)
{
done(e.message);
}
};
var placeAuthenticatedOrder = function(error)
{
if (typeof error != "undefined")
{
alert("Authentication failed: " + error);
}
else
{
alert("Authentication succeeded. Try placing the order again.");
}
};
var save = function(key)
{
var value = $("#" + key).val();
localStorage.setItem(key, value);
}
</script>
</body>