title: Monogame: Windows Phone 8 IAP (In App Purchases)
body: <p>I've been having trouble implementing IAP for Windows Phone 8 on Monogame. I've been following the API documentation: <a href="http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206950%28v=vs.105%29.aspx" rel="nofollow">WP8 In-App Purchase API</a>
  <br><br>As Monogame/XNA updates differently to Silverlight apps, I'm running IAP on the UI thread. I've tested with a beta app on the store, and can load the Store Front UI and display my product. When I attempt to purchase, it seems like it makes the purchase, tries to go back to the game but crashes. Further loads of the app don't display the store at all after this.
  <br><br>
  I've searched everywhere for a solution for this with no luck. I'd be really grateful if anyone could help me with this.</p>
  
  <p>Here is my code sample:</p>
  
  <pre><code>public static void InvokeOnUIThread(Action action)
      {
          #if WINDOWS8 || OPENGL
          action();
          #else
          var dispatcher = System.Windows.Deployment.Current.Dispatcher;
  
          if (dispatcher.CheckAccess() /*IsUIThread*/)
              action();
              else
              dispatcher.BeginInvoke(action);
          #endif
      }
  </code></pre>
  
  <p>I use it to invoke InvokeOnUIThread(Part1Purchase);</p>
  
  <pre><code>    async void Part1Purchase()
      {
          ListingInformation li = await CurrentApp.LoadListingInformationAsync();
          string pID = li.ProductListings["img.1"].ProductId;
          try
          {
              string receipt = await Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(pID, false);
  
              Part2Fulfillment();
          }
          catch (Exception ex)
          {}
      }
  
      public void Part2Fulfillment()
      {
          var productLicenses = Windows.ApplicationModel.Store.CurrentApp.LicenseInformation.ProductLicenses;
          Part3DeliverGold(productLicenses["img.1"], 50);
      }
  
      void Part3DeliverGold(ProductLicense license, int goldCount)
      {
          if (license.IsConsumable &amp;&amp; license.IsActive)
          {
              gold += goldCount;
              CurrentApp.ReportProductFulfillment(license.ProductId);
          }
      }
  </code></pre>
  
  <p>I also tries this method to execute on the UI thread, but had the same results (crashing on purchase completion)</p>
  
  <pre><code>    private void RestoreTitleDelayed(int delay)
      {
          ThreadPool.QueueUserWorkItem((object o) =&gt;
              {
                  Thread.Sleep(delay);
                  System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
                  {
                      Part1Purchase();
                  });
              });
      }
  </code></pre>