Hi, I’m working on cocos2dx 3.12 and latest sdkbox version to implement playphon in Android Studio.
I successfully implemented the playphone in my project but when I successfully purchase the item, then onSuccess is not getting called.
Following is my Appdeligate
#include “AppDelegate.h”
#include “HelloWorldScene.h”
#ifdef SDKBOX_ENABLED
#include “PluginAdMob/PluginAdMob.h”
#endif
#ifdef SDKBOX_ENABLED
#include “PluginLeaderboard/PluginLeaderboard.h”
#endif
#ifdef SDKBOX_ENABLED
#include “PluginSdkboxPlay/PluginSdkboxPlay.h”
#endif
#ifdef SDKBOX_ENABLED
#include “PluginIAP/PluginIAP.h”
#endif
#include “Logo.h”
USING_NS_CC;
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
}
// if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// if you want to use the package manager to install more packages,
// don’t modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
#ifdef SDKBOX_ENABLED
sdkbox::PluginAdMob::init();
#endif
#ifdef SDKBOX_ENABLED
sdkbox::PluginSdkboxPlay::init();
#endif
#ifdef SDKBOX_ENABLED
sdkbox::PluginLeaderboard::init();
#endif
#ifdef SDKBOX_ENABLED
sdkbox::IAP::init();
#endif
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect(“DeepaquaTreasure”, cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create(“DeepaquaTreasure”);
#endif
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(false);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
Size s = glview->getFrameSize();
glview->setDesignResolutionSize(800, 480, ResolutionPolicy::EXACT_FIT);
// create a scene. it's an autorelease object
auto scene = Logo::createScene();
// run
director->runWithScene(scene);
return true;
}
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
// if you use SimpleAudioEngine, it must be paused
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
and buy classes in which I code for playphone.
#include “buy.h”
#include “HelloWorldScene.h”
#include “MainMenuNew.h”
#include “PluginAdMob/PluginAdMob.h”
USING_NS_CC;
Scene* buy::createScene()
{
sdkbox::PluginAdMob::hide(“home”);
// ‘scene’ is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = buy::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool buy::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
sdkbox::IAP::setDebug(true);
// sdkbox::IAP::setListener(this);
// sdkbox::IAP::init();
//sdkbox::playphone
auto bg = Sprite::create("menubg.jpg");
bg->cocos2d::Node::setAnchorPoint(Vec2(0,0));
bg->setPosition(Vec2(0, 0));
this->addChild(bg);
PurchaseBg = Sprite::create("unlock0.png");
PurchaseBg->setPosition(Vec2(origin.x + visibleSize.width/2 /*- pauseItem->getContentSize().width/2*/, origin.y + visibleSize.height/2));
this->addChild(PurchaseBg);
buySprite = Sprite::create("buybt.png");
buySprite->setPosition(Vec2(origin.x + visibleSize.width/2 /*+ pauseItem->getContentSize().width/2*/, origin.y+20 + buySprite->getContentSize().height));
this->addChild(buySprite);
auto listenersprite_Upbuy = EventListenerTouchOneByOne::create();
// trigger when you push down
listenersprite_Upbuy->onTouchBegan = [&](Touch* touch, Event* event){
// Point pt = touch->getLocation();
auto bounds = event->getCurrentTarget()->getBoundingBox();
if (bounds.containsPoint(touch->getLocation())&&buySprite->isVisible())
{
buySprite->setScale(1.2f);
}
return true; // if you are consuming it
};
listenersprite_Upbuy->onTouchMoved = [this](Touch* touch, Event* event){
auto bounds = event->getCurrentTarget()->getBoundingBox();
if (bounds.containsPoint(touch->getLocation()) &&buySprite->isVisible())
{
buySprite->setScale(1.2f);
}
else
{
}
};
listenersprite_Upbuy->onTouchEnded = [&](Touch* touch, Event* event){
auto bounds = event->getCurrentTarget()->getBoundingBox() ;
if (bounds.containsPoint(touch->getLocation())&&buySprite->isVisible() )
{
buySprite->setScale(1.0f);
buy::onIAP();
}
};
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenersprite_Upbuy, buySprite);
//////////////Listener for UP End////////////////////////
sdkbox::Product test;
test.name = "Deep Aqua Treasure_Free";
_products.push_back(test);
updateIAP(_products);
isPurchessSuccessfull=false;
auto backItem = Sprite::create(“resume_bt.png”);
backItem->setPosition(Vec2(origin.x + visibleSize.width - backItem->getContentSize().width, origin.y + backItem->getContentSize().height));
this->addChild(backItem);
auto listenersprite_Up = EventListenerTouchOneByOne::create();
// trigger when you push down
listenersprite_Up->onTouchBegan = [&](Touch* touch, Event* event){
// Point pt = touch->getLocation();
auto bounds = event->getCurrentTarget()->getBoundingBox();
if (bounds.containsPoint(touch->getLocation()))
{
}
return true; // if you are consuming it
};
listenersprite_Up->onTouchMoved = [this](Touch* touch, Event* event){
auto bounds = event->getCurrentTarget()->getBoundingBox();
if (bounds.containsPoint(touch->getLocation()) )
{
}
else
{
}
};
listenersprite_Up->onTouchEnded = [&](Touch* touch, Event* event){
auto bounds = event->getCurrentTarget()->getBoundingBox() ;
if (bounds.containsPoint(touch->getLocation()) )
{
buy::GoToGameSceneMenu();
}
};
// Add listener
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenersprite_Up, backItem);
//////////////Listener for UP End////////////////////////
return true;
}
void buy::GoToGameSceneMenu()
{
///CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("audio/coins.wav");
auto scene = MainMenuNew::createScene();
Director::getInstance()->replaceScene(scene);
}
/////////////////////////////////////////////////copy code here//////////////////////////////////////renu
void buy::onRequestIAP(cocos2d::Ref* sender)
{
sdkbox::IAP::refresh();
}
void buy::onRestoreIAP(cocos2d::Ref* sender)
{
sdkbox::IAP::restore();
}
void buy::onIAP()
{
CCLog("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");
for (int i=0; i < _products.size(); i++)
{
CCLog("IAP: ========= IAP Item =========");
CCLog("IAP: Name: %s", _products[i].name.c_str());
CCLog("IAP: ID: %s", _products[i].id.c_str());
CCLog("IAP: Title: %s", _products[i].title.c_str());
CCLog("IAP: Desc: %s", _products[i].description.c_str());
CCLog("IAP: Price: %s", _products[i].price.c_str());
CCLog("IAP: Price Currency: %s", _products[i].currencyCode.c_str());
CCLog("IAP: Price Value: %f", _products[i].priceValue);
}
sdkbox::IAP::purchase("Deep Aqua Treasure_Free");
}
void buy::onInitialized(bool ok)
{
CCLog("%s : %d", func, ok);
CCLog(“IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII”);
}
void buy::onSuccess(const sdkbox::Product &p)
{
CCLog("onSuccess11: ");
isPurchessSuccessfull=true;
userDefaultScore->setBoolForKey(“isPurchesed”,true);
}
void buy::onFailure(const sdkbox::Product &p, const std::string &msg)
{
CCLog("Purchase Failed: %s", msg.c_str());
}
void buy::onCanceled(const sdkbox::Product &p)
{
CCLog("Purchase Canceled: %s", p.id.c_str());
}
void buy::onRestored(const sdkbox::Product& p)
{
CCLog("Purchase Restored: %s", p.name.c_str());
}
void buy::updateIAP(const std::vectorsdkbox::Product& products)
{
_products = products;
int posX = 0;
int posY = 0;
for (int i=0; i < _products.size(); i++)
{
CCLog("IAP: ========= IAP Item =========");
CCLog("IAP: Name: %s", _products[i].name.c_str());
CCLog("IAP: ID: %s", _products[i].id.c_str());
CCLog("IAP: Title: %s", _products[i].title.c_str());
CCLog("IAP: Desc: %s", _products[i].description.c_str());
CCLog("IAP: Price: %s", _products[i].price.c_str());
CCLog("IAP: Price Currency: %s", _products[i].currencyCode.c_str());
CCLog("IAP: Price Value: %f", _products[i].priceValue);
}
}
void buy::onProductRequestSuccess(const std::vectorsdkbox::Product &products)
{
updateIAP(products);
}
void buy::onProductRequestFailure(const std::string &msg)
{
CCLog(“Fail to load productsXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”);
}
void buy::onRestoreComplete(bool ok, const std::string &msg)
{
CCLog("%s:%d:%s", __func__, ok, msg.data());
}
/////////////////////////////////////////////copy code here/////////////////////////////////
//
// Created by Kashyap on 29-04-2017.
//
#ifndef DATERUNBOYFIAP_BUY_H
#define DATERUNBOYFIAP_BUY_H
#include <stdio.h>
#include “cocos2d.h”
/////copy code here//////
#include “PluginIAP/PluginIAP.h”
#include “PluginLeaderboard/PluginLeaderboard.h”
/////copy code here//////
//class buy : public cocos2d::Layer
class buy : public cocos2d::Layer, public sdkbox::IAPListener
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void GoToGameSceneMenu();
//////copy code here//////////
void purchaseScreen();
void onRestoreIAP(cocos2d::Ref* sender);
void onRequestIAP(cocos2d::Ref* sender);
void onIAP();
//////copy code here//////////
///////////////////////////copy code here/////////////////////////////////////////////////////
void updateIAP(const std::vector<sdkbox::Product>& products);
virtual void onInitialized(bool ok) override;
virtual void onSuccess(sdkbox::Product const& p) override;
virtual void onFailure(sdkbox::Product const& p, const std::string &msg) override;
virtual void onCanceled(sdkbox::Product const& p) override;
virtual void onRestored(sdkbox::Product const& p) override;
virtual void onProductRequestSuccess(std::vector<sdkbox::Product> const &products) override;
virtual void onProductRequestFailure(const std::string &msg) override;
void onRestoreComplete(bool ok, const std::string &msg);
cocos2d::CCMenu* _iapMenu;
std::vector<sdkbox::Product> _products;
cocos2d::Label* _txtCoin;
int _coinCount;
cocos2d::UserDefault* userDefaultScore;
///////////////////////////copy code here//////////////////////
CREATE_FUNC(buy);
cocos2d::Sprite *buySprite,*PurchaseBg;
bool isPurchessSuccessfull ;
private:
cocos2d::MenuItemImage *backItem;
};
#endif
thanks.