Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/billingservice/client.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package billingservice
6
7
import (
8
"context"
9
"fmt"
10
11
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
12
"google.golang.org/grpc"
13
"google.golang.org/grpc/credentials/insecure"
14
)
15
16
//go:generate mockgen -source=client.go Interface -destination=./mock_billingservice/billingservice.go > mock_billingservice/billingservice.go
17
18
type Interface interface {
19
FinalizeInvoice(ctx context.Context, invoiceId string) error
20
CancelSubscription(ctx context.Context, subscriptionId string) error
21
OnChargeDispute(ctx context.Context, disputeID string) error
22
UpdateCustomerSubscriptionsTaxState(ctx context.Context, customerID string) error
23
}
24
25
type Client struct {
26
b v1.BillingServiceClient
27
}
28
29
func New(billingServiceAddress string) (*Client, error) {
30
conn, err := grpc.Dial(billingServiceAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
31
if err != nil {
32
return nil, fmt.Errorf("failed to dial billing service gRPC server: %w", err)
33
}
34
35
return &Client{b: v1.NewBillingServiceClient(conn)}, nil
36
}
37
38
func (c *Client) FinalizeInvoice(ctx context.Context, invoiceId string) error {
39
_, err := c.b.FinalizeInvoice(ctx, &v1.FinalizeInvoiceRequest{InvoiceId: invoiceId})
40
if err != nil {
41
return fmt.Errorf("failed RPC to billing service: %s", err)
42
}
43
44
return nil
45
}
46
47
func (c *Client) CancelSubscription(ctx context.Context, subscriptionId string) error {
48
_, err := c.b.CancelSubscription(ctx, &v1.CancelSubscriptionRequest{SubscriptionId: subscriptionId})
49
if err != nil {
50
return fmt.Errorf("failed RPC to billing service: %s", err)
51
}
52
53
return nil
54
}
55
56
func (c *Client) OnChargeDispute(ctx context.Context, disputeID string) error {
57
_, err := c.b.OnChargeDispute(ctx, &v1.OnChargeDisputeRequest{
58
DisputeId: disputeID,
59
})
60
if err != nil {
61
return fmt.Errorf("failed RPC to billing service: %s", err)
62
}
63
64
return nil
65
}
66
67
func (c *Client) UpdateCustomerSubscriptionsTaxState(ctx context.Context, customerID string) error {
68
_, err := c.b.UpdateCustomerSubscriptionsTaxState(ctx, &v1.UpdateCustomerSubscriptionsTaxStateRequest{
69
CustomerId: customerID,
70
})
71
if err != nil {
72
return fmt.Errorf("failed RPC to billing service: %s", err)
73
}
74
75
return nil
76
}
77
78