Convert Stripe API response to JSON using stripe-php library

前端 未结 7 1633
迷失自我
迷失自我 2021-01-01 21:06

I\'m accessing customer data from the Stripe API, which I\'d like to convert to JSON. Usually I\'d convert an object to an array and use json_encode() but I don

相关标签:
7条回答
  • 2021-01-01 21:28

    On the latest version, You can use echo $customer->toJSON(); to get the output as JSON.

    0 讨论(0)
  • 2021-01-01 21:40

    The attributes of Stripe_Objects can be accessed like this:

    $customer->attribute;
    

    So to get the customer's card's last4, you can do this:

    $customer->default_card->last4;
    

    However, you'll need to make sure you have the default_card attribute populated. You can retrieve the default_card object at the same time as the rest of the customer by passing the expand argument:

    $customer = Stripe_Customer::retrieve(array(
        "id" => "cus_2dVcTSc6ZtHQcv", 
        "expand" => array("default_card")
    ));
    
    0 讨论(0)
  • 2021-01-01 21:42

    Your top level object contains other object instances - the cast to (array) affects only the top level element. You might need to recursively walk down - but I'd do it differently here given that the classes are serializable:

    $transfer = serialize($myobject);
    

    What are you going to do with the otherwise JSONified data?

    If you're going to transfer an object without the class information you might try to use Reflection:

    abstract class Object {
    
        /**
         * initialize an object from matching properties of another object
         */
        protected function cloneInstance($obj) {
            if (is_object($obj)) {
                $srfl = new ReflectionObject($obj);
                $drfl = new ReflectionObject($this);
                $sprops = $srfl->getProperties();
                foreach ($sprops as $sprop) {
                    $sprop->setAccessible(true);
                    $name = $sprop->getName();
                    if ($drfl->hasProperty($name)) {
                        $value = $sprop->getValue($obj);
                        $propDest = $drfl->getProperty($name);
                        $propDest->setAccessible(true);
                        $propDest->setValue($this,$value);
                    }
                }
            }
            else
                Log::error('Request to clone instance %s failed - parameter is not an object', array(get_class($this)));
            return $this;
        }
    
        public function stdClass() {
            $trg = (object)array();
            $srfl = new ReflectionObject($this);
            $sprops = $srfl->getProperties();
            foreach ($sprops as $sprop) {
                if (!$sprop->isStatic()) {
                    $sprop->setAccessible(true);
                    $name = $sprop->getName();
                    $value = $sprop->getValue($this);
                    $trg->$name = $value;
                }
            }
            return $trg;
        }
    
    }
    

    This is the base class of most of my transferrable classes. It creates a stdClass object from a class, or initializes a class from a stdClass object. You might easily adopt this to your own needs (e.g. create an array).

    0 讨论(0)
  • 2021-01-01 21:44

    PHP has reserved all method names with a double underscore prefix for future use. See https://www.php.net/manual/en/language.oop5.magic.php

    Currently, in the latest php-stripe library, you can convert the Stripe Object to JSON by simpl calling **->toJSON().

    [PREVIOUSLY]

    All objects created by the Stripe PHP API library can be converted to JSON with their __toJSON() methods.

    Stripe::setApiKey("sk_xxxxxxxxxxxxxxxxxxxxxxxxx");
    
    $customer = Stripe_Customer::create(array(
        "card" => $token, 
        "plan" => $plan,  
    ));
    
    $customer_json = $customer->__toJSON();
    

    There is also a __toArray($recursive=false) method. Remember to set true as argument otherwise you will get an array filled with stripe objects.

    Stripe::setApiKey("sk_xxxxxxxxxxxxxxxxxxxxxxxxx");
    
    $customer = Stripe_Customer::create(array(
        "card" => $token, 
        "plan" => $plan,  
    ));
    
    $customer_array = $customer->__toArray(true);
    
    0 讨论(0)
  • 2021-01-01 21:45

    If, like me, you arrived here looking for the python 2.7 solution, simply cast the stripe_object to str(). This triggers the object's inner __str__() function which converts the object into a JSON string.

    E.g.

    charge = stripe.Charge....
    print str(charge)
    
    0 讨论(0)
  • 2021-01-01 21:47
    Stripe::setApiKey("sk_xxxxxxxxxxxxxxxxxxxxxxxxx");
    
    $stripe_response= Stripe_Customer::create(array(
        "card" => $token, 
        "plan" => $plan,  
    ));
    
    //Encoding stripe response to json
    $resposnse_json_ecoded= json_encode($stripe_response);
    //decoding ecoded respose
    $response_decoded = json_decode($resposnse_json_ecoded, true);
    //get data in first level
    $account_id=$response_decoded['id'];
    $individual = $response_decoded['individual'];
    //get  data in second level
    $person_id=$individual['id'];
    
    0 讨论(0)
提交回复
热议问题