2

I am trying to retrieve the metadata and payment amount from an object retrieved from an endpoint using Stripe's PHP library.

I am using a very slightly modified version of their example code:

$payload = @file_get_contents('php://input');
$event = null;

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        // Read the result.
        $paymentIntentValues = $paymentIntent->values(); // Returns standard array??
        break;
    case 'payment_method.attached':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        // Read the result.
        $paymentMethodValues = $paymentMethod->values(); // Returns standard array??
        break;
    // ... handle other event types
    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}

http_response_code(200);

The problem is when I dump any of the objects, e.g. $paymentIntent, the dump is just of the Charge object and when I try get the values using values(); it gives me an ambiguous standard array.

e.g.

array (45) [
    0 => string (12) "pi_secretkey"
    1 => string (12) "paymentIntent"
    2 => integer 247
    3 => integer 0
    4 => null
    5 => null
    6 => null
    7 => string (13) "anothersecret"
    8 => Stripe\StripeObject (7) (
        protected '_lastResponse' -> null
        protected '_opts' -> Stripe\Util\RequestOptions (3) (
            public 'apiBase' -> null
            public 'apiKey' -> null
            public 'headers' -> array (0) []
        )

I want to be able to read the values of that $paymentIntent object and use the values consistently either through associative array or object.

1 Answer 1

3

In order to retrieve an associative array listing the values of Stripe objects, e.g. the PaymentIntent object, you must use the toArray() function

e.g.

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        // Read the result.
        $paymentIntentValues = $paymentIntent->toArray(); // Returns associative array of values.
        break;
    case 'payment_method.attached':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        // Read the result.
        $paymentMethodValues = $paymentMethod->toArray(); // Returns associative array of values.
        break;
    // ... handle other event types
    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}

result:

array (45) [
    'id' => string (12) "pi_secretkey"
    'object' => string (12) "paymentIntent"
    'amount' => integer 247
    'amount_refunded' => integer 0
    'application' => null
    'application_fee' => null
    'application_fee_amount' => null
    'balance_transaction' => string (13) "anothersecret"
    etc... etc...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.