IAPProcess.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // IAPProcess.cpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/1/18.
  6. //
  7. #include "IAPProcess.hpp"
  8. #include "IAPSucessProcess.hpp"
  9. #include "RUQCoreLayer.h"
  10. #include "RUReboltLayer.h"
  11. #include "IAPUtils.h"
  12. IAPProcess* IAPProcess::_instance = nullptr;
  13. IAPProcess* IAPProcess::getInstance() {
  14. if (_instance == nullptr) {
  15. _instance = new IAPProcess();
  16. _instance->autorelease();
  17. _instance->retain();
  18. _instance->init();
  19. }
  20. return _instance;
  21. }
  22. IAPProcess::IAPProcess() {
  23. }
  24. IAPProcess::~IAPProcess() {
  25. }
  26. void IAPProcess::init() {
  27. iap::IAPUtils::getInstance()->setDelegate(this);
  28. }
  29. void IAPProcess::doWhileLaunched() {
  30. _bLaunching = true;
  31. // 注册商品信息
  32. std::map<std::string, iap::IAPProductType> allGoods;
  33. std::vector<GoodsInfo> gis;
  34. IAPConf::getInstance()->getAllGoods(gis);
  35. for (auto& gi : gis) {
  36. if (gi.id.size() > 0) {
  37. allGoods.insert({gi.id, iap::IAPProductType::Consumable});
  38. }
  39. }
  40. // FIXME: piggy bank
  41. allGoods.insert({"piggybank", iap::IAPProductType::Consumable});
  42. for (auto& id : _productsIDs) {
  43. allGoods.insert({id, iap::IAPProductType::Consumable});
  44. }
  45. iap::IAPUtils::getInstance()->initWithProductIds(allGoods);
  46. }
  47. void IAPProcess::buy(const GoodsInfo& gi, int placementId, std::function<void()> cbIfSuccess, std::function<void()> cbIfFail) {
  48. _cbIfSuccess = cbIfSuccess;
  49. _cbIfFail = cbIfFail;
  50. _placementId = placementId;
  51. if (gi.id.size() == 0) {
  52. return;
  53. }
  54. auto scene = Director::getInstance()->getRunningScene();
  55. _efxLoading = redutils::QCoreLayer::Layer("shop_loading.redream");
  56. _efxLoading->playAnim("1");
  57. scene->addChild(_efxLoading, 4);
  58. // 先保存购买的商品信息
  59. // gUserDM->addProductInfoPurchasing(gi.id, gi.serialize());
  60. // 显示正在购买的界面并启动购买流程
  61. iap::IAPUtils::getInstance()->purchase(gi.id);
  62. }
  63. void IAPProcess::registerAProduct(const std::string& id) {
  64. _productsIDs.push_back(id);
  65. }
  66. void IAPProcess::onProductInfoSyncFinished() {
  67. }
  68. void IAPProcess::onProductPurchaseSuccess(const std::string& pid) {
  69. // auto strGi = gUserDM->getProductInfoPurchasing(pid);
  70. // GoodsInfo gi;
  71. // gi.deserialize(strGi);
  72. // gUserDM->rmProductInfoPurchasing(pid);
  73. //
  74. // {
  75. // // 去掉可能的货币信息
  76. // std::string cost;
  77. // for (auto& ch : gi.cost) {
  78. // if ((ch >= '0' && ch <= '9') || ch=='.') {
  79. // cost.push_back(ch);
  80. // }
  81. // }
  82. // gtuser2::GTUser::getInstance()->purchase(_placementId, gi.idxGT2, atof(cost.c_str()), 0, {}, 0, 0);
  83. // }
  84. //
  85. bool bNeedAcceptNow = false;
  86. if (_efxLoading) {
  87. _efxLoading->playAnimAndRemoveSelf("out");
  88. _efxLoading = nullptr;
  89. bNeedAcceptNow = true;
  90. }
  91. if (_cbIfSuccess) {
  92. _cbIfSuccess();
  93. }
  94. // // 判断当前是否在家装场景,如果在游戏场景,则等待
  95. // if (bNeedAcceptNow) {
  96. // // 像tilepass的内购,就不要有结算,结算是放在其活动本身的
  97. // if (gi.areas.size() > 0) {
  98. // IAPSucessProcess::create(gi);
  99. // }
  100. // }
  101. }
  102. void IAPProcess::onProductPurchaseFail(const std::string &productId, int errorCode, const std::string &errorMsg) {
  103. // gUserDM->rmProductInfoPurchasing(productId);
  104. if (_efxLoading) {
  105. _efxLoading->playAnimAndRemoveSelf("out");
  106. _efxLoading = nullptr;
  107. }
  108. auto scene = Director::getInstance()->getRunningScene();
  109. auto lyFail = redutils::RUReboltLayer::createReboltLayer("shop_buy_fail.redream");
  110. scene->addChild(lyFail, 10);
  111. lyFail->registerOnNotify([=](const redutils::ReboltNotifyData& data){
  112. if (data.notify == "关闭") {
  113. auto rm = RemoveSelf::create();
  114. lyFail->runAction(rm);
  115. if (_cbIfFail) {
  116. _cbIfFail();
  117. }
  118. }
  119. });
  120. CCLOG("[iap-tile-manor]: %d: %s", errorCode, errorMsg.c_str());
  121. lyFail->runBehaviacWhitFunName("初始化");
  122. }
  123. void IAPProcess::syncPurchasedProducts(const std::vector<std::string>& productIds) {
  124. // FIXME: 应该用商品信息同步商店等的显示
  125. return;
  126. }
  127. bool IAPProcess::needDoWhileMapIsReady(bool& bSkippable) const {
  128. return false;
  129. }
  130. void IAPProcess::doWhileMapIsReady(std::function<void()> cb) {
  131. }