php - Stripe,用户无法下载他们的发票

标签 php mysql stripe-payments

我正在使用 Stripe 支付网关,到目前为止一切正常,除了我的用户可以在我的网站上下载他们的发票的选项。可以在 Stripe 仪表板内下载发票,但这不是我需要的。我的用户付款后,他们应该能够将发票下载到我的系统。

目前我正在使用 stripe webhooks。用户付款,数据保存在我的数据库中。这是我的代码的样子:

\Stripe\Stripe::setApiKey("$stripe_secret_key_test");
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// Update database with new billing entry
if ($event_json->type == 'invoice.payment_succeeded') {
    $invoice_id = $event_json->data->object->id;
    $customer = $event_json->data->object->customer;
    $date = $event_json->data->object->date;
    $price = $event_json->data->object->amount_paid;
    $user = $event_json->data->object->lines->data[0]->metadata->userid;
    $stripe_sub_id = $event_json->data->object->lines->data[0]->id;
    $plan_id = $event_json->data->object->lines->data[0]->plan->id;
    $stamp_created = $event_json->data->object->lines->data[0]->plan->created;
    $comments = $event_json->data->object->lines->data[0]->plan->nickname;
    $period_start = $event_json->data->object->lines->data[0]->period->start;
    $period_end = $event_json->data->object->lines->data[0]->period->end;

    $content=array("invoice"=>$invoice_id,
        "user"=>$user,
        "stripe_cust_id"=>$customer,
        "stripe_sub_id"=>$stripe_sub_id,
        "idea_id"=>0,
        "billing_date"=>$date,
        "plan"=>$plan_id,
        "price"=>$price,
        "stamp_created"=>$stamp_created,
        "period_start"=>$period_start,
        "period_end"=>$period_end,
        "status"=>'paid',
        "printed"=>0);
    insertGWInfo('billing',$content);

    $content=array("user"=>$user,
        "trans_time"=>date("Y-m-d H:i:s"),
        "ip"=>$_SERVER['REMOTE_ADDR'],
        "trans_type"=>1,
        "cash_amount"=>$price,
        "comments"=>"Subscription for ".$comments."",
        "idea_id"=>0);
    insertGWInfo('user_accounting',$content);
}

现在我创建了一个表,我从数据库中提取这些条目并将它们显示给我的客户。我现在想要一个带有“下载发票”的按钮,我的客户可以在其中下载每月出现的订阅发票。

知道我该怎么做吗?我有 invoice_id,但我从哪里获得下载发票的 URL?我无法在 stripe 文档中找到任何内容。非常感谢任何帮助。

编辑: 以下是 invoice.payment_suceeded 的 JSON 结果:

"{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2018-02-28",
  "data": {
    "object": {
      "id": "in_00000000000000",
      "object": "invoice",
      "amount_due": 1000,
      "amount_paid": 1000,
      "amount_remaining": 0,
      "application_fee": null,
      "attempt_count": 1,
      "attempted": true,
      "billing": "charge_automatically",
      "charge": "_00000000000000",
      "closed": true,
      "currency": "eur",
      "customer": "cus_00000000000000",
      "date": 1526455801,
      "description": null,
      "discount": null,
      "due_date": null,
      "ending_balance": 0,
      "forgiven": false,
      "lines": {
        "data": [
          {
            "id": "sub_Cs4DUPJKcAJ7cg",
            "object": "line_item",
            "amount": 1000,
            "currency": "eur",
            "description": "1 standard-inv × Standard Investor (at €10.00 / month)",
            "discountable": true,
            "livemode": false,
            "metadata": {
            },
            "period": {
              "end": 1531726201,
              "start": 1529134201
            },
            "plan": {
              "id": "3",
              "object": "plan",
              "aggregate_usage": null,
              "amount": 1000,
              "billing_scheme": "per_unit",
              "created": 1526375755,
              "currency": "eur",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {
              },
              "nickname": "Standard Plan for Investors",
              "product": "prod_CrihTVfgRHgImD",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "proration": false,
            "quantity": 1,
            "subscription": null,
            "subscription_item": "si_Cs4DrvYN2FlCbS",
            "type": "subscription"
          }
        ],
        "has_more": false,
        "object": "list",
        "url": "/v1/invoices/in_1CSK5FDsOByhb3e8m8Z1BooY/lines"
      },
      "livemode": false,
      "metadata": {
      },
      "next_payment_attempt": null,
      "number": "774C629-0001",
      "paid": true,
      "period_end": 1526455801,
      "period_start": 1526455801,
      "receipt_number": null,
      "starting_balance": 0,
      "statement_descriptor": null,
      "subscription": "sub_00000000000000",
      "subtotal": 1000,
      "tax": null,
      "tax_percent": null,
      "total": 1000,
      "webhooks_delivered_at": 1526455810
    }
  }
}"

最佳答案

Stripe 现在确实支持这个。通过 API 列出发票并访问发票的 invoice_pdf 属性可获得 PDF 链接。

以下是获取一系列发票及其 PDF 链接的方法:

const invoicesResponse = await stripe.invoices.list({ customer: customerStripeId });
const invoices = invoicesResponse.map(invoice => ({
    invoiceId: invoice['id'],
    pdf: invoice['invoice_pdf'],
  }));
return invoices;

这是 API 文档: https://stripe.com/docs/api/invoices/object#invoice_object-invoice_pdf感谢@avelis 的建议。

关于php - Stripe,用户无法下载他们的发票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366702/

相关文章:

javascript - 两种形式,一种提交按钮

php - 播放特定格式的 m4a

mysql - Entity Framework 如何将 "Image"添加到数据库

javascript - 尝试创建一个 AngularJS 函数来删除 MySQL 条目

php - 将函数从 Javascript 转换为 PHP

mysql - 如何优化 MySQL 以处理小型数据库,即 < 100mb?

stripe-payments - Stripe Checkout - 如何使用 webhooks 处理用户 ID

google-apps-script - 将 Stripe API 语法转换为 Google Apps 脚本

java - java Stripe api 调用什么函数来完成快速帐户连接?

php - Wordpress 子主题无法正常工作