Logo
Section 1 / 2

创建发票

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

导入

创建发票

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

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

  • 所有发票均需提供订单 ID 和客户 ID,这使得其可与订单 API 和客户 API 实现内置集成。
  • Square 会自动向客户发送提醒和收据,无需开发者自行发送通知或收据。
  • Square 会生成并托管一个网页页面,客户可方便地通过该页面完成付款。

创建发票的主要工作流程包含以下两个步骤:

  • 步骤 1: 使用发票 API 中的 CreateInvoice 端点创建草稿发票:               https://connect.squareupsandbox.com/v2/invoices
  • 步骤 2: 使用发票 API 中的 PublishInvoice 端点发布发票:               https://connect.squareupsandbox.com/v2/invoices/<invoice-id>/publish

参数类型必填描述
order_id字符串是关联订单的 ID。请注意:订单必须通过订单 API 创建;订单状态必须为 OPEN;订单不能关联其他发票;一张发票只能关联一个订单。
payment_requests对象数组是包含多个 InvoicePaymentRequest 对象的数组。每个对象必须包含 request_type 和 due_date 两个字段。
accepted_payment_methods对象是表示客户可在 Square 托管的发票页面上使用的支付方式的 InvoiceAcceptedPaymentMethods 对象。支持选项包括:card(信用卡)、bank_account(银行账户)和 square_gift_card(Square礼品卡)。
delivery_method字符串是Square 用于向客户发送发票、提醒和收据的交付方式,例如 EMAIL(电子邮件)或 SMS(短信)。
primary_recipient对象否接收发票的客户 ID。该客户可以与下单客户不同。

1import fetch from 'node-fetch'; 2 3const headerParameters = { 4 'Authorization': 'Bearer {{ACCESS_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const url = new URL('https://connect.squareupsandbox.com/v2/customers'); 9 10const bodyParameters = JSON.stringify({ 11 'email_address': '<your-email-address>' 12}); 13 14const options = { 15 method: 'POST', 16 headers: headerParameters, 17 body: bodyParameters 18}; 19 20async function invokeEndpoint() { 21 try { 22 const response = await fetch(url, options); 23 // 自定义函数用于打印 API 响应 24 printResponse(response); 25 } catch (error) { 26 console.log(`Error: ${err}`); 27 } 28} 29 30invokeEndpoint();
1import fetch from 'node-fetch'; 2 3const headerParameters = { 4 'Authorization': 'Bearer {{ACCESS_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const url = new URL('https://connect.squareupsandbox.com/v2/invoices'); 9 10const body = { 11 'invoice': { 12 'order_id': 'TODO', 13 'primary_recipient': { 14 'customer_id': '{{CUSTOMER_ID}}' 15 }, 16 'payment_requests': [{ 17 'request_type': 'BALANCE', 18 'due_date': '2099-01-14' 19 }], 20 'delivery_method': 'EMAIL', 21 'title': '我的第一张发票', 22 'accepted_payment_methods': { 23 'card': true, 24 'square_gift_card': true 25 } 26 } 27}; 28 29const options = { 30 method: 'POST', 31 headers: headerParameters 32}; 33 34(async () => { 35 try { 36 const order = await createOrder('亚麻床单', '1', 799, 'USD'); 37 console.log('订单 ID: ' + order.id + ' 总金额: ' + order.total_money.amount + order.total_money.currency + '\n'); 38 39 body.invoice.order_id = order.id; 40 options.body = JSON.stringify(body); 41 42 createInvoice(); 43 } catch (err) { 44 console.log(`Error: ${err}`); 45 } 46})(); 47 48async function createInvoice() { 49 try { 50 const response = await fetch(url, options); 51 // 自定义函数用于打印 API 响应 52 printResponse(response); 53 } catch (error) { 54 console.log(`Error: ${err}`); 55 } 56}

字段类型描述
id字符串唯一标识此发票的 ID。
version整数发票版本号,每次更新时递增。
invoice_number字符串发票编号,在所在位置内唯一,显示在发票上。
title字符串发票标题。
status字符串发票状态。可能值包括:DRAFT(草稿)、UNPAID(未支付)、SCHEDULED(已安排)、PARTIALLY_PAID(部分支付)、PAID(已支付)、PARTIALLY_REFUNDED(部分退款)、REFUNDED(已退款)、CANCELED(已取消)、FAILED(失败)、PAYMENT_PENDING(支付待处理)。

参数类型必填描述
invoice_id字符串是要发布的发票 ID。
version整数是发票当前版本号。

1import fetch from 'node-fetch'; 2 3const headerParameters = { 4 'Authorization': 'Bearer {{ACCESS_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const url = new URL('https://connect.squareupsandbox.com/v2/invoices/<invoice-id>/publish'); 9 10const bodyParameters = JSON.stringify({ 11 'version': null 12}); 13 14const options = { 15 method: 'POST', 16 headers: headerParameters, 17 body: bodyParameters 18}; 19 20async function invokeEndpoint() { 21 try { 22 const response = await fetch(url, options); 23 // 自定义函数用于打印 API 响应 24 printResponse(response); 25 } catch (error) { 26 console.log(`Error: ${err}`); 27 } 28} 29 30invokeEndpoint();

响应字段

发票发布后,PublishInvoice API 返回相同的发票对象,但包含更新信息。Square 会托管发票页面,并在响应中返回其 URL,该 URL 可通过 public_url 字段获取。


品牌卡号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

处理订单

获取发票