Skip to content

Commit

Permalink
retun loop payment status
Browse files Browse the repository at this point in the history
  • Loading branch information
nnn-gif committed Oct 4, 2024
1 parent 7a93437 commit 50119d1
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 17 deletions.
7 changes: 5 additions & 2 deletions cmd/http/oraclebuilder/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 38 additions & 5 deletions cmd/http/oraclebuilder/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1364,16 +1389,18 @@ 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)

return

}

log.Errorf("Request ID: %s,LoopWebHook %v ,", requestId, ldr)

switch ldr.Event {
case "AgreementSignedUp":
{
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
88 changes: 80 additions & 8 deletions pkg/model/looppayment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"context"
"encoding/json"
"strconv"
)

// {
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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"`
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/relDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 50119d1

Please sign in to comment.