on xenial help sign in
Use the Payments API
commit156aa46abc1be2455c4f92893aaab55af64dd213 authorAlejandro J. Cura <alecu@canonical.com> dateTue 27 May 2014, 16:19 (12 years ago) parent50e450a
9 files changed+305
plugins/Ubuntu/CMakeLists.txt+3 plugins/Ubuntu/Payments/CMakeLists.txt+16 plugins/Ubuntu/Payments/Payments.cpp+84 plugins/Ubuntu/Payments/Payments.h+68 plugins/Ubuntu/Payments/plugin.cpp+39 plugins/Ubuntu/Payments/plugin.h+37 plugins/Ubuntu/Payments/qmldir+3 qml/Dash/Previews/PreviewPayments.qml+54 qml/Dash/Previews/PreviewWidgetFactory.qml+1
plugins/Ubuntu/CMakeLists.txt+3
@@ -1,2 +1,5 @@
11add_subdirectory(Gestures)
22add_subdirectory(DownloadDaemonListener)
3+add_subdirectory(Payments)
4+
5+
plugins/Ubuntu/Payments/CMakeLists.txtnew file+16
@@ -0,0 +1,16 @@
1+include_directories(
2+ ${CMAKE_CURRENT_SOURCE_DIR}
3+ ${CMAKE_CURRENT_BINARY_DIR}
4+ ${Qt5Gui_PRIVATE_INCLUDE_DIRS}
5+)
6+
7+set(PAYMENTS_SOURCES
8+ plugin.cpp
9+ Payments.cpp
10+ )
11+
12+add_library(Payments MODULE ${PAYMENTS_SOURCES})
13+
14+qt5_use_modules(Payments Qml Quick DBus Core)
15+
16+add_unity8_plugin(Ubuntu.Payments 0.1 Ubuntu/Payments TARGETS Payments)
plugins/Ubuntu/Payments/Payments.cppnew file+84
@@ -0,0 +1,84 @@
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+ */
20+
21+#include <QLocale>
22+#include <QTimer>
23+
24+#include "Payments.h"
25+
26+Payments::Payments(QObject *parent)
27+ : QObject(parent)
28+{
29+}
30+
31+QString Payments::currency() const
32+{
33+ return m_currency;
34+}
35+
36+double Payments::price() const
37+{
38+ return m_price;
39+}
40+
41+QString Payments::storeItemId() const
42+{
43+ return m_store_item_id;
44+}
45+
46+QString Payments::formattedPrice() const
47+{
48+ QLocale locale;
49+ return locale.toCurrencyString(m_price, m_currency);
50+}
51+
52+void Payments::setCurrency(const QString &currency)
53+{
54+ if(m_currency != currency){
55+ m_currency = currency;
56+ Q_EMIT currencyChanged(currency);
57+ Q_EMIT formattedPriceChanged(formattedPrice());
58+ }
59+}
60+
61+void Payments::setPrice(const double price)
62+{
63+ if(m_price != price){
64+ m_price = price;
65+ Q_EMIT priceChanged(price);
66+ Q_EMIT formattedPriceChanged(formattedPrice());
67+ }
68+}
69+
70+void Payments::setStoreItemId(const QString &store_item_id)
71+{
72+ if(m_store_item_id != store_item_id){
73+ m_store_item_id = store_item_id;
74+ Q_EMIT storeItemIdChanged(store_item_id);
75+ }
76+}
77+
78+void Payments::start()
79+{
80+ qDebug("starting the purchase");
81+ // start the purchase here, raise finished(), error or canceled when done
82+ QTimer::singleShot(3000, this, SIGNAL(finished()));
83+ // FIXME ^^^^
84+}
plugins/Ubuntu/Payments/Payments.hnew file+68
@@ -0,0 +1,68 @@
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+ */
20+
21+
22+#ifndef PAYMENTS_H
23+#define PAYMENTS_H
24+
25+#include <QObject>
26+#include <QDBusObjectPath>
27+
28+class Payments : public QObject
29+{
30+ Q_OBJECT
31+ Q_DISABLE_COPY(Payments)
32+ Q_PROPERTY(QString currency READ currency WRITE setCurrency NOTIFY currencyChanged)
33+ Q_PROPERTY(double price READ price WRITE setPrice NOTIFY priceChanged)
34+ Q_PROPERTY(QString storeItemId READ storeItemId WRITE setStoreItemId NOTIFY storeItemIdChanged)
35+ Q_PROPERTY(QString formattedPrice READ formattedPrice NOTIFY formattedPriceChanged)
36+
37+public:
38+ explicit Payments(QObject *parent = 0);
39+
40+ QString currency() const;
41+ double price() const;
42+ QString storeItemId() const;
43+ QString formattedPrice() const;
44+
45+ void setCurrency(const QString& currency);
46+ void setPrice(const double price);
47+ void setStoreItemId(const QString& store_item_id);
48+ Q_INVOKABLE void start();
49+
50+Q_SIGNALS:
51+ void currencyChanged(const QString& currency);
52+ void priceChanged(const double price);
53+ void storeItemIdChanged(const QString &store_item_id);
54+ void formattedPriceChanged(const QString &formatted_price);
55+
56+ void canceled();
57+ void error(const QString &error);
58+ void finished();
59+
60+private:
61+ QString m_currency;
62+ double m_price;
63+ QString m_store_item_id;
64+
65+ void startService();
66+};
67+
68+#endif // PAYMENTS_H
plugins/Ubuntu/Payments/plugin.cppnew file+39
@@ -0,0 +1,39 @@
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+ */
20+
21+
22+#include "plugin.h"
23+#include "Payments.h"
24+
25+#include <QtQml/qqml.h>
26+#include <QtQml/QQmlContext>
27+
28+
29+void BackendPlugin::registerTypes(const char *uri)
30+{
31+ Q_ASSERT(uri == QLatin1String("Ubuntu.Payments"));
32+
33+ qmlRegisterType<Payments>(uri, 0, 1, "Payments");
34+}
35+
36+void BackendPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
37+{
38+ QQmlExtensionPlugin::initializeEngine(engine, uri);
39+}
plugins/Ubuntu/Payments/plugin.hnew file+37
@@ -0,0 +1,37 @@
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+ */
20+
21+
22+#ifndef BACKEND_PLUGIN_H
23+#define BACKEND_PLUGIN_H
24+
25+#include <QQmlEngine>
26+#include <QQmlExtensionPlugin>
27+
28+class BackendPlugin : public QQmlExtensionPlugin
29+{
30+ Q_OBJECT
31+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
32+
33+public:
34+ void registerTypes(const char *uri);
35+ void initializeEngine(QQmlEngine *engine, const char *uri);
36+};
37+#endif // BACKEND_PLUGIN_H
plugins/Ubuntu/Payments/qmldirnew file+3
@@ -0,0 +1,3 @@
1+module Ubuntu.Payments
2+plugin Payments
3+typeinfo Payments.qmltypes
qml/Dash/Previews/PreviewPayments.qmlnew file+54
@@ -0,0 +1,54 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.1
18+import Ubuntu.Components 0.1
19+import Ubuntu.Payments 0.1
20+
21+/*! \brief Preview widget for a purchase button.
22+ *
23+ * When clicked, this button starts the payments service.
24+ * Then it waits until the purchase finishes or fails.
25+ * Those events trigger the corresponding signals.
26+ */
27+
28+PreviewWidget {
29+ id: root
30+
31+ implicitHeight: paymentButton.implicitHeight
32+ implicitWidth: paymentButton.implicitWidth
33+
34+ Button {
35+ id: paymentButton
36+
37+ property var source: widgetData["source"]
38+ objectName: "paymentButton"
39+ color: Theme.palette.selected.foreground
40+ text: paymentClient.formattedPrice
41+ iconSource: data && data.icon || ""
42+ iconPosition: "left"
43+ onClicked: paymentClient.start()
44+
45+ Payments {
46+ id: paymentClient
47+ price: paymentButton.source["price"]
48+ currency: paymentButton.source["currency"]
49+ onFinished: root.triggered(widgetId, "finished", data)
50+ onCanceled: root.triggered(widgetId, "canceled", data)
51+ onError: root.triggered(widgetId, "error", data)
52+ }
53+ }
54+}
qml/Dash/Previews/PreviewWidgetFactory.qml+1
@@ -48,6 +48,7 @@ Loader {
4848 case "header": return "PreviewHeader.qml";
4949 case "image": return "PreviewZoomableImage.qml";
5050 case "progress": return "PreviewProgress.qml";
51+ case "payments": return "PreviewPayments.qml";
5152 case "rating-input": return "PreviewRatingInput.qml";
5253 case "reviews": return "PreviewRatingDisplay.qml";
5354 case "text": return "PreviewTextSummary.qml";