on 3c32ec8 help sign in
123 lines 3.5 kB history raw
1/*
2 * Copyright (C) 2013 - Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, as
6 * published by the Free Software Foundation; either version 2.1 or 3.0
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranties of
11 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the applicable version of the GNU Lesser General Public
13 * License for more details.
14 *
15 * You should have received a copy of both the GNU Lesser General Public
16 * License along with this program. If not, see <http://www.gnu.org/licenses/>
17 *
18 * Authored by: Diego Sarmentero <diego.sarmentero@canonical.com>
19 */
21#include <QDebug>
22#include <QLocale>
23#include <QTimer>
25#include <libpay/pay-package.h>
27#include "Payments.h"
29Payments::Payments(QObject *parent)
30 : QObject(parent)
34QString Payments::currency() const
36 return m_currency;
39double Payments::price() const
41 return m_price;
44QString Payments::storeItemId() const
46 return m_store_item_id;
49QString Payments::formattedPrice() const
51 QLocale locale;
52 return locale.toCurrencyString(m_price, m_currency);
55void Payments::setCurrency(const QString &currency)
57 if(m_currency != currency){
58 m_currency = currency;
59 Q_EMIT currencyChanged(currency);
60 Q_EMIT formattedPriceChanged(formattedPrice());
61 }
64void Payments::setPrice(const double price)
66 if(m_price != price){
67 m_price = price;
68 Q_EMIT priceChanged(price);
69 Q_EMIT formattedPriceChanged(formattedPrice());
70 }
73void Payments::setStoreItemId(const QString &store_item_id)
75 if(m_store_item_id != store_item_id){
76 m_store_item_id = store_item_id;
77 Q_EMIT storeItemIdChanged(store_item_id);
78 }
81void observer(PayPackage* package, const char* /*itemid*/, PayPackageItemStatus status, void* user_data) {
82 // This function is called in libpay's thread, so be careful with what you call
83 // Emitting signals should be fine as long as they use Queued or Auto connections (the default)
84 // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
86 qDebug() << "observer called";
87 Payments *self = static_cast<Payments*>(user_data);
88 pay_package_item_observer_uninstall(package, observer, self);
89 switch (status) {
90 case PAY_PACKAGE_ITEM_STATUS_VERIFYING:
91 break;
92 case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
93 Q_EMIT self->purchaseCompleted();
94 break;
95 case PAY_PACKAGE_ITEM_STATUS_PURCHASING:
96 break;
97 case PAY_PACKAGE_ITEM_STATUS_NOT_PURCHASED:
98 Q_EMIT self->purchaseError("not purchased");
99 break;
100 case PAY_PACKAGE_ITEM_STATUS_UNKNOWN:
101 break;
102 default:
103 break;
104 }
108void Payments::start()
110 qDebug("starting the purchase");
112 auto ba = m_store_item_id.toLocal8Bit();
113 qDebug() << "the item id is" << m_store_item_id;
114 auto package = pay_package_new("clickscope");
115 qDebug() << "after new" << ba.data();
116 pay_package_item_observer_install(package, observer, this);
117 qDebug() << "after observer install";
118 // FIXME: Enable once verification is working in pay-service.
119 //pay_package_item_start_verification(package, ba.data());
120 qDebug() << "after start verify";
121 pay_package_item_start_purchase(package, ba.data());
122 qDebug() << "after start purchase";