1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

iOS Question B4i RestoreTransaction

Discussion in 'iOS Questions' started by Paul Leischow, May 29, 2019.

  1. Paul Leischow

    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?

    Code:
    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
     
  2. Computersmith64

    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:

    Code:
    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.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    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".
     
  4. Paul Leischow

    Paul Leischow Member Licensed User

    Do you have an example of how to use Product.RestoredPurchase
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Check its content and see whether it includes any additional relevant information.
     
  6. Paul Leischow

    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.
     
  7. Erel

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice