Android Question Play Store response for 'Restore Purchases' not giving updated purchase

Cliff McKibbin

Member
Licensed User
My App allows the user to purchase an annual subscription. On a test device, I purchased the subscription which worked fine when I first made the purchase. The annual (year) was up on 6/8/2021 and I received an email notice from the Play store that I had paid for the next year.

However, the logic flow in the app for 'Restore purchases' is still picking up the original purchase as you can see in the log below. It should be giving me a new p.purchasetime and p.orderid

As an aside, I had tested a purchase followed by a cancel in the Play Store to test out my logic for handling that earlier in the year, and I received the update for that with autorenewing=false so updates were coming in.

Logger connected to: samsung SAMSUNG-SM-J727AZ
--------- beginning of main
*** Service (starter) Create ***

Query completed: true
p.Sku:annual
p.OrderId = GPA.3358-5266-2632-19010
p.PurchaseState = 1
p.STATE_PURCHASED = 1
p.PurchaseTime = 20200608
p.Sku = annual
p.IsAcknowledged = true
p.DeveloperPayload =
p.IsAutoRenewing = true
Purchase Completed
starter.Ipurchase:2
Starter.iPurchase:2

You can see the p.purchasetime is still set to last year and p.orderid does not have the expected ..0 at the end to indicate the first renewal.

Here is the confirmation from the Play Store for my purchase.
You can note the Order Number does have the ..0 at the end and says it will again auto renew on Jun 8, 2022.

Thank you.
Your subscription from RateMyShows, LLC on Google Play has renewed. Manage your subscriptions.
Order number: GPA.3358-5266-2632-19010..0
Order date: Jun 8, 2021 6:49:14 PM EDT
Item Price
Annual Subscription (Rate My Shows on Netflix, HBO, Prime, Disney, etc.) $2.99/year
Yearly Subscription ‐ Auto Renews on Jun 8, 2022
Tax: $0.19
Total: $3.18/year
Payment method: xxx
By subscribing you authorize us to charge you the subscription cost (as described above) automatically, charged yearly to the payment method provided until canceled. Keep this for your records.
You're subscribed with your account [email protected]

Finally, to confirm the purchase is for the test device, I went into the Play Store from my test device and I looked at the app and looked at the subscription option and I saw my subscription had been updated.

Below is the code (in Starter) that I had found in one of several forum entries related to handling Play Store Subscriptions.
The flow starts with sub 'Restore Purchases', passes the test for my p.sku, and then jumps to sub 'HandleAdsPurchase(p)' which shows the log. There was only one log entry so that tells me (I think) that the 'For each p....." loop is only finding one entry. At one point I thought that these purchases might be stacked, but it doesn't seem the case.

The logic passes the steps to check the 'signature' as well as the Acknowledge=true.
It ends with the log of 'Purchase Completed'.

The logic related to variable 'iPurchased' is internal to my app and not related to the Play Store logic. It then updates my internal flags with the new payment date and my internal purchase code. I use these to control 'Premium' features.

The question is "Is there a step I am missing that needs to clear the old year, or something along that line, in order to let the new order flow into my App".
I also wondered if there was some kind of delay time, but if I recall, the delay from the original purchase was zero.

Thanks for any help, Cliff McKibbin



B4X:
public Sub RestorePurchases
	Dim bPurchase As Boolean
		bPurchase=False
	
	Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
	If Result.IsSuccess Then
		Wait For (billing.QueryPurchases("subs")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
		Log("Query completed: " & Result.IsSuccess )
		If Result.IsSuccess Then
			For Each p As Purchase In Purchases
				Log ("p.Sku:" & p.Sku)
				If p.Sku = ADS_SDK_ID Then 
					bPurchase=True
					HandleAdsPurchase(p)
				End If
			Next
		End If
	End If
	If bPurchase=False Then CancelPurchase
		
End Sub

public Sub CancelPurchase
	' sub to handle undo of last purchase or revert to unpaid trial
	' note also called if bautorenew is turned off
	
	If iPurchased=1 Then
		' ok still in trial
	End If

	If iPurchased=2 Then
		' indicate they have cancelled a purchase
		' note-google does not give a refund and they keep the year they paid for  5/29/20 cwm
		iPurchased=3
		' now write to setup
		WriteDB ("update setup set SData='" & iPurchased & "' where Scode=22")
	End If
			
	If iPurchased=3 Then
		' ok already know they reversed the purchase
	End If
		
	CallSub(Main, "DisplayStatus")
	
	
End Sub
Sub billing_PurchasesUpdated (Result As BillingResult, Purchases As List)
	'This event will be raised when the status of one or more of the purchases has changed.
	'It will usually happen as a result of calling LaunchBillingFlow however it can be called in other cases as well.
	If Result.IsSuccess Then
		For Each p As Purchase In Purchases
			If p.Sku = ADS_SDK_ID Then
				HandleAdsPurchase(p)
			Else
				Log("Unexpected product...")
			End If
		Next
	End If
End Sub

Public Sub HandleAdsPurchase (p As Purchase)
	Dim sPurDate As String
	DateTime.Dateformat="yyyyMMdd"
	sPurDate=DateTime.Date (p.PurchaseTime)
	
	' log the p contents
	Log("p.OrderId          = " & p.OrderId)
	Log("p.PurchaseState    = " & p.PurchaseState )
	Log("p.STATE_PURCHASED  = " & p.STATE_PURCHASED)
	Log("p.PurchaseTime     = " & sPurDate )
	Log("p.Sku              = " & p.Sku )
	Log("p.IsAcknowledged   = " & p.IsAcknowledged)
	Log("p.DeveloperPayload = " & p.DeveloperPayload)
	Log("p.IsAutoRenewing   = " & p.IsAutoRenewing)

	
	If p.PurchaseState <> p.STATE_PURCHASED Then 
		CancelPurchase
	Else
			
		'Verify the purchase signature.
		If billing.VerifyPurchase(p, BILLING_KEY) = False Then
			Log("Invalid purchase")
			Return
		End If
		If p.IsAcknowledged = False Then
			'we either acknowledge the product or consume it.
			Wait For (billing.AcknowledgePurchase(p.PurchaseToken, "")) Billing_AcknowledgeCompleted (Result As BillingResult)
			Log("Acknowledged: " & Result.IsSuccess)
		End If
		
		Log("Purchase Completed")
		
		' set the autorenew     6/14/20 cwm  
		If p.IsAutoRenewing Then
			bAutoRenew=True
			Else
			bAutoRenew=False	
		End If
		
		' update our payment setup fields here
		If sPurDate.CompareTo (SLastPayment)>0 Then
			' register the payment
			SLastPayment=sPurDate 
			iPurchased=2
			' add the length of the premium to the payment date
			' note this makes our end date=play store ending date 
			SNextPayment=DateTime.Date(p.PurchaseTime + DateTime.TicksPerDay*iPremiumPeriod)
			
			' now write to setup
			WriteDB ("update setup set SData='" & SNextPayment & "' where Scode=21")
			WriteDB ("update setup set SData='" & iPurchased & "' where Scode=22")
			WriteDB ("update setup set SData='" & SLastPayment & "' where Scode=23")
		End If
		
		' force cancel purchase if not autorenew
		If bAutoRenew=False Then 
			CancelPurchase
		Else
			If iPurchased=3 Then
				' reset back to purchased    6/15/20
				iPurchased=2
				WriteDB ("update setup set SData='" & iPurchased & "' where Scode=22")
			End If
		End If
		
	End If	
	
	CallSub(Main, "DisplayStatus")
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The question is "Is there a step I am missing that needs to clear the old year, or something along that line, in order to let the new order flow into my App".
No. Everything happens in Google Play servers.

However, the logic flow in the app for 'Restore purchases' is still picking up the original purchase as you can see in the log below. It should be giving me a new p.purchasetime and p.orderid
Why? The user state is exactly as before. Why do you need to test the purchase time?
 
Upvote 0

Cliff McKibbin

Member
Licensed User
Erel, Thanks for the update.
I had presumed that the response would include the updated 'purchase time' and 'order id'. I was then:
1. displaying a message on the main screen thanking them for their renewal
2. updating my setup variable for 'last payment'
3. displaying in 'about' that they are paid up through 'mm/dd/yyyy' (last payment + 1 year).

Rather than trying to add logic to bump up the year on my own, I may just change the messages to indicate something like 'premium member since mm/dd/yyyy, your next payment will be on mm/dd/yyy'. I can compute the latter by adding to the original purchase date until I get past 'now'.

I already have logic to handle the transition to 'non renewal' at the end of their current year, but will check to make sure it is ok. It may be based on the presumption that my 'last payment date' is being bumped.

Thanks for the update and all of your continued dedication to the product, Cliff McKibbin
 
Upvote 0
Top