From 50119d112a4152410618d36a3f042de988d083de Mon Sep 17 00:00:00 2001 From: nnn-gif Date: Fri, 4 Oct 2024 11:55:55 +0530 Subject: [PATCH] retun loop payment status --- cmd/http/oraclebuilder/account.go | 7 ++- cmd/http/oraclebuilder/api.go | 43 +++++++++++++-- pkg/model/looppayment.go | 88 ++++++++++++++++++++++++++++--- pkg/model/relDB.go | 4 +- 4 files changed, 125 insertions(+), 17 deletions(-) diff --git a/cmd/http/oraclebuilder/account.go b/cmd/http/oraclebuilder/account.go index 3cfec34fe..8d77ad8b1 100644 --- a/cmd/http/oraclebuilder/account.go +++ b/cmd/http/oraclebuilder/account.go @@ -44,14 +44,14 @@ func (ob *Env) viewAccount(context *gin.Context, publicKey string) (combined map customer, err := ob.RelDB.GetCustomerByPublicKey(publicKey) if err != nil { - log.Errorf("Request ID: %s, ViewAccount err %v ", requestId, err) + log.Errorf("Request ID: %s, ViewAccount err GetCustomerByPublicKey %v ", requestId, err) context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } totalFeeds, totalOracles, err := ob.billableResource(strconv.Itoa(customer.CustomerID)) if err != nil { - log.Errorf("Request ID: %s, ViewAccount err %v ", requestId, err) + log.Errorf("Request ID: %s, ViewAccount err billableResource %v ", requestId, err) context.JSON(http.StatusInternalServerError, gin.H{"error": "error while getting totalFeedusedByCustomer"}) return } @@ -238,6 +238,9 @@ func (ob *Env) CreateAccount(context *gin.Context) { context.JSON(http.StatusOK, gin.H{"message": "Customer created successfully"}) return + } else if err != nil { + log.Infof("Request ID: %s, CreateCustomer with wallet key %s err %v ", requestId, signer, err) + } if customer != nil { diff --git a/cmd/http/oraclebuilder/api.go b/cmd/http/oraclebuilder/api.go index d0efaeacf..d28a25617 100644 --- a/cmd/http/oraclebuilder/api.go +++ b/cmd/http/oraclebuilder/api.go @@ -403,8 +403,10 @@ func (ob *Env) ListAndViewAccount(context *gin.Context) { requestId := context.GetString(REQUEST_ID) var ( - oracles []dia.OracleConfig - account map[string]interface{} + oracles []dia.OracleConfig + account map[string]interface{} + loopEvent *models.LoopPaymentResponse + paidStatus *models.LoopPaymentTransferProcessed ) // get customer id, get oracles by customer @@ -438,9 +440,27 @@ func (ob *Env) ListAndViewAccount(context *gin.Context) { account = ob.viewAccount(context, creator) + loopEvent, err = ob.RelDB.GetLoopPaymentResponseByCustomerID(context, strconv.Itoa(customerId)) + if err != nil { + log.Errorf("Error getting loop status %v", err) + } else { + + paidStatus, err = ob.RelDB.GetLastPaymentByAgreementID(loopEvent.AgreementID) + if err != nil { + log.Errorf("Error getting loop status by agreementid %v", err) + } + + } + + } + + payment := map[string]interface{}{ + "AgreementSignedUp": loopEvent, + "TransferProcessed": paidStatus, } r := map[string]interface{}{ + "payment": payment, "owner": creator, "account": account, "oracles": oracles, @@ -551,6 +571,11 @@ type SymbolFeed struct { FeedSelection []FeedSelection `json:"FeedSelection"` } +type Adaptor struct { + LiquidityThreshold string `json:"Address"` + Symbol string `json:"Symbol"` +} + func generateFeedSelectionQuery(feedSelections []FeedSelection) string { var jsonObjects []string @@ -1364,9 +1389,9 @@ func (ob *Env) LoopWebHook(context *gin.Context) { } l := verifyWebhook(body, ob.LoopPaymentSecret, signature) - + // l = true if l { - log.Errorf("Request ID: %s,LoopWebHook verified ,", requestId) + log.Infoln("Request ID: %s,LoopWebHook verified ,", requestId) } else { log.Errorf("Request ID: %s,LoopWebHook not verified ,", requestId) @@ -1374,6 +1399,8 @@ func (ob *Env) LoopWebHook(context *gin.Context) { } + log.Errorf("Request ID: %s,LoopWebHook %v ,", requestId, ldr) + switch ldr.Event { case "AgreementSignedUp": { @@ -1382,6 +1409,7 @@ func (ob *Env) LoopWebHook(context *gin.Context) { // 2) update plan // 3) update email // + log.Infoln("Request ID: %s,LoopWebHook AgreementSignedUp start ,", requestId) customer, err := ob.RelDB.GetCustomerByPublicKey(common.HexToAddress(ldr.Subscriber).String()) if err != nil { @@ -1415,6 +1443,8 @@ func (ob *Env) LoopWebHook(context *gin.Context) { log.Infof("Request ID: %s,AgreementSignedUp GetCustomerByPublicKey %v ,", requestId, customer) log.Infof("Request ID: %s,ldr %v ,", requestId, ldr) + ldr.CustomerID = strconv.Itoa(customer.CustomerID) + ldr.Subscriber = common.HexToAddress(ldr.Subscriber).String() err = ob.RelDB.InsertLoopPaymentResponse(context, ldr) @@ -1446,6 +1476,8 @@ func (ob *Env) LoopWebHook(context *gin.Context) { case "TransferProcessed": { + log.Infof("Request ID: %s,TransferProcessed started ldr %v ,", requestId, ldr) + var lptp models.LoopPaymentTransferProcessed err = json.Unmarshal(body, &lptp) @@ -1474,7 +1506,8 @@ func (ob *Env) LoopWebHook(context *gin.Context) { lpr, err := ob.RelDB.GetLoopPaymentResponseByAgreementID(context, ldr.AgreementID) if err != nil { - log.Errorf("Request ID: %s, GetLoopPaymentResponseByAgreementID %v ,", requestId, err) + log.Errorf("Request ID: %s,TransferProcessed GetLoopPaymentResponseByAgreementID %v ,", requestId, err) + return } log.Infof("Request ID: %s,lpr %v ", requestId, lpr) log.Infof("Request ID: %s,lpr ItemID %v ", requestId, lpr.Item) diff --git a/pkg/model/looppayment.go b/pkg/model/looppayment.go index 61c828471..394470c9a 100644 --- a/pkg/model/looppayment.go +++ b/pkg/model/looppayment.go @@ -3,6 +3,7 @@ package models import ( "context" "encoding/json" + "strconv" ) // { @@ -93,6 +94,29 @@ func (reldb *RelDB) GetLastPaymentByEndUser(endUser string) (LoopPaymentTransfer return record, nil } +func (reldb *RelDB) GetLastPaymentByAgreementID(agreementID string) (*LoopPaymentTransferProcessed, error) { + query := ` + SELECT event, transaction, network_id, network_name, contract_address, email, company, parent, transfer_id, success, + payment_token_address, payment_token_symbol, end_user, reason, invoice_id, amount_paid, agreement_id, ref_id, batch_id, usd_amount + FROM loop_payment_transfer_processed + WHERE agreement_id = $1 + ORDER BY transaction DESC + LIMIT 1` + + var record LoopPaymentTransferProcessed + err := reldb.postgresClient.QueryRow(context.Background(), query, agreementID).Scan( + &record.Event, &record.Transaction, &record.NetworkID, &record.NetworkName, &record.ContractAddress, + &record.Email, &record.Company, &record.Parent, &record.TransferID, &record.Success, + &record.PaymentTokenAddress, &record.PaymentTokenSymbol, &record.EndUser, &record.Reason, + &record.InvoiceID, &record.AmountPaid, &record.AgreementID, &record.RefID, &record.BatchID, &record.UsdAmount) + + if err != nil { + return nil, err + } + + return &record, nil +} + type LoopPaymentTransferCreated struct { Event string `json:"event"` Transaction string `json:"transaction"` @@ -172,7 +196,9 @@ type LoopPaymentAgreementSignedUp struct { PaymentTokenAddress string `json:"paymentTokenAddress"` EventDate int `json:"eventDate"` RefID string `json:"refId"` - Metadata struct { + CustomerID string `json:"customerId"` + + Metadata struct { } `json:"metadata"` } @@ -202,6 +228,7 @@ type LoopPaymentResponse struct { EventDate int `json:"eventDate"` RefID string `json:"refId"` InvoiceID string `json:"invoiceId"` + CustomerID string `json:"customerId"` Metadata struct { } `json:"metadata"` @@ -213,10 +240,10 @@ func (reldb *RelDB) InsertLoopPaymentResponse(ctx context.Context, response Loop event, transaction, network_id, network_name, contract_address, email, company, parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, - payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata + payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id ) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, - $20, $21, $22, $23, $24, $25 + $20, $21, $22, $23, $24, $25, $26 ) ` _, err := reldb.postgresClient.Exec(ctx, query, @@ -226,7 +253,7 @@ func (reldb *RelDB) InsertLoopPaymentResponse(ctx context.Context, response Loop response.AgreementAmount, response.FrequencyNumber, response.FrequencyUnit, response.AddOnAgreements, response.AddOnItems, response.AddOnItemIds, response.AddOnTotalAmount, response.PaymentTokenSymbol, response.PaymentTokenAddress, - response.EventDate, response.RefID, response.InvoiceID, response.Metadata, + response.EventDate, response.RefID, response.InvoiceID, response.Metadata, response.CustomerID, ) return err } @@ -236,15 +263,18 @@ func (reldb *RelDB) GetLoopPaymentResponseByAgreementID(ctx context.Context, agr SELECT event, transaction, network_id, network_name, contract_address, email, company, parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, - payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata + payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id FROM loop_payment_responses WHERE agreement_id = $1 ` row := reldb.postgresClient.QueryRow(ctx, query, agreementID) - var response LoopPaymentResponse - var metadataJSON []byte + var ( + response LoopPaymentResponse + metadataJSON []byte + custID int + ) err := row.Scan( &response.Event, &response.Transaction, &response.NetworkID, &response.NetworkName, @@ -253,12 +283,54 @@ func (reldb *RelDB) GetLoopPaymentResponseByAgreementID(ctx context.Context, agr &response.AgreementAmount, &response.FrequencyNumber, &response.FrequencyUnit, &response.AddOnAgreements, &response.AddOnItems, &response.AddOnItemIds, &response.AddOnTotalAmount, &response.PaymentTokenSymbol, &response.PaymentTokenAddress, - &response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON, + &response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON, &custID, ) if err != nil { return nil, err } + response.CustomerID = strconv.Itoa(custID) + + if err := json.Unmarshal(metadataJSON, &response.Metadata); err != nil { + return nil, err + } + + return &response, nil +} + +func (reldb *RelDB) GetLoopPaymentResponseByCustomerID(ctx context.Context, customerID string) (*LoopPaymentResponse, error) { + query := ` + SELECT event, transaction, network_id, network_name, contract_address, email, company, + parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, + frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, + payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id + FROM loop_payment_responses + WHERE customer_id = $1 ORDER BY event_date DESC LIMIT 1 + ` + + row := reldb.postgresClient.QueryRow(ctx, query, customerID) + + var ( + response LoopPaymentResponse + metadataJSON []byte + custID int + ) + + err := row.Scan( + &response.Event, &response.Transaction, &response.NetworkID, &response.NetworkName, + &response.ContractAddress, &response.Email, &response.Company, &response.Parent, + &response.Subscriber, &response.Item, &response.ItemID, &response.AgreementID, + &response.AgreementAmount, &response.FrequencyNumber, &response.FrequencyUnit, + &response.AddOnAgreements, &response.AddOnItems, &response.AddOnItemIds, + &response.AddOnTotalAmount, &response.PaymentTokenSymbol, &response.PaymentTokenAddress, + &response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON, &custID, + ) + if err != nil { + return nil, err + } + + response.CustomerID = strconv.Itoa(custID) + if err := json.Unmarshal(metadataJSON, &response.Metadata); err != nil { return nil, err } diff --git a/pkg/model/relDB.go b/pkg/model/relDB.go index c9556afed..8f5b039ca 100644 --- a/pkg/model/relDB.go +++ b/pkg/model/relDB.go @@ -215,11 +215,11 @@ type RelDatastore interface { GetLastPaymentByEndUser(endUser string) (LoopPaymentTransferProcessed, error) GetPlan(ctx context.Context, planID int) (*Plan, error) - + GetLastPaymentByAgreementID(agreementID string) (*LoopPaymentTransferProcessed, error) InsertLoopPaymentTransferProcessed(ctx context.Context, record LoopPaymentTransferProcessed) error InsertLoopPaymentResponse(ctx context.Context, response LoopPaymentResponse) error GetLoopPaymentResponseByAgreementID(ctx context.Context, agreementID string) (*LoopPaymentResponse, error) - + GetLoopPaymentResponseByCustomerID(ctx context.Context, customerID string) (*LoopPaymentResponse, error) ChangeEcosystemConfig(oracleAddress string, enable bool) (err error) }