jamesmontemagno / InAppBillingPlugin

Cross-platform In App Billing Plugin for .NET
MIT License
651 stars 152 forks source link

Consume and Purchase returns null after redeem #100

Closed Yohandah closed 6 years ago

Yohandah commented 6 years ago

Version Number of Plugin: Last Device Tested On: Samsung Galaxy S8 Oreo Simulator Tested On: Version of VS: Last Version of Xamarin: Last Versions of other things you are using:

Steps to reproduce the Behavior

Create a product and create a promotion code, redeem the code in the app, purchase returns null and consume also for this productId

Expected Behavior

It should not return null

Actual Behavior

It returns null

Code snippet

try
{
                var connected = await CrossInAppBilling.Current.ConnectAsync();

                if (!connected)
                {
                    //Couldn't connect to billing, could be offline, alert user
                    return;
                }

                var purchases = await CrossInAppBilling.Current.GetPurchasesAsync(ItemType.InAppPurchase);
                Console.WriteLine(purchases);
//trying to consume it because just above I can see that I have the item and it's not consumed
                var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(productId, ItemType.InAppPurchase, "mypayload");

                var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "mypayload");
                if (purchase == null)
                {
                    Toast.MakeText(this,"not purchased", ToastLength.Long).Show();
                }
                else
                {
                    //Purchased, save this information
                    var id = purchase.Id;
                    var token = purchase.PurchaseToken;
                    var state = purchase.State;
                    //var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(productId, Plugin.InAppBilling.Abstractions.ItemType.InAppPurchase, "mypayload");

                    if (consumedItem != null)
                    {
                        Toast.MakeText(this, "Item Consumed", ToastLength.Short).Show();
                    }
                    else
                    {
                        Toast.MakeText(this, "Item NOT CONSUMED", ToastLength.Short).Show();
                    }
                }
            }
            catch (System.Exception ex)
            {
                //Something bad has occurred, alert user
                Console.WriteLine(ex);
            }
            finally
            {
                //Disconnect, it is okay if we never connected
                await CrossInAppBilling.Current.DisconnectAsync();
            }

Screenshotst

SkillerSKL commented 6 years ago

same issue here :s

jamesmontemagno commented 6 years ago

hmmmm, i am not sure how that works with a promo code works.

What is the ConsumptionState of the original purchase?

jamesmontemagno commented 6 years ago

You should also really use: ConsumePurchaseAsync(string productId, string purchaseToken)

It is the only reliable method here.. and you HAVE THE TOKEN! Use it! Else I just try to find the first one in the list.

I could probably check the state for not consumed as an optimization.

Yohandah commented 6 years ago

@jamesmontemagno The ConsumptionState of the original purchase was NotYetConsumed, but using ConsumePurchaseAsync(string productId, string purchaseToken) is working !

On the other hand, PurchaseAsync(productId, ItemType.InAppPurchase, "payload") returns null when using a promo code .. :s using a credit card is ok

EmilAlipiev commented 6 years ago

@meepou so ConsumePurchaseAsync worka for both promocode and other payment methods? can we safely call it instead of PurchaseAsync? actually PurchaseAsync also works but return object is just null but when I check with GetPurchasesAsync, i can see that it is in the list.

EmilAlipiev commented 6 years ago

This makes no sense. I just went through the code and ConsumePurchaseAsync just just checkes if you have purchase and uses the token from the found purchase token. even in another case, I need promotion code to be working for non-consumable products. ConsumePurcahseAsync only works for consumables I believe. how can we resolve this please?

jamesmontemagno commented 6 years ago

normal purchaseasync should work just fine I think. Else would need to debug through

EmilAlipiev commented 6 years ago

It is unfortunately not working. i mean that purchase is working but result from purchaseasync is always null. it could be some serialization problem in the application. I recognized that although i am passing payload for that action, when using redeem code, response has no payload but same purchase with credit card has payload. I wanted to debug attaching source code on my .net standard 2.0.2 project but i am getting error for current activity plugin is not recognized somehow. i will try with a fresh project. please let me know if you can test it faster :)

image

jamesmontemagno commented 6 years ago

are you passing it a "" payload? i just fixed a bug that was returning null here. Update to the latest beta if that is the case or pass it a payload, which is best practice.

jamesmontemagno commented 6 years ago

hmmm the Id is null?

jamesmontemagno commented 6 years ago

Please comment: https://github.com/jamesmontemagno/InAppBillingPlugin/issues/143