# 应用兑换码

将兑换码应用于购物车。应用后，系统会重新计算购物车总金额，以体现整车或指定商品的折扣；也可能向购物车添加赠品。折扣会在结算时生效，赠品则在支付成功后发放。付款前，用户可移除兑换码；移除后，折扣将被取消，赠品也会从购物车中移除。

Endpoint: POST /v2/project/{project_id}/promocode/redeem
Version: 2.0.0
Security: AuthForCart

## Path parameters:

  - `project_id` (integer, required)
    项目ID。您可以在发布商帐户的项目名称旁找到此参数；使用项目时，也可以在浏览器地址栏中找到此参数。URL格式如下：https://publisher.xsolla.com//projects/。
    Example: 44056

## Request fields (application/json):

  - `coupon_code` (string)
    兑换码的唯一代码。包含字母和数字。
    Example: "SUMMER2021"

  - `cart` (object,null)

  - `cart.id` (string, required)
    购物车ID。

  - `selected_unit_items` (object)
    用户选择作为赠品的平台对应游戏Key。将[收到的](/zh/api/liveops/promotions-coupons/get-coupon-rewards-by-code#promotions-coupons/get-coupon-rewards-by-code/t=response&c=200&path=bonus/item) bonus.item.sku作为键，将所选bonus.item.unit_items.sku作为其值。

## Response 200 fields (application/json):

  - `cart_id` (string)
    购物车ID。
    Example: "cart_id"

  - `price` (object,null)
    购物车价格。
    Example: {"amount":"6150.0000000000000000","amount_without_discount":"6150.0000000000000000","currency":"USD"}

  - `price.amount` (string)
    Example: "6150.0000000000000000"

  - `price.amount_without_discount` (string)
    Example: "6150.0000000000000000"

  - `price.currency` (string)
    Example: "USD"

  - `is_free` (boolean)
    该商品是否免费。

  - `items` (array)
    Example: [{"attributes":[],"description":"Take it, take it all! All of Xsolla's riches in one Mega Booster.","groups":[{"external_id":"powerups","name":"Power Ups"}],"image_url":"https://cdn.xsolla.net/img/misc/images/e9f2f4a634bc96ea03b5d5ceadd7c55f.png","is_free":false,"name":"Xsolla Booster Mega","price":{"amount":"50.0000000000000000","amount_without_discount":"100.0000000000000000","currency":"USD"},"quantity":123,"sku":"com.xsolla.booster_mega_1","type":"virtual_good","virtual_item_type":"consumable","virtual_prices":[],"promotions":[{"name":"Bonus promotion","date_start":"2020-04-15T16:16:00+03:00","date_end":"2026-04-15T16:16:00+03:00","discount":{"percent":"50.00"},"bonus":[{"quantity":1,"name":"Xsolla Minigun","image_url":"https://cdn.xsolla.net/img/misc/images/2fc5c491a47413a8e8000447889093c2.png","sku":"com.xsolla.minigun_1","type":"virtual_good"}]}],"can_be_bought":true,"vp_rewards":[{"item_id":175232,"sku":"com.xsolla.value_point_1","amount":130,"name":"Value point","image_url":"https://cdn3.xsolla.com/img/misc/images/54c0cf9d345817cdacfdde198db178e0.jpg","is_clan":false},{"item_id":186321,"sku":"com.xsolla.clan_value_point_1","amount":50,"name":"Clan Reward VP 1","image_url":"https://cdn3.xsolla.com/img/misc/images/54c0cf9d345817cdacfdde198db178e0.jpg","is_clan":true}],"limits":{"per_user":{"available":3,"recurrent_schedule":{"interval_type":"weekly","reset_next_date":1746057600},"total":5}},"periods":[{"date_from":"2020-08-11T10:00:00+03:00","date_until":"2020-08-11T20:00:00+03:00"}]}]

  - `items.sku` (string)

  - `items.groups` (array)

  - `items.groups.external_id` (string)

  - `items.groups.name` (string)

  - `items.name` (string,null)

  - `items.type` (string)

  - `items.description` (string)

  - `items.image_url` (string)

  - `items.quantity` (integer)

  - `items.is_free` (boolean)
    该商品是否免费。

  - `items.promotions` (array)
    应用于购物车中指定商品的促销活动。仅在以下情况下返回该数组：

* 为某商品配置了折扣促销活动。

* 应用了具有对所选商品提供折扣设置的兑换码。

如果未应用任何商品级促销活动，则返回空数组。

  - `items.promotions.name` (string)

  - `items.promotions.date_start` (string,null)

  - `items.promotions.date_end` (string,null)

  - `items.promotions.discount` (object,null)

  - `items.promotions.discount.percent` (string,null)

  - `items.promotions.discount.value` (string,null)

  - `items.promotions.bonus` (array)

  - `items.promotions.bonus.sku` (string)

  - `items.promotions.bonus.quantity` (integer)

  - `items.promotions.bonus.type` (string)
    赠品类型。
    Enum: "virtual_good", "virtual_currency", "bundle", "physical_good", "game_key", "nft"

  - `items.promotions.bonus.name` (string)
    赠品名称。不适用于physical_good赠品类型。

  - `items.promotions.bonus.image_url` (string)
    赠品图片URL。不适用于physical_good赠品类型。

  - `items.promotions.bonus.bundle_type` (string)
    赠品捆绑包商品类型。仅适用于bundle赠品类型。
    Enum: "standard", "virtual_currency_package"

  - `items.promotions.limits` (object)

  - `items.promotions.limits.per_user` (object)

  - `items.promotions.limits.per_user.available` (integer)

  - `items.promotions.limits.per_user.total` (integer)

  - `items.can_be_bought` (boolean)
    如为true，则用户可以购买商品。

  - `items.vp_rewards` (array)
    该商品的累充积分奖励列表。

  - `items.vp_rewards.item_id` (integer)
    内部唯一商品ID。

  - `items.vp_rewards.sku` (string)
    唯一累充积分ID。

  - `items.vp_rewards.amount` (integer)
    累充积分数量。

  - `items.vp_rewards.name` (string)
    累充积分名称。

  - `items.vp_rewards.image_url` (string)
    图片URL。

  - `items.vp_rewards.is_clan` (boolean)
    累充积分是否用于公会累充奖励链。

  - `items.loyalty_rewards` (array)
    用户购买商品后获得的忠诚积分奖励。

  - `items.loyalty_rewards.name` (string)
    忠诚积分名称。

  - `items.loyalty_rewards.sku` (string)
    忠诚积分SKU。调用其他API时，请将该值作为loyalty_point_sku参数传入，例如创建使用忠诚积分支付的订单时。

  - `items.loyalty_rewards.description` (string)
    忠诚积分描述。

  - `items.loyalty_rewards.image_url` (string,null)
    图片URL。

  - `items.loyalty_rewards.amount` (integer)
    用户购买商品后获得的忠诚积分数量。

  - `items.limits` (object,null)
    商品限制数。

  - `items.limits.per_user` (object,null)
    单个用户的商品购买数量限制。

  - `items.limits.per_user.total` (integer)
    单个用户可购买的最大商品数量。
    Example: 5

  - `items.limits.per_user.available` (integer)
    当前用户可购买的剩余商品数量。
    Example: 3

  - `items.limits.per_user.recurrent_schedule` (any)

  - `items.limits.per_user.limit_exceeded_visibility` (string)
    设定商品在达到购买限制后、下次限制重置前在商品目录中的可见性。

适用于在recurrent_schedule数组中配置了周期性重置数量限制的商品。

如果未配置重置限制，则达到购买限制后，无论limit_exceeded_visibility的值如何，该商品都不会显示在商品目录中。

可能值：
- show — 达到购买限制后，商品目录检索API调用仍会返回该商品。在客户端侧
商品目录检索API调用中，达到限制后返回的商品会带有can_be_bought: false 标志。下一次重置日期
将在reset_next_date中返回。
- hide — 达到购买限制后，在限制
重置之前，商品目录检索API调用不会返回该商品。
    Enum: "show", "hide"

  - `items.limits.per_item` (object,null)
    单个商品的购买数量限制。

  - `items.limits.per_item.total` (integer)
    所有用户可购买的最大商品数量。
    Example: 5

  - `items.limits.per_item.available` (integer)
    所有用户还可购买的剩余商品数量。
    Example: 3

  - `items.periods` (array,null)
    商品销售期。

  - `items.periods.date_from` (string)
    指定商品开始可购的日期。
    Example: "2020-08-11T10:00:00+03:00"

  - `items.periods.date_until` (string,null)
    指定商品停止可购的日期。可为null。
    Example: "2020-08-11T20:00:00+03:00"

  - `rewards` (object)

  - `rewards.bonus` (array)

  - `rewards.bonus.item` (object)

  - `rewards.bonus.item.sku` (string)
    唯一商品ID。SKU只能包含大小写英文字母、数字、句点、短横线和下划线。
    Example: "game_01"

  - `rewards.bonus.item.name` (string)
    商品名称。
    Example: "Game name"

  - `rewards.bonus.item.type` (string)
    商品类型。可能值：virtual_good — 虚拟物品virtual_currency — 虚拟货币bundle — 捆绑包unit — 游戏Key套餐
    Example: "unit"

  - `rewards.bonus.item.description` (string)
    商品描述。
    Example: "Game description"

  - `rewards.bonus.item.image_url` (string)
    图片URL。
    Example: "https://cdn.xsolla.net/img/misc/images/b79342cdf24f0f8557b63c87e8326e62.png"

  - `rewards.bonus.item.unit_items` (array)
    平台专属游戏Key数组。仅当商品类型为unit (bonus.item.type = 'unit')时使用。如果游戏Key套餐包含平台专属Key，用户可以选择其中一个作为赠品。

  - `rewards.bonus.item.unit_items.sku` (string)
    唯一的平台专属游戏Key套餐ID。允许使用以下字符：a-z、A-Z、0-9、句点（.）、连字符（-）、下划线（_）。该值由bonus.item.​sku和bonus.item.unit_items.drm_sku的值组合而成。例如，如果bonus.item.​sku = 'cool_game'，且bonus.item.unit_items.drm_sku = 'steam'，则该值为cool_game_steam。
    Example: "cool_game_steam"

  - `rewards.bonus.item.unit_items.is_free` (boolean)

  - `rewards.bonus.item.unit_items.type` (string)
    表示该商品为游戏Key。
    Enum: "game_key"

  - `rewards.bonus.item.unit_items.name` (string)
    游戏名称。
    Example: "Awesome Game"

  - `rewards.bonus.item.unit_items.drm_name` (string)
    DRM名称。
    Example: "Steam"

  - `rewards.bonus.item.unit_items.drm_sku` (string)
    唯一DRM ID，用作后缀以表示平台专属游戏Key。允许使用以下字符：a-z、A-Z、0-9、句点（.）、连字符（-）、下划线（_）。
    Example: "steam"

  - `rewards.bonus.quantity` (number)
    商品数量。

  - `rewards.discount` (object,null)
    百分比折扣。
购物车价格将按照此百分比打折，然后四舍五入到小数点后两位。

  - `rewards.discount.percent` (string)
    Example: "10.00"

  - `rewards.discounted_items` (array,null)
    通过兑换码享受折扣的商品列表。

  - `rewards.discounted_items.sku` (string, required)
    商品SKU。

  - `rewards.discounted_items.discount` (object, required)

  - `rewards.discounted_items.discount.percent` (string, required)
    百分比折扣。

购物车商品价格将按照此百分比打折，
然后四舍五入到小数点后两位。

  - `rewards.is_selectable` (boolean)
    如果为true，则用户应在核销兑换码前选择赠品。

## Response 401 fields (application/json):

  - `statusCode` (integer)
    Example: 401

  - `errorCode` (integer)
    Example: 1501

  - `errorMessage` (string)
    Example: "[0401-1501]: Authorization failed: Provide authorization"

## Response 403 fields (application/json):

  - `statusCode` (integer)
    Example: 403

  - `errorCode` (integer)

  - `errorMessage` (string)
    Example: "Authorization header not sent."

  - `transactionId` (string)
    Example: "x-x-x-x-transactionId-mock-x-x-x"

## Response 404 fields (application/json):

  - `statusCode` (integer)
    Example: 404

  - `errorCode` (integer)
    Example: 4001

  - `errorMessage` (string)
    Example: "[0401-9807]: Enter valid promo code."

## Response 422 fields (application/json):

  - `statusCode` (integer)
    Example: 422

  - `errorCode` (integer)
    Example: 1102

  - `errorMessage` (string)
    Example: "[0401-1102]: Unprocessable Entity. The property `coupon_code` is required"

  - `transactionId` (string)
    Example: "x-x-x-x-transactionId-mock-x-x-x"


