# Wrong payment success

**URL:** <https://developer.squareup.com/forums/t/wrong-payment-success/7040>\
**Category:** Questions\
**Tags:** payments-api, sandbox, web-payments-sdk\
**Created:** [September 13, 2022, 11:36pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040 "2022-09-13T23:36:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThibaudN](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@ThibaudN](https://developer.squareup.com/forums/u/ThibaudN)\
**Post date:** [September 13, 2022, 11:36pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040/1 "2022-09-13T23:36:09Z")

</div>

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](https://developer.squareup.com/docs/web-payments/take-card-payment)

```auto
<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

```auto
$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 ?

---

<div class="post-metadata">

**Author:** ![Bryan-Square](https://sea1.discourse-cdn.com/square/forums/user_avatar/developer.squareup.com/bryan-square/32/38_2.png) [@Bryan-Square](https://developer.squareup.com/forums/u/Bryan-Square)\
**Post date:** [September 14, 2022, 4:06pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040/2 "2022-09-14T16:06:52Z")

</div>

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](https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/php_payment/public/js)?

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](https://developer.squareup.com/reference/square/orders-api/create-order) request. 🙂

---

<div class="post-metadata">

**Author:** ![ThibaudN](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@ThibaudN](https://developer.squareup.com/forums/u/ThibaudN)\
**Post date:** [September 14, 2022, 4:22pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040/3 "2022-09-14T16:22:51Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Bryan-Square](https://sea1.discourse-cdn.com/square/forums/user_avatar/developer.squareup.com/bryan-square/32/38_2.png) [@Bryan-Square](https://developer.squareup.com/forums/u/Bryan-Square)\
**Post date:** [September 14, 2022, 6:05pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040/4 "2022-09-14T18:05:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ThibaudN](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@ThibaudN](https://developer.squareup.com/forums/u/ThibaudN)\
**Post date:** [September 14, 2022, 6:19pm UTC](https://developer.squareup.com/forums/t/wrong-payment-success/7040/5 "2022-09-14T18:19:30Z")

</div>

i spot a difference on the php

```auto
 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
