@@ -69,50 +69,104 @@ public static Hashtable RunTransaction(
69
69
70
70
//Get instance of service
71
71
using ( proc = new NVPTransactionProcessorClient ( currentBinding , endpointAddress ) )
72
- {
73
-
74
- //Set protection level to sign
75
- proc . Endpoint . Contract . ProtectionLevel = System . Net . Security . ProtectionLevel . Sign ;
76
-
72
+ {
77
73
// set the timeout
78
74
TimeSpan timeOut = new TimeSpan ( 0 , 0 , 0 , config . Timeout , 0 ) ;
79
75
currentBinding . SendTimeout = timeOut ;
80
76
81
77
82
78
string keyFilePath = Path . Combine ( config . KeysDirectory , config . EffectiveKeyFilename ) ;
83
- proc . ClientCredentials . ClientCertificate . Certificate = new X509Certificate2 ( keyFilePath , config . EffectivePassword , X509KeyStorageFlags . Exportable | X509KeyStorageFlags . PersistKeySet ) ;
79
+ X509Certificate2 merchantCert = null ;
80
+ X509Certificate2 cybsCert = null ;
81
+ DateTime dateFile = File . GetCreationTime ( keyFilePath ) ;
82
+ if ( config . CertificateCacheEnabled )
83
+ {
84
+ if ( ! merchantIdentities . ContainsKey ( config . MerchantID ) || IsMerchantCertExpired ( config . MerchantID , dateFile . ToFileTimeUtc ( ) , merchantIdentities ) )
85
+ {
86
+ X509Certificate2Collection collection = new X509Certificate2Collection ( ) ;
87
+ collection . Import ( keyFilePath , config . EffectivePassword , X509KeyStorageFlags . MachineKeySet | X509KeyStorageFlags . Exportable | X509KeyStorageFlags . PersistKeySet ) ;
84
88
85
- proc . ClientCredentials . ServiceCertificate . Authentication . CertificateValidationMode = System . ServiceModel . Security . X509CertificateValidationMode . None ;
86
-
87
- // Changes for SHA2 certificates support
88
- X509Certificate2Collection collection = new X509Certificate2Collection ( ) ;
89
- collection . Import ( keyFilePath , config . EffectivePassword , X509KeyStorageFlags . Exportable | X509KeyStorageFlags . PersistKeySet ) ;
90
-
91
- foreach ( X509Certificate2 cert1 in collection )
92
- {
93
- if ( cert1 . Subject . Contains ( config . MerchantID ) )
94
- {
95
- proc . ClientCredentials . ClientCertificate . Certificate = cert1 ;
96
- proc . ClientCredentials . ServiceCertificate . DefaultCertificate = cert1 ;
97
- break ;
98
- }
99
- }
100
-
101
- if ( config . UseSignedAndEncrypted )
102
- {
103
- foreach ( X509Certificate2 cert2 in collection )
104
- {
105
- //Console.WriteLine(cert1.Subject);
106
- if ( cert2 . Subject . Contains ( CYBERSOURCE_PUBLIC_KEY ) )
107
- {
108
- //Set protection level to sign & encrypt only
109
- proc . Endpoint . Contract . ProtectionLevel = System . Net . Security . ProtectionLevel . EncryptAndSign ;
110
- proc . ClientCredentials . ServiceCertificate . DefaultCertificate = cert2 ;
111
- break ;
112
- }
113
- }
89
+ X509Certificate2 newMerchantCert = null ;
90
+ X509Certificate2 newCybsCert = null ;
91
+
92
+ foreach ( X509Certificate2 cert1 in collection )
93
+ {
94
+ if ( cert1 . Subject . Contains ( config . MerchantID ) )
95
+ {
96
+ newMerchantCert = cert1 ;
97
+ }
98
+
99
+ if ( cert1 . Subject . Contains ( CYBS_SUBJECT_NAME ) )
100
+ {
101
+ newCybsCert = cert1 ;
102
+ }
103
+ }
104
+ if ( merchantIdentities . ContainsKey ( config . MerchantID ) )
105
+ {
106
+ merchantIdentities . Remove ( config . MerchantID ) ;
107
+ }
108
+ merchantIdentities . Add ( config . MerchantID , new CertificateEntry ( dateFile . ToFileTimeUtc ( ) , newMerchantCert , newCybsCert ) ) ;
109
+
110
+ }
111
+ merchantCert = GetOrFindValidMerchantCertFromStore ( config . MerchantID , merchantIdentities ) ;
112
+ if ( config . UseSignedAndEncrypted )
113
+ {
114
+ cybsCert = GetOrFindValidCybsCertFromStore ( config . MerchantID , merchantIdentities ) ;
115
+ }
114
116
}
117
+ else
118
+ {
119
+ // Changes for SHA2 certificates support
120
+ X509Certificate2Collection collection = new X509Certificate2Collection ( ) ;
121
+ collection . Import ( keyFilePath , config . EffectivePassword , X509KeyStorageFlags . MachineKeySet | X509KeyStorageFlags . Exportable | X509KeyStorageFlags . PersistKeySet ) ;
115
122
123
+ foreach ( X509Certificate2 cert1 in collection )
124
+ {
125
+ if ( cert1 . Subject . Contains ( config . MerchantID ) )
126
+ {
127
+ merchantCert = cert1 ;
128
+ break ;
129
+ }
130
+ }
131
+
132
+ if ( config . UseSignedAndEncrypted )
133
+ {
134
+ foreach ( X509Certificate2 cert2 in collection )
135
+ {
136
+ //Console.WriteLine(cert1.Subject);
137
+ if ( cert2 . Subject . Contains ( CYBERSOURCE_PUBLIC_KEY ) )
138
+ {
139
+ cybsCert = cert2 ;
140
+ break ;
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+ if ( merchantCert == null )
147
+ {
148
+ throw new ApplicationException (
149
+ "CONFIGURATION OR CODE BUG: merchant certificate is missing, check the p12 file" ) ;
150
+ }
151
+ //Set protection level to sign
152
+ proc . Endpoint . Contract . ProtectionLevel = System . Net . Security . ProtectionLevel . Sign ;
153
+ proc . ClientCredentials . ServiceCertificate . Authentication . CertificateValidationMode = System . ServiceModel . Security . X509CertificateValidationMode . None ;
154
+ proc . ClientCredentials . ClientCertificate . Certificate = merchantCert ;
155
+ proc . ClientCredentials . ServiceCertificate . DefaultCertificate = merchantCert ;
156
+
157
+ if ( config . UseSignedAndEncrypted )
158
+ {
159
+ if ( cybsCert == null )
160
+ {
161
+ throw new ApplicationException (
162
+ "CONFIGURATION OR CODE BUG: cybs certificate is missing, check the p12 file" ) ;
163
+ }
164
+
165
+ //Set protection level to sign & encrypt only
166
+ proc . Endpoint . Contract . ProtectionLevel = System . Net . Security . ProtectionLevel . EncryptAndSign ;
167
+ proc . ClientCredentials . ServiceCertificate . DefaultCertificate = cybsCert ;
168
+ }
169
+
116
170
if ( logger != null )
117
171
{
118
172
logger . LogRequest ( request , config . Demo ) ;
0 commit comments