axmol/plugin/jsbindings/manual/jsb_pluginx_manual_iap.cpp

205 lines
6.0 KiB
C++
Raw Normal View History

2013-04-07 16:58:26 +08:00
#include "jsb_pluginx_manual_iap.h"
#include "jsb_pluginx_basic_conversions.h"
#include "jsb_pluginx_spidermonkey_specifics.h"
2013-04-24 09:43:40 +08:00
#include "ProtocolAds.h"
2013-04-07 16:58:26 +08:00
using namespace pluginx;
static JSContext* s_cx = NULL;
class Pluginx_PurchaseResult : public cocos2d::plugin::PayResultListener
{
public:
virtual void payResult(cocos2d::plugin::EPayResult ret, const char* msg, cocos2d::plugin::TProductInfo info)
{
char goodInfo[1024] = { 0 };
sprintf(goodInfo, "商品名称:%s\n商品价格:%s\n商品描述:%s",
info.find("productName")->second.c_str(),
info.find("productPrice")->second.c_str(),
info.find("productDesc")->second.c_str());
LOGD(goodInfo);
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
jsval dataVal[3];
dataVal[0] = INT_TO_JSVAL(ret);
std::string strMsgInfo = msg;
dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
dataVal[2] = TProductInfo_to_jsval(cx, info);
JSObject* obj = m_pJSDelegate;
if (JS_HasProperty(cx, obj, "payResult", &hasAction) && hasAction) {
if(!JS_GetProperty(cx, obj, "payResult", &temp_retval)) {
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
JS_CallFunctionName(cx, obj, "payResult",
3, dataVal, &retval);
}
}
void setJSDelegate(JSObject* pJSDelegate)
{
m_pJSDelegate = pJSDelegate;
}
private:
JSObject* m_pJSDelegate;
};
JSBool js_pluginx_ProtocolIAP_setResultListener(JSContext *cx, uint32_t argc, jsval *vp)
{
s_cx = cx;
jsval *argv = JS_ARGV(cx, vp);
JSBool ok = JS_TRUE;
if (argc == 1) {
// save the delegate
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
Pluginx_PurchaseResult* nativeDelegate = new Pluginx_PurchaseResult();
nativeDelegate->setJSDelegate(jsDelegate);
cocos2d::plugin::ProtocolIAP::setResultListener(nativeDelegate);
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return JS_FALSE;
}
2013-04-24 09:43:40 +08:00
class Pluginx_AdListener : public cocos2d::plugin::AdListener
2013-04-07 16:58:26 +08:00
{
public:
2013-04-24 09:43:40 +08:00
virtual void onReceiveAd()
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
JSObject* obj = m_pJSDelegate;
JSBool bRet = JS_HasProperty(cx, obj, "onReceiveAd", &hasAction);
if (bRet && hasAction) {
if(!JS_GetProperty(cx, obj, "onReceiveAd", &temp_retval)) {
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
JS_CallFunctionName(cx, obj, "onReceiveAd",
0, NULL, &retval);
}
}
virtual void onPresentScreen()
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
JSObject* obj = m_pJSDelegate;
JSBool bRet = JS_HasProperty(cx, obj, "onPresentScreen", &hasAction);
if (bRet && hasAction) {
if(!JS_GetProperty(cx, obj, "onPresentScreen", &temp_retval)) {
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
JS_CallFunctionName(cx, obj, "onPresentScreen",
0, NULL, &retval);
}
}
virtual void onFailedToReceiveAd(EAdErrorCode code, const char* msg)
2013-04-07 16:58:26 +08:00
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
jsval dataVal[2];
2013-04-24 09:43:40 +08:00
dataVal[0] = INT_TO_JSVAL(code);
2013-04-07 16:58:26 +08:00
std::string strMsgInfo = msg;
dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
JSObject* obj = m_pJSDelegate;
2013-04-24 09:43:40 +08:00
JSBool bRet = JS_HasProperty(cx, obj, "onFailedToReceiveAd", &hasAction);
2013-04-07 16:58:26 +08:00
if (bRet && hasAction) {
2013-04-24 09:43:40 +08:00
if(!JS_GetProperty(cx, obj, "onFailedToReceiveAd", &temp_retval)) {
2013-04-07 16:58:26 +08:00
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
2013-04-24 09:43:40 +08:00
JS_CallFunctionName(cx, obj, "onFailedToReceiveAd",
2013-04-07 16:58:26 +08:00
2, dataVal, &retval);
}
}
2013-04-24 09:43:40 +08:00
virtual void onDismissScreen()
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
JSObject* obj = m_pJSDelegate;
JSBool bRet = JS_HasProperty(cx, obj, "onDismissScreen", &hasAction);
if (bRet && hasAction) {
if(!JS_GetProperty(cx, obj, "onDismissScreen", &temp_retval)) {
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
JS_CallFunctionName(cx, obj, "onDismissScreen",
0, NULL, &retval);
}
}
2013-04-07 16:58:26 +08:00
void setJSDelegate(JSObject* pJSDelegate)
{
m_pJSDelegate = pJSDelegate;
}
private:
JSObject* m_pJSDelegate;
};
2013-04-24 09:43:40 +08:00
JSBool js_pluginx_ProtocolAds_setAdListener(JSContext *cx, uint32_t argc, jsval *vp)
2013-04-07 16:58:26 +08:00
{
s_cx = cx;
jsval *argv = JS_ARGV(cx, vp);
JSBool ok = JS_TRUE;
if (argc == 1) {
// save the delegate
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
2013-04-24 09:43:40 +08:00
Pluginx_AdListener* nativeDelegate = new Pluginx_AdListener();
2013-04-07 16:58:26 +08:00
nativeDelegate->setJSDelegate(jsDelegate);
2013-04-24 09:43:40 +08:00
cocos2d::plugin::ProtocolAds::setAdListener(nativeDelegate);
2013-04-07 16:58:26 +08:00
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return JS_FALSE;
}