larsiusprime / SteamWrap

Haxe native extension for the Steam API
MIT License
106 stars 44 forks source link

Listening for event: MicroTxnAuthorizationResponse_t #12

Open anpShawn opened 7 years ago

anpShawn commented 7 years ago

Working on a system where I need to listen for the "MicroTxnAuthorizationResponse_t" callback. (Briefly covered here: https://partner.steamgames.com/documentation/MicroTxn#InGamePurchasing)

SteamWrap is already set up to listen for some events, so I attempted to piggyback the current system. I started by defining a new event in SteamWrap.cpp

static const char* kEventTypeOnMicroTxnAuthorizationResponse = "MicroTxnAuthorizationResponse";

Then I updated CallbackHandler with the following:

STEAM_CALLBACK( CallbackHandler, OnMicroTxnAuthorizationResponse, MicroTxnAuthorizationResponse_t , m_CallbackMicroTxnAuthorizationResponse );

and

m_CallbackMicroTxnAuthorizationResponse( this, &CallbackHandler::OnMicroTxnAuthorizationResponse )

Then I added the actual callback function:

void CallbackHandler::OnMicroTxnAuthorizationResponse( MicroTxnAuthorizationResponse_t *pCallback )
{
    SendEvent(Event(kEventTypeOnMicroTxnAuthorizationResponse, true));
}

For now the event just returns true for testing. Lastly I updated Steam.hx to recognize the event inside of steamWrap_onEvent

case "MicroTxnAuthorizationResponse": 
                trace("mtxn callback received");

There appear to be no errors when rebuilding the extension with build.bat. However, no event is fired when I attempt to authorize a purchase in-game. Will keep digging, but not sure where to go next. Is there another step for adding an event that I have missed?

My only guess so far is that something also needs to show up in Steam.cpp. When searching for the other event names it seems they show up in that file a few times, but that file also seems like it was the result of some kind of build or auto-generation process. I thought rebuilding the extension might fix that, but I was wrong.

anpShawn commented 7 years ago

Update: Stupid error on my part.

I was failing to call Steam.onEnterFrame anywhere in my main program. Up to this point I was only using SteamWrap to report achievements, so I never noticed the lack of onEnterFrame being called. I had assumed the extension was set up to run this automatically.

In addition, this extension makes use of a 'report' function to trace output, with a very similar format to the output displayed by the actual event broadcasting function. Again, leading me to believe that the event system was functioning without the need to call Steam.onEnterFrame

zaminFareed commented 4 years ago

sir i am also stuck in game purchase steam can you please help me here is my code please help using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using Steamworks; using System.Globalization; using System;

public class website : MonoBehaviour {

CSteamID j; UInt64 steamid; protected Callback m_MicroTxnAuthorizationResponse; void Start() { if (SteamManager.Initialized) { j = SteamUser.GetSteamID(); Debug.Log(j); // A correct website page. StartCoroutine(GetUserAgreementinformation("https://partner.steam-api.com/ISteamMicroTxn/GetUserInfo/v2/?key=5E1FB1A93723E5FC4B793BC830B8CBD8&steamid=76561199068127265")); StartCoroutine(initialize("https://partner.steam-api.com/ISteamMicroTxn/InitTxn/v3/?key=5E1FB1A93723E5FC4B793BC830B8CBD8&steamid=76561199068127265")); StartCoroutine(GetUserAgreementinformation("https://partner.steam-api.com/ISteamMicroTxn/GetUserAgreementInfo/v1/?key=5E1FB1A93723E5FC4B793BC830B8CBD8&steamid=76561199068127265&appid=1289300"));

        m_MicroTxnAuthorizationResponse = Callback<MicroTxnAuthorizationResponse_t>.Create(OnMicroTxnAuthorizationResponse);

        Debug.Log("SteamUserID: " + SteamUser.GetSteamID());
        Debug.Log("Language: " + SteamApps.GetCurrentGameLanguage());

    }
}

IEnumerator GetUserAgreementinformation(string uri)
{
    using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    {
        // Request and wait for the desired page.
        yield return webRequest.SendWebRequest();

        string[] pages = uri.Split('/');
        int page = pages.Length - 1;

        if (webRequest.isNetworkError)
        {
            Debug.Log(pages[page] + ": Error: " + webRequest.error);
        }
        else
        {
            Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
        }
    }
}
IEnumerator initialize(string uri)
{
    WWWForm form = new WWWForm();

    form.AddField("orderid", "0");
    form.AddField("steamid", "76561199068127265");
    form.AddField("appid", "1289300");
    form.AddField("itemcount", "1");
    form.AddField("language", "ISO 639-1");
    form.AddField("currency", "ISO 4217");
    form.AddField("itemid[0]", "0");
    form.AddField("qty[0]", "50");
    form.AddField("amount[0]", "20");
    form.AddField("description[0]", "300 gold");

    using (UnityWebRequest www = UnityWebRequest.Post(uri, form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}
//https://steamapi.xpaw.me/#ISteamMicroTxn/GetReport

IEnumerator finalize(string uri)
{ 
           WWWForm form = new WWWForm();

form.AddField("orderid", "0");

    form.AddField("appid", "1289300");

    using (UnityWebRequest www = UnityWebRequest.Post(uri, form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}
private void OnMicroTxnAuthorizationResponse(MicroTxnAuthorizationResponse_t pCallback)
{

    //   text.text = "HERE";

    StartCoroutine(finalize("https://partner.steam-api.com/ISteamMicroTxn/FinalizeTxn/v2/?key=5E1FB1A93723E5FC4B793BC830B8CBD8"));
    if (pCallback.m_bAuthorized == 1)
    {
//        text.text += Environment.NewLine + "Authorized Payment";

        Debug.Log("Authorized Payment");
        StartCoroutine(finalize("https://partner.steam-api.com/ISteamMicroTxn/FinalizeTxn/v2/?key=5E1FB1A93723E5FC4B793BC830B8CBD8"));
        //      ConfirmPurchase(pCallback.m_ulOrderID.ToString());
    }
    else
    {
      //  text.text += Environment.NewLine + "Failed to authorize payment";
        // Player didn't authorize the payment
        Debug.Log("Failed to authorize payment");
    }
}

}