앱에서 앱 내 구매 및 추가 기능 구매 사용하기

이 문서는 Windows.Services.Store 네임스페이스의 멤버를 사용하여 현재 앱 또는 해당 추가 기능 중 하나를 구매 요청하는 방법을 설명합니다. 예를 들어 사용자에게 현재 평가판 버전의 앱이 있는 경우 이 프로세스를 사용하여 사용자에 대한 전체 라이선스를 구매할 수 있습니다. 또는 이 프로세스를 사용하여 사용자의 새 게임 수준과 같은 추가 기능을 구매할 수 있습니다.

Windows.Services.Store 네임스페이스는 앱 또는 추가 기능 구매를 요청하는 데 다양한 방법을 제공합니다.

각 메서드는 사용자에게 표준 구매 UI를 표시한 다음 트랜잭션이 완료된 후 비동기적으로 완료됩니다. 메서드는 트랜잭션이 성공했는지 여부를 나타내는 개체를 반환합니다.

참고 항목

Windows.Services.Store 네임스페이스는 Windows 10 버전 1607에 도입되었으며 Windows 10 Anniversary Edition(10.0, 빌드 14393) 또는 Visual Studio의 최신 릴리스를 대상으로 하는 프로젝트에만 사용할 수 있습니다. 앱이 이전 버전의 Windows 10을 대상으로 하는 경우에는 Windows.Services.Store 네임스페이스 대신 Windows.ApplicationModel.Store 네임스페이스를 사용해야 합니다. 자세한 정보는 이 문서를 참조하십시오.

필수 조건

이 예시에는 다음과 같은 필수 조건이 있습니다.

  • Windows 10 Anniversary Edition(10.0, 빌드 14393) 이상 릴리스를 대상으로 하는 UWP(유니버설 Windows 플랫폼) 앱에 대한 Visual Studio 프로젝트.
  • 파트너 센터에서 앱 제출을 생성하였으며 해당 앱은 Store에 게시되었습니다. 테스트하는 동안 Store에서 검색이 되지 않도록 앱을 구성할 수도 있습니다. 자세한 정보는 테스트 지침을 참조하세요.
  • 앱 추가 기능에 앱 내 구매를 구현하고 싶다면 파트너 센터에서 추가 기능을 생성해야 합니다.

이 예시의 코드는 다음을 가정합니다.

  • 코드는 workingProgressRing(이)라고 이름이 지정된 ProgressRingtextBlock(이)라고 이름이 지정된 TextBlock을 포함하는 페이지의 컨텍스트에서 실행됩니다. 이러한 개체는 각각 비동기 작업의 발생을 나타내며 출력 메시지를 표시하는 데 사용됩니다.
  • 코드 파일에는 Windows.Services.Store 네임스페이스에 대한 using 문이 있습니다.
  • 앱은 해당 앱을 실행한 사용자의 컨텍스트에서만 실행되는 단일 사용자 앱입니다. 자세한 정보는 앱 내 구매 및 평가판을 참조하세요.

참고 항목

데스크톱 브리지를 사용하는 데스크톱 애플리케이션이 있는 경우 이 예제에서 표시되지 않는 별도의 코드를 추가하여 StoreContext 개체를 구성해야 할 수도 있습니다. 자세한 정보는 데스크톱 브리지를 사용하는 데스크톱 애플리케이션에서 StoreContext 클래스 사용하기를 참조하세요.

코드 예제

이 예제는 StoreContext 클래스의 RequestPurchaseAsync 메서드를 사용하여 알려진 Store ID로 앱 또는 추가 기능을 구매하는 방법을 보여 줍니다. 전체 샘플 애플리케이션은 Store 샘플을 참조하세요.

private StoreContext context = null;

public async void PurchaseAddOn(string storeId)
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    workingProgressRing.IsActive = true;
    StorePurchaseResult result = await context.RequestPurchaseAsync(storeId);
    workingProgressRing.IsActive = false;

    // Capture the error message for the operation, if any.
    string extendedError = string.Empty;
    if (result.ExtendedError != null)
    {
        extendedError = result.ExtendedError.Message;
    }

    switch (result.Status)
    {
        case StorePurchaseStatus.AlreadyPurchased:
            textBlock.Text = "The user has already purchased the product.";
            break;

        case StorePurchaseStatus.Succeeded:
            textBlock.Text = "The purchase was successful.";
            break;

        case StorePurchaseStatus.NotPurchased:
            textBlock.Text = "The purchase did not complete. " +
                "The user may have cancelled the purchase. ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.NetworkError:
            textBlock.Text = "The purchase was unsuccessful due to a network error. " +
                "ExtendedError: " + extendedError;
            break;

        case StorePurchaseStatus.ServerError:
            textBlock.Text = "The purchase was unsuccessful due to a server error. " +
                "ExtendedError: " + extendedError;
            break;

        default:
            textBlock.Text = "The purchase was unsuccessful due to an unknown error. " +
                "ExtendedError: " + extendedError;
            break;
    }
}