5

我尝试了以下代码进行应用内购买。我CurrentAppSimulator用于测试目的。

private async void history_Click(object sender, RoutedEventArgs e)
{
    bool OK = true;

    // Get the license info
    // The next line is commented out for testing.
    // licenseInformation = CurrentApp.LicenseInformation;

    // The next line is commented out for production/release.       
    LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
    if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
    {
        try
        {
            // The customer doesn't own this feature, so 
            // show the purchase dialog.

            await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
            // the in-app purchase was successful
            OK = true;
        }
        catch (Exception)
        {
            // The in-app purchase was not completed because 
            // an error occurred.
            OK = false;
        }
    }
    else
    {
        // The customer already owns this feature.
        OK = true;
    }

    if (OK)
    {
        Frame.Navigate(typeof(HistoryPage));
    }
}

但是,每次执行时都会弹出对话框history_Click,即使我选择了S_OK。我希望licenseInformation.ProductLicenses["PremiumFeatures"].IsActive在我购买PremiumFeatures.

在此处输入图像描述

我想,也许当应用内购买成功时,我需要打开IsActive标志,这样它就不会再次显示购买对话框。

        // the in-app purchase was successful
        OK = true;
        // Compile error!!!
        licenseInformation.ProductLicenses["PremiumFeatures"].IsActive = true;

好的。似乎IsActive是一个只读字段。那么,请问,处理采购成功案例的正确方法是什么?

更新 :

在查看试用应用程序和应用内购买示例后,我意识到购买成功后可以自动IsActive更改为以下代码。(但为什么它有效?)falsetrue

    private async Task LoadInAppPurchaseProxyFileAsync()
    {
        StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
        StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
        licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
        CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
        await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);

        // setup application upsell message
        ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
        var product1 = listing.ProductListings["product1"];
        var product2 = listing.ProductListings["product2"];
        Product1SellMessage.Text = "You can buy " + product1.Name + " for: " + product1.FormattedPrice + ".";
        Product2SellMessage.Text = "You can buy " + product2.Name + " for: " + product2.FormattedPrice + ".";
    }

但是,“已购买”的状态并不是坚持。如果我关闭应用程序并重新启动它,它会认为我的“高级功能”为“尚未购买”。

我怎么想使这种行为持久化?

4

5 回答 5

8

请阅读

根据您的经验,我会假设 App.IsTrial 设置为 true。当应用程序处于试用模式或(出于任何其他原因)未激活时,您无法提交购买。请注意,我指的是应用程序,而不是功能/产品。一旦应用程序处于活动状态,您的购买将成功。

于 2013-02-20T05:45:53.567 回答
3

据我所知,单击“确定”时它不会自动更新。它只返回正确的状态,就像在 Windows 应用商店中应该做的那样。

我认为是一种选择,但我自己没有实现它是使用ReloadSimulatorAsync(proxyFile)方法。在调用此方法之前,您必须自己更新 in-app-purchase.xml 并将其保存并将文件传递给 ReloadSimulatorAsync 方法。

我个人会使用 xml 文件的手动更新。但我无法判断您的申请/申请流程。

自动化此过程的良好开端是示例“应用内购买”应用程序,您可以在此处找到。如果您查看代码文件 InAppPurchase.xaml.cs,您将看到已经有一些代码用于您自己的实现。

于 2012-12-20T10:31:22.960 回答
0

我只是要清除这一点。执行时

  try {

        await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
        OK = true

    }
    catch (Exception)
    {

        OK = false;
    }

OK 不仅在用户购买该功能时是真的,如果他取消购买或者他无法被识别或他禁用他的网络连接也是如此。所以这是检查购买是否成功的错误方法。基本上 Anobik 是对的,您必须事后检查是否购买了许可证,因此正确的代码是:

if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
    try
    {
        // The customer doesn't own this feature, so 
        // show the purchase dialog.

        await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
                if (licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
                {
                    // the in-app purchase was successful
                     OK = true;
                    //Success in purchase use ur own code block
                }
                else
                {
                     OK = false;
                    //error in purchase use your own code block
                }

    }
    catch (Exception)
    {
        // The in-app purchase was not completed because 
        // an error occurred.
        OK = false;
    }
}
else
{
    // The customer already owns this feature.
    OK = true;
}

if (OK)
{
    Frame.Navigate(typeof(HistoryPage));
}

但是,如果你编译你会发现它不起作用。我认为,这是因为 CurrentAppSimulator 不会对许可证进行永久性更改。我希望这是真的 :)

于 2013-03-26T18:51:38.930 回答
0

购买后无需IsActive立即检查许可证。相反,您可以(最好)存储 的结果RequestProductPurchaseAsync,它是类型PurchaseResults并且具有Status属性。后者又是一个ProductPurchaseStatus枚举值,如果是Succeeded,您可以在不检查许可证的情况下继续。

于 2014-08-19T21:31:14.793 回答
-1

您只缺少一个 if else 条件来处理应用内购买

这是代码

private async void history_Click(object sender, RoutedEventArgs e)
{
bool OK = true;

// Get the license info
// The next line is commented out for testing.
// licenseInformation = CurrentApp.LicenseInformation;

// The next line is commented out for production/release.       
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
    try
    {
        // The customer doesn't own this feature, so 
        // show the purchase dialog.

        await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
                if (licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
                {
                    // the in-app purchase was successful
                     OK = true;
                    //Success in purchase use ur own code block
                }
                else
                {
                     OK = false;
                    //error in purchase use your own code block
                }

    }
    catch (Exception)
    {
        // The in-app purchase was not completed because 
        // an error occurred.
        OK = false;
    }
}
else
{
    // The customer already owns this feature.
    OK = true;
}

if (OK)
{
    Frame.Navigate(typeof(HistoryPage));
}

}

于 2012-12-20T12:20:38.343 回答