Sample Unity App Flow
This tutorial suggests how you can integrate Appcharge Payment Links into a Unity iOS game. To demonstrate the integration, we created a sample Unity app that implements the full flow. We provide a step-by-step breakdown of the implementation, detailing each part of the process to help you adapt it to your own project. You can view the sample app on our GitHub repository.
Before you begin
Before you begin, make sure you have the following:
- Unity version 2020.3 or higher
- XCode version 13 or higher
- Support for deep linking
- Your app has properly configured Associated Domains to handle deeplink callbacks.
Step 1 | Initialize the class
We create a PurchaseManager
class to act as the bridge between our game and the Appcharge payment system. Then we add an event that is triggered when our game receives a deeplink:
private void Awake()
{
if (Instance == null)
{
Instance = this;
Application.deepLinkActivated += onDeepLinkActivated;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL {
debugPrint(url)
}
}
Step 2 | Handle deeplink callbacks
We parse the payment status from the deeplink, and then implement our game logic based on the status.
private void onDeepLinkActivated(string deeplinkURL) {
Debug.Log("Deeplink URL: " + deeplinkURL);
PurchaseStatus status = PurchaseStatusUtils.ParseStatusFromDeepLink(deeplinkURL);
// Validate the order and game balance here before handling the payment result
switch (status)
{
case PurchaseStatus.Success:
Debug.Log("Purchase successful");
break;
case PurchaseStatus.Fail:
Debug.Log("Purchase failed");
break;
case PurchaseStatus.Cancel:
Debug.Log("Purchase canceled");
break;
case PurchaseStatus.Close:
Debug.Log("Purchase closed");
break;
default:
Debug.Log("Unknown purchase status");
break;
}
}
// Call from AppDelegate or SceneDelegate
public static func onDeepLinkActivated(deeplinkURL: String) {
print("Deeplink URL: \(deeplinkURL)")
let status = PurchaseStatusUtils.parseStatus(from: deeplinkURL)
switch status {
case .success:
print("Purchase successful")
case .fail:
print("Purchase failed")
case .cancel:
print("Purchase canceled")
case .close:
print("Purchase closed")
case .unknown:
print("Unknown purchase status")
}
}
Step 3 | Launch the checkout flow
We make a request to our game server to create a session and retrieve the checkout URL. If the checkout URL is retrieved, we use the link to open the checkout from our game.
public void OpenCheckout() {
_flowManager.ShowLoader(true);
StartCoroutine(
HttpRequest.CreateSession(
url => {
Application.OpenURL(url);
},
error => {
Debug.Log("Failed to create session: " + error);
}
)
);
}
public static func openCheckout() {
HttpRequest.createSession(
onSuccess: { url in
if let url = URL(string: url) {
UIApplication.shared.open(url)
}
},
onError: { error in
print("Failed to create session: \(error)")
}
)
}
Full example code
using Appcharge.Networking;
using Appcharge.Utils;
using UnityEngine;
namespace Appcharge
{
public class PurchaseManager : MonoBehaviour
{
public static PurchaseManager Instance { get; private set; }
[SerializeField]private FlowManager _flowManager;
private void Awake()
{
if (Instance == null)
{
Instance = this;
Application.deepLinkActivated += onDeepLinkActivated;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void onDeepLinkActivated(string deeplinkURL) {
Debug.Log("Deeplink URL: " + deeplinkURL);
PurchaseStatus status = PurchaseStatusUtils.ParseStatusFromDeepLink(deeplinkURL);
switch (status)
{
case PurchaseStatus.Success:
Debug.Log("Purchase successful");
break;
case PurchaseStatus.Fail:
Debug.Log("Purchase failed");
break;
case PurchaseStatus.Cancel:
Debug.Log("Purchase canceled");
break;
case PurchaseStatus.Close:
Debug.Log("Purchase closed");
break;
default:
Debug.Log("Unknown purchase status");
break;
}
}
public void OpenCheckout() {
_flowManager.ShowLoader(true);
StartCoroutine(
HttpRequest.CreateSession(
url => {
Application.OpenURL(url);
},
error => {
Debug.Log("Failed to create session: " + error);
}
)
);
}
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL {
debugPrint(url)
}
}
public final class PurchaseManager: NSObject {
private override init() {}
// Call from AppDelegate or SceneDelegate
public static func onDeepLinkActivated(deeplinkURL: String) {
print("Deeplink URL: \(deeplinkURL)")
let status = PurchaseStatusUtils.parseStatus(from: deeplinkURL)
switch status {
case .success:
print("Purchase successful")
case .fail:
print("Purchase failed")
case .cancel:
print("Purchase canceled")
case .close:
print("Purchase closed")
case .unknown:
print("Unknown purchase status")
}
}
public static func openCheckout() {
HttpRequest.createSession(
onSuccess: { url in
if let url = URL(string: url) {
UIApplication.shared.open(url)
}
},
onError: { error in
print("Failed to create session: \(error)")
}
)
}
}
License
Proprietary – © Appcharge. All rights reserved.
Updated 4 days ago