2019-10-17 15:04:50 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-22 18:41:24 +01:00
|
|
|
"encoding/json"
|
2020-05-28 12:31:02 +01:00
|
|
|
"strconv"
|
2020-07-22 18:41:24 +01:00
|
|
|
"strings"
|
2020-05-28 12:31:02 +01:00
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2020-07-22 18:41:24 +01:00
|
|
|
"go.uber.org/zap"
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-17 15:04:50 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
"storj.io/storj/satellite/payments/monetary"
|
2019-10-17 15:04:50 +01:00
|
|
|
)
|
|
|
|
|
2020-05-28 12:31:02 +01:00
|
|
|
const (
|
|
|
|
// StripeDepositTransactionDescription is the description for Stripe
|
|
|
|
// balance transactions representing STORJ deposits.
|
|
|
|
StripeDepositTransactionDescription = "STORJ deposit"
|
|
|
|
|
|
|
|
// StripeDepositBonusTransactionDescription is the description for Stripe
|
|
|
|
// balance transactions representing bonuses received for STORJ deposits.
|
|
|
|
StripeDepositBonusTransactionDescription = "STORJ deposit bonus"
|
2020-06-12 09:39:40 +01:00
|
|
|
|
|
|
|
// StripeMigratedDepositBonusTransactionDescription is the description for
|
|
|
|
// Stripe balance transactions representing bonuses migrated from the
|
|
|
|
// 'credits' table of the satellite DB.
|
|
|
|
StripeMigratedDepositBonusTransactionDescription = "Migrated STORJ deposit bonus"
|
2020-05-28 12:31:02 +01:00
|
|
|
)
|
|
|
|
|
2019-10-17 15:04:50 +01:00
|
|
|
// ensure that storjTokens implements payments.StorjTokens.
|
|
|
|
var _ payments.StorjTokens = (*storjTokens)(nil)
|
|
|
|
|
|
|
|
// storjTokens implements payments.StorjTokens.
|
2020-01-29 00:57:15 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-10-17 15:04:50 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
c, err := tokens.service.stripeClient.Customers().Get(customerID, nil)
|
2019-10-17 15:04:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
rate, err := tokens.service.GetRate(ctx, monetary.StorjToken, monetary.USDollars)
|
2019-11-15 14:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
tokenAmount := convertFromCents(rate, amount)
|
2019-11-21 14:25:37 +00:00
|
|
|
|
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{
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: tokenAmount.AsDecimal(),
|
|
|
|
CurrencyIn: monetary.StorjToken,
|
|
|
|
CurrencyOut: monetary.StorjToken,
|
2019-10-17 15:04:50 +01:00
|
|
|
BuyerEmail: c.Email,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
key, err := coinpayments.GetTransactionKeyFromURL(tx.CheckoutURL)
|
2019-10-17 15:04:50 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
satellite/satellitedb: prepare to remove big.Float from db
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
Now that we have amounts represented using monetary.Amount, we can
simply store them in the database using integers (as given by the
.BaseUnits() method on monetary.Amount).
We should move toward storing the currency along with any monetary
amount, wherever we are storing amounts, because satellites might want
to deal with currencies other than STORJ and USD. Even better, it
becomes much clearer what currency each monetary value is _supposed_ to
be in (I had to dig through code to find that out for our current
monetary columns).
Deployment
----------
Getting rid of the big.Float columns will take multiple deployment
steps. There does not seem to be any way to make the change in a way
that lets existing queries continue to work on CockroachDB (it could be
done with rules and triggers and a stored procedure that knows how to
gob-decode big.Float objects, but CockroachDB doesn't have rules _or_
triggers _or_ stored procedures). Instead, in this first step, we make
no changes to the database schema, but add code that knows how to deal
with the planned changes to the schema when they are made in a future
"step 2" deployment. All functions that deal with the
coinbase_transactions table have been taught to recognize the "undefined
column" error, and when it is seen, to call a separate "transition shim"
function to accomplish the task. Once all the services are running this
code, and the step 2 deployment makes breaking changes to the schema,
any services that are still running and connected to the database will
keep working correctly because of the fallback code included here. The
step 2 deployment can be made without these transition shims included,
because it will apply the database schema changes before any of its code
runs.
Step 1:
No schema changes; just include code that recognizes the
"undefined column" error when dealing with the
coinbase_transactions or stripecoinpayments_tx_conversion_rates
tables, and if found, assumes that the column changes from Step
2 have already been made.
Step 2:
In coinbase_transactions:
* change the names of the 'amount' and 'received' columns to
'amount_gob' and 'received_gob' respectively
* add new 'amount_numeric' and 'received_numeric' columns with
INT8 type.
In stripecoinpayments_tx_conversion_rates:
* change the name of the 'rate' column to 'rate_gob'
* add new 'rate_numeric' column with NUMERIC(8, 8) type
Code reading from either of these tables must query both the X_gob
and X_numeric columns. If X_numeric is not null, its value should
be used; otherwise, the gob-encoded big.Float in X_gob should be
used. A chore might be included in this step that transitions values
from X_gob to X_numeric a few rows at a time.
Step 3:
Once all prod satellites have no values left in the _gob columns, we
can drop those columns and add NOT NULL constraints to the _numeric
columns.
Change-Id: Id6db304b404e6fde44f5a8c23cdaeeaaa2324f20
2021-08-10 23:30:23 +01:00
|
|
|
createTime, 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),
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: tx.Amount,
|
|
|
|
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,
|
satellite/satellitedb: prepare to remove big.Float from db
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
Now that we have amounts represented using monetary.Amount, we can
simply store them in the database using integers (as given by the
.BaseUnits() method on monetary.Amount).
We should move toward storing the currency along with any monetary
amount, wherever we are storing amounts, because satellites might want
to deal with currencies other than STORJ and USD. Even better, it
becomes much clearer what currency each monetary value is _supposed_ to
be in (I had to dig through code to find that out for our current
monetary columns).
Deployment
----------
Getting rid of the big.Float columns will take multiple deployment
steps. There does not seem to be any way to make the change in a way
that lets existing queries continue to work on CockroachDB (it could be
done with rules and triggers and a stored procedure that knows how to
gob-decode big.Float objects, but CockroachDB doesn't have rules _or_
triggers _or_ stored procedures). Instead, in this first step, we make
no changes to the database schema, but add code that knows how to deal
with the planned changes to the schema when they are made in a future
"step 2" deployment. All functions that deal with the
coinbase_transactions table have been taught to recognize the "undefined
column" error, and when it is seen, to call a separate "transition shim"
function to accomplish the task. Once all the services are running this
code, and the step 2 deployment makes breaking changes to the schema,
any services that are still running and connected to the database will
keep working correctly because of the fallback code included here. The
step 2 deployment can be made without these transition shims included,
because it will apply the database schema changes before any of its code
runs.
Step 1:
No schema changes; just include code that recognizes the
"undefined column" error when dealing with the
coinbase_transactions or stripecoinpayments_tx_conversion_rates
tables, and if found, assumes that the column changes from Step
2 have already been made.
Step 2:
In coinbase_transactions:
* change the names of the 'amount' and 'received' columns to
'amount_gob' and 'received_gob' respectively
* add new 'amount_numeric' and 'received_numeric' columns with
INT8 type.
In stripecoinpayments_tx_conversion_rates:
* change the name of the 'rate' column to 'rate_gob'
* add new 'rate_numeric' column with NUMERIC(8, 8) type
Code reading from either of these tables must query both the X_gob
and X_numeric columns. If X_numeric is not null, its value should
be used; otherwise, the gob-encoded big.Float in X_gob should be
used. A chore might be included in this step that transitions values
from X_gob to X_numeric a few rows at a time.
Step 3:
Once all prod satellites have no values left in the _gob columns, we
can drop those columns and add NOT NULL constraints to the _numeric
columns.
Change-Id: Id6db304b404e6fde44f5a8c23cdaeeaaa2324f20
2021-08-10 23:30:23 +01:00
|
|
|
CreatedAt: createTime,
|
2019-10-17 15:04:50 +01:00
|
|
|
}, 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),
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
Amount: tx.Amount,
|
|
|
|
Received: tx.Received,
|
|
|
|
AmountCents: convertToCents(rate, tx.Amount),
|
|
|
|
ReceivedCents: convertToCents(rate, tx.Received),
|
2019-11-21 13:23:16 +00:00
|
|
|
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
|
|
|
|
}
|
2020-05-28 12:31:02 +01:00
|
|
|
|
|
|
|
// ListDepositBonuses returns all deposit bonuses from Stripe associated with user.
|
|
|
|
func (tokens *storjTokens) ListDepositBonuses(ctx context.Context, userID uuid.UUID) (_ []payments.DepositBonus, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
cusID, err := tokens.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var bonuses []payments.DepositBonus
|
2020-07-22 18:41:24 +01:00
|
|
|
|
|
|
|
customer, err := tokens.service.stripeClient.Customers().Get(cusID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range customer.Metadata {
|
|
|
|
if !strings.HasPrefix(key, "credit_") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var credit payments.Credit
|
|
|
|
err = json.Unmarshal([]byte(value), &credit)
|
|
|
|
if err != nil {
|
|
|
|
tokens.service.log.Error("Error unmarshaling credit history from Stripe metadata",
|
|
|
|
zap.String("Customer ID", cusID),
|
|
|
|
zap.String("Metadata Key", key),
|
|
|
|
zap.String("Metadata Value", value),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bonuses = append(bonuses,
|
|
|
|
payments.DepositBonus{
|
|
|
|
TransactionID: payments.TransactionID(credit.TransactionID),
|
|
|
|
AmountCents: credit.Amount,
|
|
|
|
Percentage: 10,
|
|
|
|
CreatedAt: credit.Created,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:31:02 +01:00
|
|
|
it := tokens.service.stripeClient.CustomerBalanceTransactions().List(&stripe.CustomerBalanceTransactionListParams{Customer: stripe.String(cusID)})
|
|
|
|
for it.Next() {
|
|
|
|
tx := it.CustomerBalanceTransaction()
|
|
|
|
|
|
|
|
if tx.Type != stripe.CustomerBalanceTransactionTypeAdjustment {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tx.Description != StripeDepositBonusTransactionDescription {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
percentage := int64(10)
|
|
|
|
percentageStr, ok := tx.Metadata["percentage"]
|
|
|
|
if ok {
|
|
|
|
percentage, err = strconv.ParseInt(percentageStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bonuses = append(bonuses,
|
|
|
|
payments.DepositBonus{
|
|
|
|
TransactionID: []byte(tx.Metadata["txID"]),
|
|
|
|
AmountCents: -tx.Amount,
|
|
|
|
Percentage: percentage,
|
|
|
|
CreatedAt: time.Unix(tx.Created, 0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bonuses, nil
|
|
|
|
}
|