Logo
Section 1 / 2

创建和更新折扣

学习如何使用 Eventbrite API 创建和更新活动的票务折扣。

导入

创建和更新折扣

学习如何使用 Eventbrite API 创建和更新活动的票务折扣。

Eventbrite API 允许活动组织者为一个或多个活动的多个票种创建折扣。这些折扣可以在活动列表页面以及结账过程中显示,也可以仅在结账过程中显示。

Eventbrite 提供四种类型的折扣:

  • 公开折扣:仅适用于单个活动,并对外公开展示。
  • 代码折扣:订单持有者使用一个秘密代码来获取折扣。
  • 访问码:通过秘密代码显示隐藏票种,可能包含也可能不包含折扣。
  • 预留折扣:用于已预留座位的折扣。

创建折扣

要为某个活动(或多个活动)的票种(或票种组合)创建折扣,请使用以下 URL,该接口采用 POST 请求方法调用 API:

1https://www.eventbriteapi.com/v3/organizations/{organization_id}/discounts/

该 URL 需要提供 {organization_id}。

请求参数

创建票种折扣时,以下是一些重要输入参数:

对象类型描述
discount.type字符串折扣类型;可选值为:access、hold、public 和 coded
discount.code字符串客户用于获取折扣的代码
discount.amount_off浮点数固定金额折扣,范围为 0.01 至 99999.99
discount.percent_off浮点数百分比折扣,范围为 1.00% 至 100.00%
discount.event_id字符串适用该折扣的活动 ID
discount.ticket_class_ids字符串数组要应用折扣的票种 ID 列表
discount.quantity_available整数折扣可使用的次数
discount.start_date字符串折扣可购买的开始日期
discount.start_date_relative整数从现在起多少秒后折扣上线
discount.end_date字符串折扣的结束日期
discount.end_date_relative整数事件开始前多少秒停止销售折扣
discount.ticket_group_id字符串折扣适用的票种组 ID
discount.hold_ids字符串数组该折扣可解锁的预留 ID 列表
1const endpointUrl = new URL('https://www.eventbriteapi.com/v3/events/{{EVENT_ID}}/ticket_classes/'); 2 3const headerParameters = { 4 'Authorization': 'Bearer {{PRIVATE_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const bodyParameters = JSON.stringify({ 9 "ticket_class": { 10 "name": "示例付费票", 11 "free": "False", 12 "cost": "USD,2000", 13 "capacity": "100", 14 "minimum_quantity": "1", 15 "maximum_quantity": "10", 16 "sales_channels": ["online", "atd"], 17 "delivery_methods": ["electronic"] 18 } 19}); 20 21const options = { 22 method: 'POST', 23 headers: headerParameters, 24 body: bodyParameters, 25}; 26 27async function createTicket() { 28 try { 29 const response = await fetch(endpointUrl, options); 30 printResponse(response); 31 } catch (error) { 32 printError(error); 33 } 34} 35 36// 调用函数发起 API 请求 37createTicket();
1const endpointUrl = new URL('https://www.eventbriteapi.com/v3/organizations/{{ORGANIZATION_ID}}/discounts/'); 2 3const headerParameters = { 4 'Authorization': 'Bearer {{PRIVATE_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const bodyParameters = JSON.stringify({ 9 "discount": { 10 "type": "public", 11 "code": "educative5", 12 "amount_off": "5", 13 "event_id": "{{EVENT_ID}}", 14 "ticket_class_ids": ["{{TICKET_CLASS_ID}}"], 15 "quantity_available": 5 16 } 17}); 18 19const options = { 20 method: 'POST', 21 headers: headerParameters, 22 body: bodyParameters, 23}; 24 25async function createDiscount() { 26 try { 27 const response = await fetch(endpointUrl, options); 28 printResponse(response); 29 } catch (error) { 30 printError(error); 31 } 32} 33 34// 调用函数发起 API 请求 35createDiscount();
  • 第 1 行:我们定义了以 {ORGANIZATION_ID} 作为 URL 参数的端点地址。
  • 第 8–17 行:我们通过 bodyParameters 设置包含折扣对象详细信息的数据体。
  • 第 25–32 行:我们创建了一个名为 createDiscount 的函数,使用 fetch 发起 API 请求,并处理可能出现的异常。自定义函数 printResponse 与 printError 分别用于输出响应和错误信息。
  • 第 35 行:我们调用了 createDiscount 函数。

创建的折扣代码可在结账页面中查看。如上图第一张所示,有两个票种,一个是“全价”,另一个是带有“educative5”折扣的票种。由于我们设置了原价减 5 美元,因此在第二张图中可以看到两张票共减免 10 美元的“折扣:-$10”。

响应字段

上述 API 调用的返回结果是一个包含新创建折扣所有信息的 JSON 对象。此外,还返回两个额外属性:

对象类型描述
id字符串创建的折扣 ID
quantity_sold数字已售出票数总数

更新折扣

已创建的折扣可通过折扣 ID 进行更新。以下 URL 使用 POST 请求方法更新折扣:

1https://www.eventbriteapi.com/v3/discounts/{discount_id}/

该 URL 需要提供 {discount_id}。其余输入参数与创建折扣时相同,返回值为更新后的折扣 JSON 对象。

1const endpointUrl = new URL('https://www.eventbriteapi.com/v3/discounts/{{DISCOUNT_ID}}/'); 2 3const headerParameters = { 4 'Authorization': 'Bearer {{PRIVATE_TOKEN}}', 5 'Content-Type': 'application/json' 6}; 7 8const bodyParameters = JSON.stringify({ 9 "discount": { 10 "code": "educative10", 11 "amount_off": "10" 12 } 13}); 14 15const options = { 16 method: 'POST', 17 headers: headerParameters, 18 body: bodyParameters, 19}; 20 21async function updateDiscount() { 22 try { 23 const response = await fetch(endpointUrl, options); 24 printResponse(response); 25 } catch (error) { 26 printError(error); 27 } 28} 29 30// 调用函数发起 API 请求 31updateDiscount();
  • 第 1 行:我们定义了以 {DISCOUNT_ID} 作为 URL 参数的端点地址。
  • 第 8–13 行:我们通过 bodyParameters 设置折扣的更新值。
  • 第 21–28 行:我们创建了一个名为 updateDiscount 的函数,使用 fetch 发起请求并处理异常情况。printResponse 和 printError 用于输出响应和错误信息。
  • 第 31 行:我们调用了 updateDiscount 函数。

订单与订单问题

获取并删除折扣