2020-05-18 10:17:05 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
2020-10-08 18:14:09 +01:00
|
|
|
"context"
|
2020-05-18 10:17:05 +01:00
|
|
|
"errors"
|
2020-08-19 13:43:56 +01:00
|
|
|
"fmt"
|
2020-07-23 11:41:48 +01:00
|
|
|
"sync"
|
2020-05-28 12:31:02 +01:00
|
|
|
"time"
|
2020-05-18 10:17:05 +01:00
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
|
|
|
"github.com/stripe/stripe-go/v72/charge"
|
|
|
|
"github.com/stripe/stripe-go/v72/customerbalancetransaction"
|
|
|
|
"github.com/stripe/stripe-go/v72/form"
|
|
|
|
"github.com/stripe/stripe-go/v72/invoice"
|
|
|
|
"github.com/stripe/stripe-go/v72/invoiceitem"
|
|
|
|
"github.com/stripe/stripe-go/v72/paymentmethod"
|
|
|
|
"github.com/stripe/stripe-go/v72/promotioncode"
|
2020-05-18 10:17:05 +01:00
|
|
|
|
2020-07-27 20:43:47 +01:00
|
|
|
"storj.io/common/storj"
|
2020-08-19 13:43:56 +01:00
|
|
|
"storj.io/common/testrand"
|
2020-05-18 10:17:05 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-10-08 18:14:09 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-05-18 10:17:05 +01:00
|
|
|
)
|
|
|
|
|
2020-07-27 20:43:47 +01:00
|
|
|
// mocks synchronized map for caching mockStripeClient.
|
2020-07-17 16:17:21 +01:00
|
|
|
//
|
|
|
|
// The satellite has a Core part and API part which mostly duplicate each
|
|
|
|
// other. Each of them have a StripeClient instance. This is not a problem in
|
|
|
|
// production, because the stripeClient implementation is stateless and calls
|
|
|
|
// the Web API of the same Stripe backend. But it is a problem in test
|
2020-07-27 20:43:47 +01:00
|
|
|
// environments as the mockStripeClient is stateful - the data is stored in
|
|
|
|
// in-memory maps. Therefore, we need the Core and API parts share the same
|
|
|
|
// instance of mockStripeClient.
|
|
|
|
var mocks = struct {
|
|
|
|
sync.Mutex
|
2020-10-08 18:14:09 +01:00
|
|
|
m map[storj.NodeID]*mockStripeState
|
2020-07-27 20:43:47 +01:00
|
|
|
}{
|
2020-10-08 18:14:09 +01:00
|
|
|
m: make(map[storj.NodeID]*mockStripeState),
|
2020-07-23 11:41:48 +01:00
|
|
|
}
|
2020-07-17 16:17:21 +01:00
|
|
|
|
2021-10-18 22:06:31 +01:00
|
|
|
var (
|
|
|
|
testPromoCodes = map[string]*stripe.PromotionCode{
|
|
|
|
"promo1": {
|
|
|
|
ID: "p1",
|
|
|
|
Coupon: &stripe.Coupon{
|
|
|
|
AmountOff: 500,
|
|
|
|
Currency: stripe.CurrencyUSD,
|
|
|
|
Name: "Test Promo Code 1",
|
2021-10-26 14:30:19 +01:00
|
|
|
ID: "c1",
|
2021-10-18 22:06:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"promo2": {
|
|
|
|
ID: "p2",
|
|
|
|
Coupon: &stripe.Coupon{
|
|
|
|
PercentOff: 50,
|
|
|
|
Name: "Test Promo Code 2",
|
2021-10-26 14:30:19 +01:00
|
|
|
ID: "c2",
|
2021-10-18 22:06:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
promoIDs = map[string]*stripe.PromotionCode{
|
|
|
|
"p1": testPromoCodes["promo1"],
|
|
|
|
"p2": testPromoCodes["promo2"],
|
|
|
|
}
|
2021-10-26 14:30:19 +01:00
|
|
|
couponIDs = map[string]*stripe.Coupon{
|
|
|
|
"c1": testPromoCodes["promo1"].Coupon,
|
|
|
|
"c2": testPromoCodes["promo2"].Coupon,
|
|
|
|
}
|
2021-10-18 22:06:31 +01:00
|
|
|
)
|
2021-07-08 20:06:07 +01:00
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
// mockStripeState Stripe client mock.
|
|
|
|
type mockStripeState struct {
|
|
|
|
customers *mockCustomersState
|
2020-05-18 10:17:05 +01:00
|
|
|
paymentMethods *mockPaymentMethods
|
|
|
|
invoices *mockInvoices
|
|
|
|
invoiceItems *mockInvoiceItems
|
|
|
|
customerBalanceTransactions *mockCustomerBalanceTransactions
|
|
|
|
charges *mockCharges
|
2021-06-22 01:09:56 +01:00
|
|
|
promoCodes *mockPromoCodes
|
2022-09-13 00:16:17 +01:00
|
|
|
creditNotes *mockCreditNotes
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
type mockStripeClient struct {
|
|
|
|
customersDB CustomersDB
|
|
|
|
usersDB console.Users
|
|
|
|
*mockStripeState
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
// mockEmptyQuery is a query with no results.
|
|
|
|
var mockEmptyQuery = stripe.Query(func(*stripe.Params, *form.Values) ([]interface{}, stripe.ListContainer, error) {
|
|
|
|
return nil, newListContainer(&stripe.ListMeta{}), nil
|
|
|
|
})
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
// NewStripeMock creates new Stripe client mock.
|
2020-07-27 20:43:47 +01:00
|
|
|
//
|
|
|
|
// A new mock is returned for each unique id. If this method is called multiple
|
|
|
|
// times with the same id, it will return the same mock instance for that id.
|
|
|
|
//
|
|
|
|
// If called by satellite component, the id param should be the peer.ID().
|
|
|
|
// If called by CLI tool, the id param should be a zero value, i.e. storj.NodeID{}.
|
|
|
|
// If called by satellitedb test case, the id param should be a random value,
|
|
|
|
// i.e. testrand.NodeID().
|
2020-10-08 18:14:09 +01:00
|
|
|
func NewStripeMock(id storj.NodeID, customersDB CustomersDB, usersDB console.Users) StripeClient {
|
2020-07-27 20:43:47 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
state, ok := mocks.m[id]
|
2020-07-27 20:43:47 +01:00
|
|
|
if !ok {
|
2020-10-08 18:14:09 +01:00
|
|
|
state = &mockStripeState{
|
|
|
|
customers: &mockCustomersState{},
|
2020-08-19 13:43:56 +01:00
|
|
|
paymentMethods: newMockPaymentMethods(),
|
2022-09-27 09:48:38 +01:00
|
|
|
invoices: newMockInvoices(),
|
|
|
|
invoiceItems: newMockInvoiceItems(),
|
2020-07-17 16:17:21 +01:00
|
|
|
customerBalanceTransactions: newMockCustomerBalanceTransactions(),
|
|
|
|
charges: &mockCharges{},
|
2021-07-08 20:06:07 +01:00
|
|
|
promoCodes: &mockPromoCodes{
|
2021-10-18 22:06:31 +01:00
|
|
|
promoCodes: testPromoCodes,
|
2021-07-08 20:06:07 +01:00
|
|
|
},
|
2020-07-17 16:17:21 +01:00
|
|
|
}
|
2022-09-27 09:48:38 +01:00
|
|
|
state.invoices.invoiceItems = state.invoiceItems
|
2020-10-08 18:14:09 +01:00
|
|
|
mocks.m[id] = state
|
2020-07-27 20:43:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
return &mockStripeClient{
|
|
|
|
customersDB: customersDB,
|
|
|
|
usersDB: usersDB,
|
|
|
|
mockStripeState: state,
|
|
|
|
}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) Customers() StripeCustomers {
|
2020-10-08 18:14:09 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
return &mockCustomers{
|
|
|
|
customersDB: m.customersDB,
|
|
|
|
usersDB: m.usersDB,
|
|
|
|
state: m.customers,
|
|
|
|
}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) PaymentMethods() StripePaymentMethods {
|
|
|
|
return m.paymentMethods
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) Invoices() StripeInvoices {
|
|
|
|
return m.invoices
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) InvoiceItems() StripeInvoiceItems {
|
|
|
|
return m.invoiceItems
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) CustomerBalanceTransactions() StripeCustomerBalanceTransactions {
|
|
|
|
return m.customerBalanceTransactions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockStripeClient) Charges() StripeCharges {
|
|
|
|
return m.charges
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
func (m *mockStripeClient) PromoCodes() StripePromoCodes {
|
|
|
|
return m.promoCodes
|
|
|
|
}
|
|
|
|
|
2022-09-13 00:16:17 +01:00
|
|
|
func (m *mockStripeClient) CreditNotes() StripeCreditNotes {
|
|
|
|
return m.creditNotes
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
type mockCustomers struct {
|
2020-10-08 18:14:09 +01:00
|
|
|
customersDB CustomersDB
|
|
|
|
usersDB console.Users
|
|
|
|
state *mockCustomersState
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
type mockCustomersState struct {
|
|
|
|
customers []*stripe.Customer
|
|
|
|
repopulated bool
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
// The Stripe Client Mock is in-memory so all data is lost when the satellite is stopped.
|
|
|
|
// We need to repopulate the mock on every restart to ensure that requests to the mock
|
|
|
|
// for existing users won't fail with errors like "customer not found".
|
|
|
|
func (m *mockCustomers) repopulate() error {
|
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
if !m.state.repopulated {
|
|
|
|
const limit = 25
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
cusPage, err := m.customersDB.List(ctx, 0, limit, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, cus := range cusPage.Customers {
|
|
|
|
user, err := m.usersDB.Get(ctx, cus.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.state.customers = append(m.state.customers, newMockCustomer(cus.ID, user.Email))
|
|
|
|
}
|
|
|
|
|
|
|
|
for cusPage.Next {
|
2021-03-03 23:31:46 +00:00
|
|
|
cusPage, err = m.customersDB.List(ctx, cusPage.NextOffset, limit, time.Now())
|
2020-10-08 18:14:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, cus := range cusPage.Customers {
|
|
|
|
user, err := m.usersDB.Get(ctx, cus.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.state.customers = append(m.state.customers, newMockCustomer(cus.ID, user.Email))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.state.repopulated = true
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
2020-10-08 18:14:09 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockCustomer(id, email string) *stripe.Customer {
|
|
|
|
return &stripe.Customer{
|
|
|
|
ID: id,
|
|
|
|
Email: email,
|
2020-05-18 10:17:05 +01:00
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettings{
|
|
|
|
DefaultPaymentMethod: &stripe.PaymentMethod{
|
|
|
|
ID: "pm_card_mastercard",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-08 18:14:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCustomers) New(params *stripe.CustomerParams) (*stripe.Customer, error) {
|
|
|
|
if err := m.repopulate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customer := newMockCustomer(uuid.String(), *params.Email)
|
2020-07-28 17:26:22 +01:00
|
|
|
|
2021-10-26 14:30:19 +01:00
|
|
|
if params.PromotionCode != nil && promoIDs[*params.PromotionCode] != nil {
|
|
|
|
customer.Discount = &stripe.Discount{Coupon: promoIDs[*params.PromotionCode].Coupon}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Coupon != nil {
|
|
|
|
customer.Discount = &stripe.Discount{Coupon: couponIDs[*params.Coupon]}
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:26:22 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
m.state.customers = append(m.state.customers, customer)
|
2020-05-18 10:17:05 +01:00
|
|
|
return customer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCustomers) Get(id string, params *stripe.CustomerParams) (*stripe.Customer, error) {
|
2020-10-08 18:14:09 +01:00
|
|
|
if err := m.repopulate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:26:22 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2020-10-08 18:14:09 +01:00
|
|
|
for _, customer := range m.state.customers {
|
2020-05-18 10:17:05 +01:00
|
|
|
if id == customer.ID {
|
|
|
|
return customer, nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:14:09 +01:00
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
return nil, errors.New("customer not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCustomers) Update(id string, params *stripe.CustomerParams) (*stripe.Customer, error) {
|
2020-10-08 18:14:09 +01:00
|
|
|
if err := m.repopulate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
customer, err := m.Get(id, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:39:40 +01:00
|
|
|
if params == nil {
|
|
|
|
return customer, nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:26:22 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2020-06-12 09:39:40 +01:00
|
|
|
if params.Metadata != nil {
|
|
|
|
customer.Metadata = params.Metadata
|
|
|
|
}
|
2021-10-18 22:06:31 +01:00
|
|
|
if params.PromotionCode != nil && promoIDs[*params.PromotionCode] != nil {
|
|
|
|
customer.Discount = &stripe.Discount{Coupon: promoIDs[*params.PromotionCode].Coupon}
|
2021-07-08 20:06:07 +01:00
|
|
|
}
|
2022-09-27 09:48:38 +01:00
|
|
|
if params.Coupon != nil {
|
|
|
|
customer.Discount = &stripe.Discount{Coupon: &stripe.Coupon{ID: *params.Coupon}}
|
|
|
|
}
|
2020-06-12 09:39:40 +01:00
|
|
|
|
|
|
|
// TODO update customer with more params as necessary
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
return customer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPaymentMethods struct {
|
2020-08-19 13:43:56 +01:00
|
|
|
// attached contains a mapping of customerID to its paymentMethods
|
|
|
|
attached map[string][]*stripe.PaymentMethod
|
|
|
|
// unattached contains created but not attached paymentMethods
|
|
|
|
unattached []*stripe.PaymentMethod
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:43:56 +01:00
|
|
|
func newMockPaymentMethods() *mockPaymentMethods {
|
|
|
|
return &mockPaymentMethods{
|
|
|
|
attached: map[string][]*stripe.PaymentMethod{},
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
2020-08-19 13:43:56 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
// listContainer implements Stripe's ListContainer interface.
|
|
|
|
type listContainer struct {
|
|
|
|
listMeta *stripe.ListMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListContainer(meta *stripe.ListMeta) *listContainer {
|
|
|
|
return &listContainer{listMeta: meta}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *listContainer) GetListMeta() *stripe.ListMeta {
|
|
|
|
return c.listMeta
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:43:56 +01:00
|
|
|
func (m *mockPaymentMethods) List(listParams *stripe.PaymentMethodListParams) *paymentmethod.Iter {
|
2022-08-01 13:41:12 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
listMeta := &stripe.ListMeta{
|
2020-05-18 10:17:05 +01:00
|
|
|
HasMore: false,
|
2020-08-19 13:43:56 +01:00
|
|
|
TotalCount: uint32(len(m.attached)),
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
2021-06-22 01:09:56 +01:00
|
|
|
lc := newListContainer(listMeta)
|
|
|
|
|
|
|
|
query := stripe.Query(func(*stripe.Params, *form.Values) ([]interface{}, stripe.ListContainer, error) {
|
2020-08-19 13:43:56 +01:00
|
|
|
list, ok := m.attached[*listParams.Customer]
|
|
|
|
if !ok {
|
|
|
|
list = []*stripe.PaymentMethod{}
|
|
|
|
}
|
|
|
|
ret := make([]interface{}, len(list))
|
|
|
|
|
|
|
|
for i, v := range list {
|
|
|
|
ret[i] = v
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
return ret, lc, nil
|
|
|
|
})
|
|
|
|
return &paymentmethod.Iter{Iter: stripe.GetIter(nil, query)}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentMethods) New(params *stripe.PaymentMethodParams) (*stripe.PaymentMethod, error) {
|
2020-08-19 13:43:56 +01:00
|
|
|
randID := testrand.BucketName()
|
|
|
|
newMethod := &stripe.PaymentMethod{
|
|
|
|
ID: fmt.Sprintf("pm_card_%s", randID),
|
|
|
|
Card: &stripe.PaymentMethodCard{
|
|
|
|
ExpMonth: 12,
|
|
|
|
ExpYear: 2050,
|
|
|
|
Brand: "Mastercard",
|
|
|
|
Last4: "4444",
|
|
|
|
Description: randID,
|
|
|
|
},
|
|
|
|
Type: stripe.PaymentMethodTypeCard,
|
|
|
|
}
|
|
|
|
|
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
m.unattached = append(m.unattached, newMethod)
|
|
|
|
|
|
|
|
return newMethod, nil
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentMethods) Attach(id string, params *stripe.PaymentMethodAttachParams) (*stripe.PaymentMethod, error) {
|
2020-08-19 13:43:56 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2022-08-01 13:41:12 +01:00
|
|
|
var method *stripe.PaymentMethod
|
2020-08-19 13:43:56 +01:00
|
|
|
for _, candidate := range m.unattached {
|
|
|
|
if candidate.ID == id {
|
|
|
|
method = candidate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attached, ok := m.attached[*params.Customer]
|
|
|
|
if !ok {
|
|
|
|
attached = []*stripe.PaymentMethod{}
|
|
|
|
}
|
|
|
|
m.attached[*params.Customer] = append(attached, method)
|
|
|
|
return method, nil
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPaymentMethods) Detach(id string, params *stripe.PaymentMethodDetachParams) (*stripe.PaymentMethod, error) {
|
2020-08-19 13:43:56 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2022-08-01 13:41:12 +01:00
|
|
|
var unattached *stripe.PaymentMethod
|
2020-08-19 13:43:56 +01:00
|
|
|
for user, userMethods := range m.attached {
|
|
|
|
var remaining []*stripe.PaymentMethod
|
|
|
|
for _, method := range userMethods {
|
|
|
|
if id == method.ID {
|
|
|
|
unattached = method
|
|
|
|
} else {
|
|
|
|
remaining = append(remaining, method)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.attached[user] = remaining
|
|
|
|
}
|
|
|
|
|
|
|
|
return unattached, nil
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockInvoices struct {
|
2022-09-27 09:48:38 +01:00
|
|
|
invoices map[string][]*stripe.Invoice
|
|
|
|
invoiceItems *mockInvoiceItems
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockInvoices() *mockInvoices {
|
|
|
|
return &mockInvoices{
|
|
|
|
invoices: make(map[string][]*stripe.Invoice),
|
|
|
|
}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error) {
|
2022-09-27 09:48:38 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
invoice := &stripe.Invoice{ID: "in_" + string(testrand.RandAlphaNumeric(25))}
|
|
|
|
m.invoices[*params.Customer] = append(m.invoices[*params.Customer], invoice)
|
|
|
|
|
|
|
|
if items, ok := m.invoiceItems.items[*params.Customer]; ok {
|
|
|
|
for _, item := range items {
|
|
|
|
if item.Invoice == nil {
|
|
|
|
item.Invoice = invoice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoice, nil
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockInvoices) List(listParams *stripe.InvoiceListParams) *invoice.Iter {
|
2022-09-27 09:48:38 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
listMeta := &stripe.ListMeta{
|
|
|
|
HasMore: false,
|
|
|
|
TotalCount: uint32(len(m.invoices)),
|
|
|
|
}
|
|
|
|
lc := newListContainer(listMeta)
|
|
|
|
|
|
|
|
query := stripe.Query(func(*stripe.Params, *form.Values) (ret []interface{}, _ stripe.ListContainer, _ error) {
|
|
|
|
if listParams.Customer == nil {
|
|
|
|
for _, invoices := range m.invoices {
|
|
|
|
for _, invoice := range invoices {
|
|
|
|
ret = append(ret, invoice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if list, ok := m.invoices[*listParams.Customer]; ok {
|
|
|
|
for _, invoice := range list {
|
|
|
|
ret = append(ret, invoice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, lc, nil
|
|
|
|
})
|
|
|
|
return &invoice.Iter{Iter: stripe.GetIter(nil, query)}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 19:00:07 +01:00
|
|
|
func (m *mockInvoices) Update(id string, params *stripe.InvoiceParams) (invoice *stripe.Invoice, err error) {
|
|
|
|
for _, invoices := range m.invoices {
|
|
|
|
for _, invoice := range invoices {
|
|
|
|
if invoice.ID == id {
|
|
|
|
return invoice, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("invoice not found")
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:18:36 +01:00
|
|
|
func (m *mockInvoices) FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 00:16:17 +01:00
|
|
|
func (m *mockInvoices) Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
type mockInvoiceItems struct {
|
2022-09-27 09:48:38 +01:00
|
|
|
items map[string][]*stripe.InvoiceItem
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockInvoiceItems() *mockInvoiceItems {
|
|
|
|
return &mockInvoiceItems{
|
|
|
|
items: make(map[string][]*stripe.InvoiceItem),
|
|
|
|
}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
func (m *mockInvoiceItems) Update(id string, params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockInvoiceItems) Del(id string, params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
func (m *mockInvoiceItems) New(params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error) {
|
2022-09-27 09:48:38 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
item := &stripe.InvoiceItem{
|
|
|
|
Metadata: params.Metadata,
|
|
|
|
}
|
|
|
|
m.items[*params.Customer] = append(m.items[*params.Customer], item)
|
|
|
|
|
|
|
|
return item, nil
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:31:40 +01:00
|
|
|
func (m *mockInvoiceItems) List(listParams *stripe.InvoiceItemListParams) *invoiceitem.Iter {
|
2022-09-27 09:48:38 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
listMeta := &stripe.ListMeta{
|
|
|
|
HasMore: false,
|
|
|
|
TotalCount: uint32(len(m.items)),
|
|
|
|
}
|
|
|
|
lc := newListContainer(listMeta)
|
|
|
|
|
|
|
|
query := stripe.Query(func(*stripe.Params, *form.Values) ([]interface{}, stripe.ListContainer, error) {
|
|
|
|
list, ok := m.items[*listParams.Customer]
|
|
|
|
if !ok {
|
|
|
|
list = []*stripe.InvoiceItem{}
|
|
|
|
}
|
|
|
|
ret := make([]interface{}, len(list))
|
|
|
|
|
|
|
|
for i, v := range list {
|
|
|
|
ret[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, lc, nil
|
|
|
|
})
|
|
|
|
return &invoiceitem.Iter{Iter: stripe.GetIter(nil, query)}
|
2020-07-06 22:31:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 10:17:05 +01:00
|
|
|
type mockCustomerBalanceTransactions struct {
|
2020-05-28 12:31:02 +01:00
|
|
|
transactions map[string][]*stripe.CustomerBalanceTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockCustomerBalanceTransactions() *mockCustomerBalanceTransactions {
|
|
|
|
return &mockCustomerBalanceTransactions{
|
|
|
|
transactions: make(map[string][]*stripe.CustomerBalanceTransaction),
|
|
|
|
}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCustomerBalanceTransactions) New(params *stripe.CustomerBalanceTransactionParams) (*stripe.CustomerBalanceTransaction, error) {
|
2020-05-28 12:31:02 +01:00
|
|
|
tx := &stripe.CustomerBalanceTransaction{
|
|
|
|
Type: stripe.CustomerBalanceTransactionTypeAdjustment,
|
|
|
|
Amount: *params.Amount,
|
|
|
|
Description: *params.Description,
|
|
|
|
Metadata: params.Metadata,
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:26:22 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2020-05-28 12:31:02 +01:00
|
|
|
m.transactions[*params.Customer] = append(m.transactions[*params.Customer], tx)
|
|
|
|
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCustomerBalanceTransactions) List(listParams *stripe.CustomerBalanceTransactionListParams) *customerbalancetransaction.Iter {
|
2020-07-28 17:26:22 +01:00
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
query := stripe.Query(func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
|
2020-05-28 12:31:02 +01:00
|
|
|
txs := m.transactions[*listParams.Customer]
|
|
|
|
ret := make([]interface{}, len(txs))
|
|
|
|
|
|
|
|
for i, v := range txs {
|
|
|
|
ret[i] = v
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
listMeta := &stripe.ListMeta{
|
2020-05-28 12:31:02 +01:00
|
|
|
TotalCount: uint32(len(txs)),
|
|
|
|
}
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
lc := newListContainer(listMeta)
|
|
|
|
|
|
|
|
return ret, lc, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return &customerbalancetransaction.Iter{Iter: stripe.GetIter(listParams, query)}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockCharges struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCharges) List(listParams *stripe.ChargeListParams) *charge.Iter {
|
2021-06-22 01:09:56 +01:00
|
|
|
return &charge.Iter{Iter: stripe.GetIter(listParams, mockEmptyQuery)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPromoCodes struct {
|
2021-10-18 22:06:31 +01:00
|
|
|
promoCodes map[string]*stripe.PromotionCode
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockPromoCodes) List(params *stripe.PromotionCodeListParams) *promotioncode.Iter {
|
|
|
|
mocks.Lock()
|
|
|
|
defer mocks.Unlock()
|
|
|
|
|
|
|
|
query := stripe.Query(func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
|
2021-10-18 22:06:31 +01:00
|
|
|
promoCode := m.promoCodes[*params.Code]
|
|
|
|
if promoCode == nil {
|
|
|
|
return make([]interface{}, 0), &stripe.ListMeta{TotalCount: 0}, nil
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
2021-10-18 22:06:31 +01:00
|
|
|
ret := make([]interface{}, 1)
|
|
|
|
ret[0] = promoCode
|
2021-06-22 01:09:56 +01:00
|
|
|
|
|
|
|
listMeta := &stripe.ListMeta{
|
2021-10-18 22:06:31 +01:00
|
|
|
TotalCount: 1,
|
2021-06-22 01:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lc := newListContainer(listMeta)
|
|
|
|
|
|
|
|
return ret, lc, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return &promotioncode.Iter{Iter: stripe.GetIter(params, query)}
|
2020-05-18 10:17:05 +01:00
|
|
|
}
|
2022-09-13 00:16:17 +01:00
|
|
|
|
|
|
|
type mockCreditNotes struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockCreditNotes) New(params *stripe.CreditNoteParams) (*stripe.CreditNote, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|