导入
生成订单
按照逐步说明,使用 Square API 创建、克隆或计算订单。
代码示例
1CreateOrder 2https://connect.squareupsandbox.com/v2/orders 3 4CloneOrder 5https://connect.squareupsandbox.com/v2/orders/clone 6 7CalculateOrder 8https://connect.squareupsandbox.com/v2/orders/calculate
`CreateOrder` 端点用于创建一个订单,其中包含产品信息以及该购买操作所需的设置。
此端点需要以 Order 对象作为请求参数。该对象包含多个字段,以下是一些重要字段的说明:

请求参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| location_id | 字符串 | 是 | 此订单关联的位置 ID。 |
| reference_id | 字符串 | 否 | 客户指定的 ID,用于将此订单与另一个系统中的实体关联。 |
| line_items | 对象数组 | 否 | 包含订单中各项商品的 OrderLineItem 对象数组。每个对象包含数量、名称、基础价格(base_price_money)和总价(total_money)。 |
| taxes | 对象数组 | 否 | 包含与订单相关的所有税费的 OrderLineItemTax 对象数组。每个对象包含名称、类型、税率百分比及其他字段。 |
| discounts | 对象数组 | 否 | 包含与订单相关的所有折扣的 OrderLineItemDiscount 对象数组。每个对象包含名称、类型、折扣比例、金额(amount_money)及其他字段。 |
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/orders'); 9 10const bodyParameters = JSON.stringify({ 11 'order': { 12 'location_id': '{{LOCATION_ID}}', 13 'reference_id': 'my-order-001', 14 'line_items': [{ 15 'name': '纽约牛排', 16 'quantity': '1', 17 'base_price_money': { 18 'amount': 1599, 19 'currency': 'USD' 20 } 21 }], 22 'taxes': [{ 23 'uid': 'state-sales-tax', 24 'name': '州销售税', 25 'percentage': '9', 26 'scope': 'ORDER' 27 }], 28 'discounts': [{ 29 'uid': 'labor-day-sale', 30 'name': '劳动节促销', 31 'percentage': '5', 32 'scope': 'ORDER' 33 }, 34 { 35 'uid': 'one-dollar-off', 36 'name': '促销 - 减 $1.00', 37 'amount_money': { 38 'amount': 100, 39 'currency': 'USD' 40 }, 41 'scope': 'LINE_ITEM' 42 }] 43 }, 44 'idempotency_key': getRandomString() 45}); 46 47const options = { 48 method: 'POST', 49 headers: headerParameters, 50 body: bodyParameters 51}; 52 53async function invokeEndpoint() { 54 try { 55 const response = await fetch(url, options); 56 // 自定义函数用于打印 API 响应 57 printResponse(response); 58 } catch (error) { 59 console.log(`Error: ${err}`); 60 } 61} 62 63invokeEndpoint();
克隆订单
CloneOrder 端点通过复制指定订单 ID 的现有订单来创建一个新的订单,新订单状态为 DRAFT(草稿)。该端点仅需提供订单 ID 作为必填参数。仅核心字段(如 line_items、taxes 和 discounts)从原订单复制到克隆订单中。以下代码演示了如何克隆刚刚创建的订单。
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/orders/clone'); 9 10const bodyParameters = JSON.stringify({ 11 'order_id': '{{ORDER_ID}}' 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();
计算订单
在电子商务网站上,我们通常希望在确认订单前先预估价格。Square 的 CalculateOrder 端点允许我们实现这一功能。它可让我们预览订单的价格,而无需真正创建订单。
下面的代码展示了如何使用 CalculateOrder 计算一个订单。其接受的参数与 CreateOrder 相同。
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/orders/calculate'); 9 10const bodyParameters = JSON.stringify({ 11 'order': { 12 'location_id': '{{LOCATION_ID}}', 13 'line_items': [{ 14 'name': '商品 1', 15 'quantity': '1', 16 'base_price_money': { 17 'amount': 500, 18 'currency': 'USD' 19 } 20 }, 21 { 22 'name': '商品 2', 23 'quantity': '2', 24 'base_price_money': { 25 'amount': 300, 26 'currency': 'USD' 27 } 28 }], 29 'discounts': [{ 30 'name': '50% 折扣', 31 'percentage': '50', 32 'scope': 'ORDER' 33 }] 34 } 35}); 36 37const options = { 38 method: 'POST', 39 headers: headerParameters, 40 body: bodyParameters 41}; 42 43async function invokeEndpoint() { 44 try { 45 const response = await fetch(url, options); 46 // 自定义函数用于打印 API 响应 47 printResponse(response); 48 } catch (error) { 49 console.log(`Error: ${err}`); 50 } 51} 52 53invokeEndpoint();