条纹:在我的网站上下载发票

时间:2018-05-16 09:09:06

标签: php mysql stripe-payments

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

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

"{
  "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
    }
  }
}"

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

知道我怎么能这样做吗?我有invoice_id,但在哪里可以获得下载发票的URL?我无法在条纹文档中找到任何内容。任何帮助都会非常感激。

编辑: 以下是invoice.payment_suceeded:

的JSON结果
$(document).ready(function() {
    var table = $('#example').DataTable();
 
    $('#example tbody').on( 'click', 'tr', function () {
        alert('You clicked on row ' + table.row(this).index());
        console.log('data: ' + table.row(this).data());
    } );
} );

4 个答案:

答案 0 :(得分:8)

条纹现在支持此功能。通过通过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;

答案 1 :(得分:1)

Stripe目前不支持通过API将发票下载为PDF格式,而只能在仪表板中使用。

目前,最佳解决方案是建立自己的发票收据/ pdf。您可以通过Invoice检索API对象,该对象会告诉您发票的用途:期间,金额,付款时间,发票中的内容等。然后您可以生成自己的收据为您的客户提供支持。

答案 2 :(得分:1)

这是可能的:)与Python API一样具有魅力。

import stripe
import requests

stripe_keys = {'secret_key': 'XXXxxxx','publishable_key': 'XXXxxxx'}
stripe.api_key = stripe_keys['secret_key']

stripe_subscription_id = 'XXXXXXX'
invoices = stripe.Invoice.list(subscription=stripe_subscription_id)
for inv in invoices:
    pdf          = inv.invoice_pdf
    invoice_file = requests.get(pdf)
    open('C://User//XXXXX//Desktop//invoice.pdf', 'wb').write(invoice_file.content)

答案 3 :(得分:0)

现在有一个更简单的方法。以下是我使用 Stripe 的默认方法下载发票 pdf 的步骤。

  1. 找到我想下载 pdf 格式发票的客户

    const findCustomerByEmail = async (email) => {
      try {
        const customer = await stripe.customers.list({
          email,
          limit: 1,
        });
        if (customer.data.length !== 0) {
          return customer.data[0].id;
        }
      } catch (e) {
        return (e);
      }
    };
    
  2. 既然有了客户,我就找到用户的所有发票:

     exports.getInvoices = async (req, res, next) => {
          const cusStripeId = await findCustomerByEmail(req.user.email);
    
          const invoices = await stripe.invoices.list({
            limit: 3000,
            customer: cusStripeId,
    
          });
          res.status(httpStatus.OK).json(invoices);
     };
    
  3. 现在我有了这个用户的发票清单。我可以遍历所有发票。

<块引用>

invoices.data[0].invoice_pdf

invoices 对象是一个数组,data[0].invoice_pdf 有一个可以下载 pdf 的链接。作为替代方案,我们也可以将发票视为 html 页面,无需下载,如下所示。

<块引用>

invoices.data[0].hosted_invoice_url