Prypto Redeem implemented

This commit is contained in:
Andre Jochems
2015-11-16 12:09:46 +01:00
parent 2f83b066a6
commit 15a1c2e63b
6 changed files with 170 additions and 9 deletions

View File

@@ -6,6 +6,7 @@
#include "wallet.h" #include "wallet.h"
#include "base58.h" #include "base58.h"
#include <QDebug>
#include <QFont> #include <QFont>
const QString AddressTableModel::Send = "S"; const QString AddressTableModel::Send = "S";
@@ -424,3 +425,24 @@ void AddressTableModel::emitDataChanged(int idx)
{ {
emit dataChanged(index(idx, 0, QModelIndex()), index(idx, columns.length()-1, QModelIndex())); emit dataChanged(index(idx, 0, QModelIndex()), index(idx, columns.length()-1, QModelIndex()));
} }
/* Look up address for label in address book, if not found return empty string.
*/
QString AddressTableModel::addressForLabel(const QString &label) const
{
LOCK(wallet->cs_wallet);
QString walletAddress = "";
// loop over addressbook values to find the key
std::map<CTxDestination, std::string>::iterator it;
for( it = wallet->mapAddressBook.begin(); it != wallet->mapAddressBook.end(); it++)
{
qDebug() << "Label Value: " << QString::fromStdString(it->second);
if (it->second.compare(label.toStdString()) == 0)
{
CTxDestination dest = it->first;
walletAddress = QString::fromStdString(CBitcoinAddress(dest).ToString());
break;
}
}
return walletAddress;
}

View File

@@ -69,6 +69,10 @@ public:
EditStatus getEditStatus() const { return editStatus; } EditStatus getEditStatus() const { return editStatus; }
/* Look up address for label in address book, if not found return empty string.
*/
QString addressForLabel(const QString &label) const;
private: private:
WalletModel *walletModel; WalletModel *walletModel;
CWallet *wallet; CWallet *wallet;

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>pryptopage</class> <class>PryptoPage</class>
<widget class="QDialog" name="pryptopage"> <widget class="QDialog" name="PryptoPage">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
@@ -17,8 +17,8 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>20</x> <x>20</x>
<y>20</y> <y>50</y>
<width>281</width> <width>361</width>
<height>112</height> <height>112</height>
</rect> </rect>
</property> </property>
@@ -56,6 +56,22 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QLabel" name="lblHelp">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>361</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Enter your Prypto Code and Security Code to redeem your Prypto card.</string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
</widget>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

View File

@@ -1,19 +1,116 @@
#include "pryptopage.h" #include "pryptopage.h"
#include "ui_pryptopage.h" #include "ui_pryptopage.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include <QSsl>
#include <QMessageBox>
const QString PryptoPage::strAPIEndpoint = "https://prypto.com/merchants/api/";
const QString PryptoPage::strMerchantToken = "35616ab118fa557b77fdac78ef09d5632d302609";
const QString PryptoPage::strAddressLabel = "Prypto Cards";
PryptoPage::PryptoPage(QWidget *parent) : PryptoPage::PryptoPage(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::PryptoPage) ui(new Ui::PryptoPage)
{ {
ui->setupUi(this); ui->setupUi(this);
connect( &networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(parseAPINetworkResponse(QNetworkReply*)) );
connect( this, SIGNAL(apiResponseReady(QByteArray)), this, SLOT(showAPIResult(QByteArray)) );
connect( this, SIGNAL(apiNetworkError(QNetworkReply::NetworkError*)), this, SLOT(showAPINetworkError(QNetworkReply*)) );
}
void PryptoPage::setWalletModel(WalletModel *model)
{
this->walletModel = model;
}
void PryptoPage::on_butRedeem_clicked()
{
if((ui->txtPryptoCode->text().length() == 0) ||
(ui->txtSecurityCode->text().length() == 0))
{
QMessageBox msgBox;
msgBox.setText("Both Prypto code and Security code must be entered.");
msgBox.exec();
}
else
{
// Show Busy Indicator
busyDialog = new QProgressDialog(this);
busyDialog->setWindowModality(Qt::WindowModal);
busyDialog->setLabelText("Calling Prypto Services ...");
busyDialog->setCancelButton(0);
busyDialog->setRange(0,0);
busyDialog->setMinimumDuration(0);
busyDialog->show();
// get wallet address for redeem action
QString pryptoWalletAddress = walletModel->getAddressTableModel()->addressForLabel(strAddressLabel);
// create new if empty
if(pryptoWalletAddress == "")
{
pryptoWalletAddress = walletModel->getAddressTableModel()->addRow(AddressTableModel::Receive, strAddressLabel, "");
}
// call service
QUrl url ( strAPIEndpoint );
QUrlQuery query;
query.addQueryItem("T", "RX");
query.addQueryItem("TKN", strMerchantToken);
query.addQueryItem("COIN", "CSC");
query.addQueryItem("PC", ui->txtPryptoCode->text());
query.addQueryItem("SC", ui->txtSecurityCode->text());
query.addQueryItem("RX", pryptoWalletAddress);
url.setQuery(query.query());
QNetworkRequest networkRequest ( url );
networkAccessManager.get( networkRequest );
}
}
void PryptoPage::parseAPINetworkResponse( QNetworkReply *finished )
{
if ( finished->error() != QNetworkReply::NoError )
{
// A communication error has occurred
qDebug() << "API Network Error: " << finished->errorString() << " URL: " << finished->url();
busyDialog->cancel();
emit apiNetworkError( finished->error());
return;
}
QByteArray data = finished->readAll();
qDebug() << "API data: " << data;
busyDialog->cancel();
emit apiResponseReady( data );
}
void PryptoPage::showAPINetworkError(QNetworkReply *reply)
{
QMessageBox msgBox;
msgBox.setText("Error redeeming Prypto Card: " + reply->errorString());
msgBox.exec();
}
void PryptoPage::showAPIResult(QByteArray data)
{
// reset text fields
ui->txtPryptoCode->setText("");
ui->txtSecurityCode->setText("");
// handle result
if(data.length() > 0)
{
qDebug() << "API Result: " << data;
QMessageBox msgBox;
msgBox.setText("Prypto Card succesfully redeemed.");
msgBox.exec();
}
else
{
qDebug() << "API Empty Result";
QMessageBox msgBox;
msgBox.setText("Prypto Card already redeemed or invalid.");
msgBox.exec();
}
} }
PryptoPage::~PryptoPage() PryptoPage::~PryptoPage()
{ {
delete ui; delete ui;
} }
void PryptoPage::on_butRedeem_clicked()
{
}

View File

@@ -2,6 +2,12 @@
#define PRYPTOPAGE_H #define PRYPTOPAGE_H
#include <QDialog> #include <QDialog>
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QProgressDialog>
class WalletModel;
namespace Ui { namespace Ui {
class PryptoPage; class PryptoPage;
@@ -13,13 +19,28 @@ class PryptoPage : public QDialog
public: public:
explicit PryptoPage(QWidget *parent = 0); explicit PryptoPage(QWidget *parent = 0);
void setWalletModel(WalletModel *model);
~PryptoPage(); ~PryptoPage();
signals:
void apiResponseReady( const QByteArray& content );
void apiNetworkError( QNetworkReply::NetworkError error );
private slots: private slots:
void on_butRedeem_clicked(); void on_butRedeem_clicked();
void parseAPINetworkResponse( QNetworkReply *finished );
void showAPIResult(QByteArray data);
void showAPINetworkError(QNetworkReply *reply);
private: private:
static const QString strAPIEndpoint;
static const QString strMerchantToken;
static const QString strAddressLabel;
Ui::PryptoPage *ui; Ui::PryptoPage *ui;
QNetworkAccessManager networkAccessManager;
WalletModel *walletModel;
QProgressDialog *busyDialog;
}; };
#endif // PRYPTOPAGE_H #endif // PRYPTOPAGE_H

View File

@@ -125,6 +125,7 @@ void WalletView::setWalletModel(WalletModel *walletModel)
receiveCoinsPage->setModel(walletModel->getAddressTableModel()); receiveCoinsPage->setModel(walletModel->getAddressTableModel());
sendCoinsPage->setModel(walletModel); sendCoinsPage->setModel(walletModel);
signVerifyMessageDialog->setModel(walletModel); signVerifyMessageDialog->setModel(walletModel);
pryptoPage->setWalletModel(walletModel);
setEncryptionStatus(); setEncryptionStatus();
connect(walletModel, SIGNAL(encryptionStatusChanged(int)), gui, SLOT(setEncryptionStatus(int))); connect(walletModel, SIGNAL(encryptionStatusChanged(int)), gui, SLOT(setEncryptionStatus(int)));