0

I know this question has been done several times, but after a few tests I really cant figure ho to do this. I'm implementing SEPA payments with Stripe. I've managed to POST all the infos to Stripe, but having a hard time getting a value within the JSON response.

here it is and what I need is the "mandate_url" value toward the end:

{
  "id": "XXXXXXXXXXX",
  "object": "customer",
  "account_balance": 0,
  "created": XXXXXXXXXXX,
  "currency": null,
  "default_source": "XXXXXXXXXXX",
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "livemode": true,
  "metadata": {},
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [
      {
        "id": "XXXXXXXXXXX",
        "object": "source",
        "amount": null,
        "client_secret": "XXXXXXXXXXX",
        "created": XXXXXXXXXXX,
        "currency": "eur",
        "customer": "XXXXXXXXXXX",
        "flow": "none",
        "livemode": true,
        "metadata": {},
        "owner": {
          "address": {
            "city": "XXXXXXXXXXX",
            "country": "XXXXXXXXXXX",
            "line1": null,
            "line2": null,
            "postal_code": "00000",
            "state": null
          },
          "email": null,
          "name": "John Doe",
          "phone": null,
          "verified_address": null,
          "verified_email": null,
          "verified_name": null,
          "verified_phone": null
        },
        "status": "chargeable",
        "type": "sepa_debit",
        "usage": "reusable",
        "sepa_debit": {
          "bank_code": "XXXXXXXXXXX",
          "branch_code": "XXXXXXXXXXX",
          "country": "XXXXXXXXXXX",
          "fingerprint": "XXXXXXXXXXX",
          "last4": "XXXXXXXXXXX",
          "mandate_reference": "XXXXXXXXXXX",
          "mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
        }
      }
    ],
    "has_more": false,
    "total_count": 1,
    "url": "XXXXXXXXXXX"
  },
  "subscriptions": {
    "object": "list",
    "data": [],
    "has_more": false,
    "total_count": 0,
    "url": "XXXXXXXXXXX"
  }
}

isn't there any way to get it without creating an enormous foreach?

thanks :)

Stefano
  • 47
  • 1
  • 6

1 Answers1

0

first off you're json was invalid as you had several instances of "created": XXXXXXXXXXX, so below is corrected json and example of using json_decode

<?php


$json = '{
    "id": "XXXXXXXXXXX",
    "object": "customer",
    "account_balance": 0,
    "created": "XXXXXXXXXXX",
    "currency": null,
    "default_source": "XXXXXXXXXXX",
    "delinquent": false,
    "description": null,
    "discount": null,
    "email": null,
    "livemode": true,
    "metadata": {},
    "shipping": null,
    "sources": {
        "object": "list",
        "data": [{
            "id": "XXXXXXXXXXX",
            "object": "source",
            "amount": null,
            "client_secret": "XXXXXXXXXXX",
            "created": "XXXXXXXXXXX",
            "currency": "eur",
            "customer": "XXXXXXXXXXX",
            "flow": "none",
            "livemode": true,
            "metadata": {},
            "owner": {
                "address": {
                    "city": "XXXXXXXXXXX",
                    "country": "XXXXXXXXXXX",
                    "line1": null,
                    "line2": null,
                    "postal_code": "00000",
                    "state": null
                },
                "email": null,
                "name": "John Doe",
                "phone": null,
                "verified_address": null,
                "verified_email": null,
                "verified_name": null,
                "verified_phone": null
            },
            "status": "chargeable",
            "type": "sepa_debit",
            "usage": "reusable",
            "sepa_debit": {
                "bank_code": "XXXXXXXXXXX",
                "branch_code": "XXXXXXXXXXX",
                "country": "XXXXXXXXXXX",
                "fingerprint": "XXXXXXXXXXX",
                "last4": "XXXXXXXXXXX",
                "mandate_reference": "XXXXXXXXXXX",
                "mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
            }
        }],
        "has_more": false,
        "total_count": 1,
        "url": "XXXXXXXXXXX"
    },
    "subscriptions": {
        "object": "list",
        "data": [],
        "has_more": false,
        "total_count": 0,
        "url": "XXXXXXXXXXX"
    }
}';

$data = json_decode($json);
$mandate_url = $data->sources->data[0]->sepa_debit->mandate_url);
echo $mandate_url;


?>

Alternatively you could get the json into an associative array and use the following syntax

$data = json_decode($json, true);
$mandate_url = $data['sources']['data'][0]['sepa_debit']['mandate_url'];
echo $mandate_url;
Jpsh
  • 1,697
  • 12
  • 17