IAPUtils.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // IAPUtils.h
  3. // cocos2d_libs
  4. //
  5. // Created by ZhengSong on 2021/5/6.
  6. //
  7. #ifndef IAPUtils_h
  8. #define IAPUtils_h
  9. #include <string>
  10. #include <map>
  11. #include "IAPProductInfo.h"
  12. namespace iap {
  13. class IAPDelegate;
  14. enum IAPProductType {
  15. Consumable = 0, //消耗品
  16. // 暂不支持以下类型
  17. // Non_Consumable = 1, //非消耗品(永久拥有)
  18. // Subscribe = 2, //订阅类型
  19. // SubscribeAutoRenew = 3 //自动续期订阅型
  20. };
  21. class IAPUtils {
  22. public:
  23. IAPUtils();
  24. ~IAPUtils();
  25. static IAPUtils* getInstance();
  26. void setDelegate(IAPDelegate* delegate) { _delegate = delegate; }
  27. // 建议在收到Delegate的onProductInfoSyncFinished回调之后,再调用会比较安全
  28. // !!! 可能为空
  29. IAPProductInfo getProductInfo(const std::string &productId);
  30. // 购买
  31. void purchase(const std::string &productId);
  32. // 用指定的ID进行初始化
  33. void initWithProductIds(const std::map<std::string, IAPProductType>& productIds);
  34. void setLogMode(bool isLog);
  35. // 加载iap.json配置
  36. void loadIAPConfig();
  37. public:
  38. // 平台事件回调
  39. // 商品信息同步完成
  40. void nativeCallback_onProductInfoSyncFinished();
  41. // 内购成功
  42. void nativeCallback_onPurchaseSuccess(const std::string& productId);
  43. void nativeCallback_onPurchaseFail(const std::string& productId, int errorCode, const std::string& errorMsg);
  44. // 同步已购商品(特例)
  45. void nativeCallback_onSyncPurchasedProducts(const std::string& productIdListJson);
  46. private:
  47. IAPDelegate *_delegate;
  48. // 保证只初始化一次
  49. bool _isInited = false;
  50. };
  51. }; // end of namespace iap
  52. #endif /* IAPUtils_h */