2019-10-17 15:04:50 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensure that storjTokens implements payments.StorjTokens.
|
|
|
|
var _ payments.StorjTokens = (*storjTokens)(nil)
|
|
|
|
|
|
|
|
// storjTokens implements payments.StorjTokens.
|
|
|
|
type storjTokens struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deposit creates new deposit transaction with the given amount returning
|
|
|
|
// ETH wallet address where funds should be sent. There is one
|
|
|
|
// hour limit to complete the transaction. Transaction is saved to DB with
|
|
|
|
// reference to the user who made the deposit.
|
2019-11-21 14:25:37 +00:00
|
|
|
func (tokens *storjTokens) Deposit(ctx context.Context, userID uuid.UUID, amount int64) (_ *payments.Transaction, err error) {
|
2019-10-17 15:42:18 +01:00
|
|
|
defer mon.Task()(&ctx, userID, amount)(&err)
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := tokens.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-17 15:04:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := tokens.service.stripeClient.Customers.Get(customerID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-11-15 15:27:13 +00:00
|
|
|
rate, err := tokens.service.GetRate(ctx, coinpayments.CurrencySTORJ, coinpayments.CurrencyUSD)
|
2019-11-15 14:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:25:37 +00:00
|
|
|
tokenAmount := convertFromCents(rate, amount).SetPrec(payments.STORJTokenPrecision)
|
|
|
|
|
2019-10-23 13:04:54 +01:00
|
|
|
tx, err := tokens.service.coinPayments.Transactions().Create(ctx,
|
2019-11-12 11:14:34 +00:00
|
|
|
&coinpayments.CreateTX{
|
2019-11-21 14:25:37 +00:00
|
|
|
Amount: *tokenAmount,
|
2019-11-15 15:27:13 +00:00
|
|
|
CurrencyIn: coinpayments.CurrencySTORJ,
|
|
|
|
CurrencyOut: coinpayments.CurrencySTORJ,
|
2019-10-17 15:04:50 +01:00
|
|
|
BuyerEmail: c.Email,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := coinpayments.GetTransacationKeyFromURL(tx.CheckoutURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:59:39 +00:00
|
|
|
if err = tokens.service.db.Transactions().LockRate(ctx, tx.ID, rate); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
cpTX, err := tokens.service.db.Transactions().Insert(ctx,
|
2019-10-17 15:04:50 +01:00
|
|
|
Transaction{
|
|
|
|
ID: tx.ID,
|
2019-10-17 15:42:18 +01:00
|
|
|
AccountID: userID,
|
2019-10-17 15:04:50 +01:00
|
|
|
Address: tx.Address,
|
|
|
|
Amount: tx.Amount,
|
|
|
|
Status: coinpayments.StatusPending,
|
|
|
|
Key: key,
|
2019-11-15 14:59:39 +00:00
|
|
|
Timeout: tx.Timeout,
|
2019-10-17 15:04:50 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &payments.Transaction{
|
|
|
|
ID: payments.TransactionID(tx.ID),
|
2019-11-12 11:14:34 +00:00
|
|
|
Amount: *payments.TokenAmountFromBigFloat(&tx.Amount),
|
2019-11-21 14:25:37 +00:00
|
|
|
Rate: *rate,
|
2019-10-17 15:04:50 +01:00
|
|
|
Address: tx.Address,
|
|
|
|
Status: payments.TransactionStatusPending,
|
2019-11-15 14:59:39 +00:00
|
|
|
Timeout: tx.Timeout,
|
2019-12-12 13:09:19 +00:00
|
|
|
Link: tx.CheckoutURL,
|
2019-10-17 15:04:50 +01:00
|
|
|
CreatedAt: cpTX.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-11-12 11:14:34 +00:00
|
|
|
|
|
|
|
// ListTransactionInfos fetches all transactions from the database for specified user, reconstructing checkout link.
|
|
|
|
func (tokens *storjTokens) ListTransactionInfos(ctx context.Context, userID uuid.UUID) (_ []payments.TransactionInfo, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
txs, err := tokens.service.db.Transactions().ListAccount(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []payments.TransactionInfo
|
|
|
|
for _, tx := range txs {
|
|
|
|
link := coinpayments.GetCheckoutURL(tx.Key, tx.ID)
|
|
|
|
|
|
|
|
var status payments.TransactionStatus
|
|
|
|
switch tx.Status {
|
|
|
|
case coinpayments.StatusPending:
|
|
|
|
status = payments.TransactionStatusPending
|
|
|
|
case coinpayments.StatusReceived:
|
|
|
|
status = payments.TransactionStatusPaid
|
|
|
|
case coinpayments.StatusCancelled:
|
|
|
|
status = payments.TransactionStatusCancelled
|
|
|
|
default:
|
|
|
|
// unknown
|
|
|
|
status = payments.TransactionStatus(tx.Status.String())
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:23:16 +00:00
|
|
|
rate, err := tokens.service.db.Transactions().GetLockedRate(ctx, tx.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-12 11:14:34 +00:00
|
|
|
infos = append(infos,
|
|
|
|
payments.TransactionInfo{
|
2019-11-21 13:23:16 +00:00
|
|
|
ID: []byte(tx.ID),
|
|
|
|
Amount: *payments.TokenAmountFromBigFloat(&tx.Amount),
|
|
|
|
Received: *payments.TokenAmountFromBigFloat(&tx.Received),
|
|
|
|
AmountCents: convertToCents(rate, &tx.Amount),
|
|
|
|
ReceivedCents: convertToCents(rate, &tx.Received),
|
|
|
|
Address: tx.Address,
|
|
|
|
Status: status,
|
|
|
|
Link: link,
|
|
|
|
ExpiresAt: tx.CreatedAt.Add(tx.Timeout),
|
|
|
|
CreatedAt: tx.CreatedAt,
|
2019-11-12 11:14:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos, nil
|
|
|
|
}
|