Skip to main content
Version: SDK-5.9.6

all-code

BACKND Guidelines > 6. Implementing Coupon Functions > Full Code

Full Code

BackendCoupon.cs

using System.Collections.Generic;
using System.Text;
using UnityEngine;

// Adds BACKND SDK namespace
using BackEnd;

public class BackendCoupon {
private static BackendCoupon _instance = null;

public static BackendCoupon Instance {
get {
if(_instance == null) {
_instance = new BackendCoupon();
}

return _instance;
}
}

public void CouponUse(string couponNumber) {
var bro = Backend.Coupon.UseCoupon(couponNumber);

if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while using the coupon. : " + bro);
return;
}

Debug.Log("Coupon successfully used. : " + bro);

if(BackendGameData.userData == null) {
BackendGameData.Instance.GameDataGet();
}

if(BackendGameData.userData == null) {
BackendGameData.Instance.GameDataInsert();
}

if(BackendGameData.userData == null) {
Debug.LogError("userData does not exist.");
return;
}

foreach(LitJson.JsonData item in bro.GetFlattenJSON()["itemObject"]) {
if(item["item"].ContainsKey("itemType")) {
int itemId = int.Parse(item["item"]["itemId"].ToString());
string itemType = item["item"]["itemType"].ToString();
string itemName = item["item"]["itemName"].ToString();
int itemCount = int.Parse(item["itemCount"].ToString());

if(BackendGameData.userData.inventory.ContainsKey(itemName)) {
BackendGameData.userData.inventory[itemName] += itemCount;
} else {
BackendGameData.userData.inventory.Add(itemName, itemCount);
}
} else {
Debug.LogError("Unsupported item.");
}
}

BackendGameData.Instance.GameDataUpdate();
}
}

BackendManager.cs

using UnityEngine;
using System.Threading.Tasks;

// Adds BACKND SDK namespace
using BackEnd;

public class BackendManager : MonoBehaviour {
void Start() {
var bro = Backend.Initialize(true); // Initialize BACKND

// Response value for BACKND initialization
if(bro.IsSuccess()) {
Debug.Log("Initialization successful : " + bro); // If successful, 'statusCode 204 Success'
} else {
Debug.LogError("Initialization failed : " + bro); // If failed, a 4xx statusCode error occurs
}

Test();
}

// A method that allows synchronous methods to be called from asynchronous methods(cannot be accessed by the Unity UI)
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234");

BackendCoupon.Instance.CouponUse("f9ffeed2c882bc1418"); // [Addition] Uses a coupon whose coupon code is couponNumber

Debug.Log("Test complete.");
});
}
}