iOS Question Error getting receipt for in app subscription

Jack Cole

Well-Known Member
Licensed User
I'm trying to use this code to get the receipt for an IAP. The code was recommended by Erel.

B4X:
Dim no As NativeObject = Product
Dim b() As Byte = no.NSDataToArray(no.GetField("transactionReceipt"))

I am using it in the MyStore_PurchaseCompleted event. Product is of the Purchase type.

I get the following error on the second line of the above code:
Object was not initialized (NSObject)

I have tried the following:
1) uninstalled the app
2) cleaned the project and re-installed
3) restarted b4i
4) verified that the "no" NativeObject was initialized with .IsInitialized

Any ideas?
 
Last edited:

Jack Cole

Well-Known Member
Licensed User
Same outcome with adding the line of "If no.IsInitialized Then."

Here is Log(Product):

<B4IPurchase: <SKPaymentTransaction: 0x187b6010>>

Here is
Log("Product: " & Product.ProductIdentifier & ", date=" & DateTime.Date(Product.TransactionDate) &
", Transaction identifier=" & Product.TransactionIdentifier)

Product: mobi.mindware.mindgames.3months, date=05/01/2018, Transaction identifier=1000000394872000
 
Upvote 0

tufanv

Expert
Licensed User
Same outcome with adding the line of "If no.IsInitialized Then."

Here is Log(Product):

<B4IPurchase: <SKPaymentTransaction: 0x187b6010>>

Here is
Log("Product: " & Product.ProductIdentifier & ", date=" & DateTime.Date(Product.TransactionDate) &
", Transaction identifier=" & Product.TransactionIdentifier)

Product: mobi.mindware.mindgames.3months, date=05/01/2018, Transaction identifier=1000000394872000

It should work. following code works perfect in one of my apps :

B4X:
Dim no As NativeObject = Purchases
        Dim b() As Byte = no.NSDataToArray(no.GetField("transactionReceipt"))
        Dim stringu As StringUtils
        Log(stringu.EncodeBase64(b))
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Here's the context.

B4X:
Sub MyStore_PurchaseCompleted (Success As Boolean, Product As Purchase)
    LogColor("Purchase completed",Colors.Magenta)
    Log("DateTime.Now-Product.TransactionDate "&((DateTime.Now-Product.TransactionDate)/DateTime.TicksPerHour))
    
    If Product.IsInitialized Then
        Log("Product: " & Product.ProductIdentifier & ", date=" & DateTime.Date(Product.TransactionDate) & _
       ", Transaction identifier=" & Product.TransactionIdentifier)
        PurchasedProduct.Initialize
        PurchasedProduct.ProductId=Product.ProductIdentifier
        PurchasedProduct.PurchaseDate=Product.TransactionDate
        PurchasedProduct.TransactionID=Product.TransactionIdentifier
        Dim no As NativeObject = Product
        Dim b() As Byte
        If no.IsInitialized Then b = no.NSDataToArray(no.GetField("transactionReceipt"))
        Dim stringu As StringUtils
        Log(stringu.EncodeBase64(b))
        
'        Receipt=BytesToString(b,0,b.Length,"UTF8")
'        StateManager.SetSetting("receipt",Receipt)
        
'        If DateTime.Now-Product.TransactionDate<60000 Then    EnableProMode
    End If
    Log("Success = " & Success)
    If Not(Success) And Product.IsInitialized Then
        Msgbox(Main.loc.Localize("Unable to complete purchase at this time."),Main.loc.Localize("We're sorry"))
    Else
        'may want to add a server call here to store subscription info
'        If DateTime.Now-Product.TransactionDate<60000 Then    EnableProMode
        EnableProMode(True)
    End If
End Sub

I'm running an older iPad Mini with iOS 9.3.5.
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Here's a thread that had a similar type of error message related to iOS version. Do you think that could be the problem here?

https://www.b4x.com/android/forum/threads/xcustomlistview-b4i-issue.103026/
No. It is not related.

The correct way to check whether the data is available or not:
B4X:
Dim receipt As NativeObject = no.GetField("transactionReceipt")
If receipt.IsInitialized Then
 Dim b() As Byte = receipt.NSDataToArray(receipt)
 'work with b
Else
 Log("no receipt")
End If
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Thanks Erel. I had to add one line to it (the top one). I share this in case anyone else wants to use this.

B4X:
Dim no As NativeObject = Product
Dim receipt As NativeObject = no.GetField("transactionReceipt")
If receipt.IsInitialized Then
 Dim b() As Byte = receipt.NSDataToArray(receipt)
 'work with b
Else
 Log("no receipt")
End If
 
Upvote 0
Top