diff --git a/README_Net461.md b/README_Net461.md index 256ef64..4300d21 100644 --- a/README_Net461.md +++ b/README_Net461.md @@ -47,6 +47,8 @@ To set your API credentials for an API request, configure the following informat merchantID = your_merchant_id merchantKeyId = your_key_serial_number merchantsecretKey = your_key_shared_secret + useMetaKey = false + enableClientCert = false ``` * Jwt @@ -58,8 +60,96 @@ To set your API credentials for an API request, configure the following informat keyPassword = your_merchant_id keyFileName = your_merchant_id keysDirectory = Resource + useMetaKey = false + enableClientCert = false ``` +* MetaKey Http + + ``` + authenticationType = http_Signature + merchantID = your_child_merchant_id + merchantKeyId = your_metakey_serial_number + merchantsecretKey = your_metakey_shared_secret + portfolioId = your_portfolio_id + useMetaKey = true + enableClientCert = false + ``` + +* MetaKey JWT + + ``` + authenticationType = Jwt + merchantID = your_child_merchant_id + keyAlias = your_child_merchant_id + keyPassword = your_portfolio_id + keyFileName = your_portfolio_id + keysDirectory = Resource + useMetaKey = true + enableClientCert = false + ``` + + * OAuth + + CyberSource OAuth uses mutual authentication. A Client Certificate is required to authenticate against the OAuth API. + + Refer to [Supporting Mutual Authentication](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro/Supporting-Mutual-Authentication.html) to get information on how to generate Client certificate. + + If the certificate (Public Key) and Private Key are in 2 different files, merge them into a single .p12 file using `openssl`. + + ```bash + openssl pkcs12 -export -out certificate.p12 -inkey privateKey.key -in certificate.crt + ``` + + Set the run environment to OAuth enabled URLs. OAuth only works in these run environments. + + ``` + // For TESTING use + _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.sandbox") + // For PRODUCTION use + // _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.production") + ``` + + To generate tokens, an Auth Code is required. The Auth Code can be generated by following the instructions given in [Integrating OAuth](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro/integrating_OAuth.html). + + This generated Auth Code can then be used to create the Access Token and Refresh Token. + + In `Source/Configuration.cs` file, set the following properties. + + Note that `authenticationType` is set to `MutualAuth` only to generate the Access Token and the Refresh Token. + + ``` + authenticationType = MutualAuth + enableClientCert = true + clientCertDirectory = resources + clientCertFile = your_client_cert in .p12 format + clientCertPassword = password_for_client_cert + clientId = your_client_id + clientSecret = your_client_secret + ``` + + Once the tokens are obtained, the `authenticationType` can then be set to `OAuth` to use the generated Access Token to send requests to other APIs. + + ``` + authenticationType = OAuth + enableClientCert = true + clientCertDirectory = resources + clientCertFile = your_client_cert - .p12 format + clientCertPassword = password_for_client_cert + clientId = your_client_id + clientSecret = your_client_secret + accessToken = generated_access_token + refreshToken = generated_refresh_token + ``` + + The Access Token is valid for 15 mins, whereas the Refresh Token is valid for 1 year. + + Once the Access Token expires, use the Refresh Token to generate another Access Token. + + Refer to [StandAloneOAuth.cs](https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/master/Source/Samples/Authentication/StandaloneOAuth.cs) to understand how to consume OAuth. + + For further information, refer to the documentation at [Cybersource OAuth 2.0](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro.html). + ## Switching between the sandbox environment and the production environment CyberSource maintains a complete sandbox environment for testing and development purposes. This sandbox environment is an exact duplicate of our production environment with the transaction authorization and settlement process simulated. By default, this SDK is configured to communicate with the sandbox environment. To switch to the production environment, set the appropriate environment constant in `Source\Configuration.cs` file. For example: @@ -72,6 +162,15 @@ _configurationDictionary.Add("runEnvironment", "cybersource.environment.sandbox" // _configurationDictionary.Add("runEnvironment", "cybersource.environment.production"); ``` +To use OAuth, switch to OAuth enabled URLs + +```csharp + // For TESTING use + _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.sandbox") + // For PRODUCTION use + // _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.production") +``` + The [API Reference Guide](https://developer.cybersource.com/api/reference/api-reference.html) provides examples of what information is needed for a particular request and how that information would be formatted. Using those examples, you can easily determine what methods would be necessary to include that information in a request using this SDK. ## License diff --git a/README_NetCore.md b/README_NetCore.md index 2973155..db3fe24 100644 --- a/README_NetCore.md +++ b/README_NetCore.md @@ -114,6 +114,8 @@ To set your API credentials for an API request, configure the following informat merchantID = your_merchant_id merchantKeyId = your_key_serial_number merchantsecretKey = your_key_shared_secret + useMetaKey = false + enableClientCert = false ``` * Jwt @@ -125,8 +127,96 @@ To set your API credentials for an API request, configure the following informat keyPassword = your_merchant_id keyFileName = your_merchant_id keysDirectory = Resource + useMetaKey = false + enableClientCert = false ``` +* MetaKey Http + + ``` + authenticationType = http_Signature + merchantID = your_child_merchant_id + merchantKeyId = your_metakey_serial_number + merchantsecretKey = your_metakey_shared_secret + portfolioId = your_portfolio_id + useMetaKey = true + enableClientCert = false + ``` + +* MetaKey JWT + + ``` + authenticationType = Jwt + merchantID = your_child_merchant_id + keyAlias = your_child_merchant_id + keyPassword = your_portfolio_id + keyFileName = your_portfolio_id + keysDirectory = Resource + useMetaKey = true + enableClientCert = false + ``` + + * OAuth + + CyberSource OAuth uses mutual authentication. A Client Certificate is required to authenticate against the OAuth API. + + Refer to [Supporting Mutual Authentication](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro/Supporting-Mutual-Authentication.html) to get information on how to generate Client certificate. + + If the certificate (Public Key) and Private Key are in 2 different files, merge them into a single .p12 file using `openssl`. + + ```bash + openssl pkcs12 -export -out certificate.p12 -inkey privateKey.key -in certificate.crt + ``` + + Set the run environment to OAuth enabled URLs. OAuth only works in these run environments. + + ``` + // For TESTING use + _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.sandbox") + // For PRODUCTION use + // _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.production") + ``` + + To generate tokens, an Auth Code is required. The Auth Code can be generated by following the instructions given in [Integrating OAuth](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro/integrating_OAuth.html). + + This generated Auth Code can then be used to create the Access Token and Refresh Token. + + In `Source/Configuration.cs` file, set the following properties. + + Note that `authenticationType` is set to `MutualAuth` only to generate the Access Token and the Refresh Token. + + ``` + authenticationType = MutualAuth + enableClientCert = true + clientCertDirectory = resources + clientCertFile = your_client_cert in .p12 format + clientCertPassword = password_for_client_cert + clientId = your_client_id + clientSecret = your_client_secret + ``` + + Once the tokens are obtained, the `authenticationType` can then be set to `OAuth` to use the generated Access Token to send requests to other APIs. + + ``` + authenticationType = OAuth + enableClientCert = true + clientCertDirectory = resources + clientCertFile = your_client_cert - .p12 format + clientCertPassword = password_for_client_cert + clientId = your_client_id + clientSecret = your_client_secret + accessToken = generated_access_token + refreshToken = generated_refresh_token + ``` + + The Access Token is valid for 15 mins, whereas the Refresh Token is valid for 1 year. + + Once the Access Token expires, use the Refresh Token to generate another Access Token. + + Refer to [StandAloneOAuth.cs](https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/master/Source/Samples/Authentication/StandaloneOAuth.cs) to understand how to consume OAuth. + + For further information, refer to the documentation at [Cybersource OAuth 2.0](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro.html). + ## Switching between the sandbox environment and the production environment CyberSource maintains a complete sandbox environment for testing and development purposes. This sandbox environment is an exact duplicate of our production environment with the transaction authorization and settlement process simulated. By default, this SDK is configured to communicate with the sandbox environment. To switch to the production environment, set the appropriate environment constant in `Source\Configuration.cs` file. For example: @@ -139,6 +229,15 @@ _configurationDictionary.Add("runEnvironment", "cybersource.environment.sandbox" // _configurationDictionary.Add("runEnvironment", "cybersource.environment.production"); ``` +To use OAuth, switch to OAuth enabled URLs + +```csharp + // For TESTING use + _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.sandbox") + // For PRODUCTION use + // _configurationDictionary.Add("runEnvironment", "cybersource.environment.mutualauth.production") +``` + The [API Reference Guide](https://developer.cybersource.com/api/reference/api-reference.html) provides examples of what information is needed for a particular request and how that information would be formatted. Using those examples, you can easily determine what methods would be necessary to include that information in a request using this SDK. ## License diff --git a/Source/Configuration.cs b/Source/Configuration.cs index 74cea14..a0665df 100644 --- a/Source/Configuration.cs +++ b/Source/Configuration.cs @@ -27,12 +27,19 @@ public Dictionary GetConfiguration() // Configs related to meta key _configurationDictionary.Add("portfolioID", string.Empty); _configurationDictionary.Add("useMetaKey", "false"); - - // _configurationDictionary.Add("proxyAddress", string.Empty); - // _configurationDictionary.Add("proxyPort", string.Empty); - // _configurationDictionary.Add("proxyUsername", string.Empty); - // _configurationDictionary.Add("proxyPassword", string.Empty); + // Configs related to OAuth + _configurationDictionary.Add("enableClientCert", "false"); + _configurationDictionary.Add("clientCertDirectory", "Resource"); + _configurationDictionary.Add("clientCertFile", ""); + _configurationDictionary.Add("clientCertPassword", ""); + _configurationDictionary.Add("clientId", ""); + _configurationDictionary.Add("clientSecret", ""); + + // _configurationDictionary.Add("proxyAddress", string.Empty); + // _configurationDictionary.Add("proxyPort", string.Empty); + // _configurationDictionary.Add("proxyUsername", string.Empty); + // _configurationDictionary.Add("proxyPassword", string.Empty); return _configurationDictionary; } @@ -61,6 +68,14 @@ public Dictionary GetAlternativeConfiguration() _configurationDictionary.Add("portfolioID", string.Empty); _configurationDictionary.Add("useMetaKey", "false"); + // Configs related to OAuth + _configurationDictionary.Add("enableClientCert", "false"); + _configurationDictionary.Add("clientCertDirectory", "Resource"); + _configurationDictionary.Add("clientCertFile", ""); + _configurationDictionary.Add("clientCertPassword", ""); + _configurationDictionary.Add("clientId", ""); + _configurationDictionary.Add("clientSecret", ""); + return _configurationDictionary; } diff --git a/Source/Samples/Authentication/StandAloneOAuth.cs b/Source/Samples/Authentication/StandAloneOAuth.cs new file mode 100644 index 0000000..bdd02b6 --- /dev/null +++ b/Source/Samples/Authentication/StandAloneOAuth.cs @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using CyberSource.Model; +using CyberSource.Api; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cybersource_rest_samples_dotnet.Samples.Authentication +{ + public class StandAloneOAuth + { + private static string code = ""; + private static string grantType = ""; + private static string refreshToken = ""; + private static string accessToken = ""; + private static Dictionary configDictionary; + public static bool createUsingAuthCode = false; + + public static void Run() + { + CallOAuthAPI(null); + } + + public static void CallOAuthAPI(string[] args) + { + AccessTokenResponse result ; + if(createUsingAuthCode) + { + // Create Access Token using Auth Code + code = "fHA73z"; + grantType = "authorization_code"; + result = postAccessTokenFromAuthCode(); + } + else { + // Create Access Token using Refresh Token + grantType = "refresh_token"; + refreshToken = "eyJraWQiOiIxMGM2MTYxNzg2MzE2ZWMzMGJjZmI5ZDcyZGU4MzFjOSIsImFsZyI6IlJTMjU2In0.eyJqdGkiOiJmODFiM2M3ZC00YWMzLTQ2MDctYTIyYi00YzUwZjgyMjQwMDkiLCJzY29wZXMiOlsicGF5bWVudHNfd2l0aF9zdGFuZGFsb25lX2NyZWRpdCIsInBheW1lbnRzX3dpdGhvdXRfc3RhbmRhbG9uZV9jcmVkaXQiLCJ0cmFuc2FjdGlvbnMiXSwiaWF0IjoxNjExODE4NjQzNDUwLCJhc3NvY2lhdGVkX2lkIjoiZWJjMl9jYXNfb2F1dGh0cDIiLCJjbGllbnRfaWQiOiJCeW94NGp4VWk2IiwibWVyY2hhbnRfaWQiOiJjZ2syX3B1c2hfdGVzdHMiLCJleHBpcmVzX2luIjoxNjQzMzU0NjQzNDUwLCJ0b2tlbl90eXBlIjoicmVmcmVzaF90b2tlbiIsImdyYW50X3R5cGUiOiJhdXRob3JpemF0aW9uX2NvZGUiLCJncmFudF90aW1lIjoiMjAyMTAxMjcyMzIzIn0.SYZ62TFuukxrPqjiGVPBzs7BHaiTkDOO-Rqjzfl4rZi_hP0pkHSTograBFgLZ3GnWxNUzgXsU5zTGqF2nllI_j0kvMIWuST6xAoDyXRHlDfcM8MQYDI7CaJGVTFbJh1U_qzN6sUUlVhKpk_BXt_4LH03_11HiQHIwnZfTcNCoDrvlnO_xkRonrEipPJb6iMO3ZEv6Z8UBc0Q-L_nR6DhHlL5M3U-S-Fi7pusq5bOyUi38CW9nwAQo9A0F3PG8n0Scji2LatjGUB4Y5hTCiRWEbIoa49fQwq0hroi11o32YriQQnMqGaaH_bCq8NgLQabRv1I73I37443lW4w0Hoy-A"; + result = postAccessTokenFromRefreshToken(); + } + + if(result != null) { + refreshToken = result.RefreshToken; + accessToken = result.AccessToken; + + // Save accessToken and refreshToken before making API calls + configDictionary["accessToken"] = accessToken; + configDictionary["refreshToken"] = refreshToken; + + // Set Authentication to OAuth + configDictionary["authenticationType"] = "OAuth"; + + //Call Payments SampleCode using OAuth, Set Authentication to OAuth in Sample Code Configuration + SimpleAuthorizationInternet(); + } + + } + + public static AccessTokenResponse postAccessTokenFromAuthCode() + { + AccessTokenResponse result = null; + try + { + configDictionary = new Configuration().GetConfiguration(); + configDictionary["authenticationType"] = "Mutual_Auth"; + var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary); + + var requestObj = new CreateAccessTokenRequest( + Code: code, + GrantType: grantType, + ClientId: configDictionary["clientId"], + ClientSecret: configDictionary["clientSecret"] + ); + + var apiInstance = new OAuthApi(clientConfig); + result = apiInstance.PostAccessTokenRequest(requestObj); + Console.WriteLine(result); + return result; + + } + catch (Exception e) + { + Console.WriteLine("Exception on calling the API : " + e.Message); + return null; + } + } + + public static AccessTokenResponse postAccessTokenFromRefreshToken() + { + AccessTokenResponse result = null; + try + { + configDictionary = new Configuration().GetConfiguration(); + configDictionary["authenticationType"] = "Mutual_Auth"; + var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary); + + var requestObj = new CreateAccessTokenRequest( + RefreshToken: refreshToken, + GrantType: grantType, + ClientId: configDictionary["clientId"], + ClientSecret: configDictionary["clientSecret"] + ); + + var apiInstance = new OAuthApi(clientConfig); + result = apiInstance.PostAccessTokenRequest(requestObj); + Console.WriteLine(result); + return result; + + } + catch (Exception e) + { + Console.WriteLine("Exception on calling the API : " + e.Message); + return null; + } + } + + public static PtsV2PaymentsPost201Response SimpleAuthorizationInternet() + { + string clientReferenceInformationCode = "TC50171_3"; + Ptsv2paymentsClientReferenceInformation clientReferenceInformation = new Ptsv2paymentsClientReferenceInformation( + Code: clientReferenceInformationCode + ); + + bool processingInformationCapture = false; + + Ptsv2paymentsProcessingInformation processingInformation = new Ptsv2paymentsProcessingInformation( + Capture: processingInformationCapture + ); + + string paymentInformationCardNumber = "4111111111111111"; + string paymentInformationCardExpirationMonth = "12"; + string paymentInformationCardExpirationYear = "2031"; + Ptsv2paymentsPaymentInformationCard paymentInformationCard = new Ptsv2paymentsPaymentInformationCard( + Number: paymentInformationCardNumber, + ExpirationMonth: paymentInformationCardExpirationMonth, + ExpirationYear: paymentInformationCardExpirationYear + ); + + Ptsv2paymentsPaymentInformation paymentInformation = new Ptsv2paymentsPaymentInformation( + Card: paymentInformationCard + ); + + string orderInformationAmountDetailsTotalAmount = "102.21"; + string orderInformationAmountDetailsCurrency = "USD"; + Ptsv2paymentsOrderInformationAmountDetails orderInformationAmountDetails = new Ptsv2paymentsOrderInformationAmountDetails( + TotalAmount: orderInformationAmountDetailsTotalAmount, + Currency: orderInformationAmountDetailsCurrency + ); + + string orderInformationBillToFirstName = "John"; + string orderInformationBillToLastName = "Doe"; + string orderInformationBillToAddress1 = "1 Market St"; + string orderInformationBillToLocality = "san francisco"; + string orderInformationBillToAdministrativeArea = "CA"; + string orderInformationBillToPostalCode = "94105"; + string orderInformationBillToCountry = "US"; + string orderInformationBillToEmail = "test@cybs.com"; + string orderInformationBillToPhoneNumber = "4158880000"; + Ptsv2paymentsOrderInformationBillTo orderInformationBillTo = new Ptsv2paymentsOrderInformationBillTo( + FirstName: orderInformationBillToFirstName, + LastName: orderInformationBillToLastName, + Address1: orderInformationBillToAddress1, + Locality: orderInformationBillToLocality, + AdministrativeArea: orderInformationBillToAdministrativeArea, + PostalCode: orderInformationBillToPostalCode, + Country: orderInformationBillToCountry, + Email: orderInformationBillToEmail, + PhoneNumber: orderInformationBillToPhoneNumber + ); + + Ptsv2paymentsOrderInformation orderInformation = new Ptsv2paymentsOrderInformation( + AmountDetails: orderInformationAmountDetails, + BillTo: orderInformationBillTo + ); + + var requestObj = new CreatePaymentRequest( + ClientReferenceInformation: clientReferenceInformation, + ProcessingInformation: processingInformation, + PaymentInformation: paymentInformation, + OrderInformation: orderInformation + ); + + try + { + var clientConfig = new CyberSource.Client.Configuration(merchConfigDictObj: configDictionary); + + var apiInstance = new PaymentsApi(clientConfig); + PtsV2PaymentsPost201Response result = apiInstance.CreatePayment(requestObj); + Console.WriteLine(result); + return result; + } + catch (Exception e) + { + Console.WriteLine("Exception on calling the API : " + e.Message); + return null; + } + } + } +}