-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPurchaseManager.cpp
More file actions
89 lines (74 loc) · 2.22 KB
/
Copy pathPurchaseManager.cpp
File metadata and controls
89 lines (74 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// PurchaseManager.cpp
// まじょのおしごと
//
// Created by Atsushi Yoshida on 2015/03/13.
//
//
#include "platform/CCCommon.h"
#include "PurchaseManager.h"
#include "Env.h"
using namespace cocos2d;
using namespace cocos2d::plugin;
PurchaseManager::PurchaseManager()
: _init(false)
, s_pRetListener(new PurchaseManagerResult)
{
}
bool PurchaseManager::isInitialized()
{
return _init;
}
PurchaseManager* PurchaseManager::getInstance()
{
static PurchaseManager s_instance;
return &s_instance;
}
void PurchaseManager::init()
{
if(_init){
return;
}
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
TIAPDeveloperInfo pGoogleInfo;
pGoogleInfo["GooglePlayAppKey"] = GOOGLE_APPKEY;
if(pGoogleInfo.empty()) {
char msg[256] = { 0 };
sprintf(msg, "Google App Key info is empty. PLZ fill your Google App Key info in %s(nearby line %d)", __FILE__, __LINE__);
MessageBox(msg, "Google IAP Warning");
}
s_protocolIAP = dynamic_cast<ProtocolIAP*>(PluginManager::getInstance()->loadPlugin("IAPGooglePlay"));
s_protocolIAP->configDeveloperInfo(pGoogleInfo);
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
TIAPDeveloperInfo info;
s_protocolIAP = dynamic_cast<ProtocolIAP*>(PluginManager::getInstance()->loadPlugin("IOSIAP"));
info["iapKeys"] = IOS_PRODUCT_KEYS;
s_protocolIAP->configDeveloperInfo(info);
#endif
#if COCOS2D_DEBUG
s_protocolIAP->setDebugMode(true);
#endif
_init = true;
}
void PurchaseManager::buy(const std::string& IAPId, ProtocolIAP::ProtocolIAPCallback callback)
{
s_pRetListener->setCallback(callback);
s_protocolIAP->setResultListener(s_pRetListener.get());
s_protocolIAP->onPayResult(PayResultCode::kPayCancel, "Start Paying");
TProductInfo info;
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
info["IAPId"] = IAPId;
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
info["productId"] = IAPId;
#endif
s_protocolIAP->payForProduct(info);
}
void PurchaseManagerResult::setCallback(ProtocolIAP::ProtocolIAPCallback& callback)
{
_callback = callback;
}
void PurchaseManagerResult::onPayResult(PayResultCode ret, const char* msg, TProductInfo info)
{
std::string str(msg);
_callback(ret, str);
}