package main import ( "context" "encoding/json" "strconv" "time" "github.com/jackc/pgx/v5" "github.com/samperfect/goaxios" ) type PaymentResponse struct { Data struct { Link string `json:"link"` } `json:"data"` Status string `json:"status"` Message string `json:"message"` } func (order Order) StoreTransactions() (PaymentResponse, error) { config, _ := config.LoadConfig(".") var payment Payment query := `INSERT INTO payments(amount, reference,payment_method,created_at) VALUES(@paymentAmount, @paymentReference, @paymentMethod, @paymentCreatedAt) return(id)` trans := pgx.NamedArgs{ "paymentAmount": order.Total, "paymentReference": Random(20), "paymentMethod": "Cash", "paymentCreatedAt": time.Now(), } err := database.DB.QueryRow(context.Background(), query, trans).Scan(&payment.Id) if err != nil { panic("Could Execute this Query") } res, err := InitiateCharge() if err != nil { return nil, err } var paymentResponse PaymentResponse err = json.Unmarshal(res, &paymentResponse) if err != nil { return nil, err } return paymentResponse, nil } func InitiateCharge() ([]byte, error) { body := map[string]interface{}{ "req": "card_payment", "currency": "USD", "fname": order.Customer.FirstName, "lname": order.Customer.LastName, "encryption_key": config.DBENCRYPTION, "amount": strconv.FormatFloat(order.Total, 'E', -1, 64), "emailAddress": order.Customer.Email, "call_back": config.CALLBACKURL, "success_url": config.SUCCESSURL, "failure_url": config.FAILUREURL, // "phone" : order.Customer.Phone, "description": "payments", "txRef": payment.Reference, } // tweet the message r := goaxios.GoAxios{ Url: config.PAYMENTURL, Method: "POST", BearerToken: "Your Flutterwave Secret Key", Body: body, } // send request _, b, _, err := r.RunRest() if err != nil { return nil, err } return b, nil }