聊到应用内购买 In app purchase 是目前来说应用最容易收益的一种做法,和7.5提供“试用“有异曲同工之处 但WP8是做法更为友好贴近用户使用习惯,也为大家的应用带来更多赚钱的机会,因为In app purchase的商品分为 (持久形 - Durable)例如武游戏中的器装备 和 (消耗形 - Consumable)游戏中的食品和金币 并且支持简单的支持流程.

首先在我们的手机钱包中可以绑定支付宝 Alipay 账户我们可以使用该账户进行应用购买和支付。

此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。

同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick

引用自 MSDN 这里解释的非常清楚了 所以我还是给出连接

按部就班我一个一个的来给大家介绍实现过程:

1. 商店提交应用 我之前已经介绍过了 参考

2. 提交你的应用内支付商品

visual studio 的设置 

经过以上的操作在我们的应用中就可以拿到productID中的商品了 当然是要审核通过的。

这里我们使用到了CurrentApp class 当然如果你没有通过审核也是可以使用 CurrentAppSimulator class进行模拟的 配置方法

这里常用的 loadlistingInformationAsync() 来获取所有的商品

 
  
  1. private async void btnListIAPProducts_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
  2.       { 
  3.           try 
  4.           { 
  5.               var ProdList = await CurrentApp.LoadListingInformationAsync(); 
  6.               lbProductsList.Items.Clear(); 
  7.               string t = ""
  8.  
  9.               foreach (var item in ProdList.ProductListings) 
  10.               { 
  11.                   t = string.Format("{0}, {1}, {2},{3}, {4}"
  12.                                       item.Key
  13.                                       item.Value.Name
  14.                                       item.Value.FormattedPrice, 
  15.                                       item.Value.ProductType, 
  16.                                       item.Value.Description); 
  17.  
  18.                   lbProductsList.Items.Insert(0, t); 
  19.               } 
  20.           } 
  21.           catch (Exception ex) 
  22.           { 
  23.               MessageBox.Show("Error: " + ex.Message); 
  24.           } 
  25.       } 

如图所示

既然可以拿到商品列表了 怎么进行购买呢?其实代码也很简单还是CurrentApp

 
  
  1. private async void btnOrderProduct_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
  2.     var ProdList = await CurrentApp.LoadListingInformationAsync(); 
  3.     var Prod = ProdList.ProductListings.FirstOrDefault(p => p.Value.ProductType == ProductType.Consumable); 
  4.     try 
  5.     { 
  6.         var Receipt = await CurrentApp.RequestProductPurchaseAsync(Prod.Value.ProductId, true); 
  7.  
  8.         if (CurrentApp.LicenseInformation.ProductLicenses[Prod.Value.ProductId].IsActive) 
  9.         { 
  10.             // do someting with this license... 
  11.  
  12.             // Notify the marketplace that the application has delivered the paid-for goods to the user.  
  13.             CurrentApp.ReportProductFulfillment(Prod.Value.ProductId); 
  14.         } 
  15.         MessageBox.Show(Receipt, "Fatura", MessageBoxButton.OK); 
  16.     } 
  17.     catch (Exception ex) 
  18.     { 
  19.         MessageBox.Show(ex.Message, "Fatura", MessageBoxButton.OK); 
  20.     } 

以上的代码 也可以参考

CurrentApp.RequestProductPurchaseAsync 打开应用商店进入支付页面,支付成功后 CurrentApp.LicenseInformation.ProductLicenses[Prod.Value.ProductId].IsActive 会返回true

CurrentApp.ReportProductFulfillment 是告知 MSservice 完成购买。

当然在判断支付成功后如果有需要的话还要和自己或product service同步。如果你没有开发者账号和应用 也可以自己搭建测试环境 请参考 

相信我说到这里大家已经对 windows phone 8 应用内购买 / 应用内支付 有了大概的了解,同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick