Last active
March 2, 2023 13:45
-
-
Save opensaucerer/f74f8d1f633e0daf7a647d663919734a to your computer and use it in GitHub Desktop.
Making a request with Goaxios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
func (order Order) StoreTransactions() (*PaymentResponse, error) {}
That's correct...nil is wrong...
But don't use a pointer... instead, return PaymentResponse{}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thinking this should be pointer ... it cant return a nil as return value for the payment response