github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/merchant/tokens/Client.go (about)

     1  // This class was auto-generated from the API references found at
     2  // https://epayments-api.developer-ingenico.com/
     3  
     4  package tokens
     5  
     6  import (
     7  	"github.com/Ingenico-ePayments/connect-sdk-go/communicator/communication"
     8  	"github.com/Ingenico-ePayments/connect-sdk-go/domain/errors"
     9  	"github.com/Ingenico-ePayments/connect-sdk-go/domain/token"
    10  	sdkErrors "github.com/Ingenico-ePayments/connect-sdk-go/errors"
    11  	"github.com/Ingenico-ePayments/connect-sdk-go/internal/apiresource"
    12  )
    13  
    14  // Client represents a tokens client. Thread-safe.
    15  type Client struct {
    16  	apiResource *apiresource.APIResource
    17  }
    18  
    19  // Create represents the resource /{merchantId}/tokens - Create token
    20  // Documentation can be found at https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/go/tokens/create.html
    21  //
    22  // Can return any of the following errors:
    23  // * ValidationError if the request was not correct and couldn't be processed (HTTP status code 400)
    24  // * AuthorizationError if the request was not allowed (HTTP status code 403)
    25  // * IdempotenceError if an idempotent request caused a conflict (HTTP status code 409)
    26  // * ReferenceError if an object was attempted to be referenced that doesn't exist or has been removed,
    27  // or there was a conflict (HTTP status code 404, 409 or 410)
    28  // * GlobalCollectError if something went wrong at the Ingenico ePayments platform,
    29  // the Ingenico ePayments platform was unable to process a message from a downstream partner/acquirer,
    30  // or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
    31  // * APIError if the Ingenico ePayments platform returned any other error
    32  func (c *Client) Create(body token.CreateRequest, context communication.CallContext) (token.CreateResponse, error) {
    33  	var resultObject token.CreateResponse
    34  
    35  	uri, err := c.apiResource.InstantiateURIWithContext("/v1/{merchantId}/tokens", nil)
    36  	if err != nil {
    37  		return resultObject, err
    38  	}
    39  
    40  	clientHeaders := c.apiResource.ClientHeaders()
    41  
    42  	postErr := c.apiResource.Communicator().Post(uri, clientHeaders, nil, body, context, &resultObject)
    43  	if postErr != nil {
    44  		responseError, isResponseError := postErr.(*sdkErrors.ResponseError)
    45  		if isResponseError {
    46  			var errorObject interface{}
    47  
    48  			errorObject = &errors.ErrorResponse{}
    49  			err = c.apiResource.Communicator().Marshaller().Unmarshal(responseError.Body(), errorObject)
    50  			if err != nil {
    51  				return resultObject, err
    52  			}
    53  
    54  			err, createErr := sdkErrors.CreateAPIError(responseError.StatusCode(), responseError.Body(), errorObject, context)
    55  			if createErr != nil {
    56  				return resultObject, createErr
    57  			}
    58  
    59  			return resultObject, err
    60  		}
    61  
    62  		return resultObject, postErr
    63  	}
    64  
    65  	return resultObject, nil
    66  }
    67  
    68  // Get represents the resource /{merchantId}/tokens/{tokenId} - Get token
    69  // Documentation can be found at https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/go/tokens/get.html
    70  //
    71  // Can return any of the following errors:
    72  // * ValidationError if the request was not correct and couldn't be processed (HTTP status code 400)
    73  // * AuthorizationError if the request was not allowed (HTTP status code 403)
    74  // * IdempotenceError if an idempotent request caused a conflict (HTTP status code 409)
    75  // * ReferenceError if an object was attempted to be referenced that doesn't exist or has been removed,
    76  // or there was a conflict (HTTP status code 404, 409 or 410)
    77  // * GlobalCollectError if something went wrong at the Ingenico ePayments platform,
    78  // the Ingenico ePayments platform was unable to process a message from a downstream partner/acquirer,
    79  // or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
    80  // * APIError if the Ingenico ePayments platform returned any other error
    81  func (c *Client) Get(tokenID string, context communication.CallContext) (token.Response, error) {
    82  	var resultObject token.Response
    83  
    84  	pathContext := map[string]string{
    85  		"tokenId": tokenID,
    86  	}
    87  
    88  	uri, err := c.apiResource.InstantiateURIWithContext("/v1/{merchantId}/tokens/{tokenId}", pathContext)
    89  	if err != nil {
    90  		return resultObject, err
    91  	}
    92  
    93  	clientHeaders := c.apiResource.ClientHeaders()
    94  
    95  	getErr := c.apiResource.Communicator().Get(uri, clientHeaders, nil, context, &resultObject)
    96  	if getErr != nil {
    97  		responseError, isResponseError := getErr.(*sdkErrors.ResponseError)
    98  		if isResponseError {
    99  			var errorObject interface{}
   100  
   101  			errorObject = &errors.ErrorResponse{}
   102  			err = c.apiResource.Communicator().Marshaller().Unmarshal(responseError.Body(), errorObject)
   103  			if err != nil {
   104  				return resultObject, err
   105  			}
   106  
   107  			err, createErr := sdkErrors.CreateAPIError(responseError.StatusCode(), responseError.Body(), errorObject, context)
   108  			if createErr != nil {
   109  				return resultObject, createErr
   110  			}
   111  
   112  			return resultObject, err
   113  		}
   114  
   115  		return resultObject, getErr
   116  	}
   117  
   118  	return resultObject, nil
   119  }
   120  
   121  // Update represents the resource /{merchantId}/tokens/{tokenId} - Update token
   122  // Documentation can be found at https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/go/tokens/update.html
   123  //
   124  // Can return any of the following errors:
   125  // * ValidationError if the request was not correct and couldn't be processed (HTTP status code 400)
   126  // * AuthorizationError if the request was not allowed (HTTP status code 403)
   127  // * IdempotenceError if an idempotent request caused a conflict (HTTP status code 409)
   128  // * ReferenceError if an object was attempted to be referenced that doesn't exist or has been removed,
   129  // or there was a conflict (HTTP status code 404, 409 or 410)
   130  // * GlobalCollectError if something went wrong at the Ingenico ePayments platform,
   131  // the Ingenico ePayments platform was unable to process a message from a downstream partner/acquirer,
   132  // or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
   133  // * APIError if the Ingenico ePayments platform returned any other error
   134  func (c *Client) Update(tokenID string, body token.UpdateRequest, context communication.CallContext) error {
   135  	pathContext := map[string]string{
   136  		"tokenId": tokenID,
   137  	}
   138  
   139  	uri, err := c.apiResource.InstantiateURIWithContext("/v1/{merchantId}/tokens/{tokenId}", pathContext)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	clientHeaders := c.apiResource.ClientHeaders()
   145  
   146  	var resultObject map[string]interface{}
   147  	putErr := c.apiResource.Communicator().Put(uri, clientHeaders, nil, body, context, &resultObject)
   148  	if putErr != nil {
   149  		responseError, isResponseError := putErr.(*sdkErrors.ResponseError)
   150  		if isResponseError {
   151  			var errorObject interface{}
   152  
   153  			errorObject = &errors.ErrorResponse{}
   154  			err = c.apiResource.Communicator().Marshaller().Unmarshal(responseError.Body(), errorObject)
   155  			if err != nil {
   156  				return err
   157  			}
   158  
   159  			err, createErr := sdkErrors.CreateAPIError(responseError.StatusCode(), responseError.Body(), errorObject, context)
   160  			if createErr != nil {
   161  				return createErr
   162  			}
   163  
   164  			return err
   165  		}
   166  
   167  		return putErr
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  // Delete represents the resource /{merchantId}/tokens/{tokenId} - Delete token
   174  // Documentation can be found at https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/go/tokens/delete.html
   175  //
   176  // Can return any of the following errors:
   177  // * ValidationError if the request was not correct and couldn't be processed (HTTP status code 400)
   178  // * AuthorizationError if the request was not allowed (HTTP status code 403)
   179  // * IdempotenceError if an idempotent request caused a conflict (HTTP status code 409)
   180  // * ReferenceError if an object was attempted to be referenced that doesn't exist or has been removed,
   181  // or there was a conflict (HTTP status code 404, 409 or 410)
   182  // * GlobalCollectError if something went wrong at the Ingenico ePayments platform,
   183  // the Ingenico ePayments platform was unable to process a message from a downstream partner/acquirer,
   184  // or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
   185  // * APIError if the Ingenico ePayments platform returned any other error
   186  func (c *Client) Delete(tokenID string, query DeleteParams, context communication.CallContext) error {
   187  	pathContext := map[string]string{
   188  		"tokenId": tokenID,
   189  	}
   190  
   191  	uri, err := c.apiResource.InstantiateURIWithContext("/v1/{merchantId}/tokens/{tokenId}", pathContext)
   192  	if err != nil {
   193  		return err
   194  	}
   195  
   196  	clientHeaders := c.apiResource.ClientHeaders()
   197  
   198  	var resultObject map[string]interface{}
   199  	deleteErr := c.apiResource.Communicator().Delete(uri, clientHeaders, &query, context, &resultObject)
   200  	if deleteErr != nil {
   201  		responseError, isResponseError := deleteErr.(*sdkErrors.ResponseError)
   202  		if isResponseError {
   203  			var errorObject interface{}
   204  
   205  			errorObject = &errors.ErrorResponse{}
   206  			err = c.apiResource.Communicator().Marshaller().Unmarshal(responseError.Body(), errorObject)
   207  			if err != nil {
   208  				return err
   209  			}
   210  
   211  			err, createErr := sdkErrors.CreateAPIError(responseError.StatusCode(), responseError.Body(), errorObject, context)
   212  			if createErr != nil {
   213  				return createErr
   214  			}
   215  
   216  			return err
   217  		}
   218  
   219  		return deleteErr
   220  	}
   221  
   222  	return nil
   223  }
   224  
   225  // Approvesepadirectdebit represents the resource /{merchantId}/tokens/{tokenId}/approvesepadirectdebit - Approve SEPA DD mandate
   226  // Documentation can be found at https://epayments-api.developer-ingenico.com/s2sapi/v1/en_US/go/tokens/approvesepadirectdebit.html
   227  //
   228  // Can return any of the following errors:
   229  // * ValidationError if the request was not correct and couldn't be processed (HTTP status code 400)
   230  // * AuthorizationError if the request was not allowed (HTTP status code 403)
   231  // * IdempotenceError if an idempotent request caused a conflict (HTTP status code 409)
   232  // * ReferenceError if an object was attempted to be referenced that doesn't exist or has been removed,
   233  // or there was a conflict (HTTP status code 404, 409 or 410)
   234  // * GlobalCollectError if something went wrong at the Ingenico ePayments platform,
   235  // the Ingenico ePayments platform was unable to process a message from a downstream partner/acquirer,
   236  // or the service that you're trying to reach is temporary unavailable (HTTP status code 500, 502 or 503)
   237  // * APIError if the Ingenico ePayments platform returned any other error
   238  func (c *Client) Approvesepadirectdebit(tokenID string, body token.ApproveRequest, context communication.CallContext) error {
   239  	pathContext := map[string]string{
   240  		"tokenId": tokenID,
   241  	}
   242  
   243  	uri, err := c.apiResource.InstantiateURIWithContext("/v1/{merchantId}/tokens/{tokenId}/approvesepadirectdebit", pathContext)
   244  	if err != nil {
   245  		return err
   246  	}
   247  
   248  	clientHeaders := c.apiResource.ClientHeaders()
   249  
   250  	var resultObject map[string]interface{}
   251  	postErr := c.apiResource.Communicator().Post(uri, clientHeaders, nil, body, context, &resultObject)
   252  	if postErr != nil {
   253  		responseError, isResponseError := postErr.(*sdkErrors.ResponseError)
   254  		if isResponseError {
   255  			var errorObject interface{}
   256  
   257  			errorObject = &errors.ErrorResponse{}
   258  			err = c.apiResource.Communicator().Marshaller().Unmarshal(responseError.Body(), errorObject)
   259  			if err != nil {
   260  				return err
   261  			}
   262  
   263  			err, createErr := sdkErrors.CreateAPIError(responseError.StatusCode(), responseError.Body(), errorObject, context)
   264  			if createErr != nil {
   265  				return createErr
   266  			}
   267  
   268  			return err
   269  		}
   270  
   271  		return postErr
   272  	}
   273  
   274  	return nil
   275  }
   276  
   277  // NewClient constructs a Tokens Client
   278  //
   279  // parent is the *apiresource.APIResource on top of which we want to build the new Tokens Client
   280  func NewClient(parent *apiresource.APIResource, pathContext map[string]string) *Client {
   281  	apiResource := apiresource.NewAPIResourceWithParent(parent, pathContext)
   282  
   283  	return &Client{apiResource}
   284  }