Sending invoice from the BE
This commit is contained in:
		@ -1,6 +1,8 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"myapp/internal/cards"
 | 
			
		||||
	"myapp/internal/cards/encryption"
 | 
			
		||||
@ -8,6 +10,7 @@ import (
 | 
			
		||||
	"myapp/internal/urlsigner"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-chi/chi/v5"
 | 
			
		||||
)
 | 
			
		||||
@ -95,6 +98,17 @@ func (app *application) GetTransactionData(r *http.Request) (TransactionData, er
 | 
			
		||||
	return txnData, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Invoice struct {
 | 
			
		||||
	ID        int       `json:"id"`
 | 
			
		||||
	Quantity  int       `json:"quantity"`
 | 
			
		||||
	Amount    int       `json:"amount"`
 | 
			
		||||
	Product   string    `json:"product"`
 | 
			
		||||
	FirstName string    `json:"first_name"`
 | 
			
		||||
	LastName  string    `json:"last_name"`
 | 
			
		||||
	Email     string    `json:"email"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	// read posted data
 | 
			
		||||
	err := r.ParseForm()
 | 
			
		||||
@ -150,6 +164,24 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
 | 
			
		||||
		app.errorLog.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// call microservice
 | 
			
		||||
	inv := Invoice{
 | 
			
		||||
		ID:        orderID,
 | 
			
		||||
		Quantity:  order.Quantity,
 | 
			
		||||
		Amount:    order.Amount,
 | 
			
		||||
		Product:   "Widget",
 | 
			
		||||
		FirstName: txnData.FirstName,
 | 
			
		||||
		LastName:  txnData.LastName,
 | 
			
		||||
		Email:     txnData.Email,
 | 
			
		||||
		CreatedAt: time.Now(),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = app.callInvoiceMicro(inv)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		app.errorLog.Println(err)
 | 
			
		||||
		// Don't stop the program
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	app.infoLog.Printf("order id: %d", orderID)
 | 
			
		||||
 | 
			
		||||
	app.Session.Put(r.Context(), "txn", txnData)
 | 
			
		||||
@ -157,6 +189,30 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request)
 | 
			
		||||
	http.Redirect(w, r, "/receipt", http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) callInvoiceMicro(inv Invoice) error {
 | 
			
		||||
	// TODO: Do not hard code this.
 | 
			
		||||
	url := "http://localhost:5000/invoice/create-and-send"
 | 
			
		||||
	out, err := json.MarshalIndent(inv, "", "\t")
 | 
			
		||||
	if err != err {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(out))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	req.Header.Set("Content-Type", "application/json")
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	app.infoLog.Println(resp.Body)
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *application) VirtualTerminalPaymentSucceeded(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	txnData, err := app.GetTransactionData(r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user