Unity integration for iOS and Android
Use the Stash Pay Unity integration to add checkout to your iOS and Android projects.
This guide shows you how to install the Unity SDK, configure deep links, and display the Stash checkout in your game.
Before you begin
This guide applies to both iOS and Android Unity projects. The integration process is identical for both platforms, and the Unity SDK handles platform-specific tasks automatically.
Before you integrate Stash Pay into your Unity project, make sure you understand the Stash Pay system and its integration process. We recommend reviewing the following articles:
- High-level flow: Overview of the checkout flow and steps to obtain your API key.
- Integrating Stash Pay: How to generate a checkout link and handle webhook events for integration.
Your game server must integrate with the Stash Pay API to request a checkout URL or ID on behalf of the player.
Set up the Unity SDK
Install the Stash Unity SDK before implementing the checkout flow.
-
Download the Unity SDK
- Go to the Stash Unity SDK repository and download or clone the
Stash.Popup
folder.
- Go to the Stash Unity SDK repository and download or clone the
-
Import into Unity
- Copy the
Stash.Popup
folder into your Unity project’sAssets
directory.
- Copy the
-
Verify installation
- Verify that the
Stash.Popup
namespace is available in your scripts. - Verify that all dependencies are imported.
- Verify that the
Set up deep links
Configure deep links for iOS and Android to receive the checkout completion callback.
iOS configuration
- Configure Associated Domains
- In Xcode, go to the Signing & Capabilities tab.
- Click on the + button and add Associated Domains.
- Add your domain in the format
applinks:yourdomain.com
.
Android configuration
-
Configure Intent Filters
- Add intent filters to your Android manifest for deep link handling.
- Update your
Assets/Plugins/Android/AndroidManifest.xml
.
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="your-app-scheme" /> </intent-filter> </activity>
Unity deep link implementation
Create a script that handles deep links for iOS and Android.
using UnityEngine;
using Stash.Popup;
public class StashDeepLinkHandler : MonoBehaviour
{
void Start()
{
// Check if the app was launched with a deep link
if (!string.IsNullOrEmpty(Application.absoluteURL))
{
ProcessDeepLink(Application.absoluteURL);
}
}
void OnEnable()
{
Application.deepLinkActivated += ProcessDeepLink;
}
void OnDisable()
{
Application.deepLinkActivated -= ProcessDeepLink;
}
void ProcessDeepLink(string url)
{
Debug.Log("Deep link received: " + url);
// Parse the URL to check if it's a Stash Pay callback
if (url.Contains("stash_transaction_id") || url.Contains("checkout_complete"))
{
// Handle successful payment
OnPaymentComplete(url);
}
else if (url.Contains("cancelled") || url.Contains("error"))
{
// Handle cancelled or failed payment
OnPaymentCancelled(url);
}
}
private void OnPaymentComplete(string url)
{
Debug.Log("Payment completed successfully!");
// Add your post-payment logic here
// e.g., unlock content, update UI, etc.
}
private void OnPaymentCancelled(string url)
{
Debug.Log("Payment was cancelled or failed");
// Handle payment cancellation
}
}
Get the checkout URL from your server
Communicate with your game server to get the checkout URL. You can implement this using:
UnityWebRequest
for HTTP calls.- Your existing networking solution.
- Any preferred method for server communication.
After you get the checkout URL from your server, display the checkout using the Stash Unity SDK.
Implement the Stash Checkout
Create a checkout manager with the Stash Unity SDK to display the checkout popup.
using UnityEngine;
using Stash.Popup;
public class StashCheckoutManager : MonoBehaviour
{
[Header("Stash Configuration")]
public string returnUrl = "your-app-scheme://checkout-complete";
private StashPopup stashPopup;
void Start()
{
// Initialize the Stash Popup component
stashPopup = GetComponent<StashPopup>();
if (stashPopup == null)
{
stashPopup = gameObject.AddComponent<StashPopup>();
}
// Configure popup settings
ConfigurePopup();
}
private void ConfigurePopup()
{
// Set up popup configuration for both iOS and Android
stashPopup.SetReturnUrl(returnUrl);
stashPopup.OnCheckoutComplete += HandleCheckoutComplete;
stashPopup.OnCheckoutCancelled += HandleCheckoutCancelled;
stashPopup.OnCheckoutError += HandleCheckoutError;
}
public void ShowCheckout(string checkoutUrl)
{
if (stashPopup != null)
{
Debug.Log($"Launching Stash checkout: {checkoutUrl}");
stashPopup.OpenCheckout(checkoutUrl);
}
else
{
Debug.LogError("StashPopup component not initialized!");
}
}
private void HandleCheckoutComplete(string transactionId)
{
Debug.Log($"Checkout completed! Transaction ID: {transactionId}");
// Handle successful payment
OnPaymentSuccess(transactionId);
}
private void HandleCheckoutCancelled()
{
Debug.Log("Checkout was cancelled by user");
// Handle payment cancellation
OnPaymentCancelled();
}
private void HandleCheckoutError(string error)
{
Debug.LogError($"Checkout error: {error}");
// Handle payment error
OnPaymentError(error);
}
private void OnPaymentSuccess(string transactionId)
{
// Implement your post-payment logic here
// e.g., unlock purchased content, update player inventory, etc.
ShowSuccessMessage("Payment successful!");
}
private void OnPaymentCancelled()
{
// Handle payment cancellation
ShowMessage("Payment cancelled");
}
private void OnPaymentError(string error)
{
// Handle payment errors
ShowErrorMessage($"Payment failed: {error}");
}
private void ShowSuccessMessage(string message)
{
// Implement your UI feedback here
Debug.Log(message);
}
private void ShowMessage(string message)
{
// Implement your UI feedback here
Debug.Log(message);
}
private void ShowErrorMessage(string message)
{
// Implement your error UI feedback here
Debug.LogError(message);
}
}