我正在将 First Data 支付网关服务的 API 集成到我的项目中。直到大约 3 周前,我的集成还在工作。我们重新启动了服务器,现在集成不再有效。将我的应用程序发布到开发机器上时,它可以工作,但我们的生产环境在这一点上与服务不一致。
研究错误消息会导致我遇到身份验证服务问题,但没有服务器凭据要发送到我的应用程序正在调用的服务。我认为; 或多或少,这是 IIS 的一个问题,但我有时间试图弄清楚。
我的集成如下...
班级
public class Merchant
{
private readonly string _gatewayId;
private readonly string _password;
private readonly string _keyId;
private readonly string _hmac;
private readonly bool _isDemo;
// POST URLs
private const string ProdUrl = "https://api.globalgatewaye4.firstdata.com/transaction/v14";
private const string TestUrl = "https://api.demo.globalgatewaye4.firstdata.com/transaction/v14";
public Merchant(string gatewayId, string password, string hmac, string keyId, bool isDemo = true)
{
_gatewayId = gatewayId;
_password = password;
_hmac = hmac;
_keyId = keyId;
_isDemo = isDemo;
}
public MerchantResponse Charge(
string orderId, // Order ID / Reference Number
string cardHoldersName, // Card Holder Name
string cardNumber, // Card number
string amount, // Payment amount
string expirationMonth, // Card Exp. Month
string expirationYear, // Card Exp. Year
string ccv, // CCV code
string address, // Address
string zip) // Zip
{
var client = new ServiceSoapClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), new EndpointAddress(_isDemo ? TestUrl : ProdUrl));
client.ChannelFactory.Endpoint.Behaviors.Add(new HmacHeaderBehaivour(_hmac, _keyId));
TransactionResult result = client.SendAndCommit(new Transaction
{
ExactID = _gatewayId,
Password = _password,
Transaction_Type = "00",
Card_Number = cardNumber,
CardHoldersName = cardHoldersName,
DollarAmount = amount,
Expiry_Date = string.Format("{0:D2}{1}", expirationMonth, expirationYear),
Customer_Ref = orderId.ToString(),
//VerificationStr1 = address + "|" + zip + "|" + "US",
//VerificationStr2 = ccv
});
var response = new MerchantResponse
{
IsTransactionApproved = result.Transaction_Approved,
IsError = result.Transaction_Error
};
if (!result.Transaction_Approved && !result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (!result.Transaction_Approved && result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (result.Transaction_Approved)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
return response;
}
class HmacHeaderBehaivour : IEndpointBehavior
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderBehaivour(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new HmacHeaderInspector(_hmac, _keyId));
}
}
class HmacHeaderInspector : IClientMessageInspector
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderInspector(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message msg = buffer.CreateMessage();
ASCIIEncoding encoder = new ASCIIEncoding();
var sb = new StringBuilder();
var xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings
{
OmitXmlDeclaration = true
});
var writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
msg.WriteStartEnvelope(writer);
msg.WriteStartBody(writer);
msg.WriteBodyContents(writer);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
writer.Flush();
string body = sb.ToString().Replace(" />", "/>");
byte[] xmlByte = encoder.GetBytes(body);
SHA1CryptoServiceProvider sha1Crypto = new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(sha1Crypto.ComputeHash(xmlByte)).Replace("-", "");
string hashedContent = hash.ToLower();
//assign values to hashing and header variables
string time = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string hashData = "POST\ntext/xml; charset=utf-8\n" + hashedContent + "\n" + time + "\n/transaction/v14";
//hmac sha1 hash with key + hash_data
HMAC hmacSha1 = new HMACSHA1(Encoding.UTF8.GetBytes(_hmac)); //key
byte[] hmacData = hmacSha1.ComputeHash(Encoding.UTF8.GetBytes(hashData)); //data
//base64 encode on hmac_data
string base64Hash = Convert.ToBase64String(hmacData);
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
}
客户服务参考地址
https://api.globalgatewaye4.firstdata.com/transaction/v14/wsdl
服务参考配置
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceSoap">
<security mode="Transport" />
</binding>
<binding name="ServiceSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://api.globalgatewaye4.firstdata.com/transaction/v14"
binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
contract="FirstDataReference.ServiceSoap" name="ServiceSoap" />
</client>
</system.serviceModel>
完整的堆栈跟踪
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
服务器堆栈跟踪:
HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“”。
在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest 请求,HttpWebResponse 响应,WebException responseException,HttpChannelFactory1 factory)
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory
1 工厂,WebException responseException,ChannelBinding channelBinding) at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel .Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel .Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
在 [0] 处重新引发异常:在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 处的 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 处 MyApplication.FirstDataReference。 ServiceSoap.SendAndCommit(SendAndCommitRequest request) at MyApplication.FirstDataReference.ServiceSoapClient.MyApplication.FirstDataReference.ServiceSoap.SendAndCommit(SendAndCommitRequest request) at MyApplication.FirstData.Merchant.Charge(String orderId, String cardHoldersName, String cardNumber, String amount, String expirationMonth, String expireYear, String ccv, String address, String zip) at MyApplication.controls.Confirmation.Step2SubmitButton_Click(Object sender, EventArgs e)
我想相信当我的应用程序尝试使用服务引用而不是端点自身时会出现问题。