Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build with multiple frameworks #295

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,4 @@ UpgradeLog*.htm

# Microsoft Fakes
FakesAssemblies/
/.vs
104 changes: 52 additions & 52 deletions Authorize.NET/AuthorizeNET.csproj
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5D52EAEC-42FB-4313-83B8-69E2F55EBF14}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AuthorizeNet</RootNamespace>
<AssemblyName>AuthorizeNet</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
<Version>2.0.3.1</Version>
<AssemblyName>AuthorizeNet</AssemblyName>
<RootNamespace>AuthorizeNet</RootNamespace>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Title>AuthorizeNet</Title>
<Description>Use this SDK to integrate with the Authorize.Net APIs for Payment Transactions, Recurring Billing, Customer Payment Profiles and Reporting. </Description>
<Copyright>https://github.com/AuthorizeNet/sdk-dotnet/blob/master/LICENSE.txt</Copyright>
<PackageProjectUrl>https://github.com/wjmirror/AuthorizeNet-sdk-dotnet/tree/feature/multi-frameworks</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/wjmirror/AuthorizeNet-sdk-dotnet/tree/feature/multi-frameworks</RepositoryUrl>
<PackageTags>Payments API; Authorize.Net</PackageTags>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageId>AuthorizeNet.MultiFrameworks</PackageId>
<iconUrl>http://developer.authorize.net/resources/images/favicon.ico</iconUrl>
<licenseUrl>https://github.com/AuthorizeNet/sdk-dotnet/blob/master/LICENSE.txt</licenseUrl>
<PackageReleaseNotes>Build with multiple frameworks, .Net framework 4.5, .Net Standard 2.0 and .Net 6.0</PackageReleaseNotes>
<Authors>AuthorizeNet, jimwang1014</Authors>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin/Debug/</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin/Debug/AuthorizeNet.XML</DocumentationFile>
<NoWarn>0219,1591,1635</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin/Release</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin/Release/AuthorizeNet.XML</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
Expand All @@ -54,7 +34,10 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Security" />
</ItemGroup>

<ItemGroup>
<Compile Include="AuthorizeNetExtension.cs" />
<Compile Include="AuthorizeNetOptions.cs" />
<Compile Include="Environment.cs" />
<Compile Include="MarketType.cs" />
<Compile Include="TestFriends.cs" />
Expand Down Expand Up @@ -119,15 +102,32 @@
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE.txt">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="Api\ControllerTemplate.cst" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!--Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /-->
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>3.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions">
<Version>3.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Options">
<Version>3.1.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Options">
<Version>6.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
137 changes: 137 additions & 0 deletions Authorize.NET/AuthorizeNetExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#if NETSTANDARD || NET6_0
using AuthorizeNet.Api.Contracts.V1;
using AuthorizeNet.Api.Controllers.Bases;
using AuthorizeNet.Util;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;

namespace AuthorizeNet
{
/// <summary>
/// AuthorizeNet ServiceProvider Extension.
/// </summary>
public static class AuthorizeNetExtension
{
/// <summary>
/// Setup client library options. Call this extension function to setup Authorize.Net client library.
/// It set the ILoggerFactory for logging,
/// set ApiOperationBase&lt;ANetApiRequest, ANetApiResponse&gt;.RunEnvironment according to AuthorizeNetOptions.Environment
/// set ApiOperationBase&lt;ANetApiRequest, ANetApiResponse&gt;.MerchantAuthentication according to AuthorizeNetOptions.ApiLoginId and AuthorizeNetOptions.TransactionKey.
/// </summary>
/// <param name="services">Service Provider. <see cref="IServiceProvider"/></param>
/// <param name="optionsBuilder">The AuthorizeNetOptions builder delegation. </param>
/// <param name="merchantAuenticationBuilder">The default merchant authentication builder delegation.</param>
/// <exception cref="Exception">
/// The AuthorizeNetOptions can be configured either in ConfigureService with IServiceCollection.Configure&lt;AuthorizeNetOptions&gt; or use optionsBuilder. Exception is thrown when neither of them is set.
/// The merchantAuenticationBuilder is called to setup the default marchant authentication.
/// ILoggerFactory is required in .Net Core, exception is thrown when the ILoggerFactory is not configured in DI container.
/// </exception>
public static void SetupAuthorizeNet(this IServiceProvider services, Action<AuthorizeNetOptions> optionsBuilder = null, Action<merchantAuthenticationType> merchantAuenticationBuilder = null)
{
AuthorizeNetOptions options = null;
IOptions<AuthorizeNetOptions> optionsMonitor = services.GetService<IOptions<AuthorizeNetOptions>>();

if (optionsMonitor == null && optionsBuilder == null)
throw new Exception($"The AuthorizeNetOptions is not configured and optionsBuilder Action is not set.");

if (optionsMonitor != null)
options = optionsMonitor.Value;

optionsBuilder?.Invoke(options);

//setup ILoggerFactory
ILoggerFactory loggerFactory = services.GetService<ILoggerFactory>();
if (loggerFactory == null)
throw new Exception($"Can't Get the ILoggerFactory service.");

LogFactory.LoggerFactory = loggerFactory;
LogFactory.LoggingSensitiveData = options.LoggingSensitiveData;

//setup environment
AuthorizeNet.Environment.GetConfigurationPropertyValue = (propName) =>
{
string propValue = null;
if (propName == Constants.HttpConnectionTimeout)
{
if (options.ConnectionTimeout != null)
propValue = options.ConnectionTimeout.ToString();
}
else if (propName == Constants.HttpReadWriteTimeout)
{
if (options.ReadWriteTimeout != null)
propValue = options.ReadWriteTimeout.ToString();
}
else if (propName == Constants.HttpsUseProxy)
{
if (options.UseProxy != null)
propValue = options.UseProxy.ToString();
}
else if (propName == Constants.HttpsProxyHost)
{
propValue = options.ProxyHost;
}
else if (propName == Constants.HttpsProxyPort)
{
if (options.ProxyPort != null)
propValue = options.ProxyPort.ToString();
}
else
{
throw new Exception($"The property {propName} is not configured in AuthorizeNetOptions ");
}

return propValue;
};


AuthorizeNet.Environment runningEnvironment = AuthorizeNet.Environment.LOCAL_VM;

if (0 == string.Compare(options.Environment, "PRODUCTION", true))
{
runningEnvironment = AuthorizeNet.Environment.PRODUCTION;
}
else if (0 == string.Compare(options.Environment, "SANDBOX", true))
{
runningEnvironment = AuthorizeNet.Environment.SANDBOX;
}
else if (0 == string.Compare(options.Environment, "LOCAL_VM", true))
{
runningEnvironment = AuthorizeNet.Environment.LOCAL_VM;
}
else if (0 == string.Compare(options.Environment, "HOSTED_VM", true))
{
runningEnvironment = AuthorizeNet.Environment.HOSTED_VM;
}
else if (0 == string.Compare(options.Environment, "CUSTOM", true))
{
if (string.IsNullOrWhiteSpace(options.BaseUrl))
throw new Exception("The AuthorizeNetOptions.BaseUrl can not be empty in Custom environment.");

if (string.IsNullOrWhiteSpace(options.XmlBaseUrl))
throw new Exception("The AuthorizeNetOptions.XmlBaseUrl can not be empty in Custom environment.");

runningEnvironment = AuthorizeNet.Environment.createEnvironment(options.BaseUrl, options.XmlBaseUrl, options.CardPresentUrl);
}
else
{
throw new Exception($"Invalid environment name: {options.Environment}, it must be Production, SandBox, Local_VM, Hosted_VM or Custom");
}

ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = runningEnvironment;

//setup merchant authentication
merchantAuthenticationType merchantAuthentication = new merchantAuthenticationType
{
name = options.ApiLoginId,
ItemElementName = ItemChoiceType.transactionKey,
Item = options.TransactionKey
};
merchantAuenticationBuilder?.Invoke(merchantAuthentication);
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = merchantAuthentication;
}

}
}
#endif
39 changes: 39 additions & 0 deletions Authorize.NET/AuthorizeNetOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#if NETSTANDARD || NET6_0
using AuthorizeNet.APICore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorizeNet
{
/// <summary>
/// Authorize.Net connection options, the Environment, ApiLoginId and TransactionKey are required.
/// When Environment is CUSTOM, the BaseUrl, XmlBaseUrl are required.
/// </summary>
public class AuthorizeNetOptions
{
/// <summary>
/// Environment Name, can be SandBox, Production or Custom. The BaseUrl and XmlBaseUrl are required when Environment is Custom.
/// </summary>
public string Environment { get; set; } = "SANDBOX";
public string ApiLoginId { get; set; }
public string TransactionKey { get; set; }

public bool? UseProxy { get; set; }
public string ProxyHost { get; set; }
public int? ProxyPort { get; set; }

public int? ConnectionTimeout { get; set; }
public int? ReadWriteTimeout { get; set; }

public string BaseUrl { get; set; }
public string XmlBaseUrl { get; set; }
public string CardPresentUrl { get; set; }

public bool LoggingSensitiveData { get; set; }

}
}
#endif
33 changes: 32 additions & 1 deletion Authorize.NET/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static bool getBooleanProperty( String propertyName)

return value;
}

#if NET45
private static object mutex = new object();

/// <summary>
Expand Down Expand Up @@ -148,5 +148,36 @@ public static String GetProperty(String propertyName) {
}
return stringValue;
}
#else

public static Func<string, string> GetConfigurationPropertyValue;

/// <summary>
/// Reads the value from property file and/or the environment
/// Values in property file supersede the values set in environmen
/// </summary>
/// <param name="propertyName">propertyName name of the property to read</param>
/// <returns>String property value</returns>
public static String GetProperty(String propertyName)
{
String stringValue = null;
String propValue = null;

if (GetConfigurationPropertyValue != null)
{
propValue = GetConfigurationPropertyValue(propertyName);
}
var envValue = System.Environment.GetEnvironmentVariable(propertyName);
if (null != propValue && propValue.Trim().Length > 0)
{
stringValue = propValue;
}
else if (null != envValue && envValue.Trim().Length > 0)
{
stringValue = envValue;
}
return stringValue;
}
#endif
}
}
Loading