經過測試並與Stripe支持交談之後,我發現Laravel Cashier中使用的當前updateCard()
方法存在問題。
使用當前的updateCard()
方法,將卡添加到來源列表中,然後將新卡設置爲default_source
。這種方法的結果有2個結果:
多卡被添加到列表中,雖然最近一個被設置爲default_source
使用此方法時更新卡,如果有任何未付發票(即在past_due
狀態下的發票),它們不會自動收費。
爲了條紋重新嘗試在past_due
狀態在所有發票收費客戶,將source
參數需要傳遞。所以我創建了一個這樣的新方法:
public function replaceCard($token)
{
$customer = $this->asStripeCustomer();
$token = StripeToken::retrieve($token, ['api_key' => $this->getStripeKey()]);
// If the given token already has the card as their default source, we can just
// bail out of the method now. We don't need to keep adding the same card to
// a model's account every time we go through this particular method call.
if ($token->card->id === $customer->default_source) {
return;
}
// Just pass `source: tok_xxx` in order for the previous default source
// to be deleted and any unpaid invoices to be retried
$customer->source = $token;
$customer->save();
// Next we will get the default source for this model so we can update the last
// four digits and the card brand on the record in the database. This allows
// us to display the information on the front-end when updating the cards.
$source = $customer->default_source
? $customer->sources->retrieve($customer->default_source)
: null;
$this->fillCardDetails($source);
$this->save();
}
我已經爲此添加了一個Pull request。由於編輯Billable
文件直接進行任何更改不是一個好主意,如果這不添加到收銀臺,那麼您可以在控制器文件中直接使用以下內容: