调用Cashier Stripe集成的createOrGetStripeCustomer函数时遇无效对象错误
解决Stripe Cashier createOrGetStripeCustomer()的「Invalid object」错误
问题出在你的stripeAddress()方法返回值格式不符合Stripe的要求。Stripe要求地址是结构化的对象,而非拼接后的字符串。
正确实现方式
修改stripeAddress()方法,返回符合Stripe规范的关联数组:
/** * Get the customer address that should be synced to Stripe. */ public function stripeAddress(): array|null { // 若所有地址字段为空,返回null避免无效对象 if (empty($this->address_line1) && empty($this->address_line2) && empty($this->postal_code) && empty($this->country)) { return null; } return [ 'line1' => $this->address_line1, 'line2' => $this->address_line2, 'postal_code' => $this->postal_code, 'country' => $this->country, ]; }
错误原因
- Stripe的Customer对象中
address字段需要是包含line1、line2、postal_code、country等键的结构化对象,而非字符串。 - 原方法返回的拼接字符串无法被Stripe解析为有效地址对象,因此触发「Invalid object」错误。
- 新增空值判断,避免传递全空的地址结构给Stripe。
内容的提问来源于stack exchange,提问作者GotExx
相关产品推荐
相关产品推荐

