Hi
Like my topic said, i have sometimes wrong success paument.
in front i followed this : https://developer.squareup.com/docs/web-payments/take-card-payment
<script type="text/javascript" src="https://sandbox.web.squarecdn.com/v1/square.js"></script>
<script>
const appId = 'XXXXXX';
const locationId = 'XXXXX';
async function initializeCard(payments) { const card = await payments.card(); await card.attach('#card-container'); return card; }
async function createPayment(token) {
const body = JSON.stringify({ locationId, sourceId: token, });
const paymentResponse = await fetch('z2.php?z=1', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body, });
if (paymentResponse.ok) {
return paymentResponse.json();
}
const errorBody = await paymentResponse.text();
throw new Error(errorBody);
}
async function tokenize(paymentMethod) {
const tokenResult = await paymentMethod.tokenize();
if (tokenResult.status === 'OK') {
return tokenResult.token;
}
else {
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`;
if (tokenResult.errors) { errorMessage += ` and errors: ${JSON.stringify( tokenResult.errors )}`; }
throw new Error(errorMessage);
}
}
// status is either SUCCESS or FAILURE;
function displayPaymentResults(status) {
const statusContainer = document.getElementById('payment-status-container');
if (status === 'SUCCESS') {
statusContainer.classList.remove('is-failure');
statusContainer.classList.add('is-success');
}
else {
statusContainer.classList.remove('is-success');
statusContainer.classList.add('is-failure');
}
statusContainer.style.visibility = 'visible';
}
document.addEventListener('DOMContentLoaded', async function () {
if (!window.Square) { throw new Error('Square.js failed to load properly'); }
let payments;
try { payments = window.Square.payments(appId, locationId); }
catch { const statusContainer = document.getElementById('payment-status-container');
statusContainer.className = 'missing-credentials';
statusContainer.style.visibility = 'visible';
return;
}
let card;
try {
card = await initializeCard(payments);
}
catch (e) {
console.error('Initializing Card failed', e);
return;
}
// Checkpoint 2.
async function handlePaymentMethodSubmission(event, paymentMethod) {
event.preventDefault();
try {
// disable the submit button as we await tokenization and make a payment request.
cardButton.disabled = true;
const token = await tokenize(paymentMethod);
const paymentResults = await createPayment(token);
displayPaymentResults('SUCCESS');
console.debug('Payment Success', paymentResults);
}
catch (e) {
cardButton.disabled = false;
displayPaymentResults('FAILURE');
console.error(e.message);
}
}
const cardButton = document.getElementById('card-button');
cardButton.addEventListener('click', async function (event) {
await handlePaymentMethodSubmission(event, card);
});
});
</script>
</head>
<body>
<form id="payment-form">
<div id="card-container"></div>
<button id="card-button" type="button">Pay $1.00</button>
</form>
<div id="payment-status-container"></div>
so this code send the token to a php source and it’s worked great.
i use this in PHP
$data = json_decode(file_get_contents('php://input'), true);
$square_client = new SquareClient(['accessToken' => 'XXXX','environment' => Environment::SANDBOX]);
$payments_api = $square_client->getPaymentsApi();
$money = new Money();
$money->setAmount(1000);
$money->setCurrency("EUR");
$orderId = '#1';
$create_payment_request = new CreatePaymentRequest($data['sourceId'], $orderId, $money);
$create_payment_request->setReferenceId('123456');
$create_payment_request->setNote('123456');
$response = $payments_api->createPayment($create_payment_request);
if ($response->isSuccess()) {
echo json_encode($response->getResult());
}
else {
echo json_encode($response->getErrors());
}
exit;
my problem is sometime the console display this :
Payment Success
Array [ {…} ]
0: Object { category: “INVALID_REQUEST_ERROR”, code: “IDEMPOTENCY_KEY_REUSED”, detail: “Different request parameters used for the same idempotency_key: #1.” }
i have the payment successfull. But it’s totally wrong.
What I missed. What i did wrong. if the idempotency_key is different, the payment works, and i receive the webhook, my sandbox seller dashboard see the sale.
Just i have the case, that when the array is error one, the javascript think it’s good.
Thanks to put light on my mistake. Sorry to be bad and don’t handle the problem by myself
Regards
PS : I receive two webhook, the difference is one is Version : 2 and the other is Version : 3 (the version 3 have also the fee). it’s normal ?