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 underlying http communication
- As a wrapper, please note the following
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.
Description | Version | File |
.Net SDK File | 1.0.0 | S4RestAPI.dll |
.Net Sample Project | 1.0.0 | S4DotNetSample.zip |
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
The following outlines the steps to process a transaction
The first step is to construct an S4Request
object. You must construct a new object for every request
The next step is to set some configuration parameters such as the endpoint, and the processing mode
Next, you’ll set the http headers as needed for your REST (Shift4 Gateway API ) request
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
Next, call the Process() method. The Process() method blocks until the transaction is completely processed. Process with return an S4Response
object
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
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
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
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");
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 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 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 Syntax | S4DynamicObject |
Parameters | None |
Results | S4DynamicObject instance |
Description | S4DynamicObject 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 |
Constructor Syntax | S4Request |
Parameters | None |
Results | S4Request instance |
Description | S4Request is a class that is used to build and process a request |
Constructor Syntax | S4Response |
Parameters | None |
Results | S4Response instance |
Description | S4Response is a class that represents a response. An S4Response is returned when calling S4Request.Process(). |
Method Syntax |
|
Parameters |
|
Results | None |
Description | Used 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. |
Exceptions | None |
Method Syntax | void SetHeader(String FieldName, String FieldValue) |
Parameters |
|
Results | None |
Description | This 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. |
Exceptions | None |
Method Syntax |
|
Parameters |
|
Results | None |
Description | This 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 |
Exceptions | None |
Method Syntax | S4Response = S4Request.Process() |
Parameters | None |
Results | Returns an S4Response object that represents the response |
Description | This function is used to process a request. This call is a blocking call |
Exceptions | None |
Method Syntax | int GetHttpStatusCode() |
Parameters | None |
Results | Returns an integer representing the http response code
|
Description | This function is used to obtain the http response code of the request. For example for an Authorization, you might get
|
Exceptions | None |
Method Syntax | string GetHttpError() |
Parameters | None |
Results | Returns an error string representing the http error |
Description | This function is used to obtain the http error string. This can be used when GetHttpStatusCode returns a value other than 200 |
Exceptions | None |
Method Syntax | bool GetRequiresAudit |
Parameters | None |
Results | Returns a boolean indicating if the request requires an audit |
Description | As 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. |
Exceptions | None |
Method Syntax | S4DynamicObject GetResponse() |
Parameters | None |
Results | Returns a S4DynamicObject representing the JSON response |
Description | This function is used to obtain an object representing the JSON response. |
Exceptions | None |
Method Syntax | S4DynamicObject GetHeaders() |
Parameters | None |
Results | Returns a S4DynamicObject representing the http response headers |
Description | This function is used to obtain the http response headers of the transaction |
Exceptions | None |
Method Syntax | IsSet(String path) |
Parameters | String path
|
Results | Returns a boolean indicating if the path exists or not |
Description | This function can be used to determine if a field is set in a S4DynamicObject variable. Example:
|
Exceptions | None |
Method Syntax | String ToLog() |
Parameters | None |
Results | Returns a string that has sensitive fields masked and is safe to be written to a log file. |
Description | Requests 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. |
Exceptions | None |
Method Syntax | String ToJson() |
Parameters | None |
Results | Returns a string representing the JSON response |
Description | This function returns a string representing the response. It returns a value similar to the ToLog function, but sensitive fields are not masked out. |
Exceptions | None |
Parameter Name | Mode |
Mandatory/Optional | Mandatory |
Description | This is a configuration that instructs the API on how it’s processing the transaction. Options are:
|
Parameter Name | Host |
Mandatory/Optional | Mandatory |
Description | This 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
|
Parameter Name | Method |
Mandatory/Optional | Mandatory |
Description | This is used to specify the the http method for the transaction request that you are performing
|
Parameter Name | LogEnabled |
Mandatory/Optional | Optional |
Description | By default, the API will record API activity to log file. It’s recommended to always leave this on.
|
Parameter Name | LogVerbosity |
Mandatory/Optional | Optional |
Description | By default, the API will record API activity to log file with a log verbosity of Info . LogVerbosity can be adjusted with this configurationAll logging in the SDK is done at one of the three levels
|
Parameter Name | LogPath |
Mandatory/Optional | Optional |
Description | By 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 |
Parameter Name | Port |
Mandatory/Optional | Optional |
Description | This 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.
|
Parameter Name | Timeout |
Mandatory/Optional | Optional |
Description | This is used to set the request timeout value in seconds. It is not recommended to adjust this configuration. Default value is 65 |
Parameter Name | URLPath |
Mandatory/Optional | Optional |
Description | This 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 |
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