Skip to content
Advertisement

Trying to access data returned from Stripe API with Alamofire, but result is not valid JSON

I’m trying to interface a Swift app with the Stripe API, using Alamofire, but I’m having trouble with the returned results. I was expecting valid JSON, but I am getting the following:

StripeSetupIntent JSON: {
    "id": "seti_1Go**************g4qyJgp",
    "object": "setup_intent",
    "application": null,
    "cancellation_reason": null,
    "client_secret": "seti_1Go***************gp_secret_HM*******************ObpHy7Mk",
    "created": 1590784053,
    "customer": "cus_H*********Tna",
    "description": null,
    "last_setup_error": null,
    "livemode": false,
    "mandate": null,
    "metadata": [],
    "next_action": null,
    "on_behalf_of": null,
    "payment_method": null,
    "payment_method_options": {
        "card": {
            "request_three_d_secure": "automatic"
        }
    },
    "payment_method_types": [
        "card"
    ],
    "single_use_mandate": null,
    "status": "requires_payment_method",
    "usage": "off_session"
}

I’m using the following PHP code:

$customer = StripeCustomer::create();
$setupIntent = StripeSetupIntent::create([
    'customer' => $customer->id
]);

$this->response($setupIntent, 200);

How can I get at the JSON inside this object? I tried converting it to json, but then I get nothing. Alamofire doesn’t like it the way it is now. Do I need to somehow strip off the “StripeSetupIntent JSON:”?

Advertisement

Answer

You can access the json object body that came back from the API with getLastResponse():

$setupIntent->getLastResponse()->body

edit. You can also use json_encode() to produce the json from the object yourself:

json_encode($setupIntent)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement