You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Coinbase Commerce API返回的ArrayObject转换为JSON并获取发票状态值

How to Retrieve the Status of a Coinbase Commerce Charge in PHP

Problem

I'm trying to get the status of a Coinbase invoice I created via their API. The API returns an ArrayObject, and I want to convert it to JSON (or access the field directly) to get the value of the "status" field. Here's my current code, with a comment where I need to fetch the status:

<?php require_once("../vendor/autoload.php");
use CoinbaseCommerce\ApiClient;
use CoinbaseCommerce\Resources\Charge;

ApiClient::init("XXXX-XXXXX-XXXX-XXXX-XXXXX");
$chargeData = [
    'name' => 'CloudPay - Recharge 10€',
    'description' => 'wwww.CloudPay.fr | Rechargez votre solde CloudPay de 10€',
    'local_price' => [
        'amount' => '10.00',
        'currency' => 'EUR'
    ],
    'pricing_type' => 'fixed_price',
    'redirect_url' => "https://cloudpay.fr/paiement?status=success",
    'cancel_url' => "https://cloudpay.fr/paiement?error=crypto_canceled"
];
$chargeObj = Charge::create($chargeData);
echo ("id : ");
echo $chargeObj->id;
echo ("<br><br>");
echo ("url : ");
echo $chargeObj->hosted_url;
echo ("<br><br><br>");
// Retrieve charge by "id"
try {
    $retrievedCharge = Charge::retrieve($chargeObj->id);
    echo sprintf("Successfully retrieved charge\n");
    echo ('<br><br>');
    echo $retrievedCharge;
    echo ('<br><br><br><br>');
    // here I want to get the status of the invoice
} catch (\Exception $exception) {
    echo sprintf("Enable to retrieve charge. Error: %s \n", $exception->getMessage());
}
?>

(Note: The API response confirms the "status" field is included in the returned Charge data.)

Solution

Hey there! Let's break down how to get that invoice status easily—you don't even need to convert to JSON unless you specifically want to. Here are the simplest approaches:

1. Directly Access the Status Property

The Coinbase Commerce PHP SDK wraps the API response into a Charge object that maps all response fields to public properties. You can grab the status directly like this:

// Inside your try block, right after retrieving $retrievedCharge
$chargeStatus = $retrievedCharge->status;
echo "Charge Status: " . $chargeStatus;

This works for all other fields too (like amount, created_at)—just use the same dot notation.

2. Convert to Array or JSON (If Needed)

If you need to work with the data as an array or JSON string, use these methods:

Convert to Array

Use the SDK's built-in toArray() method to turn the object into a PHP array:

$chargeArray = $retrievedCharge->toArray();
$status = $chargeArray['status'];
echo "Charge Status (from array): " . $status;
Convert to JSON String

Encode the object directly to JSON, then decode it if you need an array:

$chargeJson = json_encode($retrievedCharge);
// Decode to array if needed
$chargeData = json_decode($chargeJson, true);
$status = $chargeData['status'];
echo "Charge Status (from JSON): " . $status;

Updated Working Code

Here's your full code with the direct status access added:

<?php require_once("../vendor/autoload.php");
use CoinbaseCommerce\ApiClient;
use CoinbaseCommerce\Resources\Charge;

ApiClient::init("XXXX-XXXXX-XXXX-XXXX-XXXXX");
$chargeData = [
    'name' => 'CloudPay - Recharge 10€',
    'description' => 'wwww.CloudPay.fr | Rechargez votre solde CloudPay de 10€',
    'local_price' => [
        'amount' => '10.00',
        'currency' => 'EUR'
    ],
    'pricing_type' => 'fixed_price',
    'redirect_url' => "https://cloudpay.fr/paiement?status=success",
    'cancel_url' => "https://cloudpay.fr/paiement?error=crypto_canceled"
];
$chargeObj = Charge::create($chargeData);
echo ("id : ");
echo $chargeObj->id;
echo ("<br><br>");
echo ("url : ");
echo $chargeObj->hosted_url;
echo ("<br><br><br>");
// Retrieve charge by "id"
try {
    $retrievedCharge = Charge::retrieve($chargeObj->id);
    echo sprintf("Successfully retrieved charge\n");
    echo ('<br><br>');
    echo $retrievedCharge;
    echo ('<br><br><br><br>');
    
    // Get the status directly from the Charge object
    $chargeStatus = $retrievedCharge->status;
    echo "Charge Status: " . $chargeStatus;
    
} catch (\Exception $exception) {
    echo sprintf("Unable to retrieve charge. Error: %s \n", $exception->getMessage());
}
?>

内容的提问来源于stack exchange,提问作者Merlin.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:29:06