我正在研究Stripe集成,我對從PHP API得到的實際響應感到困惑。我開始相信API參考是準確的,並且響應將是每個方法所示的JSON字符串。我很快發現了顯着的差異。大多數情況下,JSON響應中缺少id字段。此外,響應似乎是一個字符串,一個對象,或者其他一些結構,都在同一時間。Stripe PHP API方法響應的結構是什麼?
這是我的調試代碼。我正在使用最新的Stripe PHP庫1.7.15版。
function var_dump_ret($mixed=null)
{
ob_start();
var_dump($mixed);
$content=ob_get_contents();
ob_end_clean();
return($content);
}
$token=$_POST['stripeToken'];
$customer=Stripe_Customer::create(array(
"card"=>$token,
"plan"=>"agency")
);
$custVarDump=var_dump_ret($customer);
$cDecoded=json_decode($customer);
$Debug="Invidual attributes of JSON decoded customer object:"._EOL;
$Debug.="object:".$cDecoded->object._EOL;
$Debug.="created:".$cDecoded->created._EOL;
$Debug.="id:".$cDecoded->id._EOL;
$Debug.="livemode:".$cDecoded->livemode._EOL;
$Debug.="description:".$cDecoded->description._EOL;
$Debug.="active_card.object:".$cDecoded->active_card->object._EOL;
$Debug.="active_card.last4:".$cDecoded->active_card->last4._EOL;
$Debug.="active_card.type:".$cDecoded->active_card->type._EOL;
$Debug.="active_card.exp_month:".$cDecoded->active_card->exp_month._EOL;
$Debug.="active_card.exp_year:".$cDecoded->active_card->exp_year._EOL;
$Debug.="active_card.fingerprint:".$cDecoded->active_card->fingerprint._EOL;
$Debug.="active_card.country:".$cDecoded->active_card->country._EOL;
$Debug.="active_card.name:".$cDecoded->active_card->name._EOL;
$Debug.="active_card.address_line1:".$cDecoded->active_card->address_line1._EOL;
$Debug.="active_card.address_line2:".$cDecoded->active_card->address_line2._EOL;
$Debug.="active_card.address_city:".$cDecoded->active_card->address_city._EOL;
$Debug.="active_card.address_state:".$cDecoded->active_card->address_state._EOL;
$Debug.="active_card.address_zip:".$cDecoded->active_card->address_zip._EOL;
$Debug.="active_card.address_country:".$cDecoded->active_card->address_country._EOL;
$Debug.="active_card.cvc_check:".$cDecoded->active_card->cvc_check._EOL;
$Debug.="active_card.address_line1_check:".$cDecoded->active_card->address_line1_check._EOL;
$Debug.="active_card.address_zip_check:".$cDecoded->active_card->address_zip_check._EOL;
$Debug.="email:".$cDecoded->email._EOL;
$Debug.="delinquent:".$cDecoded->delinquent._EOL;
//$Debug.="subscription:".$cDecoded->subscription._EOL;
$Debug.="discount:".$cDecoded->discount._EOL;
$Debug.="account_balance:".$cDecoded->account_balance._EOL;
$Debug.="unaltered response from Stripe_Customer::create:"._EOL.$customer._EOL.
"var dump of response:"._EOL.$custVarDump._EOL.
"print_r of json_decode of response:"._EOL.print_r($cDecoded,true)._EOL;
file_put_contents(_LOGFILE,$Debug,FILE_APPEND);
下面是我的JSON解碼客戶對象的invidual屬性的調試文件的內容。執行時,代碼發佈通知。
公告:未定義的屬性:條紋/ subscription.php stdClass的:: $ ID上線51
另外請注意,我不得不註釋掉添加「訂閱」到調試串線由於致命有關stdClass的錯誤。
object:customer
created:1365951909
id:
livemode:
description:
active_card.object:card
active_card.last4:4242
active_card.type:Visa
active_card.exp_month:7
active_card.exp_year:2013
active_card.fingerprint:WTXPLgKDCXyp9xpD
active_card.country:US
active_card.name:charlie
active_card.address_line1:
active_card.address_line2:
active_card.address_city:
active_card.address_state:
active_card.address_zip:
active_card.address_country:
active_card.cvc_check:pass
active_card.address_line1_check:
active_card.address_zip_check:
email:
delinquent:
discount:
account_balance:0
最值得注意的是客戶ID。它在JSON響應中不存在。但是,正如在某些Stripe示例程序中所看到的,可以使用$ customer-> id來訪問它。此外,var_dump輸出表明更多的屬性出現在我無法弄清楚的結構中。整個調試文件位於http://www.helioza.com/stripe/debug.txt。我只顯示了客戶創建方法,但我遇到了與發票類似的問題,無法在Stripe_Invoice :: all或Stripe_Invoice ::即將發生的響應中的任何位置找到發票ID。
問題
1)如何能值由Stripe_Customer返回::創建既是一個字符串,並在同一時間的對象?
2)哪裏可以找到描述API方法返回值的文檔,包括如何訪問每個屬性?
謝謝埃文。這有助於我的一些難題。我仍然不明白爲什麼我可以回顯對象並獲取有效的文本。通常,PHP只是報告「對象」,並拒絕迴應其他任何內容。我還在努力弄清楚迴應的結構。反覆試驗需要很長時間。如果有每種方法的返回結構文檔,肯定會有幫助。 – Helioza
所有對象的屬性都受保護,這就是爲什麼你沒有看到它們。 PHP SDK實際上實現了'ArrayAccess' PHP接口,它允許您通過'$ response ['some_key']'訪問所需的數據,其中的密鑰是https://stripe.com/docs/中列出的所有選項API?LANG = PHP#費用。這很容易。 –