Skip to content

Instantly share code, notes, and snippets.

@opensaucerer
Last active March 2, 2023 13:45
Show Gist options
  • Save opensaucerer/f74f8d1f633e0daf7a647d663919734a to your computer and use it in GitHub Desktop.
Save opensaucerer/f74f8d1f633e0daf7a647d663919734a to your computer and use it in GitHub Desktop.

Revisions

  1. opensaucerer revised this gist Mar 2, 2023. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,15 @@ import (
    "github.com/samperfect/goaxios"
    )

    func (order Order) StoreTransactions() (interface{}, error) {
    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)
    @@ -31,13 +39,7 @@ func (order Order) StoreTransactions() (interface{}, error) {
    return nil, err
    }

    var paymentResponse struct {
    Data struct {
    Link string `json:"link"`
    } `json:"data"`
    Status string `json:"status"`
    Message string `json:"message"`
    }
    var paymentResponse PaymentResponse

    err = json.Unmarshal(res, &paymentResponse)
    if err != nil {
  2. opensaucerer revised this gist Mar 2, 2023. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ package main

    import (
    "context"
    "encoding/json"
    "strconv"
    "time"

    @@ -30,7 +31,20 @@ func (order Order) StoreTransactions() (interface{}, error) {
    return nil, err
    }

    return res, nil
    var paymentResponse struct {
    Data struct {
    Link string `json:"link"`
    } `json:"data"`
    Status string `json:"status"`
    Message string `json:"message"`
    }

    err = json.Unmarshal(res, &paymentResponse)
    if err != nil {
    return nil, err
    }

    return paymentResponse, nil
    }

    func InitiateCharge() ([]byte, error) {
  3. opensaucerer renamed this gist Mar 2, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. opensaucerer created this gist Mar 2, 2023.
    68 changes: 68 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    package main

    import (
    "context"
    "strconv"
    "time"

    "github.com/jackc/pgx/v5"
    "github.com/samperfect/goaxios"
    )

    func (order Order) StoreTransactions() (interface{}, 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
    }

    return res, 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
    }