IAsyncAction WindowsStoreImpl::Purchase()

in WindowsStoreDLL/WindowsStoreImpl.cpp [45:117]


IAsyncAction WindowsStoreImpl::Purchase(WindowsStoreCallback callback, void* userData)
{
    // return control to caller
    co_await winrt::resume_background();

    // now execute the async code
    HRESULT hr = E_FAIL;

    // Assign the app's hwnd to the storeContext
    auto initWindow = m_storeContext.try_as<IInitializeWithWindow>();
    if (initWindow != nullptr)
    {
        hr = initWindow->Initialize(m_hwnd);
    }

    if (!SUCCEEDED(hr))
    {
        std::wstringstream ws;
        callback(hr, L"Can't initial StoreContext with hwnd: ", userData);
        co_return;
    }

    StoreProductResult productResult = co_await m_storeContext.GetStoreProductForCurrentAppAsync();
    if (productResult.ExtendedError().value != S_OK)
    {
        std::wstringstream ws;
        ws << L"GetStoreProductForCurrentAppAsync Error: " << productResult.ExtendedError().value;
        callback(productResult.ExtendedError().value, ws.str().c_str(), userData);
        co_return;
    }

    StoreAppLicense license = co_await m_storeContext.GetAppLicenseAsync();

    if (license.IsTrial())
    {
        auto result = co_await productResult.Product().RequestPurchaseAsync();
        std::wstringstream ws;
        switch (result.Status())
        {
        case StorePurchaseStatus::AlreadyPurchased:
            ws << L"You already bought this app and have a fully-licensed version.";
            break;

        case StorePurchaseStatus::Succeeded:
            // License will refresh automatically using the StoreContext.OfflineLicensesChanged event
            break;

        case StorePurchaseStatus::NotPurchased:
            ws << L"Product was not purchased, it may have been canceled";
            break;

        case StorePurchaseStatus::NetworkError:
            ws << L"Product was not purchased due to a Network Error.";
            break;

        case StorePurchaseStatus::ServerError:
            ws << L"Product was not purchased due to a Server Error.";
            break;

        default:
            ws << L"Product was not purchased due to a Unknown Error.";
            break;
        }
        callback(E_FAIL, ws.str().c_str(), userData);
    }
    else
    {
        std::wstringstream ws;
        callback(S_OK, L"You already bought this app and have a fully-licensed version.", userData);
    }

    co_return;
}