// // IAPProcess.cpp // TileManor // // Created by 徐炼新 on 2024/1/18. // #include "IAPProcess.hpp" #include "IAPSucessProcess.hpp" #include "RUQCoreLayer.h" #include "RUReboltLayer.h" #include "IAPUtils.h" #include "IAPCtlShop.hpp" #include IAPProcess* IAPProcess::_instance = nullptr; IAPProcess* IAPProcess::getInstance() { if (_instance == nullptr) { _instance = new IAPProcess(); _instance->autorelease(); _instance->retain(); _instance->init(); } return _instance; } IAPProcess::IAPProcess() { } IAPProcess::~IAPProcess() { } void IAPProcess::init() { iap::IAPUtils::getInstance()->setDelegate(this); } void IAPProcess::doWhileLaunched() { _bLaunching = true; // 注册商品信息 std::map allGoods; std::vector gis; IAPConf::getInstance()->getAllGoods(gis); for (auto& gi : gis) { if (gi.id.size() > 0) { allGoods.insert({gi.id, iap::IAPProductType::Consumable}); } } // FIXME: piggy bank allGoods.insert({"piggybank", iap::IAPProductType::Consumable}); for (auto& id : _productsIDs) { allGoods.insert({id, iap::IAPProductType::Consumable}); } iap::IAPUtils::getInstance()->initWithProductIds(allGoods); } void IAPProcess::buy(const GoodsInfo& gi, int placementId, std::function cbIfSuccess, std::function cbIfFail) { _cbIfSuccess = cbIfSuccess; _cbIfFail = cbIfFail; _placementId = placementId; if (gi.id.size() == 0) { return; } auto scene = Director::getInstance()->getRunningScene(); _efxLoading = redutils::QCoreLayer::Layer("shop_loading.redream"); _efxLoading->playAnim("1"); scene->addChild(_efxLoading, 4); // 先保存购买的商品信息 // gUserDM->addProductInfoPurchasing(gi.id, gi.serialize()); // 显示正在购买的界面并启动购买流程 iap::IAPUtils::getInstance()->purchase(gi.id); } void IAPProcess::registerAProduct(const std::string& id) { _productsIDs.push_back(id); } void IAPProcess::onProductInfoSyncFinished() { } void IAPProcess::onProductPurchaseSuccess(const std::string& pid) { // auto strGi = gUserDM->getProductInfoPurchasing(pid); // GoodsInfo gi; // gi.deserialize(strGi); // gUserDM->rmProductInfoPurchasing(pid); // // { // // 去掉可能的货币信息 // std::string cost; // for (auto& ch : gi.cost) { // if ((ch >= '0' && ch <= '9') || ch=='.') { // cost.push_back(ch); // } // } // gtuser2::GTUser::getInstance()->purchase(_placementId, gi.idxGT2, atof(cost.c_str()), 0, {}, 0, 0); // } // bool bNeedAcceptNow = false; if (_efxLoading) { _efxLoading->playAnimAndRemoveSelf("out"); _efxLoading = nullptr; bNeedAcceptNow = true; } if (_cbIfSuccess) { _cbIfSuccess(); } // 记录用户购买信息 iapshop::IAPCtlShop::createWith()->addUserBuyInfo(pid); printf("购买%s成功\n", pid.c_str()); // // 判断当前是否在家装场景,如果在游戏场景,则等待 // if (bNeedAcceptNow) { // // 像tilepass的内购,就不要有结算,结算是放在其活动本身的 // if (gi.areas.size() > 0) { // IAPSucessProcess::create(gi); // } // } } void IAPProcess::onProductPurchaseFail(const std::string &productId, int errorCode, const std::string &errorMsg) { // gUserDM->rmProductInfoPurchasing(productId); if (_efxLoading) { _efxLoading->playAnimAndRemoveSelf("out"); _efxLoading = nullptr; } auto scene = Director::getInstance()->getRunningScene(); auto lyFail = redutils::RUReboltLayer::createReboltLayer("shop_buy_fail.redream"); scene->addChild(lyFail, 10); lyFail->registerOnNotify([=](const redutils::ReboltNotifyData& data){ if (data.notify == "关闭") { auto rm = RemoveSelf::create(); lyFail->runAction(rm); if (_cbIfFail) { _cbIfFail(); } } }); CCLOG("[iap-tile-manor]: %d: %s", errorCode, errorMsg.c_str()); lyFail->runBehaviacWhitFunName("初始化"); } void IAPProcess::syncPurchasedProducts(const std::vector& productIds) { // FIXME: 应该用商品信息同步商店等的显示 return; } bool IAPProcess::needDoWhileMapIsReady(bool& bSkippable) const { return false; } void IAPProcess::doWhileMapIsReady(std::function cb) { }