forked from OpenBazaar/openbazaar-go
/
purchases.go
560 lines (513 loc) · 17 KB
/
purchases.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package db
import (
"database/sql"
"encoding/json"
"fmt"
"math/big"
"strings"
"sync"
"time"
"github.com/OpenBazaar/jsonpb"
"github.com/OpenBazaar/wallet-interface"
btc "github.com/btcsuite/btcutil"
"github.com/phoreproject/pm-go/pb"
"github.com/phoreproject/pm-go/repo"
)
type PurchasesDB struct {
modelStore
}
func NewPurchaseStore(db *sql.DB, lock *sync.Mutex) repo.PurchaseStore {
return &PurchasesDB{modelStore{db, lock}}
}
func (p *PurchasesDB) Put(orderID string, contract pb.RicardianContract, state pb.OrderState, read bool) error {
p.lock.Lock()
defer p.lock.Unlock()
readInt := 0
if read {
readInt = 1
}
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: false,
}
out, err := m.MarshalToString(&contract)
if err != nil {
return err
}
stm := `insert or replace into purchases(orderID, contract, state, read, timestamp, total, thumbnail, vendorID, vendorHandle, title, shippingName, shippingAddress, paymentAddr, paymentCoin, coinType, funded, transactions, disputedAt) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,(select funded from purchases where orderID="` + orderID + `"),(select transactions from purchases where orderID="` + orderID + `"),?)`
stmt, err := p.db.Prepare(stm)
if err != nil {
return err
}
defer stmt.Close()
var (
paymentAddr, shippingName, shippingAddress string
disputedAt int
dispute = contract.Dispute
handle = contract.VendorListings[0].VendorID.Handle
)
if contract.BuyerOrder.Shipping != nil {
shippingName = contract.BuyerOrder.Shipping.ShipTo
shippingAddress = contract.BuyerOrder.Shipping.Address
}
if contract.BuyerOrder.Payment.Method == pb.Order_Payment_DIRECT || contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED {
paymentAddr = contract.BuyerOrder.Payment.Address
} else if contract.BuyerOrder.Payment.Method == pb.Order_Payment_ADDRESS_REQUEST {
paymentAddr = contract.VendorOrderConfirmation.PaymentAddress
}
if dispute != nil {
disputedAt = int(dispute.Timestamp.Seconds)
}
paymentCoin, err := PaymentCoinForContract(&contract)
if err != nil {
return err
}
_, err = stmt.Exec(
orderID,
out,
int(state),
readInt,
int(contract.BuyerOrder.Timestamp.Seconds),
contract.BuyerOrder.Payment.BigAmount,
contract.VendorListings[0].Item.Images[0].Tiny,
contract.VendorListings[0].VendorID.PeerID,
handle,
contract.VendorListings[0].Item.Title,
shippingName,
shippingAddress,
paymentAddr,
paymentCoin,
CoinTypeForContract(&contract),
disputedAt,
)
if err != nil {
return fmt.Errorf("commit purchase: %s", err.Error())
}
return nil
}
func (p *PurchasesDB) MarkAsRead(orderID string) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := p.db.Exec("update purchases set read=? where orderID=?", 1, orderID)
if err != nil {
return err
}
return nil
}
func (p *PurchasesDB) MarkAsUnread(orderID string) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := p.db.Exec("update purchases set read=? where orderID=?", 0, orderID)
if err != nil {
return err
}
return nil
}
func (p *PurchasesDB) UpdateFunding(orderId string, funded bool, records []*wallet.TransactionRecord) error {
p.lock.Lock()
defer p.lock.Unlock()
fundedInt := 0
if funded {
fundedInt = 1
}
serializedTransactions, err := json.Marshal(records)
if err != nil {
return err
}
_, err = p.db.Exec("update purchases set funded=?, transactions=? where orderID=?", fundedInt, string(serializedTransactions), orderId)
if err != nil {
return err
}
return nil
}
func (p *PurchasesDB) Delete(orderID string) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := p.db.Exec("delete from purchases where orderID=?", orderID)
if err != nil {
return err
}
return nil
}
func (p *PurchasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByAscending bool, sortByRead bool, limit int, exclude []string) ([]repo.Purchase, int, error) {
p.lock.Lock()
defer p.lock.Unlock()
q := query{
table: "purchases",
columns: []string{"orderID", "contract", "timestamp", "total", "title", "thumbnail", "vendorID", "vendorHandle", "shippingName", "shippingAddress", "state", "read", "coinType", "paymentCoin"},
stateFilter: stateFilter,
searchTerm: searchTerm,
searchColumns: []string{"orderID", "timestamp", "total", "title", "thumbnail", "vendorID", "vendorHandle", "shippingName", "shippingAddress", "paymentAddr"},
sortByAscending: sortByAscending,
sortByRead: sortByRead,
id: "orderID",
exclude: exclude,
limit: limit,
}
stm, args := filterQuery(q)
rows, err := p.db.Query(stm, args...)
if err != nil {
return nil, 0, err
}
defer rows.Close()
var ret []repo.Purchase
for rows.Next() {
var orderID, title, thumbnail, vendorID, vendorHandle, shippingName, shippingAddr, coinType, paymentCoin string
var contract []byte
var timestamp, stateInt, readInt int
totalStr := ""
if err := rows.Scan(&orderID, &contract, ×tamp, &totalStr, &title, &thumbnail, &vendorID, &vendorHandle, &shippingName, &shippingAddr, &stateInt, &readInt, &coinType, &paymentCoin); err != nil {
return ret, 0, err
}
read := false
if readInt > 0 {
read = true
}
rc := new(pb.RicardianContract)
if err := jsonpb.UnmarshalString(string(contract), rc); err != nil {
return ret, 0, err
}
var slug string
var moderated bool
if len(rc.VendorListings) > 0 {
slug = rc.VendorListings[0].Slug
}
// Convert buyerOrder to v5
v5order, err := repo.ToV5Order(rc.BuyerOrder, nil)
if err != nil {
return nil, 0, err
}
rc.BuyerOrder = v5order
if rc.BuyerOrder != nil && rc.BuyerOrder.Payment != nil && rc.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED {
moderated = true
}
if len(rc.VendorListings) > 0 && rc.VendorListings[0].Metadata != nil && rc.VendorListings[0].Metadata.ContractType != pb.Listing_Metadata_CRYPTOCURRENCY {
coinType = ""
}
if totalStr == "" {
log.Warningf("the database total is empty when it should contain a value")
totalStr = rc.BuyerOrder.Payment.BigAmount
}
if strings.Contains(totalStr, "e") {
flt, _, err := big.ParseFloat(totalStr, 10, 0, big.ToNearestEven)
if err != nil {
return nil, 0, err
}
var i = new(big.Int)
i, _ = flt.Int(i)
totalStr = i.String()
}
cv, err := repo.NewCurrencyValueWithLookup(totalStr, paymentCoin)
if err != nil {
return nil, 0, err
}
ret = append(ret, repo.Purchase{
OrderId: orderID,
Slug: slug,
Timestamp: time.Unix(int64(timestamp), 0),
Title: title,
Thumbnail: thumbnail,
Total: *cv,
VendorId: vendorID,
VendorHandle: vendorHandle,
ShippingName: shippingName,
ShippingAddress: shippingAddr,
CoinType: coinType,
PaymentCoin: paymentCoin,
State: pb.OrderState(stateInt).String(),
Moderated: moderated,
Read: read,
})
}
q.columns = []string{"Count(*)"}
q.limit = -1
q.exclude = []string{}
stm, args = filterQuery(q)
row := p.db.QueryRow(stm, args...)
var count int
err = row.Scan(&count)
if err != nil {
return ret, 0, err
}
return ret, count, nil
}
func (p *PurchasesDB) GetUnfunded() ([]repo.UnfundedOrder, error) {
p.lock.Lock()
defer p.lock.Unlock()
var ret []repo.UnfundedOrder
rows, err := p.db.Query(`select orderID, contract, timestamp, paymentAddr from purchases where state=?`, 1)
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var orderID, paymentAddr string
var timestamp int
var contractBytes []byte
err := rows.Scan(&orderID, &contractBytes, ×tamp, &paymentAddr)
if err != nil {
return ret, err
}
if timestamp > 0 {
rc := new(pb.RicardianContract)
err = jsonpb.UnmarshalString(string(contractBytes), rc)
if err != nil {
return ret, err
}
v5Order, err := repo.ToV5Order(rc.BuyerOrder, repo.AllCurrencies().Lookup)
if err != nil {
log.Errorf("failed converting contract buyer order to v5 schema: %s", err.Error())
return nil, err
}
ret = append(ret, repo.UnfundedOrder{
OrderId: orderID,
Timestamp: time.Unix(int64(timestamp), 0),
PaymentCoin: v5Order.Payment.AmountCurrency.Code,
PaymentAddress: paymentAddr,
})
}
}
return ret, nil
}
func (p *PurchasesDB) GetByPaymentAddress(addr btc.Address) (*pb.RicardianContract, pb.OrderState, bool, []*wallet.TransactionRecord, error) {
if addr == nil {
return nil, pb.OrderState(0), false, nil, fmt.Errorf("unable to find purchase with nil payment address")
}
p.lock.Lock()
defer p.lock.Unlock()
stmt, err := p.db.Prepare("select contract, state, funded, transactions from purchases where paymentAddr=?")
if err != nil {
return nil, pb.OrderState(0), false, nil, err
}
defer stmt.Close()
var (
contract []byte
stateInt int
fundedInt *int
serializedTransactions []byte
)
err = stmt.QueryRow(addr.EncodeAddress()).Scan(&contract, &stateInt, &fundedInt, &serializedTransactions)
if err != nil {
return nil, pb.OrderState(0), false, nil, err
}
rc := new(pb.RicardianContract)
err = jsonpb.UnmarshalString(string(contract), rc)
if err != nil {
return nil, pb.OrderState(0), false, nil, err
}
funded := false
if fundedInt != nil && *fundedInt == 1 {
funded = true
}
var records []*wallet.TransactionRecord
if len(serializedTransactions) > 0 {
err = json.Unmarshal(serializedTransactions, &records)
if err != nil {
return nil, pb.OrderState(0), false, nil, err
}
}
return rc, pb.OrderState(stateInt), funded, records, nil
}
func (p *PurchasesDB) GetByOrderId(orderId string) (*pb.RicardianContract, pb.OrderState, bool, []*wallet.TransactionRecord, bool, *repo.CurrencyCode, error) {
p.lock.Lock()
defer p.lock.Unlock()
stmt, err := p.db.Prepare("select contract, state, funded, transactions, read, paymentCoin from purchases where orderID=?")
if err != nil {
return nil, pb.OrderState(0), false, nil, false, nil, err
}
defer stmt.Close()
var (
contract []byte
stateInt int
fundedInt *int
readInt *int
serializedTransactions []byte
paymentCoin string
)
err = stmt.QueryRow(orderId).Scan(&contract, &stateInt, &fundedInt, &serializedTransactions, &readInt, &paymentCoin)
if err != nil {
return nil, pb.OrderState(0), false, nil, false, nil, err
}
rc := new(pb.RicardianContract)
err = jsonpb.UnmarshalString(string(contract), rc)
if err != nil {
return nil, pb.OrderState(0), false, nil, false, nil, err
}
funded := false
if fundedInt != nil && *fundedInt == 1 {
funded = true
}
read := false
if readInt != nil && *readInt == 1 {
read = true
}
def, err := repo.AllCurrencies().Lookup(paymentCoin)
if err != nil {
return nil, pb.OrderState(0), false, nil, false, nil, fmt.Errorf("validating payment coin: %s", err.Error())
}
var records []*wallet.TransactionRecord
if len(serializedTransactions) > 0 {
err = json.Unmarshal(serializedTransactions, &records)
if err != nil {
return nil, pb.OrderState(0), false, nil, false, nil, fmt.Errorf("unmarshal purchase transactions: %s", err.Error())
}
}
cc := def.CurrencyCode()
return rc, pb.OrderState(stateInt), funded, records, read, &cc, nil
}
func (p *PurchasesDB) Count() int {
p.lock.Lock()
defer p.lock.Unlock()
row := p.db.QueryRow("select Count(*) from purchases")
var count int
err := row.Scan(&count)
if err != nil {
log.Errorf("failed scanning purchase count: %s", err.Error())
return 0
}
return count
}
func (p *PurchasesDB) GetPurchasesForDisputeExpiryNotification() ([]*repo.PurchaseRecord, error) {
p.lock.Lock()
defer p.lock.Unlock()
s := fmt.Sprintf("select orderID, contract, state, timestamp, lastDisputeExpiryNotifiedAt, disputedAt from purchases where (lastDisputeExpiryNotifiedAt - disputedAt) < %d and state = %d",
int(repo.BuyerDisputeExpiry_lastInterval.Seconds()),
pb.OrderState_DISPUTED,
)
rows, err := p.db.Query(s)
if err != nil {
return nil, fmt.Errorf("selecting purchases: %s", err.Error())
}
result := make([]*repo.PurchaseRecord, 0)
for rows.Next() {
var (
disputedAt int64
lastDisputeExpiryNotifiedAt int64
contract []byte
stateInt int
r = &repo.PurchaseRecord{
Contract: &pb.RicardianContract{},
}
timestamp = sql.NullInt64{}
)
if err := rows.Scan(&r.OrderID, &contract, &stateInt, ×tamp, &lastDisputeExpiryNotifiedAt, &disputedAt); err != nil {
return nil, fmt.Errorf("scanning purchases: %s\n", err.Error())
}
if err := jsonpb.UnmarshalString(string(contract), r.Contract); err != nil {
return nil, fmt.Errorf("unmarshaling contract: %s\n", err.Error())
}
r.OrderState = pb.OrderState(stateInt)
if timestamp.Valid {
r.Timestamp = time.Unix(timestamp.Int64, 0)
} else {
r.Timestamp = time.Now()
}
r.LastDisputeExpiryNotifiedAt = time.Unix(lastDisputeExpiryNotifiedAt, 0)
r.DisputedAt = time.Unix(disputedAt, 0)
result = append(result, r)
}
return result, nil
}
// GetPurchasesForDisputeTimeoutNotification returns []*PurchaseRecord including
// each record which needs Notifications to be generated.
func (p *PurchasesDB) GetPurchasesForDisputeTimeoutNotification() ([]*repo.PurchaseRecord, error) {
p.lock.Lock()
defer p.lock.Unlock()
s := fmt.Sprintf("select orderID, contract, state, timestamp, lastDisputeTimeoutNotifiedAt from purchases where (lastDisputeTimeoutNotifiedAt - timestamp) < %d and state in (%d, %d, %d)",
int(repo.BuyerDisputeTimeout_totalDuration.Seconds()),
pb.OrderState_PENDING,
pb.OrderState_AWAITING_FULFILLMENT,
pb.OrderState_FULFILLED,
)
rows, err := p.db.Query(s)
if err != nil {
return nil, fmt.Errorf("selecting purchases: %s", err.Error())
}
result := make([]*repo.PurchaseRecord, 0)
for rows.Next() {
var (
lastDisputeTimeoutNotifiedAt int64
contract []byte
stateInt int
r = &repo.PurchaseRecord{
Contract: &pb.RicardianContract{},
}
timestamp = sql.NullInt64{}
)
if err := rows.Scan(&r.OrderID, &contract, &stateInt, ×tamp, &lastDisputeTimeoutNotifiedAt); err != nil {
return nil, fmt.Errorf("scanning purchases: %s\n", err.Error())
}
if err := jsonpb.UnmarshalString(string(contract), r.Contract); err != nil {
return nil, fmt.Errorf("unmarshaling contract: %s\n", err.Error())
}
r.OrderState = pb.OrderState(stateInt)
if timestamp.Valid {
r.Timestamp = time.Unix(timestamp.Int64, 0)
} else {
r.Timestamp = time.Now()
}
r.LastDisputeTimeoutNotifiedAt = time.Unix(lastDisputeTimeoutNotifiedAt, 0)
if r.IsDisputeable() {
result = append(result, r)
}
}
return result, nil
}
// UpdatePurchasesLastDisputeTimeoutNotifiedAt accepts []*repo.PurchaseRecord and updates
// each PurchaseRecord by their OrderID to the set LastDisputeTimeoutNotifiedAt value. The
// update will be attempted atomically with a rollback attempted in the event of
// an error.
func (p *PurchasesDB) UpdatePurchasesLastDisputeTimeoutNotifiedAt(purchases []*repo.PurchaseRecord) error {
p.lock.Lock()
defer p.lock.Unlock()
tx, err := p.BeginTransaction()
if err != nil {
return fmt.Errorf("begin update purchase transaction: %s", err.Error())
}
for _, p := range purchases {
_, err = tx.Exec("update purchases set lastDisputeTimeoutNotifiedAt = ? where orderID = ?", int(p.LastDisputeTimeoutNotifiedAt.Unix()), p.OrderID)
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("update purchase: (%s) w rollback error: (%s)", err.Error(), rErr.Error())
}
return fmt.Errorf("update purchase: %s", err.Error())
}
}
if err = tx.Commit(); err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("commit purchase: (%s) w rollback error: (%s)", err.Error(), rErr.Error())
}
return fmt.Errorf("commit update purchase transaction: %s", err.Error())
}
return nil
}
// UpdatePurchasesLastDisputeExpiryNotifiedAt accepts []*repo.PurchaseRecord and updates
// each PurchaseRecord by their OrderID to the set LastDisputeExpiryNotifiedAt value. The
// update will be attempted atomically with a rollback attempted in the event of
// an error.
func (p *PurchasesDB) UpdatePurchasesLastDisputeExpiryNotifiedAt(purchases []*repo.PurchaseRecord) error {
p.lock.Lock()
defer p.lock.Unlock()
tx, err := p.BeginTransaction()
if err != nil {
return fmt.Errorf("begin update purchase transaction: %s", err.Error())
}
for _, p := range purchases {
_, err = tx.Exec("update purchases set lastDisputeExpiryNotifiedAt = ? where orderID = ?", int(p.LastDisputeExpiryNotifiedAt.Unix()), p.OrderID)
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("update purchase error: (%s) w rollback error: (%s)", err.Error(), rErr.Error())
}
return fmt.Errorf("update purchase: %s", err.Error())
}
}
if err = tx.Commit(); err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf("commit purchase error: (%s) w rollback error: (%s)", err.Error(), rErr.Error())
}
return fmt.Errorf("commit update purchase transaction: %s", err.Error())
}
return nil
}