My code is large as I am working on a subscription PHP plugin for WordPress. However, although most is done I now face a glitch that I can't fix. This is the flow:
- User fills in the signup form with shipping, billing and account details
- I create his account (WordPress)
- Once confirmed, I call \Stripe\Customer::create() with his email and card details
- Stripe creates the customer and sends it as JSON response
- I convert the Stripe Customer Object into an array called $object (->__toArray( true )) and then insert each field into my database
This is where my foreach comes into play. I made an error_log( $object ) before the foreach just to make sure there was everything I need and it's correct.
[26-Jan-2016 12:01:58 UTC] Array
(
[id] => cus_7n5hEVJs2btpg8
[object] => customer
[account_balance] => 0
[created] => 1453809716
[currency] =>
[default_source] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[delinquent] =>
[description] =>
[discount] =>
[email] => hello@xfsgfs.com
[livemode] =>
[metadata] => Array
(
)
[shipping] =>
[sources] => Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[id] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[object] => card
[address_city] =>
[address_country] =>
[address_line1] =>
[address_line1_check] =>
[address_line2] =>
[address_state] =>
[address_zip] => W12 8QQ
[address_zip_check] => pass
[brand] => Visa
[country] => US
[customer] => cus_7n5hEVJs2btpg8
[cvc_check] => pass
[dynamic_last4] =>
[exp_month] => 1
[exp_year] => 2019
[fingerprint] => FIYdIZ89jqCdZ9G4
[funding] => credit
[last4] => 4242
[metadata] => Array
(
)
[name] => Lazhar Ich
[tokenization_method] =>
)
)
[has_more] =>
[total_count] => 1
[url] => /v1/customers/cus_7n5hEVJs2btpg8/sources
)
[subscriptions] => Array
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/customers/cus_7n5hEVJs2btpg8/subscriptions
)
)
However, the foreach loop going through each of these $k/$v stops after two or three iterations and my actual page gives me a Chrome error message "err_empty_response.
[26-Jan-2016 11:58:02 UTC] id = cus_7n5diIO2wjeaYX
[26-Jan-2016 11:58:02 UTC] object = customer
My actual loop is like that:
public function reconcile( $object = null, $action = null ) {
$exists = $this->exists();
if( $exists ) {
if( !$object )
$object = $this->retrieve();
if( !is_array( $object ) && !($object instanceof stdClass) )
$object = $object->__toArray( true );
error_log( print_r( $object, true ) );
foreach ( $object as $key => $value ) {
error_log( $key . ' = ' . $this->formatvalue( $value, false, true ) );
$this->$key = $this->formatvalue( $value, false, true );
}
error_log( 'update to be called next.' );
$this->update();
}
}
public function formatvalue( $value, $serialize = true, $nulltoemptystring = false ) {
if( !isset( $value ) )
return $nulltoemptystring ? '' : null;
elseif( is_int( $value ) && $value == '0' )
return 0;
elseif( is_array( $value ) )
return $serialize ? serialize( $value ) : $value;
elseif( is_bool( $value ) )
return $value ? 1 : 0;
elseif( trim( $value ) === '' )
return '';
else
return $value;
}
I am very confused as in why it stops at object (just a string) or account_balance (just an int, usually 0) and never goes to created and the rest.
EDIT: here is a screenshot of my entire error_log
EDIT2: to make sure it is not about these specific array keys or values, I changed the code and the same behaviour happens, it stops after a couple of iterations
[26-Jan-2016 13:02:13 UTC] id = (string) cus_7n6fFVw0w3awiy
[26-Jan-2016 13:02:13 UTC] created = (integer) 1453813333
Results from:
if( $key != 'object' && $key != 'account_balance' ) {
$v = $this->formatvalue( $value, false, true );
error_log( $key . ' = (' . gettype( $v ) . ') ' . $v );
$this->$key = $this->formatvalue( $value, false, true );
}
So it is not even about the keys or values....