导入
创建和更新折扣
学习如何使用 Eventbrite API 创建和更新活动门票的折扣。
Eventbrite API 允许组织所有者为一个或多个活动的多个票务设置折扣。这些折扣可以显示在活动列表中,也可以在结账过程中显示,或者仅在结账过程中显示。
Eventbrite 提供四种类型的折扣:
- 公开折扣:仅用于单个活动,并对外公开展示。
- 代码折扣:订单持有者使用秘密代码获取折扣。
- 访问码:通过秘密代码显示隐藏票务,可能包含也可能不包含折扣。
- 预留折扣:用于已预留座位的折扣。
创建折扣
要为活动的票务创建折扣,请使用以下 URL,该接口采用 POST 方法:
1https://www.eventbriteapi.com/v3/organizations/{organization_id}/discounts/
该 URL 需要提供 {organization_id}。
请求参数
创建活动票务折扣时,以下是一些重要输入参数:
| 对象 | 类型 | 描述 |
|---|---|---|
| discount.type | string | 折扣类型,可选值为 access、hold、public 和 coded。 |
| discount.code | string | 客户用于获取折扣的代码。 |
| discount.amount_off | decimal | 固定金额折扣,取值范围为 0.01 到 99999.99。 |
| discount.percent_off | decimal | 百分比折扣,取值范围为 1.00% 到 100.00%。 |
| discount.event_id | string | 该折扣适用的活动 ID。 |
| discount.ticket_class_ids | array | 要应用折扣的票类 ID 列表。 |
| discount.quantity_available | number | 折扣可使用的次数。 |
| discount.start_date | string | 折扣可购买的开始日期。 |
| discount.start_date_relative | number | 折扣上线时间(以秒为单位,从当前时间起算)。 |
| discount.end_date | string | 折扣的结束日期。 |
| discount.end_date_relative | number | 事件开始前多少秒停止折扣销售。 |
| discount.ticket_group_id | string | 折扣适用的票务组 ID。 |
| discount.hold_ids | array | 可通过此折扣解锁的预留 ID 列表。 |
响应参数
上述 API 调用的返回结果是一个包含折扣详细信息的 JSON 对象。此外,还返回两个额外属性:
| 对象 | 类型 | 描述 |
|---|---|---|
| discount.id | string | 创建的折扣 ID。 |
| discount.quantity_sold | number | 已售出的票数总数。 |
注意:必须先创建一个付费活动并发布后,才能为其创建折扣。请先创建一个带有付费票务的活动并发布,然后才能设置折扣。
下面我们为某个活动的票务添加一个折扣。在下面的例子中,我们设置了代码为 educative5,折扣金额为 5 单位 amount_off。
1import requests 2import json 3 4url = 'https://www.eventbriteapi.com/v3/organizations/{{ORGANIZATION_ID}}/discounts/' 5 6headers = { 7 'Authorization': 'Bearer {{PRIVATE_TOKEN}}', 8 'Content-Type': 'application/json' 9} 10 11values = json.dumps({ 12 "discount": { 13 "type": "public", 14 "code": "educative5", 15 "amount_off": "5", 16 "event_id": "{{EVENT_ID}}", 17 "ticket_class_ids": ["{{TICKET_CLASS_ID}}"], 18 "quantity_available": 5 19 } 20}) 21 22response = requests.request("POST", url, headers=headers, data=values).json() 23print(json.dumps(response, indent=4))
我们可以看到在结账页面上显示了折扣代码。在上方第一张图中,可以看到两张票:一张标有“全价”,另一张使用了 educative5 折扣。由于我们在原价基础上减去了 5 单位金额,第二张图中显示了“折扣:-10”的字样,适用于两张票。
更新折扣
可以通过折扣 ID 端点更新之前创建的折扣。以下 URL 使用 POST 方法更新折扣:
1https://www.eventbriteapi.com/v3/discounts/{discount_id}/
该 URL 需要提供 {discount_id}。其余输入参数与创建折扣时相同,返回更新后的折扣 JSON 对象。
1import requests 2import json 3 4url = 'https://www.eventbriteapi.com/v3/discounts/{{DISCOUNT_ID}}/' 5 6headers = { 7 'Authorization': 'Bearer {{PRIVATE_TOKEN}}', 8 'Content-Type': 'application/json' 9} 10 11values = json.dumps({ 12 "discount": { 13 "code": "educative10", 14 "amount_off": "10" 15 } 16}) 17 18response = requests.request("POST", url, headers=headers, data=values).json() 19print(json.dumps(response, indent=4))