SNaRe
SNaRe

Reputation: 2067

How can I reach an element in an Object?

I'm trying to reach an element in an object array. But I couldn't succeed. Let's think this as an object called $result How can I reach maskedNumber?

Braintree_Result_Successful Object
(
    [success] => 1
    [_returnObjectName:private] => customer
    [customer] => Braintree_Customer Object
        (
            [_attributes:protected] => Array
                (
                    [creditCards] => Array
                        (
                            [0] => Braintree_CreditCard Object
                                (
                                    [_attributes] => Array
                                        (
                                            [maskedNumber] => ***********5897

Upvotes: 0

Views: 745

Answers (5)

David Fairbanks
David Fairbanks

Reputation: 648

I know this is old, but this might help some.

Braintree has methods for returning the protected information that's returned from functions like Braintree_Customer::create();

$result = Braintree_Customer::create(array(
    'firstName'     => $_POST['first_name'],
    'lastName'      => $_POST['last_name'],
    'email'         => $_POST['email'],
    'creditCard'    => array(
        'cardholderName'    => $_POST['cardholder_name'],
        'number'            => $_POST['number'],
        'expirationMonth'   => $_POST['month'],
        'expirationYear'    => $_POST['year'],
        'cvv'               => $_POST['cvv'],
        'billingAddress'    => array(
            'postalCode'        => $_POST['postal_code']
        )
    )
));

var_dump($result->customer->__get('id'));
var_dump($result->customer->__get('creditCards'));

The _attributes of customer are protected, but the get function returns them. This method does not require re-requesting data from Braintree.

Upvotes: 1

SNaRe
SNaRe

Reputation: 2067

I solved the problem by asking braintreepayments. They said that I could retrieve this data after I add user to braintree. But my solution is if I really needed this at the very beginning would be to take it with REGEX. For people who are looking for a great online payment company I suggest you to go with braintree

Upvotes: 1

pb149
pb149

Reputation: 2298

Since the _attributes property of Braintree_Customer is protected you will need to define an accessor method. The other _attributes property of Braintree_CreditCard also looks like it's supposed to be protected, so I've assumed an identical accessor should exist:

$cards = $object->customer->getAttribute('creditCards');
$number = $cards[0]->getAttribute('maskedNumber');

Accessor method to be placed in both classes:

function getAttribute($attribute) {
    return $this->_attributes[$attribute];
}

Edit:

Just to improve upon my original answer a little, I would put some decent error checking in an actual accessor method.

function getAttribute($attribute) {
    if (isset($this->_attributes[$attribute])) {
      return $this->_attributes[$attribute];
    }
    return NULL;
}

You could also consider using the magic methods __get() and __set() to act as getters and setters.

Upvotes: 2

SERPRO
SERPRO

Reputation: 10067

with $result->customer you should get the *Braintree_Customer* Object and then in that object you should have methods to retrieve the cards as those methods are protected and cannot be accessed directly. Something like

$customer = $result->customer;
foreach($customer->getCreditCards() as $card)
{
   echo $card->getMaskedNumber(); // You will need to create that method too
}

Example of getCreditCards method:

Class Braintree_Customer
{
  protected $creditCards;

  function getCreditCards()
  {
     return $creditCards;
  }

  ...
}

Upvotes: 0

xkeshav
xkeshav

Reputation: 54084

try

$Result->customer->_attributes['creditCards'][0]->_attributes['maskedNumber']

Upvotes: 0

Related Questions