且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows 10应用中实施应用内购买?

更新时间:2022-12-31 09:57:06

I also had this issue and the problem was in the WindowsStoreProxy.xml file.

Solution in short

By default in the WindowsStoreProxy.xml the IsTrial is set to true and in that mode in-app purchases do not seem to work. When I changed it to false it started to work for me.

Solution a little bit longer

  • So first of all here we are talking about the simulation of an In-App Purchase in development time (by using the CurrentAppSimulator class). In that case you need a WindowsStoreProxy.xml file. It’s described here

  • Now the window you showed is opened by the CurrentAppSimulator.RequestProductPurchaseAsync line. It basically controls the return value of a Windows Runtime native method (which is very strange for me… I think it’s not intentional by Microsoft… something else should be done there), but if you let it return S_OK that basically is the case when the user paid for the in-App Purchase.

  • When it returns nothing then with very high probability something in the WindowsStoreProxy.xml is wrong. I suggest you to create your own WindowsStoreProxy.xml and read it with the CurrentAppSimulator.ReloadSimulatorAsync method like this:

    var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Testing\WindowsStoreProxy.xml");
    await CurrentAppSimulator.ReloadSimulatorAsync(file);
    

  • For me using the default one from C:\Users\<username>\AppData\Local\Packages\<app package folder>\LocalState\Microsoft\Windows Store\ApiData\WindowsStoreProxy.xml did not work, but a single change already solved the problem: I changed this part

    <LicenseInformation>
            <App>
                <IsActive>true</IsActive>
                <IsTrial>true</IsTrial>
            </App>
    </LicenseInformation>
    

    To this:

    <LicenseInformation>
            <App>
                <IsActive>true</IsActive>
                <IsTrial>false</IsTrial>
            </App>
    </LicenseInformation>
    

    (So IsTrial was set to false...)

  • Now at this point I also would like to mention that this was a little bit strange, since in in the default WindowsStoreProxy.xml there was no Product defined for my In-App Purchase. So for my "RemoveAds" a proper WindowsStoreProxy.xml would be something like this:

    <?xml version="1.0" encoding="utf-16" ?>
    <CurrentApp>
        <ListingInformation>
            <App>
                <AppId>00000000-0000-0000-0000-000000000000</AppId>
                <LinkUri>http://apps.microsoft.com/webpdp/app/00000000-0000-0000-0000-000000000000</LinkUri>
                <CurrentMarket>en-US</CurrentMarket>
                <AgeRating>3</AgeRating>
                <MarketData xml:lang="en-US">
                    <Name>AppName</Name>
                    <Description>AppDescription</Description>
                    <Price>1.00</Price>
                    <CurrencySymbol>$</CurrencySymbol>
                    <CurrencyCode>USD</CurrencyCode>
                </MarketData>
            </App>
            <Product ProductId="RemoveAds" LicenseDuration="1" ProductType="Durable">
                <MarketData xml:lang="en-US">
                    <Name>RemoveAds</Name>
                    <Price>1.00</Price>
                    <CurrencySymbol>$</CurrencySymbol>
                    <CurrencyCode>USD</CurrencyCode>
                </MarketData>
            </Product>      
        </ListingInformation>
        <LicenseInformation>
            <App>
                <IsActive>true</IsActive>
                <IsTrial>false</IsTrial>
            </App>
            <Product ProductId="1">
                <IsActive>true</IsActive>
            </Product>
        </LicenseInformation>
        <ConsumableInformation>
            <Product ProductId="RemoveAds" TransactionId="10000000-0000-0000-0000-000000000000" Status="Active" />
        </ConsumableInformation>
    </CurrentApp>
    

  • Another thing I would like to point out is that the CurrentAppSimulator.RequestProductPurchaseAsync with two parameter is obsolete. Leave the true parameter out and you get PurchaseResults instance as the result, which contains the receipt in the ReceiptXML property.