satellite/payments/stripecoinpayments: add test for invoice items

values

Change-Id: I924b02e70d7ed36b0a769f1072410dd811390abb
This commit is contained in:
Michal Niewrzal 2020-05-27 14:08:37 +02:00
parent 24a1eac16c
commit f532d4b25c
2 changed files with 89 additions and 15 deletions

View File

@ -702,36 +702,45 @@ func (service *Service) createInvoiceItems(ctx context.Context, cusID, projName
return err
}
projectItem := &stripe.InvoiceItemParams{
Currency: stripe.String(string(stripe.CurrencyUSD)),
Customer: stripe.String(cusID),
}
projectItem.AddMetadata("projectID", record.ProjectID.String())
items := service.InvoiceItemsFromProjectRecord(projName, record)
for _, item := range items {
item.Currency = stripe.String(string(stripe.CurrencyUSD))
item.Customer = stripe.String(cusID)
item.AddMetadata("projectID", record.ProjectID.String())
_, err = service.stripeClient.InvoiceItems().New(item)
if err != nil {
return err
}
}
return nil
}
// InvoiceItemsFromProjectRecord calculates Stripe invoice item from project record.
func (service *Service) InvoiceItemsFromProjectRecord(projName string, record ProjectRecord) (result []*stripe.InvoiceItemParams) {
projectItem := &stripe.InvoiceItemParams{}
projectItem.Description = stripe.String(fmt.Sprintf("Project %s - Object Storage (MB-Month)", projName))
projectItem.Quantity = stripe.Int64(storageMBMonthDecimal(record.Storage).IntPart())
storagePrice, _ := service.StorageMBMonthPriceCents.Float64()
projectItem.UnitAmountDecimal = stripe.Float64(storagePrice)
_, err = service.stripeClient.InvoiceItems().New(projectItem)
if err != nil {
return err
}
result = append(result, projectItem)
projectItem = &stripe.InvoiceItemParams{}
projectItem.Description = stripe.String(fmt.Sprintf("Project %s - Egress Bandwidth (MB)", projName))
projectItem.Quantity = stripe.Int64(egressMBDecimal(record.Egress).IntPart())
egressPrice, _ := service.EgressMBPriceCents.Float64()
projectItem.UnitAmountDecimal = stripe.Float64(egressPrice)
_, err = service.stripeClient.InvoiceItems().New(projectItem)
if err != nil {
return err
}
result = append(result, projectItem)
projectItem = &stripe.InvoiceItemParams{}
projectItem.Description = stripe.String(fmt.Sprintf("Project %s - Object Fee (Object-Month)", projName))
projectItem.Quantity = stripe.Int64(objectMonthDecimal(record.Objects).IntPart())
objectPrice, _ := service.ObjectMonthPriceCents.Float64()
projectItem.UnitAmountDecimal = stripe.Float64(objectPrice)
_, err = service.stripeClient.InvoiceItems().New(projectItem)
return err
result = append(result, projectItem)
return result
}
// InvoiceApplyCoupons iterates through unapplied project coupons and creates invoice line items

View File

@ -572,3 +572,68 @@ func TestService_ProjectsWithMembers(t *testing.T) {
require.Equal(t, len(projects), len(recordsPage.Records))
})
}
func TestService_InvoiceItemsFromProjectRecord(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellite := planet.Satellites[0]
// these numbers are fraction of cents, not of dollars.
expectedStoragePrice := 0.001
expectedEgressPrice := 0.0045
expectedObjectPrice := 0.00022
type TestCase struct {
Storage float64
Egress int64
Objects float64
StorageQuantity int64
EgressQuantity int64
ObjectsQuantity int64
}
var testCases = []TestCase{
{}, // all zeros
{
Storage: 10000000000, // Byte-Hours
// storage quantity is calculated to Megabyte-Months
// (10000000000 / 1000000) Byte-Hours to Megabytes-Hours
// round(10000 / 720) Megabytes-Hours to Megabyte-Months, 720 - hours in month
StorageQuantity: 14, // Megabyte-Months
},
{
Egress: 134 * memory.GB.Int64(), // Bytes
// egress quantity is calculated to Megabytes
// (134000000000 / 1000000) Bytes to Megabytes
EgressQuantity: 134000, // Megabytes
},
{
Objects: 400000, // Object-Hours
// object quantity is calculated to Object-Months
// round(400000 / 720) Object-Hours to Object-Months, 720 - hours in month
ObjectsQuantity: 556, // Object-Months
},
}
for _, tc := range testCases {
record := stripecoinpayments.ProjectRecord{
Storage: tc.Storage,
Egress: tc.Egress,
Objects: tc.Objects,
}
items := satellite.API.Payments.Service.InvoiceItemsFromProjectRecord("project name", record)
require.Equal(t, tc.StorageQuantity, *items[0].Quantity)
require.Equal(t, expectedStoragePrice, *items[0].UnitAmountDecimal)
require.Equal(t, tc.EgressQuantity, *items[1].Quantity)
require.Equal(t, expectedEgressPrice, *items[1].UnitAmountDecimal)
require.Equal(t, tc.ObjectsQuantity, *items[2].Quantity)
require.Equal(t, expectedObjectPrice, *items[2].UnitAmountDecimal)
}
})
}