2020-05-28 12:31:02 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe_test
|
2020-05-28 12:31:02 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-07-22 18:41:24 +01:00
|
|
|
"encoding/json"
|
2020-05-28 12:31:02 +01:00
|
|
|
"testing"
|
2020-07-22 18:41:24 +01:00
|
|
|
"time"
|
2020-05-28 12:31:02 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2020-05-28 12:31:02 +01:00
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/testplanet"
|
2020-07-22 18:41:24 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
2023-04-06 12:41:14 +01:00
|
|
|
stripe1 "storj.io/storj/satellite/payments/stripe"
|
2020-05-28 12:31:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTokens_ListDepositBonuses(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
userID := planet.Uplinks[0].Projects[0].Owner.ID
|
|
|
|
|
|
|
|
// No deposit bonuses expected at this point
|
|
|
|
bonuses, err := satellite.API.Payments.Accounts.StorjTokens().ListDepositBonuses(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, bonuses, 0)
|
|
|
|
|
|
|
|
customerID, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
txID := base64.StdEncoding.EncodeToString(testrand.Bytes(4 * memory.B))
|
|
|
|
|
|
|
|
// Credit the Stripe balance with a STORJ deposit of $10
|
2020-07-22 18:41:24 +01:00
|
|
|
txParams := stripe.CustomerBalanceTransactionParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2020-05-28 12:31:02 +01:00
|
|
|
Customer: stripe.String(customerID),
|
|
|
|
Amount: stripe.Int64(-1000),
|
2023-04-06 12:41:14 +01:00
|
|
|
Description: stripe.String(stripe1.StripeDepositTransactionDescription),
|
2020-05-28 12:31:02 +01:00
|
|
|
}
|
2020-07-22 18:41:24 +01:00
|
|
|
txParams.AddMetadata("txID", txID)
|
2022-04-28 03:54:56 +01:00
|
|
|
_, err = satellite.API.Payments.StripeClient.CustomerBalanceTransactions().New(&txParams)
|
2020-05-28 12:31:02 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Credit the Stripe balance with a 13% bonus - $1.30
|
2020-07-22 18:41:24 +01:00
|
|
|
txParams = stripe.CustomerBalanceTransactionParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2020-05-28 12:31:02 +01:00
|
|
|
Customer: stripe.String(customerID),
|
|
|
|
Amount: stripe.Int64(-130),
|
2023-04-06 12:41:14 +01:00
|
|
|
Description: stripe.String(stripe1.StripeDepositBonusTransactionDescription),
|
2020-05-28 12:31:02 +01:00
|
|
|
}
|
2020-07-22 18:41:24 +01:00
|
|
|
txParams.AddMetadata("txID", txID)
|
|
|
|
txParams.AddMetadata("percentage", "13")
|
2022-04-28 03:54:56 +01:00
|
|
|
bonusTx, err := satellite.API.Payments.StripeClient.CustomerBalanceTransactions().New(&txParams)
|
2020-05-28 12:31:02 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-22 18:41:24 +01:00
|
|
|
// 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)
|
2023-03-14 02:59:24 +00:00
|
|
|
customerParams := stripe.CustomerParams{
|
|
|
|
Params: stripe.Params{
|
|
|
|
Context: ctx,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"credit_" + credit.TransactionID.String(): string(b),
|
|
|
|
},
|
|
|
|
},
|
2020-07-22 18:41:24 +01:00
|
|
|
}
|
2022-04-28 03:54:56 +01:00
|
|
|
_, err = satellite.API.Payments.StripeClient.Customers().Update(customerID, &customerParams)
|
2020-07-22 18:41:24 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect the list to contain the 10% bonus from Stripe metadata and 13% bonus from Stripe balance
|
2020-05-28 12:31:02 +01:00
|
|
|
bonuses, err = satellite.API.Payments.Accounts.StorjTokens().ListDepositBonuses(ctx, userID)
|
|
|
|
require.NoError(t, err)
|
2020-07-22 18:41:24 +01:00
|
|
|
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)
|
2020-05-28 12:31:02 +01:00
|
|
|
require.Equal(t, bonusTx.Created, bonuses[0].CreatedAt.Unix())
|
2020-07-22 18:41:24 +01:00
|
|
|
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())
|
2020-05-28 12:31:02 +01:00
|
|
|
})
|
|
|
|
}
|