Unity SDK Integration Guide

Appcharge Unity SDK

To seamlessly integrate Appcharge’s Unity SDK into your game, follow these steps:

Alt Text

  1. Download the latest SDK release (Contact The Appcharge Team).

  2. Import Appcharge to Unity by simply double-clicking it. (Unity project must be open).
    Import entire package content.

  3. Create a Unity component by adding a new C# script.

  4. Create a method that will be used to trigger the checkout:

    public async void OpenAppchargeCheckout() 
    {
    	// Checkout code goes here...
    }
    
  5. Now you're ready to initialize the AppCharge checkout model:

    // Declare a model for getting session token
    AppChargeCheckoutModel checkoutModel = new AppChargeCheckoutModel(...);
    

    Here are the parameters needed for creating the model:

    ParameterTypeDescription
    customerIdstringThe name of the customer
    emailstringCustomer email address
    pricefloatOffer price (in cents)
    currencystringOffer price currency. For example: "USD", "EUR"
    offerNamestringThe name of the offer
    offerSkustringOffer's SKU
    offerAssetUrlstringAn image URL for the offer
    offerDescriptionstringOffer's description

    If your offer contains a set of items (bundle) you can add them as follows:

    // Utilize the model by adding extra items
    checkoutModel.AddItem(...);
    

    Here are the parameters needed for creating an item:

    ParameterTypeDescription
    namestringThe name of the item
    assetUrlstringAn image that represents the item
    skustringItem's SKU
    quantityintQuantity
  6. To open and close the checkout use the following:

    Checkout checkout = new Checkout();
    checkout.Open(gameObject, checkoutModel); // Open Checkout
    checkout.Close(); // Close Checkout
    

    It is important to deliver a game object which will be used to start the webview process.
    Note that there are multiple ways to open the checkout where you can provide more details by gathering a token on your own:

     var response = await AppCharge.Services.HttpRequest.SendPostRequestAsync(checkoutModel, xPublisherToken, EnvironmentType.Sandbox);
    
  7. Listening to events from the webview is important. In order to determine what happened in the webview you may need to listen to events as follows:

    checkout.OnCheckoutEvent += (object sender, CheckoutEvent checkoutEvent) => {
        switch (checkoutEvent)
        {
        	case CheckoutEvent.PAYMENT_INTENT_SUCCESS:
        	    Debug.Log("Payment intenet success triggered");
        	    break;
        	case CheckoutEvent.ORDER_COMPLETED_SUCCESS:
        	    Debug.Log("Order success triggered");
        	    break;
        	case CheckoutEvent.ORDER_COMPLETED_FAILED:
        	    Debug.Log("Order failed triggered");
        	    break;
        	case CheckoutEvent.PAYMENT_INTENT_FAILED:
        	    Debug.Log("Payment intenet failed triggered");
        	    break;
        	case CheckoutEvent.CLOSE:
        	    Debug.Log("Checkout close triggered");
        	    // Close the webview when event is triggered
        	    checkout.Close(gameObject);
        	    break;
        }
    };
    
    
  8. Here is a full operating code for initializing the entire process:

    using UnityEngine;
    using AppCharge.Models;
    using AppCharge.Enums;
    using AppCharge;
    
    public class Sample : MonoBehaviour
    {
        public void OpenAppchargeCheckout() 
        {
            // Declare a model for getting session token
            AppChargeCheckoutModel checkoutModel = new AppChargeCheckoutModel(
                "John Doe",
                "[email protected]",
                129,
                "usd",
                "Coins Shop",
                "CoinsShop",
                "https://media-dev.appcharge.com/media/64ad4f25cc1a482bac467ae5/fire-fire-logo-fire-fire-icon-fire-sign-fire-symbol-transparent-background-ai-generative-free-png.png",
                "Coin Pack Bundle"
            );
    
            // Utilize the model by adding extra items
            checkoutModel.AddItem("Coins", "https://media-dev.appcharge.com/media/product-3.png", "coins_xoxoxo", 300);
    
            Checkout checkout = new Checkout();
            checkout.Open(gameObject, checkoutModel);
    
            checkout.OnCheckoutEvent += (object sender, CheckoutEvent checkoutEvent) => {
                switch (checkoutEvent)
                {
                    case CheckoutEvent.PAYMENT_INTENT_SUCCESS:
                        Debug.Log("Payment intent success triggered");
                        break;
                    case CheckoutEvent.ORDER_COMPLETED_SUCCESS:
                        Debug.Log("Order success triggered");
                        break;
                    case CheckoutEvent.ORDER_COMPLETED_FAILED:
                        Debug.Log("Order failed triggered");
                        break;
                    case CheckoutEvent.PAYMENT_INTENT_FAILED:
                        Debug.Log("Payment intent failed triggered");
                        break;
                    case CheckoutEvent.CLOSE:
                        Debug.Log("Checkout close triggered");
                        // Close the webview when event is triggered
                        checkout.Close();
                        break;
                }
            };
        }
    }
    
    

Configurations

The package will open a web-based webview application. In order to change its path, go to AppCharge -> Checkout.cs, look for a constant called WRAPPER_URL and change its value.