Logo
Section 1 / 2

创建发票

了解 Square 如何使用发票 API 实现发票管理。

导入

创建发票

了解 Square 如何使用发票 API 实现发票管理。

**发票 API** 可用于创建发票以请求支付订单款项。将发票 API 集成到应用程序中,可以帮助卖家更简单、更轻松地管理发票,从而在以下方面降低开发成本:

  • 所有发票都需要订单 ID 和客户 ID,这使得其可与订单 API 和客户 API 实现内置集成。
  • Square 会自动向客户发送提醒,因此无需自行发送提醒或收据。
  • Square 会生成并托管一个网页页面,客户可随时在线完成付款。

Course Image

要创建发票,首先需要创建一个订单。我们使用 create_order 来创建订单。

注意: 只有通过订单 API 创建的订单才能用于创建发票。

让我们开始操作,如下所示创建一个订单:

1result = client.orders.create_order( 2 body = { 3 "order": { 4 "location_id": "{{LOCATION_ID}}", 5 "state": "OPEN", 6 "line_items": [{ 7 "name": "亚麻床单", 8 "quantity": "1", 9 "base_price_money": { 10 "amount": 799, 11 "currency": "USD" 12 } 13 }] 14 } 15 } 16) 17 18if result.is_success(): 19 pprint(result.body) 20elif result.is_error(): 21 pprint(result.errors)

创建订单后,创建发票的主要流程主要包括以下两个步骤:

  1. 使用发票 API 中的 CreateInvoice 端点创建草稿发票。
  2. 使用发票 API 中的 PublishInvoice 端点发布发票。

第一步:创建草稿发票

创建发票时,需提供以下信息:

参数类型必填描述
invoiceInvoice必填要创建的发票。
idempotency_keyString可选用于标识 CreateInvoice 请求的唯一字符串。若未提供或为空,则该请求被视为独立请求。字符串最大长度为 128 个字符。

invoice 可包含多个字段,用于描述各种信息。部分字段说明如下:

  • order_id:关联订单的 ID。
    • 该订单必须通过订单 API 创建。
    • 订单状态必须为 OPEN。
    • 该订单不能已关联其他发票。
    • 一张发票只能关联一个订单。
  • customer_id:接收发票的客户 ID。
    • 该客户可以与创建订单的客户不同。
  • payment_requests:一个或多个付款请求,其总金额等于订单的 total_money。
  • delivery_method:Square 用于向客户发送发票、提醒和收据的交付方式。
  • accepted_payment_methods:客户在 Square 托管的发票页面上可用于付款的支付方式。

如前所述,我们可以指定接收发票的客户。为此,我们先创建一个客户:

注意: Square 也会通过电子邮件发送发票。因此,请在以下代码中使用真实的邮箱地址,以便您能收到发票。

1result = client.customers.create_customer( 2 body = { 3 "email_address": "<your-email-address>" 4 } 5) 6 7if result.is_success(): 8 pprint(result.body) 9elif result.is_error(): 10 pprint(result.errors)

客户创建完成后,即可开始创建发票。以下代码为之前创建的订单创建一张发票:

1result = client.invoices.create_invoice( 2 body = { 3 "invoice": { 4 "order_id": "{{ORDER_ID}}", 5 "primary_recipient": { 6 "customer_id": "{{CUSTOMER_ID}}" 7 }, 8 "payment_requests": [{ 9 "request_type": "BALANCE", 10 "due_date": "2099-01-14" 11 }], 12 "delivery_method": "EMAIL", 13 "title": "我的第一张发票", 14 "accepted_payment_methods": { 15 "card": True, 16 "square_gift_card": True 17 } 18 } 19 } 20) 21 22if result.is_success(): 23 pprint(result.body) 24elif result.is_error(): 25 pprint(result.errors)

第二步:发布发票

创建草稿发票后,可通过发票 API 的 PublishInvoice 发布发票。该操作需要发票 ID 和当前版本号,版本号可从 CreateInvoice 响应返回的发票信息中获取。

1result = client.invoices.publish_invoice( 2 invoice_id = "{{INVOICE_ID}}", 3 body = { 4 "version": {{VERSION}} 5 } 6) 7 8if result.is_success(): 9 pprint(result.body) 10elif result.is_error(): 11 pprint(result.errors)

发票发布后,Square 将托管发票页面,并在响应中返回其 URL。该 URL 可在响应字段 public_url 中找到。

Square Sandbox 支持多种 Square 支持的信用卡号码。我们可使用以下卡号来模拟 Square 支持国家中的客户端支付场景。可任意填写有效的邮政编码,卡有效期可设置为任意未来月份和年份。

品牌卡号CVV
Visa4111 1111 1111 1111111
Mastercard5105 1051 0510 5100111
Discover6011 0000 0000 0004111
Diners Club3000 000000 0004111
JCB3569 9900 1009 5841111
American Express3400 000000 000091111
China UnionPay6222 9888 1234 0000123
Square Gift Card7783 3200 0000 0000N/A

处理订单

获取发票