github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/errors/DeclinedPaymentError.go (about)

     1  package errors
     2  
     3  import (
     4  	goerr "errors"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/Ingenico-ePayments/connect-sdk-go/domain/errors"
     9  	"github.com/Ingenico-ePayments/connect-sdk-go/domain/payment"
    10  )
    11  
    12  // DeclinedPaymentError represents an error response from a create payment call
    13  type DeclinedPaymentError struct {
    14  	errorMessage  string
    15  	statusCode    int
    16  	responseBody  string
    17  	errorResponse *payment.ErrorResponse
    18  }
    19  
    20  // Message returns the error message
    21  func (dpe *DeclinedPaymentError) Message() string {
    22  	return dpe.errorMessage
    23  }
    24  
    25  // StatusCode returns the status code
    26  func (dpe *DeclinedPaymentError) StatusCode() int {
    27  	return dpe.statusCode
    28  }
    29  
    30  // ResponseBody returns the response body
    31  func (dpe *DeclinedPaymentError) ResponseBody() string {
    32  	return dpe.responseBody
    33  }
    34  
    35  // ErrorID returns the error id
    36  func (dpe *DeclinedPaymentError) ErrorID() string {
    37  	if dpe.errorResponse.ErrorID == nil {
    38  		return ""
    39  	}
    40  	return *dpe.errorResponse.ErrorID
    41  }
    42  
    43  // Errors returns a slice of underlying errors
    44  func (dpe *DeclinedPaymentError) Errors() []errors.APIError {
    45  	// Return a clone instead of the original slice - immutability insurance
    46  	if dpe.errorResponse.Errors == nil {
    47  		return []errors.APIError{}
    48  	}
    49  	return append([]errors.APIError{}, *dpe.errorResponse.Errors...)
    50  }
    51  
    52  // PaymentResult returns the payment creation result
    53  func (dpe *DeclinedPaymentError) PaymentResult() *payment.CreateResult {
    54  	return dpe.errorResponse.PaymentResult
    55  }
    56  
    57  // String implements the Stringer interface
    58  // Format: 'errorMessage; statusCode=; responseBody='
    59  func (dpe *DeclinedPaymentError) String() string {
    60  	list := dpe.errorMessage
    61  
    62  	if dpe.statusCode > 0 {
    63  		list = list + "; statusCode=" + strconv.Itoa(dpe.statusCode)
    64  	}
    65  	if len(dpe.responseBody) != 0 {
    66  		list = list + "; responseBody='" + dpe.responseBody + "'"
    67  	}
    68  
    69  	return list
    70  }
    71  
    72  // Error implements the Error interface
    73  func (dpe *DeclinedPaymentError) Error() string {
    74  	return dpe.String()
    75  }
    76  
    77  // NewDeclinedPaymentError creates a DeclinedTransactionError with the given statusCode, responseBody and errorResponse
    78  func NewDeclinedPaymentError(statusCode int, responseBody string, errorResponse *payment.ErrorResponse) (*DeclinedPaymentError, error) {
    79  	if errorResponse.PaymentResult.Payment.ID == nil || errorResponse.PaymentResult.Payment.StatusOutput == nil {
    80  		return nil, goerr.New("Cannot get payment")
    81  	}
    82  	errorMessage := fmt.Sprintf("declined payment '%s' with status '%s'",
    83  		*errorResponse.PaymentResult.Payment.ID,
    84  		*errorResponse.PaymentResult.Payment.Status)
    85  
    86  	return &DeclinedPaymentError{errorMessage, statusCode, responseBody, errorResponse}, nil
    87  }