on xenial help sign in
Refactor the pay button a bit to follow pay-service expectations better.
7add60aRodney Dawes
plugins/Ubuntu/Payments/Payments.cpp+37−39
@@ -26,9 +26,36 @@
26
26
27#include "Payments.h"
27#include "Payments.h"
28
28
29+
30+// The observer callback for the package we are watching.
31+static void observer(PayPackage* /* package */, const char* /*itemid*/, PayPackageItemStatus status, void* user_data) {
32+ // This function is called in libpay's thread, so be careful what you call
33+ // Emitting signals should be fine as long as they use Queued or Auto
34+ // connections (the default)
35+ // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
36+
37+ Payments *self = static_cast<Payments*>(user_data);
38+
39+ // FIXME: No error reporting from libpay, but we need to show some
40+ // types of errors to the user. https://launchpad.net/bugs/1333403
41+ switch (status) {
42+ case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
43+ Q_EMIT self->purchaseCompleted();
44+ break;
45+ default:
46+ break;
47+ }
48+}
49+
29Payments::Payments(QObject *parent)
50Payments::Payments(QObject *parent)
30 : QObject(parent)
51 : QObject(parent)
31{
52{
53+ m_package = pay_package_new("click-scope");
54+}
55+
56+Payments::~Payments()
57+{
58+ pay_package_item_observer_uninstall(m_package, observer, this);
32}
59}
33
60
34QString Payments::currency() const
61QString Payments::currency() const
@@ -72,52 +99,23 @@ void Payments::setPrice(const double price)
72
99
73void Payments::setStoreItemId(const QString &store_item_id)
100void Payments::setStoreItemId(const QString &store_item_id)
74{
101{
75− if(m_store_item_id != store_item_id){
102+ if (m_store_item_id != store_item_id){
76 m_store_item_id = store_item_id;
103 m_store_item_id = store_item_id;
77− Q_EMIT storeItemIdChanged(store_item_id);
104+ Q_EMIT storeItemIdChanged(m_store_item_id);
78 }
105 }
79−}
80−
81−void 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
85
106
86− qDebug() << "observer called";
107+ if (m_store_item_id.isEmpty()) {
87− Payments *self = static_cast<Payments*>(user_data);
108+ return;
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 }
109 }
105
110
111+ pay_package_item_observer_uninstall(m_package, observer, this);
112+ pay_package_item_observer_install(m_package, observer, this);
113+ pay_package_item_start_verification(m_package,
114+ m_store_item_id.toLocal8Bit().data());
106}
115}
107
116
108void Payments::start()
117void Payments::start()
109{
118{
110− qDebug("starting the purchase");
111−
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());
119+ pay_package_item_start_purchase(m_package,
122− qDebug() << "after start purchase";
120+ m_store_item_id.toLocal8Bit().data());
123}
121}
plugins/Ubuntu/Payments/Payments.h+6
@@ -25,6 +25,9 @@
25#include <QObject>
25#include <QObject>
26#include <QDBusObjectPath>
26#include <QDBusObjectPath>
27
27
28+#include <libpay/pay-package.h>
29+
30+
28class Payments : public QObject
31class Payments : public QObject
29{
32{
30 Q_OBJECT
33 Q_OBJECT
@@ -36,6 +39,7 @@ class Payments : public QObject
36
39
37public:
40public:
38 explicit Payments(QObject *parent = 0);
41 explicit Payments(QObject *parent = 0);
42+ virtual ~Payments();
39
43
40 QString currency() const;
44 QString currency() const;
41 double price() const;
45 double price() const;
@@ -62,6 +66,8 @@ private:
62 double m_price;
66 double m_price;
63 QString m_store_item_id;
67 QString m_store_item_id;
64
68
69+ PayPackage *m_package;
70+
65 void startService();
71 void startService();
66};
72};
67
73