Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1984

Merged
merged 3 commits into from
Jan 28, 2025
Merged

Dev #1984

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion admin-ui/src/components/plan/ovhPrices.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ export default {
},
sp() {
return this.$store.getters["servicesProviders/all"].find(
({ type }) => type === "ovh"
(sp) =>
sp.type === "ovh" && sp.meta?.plans?.includes(this.template.uuid)
);
},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/billing/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func (s *BillingServiceServer) ProcessInvoiceWhmcsSync(log *zap.Logger, ctx cont
return nil
}

if event.GetData()["paid-with-balance"].GetBoolValue() {
ctx = context.WithValue(ctx, "paid-with-balance", true)
}
if err = gw.UpdateInvoice(ctx, inv.Invoice); err != nil {
return fmt.Errorf("failed to update invoice on whmcs: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/billing/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,13 @@ quit:
return nil, status.Error(codes.Internal, "Failed to update invoice")
}

paidWithBalance, _ := ctx.Value("paid-with-balance").(bool)
if err = s.invoicesPublisher(&epb.Event{
Uuid: old.GetUuid(),
Key: billing.InvoiceStatusToKey(newStatus),
Data: map[string]*structpb.Value{
"gw-callback": structpb.NewBoolValue(payments.GetGatewayCallbackValue(ctx, req.Header())),
"paid-with-balance": structpb.NewBoolValue(paidWithBalance),
"gw-callback": structpb.NewBoolValue(payments.GetGatewayCallbackValue(ctx, req.Header())),
},
}); err != nil {
log.Error("Failed to publish invoice status update", zap.Error(err))
Expand Down Expand Up @@ -655,6 +657,7 @@ func (s *BillingServiceServer) PayWithBalance(ctx context.Context, r *connect.Re
return nil, status.Error(codes.FailedPrecondition, "Not enough balance to perform operation")
}

ctx = context.WithValue(ctx, "paid-with-balance", true)
resp, err := s.UpdateInvoiceStatus(ctxWithRoot(ctx), connect.NewRequest(&pb.UpdateInvoiceStatusRequest{
Uuid: inv.GetUuid(),
Status: pb.BillingStatus_PAID,
Expand Down Expand Up @@ -744,7 +747,7 @@ func (s *BillingServiceServer) payWithBalanceWhmcsInvoice(ctx context.Context, i
return nil, status.Error(codes.FailedPrecondition, "Not enough balance to perform operation")
}

if err = s.whmcsGateway.PayInvoice(ctx, int(invId)); err != nil {
if err = s.whmcsGateway.PayInvoice(ctx, int(invId), true); err != nil {
log.Error("Failed to pay invoice", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to perform payment with balance. Error: "+err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/nocloud/payments/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type PaymentGateway interface {
//AddClient(types.CreateUserParams) (int, error)
}

type ContextKey string

var paidWithBalanceKey = ContextKey("paid-with-balance")

func GetGatewayCallbackValue(ctx context.Context, h ...http.Header) bool {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
Expand Down
30 changes: 27 additions & 3 deletions pkg/nocloud/payments/whmcs_gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const invoiceNotFoundApiMsg = "Invoice ID Not Found"

const getInvoicesBatchSize = 10000 // WHMCS throws memory limit exception nearly on 50000 batch size

const BalancePayMethod = "balancepay"

var balancePayMethod = BalancePayMethod

func NewWhmcsGateway(data WhmcsData, acc graph.AccountsController, curr graph.CurrencyController, invMan NoCloudInvoicesManager, whmcsTaxExcluded bool) *WhmcsGateway {
return &WhmcsGateway{
m: &sync.Mutex{},
Expand Down Expand Up @@ -235,7 +239,8 @@ func (g *WhmcsGateway) UpdateInvoice(ctx context.Context, inv *pb.Invoice) error
body.Status = nil
if inv.Status != statusToNoCloud(whmcsInv.Status) {
if inv.Status == pb.BillingStatus_PAID && whmcsInv.Status != statusToWhmcs(pb.BillingStatus_PAID) {
if err = g.PayInvoice(ctx, int(id.GetNumberValue())); err != nil {
paidWithBalance, _ := ctx.Value("paid-with-balance").(bool)
if err = g.PayInvoice(ctx, int(id.GetNumberValue()), paidWithBalance); err != nil {
return fmt.Errorf("failed to pay invoice: %w", err)
}
}
Expand Down Expand Up @@ -300,7 +305,7 @@ func (g *WhmcsGateway) UpdateInvoice(ctx context.Context, inv *pb.Invoice) error
return nil
}

func (g *WhmcsGateway) PayInvoice(ctx context.Context, whmcsInvoiceId int) error {
func (g *WhmcsGateway) PayInvoice(ctx context.Context, whmcsInvoiceId int, payWithBalance ...bool) error {
reqUrl, err := url.Parse(g.baseUrl)
if err != nil {
return err
Expand All @@ -314,10 +319,29 @@ func (g *WhmcsGateway) PayInvoice(ctx context.Context, whmcsInvoiceId int) error
return nil
}

isPayWithBalance := len(payWithBalance) > 0 && payWithBalance[0]

if isPayWithBalance {
// Update invoice payment method first, then pay it
updBody := g.buildUpdateInvoiceQueryBase(whmcsInvoiceId)
updBody.PaymentMethod = &balancePayMethod
q, err := query.Values(updBody)
if err != nil {
return err
}
_, err = sendRequestToWhmcs[InvoiceResponse](http.MethodPost, reqUrl.String()+"?"+q.Encode(), nil)
if err != nil {
return err
}
}

body := g.buildAddPaymentQueryBase(whmcsInvoiceId)
body.TransId = uuid.New().String()
body.Date = time.Now().Format("2006-01-02 15:04:05")
body.Gateway = "balancepay"
body.Gateway = "system"
if isPayWithBalance {
body.Gateway = balancePayMethod
}
if inv.Balance <= 0 {
body.Amount = ptr(inv.Total)
}
Expand Down
Loading