Skip to content
Last updated

The S4/.Net SDK is a wrapper around the Shift4 REST API (Shift4 Gateway API). The Shift4 REST API is an http based API.

  • As an integrator, you can integrate directly to the Shift4 REST API
  • Or, you can use the SDK, which simplifies and abstracts many of the complexities of the REST API
    • As a wrapper, please note the following
      • You’ll still refer to the core REST API documentation (Shift4 Gateway API) for details on the transaction types, fields and all other details
      • The SDK allows you to build requests and receive responses. Using the SDK, you’ll control the following aspects:
        • The HTTP headers
        • The HTTP body
        • The HTTP type (POST, GET, etc)
      • Some of the areas of the REST API that are simplified or eliminated are:
        • The underlying http communication
          • The SDK will handle http communication automatically
        • Timeout and communication failure handling
          • The SDK will handle timeouts and communication failures automatically
        • Logging and log masking
          • The SDK will log requests/responses and will mask sensitive data

The S4/.Net component requires .Net Framework version 6.0 or higher on Windows. If there is a need for compatibility with earlier versions of the .Net Framework, please contact your Shift4 Integration Analyst for review. If you do not already have an Shift4 Integration Analyst assigned, one will be assigned to you during project kickoff.

SDK & Sample Application

DescriptionVersionFile
.Net SDK File1.0.0S4RestAPI.dll
.Net Sample Project1.0.0S4DotNetSample.zip

API Description

S4/.Net is comprised of the following classes, all of which are located in the namespace S4RestAPI:

  • S4Request: This class is used to setup a request
  • S4Response: This is the class that represents a response
  • S4DynamicObject: this class is used to represent a JSON like structure. It works similar to a .Net ExpandoObject
  • S4Mode: this class has defines that instruct the API on which processing mode is being used
  • S4Method: this class has defines that instruct the API on which http method is being used on the underlying request to Shift4

Expected Flow

The following outlines the steps to process a transaction

Step 1: Construct

The first step is to construct an S4Request object. You must construct a new object for every request

Step 2: SetConfig

The next step is to set some configuration parameters such as the endpoint, and the processing mode

Step 3: SetHeaders

Next, you’ll set the http headers as needed for your REST (Shift4 Gateway API ) request

Step 4: SetRequest

The developer must then build a request object, and populate it with all of the structured data fields required for the request. When the request object is built, the developer calls SetRequest

Step 5: Process

Next, call the Process() method. The Process() method blocks until the transaction is completely processed. Process with return an S4Response object

Step 6: Response Handling

The developer then calls GetHttpStatusCode() to confirm the http result and GetResponse() to retrieve a response object. This object contains all of the response fields. At this stage the response can be handled and processed in your integration layer

Installing S4/.Net (Windows)

To install S4 /.Net, you first need to install the .Net framework version 6.0 or higher. Please refer to https://learn.microsoft.com/en-us/dotnet/framework/install/ for install instructions.

Confirm that you have received the S4/.Net Windows DLL. This should have been provided to you by your Integration Analyst prior to the integration project kick-off meeting. The DLL can be dropped into your .Net project root directory.

In addition, you’ll need to use NuGet packages to add a package to your project

  • In your visual studio project, right click on your solution
  • Select Manage NuGet Packages
  • Install Newtonsoft.Json

Running as a Windows Service

If you are running your application as a windows service, please note the following

The LogPath will also need to be defined as a folder that the client application has permission to write to. LogPath can be updated with a SetConfig call

Sample Code

using S4RestAPI;

S4Request s4Request = new S4Request();

s4Request.SetConfig("Mode",    S4Mode.HOST_DIRECT);
s4Request.SetConfig("Method",  S4Method.POST);
s4Request.SetConfig("Host",    "https://api.shift4test.com");
s4Request.SetConfig("URLPath", "/api/rest/v1/transactions/sale");

s4Request.SetHeader("InterfaceVersion", "2.1");
s4Request.SetHeader("InterfaceName",    "RestAPIDotNetSdk");
s4Request.SetHeader("CompanyName",      "Shift4");
s4Request.SetHeader("AccessToken",      ""); //PROVIDED BY SHIFT4

dynamic dRequest = new S4DynamicObject();

dRequest.dateTime = DateTime.Now.ToString("o");

dRequest.amount = new S4DynamicObject();
dRequest.amount.tax = 1.00;
dRequest.amount.total = 11.00;

dRequest.apiOptions = new S4DynamicObject[1];
dRequest.apiOptions[0] = new S4DynamicObject();
dRequest.apiOptions[0] = "ALLOWPARTIALAUTH";

dRequest.clerk = new S4DynamicObject();
dRequest.clerk.numericId = "12345";

dRequest.transaction = new S4DynamicObject();
dRequest.transaction.invoice = "6820973";

dRequest.card = new S4DynamicObject();
dRequest.card.entryMode = "M";
dRequest.card.number = "4444333322221111";
dRequest.card.expirationDate = 1229;
dRequest.card.securityCode = new S4DynamicObject();
dRequest.card.securityCode.indicator = "1";
dRequest.card.securityCode.value = "333";

dRequest.customer = new S4DynamicObject();

dRequest.postalCode = "89000";


s4Request.SetRequest(dRequest);
Console.WriteLine("Processing Transaction");
S4Response s4Response = s4Request.Process();
dynamic resultObj = s4Response.GetResponse();
if (s4Response.GetHttpStatusCode() == 200)
{
    if (resultObj.IsSet("result[0].transaction.responseCode"))
        Console.WriteLine("Response Code:" + resultObj.result[0].transaction.responseCode);
    if (resultObj.IsSet("result[0].transaction.authorizationCode"))
        Console.WriteLine("Authorization Code:" + resultObj.result[0].transaction.authorizationCode);
}
else
{
    Console.WriteLine("Error:" + s4Response.GetHttpStatusCode());
    Console.WriteLine("Error Response:" + resultObj.ToLog());
    if (s4Response.GetRequiresAudit())
    {
        Console.WriteLine("Transaction Requires Audit");
        //Developer needs to add this transaction to a list that requires manual audit
        //See https://myportal.shift4.com/index.cfm?action=development.shift4api#section/Logging-Communication-Failures-for-Auditor-Review
    }
}

Console.WriteLine("Complete");

Static Class Definitions

S4Mode

S4Mode is used to control the transaction processing mode

  • S4Mode.COM_ENG: commerce engine. Used when processing direct to commerce engine on premises
  • S4Mode.COM_ENG_CLOUD: Commerce engine cloud. Used when processing via commerce engine cloud
  • S4Mode.HOST_DIRECT: Host direct. Used when processing direct to the Shift4 cloud endpoint

S4Method

S4Method is used to control the http method used to process a transaction.

  • S4Method.DELETE: http delete
  • S4Method.POST: http post
  • S4Method.GET: http get

S4LogVerbosity

S4LogVerbosity is used to control the log verbosity

  • S4LogVerbosity.Info: highest level of logging
  • S4LogVerbosity.Warning: log warnings and errors
  • S4LogVerbosity.Error: log errors only

Constructor Definitions

S4DynamicObject

Constructor SyntaxS4DynamicObject
ParametersNone
ResultsS4DynamicObject instance
DescriptionS4DynamicObject is a class that is used for setting requests and receiving responses. It can be used to dynamically access and/or set properties.

It offers similar functionality to an ExpandoObject with one major difference: If you access a property that does not exist, then a blank value will be returned

S4Request

Constructor SyntaxS4Request
ParametersNone
ResultsS4Request instance
DescriptionS4Request is a class that is used to build and process a request

S4Response

Constructor SyntaxS4Response
ParametersNone
ResultsS4Response instance
DescriptionS4Response is a class that represents a response. An S4Response is returned when calling S4Request.Process().

Method Definitions

S4Request.SetConfig

Method Syntax
  • void SetConfig(String FieldName, String FieldValue)
  • void SetConfig(String FieldName, S4Mode mode)
  • void SetConfig(String FieldName, S4Method method)
  • void SetConfig(String FieldName, S4LogVerbosity logVerbosity)
Parameters
  • String FieldName
    • configuration name
  • String FieldValue
    • configuration value
  • S4Mode mode
    • processing mode
  • S4Method
    • processing method
  • S4LogVerbosity
    • log verbosity
ResultsNone
DescriptionUsed to set configuration for a request. For a list of configuration values, see Configuration Options below. This function must be called once for each configuration that is being set.set.
ExceptionsNone

S4Request.SetHeader

Method Syntaxvoid SetHeader(String FieldName, String FieldValue)
Parameters
  • String FieldName
    • header name
  • String FieldValue
    • header value
ResultsNone
DescriptionThis function is used to specify the http headers in the request.

For example, an Authorization requires 4 headers: InterfaceVersion, InterfaceName, CompanyName and AccessToken. To perform this transaction, you would call SetHeader 4 times, once for each header.
ExceptionsNone

S4Request.SetRequest

Method Syntax
  • void SetRequest(S4DynamicObject request)
  • void SetRequest(String json)
Parameters
  • S4DynamicObject request
    • request is the object that contains the request structure.
  • String json
    • json body of the request
ResultsNone
DescriptionThis function is used to specify the body of the request. The body must match the JSON fields indicated in the REST API for the request that you are processing
ExceptionsNone

S4Request.Process

Method SyntaxS4Response = S4Request.Process()
ParametersNone
ResultsReturns an S4Response object that represents the response
DescriptionThis function is used to process a request. This call is a blocking call
ExceptionsNone

S4Response.GetHttpStatusCode

Method Syntaxint GetHttpStatusCode()
ParametersNone
ResultsReturns an integer representing the http response code
  • 200 is a normal/valid response
  • anything else is considered an error
DescriptionThis function is used to obtain the http response code of the request. For example for an Authorization, you might get
  • 200 for a valid response
  • 4xx or 5xx for an error
ExceptionsNone

S4Response.GetHttpError

Method Syntaxstring GetHttpError()
ParametersNone
ResultsReturns an error string representing the http error
DescriptionThis function is used to obtain the http error string. This can be used when GetHttpStatusCode returns a value other than 200
ExceptionsNone

S4Response.GetRequiresAudit

Method Syntaxbool GetRequiresAudit
ParametersNone
ResultsReturns a boolean indicating if the request requires an audit
DescriptionAs noted in the Handling Response guide, certain transactions may need to be recorded for manual review to determine if they processed not. While it’s rare that a transaction requires manual review, it can happen. This function call can be used to determine if manual review/audit is required.

Note that when GetHttpStatusCode returns a 200 (indicating a valid response), GetRequiresAudit does not need to be checked.
ExceptionsNone

S4Response.GetResponse

Method SyntaxS4DynamicObject GetResponse()
ParametersNone
ResultsReturns a S4DynamicObject representing the JSON response
DescriptionThis function is used to obtain an object representing the JSON response.
ExceptionsNone

S4Response.GetHeaders

Method SyntaxS4DynamicObject GetHeaders()
ParametersNone
ResultsReturns a S4DynamicObject representing the http response headers
DescriptionThis function is used to obtain the http response headers of the transaction
ExceptionsNone

S4DynamicObject.IsSet

Method SyntaxIsSet(String path)
ParametersString path
  • this is the path that you want to check if it’s set
ResultsReturns a boolean indicating if the path exists or not
DescriptionThis function can be used to determine if a field is set in a S4DynamicObject variable. Example:
  • IsSet("result[0].transaction.responseCode")
ExceptionsNone

S4DynamicObject.ToLog

Method SyntaxString ToLog()
ParametersNone
ResultsReturns a string that has sensitive fields masked and is safe to be written to a log file.
DescriptionRequests and responses may contain sensitive data that cannot be written to disk such as the credit card number and the credit card CVV. This function returns a string representing the response, but with sensitive fields masked out.
ExceptionsNone

S4DynamicObject.ToJson

Method SyntaxString ToJson()
ParametersNone
ResultsReturns a string representing the JSON response
DescriptionThis function returns a string representing the response. It returns a value similar to the ToLog function, but sensitive fields are not masked out.
ExceptionsNone

Configuration Options

Mode

Parameter NameMode
Mandatory/OptionalMandatory
DescriptionThis is a configuration that instructs the API on how it’s processing the transaction. Options are:
  • S4Mode.HOST_DIRECT: this mode is used when you need to send requests directly to the Shift4 host
  • S4Mode.COM_ENG: this mode is used when you need to send requests to commerce engine on premise
  • S4Mode.COM_ENG_CLOUD: this mode is used when you are sending requests to commerce engine cloud

Host

Parameter NameHost
Mandatory/OptionalMandatory
DescriptionThis is used to specify the host that the API will connect to. See Quick Start URLs for a list of potential values to use for the host.
Example: for host direct, you would use
  • Test: https://api.shift4test.com/api/rest/v1
  • Production: https://api.shift4api.net/api/rest/v1

Method

Parameter NameMethod
Mandatory/OptionalMandatory
DescriptionThis is used to specify the the http method for the transaction request that you are performing
  • S4Method.GET: process the request using http GET
  • S4Method.POST: process the request using http POST
  • S4Method.DELETE: process the request using http DELETE

    This will match the http method specified in the REST API. For example, an Authorization would use S4Method.POST

LogEnabled

Parameter NameLogEnabled
Mandatory/OptionalOptional
DescriptionBy default, the API will record API activity to log file. It’s recommended to always leave this on.
  • To disable logging, set LogEnabled to “N”
  • To enable logging, set LogEnabled to “Y”

LogVerbosity

Parameter NameLogVerbosity
Mandatory/OptionalOptional
DescriptionBy default, the API will record API activity to log file with a log verbosity of Info. LogVerbosity can be adjusted with this configuration

All logging in the SDK is done at one of the three levels
  • S4LogVerbosity.Info
  • S4LogVerbosity.Warning
  • S4LogVerbosity.Error

    It’s recommended to keep LogVerbosity at the default level

LogPath

Parameter NameLogPath
Mandatory/OptionalOptional
DescriptionBy default, the API will record API activity to log file in the local directory of the application that is running. This configuration can be used to change the log file path

Port

Parameter NamePort
Mandatory/OptionalOptional
DescriptionThis is used to specify the port that the API will connect to. See Quick Start URLs for a list of potential values to use for the host port.
  • Host Direct: 443
  • Commerce Engine For Cloud: 443
  • Commerce Engine For On Premise: 8085

    Default value is 443.

Timeout

Parameter NameTimeout
Mandatory/OptionalOptional
DescriptionThis is used to set the request timeout value in seconds. It is not recommended to adjust this configuration.

Default value is 65

URLPath

Parameter NameURLPath
Mandatory/OptionalOptional
DescriptionThis is used to specify the the URL path for the request that you are performing

This will match the url path specified in the REST API. For example, an Authorization would use api/rest/v1/transactions/authorization

Technical Considerations

Threading

The S4/.Net library supports a multi-threaded environment. However, each instance (object) of the API can only be used in one thread at a time. You cannot share an object concurrently between different threads at the same time. In practical terms, this means that a program must complete a request in one thread without using the same object in a different thread. Objects should be created and then discarded after each request