应用内购买(In-App Purchase)对于开发者来讲绝对是一个很是重要的功能,它提供了一个便捷的入口供用户来购买付费。在IAP盛行以前的游戏运营商通常都是经过接入第三方支付入口来收费。以前作过的一个安卓手机游戏服务器(Asp.Net),他们采用的付费方式有两种,一个是接入支付宝的接口,让用户能够经过支付宝来付费。还有一种是经过手机运营商来付费,先由用户把钱付给运营商,运营商经过你注册的服务器的API告知该用户已付费。在Windows Phone 8中就不用担忧第三方付费服务器的问题了,微软为咱们提供了一个付费的功能,也就是以前提到的IAP,付费的整个过程都是由微软的交易平台(Microsoft Commerce Platform)来提供支持的,经过Windows.ApplicationModel.Store命名空间下的API能够很是容易的实现IAP的功能,省去了不少接入第三方付费接口的调试时间。下面这张图介绍了建立和购买虚拟物品的流程。web
a/b:开发者经过Dev Center注册本身的付费App和全部虚拟物品信息。若是商品要从服务器下载,那么你须要本身提供这个服务器。windows
c/d:开发者拉取Store上注册的虚拟物品并展现在App内,由用户来点击购买。后端
e/f:微软交易平台告知你的App用户付费成功,并获取电子收据。服务器
g/h:经过微软交易平台提供的收据做为凭证,从你的服务器下载虚拟物品。并告知交易平台该商品已经成功分发。app
整个过程仍是很是简单清晰的,须要注意的是若是你的游戏很是的轻量,好比是个单机游戏,那么虚拟商品就不须要服务器的支持,电子收据这步能够省略掉。大型的手机游戏通常都会有后端服务器的支持。电子收据为咱们从服务器下载插件时提供了有效的凭证。下面咱们就先省略掉注册应用和虚拟物品这两个步骤,作一个简单客户端IAP的例子。异步
private async void LoadListingInformationAsync() { try { // Query the Windows Phone Store for the in-app products defined for the currently-running app. var listingInformation = await CurrentApp.LoadListingInformationAsync(); foreach (var v in listingInformation.ProductListings) { Products.Add(new ProductViewModel() { Title = v.Value.Name, ProductId = v.Value.ProductId, ImageUri = v.Value.ImageUri }); } } catch { // An exception is expected to be raised by CurrentApp.LoadListingInformationAsync()
//when it is not called from an app installed from the Windows Phone Store. } }
上面这段代码就是异步的从Store中获取咱们事先注册好的虚拟物品集合。CurrentApp是一个很是重要的类,它包含了全部主要操做虚拟物品的方法。经过上面的方法获取到虚拟物品的信息后,咱们就能够对数据作进一步的处理,例子当中用一个ObservableCollection类型的集合Producets来保存数据,并经过databinding将其展现在商品页面上,供用户选择购买。async
private async void PurchaseProduct(string productId) { try { // Kick off purchase; don't ask for a receipt when it returns await CurrentApp.RequestProductPurchaseAsync(productId, false); // Now that purchase is done, give the user the goods they paid for // (DoFulfillment is defined later) //DoFulfillment(); } catch (Exception ex) { // When the user does not complete the purchase (e.g. cancels or navigates back from the Purchase Page),
// an exception with an HRESULT of E_FAIL is expected. } }
当用户肯定要购买某项虚拟物品的时候经过调用RequestProductPurchaseAsync方法来开启用户付费的过程,这个方法须要两个参数,一个是以前获取到的虚拟物品的id,另外一个是bool类型的参数,表明在付费成功后是否要返回购买虚拟物品的电子收据,电子收据是一个xml格式的信息。若是用户没有完成付费操做,好比取消或者经过back键返回等,都会触发一个错误类型为E_FALL的Exception。this
public void DoFulfillment() { var productLicenses = CurrentApp.LicenseInformation.ProductLicenses; DistributeProduct(productLicenses); } private void DistributeProduct(IReadOnlyDictionary<string, ProductLicense> productLicenses) { var bagOfSilver = new Regex(@"Bag\.Silver\.(\d+)"); foreach (ProductLicense license in productLicenses.Values) { if (license.IsConsumable && license.IsActive) { var m = bagOfSilver.Matches(license.ProductId); if ((m.Count == 2) && (m[1].Success)) { m_silverCount += int.Parse(m[1].Value); CurrentApp.ReportProductFulfillment(license.ProductId); } } } }
当用户付费成功后咱们就能够在App里面将虚拟物品分发给用户,经过CurrentApp.LicenseInformation.ProductLicenses获取到用户购买的虚拟物品的受权license,ProductLicenses是一个IDictionary类型的集合,能够经过TryGetValue方法获取到某个商品的受权。IsConsumable属性表明商品能够进行消费,IsActive属性表明这个license是有效的。若是license有效,咱们就能够把虚拟物品分发给用户了。最后还要调用ReportProductFulfillment方法告诉Store商品已经分发,整个购买过程结束。spa
public async Task<bool> LoadLevelAsync(string levelProductId) { var license = CurrentApp.LicenseInformation.ProductLicenses[levelProductId]; if (!license.IsActive) { // User doesn't own this level return false; } if (!IsLevelDownloaded(levelProductId)) { var receiptXml = await CurrentApp.GetProductReceiptAsync(levelProductId); await DownloadLevelAsync(receiptXml); } // TODO: Load the level return true; } private async Task DownloadLevelAsync(string receiptXml) { var webReq = (HttpWebRequest)WebRequest.Create(sc_DownloadUrl); webReq.Method = "POST"; AddStringToWebRequestStream(webReq, receiptXml); WebResponse response = await webReq.GetResponseAsync(); // TODO: Save the level to disk }
若是咱们的虚拟物品要从本身的服务器下载好比皮肤和主题文件,使用GetProductReceiptAsync方法获取电子收据recipt。有了电子收据方便咱们的服务器对恶意的商品请求作出过滤,避免第三方经过不法手段获取虚拟商品。插件
Windows Phone Store的IAP已经封装成了一个很是方便的接口,若是你尚未注册虚拟物品到Dev Center能够先经过CurrentAppSimulator类来模拟整个过程。IAP绝对是一个有益于开发者的特性,能够吸引更多的开发者来完善整个生态圈。那么就开始你的IAP之旅吧。