forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Payment.cpp
168 lines (134 loc) · 4.73 KB
/
Payment.cpp
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
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Payment.h"
#include "MyOrders.h"
#include "zrtypes.h"
#include "zrdb.h"
#include <QListWidget>
#include <QDateTime>
#include <sstream>
#include <stdlib.h>
QListWidget * Payment::txLogView = NULL;
Payment::Requests Payment::requestList;
Payment::Requests Payment::myRequests;
Payment::Payment( const std::string & counterparty, const ZR::ZR_Number & amount, const std::string & currency, Category category) :
m_credit( counterparty, currency ),
m_amount( amount ),
m_category( category )
{
m_credit.loadPeer();
}
void Payment::setCounterparty( const std::string & counterparty )
{
m_credit.m_id = counterparty;
m_credit.loadPeer();
}
const Payment::Request Payment::getRequest( const ZR::VirtualAddress & addr )
{
Requests::iterator it = requestList.find( addr );
if( it == requestList.end() )
return Request( 0, Currency::INVALID );
return (*it).second;
}
const Payment::Request Payment::getMyRequest( const ZR::VirtualAddress & addr )
{
Requests::iterator it = myRequests.find( addr );
if( it == myRequests.end() )
return Request( 0, Currency::INVALID );
return (*it).second;
}
/////// PaymentReceiver
PaymentReceiver::PaymentReceiver( const std::string & counterparty, const ZR::ZR_Number & amount, const std::string & currency, Category category) :
Payment( counterparty, amount, currency, category)
{}
ZR::ZR_Number PaymentReceiver::newBalance() const
{
return m_credit.m_balance + m_amount;
}
int PaymentReceiver::init()
{
switch( m_category )
{
case BITCOIN:
if( m_credit.getPeerAvailable() <= 0 )
return ZR::ZR_FAILURE;
if( m_amount > m_credit.getPeerAvailable() ){
m_amount = m_credit.getPeerAvailable();
}
return MyOrders::Instance()->startExecute( this );
case PAYMENT:
if( m_credit.getPeerAvailable() < m_amount ) return ZR::ZR_FAILURE;
return ZR::ZR_SUCCESS;
default:
return ZR::ZR_FAILURE;
}
}
int PaymentReceiver::commit( const ZR::TransactionId &txId )
{
m_credit.loadPeer();
m_credit.m_balance = newBalance();
// TODO: make atomic !!!!
ZrDB::Instance()->updatePeerCredit( m_credit, "balance", m_credit.m_balance );
ZrDB::Instance()->appendTx( m_credit.m_id, m_credit.m_currency, m_amount );
if( txLogView ){
txLogView->insertItem( 0, QDateTime::currentDateTime().toString() + " : " + m_credit.m_currency.c_str() + " : +" + m_amount.toDecimalQString() );
txLogView->setCurrentRow( 0 ); // make the view emit currentItemChanged()
}
switch( m_category )
{
case BITCOIN:
return MyOrders::Instance()->finishExecute( this );
case PAYMENT:
return ZR::ZR_SUCCESS;
default:
return ZR::ZR_FAILURE;
}
}
/////// PaymentSpender
PaymentSpender::PaymentSpender(const std::string & counterparty, ZR::ZR_Number amount, const std::string & currency, Category category) :
Payment( counterparty, amount, currency, category)
{}
ZR::ZR_Number PaymentSpender::newBalance() const
{
return m_credit.m_balance - m_amount;
}
int PaymentSpender::init()
{
if( m_credit.getMyAvailable() < m_amount ){
return ZR::ZR_FAILURE;
}
return ZR::ZR_SUCCESS;
}
int PaymentSpender::commit( const ZR::TransactionId & txId )
{
m_credit.loadPeer();
m_credit.m_balance = newBalance();
// TODO: make atomic !!!!
ZrDB::Instance()->updatePeerCredit( m_credit, "balance", m_credit.m_balance );
ZrDB::Instance()->appendTx( m_credit.m_id, m_credit.m_currency, -m_amount );
if( txLogView ){
txLogView->insertItem( 0, QDateTime::currentDateTime().toString() + " : " + m_credit.m_currency.c_str() + " : -" + m_amount.toDecimalQString() );
txLogView->setCurrentRow( 0 ); // make the view emit currentItemChanged()
}
switch( m_category )
{
case BITCOIN:
return MyOrders::Instance()->updateOrders( this, txId );
case PAYMENT:
return ZR::ZR_SUCCESS;
default:
return ZR::ZR_FAILURE;
}
return ZR::ZR_SUCCESS;
}