void WindowsStoreImpl::Purchase()

in WindowsStore/WindowsStoreImpl.cpp [47:119]


void WindowsStoreImpl::Purchase(WindowsStoreCallback callback, void* userData)
{
    // Assign the app's hwnd to the storeContext
    ComPtr<IInitializeWithWindow> initWindow;
    IUnknown* temp = reinterpret_cast<IUnknown*>(m_storeContext);
    HRESULT hr = temp->QueryInterface(initWindow.GetAddressOf());
    if (SUCCEEDED(hr))
    {
        hr = initWindow->Initialize(m_hwnd);
        if (!SUCCEEDED(hr))
        {
            std::wstringstream ws;
            callback(hr, L"Can't initial StoreContext with hwnd: ", userData);
            return;
        }
    }

    create_task(m_storeContext->GetStoreProductForCurrentAppAsync()).then([this, callback, userData](StoreProductResult^ productResult)
    {
        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);
            return;
        }

        create_task(m_storeContext->GetAppLicenseAsync()).then([this, productResult, callback, userData](StoreAppLicense^ license)
        {
            if (license->IsTrial)
            {
                create_task(productResult->Product->RequestPurchaseAsync()).then([this, callback, userData](StorePurchaseResult^ result)
                {
                    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);

            }
        }, task_continuation_context::get_current_winrt_context());
    }, task_continuation_context::get_current_winrt_context());
}