IAPWrapper.mm 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /****************************************************************************
  2. Copyright (c) 2014 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. #import "IAPWrapper.h"
  21. #include "PluginUtilsIOS.h"
  22. #include "ProtocolIAP.h"
  23. #import <StoreKit/StoreKit.h>
  24. #import "ParseUtils.h"
  25. using namespace cocos2d::plugin;
  26. @implementation IAPWrapper
  27. + (void) onPayResult:(id) obj withRet:(IAPResult) ret withMsg:(NSString*) msg
  28. {
  29. PluginProtocol* plugin = PluginUtilsIOS::getPluginPtr(obj);
  30. ProtocolIAP* iapPlugin = dynamic_cast<ProtocolIAP*>(plugin);
  31. ProtocolIAP::ProtocolIAPCallback callback = iapPlugin->getCallback();
  32. const char* chMsg = [msg UTF8String];
  33. PayResultCode cRet = (PayResultCode) ret;
  34. if (iapPlugin) {
  35. iapPlugin->onPayResult(cRet, chMsg);
  36. }else if(callback){
  37. std::string stdmsg(chMsg);
  38. callback(cRet,stdmsg);
  39. } else {
  40. PluginUtilsIOS::outputLog("Can't find the C++ object of the IAP plugin");
  41. }
  42. }
  43. +(void) onRequestProduct:(id)obj withRet:(ProductRequest) ret withProducts:(NSArray *)products{
  44. PluginProtocol* plugin = PluginUtilsIOS::getPluginPtr(obj);
  45. ProtocolIAP* iapPlugin = dynamic_cast<ProtocolIAP*>(plugin);
  46. PayResultListener *listener = iapPlugin->getResultListener();
  47. ProtocolIAP:: ProtocolIAPCallback callback = iapPlugin->getCallback();
  48. if (iapPlugin) {
  49. if(listener){
  50. TProductList pdlist;
  51. if (products) {
  52. for(SKProduct *product in products){
  53. TProductInfo info;
  54. info.insert(std::make_pair("productId", std::string([product.productIdentifier UTF8String])));
  55. info.insert(std::make_pair("productName", std::string([product.localizedTitle UTF8String])));
  56. info.insert(std::make_pair("productPrice", std::string([[product.price stringValue] UTF8String])));
  57. info.insert(std::make_pair("productDesc", std::string([product.localizedDescription UTF8String])));
  58. pdlist.push_back(info);
  59. }
  60. }
  61. listener->onRequestProductsResult((IAPProductRequest )ret,pdlist);
  62. }else if(callback){
  63. NSString *productInfo = [ParseUtils NSDictionaryToNSString:products];
  64. const char *charProductInfo;
  65. if(productInfo !=nil){
  66. charProductInfo =[productInfo UTF8String];
  67. }else{
  68. charProductInfo = "parse productInfo fail";
  69. }
  70. std::string stdstr(charProductInfo);
  71. callback((IAPProductRequest )ret,stdstr);
  72. }
  73. } else {
  74. PluginUtilsIOS::outputLog("Can't find the C++ object of the IAP plugin");
  75. }
  76. }
  77. @end