123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- //
- // 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 <cocos2d.h>
- 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<std::string, iap::IAPProductType> allGoods;
- std::vector<GoodsInfo> 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<void()> cbIfSuccess, std::function<void()> 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<std::string>& productIds) {
- // FIXME: 应该用商品信息同步商店等的显示
- return;
- }
- bool IAPProcess::needDoWhileMapIsReady(bool& bSkippable) const {
- return false;
- }
- void IAPProcess::doWhileMapIsReady(std::function<void()> cb) {
- }
|