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());
}
Once the payment is completed you will get a response from Square that you can pars and save to your database.
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
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
?
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
help me with an example of extracting $response = $payments api->create Payment($create_payment_request); various JSON data
and then store in the database
Here is an example of how to get the orderId
using the PHP SDK.
<?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/>";
}
This code makes me the process is functional,
<?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
-
payment: {id: “NEtjU5vvnZl4m8qzI38ypiviMJ8YY”, created_at: “2023-02-06T20:59:08.002Z”,…}}
-
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"
The results are nested in the JSON. You will need to parse the result. For example:
if ($apiResponse->isSuccess()) {
$result = $apiResponse->getResult();
foreach ($result->getPayments() as $payment) {
printf(
"%s: %s <p/>",
$payment->getId(),
$payment->getOrderId()
);
}
} else
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”
Are you using one of our valid sandbox test value to process the payment?
yes correct, since I am doing the tests
What test values are you using that are failing?
I have this
$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
What’s your application ID?
sandbox-sq0idb-Wcpg1dCm75NuPk91aZFdFg
Hi, I still haven’t resolved the payment failed
I see the payments succeeding. Did you change the quickstart and it possibly brake the payment status container?
what do you suggest me to do?