IAPProcess.cpp 4.5 KB

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