Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #125 #128

Merged
merged 2 commits into from
Mar 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions src/Plugin.InAppBilling.Android/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,46 +286,51 @@ public async override Task<InAppBillingPurchase> PurchaseAsync(string productId,

async Task<Purchase> PurchaseAsync(string productSku, string itemType, string payload, IInAppBillingVerifyPurchase verifyPurchase)
{
lock (purchaseLocker)
{
if (tcsPurchase != null && !tcsPurchase.Task.IsCompleted)
return null;

if (tcsPurchase != null && !tcsPurchase.Task.IsCompleted)
return null;
Bundle buyIntentBundle = serviceConnection.Service.GetBuyIntent(3, Context.PackageName, productSku, itemType, payload);
var response = GetResponseCodeFromBundle(buyIntentBundle);

tcsPurchase = new TaskCompletionSource<PurchaseResponse>();
switch (response)
{
case 0:
//OK to purchase
break;
case 1:
//User Cancelled, should try again
throw new InAppBillingPurchaseException(PurchaseError.UserCancelled);
case 2:
//Network connection is down
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable);
case 3:
//Billing Unavailable
throw new InAppBillingPurchaseException(PurchaseError.BillingUnavailable);
case 4:
//Item Unavailable
throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);
case 5:
//Developer Error
throw new InAppBillingPurchaseException(PurchaseError.DeveloperError);
case 6:
//Generic Error
throw new InAppBillingPurchaseException(PurchaseError.GeneralError);
case 7:
//already purchased
throw new InAppBillingPurchaseException(PurchaseError.AlreadyOwned);
}

Bundle buyIntentBundle = serviceConnection.Service.GetBuyIntent(3, Context.PackageName, productSku, itemType, payload);
var response = GetResponseCodeFromBundle(buyIntentBundle);

switch (response)
{
case 0:
//OK to purchase
break;
case 1:
//User Cancelled, should try again
throw new InAppBillingPurchaseException(PurchaseError.UserCancelled);
case 2:
//Network connection is down
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable);
case 3:
//Billing Unavailable
throw new InAppBillingPurchaseException(PurchaseError.BillingUnavailable);
case 4:
//Item Unavailable
throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);
case 5:
//Developer Error
throw new InAppBillingPurchaseException(PurchaseError.DeveloperError);
case 6:
//Generic Error
var pendingIntent = buyIntentBundle.GetParcelable(RESPONSE_BUY_INTENT) as PendingIntent;
if (pendingIntent == null)
throw new InAppBillingPurchaseException(PurchaseError.GeneralError);
case 7:
//already purchased
throw new InAppBillingPurchaseException(PurchaseError.AlreadyOwned);
}

var pendingIntent = buyIntentBundle.GetParcelable(RESPONSE_BUY_INTENT) as PendingIntent;
if (pendingIntent != null)
tcsPurchase = new TaskCompletionSource<PurchaseResponse>();

Context.StartIntentSenderForResult(pendingIntent.IntentSender, PURCHASE_REQUEST_CODE, new Intent(), 0, 0, 0);
}

var result = await tcsPurchase.Task;

Expand Down Expand Up @@ -565,6 +570,7 @@ class PurchaseResponse

InAppBillingServiceConnection serviceConnection;
static TaskCompletionSource<PurchaseResponse> tcsPurchase;
static readonly object purchaseLocker = new object();

static bool ValidOwnedItems(Bundle purchased)
{
Expand Down