Wrong payment success

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 ?

It looks like any time you submit a request to Square whether it’s successful or not you show a success. Just to confirm is the success showing in the console or is it also showing success on the page? Also since your using PHP have you seen our PHP example?

Lastly, I see that you set an orderId that’s not a Square generated orderId. If your going to pass an orderId in it needs to be one that’s generated from a CreateOrder request. :slightly_smiling_face:

Just to confirm is the success showing in the console or is it also showing success on the page?

it’s do the both. And it’s this part who do it.

clearly, i think that, if the await create payment return json => success message
the code don’t handle the json who return an error without error key.

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);
          }

Also since your using PHP have you seen our PHP example?
the page you send me have 16 months ago. I thinked it was not outdated. so i found a code on this forum who was more recent, and it’s why i took it. (for the php part). for the js part i follow the guide where i shared the link on my first post.

Lastly, I see that you set an orderId that’s not a Square generated orderId. If your going to pass an orderId in it needs to be one that’s generated from a CreateOrder request

Yes i do some test to see what is send via the webhook, to be able to rely a cart/basket to the paiement.

The PHP example uses the same Web Payments SDK that you’re using from the Quickstart. :slightly_smiling_face:

i spot a difference on the php

 if ($response->isSuccess()) {
    echo json_encode($response->getResult());
  } else {
    echo json_encode(array('errors' => $response->getErrors()));
  }
} catch (ApiException $e) {
  echo json_encode(array('errors' => $e));
}

and by your answer you told me to use the javascript that you put me the link on your first answer ?

sometimes i need clear answer to be sure what to do. sorry about that