github.com/diadata-org/diadata@v1.4.593/pkg/model/looppayment.go (about) 1 package models 2 3 import ( 4 "context" 5 "encoding/json" 6 "strconv" 7 ) 8 9 // { 10 // "event": "TransferProcessed", 11 // "transaction": "0x4e5e20a1cfca858b1def7ad70b9a286d046b084b47970b1850063b2ea86e8405", 12 // "networkId": 11155111, 13 // "networkName": "sepolia", 14 // "contractAddress": "0xb436D38bC878E5a202Da9e609a549249D178f7fE", 15 // "email": "a@s.com", 16 // "company": "DIA Data (085b1ed6-c637-4f99-a034-3ea718bcce34)", 17 // "parent": "-", 18 // "transferId": "252fdc42-0935-4a89-bda2-7618f6dfcc40", 19 // "success": true, 20 // "paymentTokenAddress": "0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9", 21 // "paymentTokenSymbol": "WETH", 22 // "endUser": "0xF231DB04c5d92396235506232Ca5F40fcf8dAfb2", 23 // "reason": "", 24 // "invoiceId": "DIADA-13", 25 // "amountPaid": 0.00029615, 26 // "agreementId": "7f75d1f9-0c6c-4b71-8892-0ab2aaab07c1", 27 // "refId": "", 28 // "batchId": "ded7256e-4722-4706-b768-4da06fd930f8", 29 // "usdAmount": "1.00" 30 // } 31 32 type LoopPaymentTransferProcessed struct { 33 Event string `json:"event"` 34 Transaction string `json:"transaction"` 35 NetworkID int `json:"networkId"` 36 NetworkName string `json:"networkName"` 37 ContractAddress string `json:"contractAddress"` 38 Email string `json:"email"` 39 Company string `json:"company"` 40 Parent string `json:"parent"` 41 TransferID string `json:"transferId"` 42 Success bool `json:"success"` 43 PaymentTokenAddress string `json:"paymentTokenAddress"` 44 PaymentTokenSymbol string `json:"paymentTokenSymbol"` 45 EndUser string `json:"endUser"` 46 Reason string `json:"reason"` 47 InvoiceID string `json:"invoiceId"` 48 AmountPaid float64 `json:"amountPaid"` 49 AgreementID string `json:"agreementId"` 50 RefID string `json:"refId"` 51 BatchID string `json:"batchId"` 52 UsdAmount string `json:"usdAmount"` 53 RedirectUrl string `json:"redirectURL"` 54 } 55 56 func (reldb *RelDB) InsertLoopPaymentTransferProcessed(ctx context.Context, record LoopPaymentTransferProcessed) error { 57 query := ` 58 INSERT INTO loop_payment_transfer_processed ( 59 event, transaction, network_id, network_name, contract_address, email, company, parent, transfer_id, success, 60 payment_token_address, payment_token_symbol, end_user, reason, invoice_id, amount_paid, agreement_id, ref_id, batch_id, usd_amount 61 ) VALUES ( 62 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20 63 )` 64 65 _, err := reldb.postgresClient.Exec(ctx, query, 66 record.Event, record.Transaction, record.NetworkID, record.NetworkName, record.ContractAddress, 67 record.Email, record.Company, record.Parent, record.TransferID, record.Success, 68 record.PaymentTokenAddress, record.PaymentTokenSymbol, record.EndUser, record.Reason, 69 record.InvoiceID, record.AmountPaid, record.AgreementID, record.RefID, record.BatchID, record.UsdAmount) 70 71 return err 72 } 73 74 func (reldb *RelDB) GetLastPaymentByEndUser(endUser string) (LoopPaymentTransferProcessed, error) { 75 query := ` 76 SELECT event, transaction, network_id, network_name, contract_address, email, company, parent, transfer_id, success, 77 payment_token_address, payment_token_symbol, end_user, reason, invoice_id, amount_paid, agreement_id, ref_id, batch_id, usd_amount 78 FROM loop_payment_transfer_processed 79 WHERE end_user = $1 80 ORDER BY transaction DESC 81 LIMIT 1` 82 83 var record LoopPaymentTransferProcessed 84 err := reldb.postgresClient.QueryRow(context.Background(), query, endUser).Scan( 85 &record.Event, &record.Transaction, &record.NetworkID, &record.NetworkName, &record.ContractAddress, 86 &record.Email, &record.Company, &record.Parent, &record.TransferID, &record.Success, 87 &record.PaymentTokenAddress, &record.PaymentTokenSymbol, &record.EndUser, &record.Reason, 88 &record.InvoiceID, &record.AmountPaid, &record.AgreementID, &record.RefID, &record.BatchID, &record.UsdAmount) 89 90 if err != nil { 91 return record, err 92 } 93 94 return record, nil 95 } 96 97 func (reldb *RelDB) GetLastPaymentByAgreementID(agreementID string) (*LoopPaymentTransferProcessed, error) { 98 query := ` 99 SELECT event, transaction, network_id, network_name, contract_address, email, company, parent, transfer_id, success, 100 payment_token_address, payment_token_symbol, end_user, reason, invoice_id, amount_paid, agreement_id, ref_id, batch_id, usd_amount 101 FROM loop_payment_transfer_processed 102 WHERE agreement_id = $1 103 ORDER BY transaction DESC 104 LIMIT 1` 105 106 var record LoopPaymentTransferProcessed 107 err := reldb.postgresClient.QueryRow(context.Background(), query, agreementID).Scan( 108 &record.Event, &record.Transaction, &record.NetworkID, &record.NetworkName, &record.ContractAddress, 109 &record.Email, &record.Company, &record.Parent, &record.TransferID, &record.Success, 110 &record.PaymentTokenAddress, &record.PaymentTokenSymbol, &record.EndUser, &record.Reason, 111 &record.InvoiceID, &record.AmountPaid, &record.AgreementID, &record.RefID, &record.BatchID, &record.UsdAmount) 112 113 if err != nil { 114 return nil, err 115 } 116 117 return &record, nil 118 } 119 120 type LoopPaymentTransferCreated struct { 121 Event string `json:"event"` 122 Transaction string `json:"transaction"` 123 NetworkID int `json:"networkId"` 124 NetworkName string `json:"networkName"` 125 ContractAddress string `json:"contractAddress"` 126 Email string `json:"email"` 127 Company string `json:"company"` 128 Parent string `json:"parent"` 129 ID string `json:"id"` 130 InvoiceID string `json:"invoiceId"` 131 BillDate int `json:"billDate"` 132 ToAddress string `json:"toAddress"` 133 FromAddress string `json:"fromAddress"` 134 TokenSymbol string `json:"tokenSymbol"` 135 TokenAddress string `json:"tokenAddress"` 136 PaymentType string `json:"paymentType"` 137 Usd bool `json:"usd"` 138 Amount string `json:"amount"` 139 Item string `json:"item"` 140 ItemID int `json:"itemId"` 141 Source string `json:"source"` 142 BatchID string `json:"batchId"` 143 RefID string `json:"refId"` 144 AgreementID string `json:"agreementId"` 145 TransferID string `json:"transferId"` 146 } 147 148 // { 149 // "event": "AgreementSignedUp", 150 // "transaction": "-", 151 // "networkId": 11155111, 152 // "networkName": "sepolia", 153 // "contractAddress": "0xb436d38bc878e5a202da9e609a549249d178f7fe", 154 // "email": "a@s.com", 155 // "company": "DIA Data (085b1ed6-c637-4f99-a034-3ea718bcce34)", 156 // "parent": "-", 157 // "subscriber": "0xf231db04c5d92396235506232ca5f40fcf8dafb2", 158 // "item": "Product 2", 159 // "itemId": "5597e580-5026-46b6-a0bf-97ae1a88bd0a", 160 // "agreementId": "7f75d1f9-0c6c-4b71-8892-0ab2aaab07c1", 161 // "agreementAmount": "1.00", 162 // "frequencyNumber": 1, 163 // "frequencyUnit": "Day", 164 // "addOnAgreements": "", 165 // "addOnItems": "", 166 // "addOnItemIds": "", 167 // "addOnTotalAmount": "0.00", 168 // "paymentTokenSymbol": "WETH", 169 // "paymentTokenAddress": "0x7b79995e5f793a07bc00c21412e50ecae098e7f9", 170 // "eventDate": 1722344804, 171 // "refId": "", 172 // "metadata": {} 173 // } 174 175 type LoopPaymentAgreementSignedUp struct { 176 Event string `json:"event"` 177 Transaction string `json:"transaction"` 178 NetworkID int `json:"networkId"` 179 NetworkName string `json:"networkName"` 180 ContractAddress string `json:"contractAddress"` 181 Email string `json:"email"` 182 Company string `json:"company"` 183 Parent string `json:"parent"` 184 Subscriber string `json:"subscriber"` 185 Item string `json:"item"` 186 ItemID string `json:"itemId"` 187 AgreementID string `json:"agreementId"` 188 AgreementAmount string `json:"agreementAmount"` 189 FrequencyNumber int `json:"frequencyNumber"` 190 FrequencyUnit string `json:"frequencyUnit"` 191 AddOnAgreements string `json:"addOnAgreements"` 192 AddOnItems string `json:"addOnItems"` 193 AddOnItemIds string `json:"addOnItemIds"` 194 AddOnTotalAmount string `json:"addOnTotalAmount"` 195 PaymentTokenSymbol string `json:"paymentTokenSymbol"` 196 PaymentTokenAddress string `json:"paymentTokenAddress"` 197 EventDate int `json:"eventDate"` 198 RefID string `json:"refId"` 199 CustomerID string `json:"customerId"` 200 201 Metadata struct { 202 } `json:"metadata"` 203 } 204 205 type LoopPaymentResponse struct { 206 Event string `json:"event"` 207 208 Transaction string `json:"transaction"` 209 NetworkID int `json:"networkId"` 210 NetworkName string `json:"networkName"` 211 ContractAddress string `json:"contractAddress"` 212 Email string `json:"email"` 213 Company string `json:"company"` 214 Parent string `json:"parent"` 215 Subscriber string `json:"subscriber"` 216 Item string `json:"item"` 217 ItemID string `json:"itemId"` 218 AgreementID string `json:"agreementId"` 219 AgreementAmount string `json:"agreementAmount"` 220 FrequencyNumber int `json:"frequencyNumber"` 221 FrequencyUnit string `json:"frequencyUnit"` 222 AddOnAgreements string `json:"addOnAgreements"` 223 AddOnItems string `json:"addOnItems"` 224 AddOnItemIds string `json:"addOnItemIds"` 225 AddOnTotalAmount string `json:"addOnTotalAmount"` 226 PaymentTokenSymbol string `json:"paymentTokenSymbol"` 227 PaymentTokenAddress string `json:"paymentTokenAddress"` 228 EventDate int `json:"eventDate"` 229 RefID string `json:"refId"` 230 InvoiceID string `json:"invoiceId"` 231 CustomerID string `json:"customerId"` 232 233 Metadata struct { 234 } `json:"metadata"` 235 } 236 237 func (reldb *RelDB) InsertLoopPaymentResponse(ctx context.Context, response LoopPaymentResponse) error { 238 query := ` 239 INSERT INTO loop_payment_responses ( 240 event, transaction, network_id, network_name, contract_address, email, company, 241 parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, 242 frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, 243 payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id 244 ) VALUES ( 245 $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, 246 $20, $21, $22, $23, $24, $25, $26 247 ) 248 ` 249 _, err := reldb.postgresClient.Exec(ctx, query, 250 response.Event, response.Transaction, response.NetworkID, response.NetworkName, 251 response.ContractAddress, response.Email, response.Company, response.Parent, 252 response.Subscriber, response.Item, response.ItemID, response.AgreementID, 253 response.AgreementAmount, response.FrequencyNumber, response.FrequencyUnit, 254 response.AddOnAgreements, response.AddOnItems, response.AddOnItemIds, 255 response.AddOnTotalAmount, response.PaymentTokenSymbol, response.PaymentTokenAddress, 256 response.EventDate, response.RefID, response.InvoiceID, response.Metadata, response.CustomerID, 257 ) 258 return err 259 } 260 261 func (reldb *RelDB) GetLoopPaymentResponseByAgreementID(ctx context.Context, agreementID string) (*LoopPaymentResponse, error) { 262 query := ` 263 SELECT event, transaction, network_id, network_name, contract_address, email, company, 264 parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, 265 frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, 266 payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id 267 FROM loop_payment_responses 268 WHERE agreement_id = $1 269 ` 270 271 row := reldb.postgresClient.QueryRow(ctx, query, agreementID) 272 273 var ( 274 response LoopPaymentResponse 275 metadataJSON []byte 276 custID int 277 ) 278 279 err := row.Scan( 280 &response.Event, &response.Transaction, &response.NetworkID, &response.NetworkName, 281 &response.ContractAddress, &response.Email, &response.Company, &response.Parent, 282 &response.Subscriber, &response.Item, &response.ItemID, &response.AgreementID, 283 &response.AgreementAmount, &response.FrequencyNumber, &response.FrequencyUnit, 284 &response.AddOnAgreements, &response.AddOnItems, &response.AddOnItemIds, 285 &response.AddOnTotalAmount, &response.PaymentTokenSymbol, &response.PaymentTokenAddress, 286 &response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON, &custID, 287 ) 288 if err != nil { 289 return nil, err 290 } 291 292 response.CustomerID = strconv.Itoa(custID) 293 294 if err := json.Unmarshal(metadataJSON, &response.Metadata); err != nil { 295 return nil, err 296 } 297 298 return &response, nil 299 } 300 301 func (reldb *RelDB) GetLoopPaymentResponseByCustomerID(ctx context.Context, customerID string) (*LoopPaymentResponse, error) { 302 query := ` 303 SELECT event, transaction, network_id, network_name, contract_address, email, company, 304 parent, subscriber, item, item_id, agreement_id, agreement_amount, frequency_number, 305 frequency_unit, add_on_agreements, add_on_items, add_on_item_ids, add_on_total_amount, 306 payment_token_symbol, payment_token_address, event_date, ref_id, invoice_id, metadata, customer_id 307 FROM loop_payment_responses 308 WHERE customer_id = $1 ORDER BY event_date DESC LIMIT 1 309 ` 310 311 row := reldb.postgresClient.QueryRow(ctx, query, customerID) 312 313 var ( 314 response LoopPaymentResponse 315 metadataJSON []byte 316 custID int 317 ) 318 319 err := row.Scan( 320 &response.Event, &response.Transaction, &response.NetworkID, &response.NetworkName, 321 &response.ContractAddress, &response.Email, &response.Company, &response.Parent, 322 &response.Subscriber, &response.Item, &response.ItemID, &response.AgreementID, 323 &response.AgreementAmount, &response.FrequencyNumber, &response.FrequencyUnit, 324 &response.AddOnAgreements, &response.AddOnItems, &response.AddOnItemIds, 325 &response.AddOnTotalAmount, &response.PaymentTokenSymbol, &response.PaymentTokenAddress, 326 &response.EventDate, &response.RefID, &response.InvoiceID, &metadataJSON, &custID, 327 ) 328 if err != nil { 329 return nil, err 330 } 331 332 response.CustomerID = strconv.Itoa(custID) 333 334 if err := json.Unmarshal(metadataJSON, &response.Metadata); err != nil { 335 return nil, err 336 } 337 338 return &response, nil 339 }