github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/service/payable_resource.go (about) 1 package service 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 8 "github.com/companieshouse/chs.go/log" 9 "github.com/companieshouse/lfp-pay-api-core/models" 10 "github.com/companieshouse/lfp-pay-api-core/validators" 11 "github.com/companieshouse/lfp-pay-api/config" 12 "github.com/companieshouse/lfp-pay-api/dao" 13 "github.com/companieshouse/lfp-pay-api/e5" 14 "github.com/companieshouse/lfp-pay-api/transformers" 15 ) 16 17 var ( 18 // ErrPaymentNotFulfilled represents the scenario that the payment resource itself is not paid 19 ErrPaymentNotFulfilled = errors.New("the resource you are trying to pay for has not been paid") 20 // ErrAlreadyPaid represents when the LFP payable resource is already paid 21 ErrAlreadyPaid = errors.New("the LFP has already been paid") 22 // ErrLFPNotFound represents when the payable resource does not exist in the db 23 ErrLFPNotFound = errors.New("the LFP does not exist") 24 // ErrPayment represents an error when the payable resource amount does not match the amount in the payment resource 25 ErrPayment = errors.New("there was a problem validating the payment") 26 ) 27 28 // PayableResourceService contains the DAO for db access 29 type PayableResourceService struct { 30 DAO dao.Service 31 Config *config.Config 32 } 33 34 // GetPayableResource retrieves the payable resource with the given company number and reference from the database 35 func (s *PayableResourceService) GetPayableResource(req *http.Request, companyNumber string, reference string) (*models.PayableResource, ResponseType, error) { 36 payable, err := s.DAO.GetPayableResource(companyNumber, reference) 37 if err != nil { 38 err = fmt.Errorf("error getting payable resource from db: [%v]", err) 39 log.ErrorR(req, err) 40 return nil, Error, err 41 } 42 if payable == nil { 43 log.TraceR(req, "payable resource not found", log.Data{"company_number": companyNumber, "reference": reference}) 44 return nil, NotFound, nil 45 } 46 47 payableRest := transformers.PayableResourceDBToRequest(payable) 48 49 return payableRest, Success, nil 50 } 51 52 // UpdateAsPaid will update the resource as paid and persist the changes in the database 53 func (s *PayableResourceService) UpdateAsPaid(resource models.PayableResource, payment validators.PaymentInformation) error { 54 model, err := s.DAO.GetPayableResource(resource.CompanyNumber, resource.Reference) 55 if err != nil { 56 err = fmt.Errorf("error getting payable resource from db: [%v]", err) 57 log.Error(err, log.Data{ 58 "lfp_reference": resource.Reference, 59 "company_number": resource.CompanyNumber, 60 }) 61 return ErrLFPNotFound 62 } 63 64 // check if this resource has already been paid 65 if model.IsPaid() { 66 err = errors.New("this LFP has already been paid") 67 log.Error(err, log.Data{ 68 "lfp_reference": model.Reference, 69 "company_number": model.CompanyNumber, 70 "payment_id": model.Data.Payment.Reference, 71 }) 72 return ErrAlreadyPaid 73 } 74 75 model.Data.Payment.Reference = payment.Reference 76 model.Data.Payment.Status = payment.Status 77 model.Data.Payment.PaidAt = &payment.CompletedAt 78 model.Data.Payment.Amount = payment.Amount 79 80 return s.DAO.UpdatePaymentDetails(model) 81 } 82 83 // RecordE5CommandError will mark the resource as having failed to update E5. 84 func (s *PayableResourceService) RecordE5CommandError(resource models.PayableResource, action e5.Action) error { 85 return s.DAO.SaveE5Error(resource.CompanyNumber, resource.Reference, action) 86 }