A couple of questions about the .NET SDK

Hello everyone!

We’re working on integrating Square payments into our software now and had two questions (We’re using your .NET SDK).

a) For the “idempotencyKey”, are we able to set this to whatever we would like per transaction? For PayPal, for instance, they have a call to generate a token prior to processing the payment, so PayPal would supply the token. We were a little confused by this and wanted some clarification.

b) For the “client.PaymentsApi.CreatePaymentAsync(body: body);” line, where is “client” found in the SDK or otherwise defined? In your sample code, “client” is not defined, and we’re getting an error that “the name client doesn’t exist in the current context”. We’ve attempted to find client within the SDK, but have not been able to do so.

Below is the code we’re working with. We appreciate everyone’s help!

List result = new List();
string Response = “”;
string ResponseID = “”;
string ResponseDescription = “”;
string ResponseAuthCode = “”;
string ResponseResponseCode = “”;
string ResponseMessageCode = “”;

        //Sandbox
        string ApplicationID = "sandbox----";
        string AccessToken = "----";

        ////Live

        //Set Idempotencykey


        
        var amountMoney = new Square.Models.Money.Builder()
            .Amount(100L)
            .Currency("USD")
            .Build();

        var body = new Square.Models.CreatePaymentRequest.Builder(
            sourceId: "cnon:card-nonce-ok",
            idempotencyKey: "22ab1f78-6bbb-4c26-a968-5932376625bb",
            amountMoney: amountMoney)
          .Build();

        try
        {
            var presult = await client.PaymentsApi.CreatePaymentAsync(body: body);
        }
        catch (Square.Exceptions.ApiException e)
        {
            Console.WriteLine("Failed to make the request");
            Console.WriteLine($"Response Code: {e.ResponseCode}");
            Console.WriteLine($"Exception: {e.Message}");
        }

:wave: Idempotency keys can be anything, but they need to be unique. Virtually all popular programming languages provide a function for generating unique strings. For the .NET SDK you can use Guid.NewGuid.

Also before using the SDK you’ll need to Create a Square Client for the requests. :slightly_smiling_face:

1 Like

Thank you very much, I appreciate the help!