Skip to content

Instantly share code, notes, and snippets.

@brianmatic
Last active June 28, 2017 12:38
Show Gist options
  • Save brianmatic/41a09e17d6044139d76a91fac757d9ce to your computer and use it in GitHub Desktop.
Save brianmatic/41a09e17d6044139d76a91fac757d9ce to your computer and use it in GitHub Desktop.
Purchase code using InAppBilling Plugin
public Command UnlockReports
{
get
{
return new Command(async (param) =>
{
if (IsBusy || SalesBySellerItems.All(p => p.HasPayed)) return;
await RunSafe(PromptBuyNowAsync(), true);
});
}
}
async Task PromptBuyNowAsync()
{
try
{
Tracker.TrackEvent("Reports Unlock", "TappedUnlock", "Event=" + Event.EventId);
var connected = await CrossInAppBilling.Current.ConnectAsync();
if (!connected)
{
//Couldn't connect
Tracker.TrackEvent("Reports Unlock Error: Unlock", "UnableToConnect");
await CoreMethods.DisplayAlert("Connect Error", "Unable to connect to the store. Please report to support@tallysheetapp.com.", "Ok");
return;
}
var items = await CrossInAppBilling.Current.GetProductInfoAsync(ItemType.InAppPurchase, new string[] { Config.PurchaseProductID });
if (items != null && items.Count() > 0)
{
PurchaseStartedResponse startedResponse = await TrackPurchaseStarted(items.First());
Tracker.TrackEvent("Reports Unlock", "LoggedStarted", "UserId=" + startedResponse.UserId + ", Event=" + startedResponse.EventId);
if (startedResponse != null && startedResponse.InAppPurchaseStartedId != 0)
{
Tracker.TrackEvent("Reports Unlock", "PurchaseStarted", "UserId=" + startedResponse.UserId + ", Event=" + startedResponse.EventId);
var purchaseSuccess = await Purchase(startedResponse.InAppPurchaseStartedId, items.First());
Tracker.TrackEvent("Reports Unlock", "PurchaseEnded", "UserId=" + startedResponse.UserId + ", Event=" + startedResponse.EventId);
//await CoreMethods.PopPageModel(purchaseSuccess, true, false);
UpdateReportPurchaseInfo(purchaseSuccess);
HasPayed = purchaseSuccess;
}
else
{
await CoreMethods.DisplayAlert("Purchase Error", "We had an problem starting this purchase. Please report to support@tallysheetapp.com if it continues.", "Ok");
}
}
}
catch (Exception ex)
{
Tracker.TrackException("Unlock Reports:PromptBuyNowAsync:catch(Exception): " + ex.ToString(), false);
await CoreMethods.DisplayAlert("Purchase Error", "Something went wrong. " + ex.Message + ". Please report to support@tallysheetapp.com if it continues.", "Ok");
}
finally
{
await CrossInAppBilling.Current.DisconnectAsync();
UserDialogs.Instance.HideLoading();
IsBusy = false;
}
}
async Task<PurchaseStartedResponse> TrackPurchaseStarted(InAppBillingProduct product)
{
var model = new InAppPurchaseStartedModel();
model.AppStore = (Device.RuntimePlatform == Device.iOS) ? "Apple" : "PlayStore";
model.Product = product.ProductId;
model.EventId = Event.EventId;
var reports = await _apiManager.PurchaseStartAsync(model);
return reports;
}
async Task RegisterPurchase(int purchaseStartedId, string purchaseId, string purchaseToken, InAppBillingProduct product)
{
var transaction = new InAppPurchaseTransactions();
transaction.Amount = product.LocalizedPrice;
transaction.CurrencyCode = product.CurrencyCode;
transaction.AppStore = (Device.RuntimePlatform == Device.iOS) ? "Apple" : "PlayStore";
transaction.Product = product.ProductId;
transaction.InAppPurchaseStartedId = purchaseStartedId;
transaction.MicrosPrice = product.MicrosPrice;
transaction.EventId = Event.EventId;
//Purchased, save this information
transaction.PurchaseId = purchaseId ?? purchaseToken;
transaction.PurchaseToken = purchaseToken ?? purchaseId;
var reports = await _apiManager.PurchaseEndAsync(transaction);
}
async Task<bool> Purchase(int purchaseStartedId, InAppBillingProduct product)
{
try
{
//try to purchase item
Tracker.TrackEvent("Reports Unlock", "Calling PurchaseAsync", "EventId=" + Event.EventId);
if (Device.RuntimePlatform == Device.Android)
{
var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(product.ProductId, ItemType.InAppPurchase, "apppayload");
if (consumedItem != null)
{
await RegisterPurchase(purchaseStartedId, consumedItem.Id, consumedItem.PurchaseToken, product);
Tracker.TrackEvent("Reports Unlock", "PendingPurchaseConsumed", value: product.MicrosPrice);
return true;
}
}
UserDialogs.Instance.HideLoading();
var purchase = await CrossInAppBilling.Current.PurchaseAsync(product.ProductId, ItemType.InAppPurchase, Event.EventId.ToString());
if (purchase == null)
{
Tracker.TrackEvent("Reports Unlock Error", "InAppBillingPurchase=Null");
Tracker.TrackException("Purchase Unsuccessful:Event=" + Event.EventId, false);
await CoreMethods.DisplayAlert("Purchase Error", "Something went wrong. Please report to support@tallysheetapp.com if it continues.", "Ok");
}
else
{
bool purchaseDone = true;
if (Device.RuntimePlatform == Device.Android)
{
var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);
purchaseDone = consumedItem != null;
if (purchaseDone)
Tracker.TrackEvent("Reports Unlock", "PurchaseConsumed", value: product.MicrosPrice);
else
Tracker.TrackEvent("Reports Unlock", "PurchaseNotConsumed", value: product.MicrosPrice);
}
if (purchaseDone)
{
await RegisterPurchase(purchaseStartedId, purchase.Id, purchase.PurchaseToken, product);
Tracker.TrackEvent("Reports Unlock", "PurchaseSuccess", value: product.MicrosPrice);
return true;
}
else
{
Tracker.TrackEvent("Reports Unlock", "PurchaseFailed", value: product.MicrosPrice);
return false;
}
}
}
catch (InAppBillingPurchaseException ex)
{
Tracker.TrackException("Purchase Error:Purchase:catch (InAppBillingPurchaseException ex): " + ex.ToString(), false);
switch (ex.PurchaseError)
{
case PurchaseError.UserCancelled:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "UserCancelled", Event.EventId.ToString());
await CoreMethods.DisplayAlert("You Canceled :(", "We hope you reconsider in the future. Please contact us at support@tallysheetapp.com if you are having trouble.", "Ok");
break;
case PurchaseError.BillingUnavailable:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "BillingUnavailable", Event.EventId.ToString());
await CoreMethods.DisplayAlert("Billing Unavailable", ex.Message + ". Please contact us at support@tallysheetapp.com if it continues.", "Ok");
break;
case PurchaseError.ItemUnavailable:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "ItemUnavailable", Event.EventId.ToString());
await CoreMethods.DisplayAlert("Item Unavailable", ex.Message + ". Please contact us at support@tallysheetapp.com if it continues.", "Ok");
break;
case PurchaseError.PaymentInvalid:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "PaymentInvalid", Event.EventId.ToString());
await CoreMethods.DisplayAlert("Payment Invalid", ex.Message + ". Please contact us at support@tallysheetapp.com if it continues.", "Ok");
break;
case PurchaseError.PaymentNotAllowed:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "PaymentNotAllowed", Event.EventId.ToString());
await CoreMethods.DisplayAlert("Payment Not Allowed", ex.Message + ". Please contact us at support@tallysheetapp.com if it continues.", "Ok");
break;
case PurchaseError.GeneralError:
Tracker.TrackEvent("Reports Unlock:TappedBuyNow", "General", ex.ToString());
//await CoreMethods.DisplayAlert("General Error", ex.Message + ". Please contact us at support@tallysheetapp.com if it continues.", "Ok");
break;
}
}
catch (Exception ex)
{
Tracker.TrackException("Unlock Reports:TappedBuyNow:catch(Exception): " + ex.ToString(), false);
await CoreMethods.DisplayAlert("Purchase Error", ex.Message + ". Please report to support@tallysheetapp.com if it continues.", "Ok");
}
return false;
}
}
MainActivity:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
CallbackManager.OnActivityResult(requestCode, (int)resultCode, data);
InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment