github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/handlers/payment_details.go (about)

     1  package handlers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/companieshouse/chs.go/log"
     8  	"github.com/companieshouse/lfp-pay-api-core/models"
     9  	"github.com/companieshouse/lfp-pay-api/config"
    10  	"github.com/companieshouse/lfp-pay-api/service"
    11  	"github.com/companieshouse/lfp-pay-api/utils"
    12  )
    13  
    14  // HandleGetPaymentDetails retrieves costs for a supplied company number and reference.
    15  func HandleGetPaymentDetails(w http.ResponseWriter, req *http.Request) {
    16  
    17  	// get payable resource from context, put there by PayableResourceAuthenticationInterceptor
    18  	payableResource, ok := req.Context().Value(config.PayableResource).(*models.PayableResource)
    19  
    20  	if !ok {
    21  		log.ErrorR(req, fmt.Errorf("invalid PayableResource in request context"))
    22  		m := models.NewMessageResponse("the payable resource is not present in the request context")
    23  		utils.WriteJSONWithStatus(w, req, m, http.StatusInternalServerError)
    24  		return
    25  	}
    26  
    27  	// Get the payment details from the payable resource
    28  	paymentDetails, responseType, err := paymentDetailsService.GetPaymentDetailsFromPayableResource(req, payableResource)
    29  	logData := log.Data{"company_number": payableResource.CompanyNumber, "reference": payableResource.Reference}
    30  	if err != nil {
    31  		switch responseType {
    32  		case service.InvalidData:
    33  			log.DebugR(req, fmt.Sprintf("invalid data getting payment details from payable resource so returning not found [%s]", err.Error()), logData)
    34  			m := models.NewMessageResponse("payable resource does not exist or has insufficient data")
    35  			utils.WriteJSONWithStatus(w, req, m, http.StatusNotFound)
    36  			return
    37  		default:
    38  			log.ErrorR(req, fmt.Errorf("error when getting payment details from PayableResource: [%v]", err), logData)
    39  			m := models.NewMessageResponse("payable resource does not exist or has insufficient data")
    40  			utils.WriteJSONWithStatus(w, req, m, http.StatusInternalServerError)
    41  			return
    42  		}
    43  	}
    44  
    45  	utils.WriteJSON(w, req, paymentDetails)
    46  
    47  	log.InfoR(req, "Successful GET request for payment details", logData)
    48  
    49  }