-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPayment.cpp
More file actions
171 lines (134 loc) · 4.54 KB
/
Payment.cpp
File metadata and controls
171 lines (134 loc) · 4.54 KB
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
168
169
170
/*
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 "ZeroReservePlugin.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 PAYMENT:
if( m_credit.getPeerAvailable() < m_amount ) return ZR::ZR_FAILURE;
return ZR::ZR_SUCCESS;
default:
return ZR::ZR_FAILURE;
}
}
int PaymentReceiver::commit()
{
try{
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() );
}
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( __func__ ) + ": Exception caught: " + e.what() );
return ZR::ZR_FAILURE;
}
switch( m_category )
{
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()
{
try{
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() );
}
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( __func__ ) + ": Exception caught: " + e.what() );
return ZR::ZR_FAILURE;
}
switch( m_category )
{
case PAYMENT:
return ZR::ZR_SUCCESS;
default:
return ZR::ZR_FAILURE;
}
return ZR::ZR_SUCCESS;
}