Skip to content

Instantly share code, notes, and snippets.

@srlowe
Last active November 9, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srlowe/ed8bfe090421822042ba to your computer and use it in GitHub Desktop.
Save srlowe/ed8bfe090421822042ba to your computer and use it in GitHub Desktop.
Greenworks MicroTxnAuthorizationListener
// In `greenworks_api.cc`:
static MicroTxnAuthorizationListener *microTxnAuthorizationListener;
//...
NAN_METHOD(SetMicroTxnAuthorizationResponseCallback) {
Nan::HandleScope scope;
if (info.Length() < 1 || !info[0]->IsFunction()) {
THROW_BAD_ARGS("Bad arguments - [SetMicroTxnAuthorizationResponseCallback]");
}
Nan::Callback* event_callback = new Nan::Callback(info[0].As<v8::Function>());
microTxnAuthorizationListener = new MicroTxnAuthorizationListener(event_callback);
info.GetReturnValue().Set(Nan::Undefined());
}
//...
Nan::Set(target,
Nan::New("SetMicroTxnAuthorizationResponseCallback").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(SetMicroTxnAuthorizationResponseCallback)->GetFunction());
//Our MicroTxnAuthorizationListener class:
// class that does nothing other than register itself as a Steam
// callback listener when it is constructed
class MicroTxnAuthorizationListener {
public:
MicroTxnAuthorizationListener(Nan::Callback* event_callback)
// initializing this member object is what actually
// registers our callback
: mMicroTxnAuthorizationResponse(
this,
&MicroTxnAuthorizationListener::OnMicroTxnAuthorizationResponse) {
cb = event_callback;
}
// callback when ready
// this macro declares our callback function for us AND
// defines our member object
STEAM_CALLBACK(
// class that the callback function is in
MicroTxnAuthorizationListener,
// name of callback function
OnMicroTxnAuthorizationResponse,
// type of callback
MicroTxnAuthorizationResponse_t,
// name of the member object
mMicroTxnAuthorizationResponse);
private:
Nan::Callback* cb;
MicroTxnAuthorizationResponse_t lastResponse_;
};
void MicroTxnAuthorizationListener::OnMicroTxnAuthorizationResponse(MicroTxnAuthorizationResponse_t *callbackData) {
std::cout << "OnMicroTxnAuthorizationResponse" << std::endl;
//Convert orderId ulong to double
double dbl_orderId = (double)callbackData->m_ulOrderID;
Nan::HandleScope scope;
v8::Local<v8::Object> sendData = Nan::New<v8::Object>();
sendData->Set(Nan::New("orderId").ToLocalChecked(), Nan::New(dbl_orderId));
sendData->Set(Nan::New("authorized").ToLocalChecked(), Nan::New(callbackData->m_bAuthorized));
v8::Local<v8::Value> argv[] = { sendData };
cb->Call(1, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment