Appcharge Unity SDK
To seamlessly integrate Appcharge’s Unity SDK into your game, follow these steps:
-
Download the latest SDK release (Contact The Appcharge Team).
-
Import Appcharge to Unity by simply double-clicking it. (Unity project must be open).
Import entire package content. -
Create a Unity component by adding a new C# script.
-
Create a method that will be used to trigger the checkout:
public async void OpenAppchargeCheckout() { // Checkout code goes here... }
-
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:
Parameter Type Description customerId string
The name of the customer email string
Customer email address price float
Offer price (in cents) currency string
Offer price currency. For example: "USD", "EUR" offerName string
The name of the offer offerSku string
Offer's SKU offerAssetUrl string
An image URL for the offer offerDescription string
Offer'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:
Parameter Type Description name string
The name of the item assetUrl string
An image that represents the item sku string
Item's SKU quantity int
Quantity -
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);
-
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; } };
-
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.