bbbbbbbbbb
bbbbbbbbbb

Reputation: 319

PayPal PHP SDK REST API Response not JSON

I get an object with nested arrays of nested objects when I make an API call. In the SDK I have the header set to accept: application/json. I am wondering why I am getting objects and arrays back?

Here is a partial part of the response:

PayPal\Api\CreditCard Object
(
    [_propMap:PayPal\Common\PPModel:private] => Array
        (
            [type] => amex
            [number] => xxxxxxxxxxx0005
            [expire_month] => 5
            [expire_year] => 2015
            [cvv2] => 1234
            [first_name] => 
            [last_name] => 
            [payer_id] => 3zIVtTFQ7UdKjP5mssjtzoUo6NvrsExl466oPC4Mm8nwOjI6BS
            [id] => CARD-35X96613EN689504VKKCA4RA
            [state] => ok
            [valid_until] => 2015-06-01T00:00:00Z
            [create_time] => 2013-11-13T23:41:56Z
            [update_time] => 2013-11-13T23:41:56Z
            [links] => Array
                (
                    [0] => PayPal\Api\Links Object
                        (
                            [_propMap:PayPal\Common\PPModel:private] => Array
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/vault/credit-card/CARD-35X96613EN689504VKKCA4RA
                                    [rel] => self
                                    [method] => GET
                                )

   )

[1] => PayPal\Api\Links Object

... and so on Code that creates this

public static function get($creditCardId, $apiContext = null) { 
    if (($creditCardId == null) || (strlen($creditCardId) <= 0)) { 
        throw new \InvalidArgumentException("creditCardId cannot be null or empty"); 
    }
    $payLoad = ""; 
    if ($apiContext == null) { 
         $apiContext = new ApiContext(self::$credential); 
    } 
    $call = new PPRestCall($apiContext); 
    print_r($call); 
    die;
    $json = $call->execute(array('PayPal\Rest\RestHandler'), "/v1/vault/credit-card/$creditCardId", "GET", $payLoad); 
    $ret = new CreditCard(); 
    $ret->fromJson($json); 
    return $ret;
}

Upvotes: 2

Views: 1371

Answers (3)

Juni Brosas
Juni Brosas

Reputation: 1486

json_decode( $call->execute(array('PayPal\Rest\RestHandler'), "/v1/vault/credit-card/$creditCardId", "GET", $payLoad) );

Upvotes: 0

bbbbbbbbbb
bbbbbbbbbb

Reputation: 319

Ok, so thank you Machavity for the help. The issue was that in the function that I posted above, it takes the JSON response and converts it to some wierd object thing with:

$ret->fromJson($json);

I removed that line and I now get JSON. I still wonder why having that object I intitially posted would be helpful and where it could be used. I also wonder why the SDK documentation doesn't explicitly state that the SDK does not return JSON as the final format. I feel it would save a lot of time if PayPal informed a developer that the response gets converted to an object, so we wouldn't have to search through thousands of lines of code to figure it out.

Upvotes: 3

Machavity
Machavity

Reputation: 31614

It's likely the SDK is accepting JSON on the communications layer and then running json_decode to pass you back an object you can work with. My own API system converts the response back into a PHP array.

Upvotes: 0

Related Questions