title: Back Button/ Cancel Button during In App Purchase
body: <p>I am having trouble detecting the Exception that is thrown by the in-app-purchase store during Unit Test (Beta app) for Windows Phone 8 when I press the Cancel or Back button on the phone.  The app simply exits. </p>
  
  <p>There are no errors when I use the MockIAP. Cancel or Back Button returns an empty receipt variable during the <code>await receipt = Store..</code>.  It is handled correctly in MockIAP. But apparently Unit Test and the real app Store handleds Cancel or Back events differently. The app simply exits, which I believe because it is throwing an unhandled error. </p>
  
  <p>My app is a Phonegap 2.3 and the purchase part is handled by the plugin. Unlike the MockIAP, I can't see (i.e. attach break points) what is happening on the wrapper side when Cancel or Back button is pressed during purchase.  I have tried showing <code>MessageBox.Show</code> for every step of the purchase.  The <code>MessageBox.Show</code> code is working when I press confirm purchase but not when I press Cancel or Back Button.  I have made it synchronous already with <code>EventWaitHandle</code>. </p>
  
  <p>In addition, I have set <code>e.Handled = true</code> for the unhandled Exception event to try to stop it from exit the app with no luck. </p>
  
  <p>From online, my purchase code is boilerplate, so I dont' understand why other people hasn't come across this problem before, and why there are no solutions online.  Does anyone have any idea how to fix this?</p>
  
  <p>Purchase.cs (Plugin):</p>
  
  <pre><code>private static string receipt;
  
  private async void purchaseProduct()
          {
              bool canBuy = false;
              try
              {
                  li = await Store.CurrentApp.LoadListingInformationAsync();
  
                  if (li.ProductListings.ContainsKey(package_id))
                  {
                      canBuy = true;
  
                      EventWaitHandle Wait = new AutoResetEvent(false);
                      Deployment.Current.Dispatcher.BeginInvoke(async () =&gt;
                      {
  
                          // Here is the problem.. Don't know what is passed back to receipt when Cancel or Back is pressed, which is causing the app to close during Unit Test but not MockIAP
  
                          receipt = await Store.CurrentApp.RequestProductPurchaseAsync(package_id, true);
                          receipt = receipt.ToString();
                          Wait.Set();
                      });
  
                      Wait.WaitOne();
                  }
              }
              catch(Exception e)
              {
                  var eMsg = e.Message.ToString();
                  errorMsg("Catch Exception: ", eMsg);
                  DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
              }
              finally
              {
                  errorMsg("Receipt with await: ", receipt);
  
                  if (canBuy &amp;&amp; receipt!= "")
                  {
                      errorMsg("Hitting the parsing", "");
  
                      parseXML(receipt);
                      prepData();
                      httpPostData();
                      Store.CurrentApp.ReportProductFulfillment(package_id);
                  }
                  else
                  {
                      errorMsg("Else Finally", "");
  
                      DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
                  }
              }
          }
  
          private static void errorMsg(String caption, String msg)
          {
              EventWaitHandle Wait = new AutoResetEvent(false);
              Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
              {
                  MessageBox.Show(caption + msg);
                  Wait.Set();
              });
  
              Wait.WaitOne();
          }
  </code></pre>
  
  <p>App.cs</p>
  
  <pre><code>  private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
          {
              Exception ex = (Exception)e.ExceptionObject;
  
              EventWaitHandle Wait = new AutoResetEvent(false);
              Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
              {
                  MessageBox.Show("Unhandled Exception: " + ex.Message);
                  Wait.Set();
              });
  
              Wait.WaitOne();
  
              // Stop from exiting.. 
              e.Handled = true;
  
              if (System.Diagnostics.Debugger.IsAttached)
              {
                  // An unhandled exception has occurred; break into the debugger
                  //System.Diagnostics.Debugger.Break();
              }
          }
  </code></pre>