iOS Question B4i RestoreTransaction

Paul Leischow

Member
Licensed User
Longtime 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

 

Computersmith64

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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".
 
Upvote 0

Paul Leischow

Member
Licensed User
Longtime 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…