I followed the tutorial code-less IAP in unity from YouTube.
Below is my code to implement purchase in my game, it was working well on unity editor but when I installed on my physical device when I click on buy nothing happens and even values of title, description and price not updated as well. Please help me I will appreciate that! Thanks in advance…
using UnityEngine.Purchasing;
public class IAPManager : MonoBehaviour
{
public GameObject message;
[SerializeField]
private Text coinsLabel;
private string purchaseOne = “com.Asad.MyApp.purchaseOne”;
private string purchaseTwo = “com.Asad.MyApp.purchaseTwo”;
private string purchaseThree = “com.Asad.MyApp.purchaseThree”;
private void Awake()
{
message.SetActive(false);
}
private void Start()
{
if (PlayerPrefs.HasKey(“Coins”))
{
UpdateCoins();
}
}
private void Update()
{
}
public void OnPurchaseComplete(Product product)
{
if (product.definition.id == purchaseOne)
{
GiveUserCoins(1);
StartCoroutine(ShowMessage());
Debug.Log(“Purchase 1 Completed”);
}
else if(product.definition.id == purchaseTwo)
{
GiveUserCoins(2);
StartCoroutine(ShowMessage());
Debug.Log(“Purchase 2 Completed”);
}
else if(product.definition.id == purchaseThree)
{
GiveUserCoins(3);
StartCoroutine(ShowMessage());
Debug.Log(“Purchase 3 Completed”);
}
}
public void GiveUserCoins(int value)
{
int coins = PlayerPrefs.GetInt(“Coins”);
switch (value)
{
case 1:
{
coins += 20000;
break;
}
case 2:
{
coins += 100000;
break;
}
case 3:
{
coins += 1000000;
break;
}
default:
{
coins += 0;
break;
}
}
PlayerPrefs.SetInt(“Coins”, coins);
UpdateCoins();
}
public void UpdateCoins()
{
int coins = PlayerPrefs.GetInt(“Coins”);
if (coins >= 1000)
{
coinsLabel.text = Math.Round((coins / (float)1000), 1).ToString() + " k";
if (coins >= 1000000)
{
coinsLabel.text = Math.Round((coins / (float)1000000), 1).ToString() + " M";
}
}
else
{
coinsLabel.text = coins.ToString();
}
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
{
Debug.Log("Purchase of " + product.definition.id + " failed due to " + reason);
}
IEnumerator ShowMessage()
{
message.SetActive(true);
Vector3 vector = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0);
message.transform.position = vector;
yield return new WaitForSeconds(0.7f);
message.SetActive(false);
}
public void LoadHomeScene()
{
SceneManager.LoadScene(“HomeScene”);
}
}