【问题标题】:Stripe - Charge user and send invoice via e-mailStripe - 向用户收费并通过电子邮件发送发票
【发布时间】:2026-02-02 09:10:01
【问题描述】:

我有一个使用 Lumen 作为后端的移动应用 (Flutter)。对于付款,我使用 Stripe SDK for PHP。

收费成功后,我需要向用户发送发票。我理解它是这样工作的:

  1. 在 Stripe 中创建客户
  2. 使用customer_id向客户收费
  3. 使用customer_id 创建发票项目
  4. Stripe 将使用客户的电子邮件自动通过电子邮件发送发票

我有两个功能:

public function addCard(Request $request) {
    $user = User::find($user->id);

    if (is_null($user->stripe_id) || empty($user->stripe_id)) {

        $customer = \Stripe\Customer::create([
            "name" => $user->name,
            "email" => $user->email,
            "description" => "Test",
        ]);

        $user->stripe_id = $customer->id;

        $user->save();
    }
}

public function charge(Request $request) {
    $charge = \Stripe\Charge::create([
        'amount' => 2000,
        'currency' => 'eur',
        'customer' => $user->stripe_id,
        'description' => 'Some description',
    ]);

    \Stripe\InvoiceItem::create([
        'customer' => $user->stripe_id,
        'amount' => 2500,
        'currency' => 'usd',
        'description' => 'One-time setup fee',
        'auto_advance' => true,
    ]);
}

它添加了用户,但没有信用卡详细信息。我应该传递哪些参数以便 Stripe 可以为指定用户创建卡令牌?因为它是一个移动应用程序,所以我不能为此目的使用 Stripe.js。

在调用收费函数的那一刻,我收到错误消息:

无法向没有有效卡的客户收费

这是我的第一个支付网关项目......所以非常感谢任何帮助。

【问题讨论】:

  • 当用户输入信用卡详细信息时,您必须使用stripe.js 或 Stripe Checkout 来获取卡令牌。
  • 我对移动SDK不熟悉,但应该有类似的东西。

标签: php flutter stripe-payments lumen


【解决方案1】:

查看这篇文章 (link)。您可以发送交易收据(不是发票)

在您的情况下,我认为您需要通过以下方式从服务器传递电子邮件

stripe.paymentIntents.create(
              {
                amount: 1000,
                currency: USD,
                payment_method: clonedPaymentMethod.id,
                confirmation_method: 'automatic',
                confirm: true,
                receipt_email: 'abc@cde.com',
              }, {
                stripeAccount: stripeVendorAccount
              },
              function(err, paymentIntent) {
                // asynchronously called
                const paymentIntentReference = paymentIntent;

【讨论】:

    【解决方案2】:

    澄清一下,Invoice 是您将发送给您的客户并要求您的客户支付发票 0 的东西。

    如果您已经向您的客户收费,您将不会向您的客户发送发票,因为这最终会向您的客户收取双重费用。相反,您应该根据您创建的费用向您的客户发送receipt1

    说了这么多,

    要通过 Stripe 接受客户的付款,您可以执行以下操作

    1.创建一次性费用

    第 1 步:创建一个代表信用卡信息的令牌。这通常涉及您的客户访问您的应用程序/网站并查看信用卡表格 您可以使用 Stripe Element,这是一个 preBuild 安全 UI,显示信用卡表单

    ...
    ...
    var token = stripe.createToken(cardElement); 
    ...
    

    第 2 步:获取令牌后,将其传递给您的 PHP 服务器端并调用 Stripe Charge API

    // see https://stripe.com/docs/api/charges/create?lang=php#create_charge-source
        $charge = \Stripe\Charge::create([
            'amount' => 2000,
            'currency' => 'eur',
            'source' => $token->id,
            'description' => 'Some description',
        ]);
    

    关注step by step guide 试试以上方法

    2。将信用卡保存给客户并稍后向客户收费

    这与案例 1 非常相似,只是现在您将卡保存给客户并向客户收费。 token 默认是一次性使用,如果您想重复使用该卡,例如您稍后要向客户收取某些订阅费用,您需要将卡保存给客户,即 Attach Card to customer,然后才能重新使用令牌/卡。

    第一步:创建同上的token

    ...
    var token = stripe.createToken(cardElement); 
    ...
    

    第2步:将令牌传递给您的PHP服务器端并将卡保存给客户

    // Create a new customer with the token 
    // https://stripe.com/docs/api/customers/create#create_customer-source 
           $customer = \Stripe\Customer::create([
                "name" => $user->name,
                "email" => $user->email,
                "description" => "Test",
                "source" => $token->id,
            ]);
    
    
    // Or you could attach it to an existing customer 
    \Stripe\Customer::createSource(
      'cus_xxxx',
      [
        'source' => $token->id,
      ]
    );
    

    第 3 步:使用保存的卡向客户收费。

    // When charging this customer, it will use the default card saved at step 2
        $charge = \Stripe\Charge::create([
            'amount' => 2000,
            'currency' => 'eur',
            'customer' => $customer->id, 
            'description' => 'Some description',
        ]);
    
    

    关注step by step guide试试以上方法

    上面显示了使用 Stripe API + Stripe Element 向客户信用卡收费的最简单方法

    这只是起点。熟悉了上面的概念后,可以进一步探索 1. PaymentMethod 和 PaymentIntent 这些是 Stripe 推荐的新 API,但建立在收费 API 之上 https://stripe.com/docs/payments/accept-a-payment https://stripe.com/docs/payments/save-and-reuse

    1. 开票 https://stripe.com/docs/billing/invoices/workflow https://stripe.com/docs/billing/invoices/create-invoice

    这是一款功能强大的计费工具,可让您通过客户保存的卡向客户开具账单,或向客户发送发票以进行付款。

    【讨论】: