StorePurchaseStatus.NotPurchased immediately after RequestPurchaseAsync()

203 Views Asked by At

In my WPF application which is packaged via Desktop Bridge I found a problem that some users can't buy addon via in-app purchase. It displays my "Canceled" alert which represents StorePurchaseStatus.NotPurchased where result.ExtendedError is null.

Target framework is:

<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>

Here is the simplified code that procures the purchase:

namespace MyApp {


    public partial class MainWindow: Window {
        private readonly StoreContext context;


         public MainWindow(){
            context = StoreContext.GetDefault();
         }
    

        private bool IsAdministrator()
        {
            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        private async void BuyButtonClick(object sender, RoutedEventArgs e) {

            if (IsAdministrator())
            {
                ShowAlert("Cannot run under administrator rights");
                return;
            }


            if (sender is Button button)
            {
                StoreProduct? storeProduct = ((Product)dataContext).storeProduct;

                if (storeProduct != null)
                {
                     Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(async delegate
                     {
                        var hwnd = new WindowInteropHelper(this).Handle;
                        WinRT.Interop.InitializeWithWindow.Initialize(context, hwnd);
                        var result = await context.RequestPurchaseAsync(storeProduct.StoreId);


                        switch (result.Status)
                        {
                            case StorePurchaseStatus.Succeeded:
                                ShowAlert("Succeeded");
                                break;
                            case StorePurchaseStatus.AlreadyPurchased:
                                ShowAlert("AlreadyPurchased");
                                break;
                            case StorePurchaseStatus.NotPurchased:
                                var extendedError = result.ExtendedError;

                                if (extendedError != null)
                                {
                                    ShowAlert(extendedError.Message);
                                }
                                else
                                {
                                    ShowAlert("Canceled");
                                }
                                break;
                            case StorePurchaseStatus.NetworkError:
                                ShowAlert("NetworkError");
                                break;
                            case StorePurchaseStatus.ServerError:
                                ShowAlert("ServerError");
                                break;
                        }

                     }
                }

            }

        }

    }

It works everywhere on my devices (Windows 11 and Windows 10). The user who cannot buy has Windows 11.

1

There are 1 best solutions below

15
Roy Li - MSFT On

This might be caused by the account type that the customer is using.

First of all, the store purchase will fail if the app is running as administrator.

Normal “admin” accounts (people in the administrators group with a split token) will just run your desktop bridge app as a standard user, unless they right-click and launch something elevated explicitly.

But if the customer is using the system built-in account on their device, the purchase will be failed as the app will be running as administrator. This is not allowed for Microsoft Store purchase API.