Coder Social home page Coder Social logo

gfaraj / purchases-unity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from revenuecat/purchases-unity

0.0 2.0 0.0 2.96 MB

Support files for RevenueCat Unity integration.

License: MIT License

Shell 1.60% Java 30.37% C# 46.12% Objective-C 21.84% Ruby 0.07%

purchases-unity's Introduction

Purchases.framework for Unity

The Purchases SDK provided by RevenueCat allows you to implement subscriptions in your Unity app easily.

1. Add the Purchases Unity package

Get the latest version of Purchases.unitypackage from Github. Add it to your Unity project.

2. Create a GameObject with the Purchases behavior

The Purchases package will include a MonoBehaviour called Purchases. This will be your access point to RevenueCat from inside Unity. It should be instantiated once and kept as a singleton. You can use properties to configure your API Key, app user ID (if you have one), and product identifiers you want to fetch.

The Purchases behaviour is configured with your RevenueCat API key and the product identifiers you want to fetch.

3. Subclass Purchases.Listener MonoBehaviour

The Purchases behavior takes one additional parameter, a GameObject with a Purchases.Listener component. This will be where you handle purchase events, and updated subscriber information from RevenueCat. Here is a simple example:

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class PurchasesListener : Purchases.UpdatedPurchaserInfoListener
{
    void ButtonClicked(string product)
    {
        Purchases purchases = GetComponent<Purchases>();
        purchases.MakePurchase(product, (productIdentifier, purchaserInfo, userCancelled, error) =>
        {
            if (!userCancelled)
            {
                if (error != null)
                {
                    LogError(error);
                }
                else
                {
                    DisplayPurchaserInfo(purchaserInfo);
                }
            } else
            {
                Debug.Log("Subtester: User cancelled, don't show an error");
            }
        });
    }
    
    void DoOtherStuff()
    {
        var purchases = GetComponent<Purchases>();
        var data = new AdjustData
        {
            adid = "test",
            network = "network",
            adgroup = "adgroup",
            campaign = "campaign",
            creative = "creative",
            clickLabel = "clickLabel",
            trackerName = "trackerName",
            trackerToken = "trackerToken"
        };

        purchases.AddAttributionData(JsonUtility.ToJson(data), Purchases.AttributionNetwork.ADJUST);
        
        purchases.GetPurchaserInfo((info, error) =>
        {
            Debug.Log("purchaser info " + info.ActiveSubscriptions);
            if (error != null) {
                LogError(error);
            }
        });
        purchases.GetProducts(new []{ "onemonth_freetrial", "annual_freetrial" }, (products, error) =>
        {
            Debug.Log("getProducts " + products);
            if (error != null) {
                LogError(error);
            }
        });
        
        Debug.Log("user ID " + purchases.GetAppUserId());
    }

    void RestoreClicked()
    {
        var purchases = GetComponent<Purchases>();
        purchases.RestoreTransactions((purchaserInfo, error) =>
        {
            if (error != null)
            {
                LogError(error);
            }
            else
            {
                DisplayPurchaserInfo(purchaserInfo);
            }
        });
    }

    
    public override void PurchaserInfoReceived(Purchases.PurchaserInfo purchaserInfo)
    {
        DisplayPurchaserInfo(purchaserInfo);
    }
    
    private void logError(Purchases.Error error)
    {
        Debug.Log("Subtester: " + JsonUtility.ToJson(error));
    }


    private void DisplayPurchaserInfo(Purchases.PurchaserInfo purchaserInfo)
    {
        // Show purchaser info on screen
    }
}

purchases-unity's People

Contributors

jeiting avatar vegaro avatar aqvinsaldanha avatar rkotzy avatar

Watchers

James Cloos avatar George Faraj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.