我尝试使用以下代码进行应用内购买。我正在使用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
应该更改为true。
我想,也许当应用内购买成功时,我需要打开IsActive
标记,这样它就不会再次向我显示购买对话框。
// the in-app purchase was successful
OK = true;
// Compile error!!!
licenseInformation.ProductLicenses["PremiumFeatures"].IsActive = true;
行。似乎IsActive
是一个只读字段。那么,我可以知道,处理购买成功案例的正确方法是什么?
在查看Trial app and in-app purchase sample后,我意识到在购买成功后,以下代码可以IsActive
从false
更改为true
自动 。 (但为什么会这样?)
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 + ".";
}
但是,“已购买”状态不是持久性。如果我关闭应用程序并再次启动它,它会认为我的“高级功能”“尚未购买”。
我认为如何使这种行为持续存在?
答案 0 :(得分:8)
请阅读
根据您的经验,我认为App.IsTrial设置为true。当应用程序处于试用模式或(由于任何其他原因)未激活时,您无法提交购买。请注意,我指的是应用程序,而不是功能/产品。一旦应用程序处于活动状态,您的购买就会成功。
答案 1 :(得分:3)
据我所知,当您单击“确定”时,它不会自动更新。它只返回正确的状态,就像在Windows应用商店中一样。
我认为这是一个可行的选择,但我自己没有实现它是使用ReloadSimulatorAsync(proxyFile)
方法。在调用此方法之前,您必须自己更新in-app-purchase.xml并保存它并将文件传递给ReloadSimulatorAsync方法。
我个人会使用xml文件的手动更新。但我无法判断您的申请/申请流程。
自动执行此流程的良好开端是“应用内购买”应用示例,您可以找到here。如果您查看代码文件InAppPurchase.xaml.cs,您将看到已经有一些代码用于您自己的实现。
答案 2 :(得分:0)
我要清楚这一点。执行时
try {
await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
OK = true
}
catch (Exception)
{
OK = false;
}
如果用户购买了该功能,那么不仅如此,如果他取消购买或者无法识别他或者他禁用网络连接也是如此。所以这是检查购买是否成功的错误方法。基本上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不会对许可证进行永久性更改。 我希望这是真的:))
答案 3 :(得分:0)
购买后不必立即检查许可证IsActive
。相反,您可以(您最好)存储RequestProductPurchaseAsync
的结果,该结果的类型为PurchaseResults
且具有Status
属性。后者依次为ProductPurchaseStatus
枚举值,如果为Succeeded
,则无需检查许可即可继续。
答案 4 :(得分:-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));
}
}