# 구매 한도 설정

원문: https://commerce.bootpay.ai/api/marketplace/group-limit

> 고객 그룹의 월간 · 주간 구매 한도와 한도 초과 안내 문구를 정합니다. 빼고 보낸 한도는 0 으로 저장되니 월 · 주 한도를 모두 다시 보냅니다.

고객 그룹의 주간·월간 구매 한도를 설정합니다.

## API 엔드포인트

::: endpoint
method: PUT
url: https://api.bootapi.com/v1/user-groups/:id/limit
auth: [Basic Auth](https://commerce.bootpay.ai/api/server/authentication)
scope: manager:limit
:::

이 scope 는 Bootpay-Role 헤더와 상관없이 검사합니다. [v1 회원 그룹 API 상세 명세](https://commerce.bootpay.ai/api/reference/v1/user-groups)에서 인증과 응답 필드를 확인합니다.

## 요청 파라미터

::: parameters

| 파라미터 | 타입 | 필수 | 설명 |
|---------|------|------|------|
| id | String | Y | 그룹 고유 ID (URL 파라미터) |
| use_limit | Boolean | N | 구매 한도 기능 사용 여부. JSON boolean 으로 보냅니다. 문자열 `"false"` 는 켠 것으로 검증됩니다 |
| limit_month_purchase | Number | N | 월 구매 한도 금액 (단위: 원) |
| limit_message | String | N | 한도 초과 시 노출할 안내 문구 |
| limit_week_purchase | Number | N | 주 구매 한도 금액 (단위: 원) |

:::

::: warning 생략한 한도는 0 으로 저장됩니다
지금은 `limit_month_purchase`·`limit_week_purchase` 중 빼고 보낸 값이 기존 값이 아니라 0 으로 저장됩니다. 그래서 `use_limit: true` 일 때 하나라도 빼면 `USER_GROUP_PURCHASE_LIMIT_REQUIRED` 로 거절되고, `use_limit` 없이 `limit_message` 만 바꿔도 두 한도가 0 이 됩니다.

기존 한도를 유지하려면 월·주 한도를 모두 다시 보냅니다. `use_limit: true` 일 때는 두 값이 모두 0보다 크고, 이미 구독 중인 금액 이상이어야 합니다. `limit_message` 는 생략하면 기존 값을 유지합니다.
:::

## 코드 예제

::: code-group

```bash [cURL]
curl -X PUT "https://api.bootapi.com/v1/user-groups/67e2052b03d0cb4e4117b0af/limit" \
  -H "Authorization: Basic {base64(client_key:secret_key)}" \
  -H "Bootpay-Role: manager" \
  -H "Content-Type: application/json" \
  -d '{
    "use_limit": true,
    "limit_month_purchase": 3500000,
    "limit_week_purchase": 1000000
  }'
```

```javascript [Node.js]
import { BootpayCommerce } from '@bootpay/backend-js';

const commerce = new BootpayCommerce({
    client_key: '{client_key}',
    secret_key: '{secret_key}'
});

const userGroupId = '67e2052b03d0cb4e4117b0af';
const response = await commerce.userGroup.limit({
    user_group_id: userGroupId,
    use_limit: true,
    limit_month_purchase: 3500000,
    limit_week_purchase: 1000000
});
console.log(response);
```

```python [Python]
from bootpay_backend.commerce import BootpayCommerce

commerce = BootpayCommerce('{client_key}', '{secret_key}')
user_group_id = '67e2052b03d0cb4e4117b0af'
response = commerce.user_group.limit({
    'user_group_id': user_group_id,
    'use_limit': True,
    'limit_month_purchase': 3500000,
    'limit_week_purchase': 1000000
})
print(response)
```

```php [PHP]
use Bootpay\ServerPhp\BootpayCommerceApi;

$commerce = new BootpayCommerceApi("{client_key}", "{secret_key}");
$userGroupId = '67e2052b03d0cb4e4117b0af';
$response = $commerce->userGroup->limit([
    'user_group_id' => $userGroupId,
    'use_limit' => true,
    'limit_month_purchase' => 3500000,
    'limit_week_purchase' => 1000000
]);
print_r($response);
```

```java [Java]
import kr.co.bootpay.store.BootpayStore;
import kr.co.bootpay.store.model.request.TokenPayload;
import kr.co.bootpay.store.model.request.userGroup.UserGroupLimitParams;

TokenPayload tokenPayload = new TokenPayload("{client_key}", "{secret_key}");
BootpayStore bootpay = new BootpayStore(tokenPayload).withToken();
UserGroupLimitParams params = new UserGroupLimitParams();
params.userGroupId = "67e2052b03d0cb4e4117b0af";
params.useLimit = true;
params.limitMonthPurchase = 3500000.0;
params.limitWeekPurchase = 1000000.0;

var response = bootpay.userGroup.limit(params);
System.out.println(response.getData());
```

```ruby [Ruby]
require 'bootpay'

commerce = BootpayStore::RestClient.new(client_key: '{client_key}', secret_key: '{secret_key}')
user_group_id = '67e2052b03d0cb4e4117b0af'
response = commerce.user_group_limit(
  user_group_id: user_group_id,
  use_limit: true,
  limit_month_purchase: 3500000,
  limit_week_purchase: 1000000
)
puts response.data
```

```go [Go]
commerce := bootpay.NewCommerceAPI("{client_key}", "{secret_key}", nil, "production")
userGroupId := "67e2052b03d0cb4e4117b0af"
monthLimit, weekLimit := 3500000, 1000000
response, err := commerce.UserGroup.Limit(bootpay.UserGroupLimitParams{
    UserGroupId:        userGroupId,
    UseLimit:           true,
    LimitMonthPurchase: &monthLimit,
    LimitWeekPurchase:  &weekLimit,
})
fmt.Println(response, err)
```

```csharp [.NET]
using Bootpay.Commerce;
using Bootpay.Commerce.Models;

var commerce = new BootpayCommerceApi("{client_key}", "{secret_key}");
var userGroupId = "67e2052b03d0cb4e4117b0af";
var response = await commerce.UserGroupLimit(new UserGroupLimitParams {
    UserGroupId = userGroupId,
    UseLimit = true,
    LimitMonthPurchase = 3500000,
    LimitWeekPurchase = 1000000
});
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

:::

## 응답

#### 성공 응답

```json
{
  "use_limit": true,
  "limit_month_purchase": 3500000,
  "limit_week_purchase": 1000000,
  "subscribed_month_limit": 1200000,
  "subscribed_week_limit": 300000,
  "limit_message": "한도를 초과했습니다."
}
```

::: tip
`subscribed_month_limit`과 `subscribed_week_limit`은 현재까지의 구독 사용량입니다. 한도에서 이 값을 빼면 남은 한도입니다.
:::

## 에러 코드

::: info 공통 에러
인증·권한 관련 에러는 [에러 코드표](https://commerce.bootpay.ai/api/reference/codes#커머스-api-인증-에러)를 참고합니다.
:::

::: error-codes

| 코드 | 메시지 | 대처 방법 |
|------|--------|-----------|
| `USER_GROUP_NOT_FOUND` | 회원 그룹 정보를 찾지 못했습니다. 다시 로그인 해주세요. | user_group_id를 확인합니다 |
| `USER_GROUP_NOT_AUTHORIZED` | 그룹 정보를 설정할 권한이 없습니다. (HTTP 401) | 이 프로젝트의 그룹인지 확인합니다 |
| `USER_GROUP_PURCHASE_LIMIT_REQUIRED` | 월 구매 한도 금액 값을 입력해주세요. | `use_limit: true` 면 `limit_month_purchase`·`limit_week_purchase` 를 0보다 크게 넣습니다 |
| `USER_GROUP_PURCHASE_LIMIT_BELOW_SUBSCRIBED` | 월 구매 한도 금액이 현재 구독된 금액보다 적습니다. | 현재 구독 금액 이상으로 설정합니다 |

:::
