-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPurchaseStore.cpp
More file actions
executable file
·252 lines (225 loc) · 8.5 KB
/
Copy pathPurchaseStore.cpp
File metadata and controls
executable file
·252 lines (225 loc) · 8.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "PurchaseStore.hpp"
#include <QSettings>
#include <QStringList>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <bb/ApplicationInfo>
#include <bb/platform/PurchaseReply>
#include <bb/platform/PurchaseReceipt>
#include <bb/platform/DigitalGoodState>
#include <bb/platform/PaymentConnectionMode>
#include <bb/device/HardwareInfo>
#include "FileUtils.hpp"
namespace {
QString const PURCHASE_KEY_NAME = "purchases/sku";
QString const PURCHASEID_KEY_NAME = "purchases/purchaseId";
QString const PURCHASEDATE_KEY_NAME = "purchases/purchaseDate";
QString const FEATURE_NAME = "ModPlayerPlus";
QString const PURCHASE_FILE_NAME = ".purchase";
}
PurchaseStore::PurchaseStore(QSettings &settings, QObject* parent)
: QObject(parent),
m_store(settings),
m_updatingStatus(false),
m_reloadingStatus(false)
{
if(!isModPlayerPlusEdition()) {
int rc;
Q_UNUSED(rc);
rc = QObject::connect(&m_manager, SIGNAL(purchaseFinished(bb::platform::PurchaseReply*)),
this, SLOT(onPurchaseFinished(bb::platform::PurchaseReply*)));
Q_ASSERT(rc);
rc = QObject::connect(&m_manager, SIGNAL(existingPurchasesFinished(bb::platform::ExistingPurchasesReply*)),
this, SLOT(onExistingPurchasesFinished(bb::platform::ExistingPurchasesReply*)));
Q_ASSERT(rc);
//m_manager.setConnectionMode(bb::platform::PaymentConnectionMode::Test);
m_manager.setConnectionMode(bb::platform::PaymentConnectionMode::Production);
}
}
void PurchaseStore::buy() {
if(isModPlayerPlusEdition()) {
return;
}
using namespace bb::device;
HardwareInfo hwInfo;
QString metadata;
{
QTextStream stream;
stream.setString(&metadata);
stream << "IMEI: " << hwInfo.imei() << "; ";
stream << "PIN: " << hwInfo.pin() << "; ";
stream << "DEVICENAME: " << hwInfo.deviceName() << "; ";
stream << "HARDWAREID: " << hwInfo.hardwareId() << "; ";
stream << "MODELNAME: " << hwInfo.modelName() << "; ";
stream << "MODELNUMBER: " << hwInfo.modelNumber() << "; ";
stream << "SERIALNUMBER: " << hwInfo.serialNumber() << ";";
}
QVariantMap variableMap;
variableMap["IMEI"] = hwInfo.imei();
variableMap["PIN"] = hwInfo.pin();
variableMap["DEVICENAME"] = hwInfo.deviceName();
variableMap["HARDWAREID"] = hwInfo.hardwareId();
variableMap["MODELNAME"] = hwInfo.modelName();
variableMap["MODELNUMBER"] = hwInfo.modelNumber();
variableMap["SERIALNUMBER"] = hwInfo.serialNumber();
m_manager.requestPurchase("", FEATURE_NAME, "", metadata, variableMap);
}
void PurchaseStore::onExistingPurchasesFinished(bb::platform::ExistingPurchasesReply *reply) {
qDebug() << "=== Update existing purchases finished ===";
m_updatingStatus = false;
bool isPurchased = false;
using namespace bb::platform;
if(reply != NULL && reply->isFinished() && !reply->isError()) {
const QList<PurchaseReceipt> purchaseReceipts = reply->purchases();
const int numPurchases = purchaseReceipts.size();
for(int i = 0; i < numPurchases; i++) {
PurchaseReceipt const& receipt = purchaseReceipts[i];
if(receipt.isValid()) {
if(receipt.state() == DigitalGoodState::Owned) {
savePurchase(receipt.digitalGoodSku());
isPurchased = true;
}
}
}
}
if(isPurchased) {
qDebug() << "=== ModPlayer Plus is purchased ===";
} else {
qDebug() << "=== ModPlayer Plus is not purchased ===";
}
if(m_reloadingStatus) {
m_reloadingStatus = false;
emit remoteStatusRetrieved();
}
emit updatingStatusChanged();
}
void PurchaseStore::onPurchaseFinished(bb::platform::PurchaseReply *reply) {
using namespace bb::platform;
if(reply != NULL && reply->isFinished()) {
if(reply->isError()) {
qDebug() << "=========== Purchase error ===========";
qDebug() << reply->errorText();
emit purchaseFailed(reply->errorText());
} else {
const QString sku = reply->digitalGoodSku();
const PurchaseReceipt receipt = reply->receipt();
if(receipt.state() == DigitalGoodState::Owned) {
savePurchase(sku);
if(sku == FEATURE_NAME) {
updatePurchaseMetadata(receipt.purchaseId(),
receipt.date(),
reply->purchaseMetadata(),
receipt.extraParameters());
QString info;
info += "PURCHASEID: " + receipt.purchaseId() + "; ";
info += "DATE: " + receipt.date().toString(Qt::ISODate) + "; ";
info += "DATA: [" + reply->purchaseMetadata() + "]";
qDebug() << "=========== Purchase succeeded ===========";
qDebug() << info;
emit purchaseSucceeded(info);
} else {
qDebug() << "Invalid SKU in purchase";
emit purchaseFailed("Purchased invalid SKU");
}
} else {
qDebug() << "Invalid SKU state in purchase";
emit purchaseFailed("Invalid SKU state");
}
}
} else {
qDebug() << "Invalid purchase reply";
}
}
void PurchaseStore::updatePurchaseMetadata(QString const& purchaseId,
QDateTime datetime,
QString const& metadata,
QVariantMap parameters) {
Q_UNUSED(parameters);
QFile file(FileUtils::joinPath(QDir::homePath(), PURCHASE_FILE_NAME));
if(file.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream stream(&file);
stream << "PURCHASEID: " << purchaseId << ";\n";
stream << "DATE: " << datetime.toString(Qt::ISODate) << ";\n";
stream << "DATA: [" << metadata << "]\n";
file.flush();
file.close();
}
m_store.setValue(PURCHASEDATE_KEY_NAME, datetime.toString(Qt::ISODate));
m_store.setValue(PURCHASEID_KEY_NAME, purchaseId);
m_store.sync();
}
bool PurchaseStore::purchased() const {
bool result;
if(isModPlayerPlusEdition()) {
result = true;
} else {
const QStringList purchases = m_store.value(PURCHASE_KEY_NAME,
QStringList()).toStringList();
result = purchases.contains(FEATURE_NAME);
if(!result){
result = QFile(FileUtils::joinPath(QDir::homePath(),
PURCHASE_FILE_NAME)).exists();
}
}
return result;
}
bool PurchaseStore::updatingStatus() const {
return m_updatingStatus;
}
void PurchaseStore::savePurchase(QString const& sku) {
if(isModPlayerPlusEdition()) {
// do nothing
} else {
QStringList purchases = m_store.value(PURCHASE_KEY_NAME, QStringList()).toStringList();
if (!purchases.contains(sku)) {
purchases.append(sku);
m_store.setValue(PURCHASE_KEY_NAME, purchases);
m_store.sync();
}
if(sku == FEATURE_NAME) {
emit purchasedChanged();
}
}
}
void PurchaseStore::loadPurchasesFromStore() {
if(isModPlayerPlusEdition()) {
// do nothing
} else {
if(!m_updatingStatus) {
m_updatingStatus = true;
emit updatingStatusChanged();
m_manager.requestExistingPurchases(false);
}
}
}
void PurchaseStore::reloadPurchasesFromStore() {
if(isModPlayerPlusEdition()) {
// do nothing
} else {
if(!m_updatingStatus) {
m_updatingStatus = true;
m_reloadingStatus = true;
emit updatingStatusChanged();
m_manager.requestExistingPurchases(true);
}
}
}
void PurchaseStore::loadLocalPurchases() {
if(isModPlayerPlusEdition()) {
// do nothing
} else {
QStringList purchases = m_store.value(PURCHASE_KEY_NAME).toStringList();
if (!purchases.isEmpty()) {
foreach(QString sku, purchases) {
if(sku == FEATURE_NAME) {
emit purchasedChanged();
}
}
}
}
}
bool PurchaseStore::isModPlayerPlusEdition() const {
bb::ApplicationInfo appInfo;
return appInfo.title() == "ModPlayer Plus";
}