Skip to content

Purchase flow

Stanislav Osipov edited this page Mar 16, 2021 · 4 revisions

Your application should add an observer to the payment queue during application initialization. If there are no observers attached to the queue. If an application quits when transactions are still being processed, those transactions are not lost. The next time the application launches, the payment queue will resume processing the transactions. Your application should always expect to be notified of completed transactions. You can only attach one observer.

The code snippet below shows how to create a simple transaction observer, attach it to a payment queue and respond to observer events:

using SA.CrossPlatform.InApp;
...

MyTransactionObserver observer = new MyTransactionObserver();
UM_InAppService.Client.SetTransactionObserver(observer);

Observer implementation example. Please read comments inside the example.

using SA.CrossPlatform.InApp;
...

public class MyTransactionObserver : UM_iTransactionObserver
{
        public void OnRestoreTransactionsComplete(SA_Result result) {
        //Restore transaction flow was completed.
        //If you've set any flasg befpre starrting the Resoration flow, this is the perfect spot to drop it.
    }

    public void OnTransactionUpdated(UM_iTransaction transaction) {

        //Transactions have been updated.
        //Let's act accordinaly

        switch (transaction.State) {
            case UM_TransactionState.Purchased:
            case UM_TransactionState.Restored:
                //Our product has been succsesly purchased or restored
                //So we need to provide content to our user depends on productIdentifier

                UnlockProduct(transaction);

                //Once product is successfully unlocked we can finish the transactiom
                UM_InAppService.Client.FinishTransaction(transaction);
                break;
            case UM_TransactionState.Pending:
                //Only fir iOS
                //iOS 8 introduces Ask to Buy, which lets parents approve any 
                //purchases initiated by children
                //You should update your UI to reflect this deferred state, 
                //and expect another Transaction Complete to be called again 
                //with a new transaction state 
                //reflecting the parent’s decision or after the transaction times out. 
                //Avoid blocking your UI or gameplay while waiting 
                //for the transaction to be updated.
                break;
            case UM_TransactionState.Failed:
                //Our purchase flow is failed.
                //We can unlock intrefase and tekll user that the purchase is failed. 

                UM_InAppService.Client.FinishTransaction(transaction);
                break;
        }
    }

    private void PrintTransactionInfo(UM_iTransaction transaction) {
        Debug.Log("transaction.Id: " + transaction.Id);
        Debug.Log("transaction.State: " + transaction.State);
        Debug.Log("transaction.ProductId: " + transaction.ProductId);
        Debug.Log("transaction.Timestamp: " + transaction.Timestamp);

    }

    private void UnlockProduct(UM_iTransaction transaction) {
        //Reward your used based on transaction.Id
    }
}

Add Payment Request

When a payment request is added to the queue, the payment queue processes that request and arranges for payment from the user. When that transaction is complete or if a failure occurs, the payment queue sends the UM_iTransaction object that encapsulates the request to all transaction observers.

using SA.CrossPlatform.InApp;
...

string SMALL_PACK = "your.product.id1.here";
UM_InAppService.Client.AddPayment(SMALL_PACK);

Finish Transaction

Your application should call FinishTransaction method from a transaction observer that received a notification from the payment queue. Calling on a transaction removes it from the queue. Your application should call FinishTransaction only after it has successfully processed the transaction and unlocked the functionality purchased by the user.

In case FinishTransaction wasn't called you will get the same transaction update event one you connect to belling service and assign the observer.

UM_InAppService.Client.FinishTransaction(transaction);

Restore Completed Transactions

According to the Apple policy, if your application has Non-Consumable in-app items, you have to provide an ability to restore previously completed purchases for your user. For example, your application would use this to allow a user to unlock previously purchased content onto a new device.

Running the Restore purchase flow on the iOS device will lock your game and will ask a user for an AppStore password in order to start the restore flow. On Android, however, the restored flow will go silently.

Example how to start the restore flow:

using SA.CrossPlatform.InApp;
...

UM_InAppService.Client.RestoreCompletedTransactions();
Clone this wiki locally