Path: blob/main/components/public-api-server/pkg/billingservice/client.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package billingservice56import (7"context"8"fmt"910v1 "github.com/gitpod-io/gitpod/usage-api/v1"11"google.golang.org/grpc"12"google.golang.org/grpc/credentials/insecure"13)1415//go:generate mockgen -source=client.go Interface -destination=./mock_billingservice/billingservice.go > mock_billingservice/billingservice.go1617type Interface interface {18FinalizeInvoice(ctx context.Context, invoiceId string) error19CancelSubscription(ctx context.Context, subscriptionId string) error20OnChargeDispute(ctx context.Context, disputeID string) error21UpdateCustomerSubscriptionsTaxState(ctx context.Context, customerID string) error22}2324type Client struct {25b v1.BillingServiceClient26}2728func New(billingServiceAddress string) (*Client, error) {29conn, err := grpc.Dial(billingServiceAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))30if err != nil {31return nil, fmt.Errorf("failed to dial billing service gRPC server: %w", err)32}3334return &Client{b: v1.NewBillingServiceClient(conn)}, nil35}3637func (c *Client) FinalizeInvoice(ctx context.Context, invoiceId string) error {38_, err := c.b.FinalizeInvoice(ctx, &v1.FinalizeInvoiceRequest{InvoiceId: invoiceId})39if err != nil {40return fmt.Errorf("failed RPC to billing service: %s", err)41}4243return nil44}4546func (c *Client) CancelSubscription(ctx context.Context, subscriptionId string) error {47_, err := c.b.CancelSubscription(ctx, &v1.CancelSubscriptionRequest{SubscriptionId: subscriptionId})48if err != nil {49return fmt.Errorf("failed RPC to billing service: %s", err)50}5152return nil53}5455func (c *Client) OnChargeDispute(ctx context.Context, disputeID string) error {56_, err := c.b.OnChargeDispute(ctx, &v1.OnChargeDisputeRequest{57DisputeId: disputeID,58})59if err != nil {60return fmt.Errorf("failed RPC to billing service: %s", err)61}6263return nil64}6566func (c *Client) UpdateCustomerSubscriptionsTaxState(ctx context.Context, customerID string) error {67_, err := c.b.UpdateCustomerSubscriptionsTaxState(ctx, &v1.UpdateCustomerSubscriptionsTaxStateRequest{68CustomerId: customerID,69})70if err != nil {71return fmt.Errorf("failed RPC to billing service: %s", err)72}7374return nil75}767778