2019-10-31 16:56:54 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2022-12-21 20:27:29 +00:00
|
|
|
"github.com/zeebo/errs"
|
2023-02-13 17:32:39 +00:00
|
|
|
"go.uber.org/zap"
|
2019-10-31 16:56:54 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-31 16:56:54 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// invoices is an implementation of payments.Invoices.
|
2019-11-05 13:16:02 +00:00
|
|
|
//
|
2020-01-29 00:57:15 +00:00
|
|
|
// architecture: Service
|
2019-10-31 16:56:54 +00:00
|
|
|
type invoices struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
2023-02-02 22:11:09 +00:00
|
|
|
func (invoices *invoices) Create(ctx context.Context, userID uuid.UUID, price int64, desc string) (*payments.Invoice, error) {
|
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inv, err := invoices.service.stripeClient.Invoices().New(&stripe.InvoiceParams{
|
|
|
|
Customer: stripe.String(customerID),
|
|
|
|
Discounts: []*stripe.InvoiceDiscountParams{},
|
|
|
|
Description: stripe.String(desc),
|
|
|
|
PendingInvoiceItemsBehavior: stripe.String("exclude"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := invoices.service.stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{
|
|
|
|
Customer: stripe.String(customerID),
|
|
|
|
Amount: stripe.Int64(price),
|
|
|
|
Description: stripe.String(desc),
|
|
|
|
Currency: stripe.String(string(stripe.CurrencyUSD)),
|
|
|
|
Invoice: stripe.String(inv.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &payments.Invoice{
|
|
|
|
ID: inv.ID,
|
|
|
|
Description: inv.Description,
|
|
|
|
Amount: item.Amount,
|
|
|
|
Status: string(inv.Status),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (invoices *invoices) Pay(ctx context.Context, invoiceID, paymentMethodID string) (*payments.Invoice, error) {
|
|
|
|
inv, err := invoices.service.stripeClient.Invoices().Pay(invoiceID, &stripe.InvoicePayParams{
|
|
|
|
PaymentMethod: stripe.String(paymentMethodID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return &payments.Invoice{
|
|
|
|
ID: inv.ID,
|
|
|
|
Description: inv.Description,
|
|
|
|
Amount: inv.AmountPaid,
|
|
|
|
Status: string(inv.Status),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:27:29 +00:00
|
|
|
// AttemptPayOverdueInvoices attempts to pay a user's open, overdue invoices.
|
|
|
|
func (invoices *invoices) AttemptPayOverdueInvoices(ctx context.Context, userID uuid.UUID) (err error) {
|
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
Status: stripe.String(string(stripe.InvoiceStatusOpen)),
|
|
|
|
DueDateRange: &stripe.RangeQueryParams{LesserThan: time.Now().Unix()},
|
|
|
|
}
|
|
|
|
|
|
|
|
var errGrp errs.Group
|
|
|
|
|
|
|
|
invoicesIterator := invoices.service.stripeClient.Invoices().List(params)
|
|
|
|
for invoicesIterator.Next() {
|
|
|
|
stripeInvoice := invoicesIterator.Invoice()
|
|
|
|
|
|
|
|
params := &stripe.InvoicePayParams{}
|
|
|
|
invResponse, err := invoices.service.stripeClient.Invoices().Pay(stripeInvoice.ID, params)
|
|
|
|
if err != nil {
|
|
|
|
errGrp.Add(Error.New("unable to pay invoice %s: %w", stripeInvoice.ID, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if invResponse != nil && invResponse.Status != stripe.InvoiceStatusPaid {
|
|
|
|
errGrp.Add(Error.New("invoice not paid after payment triggered %s", stripeInvoice.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = invoicesIterator.Err(); err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errGrp.Err()
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:18:18 +01:00
|
|
|
// List returns a list of invoices for a given payment account.
|
|
|
|
func (invoices *invoices) List(ctx context.Context, userID uuid.UUID) (invoicesList []payments.Invoice, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
}
|
|
|
|
|
|
|
|
invoicesIterator := invoices.service.stripeClient.Invoices().List(params)
|
|
|
|
for invoicesIterator.Next() {
|
|
|
|
stripeInvoice := invoicesIterator.Invoice()
|
|
|
|
|
|
|
|
total := stripeInvoice.Total
|
2023-02-02 22:11:09 +00:00
|
|
|
if stripeInvoice.Lines != nil {
|
|
|
|
for _, line := range stripeInvoice.Lines.Data {
|
|
|
|
// If amount is negative, this is a coupon or a credit line item.
|
|
|
|
// Add them to the total.
|
|
|
|
if line.Amount < 0 {
|
|
|
|
total -= line.Amount
|
|
|
|
}
|
2021-10-18 23:18:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invoicesList = append(invoicesList, payments.Invoice{
|
|
|
|
ID: stripeInvoice.ID,
|
2023-02-13 17:32:39 +00:00
|
|
|
CustomerID: customerID,
|
2021-10-18 23:18:18 +01:00
|
|
|
Description: stripeInvoice.Description,
|
|
|
|
Amount: total,
|
2023-02-11 16:01:28 +00:00
|
|
|
Status: convertStatus(stripeInvoice.Status),
|
2021-10-18 23:18:18 +01:00
|
|
|
Link: stripeInvoice.InvoicePDF,
|
|
|
|
Start: time.Unix(stripeInvoice.PeriodStart, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = invoicesIterator.Err(); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoicesList, nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 17:32:39 +00:00
|
|
|
func (invoices *invoices) ListFailed(ctx context.Context) (invoicesList []payments.Invoice, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
status := string(stripe.InvoiceStatusOpen)
|
|
|
|
params := &stripe.InvoiceListParams{
|
|
|
|
Status: &status,
|
|
|
|
}
|
|
|
|
|
|
|
|
invoicesIterator := invoices.service.stripeClient.Invoices().List(params)
|
|
|
|
for invoicesIterator.Next() {
|
|
|
|
stripeInvoice := invoicesIterator.Invoice()
|
|
|
|
|
|
|
|
total := stripeInvoice.Total
|
|
|
|
for _, line := range stripeInvoice.Lines.Data {
|
|
|
|
// If amount is negative, this is a coupon or a credit line item.
|
|
|
|
// Add them to the total.
|
|
|
|
if line.Amount < 0 {
|
|
|
|
total -= line.Amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoices.isInvoiceFailed(stripeInvoice) {
|
|
|
|
invoicesList = append(invoicesList, payments.Invoice{
|
|
|
|
ID: stripeInvoice.ID,
|
|
|
|
CustomerID: stripeInvoice.Customer.ID,
|
|
|
|
Description: stripeInvoice.Description,
|
|
|
|
Amount: total,
|
|
|
|
Status: string(stripeInvoice.Status),
|
|
|
|
Link: stripeInvoice.InvoicePDF,
|
|
|
|
Start: time.Unix(stripeInvoice.PeriodStart, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = invoicesIterator.Err(); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoicesList, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:18:18 +01:00
|
|
|
// ListWithDiscounts returns a list of invoices and coupon usages for a given payment account.
|
|
|
|
func (invoices *invoices) ListWithDiscounts(ctx context.Context, userID uuid.UUID) (invoicesList []payments.Invoice, couponUsages []payments.CouponUsage, err error) {
|
2019-10-31 16:56:54 +00:00
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-31 16:56:54 +00:00
|
|
|
if err != nil {
|
2021-08-27 01:51:26 +01:00
|
|
|
return nil, nil, Error.Wrap(err)
|
2019-10-31 16:56:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
}
|
2021-10-22 15:16:27 +01:00
|
|
|
params.AddExpand("data.total_discount_amounts.discount")
|
2019-10-31 16:56:54 +00:00
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
invoicesIterator := invoices.service.stripeClient.Invoices().List(params)
|
2019-10-31 16:56:54 +00:00
|
|
|
for invoicesIterator.Next() {
|
|
|
|
stripeInvoice := invoicesIterator.Invoice()
|
|
|
|
|
2020-05-08 12:14:55 +01:00
|
|
|
total := stripeInvoice.Total
|
|
|
|
for _, line := range stripeInvoice.Lines.Data {
|
|
|
|
// If amount is negative, this is a coupon or a credit line item.
|
|
|
|
// Add them to the total.
|
|
|
|
if line.Amount < 0 {
|
|
|
|
total -= line.Amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
invoicesList = append(invoicesList, payments.Invoice{
|
|
|
|
ID: stripeInvoice.ID,
|
2023-02-13 17:32:39 +00:00
|
|
|
CustomerID: customerID,
|
2019-10-31 16:56:54 +00:00
|
|
|
Description: stripeInvoice.Description,
|
2020-05-08 12:14:55 +01:00
|
|
|
Amount: total,
|
2023-02-11 16:01:28 +00:00
|
|
|
Status: convertStatus(stripeInvoice.Status),
|
2019-10-31 16:56:54 +00:00
|
|
|
Link: stripeInvoice.InvoicePDF,
|
|
|
|
Start: time.Unix(stripeInvoice.PeriodStart, 0),
|
|
|
|
})
|
2021-08-27 01:51:26 +01:00
|
|
|
|
|
|
|
for _, dcAmt := range stripeInvoice.TotalDiscountAmounts {
|
|
|
|
if dcAmt == nil {
|
|
|
|
return nil, nil, Error.New("discount amount is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
dc := dcAmt.Discount
|
|
|
|
|
2023-03-13 06:52:01 +00:00
|
|
|
coupon, err := invoices.service.stripeDiscountToPaymentsCoupon(dc)
|
2021-08-27 01:51:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := payments.CouponUsage{
|
|
|
|
Coupon: *coupon,
|
|
|
|
Amount: dcAmt.Amount,
|
|
|
|
PeriodStart: time.Unix(stripeInvoice.PeriodStart, 0),
|
|
|
|
PeriodEnd: time.Unix(stripeInvoice.PeriodEnd, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.PromotionCode != nil {
|
|
|
|
usage.Coupon.PromoCode = dc.PromotionCode.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
couponUsages = append(couponUsages, usage)
|
|
|
|
}
|
2019-10-31 16:56:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = invoicesIterator.Err(); err != nil {
|
2021-08-27 01:51:26 +01:00
|
|
|
return nil, nil, Error.Wrap(err)
|
2019-10-31 16:56:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 01:51:26 +01:00
|
|
|
return invoicesList, couponUsages, nil
|
2019-10-31 16:56:54 +00:00
|
|
|
}
|
2020-07-06 22:31:40 +01:00
|
|
|
|
|
|
|
// CheckPendingItems returns if pending invoice items for a given payment account exist.
|
|
|
|
func (invoices *invoices) CheckPendingItems(ctx context.Context, userID uuid.UUID) (existingItems bool, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return false, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceItemListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
Pending: stripe.Bool(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
itemIterator := invoices.service.stripeClient.InvoiceItems().List(params)
|
|
|
|
for itemIterator.Next() {
|
|
|
|
item := itemIterator.InvoiceItem()
|
|
|
|
if item != nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = itemIterator.Err(); err != nil {
|
|
|
|
return false, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-02-11 16:01:28 +00:00
|
|
|
|
2023-02-11 16:36:21 +00:00
|
|
|
// Delete a draft invoice.
|
|
|
|
func (invoices *invoices) Delete(ctx context.Context, id string) (_ *payments.Invoice, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
stripeInvoice, err := invoices.service.stripeClient.Invoices().Del(id, &stripe.InvoiceParams{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return &payments.Invoice{
|
|
|
|
ID: stripeInvoice.ID,
|
|
|
|
Description: stripeInvoice.Description,
|
|
|
|
Amount: stripeInvoice.AmountDue,
|
|
|
|
Status: convertStatus(stripeInvoice.Status),
|
|
|
|
Link: stripeInvoice.InvoicePDF,
|
|
|
|
Start: time.Unix(stripeInvoice.PeriodStart, 0),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-11 16:01:28 +00:00
|
|
|
func convertStatus(stripestatus stripe.InvoiceStatus) string {
|
|
|
|
var status string
|
|
|
|
switch stripestatus {
|
|
|
|
case stripe.InvoiceStatusDraft:
|
|
|
|
status = payments.InvoiceStatusDraft
|
|
|
|
case stripe.InvoiceStatusOpen:
|
|
|
|
status = payments.InvoiceStatusOpen
|
|
|
|
case stripe.InvoiceStatusPaid:
|
|
|
|
status = payments.InvoiceStatusPaid
|
|
|
|
case stripe.InvoiceStatusUncollectible:
|
|
|
|
status = payments.InvoiceStatusUncollectible
|
|
|
|
case stripe.InvoiceStatusVoid:
|
|
|
|
status = payments.InvoiceStatusVoid
|
|
|
|
default:
|
|
|
|
status = string(stripestatus)
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
2023-02-13 17:32:39 +00:00
|
|
|
|
|
|
|
// isInvoiceFailed returns whether an invoice has failed.
|
|
|
|
func (invoices *invoices) isInvoiceFailed(invoice *stripe.Invoice) bool {
|
|
|
|
if invoice.DueDate > 0 {
|
|
|
|
// https://github.com/storj/storj/blob/77bf88e916a10dc898ebb594eafac667ed4426cd/satellite/payments/stripecoinpayments/service.go#L781-L787
|
|
|
|
invoices.service.log.Info("Skipping invoice marked for manual payment",
|
|
|
|
zap.String("id", invoice.ID),
|
|
|
|
zap.String("number", invoice.Number),
|
|
|
|
zap.String("customer", invoice.Customer.ID))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// https://stripe.com/docs/api/invoices/retrieve
|
|
|
|
if invoice.NextPaymentAttempt > 0 {
|
|
|
|
// stripe will automatically retry collecting payment.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|