-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
satellite/payments: token deposit (#3283)
- Loading branch information
Showing
21 changed files
with
1,197 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (C) 2019 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package coinpayments | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/zeebo/errs" | ||
) | ||
|
||
// ErrNoAuthorizationKey is error that indicates that there is no authorization key. | ||
var ErrNoAuthorizationKey = Error.New("no authorization key") | ||
|
||
// GetTransacationKeyFromURL parses provided raw url string | ||
// and extracts authorization key from it. Returns ErrNoAuthorizationKey if | ||
// there is no authorization key and error if rawurl cannot be parsed. | ||
func GetTransacationKeyFromURL(rawurl string) (string, error) { | ||
u, err := url.Parse(rawurl) | ||
if err != nil { | ||
return "", errs.Wrap(err) | ||
} | ||
|
||
key := u.Query().Get("key") | ||
if key == "" { | ||
return "", ErrNoAuthorizationKey | ||
} | ||
|
||
return key, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (C) 2019 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package stripecoinpayments | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"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 { | ||
userID uuid.UUID | ||
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. | ||
func (tokens *storjTokens) Deposit(ctx context.Context, amount big.Float) (_ *payments.Transaction, err error) { | ||
defer mon.Task()(&ctx, amount)(&err) | ||
|
||
customerID, err := tokens.service.customers.GetCustomerID(ctx, tokens.userID) | ||
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) | ||
} | ||
|
||
tx, err := tokens.service.coinpayments.Transactions().Create(ctx, | ||
coinpayments.CreateTX{ | ||
Amount: amount, | ||
CurrencyIn: coinpayments.CurrencyLTCT, | ||
CurrencyOut: coinpayments.CurrencyLTCT, | ||
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) | ||
} | ||
|
||
cpTX, err := tokens.service.transactionsDB.Insert(ctx, | ||
Transaction{ | ||
ID: tx.ID, | ||
AccountID: tokens.userID, | ||
Address: tx.Address, | ||
Amount: tx.Amount, | ||
Received: big.Float{}, | ||
Status: coinpayments.StatusPending, | ||
Key: key, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, Error.Wrap(err) | ||
} | ||
|
||
return &payments.Transaction{ | ||
ID: payments.TransactionID(tx.ID), | ||
AccountID: tokens.userID, | ||
Amount: tx.Amount, | ||
Received: big.Float{}, | ||
Address: tx.Address, | ||
Status: payments.TransactionStatusPending, | ||
CreatedAt: cpTX.CreatedAt, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (C) 2019 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package stripecoinpayments | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/skyrings/skyring-common/tools/uuid" | ||
|
||
"storj.io/storj/satellite/payments/coinpayments" | ||
) | ||
|
||
// TransactionsDB is an interface which defines functionality | ||
// of DB which stores coinpayments transactions. | ||
// | ||
// architecture: Database | ||
type TransactionsDB interface { | ||
// Insert inserts new coinpayments transaction into DB. | ||
Insert(ctx context.Context, tx Transaction) (*Transaction, error) | ||
} | ||
|
||
// Transaction defines coinpayments transaction info that is stored in the DB. | ||
type Transaction struct { | ||
ID coinpayments.TransactionID | ||
AccountID uuid.UUID | ||
Address string | ||
Amount big.Float | ||
Received big.Float | ||
Status coinpayments.Status | ||
Key string | ||
CreatedAt time.Time | ||
} |
60 changes: 60 additions & 0 deletions
60
satellite/payments/stripecoinpayments/transactions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (C) 2019 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package stripecoinpayments_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/skyrings/skyring-common/tools/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"storj.io/storj/internal/testcontext" | ||
"storj.io/storj/satellite" | ||
"storj.io/storj/satellite/payments/coinpayments" | ||
"storj.io/storj/satellite/payments/stripecoinpayments" | ||
"storj.io/storj/satellite/satellitedb/satellitedbtest" | ||
) | ||
|
||
func TestTransactionsDB(t *testing.T) { | ||
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) { | ||
ctx := testcontext.New(t) | ||
defer ctx.Cleanup() | ||
|
||
transactions := db.CoinpaymentsTransactions() | ||
|
||
t.Run("insert", func(t *testing.T) { | ||
amount, received := new(big.Float).SetPrec(1000), new(big.Float).SetPrec(1000) | ||
|
||
amount, ok := amount.SetString("2.0000000000000000005") | ||
require.True(t, ok) | ||
received, ok = received.SetString("1.0000000000000000003") | ||
require.True(t, ok) | ||
|
||
createTx := stripecoinpayments.Transaction{ | ||
ID: "testID", | ||
AccountID: uuid.UUID{1, 2, 3}, | ||
Address: "testAddress", | ||
Amount: *amount, | ||
Received: *received, | ||
Status: coinpayments.StatusReceived, | ||
Key: "testKey", | ||
} | ||
|
||
tx, err := transactions.Insert(ctx, createTx) | ||
require.NoError(t, err) | ||
require.NotNil(t, tx) | ||
|
||
assert.Equal(t, createTx.ID, tx.ID) | ||
assert.Equal(t, createTx.AccountID, tx.AccountID) | ||
assert.Equal(t, createTx.Address, tx.Address) | ||
assert.Equal(t, createTx.Amount, tx.Amount) | ||
assert.Equal(t, createTx.Received, tx.Received) | ||
assert.Equal(t, createTx.Status, tx.Status) | ||
assert.Equal(t, createTx.Key, tx.Key) | ||
assert.False(t, tx.CreatedAt.IsZero()) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.