0

I have the following response from a payment gateway (JSON):

    {
  "response": {
    "token": "ch_lfUYEBK14zotCTykezJkfg",
    "success": true,
    "amount": 400,
    "currency": null,
    "description": "test charge",
    "email": "user@user.com",
    "ip_address": "203.192.1.172",
    "created_at": "2012-06-20T03:10:49Z",
    "status_message": "Success!",
    "error_message": null,
    "card": {
      "token": "card_nytGw7koRg23EEp9NTmz9w",
      "display_number": "XXXX-XXXX-XXXX-0000",
      "scheme": "master",
      "address_line1": "42 Sevenoaks St",
      "address_line2": null,
      "address_city": "Lathlain",
      "address_postcode": "6454",
      "address_state": "WA",
      "address_country": "Australia"
    },
    "transfer": null
  }
}

I'm trying to get the success => true part in an if statement like so:

<?php
    if($response['success'] == true) {
     // continue transaction
    }
?>

Without any luck, so I'm asking for some help on how I achieve this. It would be useful to be able to extract other key values too.

Thanks

Dean
  • 755
  • 3
  • 15
  • 31

3 Answers3

3

Update

Your $response is already an object, therefore this expression should work:

if ($response->response->success == true) {
    // etc
}

Are you just looking for code that decodes the string representation? If so:

$response = <<<'EOM'
{
  "response": {
    "token": "ch_lfUYEBK14zotCTykezJkfg",
    "success": true,
    "amount": 400,
    "currency": null,
    "description": "test charge",
    "email": "user@user.com",
    "ip_address": "203.192.1.172",
    "created_at": "2012-06-20T03:10:49Z",
    "status_message": "Success!",
    "error_message": null,
    "card": {
      "token": "card_nytGw7koRg23EEp9NTmz9w",
      "display_number": "XXXX-XXXX-XXXX-0000",
      "scheme": "master",
      "address_line1": "42 Sevenoaks St",
      "address_line2": null,
      "address_city": "Lathlain",
      "address_postcode": "6454",
      "address_state": "WA",
      "address_country": "Australia"
    },
    "transfer": null
  }
}
EOM;

$responseData = json_decode($responseText, true);

if ($responseData['response']['success'] == true) {
 // continue transaction
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • There's one more `"response"` key in there. – deceze Mar 22 '13 at 07:36
  • This returns null, I did try this before hand. Code: http://codepad.org/BmAT3bMY $responseData = NULL but $response does have the JSON array. – Dean Mar 22 '13 at 07:40
  • I have also tried http://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json with no success. Any tips? – Dean Mar 22 '13 at 07:50
  • @Dean Updated answer with code that definitely works. – Ja͢ck Mar 22 '13 at 07:56
  • @Jack thanks, but I am getting the `$response` from a payment gateway https://pin.net.au/docs/api/charges#post-charges So I can't really edit the response I get from them. How would you suggest I proceed? – Dean Mar 22 '13 at 07:58
  • @Dean Then, the response is possibly empty and you should check the response headers. – Ja͢ck Mar 22 '13 at 07:59
  • 1
    Perhaps I should say, it returns `Cannot use object of type stdClass as array`. – Dean Mar 22 '13 at 08:35
  • 1
    @Dean Then the response is already a proper object, so `$response->response->success` should be `true`. – Ja͢ck Mar 22 '13 at 08:58
  • Spot on you are. Thanks a bunch. – Dean Mar 22 '13 at 09:01
2

The reason you aren't able to json_decode($response) is because $response is an object, not a JSON string.

The Pin-PHP library will automatically decode all requests for you, as per https://github.com/noetix/pin-php/blob/master/src/Pin/Handler.php#L87

To see if the response has been successful, use the response object as an object:

if ($response->response->success) {
    echo "Success!";
} else {
    echo "Failed :(";
}
noetix
  • 4,773
  • 3
  • 26
  • 47
0

Why not just use a json object like:

$jsonobject = json_encode($responseText);
$response = $jsonobject->response->success;
if($response === true) {
    // code here
}
  • Same deal as http://stackoverflow.com/questions/15565067/extracting-specific-key-value-from-json-array-in-php#comment22061209_15565109 it returns null. – Dean Mar 22 '13 at 07:54