ProtocolIAP.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "ProtocolIAP.h"
  21. #include "PluginJniHelper.h"
  22. #include <android/log.h>
  23. #include "PluginUtils.h"
  24. #include "PluginJavaData.h"
  25. namespace cocos2d { namespace plugin {
  26. extern "C" {
  27. JNIEXPORT void JNICALL Java_org_cocos2dx_plugin_IAPWrapper_nativeOnPayResult(JNIEnv* env, jobject thiz, jstring className, jint ret, jstring msg)
  28. {
  29. std::string strMsg = PluginJniHelper::jstring2string(msg);
  30. std::string strClassName = PluginJniHelper::jstring2string(className);
  31. PluginProtocol* pPlugin = PluginUtils::getPluginPtr(strClassName);
  32. PluginUtils::outputLog("ProtocolIAP", "nativeOnPayResult(), Get plugin ptr : %p", pPlugin);
  33. if (pPlugin != NULL)
  34. {
  35. PluginUtils::outputLog("ProtocolIAP", "nativeOnPayResult(), Get plugin name : %s", pPlugin->getPluginName());
  36. ProtocolIAP* pIAP = dynamic_cast<ProtocolIAP*>(pPlugin);
  37. if (pIAP != NULL)
  38. {
  39. pIAP->onPayResult((PayResultCode) ret, strMsg.c_str());
  40. }
  41. else
  42. {
  43. ProtocolIAP::ProtocolIAPCallback callback = pIAP->getCallback();
  44. if(callback)
  45. callback(ret, strMsg);
  46. }
  47. }
  48. }
  49. }
  50. bool ProtocolIAP::_paying = false;
  51. ProtocolIAP::ProtocolIAP()
  52. : _listener(NULL)
  53. {
  54. }
  55. ProtocolIAP::~ProtocolIAP()
  56. {
  57. }
  58. void ProtocolIAP::configDeveloperInfo(TIAPDeveloperInfo devInfo)
  59. {
  60. if (devInfo.empty())
  61. {
  62. PluginUtils::outputLog("ProtocolIAP", "The developer info is empty!");
  63. return;
  64. }
  65. else
  66. {
  67. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  68. PluginJniMethodInfo t;
  69. if (PluginJniHelper::getMethodInfo(t
  70. , pData->jclassName.c_str()
  71. , "configDeveloperInfo"
  72. , "(Ljava/util/Hashtable;)V"))
  73. {
  74. // generate the hashtable from map
  75. jobject obj_Map = PluginUtils::createJavaMapObject(&devInfo);
  76. // invoke java method
  77. t.env->CallVoidMethod(pData->jobj, t.methodID, obj_Map);
  78. t.env->DeleteLocalRef(obj_Map);
  79. t.env->DeleteLocalRef(t.classID);
  80. }
  81. }
  82. }
  83. void ProtocolIAP::payForProduct(TProductInfo info)
  84. {
  85. if (_paying)
  86. {
  87. PluginUtils::outputLog("ProtocolIAP", "Now is paying");
  88. return;
  89. }
  90. if (info.empty())
  91. {
  92. if (NULL != _listener)
  93. {
  94. onPayResult(kPayFail, "Product info error");
  95. }
  96. PluginUtils::outputLog("ProtocolIAP", "The product info is empty!");
  97. return;
  98. }
  99. else
  100. {
  101. _paying = true;
  102. _curInfo = info;
  103. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  104. PluginJniMethodInfo t;
  105. if (PluginJniHelper::getMethodInfo(t
  106. , pData->jclassName.c_str()
  107. , "payForProduct"
  108. , "(Ljava/util/Hashtable;)V"))
  109. {
  110. // generate the hashtable from map
  111. jobject obj_Map = PluginUtils::createJavaMapObject(&info);
  112. // invoke java method
  113. t.env->CallVoidMethod(pData->jobj, t.methodID, obj_Map);
  114. t.env->DeleteLocalRef(obj_Map);
  115. t.env->DeleteLocalRef(t.classID);
  116. }
  117. }
  118. }
  119. void ProtocolIAP::payForProduct(TProductInfo info, ProtocolIAPCallback cb)
  120. {
  121. _callback = cb;
  122. payForProduct(info);
  123. }
  124. void ProtocolIAP::setResultListener(PayResultListener* pListener)
  125. {
  126. _listener = pListener;
  127. }
  128. void ProtocolIAP::onPayResult(PayResultCode ret, const char* msg)
  129. {
  130. _paying = false;
  131. if (_listener)
  132. {
  133. _listener->onPayResult(ret, msg, _curInfo);
  134. }
  135. else
  136. {
  137. PluginUtils::outputLog("ProtocolIAP", "Result listener is null!");
  138. }
  139. _curInfo.clear();
  140. PluginUtils::outputLog("ProtocolIAP", "Pay result is : %d(%s)", (int) ret, msg);
  141. }
  142. }} // namespace cocos2d { namespace plugin {