【问题标题】:Laravel Cashier Re-attempt Pending Invoice after Card UpdatesLaravel Cashier 在卡更新后重新尝试未结发票
【发布时间】:2017-05-16 15:00:41
【问题描述】:

我正在使用 Laravel 5.3 和 Cashier。如果客户更新了他们的卡详细信息,我如何检查是否有待处理的发票并要求 Stripe 重新尝试对新卡进行收费?目前,我已经在 Stripe 仪表板中设置了尝试设置。但据我了解,如果客户更新了他们的卡详细信息,Stripe 不会自动尝试向他们收费,它会等待下一个尝试日期再试一次。这就是为什么我想在客户更新卡后立即手动尝试向客户收取待处理发票的费用。我阅读了 Cashier 文档和 Github 页面,但没有涉及到这个案例。

$user->updateCard($token);
// Next charge customer if there is a pending invoice

谁能帮帮我。

【问题讨论】:

    标签: laravel stripe-payments laravel-5.3 laravel-cashier


    【解决方案1】:

    在测试并与 Stripe 支持人员交谈后,我发现了 Laravel Cashier 中当前使用的 updateCard() 方法的问题。

    使用当前的updateCard() 方法,将卡添加到源列表中,然后将新卡设置为default_source。此方法的结果有 2 个结果:

    1. 尽管最近一张被设置为default_source,但多张卡片被添加到列表中

    2. 使用此方法更新卡时,如果有未付发票(即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 文件以进行任何更改不是一个好主意,如果这没有被添加到 Cashier,那么您可以使用 Controller 文件中的以下内容直接从那里进行操作:

    $user = Auth::User();
    
    $customer = $user->asStripeCustomer();
    $token = StripeToken::retrieve($token, ['api_key' => config('services.stripe.secret')]);
    
    if (!($token->card->id === $customer->default_source)) {
      $customer->source = $token;
      $customer->save();
      // Next synchronise user's card details and update the database
      $user->updateCardFromStripe();
    }
    

    【讨论】:

    • 这适用于 laravel 5.7 我想在卡详细信息更新后立即收取付款
    猜你喜欢
    • 2016-02-16
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多