我正在研究 Stripe 集成,我对从 PHP API 得到的实际响应感到困惑。我开始相信 API 参考是准确的,并且响应将是一个 JSON 字符串,如每个方法所示。我很快发现了显着的差异。大多数情况下,JSON 响应中缺少 id 字段。此外,响应似乎同时是一个字符串、一个对象,也许还有其他一些结构。
这是我的调试代码。我正在使用最新的 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 解码的客户对象的个人属性。执行时,代码会发布一条通知。
注意:未定义的属性:第 51 行的 stripe/subscription.php 中的 stdClass::$id
另请注意,由于有关 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::upcoming 响应中的任何地方都找不到发票 ID。
问题
1) Stripe_Customer::create 返回的值如何同时是字符串和对象?
2) 我在哪里可以找到描述 API 方法返回值的文档,包括如何访问每个属性?