satellite/payments: fetch old deposit bonuses from Stripe metadata
Jira: https://storjlabs.atlassian.net/browse/USR-822 The balance history in Satellite GUI display the deposit bonuses as separate rows. These bonuses used to be stored in the satellite DB. We recently started depositing the bonus directly to the Stripe balance and migrated old bonuses to Stripe metadata. This change displays all billing history entirely from Stripe, so we can remove the `credits` and `credits_spendings` DB tables in a next step. Change-Id: I14c304c66ec47c6a51f5b8508f11470cf36c4e24
This commit is contained in:
parent
20437b43a1
commit
4bcf308a04
@ -335,23 +335,6 @@ func (paymentService PaymentsService) BillingHistory(ctx context.Context) (billi
|
||||
)
|
||||
}
|
||||
|
||||
credits, err := paymentService.service.accounts.Credits().ListByUserID(ctx, auth.User.ID)
|
||||
if err != nil {
|
||||
return nil, Error.Wrap(err)
|
||||
}
|
||||
|
||||
for _, credit := range credits {
|
||||
billingHistory = append(billingHistory,
|
||||
&BillingHistoryItem{
|
||||
Description: "10% Bonus for STORJ Token Deposit",
|
||||
Amount: credit.Amount,
|
||||
Status: "Added to balance",
|
||||
Start: credit.Created,
|
||||
Type: DepositBonus,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
bonuses, err := paymentService.service.accounts.StorjTokens().ListDepositBonuses(ctx, auth.User.ID)
|
||||
if err != nil {
|
||||
return nil, Error.Wrap(err)
|
||||
|
@ -5,10 +5,13 @@ package stripecoinpayments
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/stripe/stripe-go"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"storj.io/common/uuid"
|
||||
"storj.io/storj/satellite/payments"
|
||||
@ -172,6 +175,39 @@ func (tokens *storjTokens) ListDepositBonuses(ctx context.Context, userID uuid.U
|
||||
}
|
||||
|
||||
var bonuses []payments.DepositBonus
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
it := tokens.service.stripeClient.CustomerBalanceTransactions().List(&stripe.CustomerBalanceTransactionListParams{Customer: stripe.String(cusID)})
|
||||
for it.Next() {
|
||||
tx := it.CustomerBalanceTransaction()
|
||||
|
@ -5,7 +5,9 @@ package stripecoinpayments_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stripe/stripe-go"
|
||||
@ -14,6 +16,8 @@ import (
|
||||
"storj.io/common/testcontext"
|
||||
"storj.io/common/testrand"
|
||||
"storj.io/storj/private/testplanet"
|
||||
"storj.io/storj/satellite/payments"
|
||||
"storj.io/storj/satellite/payments/coinpayments"
|
||||
"storj.io/storj/satellite/payments/stripecoinpayments"
|
||||
)
|
||||
|
||||
@ -35,33 +39,53 @@ func TestTokens_ListDepositBonuses(t *testing.T) {
|
||||
txID := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
|
||||
|
||||
// Credit the Stripe balance with a STORJ deposit of $10
|
||||
params := stripe.CustomerBalanceTransactionParams{
|
||||
txParams := stripe.CustomerBalanceTransactionParams{
|
||||
Customer: stripe.String(customerID),
|
||||
Amount: stripe.Int64(-1000),
|
||||
Description: stripe.String(stripecoinpayments.StripeDepositTransactionDescription),
|
||||
}
|
||||
params.AddMetadata("txID", txID)
|
||||
_, err = satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(¶ms)
|
||||
txParams.AddMetadata("txID", txID)
|
||||
_, err = satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(&txParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Credit the Stripe balance with a 13% bonus - $1.30
|
||||
params = stripe.CustomerBalanceTransactionParams{
|
||||
txParams = stripe.CustomerBalanceTransactionParams{
|
||||
Customer: stripe.String(customerID),
|
||||
Amount: stripe.Int64(-130),
|
||||
Description: stripe.String(stripecoinpayments.StripeDepositBonusTransactionDescription),
|
||||
}
|
||||
params.AddMetadata("txID", txID)
|
||||
params.AddMetadata("percentage", "13")
|
||||
bonusTx, err := satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(¶ms)
|
||||
txParams.AddMetadata("txID", txID)
|
||||
txParams.AddMetadata("percentage", "13")
|
||||
bonusTx, err := satellite.API.Payments.Stripe.CustomerBalanceTransactions().New(&txParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Expect the list to contain the 13% bonus
|
||||
// Add migrated deposit bonus to Stripe metadata
|
||||
credit := payments.Credit{
|
||||
UserID: userID,
|
||||
Amount: 100,
|
||||
TransactionID: coinpayments.TransactionID(base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))),
|
||||
Created: time.Now(),
|
||||
}
|
||||
b, err := json.Marshal(credit)
|
||||
require.NoError(t, err)
|
||||
customerParams := stripe.CustomerParams{}
|
||||
customerParams.Metadata = map[string]string{
|
||||
"credit_" + credit.TransactionID.String(): string(b),
|
||||
}
|
||||
_, err = satellite.API.Payments.Stripe.Customers().Update(customerID, &customerParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Expect the list to contain the 10% bonus from Stripe metadata and 13% bonus from Stripe balance
|
||||
bonuses, err = satellite.API.Payments.Accounts.StorjTokens().ListDepositBonuses(ctx, userID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, bonuses, 1)
|
||||
require.EqualValues(t, txID, bonuses[0].TransactionID)
|
||||
require.EqualValues(t, 130, bonuses[0].AmountCents)
|
||||
require.EqualValues(t, 13, bonuses[0].Percentage)
|
||||
require.Len(t, bonuses, 2)
|
||||
require.EqualValues(t, credit.TransactionID, bonuses[0].TransactionID)
|
||||
require.EqualValues(t, 100, bonuses[0].AmountCents)
|
||||
require.EqualValues(t, 10, bonuses[0].Percentage)
|
||||
require.Equal(t, bonusTx.Created, bonuses[0].CreatedAt.Unix())
|
||||
require.EqualValues(t, txID, bonuses[1].TransactionID)
|
||||
require.EqualValues(t, 130, bonuses[1].AmountCents)
|
||||
require.EqualValues(t, 13, bonuses[1].Percentage)
|
||||
require.Equal(t, bonusTx.Created, bonuses[1].CreatedAt.Unix())
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user