Giter Site home page Giter Site logo

sarn1 / example-cybersource-csharp-api-rest Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 17 KB

An example C# version of posting to the CyberSource REST Web API.

Home Page: https://sarn.phamornsuwana.com/c-code-for-cybersource-rest-apis/

License: MIT License

C# 100.00%
cybersource rest-api csharp-code csharp payment-integration payment-gateway

example-cybersource-csharp-api-rest's Introduction

Example C# Code For Posting to CyberSource APIs

This is sample code to show you how to post to CyberSource web APIs. This sample is posting to the development environment at https://apitest.cybersource.com From this code, you will be able to create the request headers necessary for your calls to be authorized by CyberSource. There are some variations that needs to be adjusted for calls to other methods and HTTP verbs, but the hardest part which is creating the acceptable request headers is provided to you in this sample. If you're still stuck, read in more details at https://sarn.phamornsuwana.com/c-code-for-cybersource-rest-apis/

Getting Started

You will need to have your merchant Id, secret key, and key Id ready. Please put the sample JSON file in your project's bin directory.

Usage

The codebase is in .NET Core, since at this time (3/2019) the CyberSource Rest API SDK only works on the .NET Framework. I'm assuming, most people who will find this sample code useful are those on .NET Core, issues compiling the SDK along with their other NUGETs, deployment workflow or just prefer to utilize the web APIs directly. Please note that this code is ONLY for learning purposes and a proof of concept. It is NOT production ready code. Please refactor accordingly.

License

Distributed under the MIT License. See LICENSE for more information.

example-cybersource-csharp-api-rest's People

Contributors

sarn1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

example-cybersource-csharp-api-rest's Issues

Can you provide sample code for Create a Payment Instrument(/tms/v1/paymentinstruments) in c# ?

i am working on .net core. i checked sample from this repository and its working fine but i tried for Create a Payment Instrument which is not working. i have tried below code. please advice.

var profileId = "93B32398-AD51-4CC2-A682-EA3E93614EB1";
            var requestObj = new CreatePaymentInstrumentRequest();

             var cardObj = new TmsV1InstrumentIdentifiersPaymentInstrumentsGet200ResponseEmbeddedCard
            (
                ExpirationMonth: "09",
                ExpirationYear: "2022",
                Type: TmsV1InstrumentIdentifiersPaymentInstrumentsGet200ResponseEmbeddedCard.TypeEnum.Visa,
                IssueNumber: "01",
                StartMonth: "01",
                StartYear: "2016"
            );

            requestObj.Card = cardObj;

            var billToObj = new TmsV1InstrumentIdentifiersPaymentInstrumentsGet200ResponseEmbeddedBillTo
            {
                FirstName = "John",
                LastName = "Smith",
                Company = "CyberSource",
                Address1 = "12 Main Street",
                Address2 = "20 My Street",
                Locality = "San Francisco",
                AdministrativeArea = "CA",
                PostalCode = "90200",
                Country = "US",
                Email = "[email protected]",
                PhoneNumber = "555123456"
            };

            requestObj.BillTo = billToObj;

            var instrumentIdentifierObj = new TmsV1InstrumentIdentifiersPaymentInstrumentsGet200ResponseEmbeddedInstrumentIdentifier();

            var cardObj2 = new TmsV1InstrumentIdentifiersPost200ResponseCard
            {
                Number = "4111111111111111"
            };

            instrumentIdentifierObj.Card = cardObj2;

            requestObj.InstrumentIdentifier = instrumentIdentifierObj;

            //var jsonstr = new JavaScriptSerializer().Serialize(requestObj);
            var jsonstr = JsonConvert.SerializeObject(requestObj,
            new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });


            string SecretKey = "yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=";
            string KeyId = "08c94330-f618-42a3-b09d-e1e43be5efda";
            string MerchantId = "testrest";

            // pulling from file to control the body payload
            string JsonObj = "";//File.ReadAllText(@"json.txt");

            
            JsonObj = jsonstr;

            var Digest = GenerateDigest(JsonObj);
            //Console.WriteLine(Digest);

            var SignatureParm = "host: apitest.cybersource.com\n(request-target): post /tms/v1/paymentinstruments/\ndigest: " + Digest + "\nv-c-merchant-id: " + MerchantId;
            var SignatureHash = GenerateSignatureFromParams(SignatureParm, SecretKey);
            //Console.WriteLine(SignatureHash);

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apitest.cybersource.com/tms/v1/paymentinstruments/");
            httpWebRequest.Method = "POST";

            httpWebRequest.Headers.Add("v-c-merchant-id", MerchantId);
            httpWebRequest.Headers.Add("v-c-date", DateTime.Now.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'"));
            httpWebRequest.Headers.Add("Digest", Digest);
            httpWebRequest.Headers.Add("profile-id", profileId);
            httpWebRequest.Headers.Add("Signature", "keyid=\"" + KeyId + "\", algorithm=\"HmacSHA256\", headers=\"host (request-target) digest v-c-merchant-id\", signature=\"" + SignatureHash + "\"");
            httpWebRequest.ContentType = "application/json";

            //   var httpResponse = new HttpWebResponse();
            string res = "";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(JsonObj);
                streamWriter.Flush();
                streamWriter.Close();
            }

            try
            {
               

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    res = streamReader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                // Console.WriteLine(httpResponse.ToString());
            }

            Console.Write(res);

            Console.ReadKey();

This is json string.
Jsonstring.txt

Thank you
Suraj Patel.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.