iOS Question B4i RestoreTransaction

Paul Leischow

Member
Licensed User
Not sure if this is a bug or I might be doing something wrong ;)

For a simple test I have a single Page with a Purchase Button and a Restore Button that works in Sandbox mode with the Apple in-app stuff.

If I press the Purchase button I can buy a subscription and PurchaseCompleted is called.
If I press the Purchase button a second time it tells me I am already subscribed and I can press Manage or OK. If I press Manage then go back, PurchaseCompleted is called again and it gives a list of about 20 subscriptions I have tested with, all with different dates and Transaction IDs.
So far this is all good.

Next I press the Restore button to Restore Transactions and PurchaseCompleted is called.
BUT... instead of listing the 20 subscriptions I have tested with, all with different dates and Transaction IDs, it lists a single subscription with the same date and Transaction ID 20 times. (and it's not even the latest Purchase, it's an old one)

How can I fix this?

B4X:
Private Sub Application_Start (Nav As NavigationController)
    NavControl=Nav   
    Test.Initialize("Test")
    Test.RootPanel.LoadLayout("Test")
    NavControl.ShowPage(Test)
   
    MyStore.Initialize("MyStore")
    inappsupport=MyStore.CanMakePayments
End Sub

Sub MyStore_PurchaseCompleted (Success As Boolean, Product As Purchase)
    If Success=True Then
        Log("Product: " & Product.ProductIdentifier & ", date=" & DateTime.Date(Product.TransactionDate) &" "& DateTime.Time(Product.TransactionDate) & _
       ", Transaction identifier=" & Product.TransactionIdentifier) 
    End If
End Sub


Sub Subscribe_Click
    If inappsupport=True Then
        MyStore.RequestPayment("test123")
    End If
End Sub

Sub Restore_Click
    If inappsupport=True Then
        Log("RESTORE")
        MyStore.RestoreTransactions
    End If
End Sub
Results from Purchasing...

Product: test123, date=05/29/2019 13:31:08, Transaction identifier=1000000532568554
Product: test123, date=05/29/2019 13:36:08, Transaction identifier=1000000532569667
Product: test123, date=05/29/2019 13:41:08, Transaction identifier=1000000532570311
Product: test123, date=05/28/2019 23:40:47, Transaction identifier=1000000532571015
Product: test123, date=04/30/2019 08:59:51, Transaction identifier=1000000532571016
Product: test123, date=04/25/2019 14:44:25, Transaction identifier=1000000532571017
Product: test123, date=04/24/2019 23:35:36, Transaction identifier=1000000532571018
Product: test123, date=05/28/2019 23:35:47, Transaction identifier=1000000532571019
Product: test123, date=04/25/2019 15:48:42, Transaction identifier=1000000532571020
Product: test123, date=05/29/2019 00:04:53, Transaction identifier=1000000532571021
Product: test123, date=04/25/2019 15:09:25, Transaction identifier=1000000532571022
Product: test123, date=05/29/2019 08:25:37, Transaction identifier=1000000532571023
Product: test123, date=04/25/2019 23:23:33, Transaction identifier=1000000532571024
Product: test123, date=04/24/2019 23:25:36, Transaction identifier=1000000532571025
Product: test123, date=04/25/2019 23:18:33, Transaction identifier=1000000532571026
Product: test123, date=05/28/2019 23:30:47, Transaction identifier=1000000532571027
Product: test123, date=05/29/2019 08:35:53, Transaction identifier=1000000532571028
Product: test123, date=04/30/2019 08:44:51, Transaction identifier=1000000532571029
Product: test123, date=04/24/2019 23:45:36, Transaction identifier=1000000532571030
Product: test123, date=04/25/2019 23:38:33, Transaction identifier=1000000532571031
Results from Restore Transaction...

Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
Product: test123, date=04/24/2019 23:20:37, Transaction identifier=1000000522570757
etc...
 

Computersmith64

Well-Known Member
Licensed User
If your subscriptions are all part of the same group, your users can only have 1 current subscription at any time. I don't use B4i, however I can tell you that apps written with Xcode need to call finishTransaction on the payment queue if the app purchase is successful or fails, & also on a restore. If you don't do that you end up with all your previous transactions sitting in the queue - which is what looks like is happening to you. Looking at the B4i IAP tutorial I don't see any equivalent of finishTransaction, so maybe Erel has implemented it in the library, but it is odd that you are seeing all of your previous subscription purchases. You should only be seeing the current subscription if you make a successful purchase, or if you are restoring. FWIW, here's the Swift equivalent of _PurchaseCompleted showing the finishTransaction calls:

B4X:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

        for transaction in transactions {

            switch transaction.transactionState {
            case .purchased:
                print("Transaction Approved")
                print("Product Identifier: \(transaction.payment.productIdentifier)")
                self.deliverProduct(transaction)
                SKPaymentQueue.default().finishTransaction(transaction)
                checkForReceipt(false)
            case .failed:
                print("Transaction Failed")
                SKPaymentQueue.default().finishTransaction(transaction)
            case .restored:
                print("PQ Restored")
                SKPaymentQueue.default().finishTransaction(transaction)
            case .deferred:
                print("Returned Deferred")
            case .purchasing:
                print("Returned Purchasing")
            @unknown default:
                print("Unknown transactionState")
            }
        }
    }
- Colin.
 

Erel

Administrator
Staff member
Licensed User
Xcode need to call finishTransaction on the payment queue if the app purchase is successful or fails, & also on a restore.
It is called by the library.

Note that you can find more information about restored transactions with Prodcut.RestoredPurchase.
As Colin wrote you can only subscribe to each item once. I'm not sure whether you are seeing a real issue or a "testing issue".
 

Erel

Administrator
Staff member
Licensed User
Check its content and see whether it includes any additional relevant information.
 

Paul Leischow

Member
Licensed User
Being quite new to B4x and example would be really helpful.

If I do a simple Log("Product.RestoredPurchase") and the result is
<B4IPurchase: <SKPaymentTransaction: 0x14e6efc0>>
along with "object converted to String. This is probably a programming mistake" so I'm trying to figure out how to parse the object.

I've already searched through hundreds of forum posts trying to find an answer but it seems searching the forum is only good if you know the right keywords to search for.
 

Erel

Administrator
Staff member
Licensed User
I don't think that there is a real issue here. It shouldn't be possible to subscribe to the same product multiple times. It looks like an issue with the testing sandbox.
 
Top