# Square API Inquiry

**URL:** <https://developer.squareup.com/forums/t/square-api-inquiry/8472>\
**Category:** Questions\
**Created:** [February 5, 2023, 4:06pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472 "2023-02-05T16:06:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 5, 2023, 4:06pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/1 "2023-02-05T16:06:38Z")

</div>

Hello, I have this code obtained, I need to obtain broken down information, to be stored in the database table Mysql

require ‘vendor/autoload.php’;  
use Square\SquareClient;  
use Square\LocationsApi;  
use Square\Exceptions\ApiException;  
use Square\Models\ListLocationsResponse;  
use Square\Environment;  
use Square\Models\Money;  
use Square\Models\CreatePaymentRequest;  
use Square\Models\createPayment;  
use Square\Models\Transaction;

$data = json\_decode(file\_get\_contents(‘php://input’), true);  
$squareClient = new SquareClient([  
‘accessToken’ =\> ‘XXXXXX’,  
‘environment’ =\> Environment::SANDBOX,  
]);

$payments\_api = $squareClient-\>getPaymentsApi();  
$money = new Money();  
$amount = $\_REQUEST[‘amount’] \* 100;  
$money-\>setAmount($amount);  
$money-\>setCurrency(‘USD’);

$orderId = rand(9000,1000);  
$create\_payment\_request = new CreatePaymentRequest($data[‘sourceId’], $orderId, $money);

$response = $payments\_api-\>createPayment($create\_payment\_request);  
if ($response-\>isSuccess()) {

“This is where I need to do the process to save information to the database table”

$order = new Square\Models\Order(“location\_id\_here”);  
$order-\>cart\_number = $response[‘cart\_number’];  
$order-\>expiry\_date = $response[‘expiry\_date’];  
$order-\>cvc\_code = $response[‘cvc\_code’];  
$order-\>payment\_method = ‘Prepaid’;  
$order-\>total = $amount;

if($order[‘payment\_method’]==“Prepaid”){  
// Insert into the database  
}  
echo json\_encode($response-\>getResult());  
} else {  
echo json\_encode($response-\>getErrors());  
}

---

<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:** [February 6, 2023, 5:16pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/2 "2023-02-06T17:16:25Z")

</div>

Once the payment is completed you will get a [response](https://developer.squareup.com/reference/square/payments-api/create-payment#response__property-payment) from Square that you can pars and save to your database. 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 6, 2023, 5:33pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/3 "2023-02-06T17:33:34Z")

</div>

Correct, but in the manuals they do not specify how the information is extracted,

For example I have this $response = $payments\_api-\>createPayment($create\_payment\_request); how to decompose and get the transaction ID

---

<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:** [February 6, 2023, 5:40pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/4 "2023-02-06T17:40:31Z")

</div>

The information is returned in the form of a JSON response. You can extract the information from the response to your database. Also the `order_id` in the payment response is equal to the `transactions_id`. Is there a reason your logging it as a `transaction_id` instead of an `order_id`? 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 6, 2023, 5:50pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/5 "2023-02-06T17:50:57Z")

</div>

transaction or order both are useful to me, since this is how I will make a comparison of what is registered in the database and what appears in the square transaction dashboard.  
What if I don’t know how to extract that data from the JSON 😌

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 6, 2023, 5:53pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/6 "2023-02-06T17:53:53Z")

</div>

help me with an example of extracting $response = $payments api-\>create Payment($create\_payment\_request); various JSON data  
and then store in the database

---

<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:** [February 6, 2023, 7:32pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/7 "2023-02-06T19:32:03Z")

</div>

Here is an example of how to get the `orderId` using the PHP SDK.

```auto
<?php

require 'vendor/autoload.php';

use Square\SquareClient;
use Square\Environment;
use Square\Exceptions\ApiException;

$client = new SquareClient([
    'accessToken' => getenv('SQUARE_ACCESS_TOKEN'),
    'environment' => Environment::SANDBOX,
]);

try {

    $apiResponse = $client->getPaymentsApi()->listPayments();

    if ($apiResponse->isSuccess()) {
        $result = $apiResponse->getResult();
        foreach ($result->getPayments() as $payment) {
            printf(
                "%s<p/>",
                $payment->getOrderId()
            );
        }

    } else {
        $errors = $apiResponse->getErrors();
        foreach ($errors as $error) {
            printf(
                "%s<br/> %s<br/> %s<p/>",
                $error->getCategory(),
                $error->getCode(),
                $error->getDetail()
            );
        }
    }

} catch (ApiException $e) {
    echo "ApiException occurred: <b/>";
    echo $e->getMessage() . "<p/>";
}

```

🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 6, 2023, 8:51pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/8 "2023-02-06T20:51:21Z")

</div>

It does not work for me

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 6, 2023, 9:04pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/10 "2023-02-06T21:04:45Z")

</div>

This code makes me the process is functional,

```auto
<?php
require 'vendor/autoload.php';

use Square\SquareClient;
use Square\LocationsApi;
use Square\Exceptions\ApiException;
use Square\Models\ListLocationsResponse;
use Square\Environment;
use Square\Models\Money;
use Square\Models\CreatePaymentRequest;
use Square\Models\CreatePayment;
use Square\Models\CreateOrderRequest;
use Square\Models\CreateCheckoutRequest;
use Square\Models\Order;
use Square\Models\OrderLineItem;

$data = json_decode(file_get_contents('php://input'), true);
$squareClient = new SquareClient([
    'accessToken' => 'xxxxx',
    'environment' => Environment::SANDBOX,
]);

$payments_api = $squareClient->getPaymentsApi();
$money = new Money();

$importe = $_REQUEST['importe'] * 100;
$money->setAmount($importe);
$money->setCurrency('USD');

$create_payment_request = new CreatePaymentRequest($data['sourceId'], uniqid(), $money);
$response = $payments_api->createPayment($create_payment_request);

if ($response->isSuccess()) {
	echo json_encode($response->getResult());
} else {
  echo json_encode($response->getErrors());
}
?>

```

**and I want to Get Order \_ID or ID**

1. payment: {id: “NEtjU5vvnZl4m8qzI38ypiviMJ8YY”, created\_at: “2023-02-06T20:59:08.002Z”,…}}

2. payment: {id: “NEtjU5vvnZl4m8qzI38ypiviMJ8YY”, created\_at: “2023-02-06T20:59:08.002Z”,…}

```
1. amount_money: {amount: 10, currency: "USD"}
2. application_details: {square_product: "ECOMMERCE_API", application_id: "sandbox-sq0idb-Wcpg1dCm75NuPk91aZFdFg"}
3. approved_money: {amount: 10, currency: "USD"}
4. card_details: {status: "CAPTURED", card: {card_brand: "VISA", last_4: "1111", exp_month: 11, exp_year: 2023,…},…}
5. created_at: "2023-02-06T20:59:08.002Z"
6. delay_action: "CANCEL"
7. delay_duration: "PT168H"
8. delayed_until: "2023-02-13T20:59:08.002Z"
**9. id: "NEtjU5vvnZl4m8qzI38ypiviMJ8YY"**
10. location_id: "LMHT9B42D1W1X"
**11. order_id: "0tuEnv1tf4Ztmz3ZOY5ooTBEQUeZY"**
12. receipt_number: "NEtj"
13. receipt_url: "https://squareupsandbox.com/receipt/preview/NEtjU5vvnZl4m8qzI38ypiviMJ8YY"
14. source_type: "CARD"
15. status: "COMPLETED"
16. total_money: {amount: 10, currency: "USD"}
17. updated_at: "2023-02-06T20:59:08.235Z"
18. version_token: "MyDNIJ4BttDfJgFX3HxXC20Q8UQyx71VA3BSjunQt5l6o"

```

---

<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:** [February 6, 2023, 9:59pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/11 "2023-02-06T21:59:48Z")

</div>

The results are nested in the JSON. You will need to parse the result. For example:

```auto
    if ($apiResponse->isSuccess()) {
        $result = $apiResponse->getResult();
        foreach ($result->getPayments() as $payment) {
            printf(
                "%s: %s <p/>",
                $payment->getId(),
                $payment->getOrderId()
            );
        }

    } else

```

🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 7, 2023, 12:40am UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/12 "2023-02-07T00:40:54Z")

</div>

Well, now I get the data I need, and add it to the database, and the process appears in the sandbox but I always get “Payment Failed”

---

<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:** [February 7, 2023, 12:43am UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/13 "2023-02-07T00:43:17Z")

</div>

Are you using one of our valid [sandbox test value](https://developer.squareup.com/docs/devtools/sandbox/payments) to process the payment? 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 7, 2023, 12:44am UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/14 "2023-02-07T00:44:17Z")

</div>

yes correct, since I am doing the tests

---

<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:** [February 7, 2023, 4:47pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/15 "2023-02-07T16:47:20Z")

</div>

What test values are you using that are failing? 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 7, 2023, 5:36pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/16 "2023-02-07T17:36:07Z")

</div>

I have this

```auto
$create_payment_request = new CreatePaymentRequest($data['sourceId'], uniqid(), $money);
$response = $payments_api->createPayment($create_payment_request);

if ($response->isSuccess()) {
	
		$decoded_json = json_decode(json_encode($response->getResult()),true);
    
	foreach($decoded_json as $key => $value) {
            $id = $decoded_json[$key]["id"];
	  $orden_id = $decoded_json[$key]["order_id"];	
    $created_at = $decoded_json[$key]["created_at"];
        $amount = $decoded_json[$key]["amount_money"]["amount"];
		$status = $decoded_json[$key]["status"];
   	
          echo 'id:'.$id.'<br>';
		  echo 'orden_id:'.$orden_id.'<br>';
		  echo 'created_at:'.$created_at.'<br>';
		  echo 'amount:'.$amount.'<br>';
		  echo 'status:'.$status.'<br>';

      }
	echo json_encode($response->getResult());
} 

```

It does the whole process well, which in the transaction panel is reflected in sandbox, but it comes out at the end

Payment Failed

![paymentfailled](https://global.discourse-cdn.com/square/original/2X/c/c12e404913c2079ff1c2604662c433d900e41a6b.jpeg)

---

<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:** [February 7, 2023, 6:42pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/17 "2023-02-07T18:42:18Z")

</div>

What’s your application ID? 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 7, 2023, 7:00pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/18 "2023-02-07T19:00:34Z")

</div>

sandbox-sq0idb-Wcpg1dCm75NuPk91aZFdFg

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 10, 2023, 10:27pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/19 "2023-02-10T22:27:03Z")

</div>

Hi, I still haven’t resolved the payment failed

---

<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:** [February 10, 2023, 11:16pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/20 "2023-02-10T23:16:05Z")

</div>

I see the payments succeeding. Did you change the quickstart and it possibly brake the [payment status container](https://github.com/square/web-payments-quickstart/blob/fa10bcb121a9bef869218530eba99025c0e9b180/public/examples/card.html#L58)? 🙂

---

<div class="post-metadata">

**Author:** ![digracom](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@digracom](https://developer.squareup.com/forums/u/digracom)\
**Post date:** [February 10, 2023, 11:25pm UTC](https://developer.squareup.com/forums/t/square-api-inquiry/8472/21 "2023-02-10T23:25:56Z")

</div>

what do you suggest me to do?

[Next page](https://developer.squareup.com/forums/t/square-api-inquiry/8472.md?page=2)
