ProtocolIAP.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /****************************************************************************
  2. Copyright (c) 2012-2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CCX_PROTOCOL_IAP_H__
  21. #define __CCX_PROTOCOL_IAP_H__
  22. #include "PluginProtocol.h"
  23. #include <map>
  24. #include <string>
  25. #include <functional>
  26. namespace cocos2d { namespace plugin {
  27. typedef std::map<std::string, std::string> TIAPDeveloperInfo;
  28. typedef std::map<std::string, std::string> TProductInfo;
  29. typedef std::vector<TProductInfo> TProductList;
  30. typedef enum
  31. {
  32. kPaySuccess = 0,
  33. kPayFail,
  34. kPayCancel,
  35. kPayTimeOut,
  36. } PayResultCode;
  37. typedef enum {
  38. RequestSuccees=0,
  39. RequestFail,
  40. RequestTimeout,
  41. } IAPProductRequest;
  42. class PayResultListener
  43. {
  44. public:
  45. virtual void onPayResult(PayResultCode ret, const char* msg, TProductInfo info) = 0;
  46. virtual void onRequestProductsResult(IAPProductRequest ret, TProductList info){}
  47. };
  48. class ProtocolIAP : public PluginProtocol
  49. {
  50. public:
  51. ProtocolIAP();
  52. virtual ~ProtocolIAP();
  53. typedef std::function<void(int, std::string&)> ProtocolIAPCallback;
  54. /**
  55. @brief config the developer info
  56. @param devInfo This parameter is the info of developer,
  57. different plugin have different format
  58. @warning Must invoke this interface before other interfaces.
  59. And invoked only once.
  60. */
  61. void configDeveloperInfo(TIAPDeveloperInfo devInfo);
  62. /**
  63. @brief pay for product
  64. @param info The info of product, must contains key:
  65. productName The name of product
  66. productPrice The price of product(must can be parse to float)
  67. productDesc The description of product
  68. @warning For different plugin, the parameter should have other keys to pay.
  69. Look at the manual of plugins.
  70. */
  71. void payForProduct(TProductInfo info);
  72. void payForProduct(TProductInfo info, ProtocolIAPCallback cb);
  73. /**
  74. @deprecated
  75. @breif set the result listener
  76. @param pListener The callback object for pay result
  77. @wraning Must invoke this interface before payForProduct.
  78. */
  79. CC_DEPRECATED_ATTRIBUTE void setResultListener(PayResultListener* pListener);
  80. /**
  81. @deprecated
  82. @breif get the result listener
  83. */
  84. CC_DEPRECATED_ATTRIBUTE inline PayResultListener* getResultListener()
  85. {
  86. return _listener;
  87. }
  88. /**
  89. @brief pay result callback
  90. */
  91. void onPayResult(PayResultCode ret, const char* msg);
  92. /**
  93. @brief set callback function
  94. */
  95. inline void setCallback(ProtocolIAPCallback &cb)
  96. {
  97. _callback = cb;
  98. }
  99. /**
  100. @brief get callback function
  101. */
  102. inline ProtocolIAPCallback getCallback()
  103. {
  104. return _callback;
  105. }
  106. protected:
  107. static bool _paying;
  108. TProductInfo _curInfo;
  109. PayResultListener* _listener;
  110. ProtocolIAPCallback _callback;
  111. };
  112. }} // namespace cocos2d { namespace plugin {
  113. #endif /* __CCX_PROTOCOL_IAP_H__ */