質問編集履歴
1
コードを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -33,3 +33,321 @@
|
|
33
33
|
|
34
34
|
|
35
35
|
ローカル環境では動作するものが、本番環境では動作しない原因など、もし見当つく方がいればご回答お願いいたします。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
### コード
|
40
|
+
|
41
|
+
```index.html
|
42
|
+
|
43
|
+
<script type="text/javascript">
|
44
|
+
|
45
|
+
// Create and initialize a payment form object
|
46
|
+
|
47
|
+
const paymentForm = new SqPaymentForm({
|
48
|
+
|
49
|
+
// Initialize the payment form elements
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
//TODO: Replace with your sandbox application ID
|
54
|
+
|
55
|
+
applicationId: "application ID",
|
56
|
+
|
57
|
+
inputClass: 'sq-input',
|
58
|
+
|
59
|
+
autoBuild: false,
|
60
|
+
|
61
|
+
// Customize the CSS for SqPaymentForm iframe elements
|
62
|
+
|
63
|
+
inputStyles: [{
|
64
|
+
|
65
|
+
fontSize: '16px',
|
66
|
+
|
67
|
+
lineHeight: '24px',
|
68
|
+
|
69
|
+
padding: '16px',
|
70
|
+
|
71
|
+
placeholderColor: '#a0a0a0',
|
72
|
+
|
73
|
+
backgroundColor: 'transparent',
|
74
|
+
|
75
|
+
}],
|
76
|
+
|
77
|
+
// Initialize the credit card placeholders
|
78
|
+
|
79
|
+
cardNumber: {
|
80
|
+
|
81
|
+
elementId: 'sq-card-number',
|
82
|
+
|
83
|
+
placeholder: 'Card Number'
|
84
|
+
|
85
|
+
},
|
86
|
+
|
87
|
+
cvv: {
|
88
|
+
|
89
|
+
elementId: 'sq-cvv',
|
90
|
+
|
91
|
+
placeholder: 'CVV'
|
92
|
+
|
93
|
+
},
|
94
|
+
|
95
|
+
expirationDate: {
|
96
|
+
|
97
|
+
elementId: 'sq-expiration-date',
|
98
|
+
|
99
|
+
placeholder: 'MM/YY'
|
100
|
+
|
101
|
+
},
|
102
|
+
|
103
|
+
postalCode: {
|
104
|
+
|
105
|
+
elementId: 'sq-postal-code',
|
106
|
+
|
107
|
+
placeholder: 'Postal'
|
108
|
+
|
109
|
+
},
|
110
|
+
|
111
|
+
// SqPaymentForm callback functions
|
112
|
+
|
113
|
+
callbacks: {
|
114
|
+
|
115
|
+
/*
|
116
|
+
|
117
|
+
* callback function: cardNonceResponseReceived
|
118
|
+
|
119
|
+
* Triggered when: SqPaymentForm completes a card nonce request
|
120
|
+
|
121
|
+
*/
|
122
|
+
|
123
|
+
cardNonceResponseReceived: function (errors, nonce, cardData) {
|
124
|
+
|
125
|
+
if (errors) {
|
126
|
+
|
127
|
+
// Log errors from nonce generation to the browser developer console.
|
128
|
+
|
129
|
+
console.error('Encountered errors:');
|
130
|
+
|
131
|
+
errors.forEach(function (error) {
|
132
|
+
|
133
|
+
console.error(' ' + error.message);
|
134
|
+
|
135
|
+
});
|
136
|
+
|
137
|
+
alert('Encountered errors, check browser developer console for more details');
|
138
|
+
|
139
|
+
return;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
alert(`The generated nonce is:\n${nonce}`);
|
144
|
+
|
145
|
+
fetch('process-payment', {
|
146
|
+
|
147
|
+
method: 'POST',
|
148
|
+
|
149
|
+
headers: {
|
150
|
+
|
151
|
+
'Accept': 'application/json',
|
152
|
+
|
153
|
+
'Content-Type': 'application/json'
|
154
|
+
|
155
|
+
},
|
156
|
+
|
157
|
+
body: JSON.stringify({
|
158
|
+
|
159
|
+
nonce: nonce
|
160
|
+
|
161
|
+
})
|
162
|
+
|
163
|
+
})
|
164
|
+
|
165
|
+
.catch(err => {
|
166
|
+
|
167
|
+
alert('Network error: ' + err);
|
168
|
+
|
169
|
+
})
|
170
|
+
|
171
|
+
.then(response => {
|
172
|
+
|
173
|
+
if (!response.ok) {
|
174
|
+
|
175
|
+
return response.json().then(errorInfo => Promise.reject(errorInfo)); //UPDATE HERE
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
return response.json(); //UPDATE HERE
|
180
|
+
|
181
|
+
})
|
182
|
+
|
183
|
+
.then(data => {
|
184
|
+
|
185
|
+
console.log(data); //UPDATE HERE
|
186
|
+
|
187
|
+
alert('Payment complete successfully!\nCheck browser developer console for more details');
|
188
|
+
|
189
|
+
})
|
190
|
+
|
191
|
+
.catch(err => {
|
192
|
+
|
193
|
+
console.error(err);
|
194
|
+
|
195
|
+
alert('Payment failed to complete!\nCheck browser developer console for more details');
|
196
|
+
|
197
|
+
});
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
});
|
204
|
+
|
205
|
+
paymentForm.build();
|
206
|
+
|
207
|
+
// onGetCardNonce is triggered when the "Pay $1.00" button is clicked
|
208
|
+
|
209
|
+
function onGetCardNonce(event) {
|
210
|
+
|
211
|
+
// Don't submit the form until SqPaymentForm returns with a nonce
|
212
|
+
|
213
|
+
event.preventDefault();
|
214
|
+
|
215
|
+
// Request a nonce from the SqPaymentForm object
|
216
|
+
|
217
|
+
paymentForm.requestCardNonce();
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
</script>
|
222
|
+
|
223
|
+
```
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
```server.js
|
228
|
+
|
229
|
+
const express = require('express');
|
230
|
+
|
231
|
+
const bodyParser = require('body-parser');
|
232
|
+
|
233
|
+
const crypto = require('crypto');
|
234
|
+
|
235
|
+
const squareConnect = require('square-connect');
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
const app = express();
|
240
|
+
|
241
|
+
const port = 3000;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
// Set the Access Token
|
246
|
+
|
247
|
+
const accessToken = 'Access Token';
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
app.use(bodyParser.json());
|
252
|
+
|
253
|
+
app.use(bodyParser.urlencoded({ extended: false }));
|
254
|
+
|
255
|
+
app.use(express.static(__dirname));
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
// Set Square Connect credentials and environment
|
260
|
+
|
261
|
+
const defaultClient = squareConnect.ApiClient.instance;
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// Configure OAuth2 access token for authorization: oauth2
|
266
|
+
|
267
|
+
const oauth2 = defaultClient.authentications['oauth2'];
|
268
|
+
|
269
|
+
oauth2.accessToken = accessToken;
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
// Set 'basePath' to switch between sandbox env and production env
|
274
|
+
|
275
|
+
// sandbox: https://connect.squareupsandbox.com
|
276
|
+
|
277
|
+
// production: https://connect.squareup.com
|
278
|
+
|
279
|
+
defaultClient.basePath = 'https://connect.squareup.com';
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
app.post('/process-payment', async (req, res) => {
|
284
|
+
|
285
|
+
const request_params = req.body;
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
// length of idempotency_key should be less than 45
|
290
|
+
|
291
|
+
const idempotency_key = crypto.randomBytes(22).toString('hex');
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
// Charge the customer's card
|
296
|
+
|
297
|
+
const payments_api = new squareConnect.PaymentsApi();
|
298
|
+
|
299
|
+
const request_body = {
|
300
|
+
|
301
|
+
source_id: request_params.nonce,
|
302
|
+
|
303
|
+
amount_money: {
|
304
|
+
|
305
|
+
amount: 100, // $1.00 charge
|
306
|
+
|
307
|
+
currency: 'JPY'
|
308
|
+
|
309
|
+
},
|
310
|
+
|
311
|
+
idempotency_key: idempotency_key
|
312
|
+
|
313
|
+
};
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
try {
|
318
|
+
|
319
|
+
const response = await payments_api.createPayment(request_body);
|
320
|
+
|
321
|
+
res.status(200).json({
|
322
|
+
|
323
|
+
'title': 'Payment Successful',
|
324
|
+
|
325
|
+
'result': response
|
326
|
+
|
327
|
+
});
|
328
|
+
|
329
|
+
} catch(error) {
|
330
|
+
|
331
|
+
res.status(500).json({
|
332
|
+
|
333
|
+
'title': 'Payment Failure',
|
334
|
+
|
335
|
+
'result': error.response.text
|
336
|
+
|
337
|
+
});
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
});
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
app.listen(
|
346
|
+
|
347
|
+
port,
|
348
|
+
|
349
|
+
() => console.log(`listening on - http://localhost:${port}`)
|
350
|
+
|
351
|
+
);
|
352
|
+
|
353
|
+
```
|