Cyber​​Source没有WS-Security Header问题

时间:2015-12-16 20:55:33

标签: web-services

我找到了Cyber​​Source Payment Gateway的以下示例代码,但无法让它工作。当我运行示例时,我收到以下错误:FaultException:安全数据:没有WS-Security标头。我已经浏览了网关文档并搜索了所有论坛,但未能找到解决方案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using PaymentGatewayConsole.CyberSourceTest;

namespace PaymentGatewayConsole
{
    class Program
    {
                private const String MERCHANT_ID = "removed";
                private const String TRANSACTION_KEY = "removed";
                private static string REQUEST_TOKEN = string.Empty;
                private static string REQUEST_ID = string.Empty;

        static void Main(string[] args)
        {
            MakePayment(); 

            RequestRefund();
        }

private static void MakePayment()
{
    RequestMessage request = new RequestMessage();
    request.merchantID = MERCHANT_ID;
    // replace request.merchantReferenceCode with reference number for the current transaction.
    request.merchantReferenceCode = "123";
    request.clientLibrary = ".NET WCF";
    request.clientLibraryVersion = Environment.Version.ToString();
    request.clientEnvironment = Environment.OSVersion.Platform + Environment.OSVersion.Version.ToString();

    request.ccAuthService = new CCAuthService();
    request.ccAuthService.run = "true";

    BillTo billTo = new BillTo();
    billTo.firstName = "John";
    billTo.lastName = "Doe";
    billTo.street1 = "1295 Charleston Road";
    billTo.city = "Mountain View";
    billTo.state = "CA";
    billTo.postalCode = "94043";
    billTo.country = "US";
    billTo.email = "null@cybersource.com";
    billTo.ipAddress = "10.7.111.111";
    request.billTo = billTo;

    Card card = new Card();
    card.accountNumber = "4111111111111111";
    card.expirationMonth = "12";
    card.expirationYear = "2020";
    card.cardType = "Visa";
    request.card = card;

    PurchaseTotals purchaseTotals = new PurchaseTotals();
    purchaseTotals.currency = "USD";
    request.purchaseTotals = purchaseTotals;

    request.item = new Item[1];
    Item item = new Item();
    item.id = "0";
    item.unitPrice = "49.00";
    request.item[0] = item;

    try
    {
        TransactionProcessorClient proc = new TransactionProcessorClient();

        proc.ChannelFactory.Credentials.UserName.UserName = request.merchantID;
        proc.ChannelFactory.Credentials.UserName.Password = TRANSACTION_KEY;

        ReplyMessage reply = proc.runTransaction(request);

        REQUEST_ID = reply.requestID;
        REQUEST_TOKEN = reply.requestToken;

        Console.WriteLine("decision = " + reply.decision);
        Console.WriteLine("reasonCode = " + reply.reasonCode);
        Console.WriteLine("requestID = " + reply.requestID);
        Console.WriteLine("requestToken = " + reply.requestToken);
        Console.WriteLine("ccAuthReply.reasonCode = " + reply.ccAuthReply.reasonCode);
    }
    catch (TimeoutException e)
    {
        Console.WriteLine("TimeoutException: " + e.Message + "\n" + e.StackTrace);
    }
    catch (FaultException e)
    {
        Console.WriteLine("FaultException: " + e.Message + "\n" + e.StackTrace);
    }
    catch (CommunicationException e)
    {
        Console.WriteLine("CommunicationException: " + e.Message + "\n" + e.StackTrace);
    }
    Console.ReadLine();
}

/// <summary>
/// Method for requesting refund 
/// </summary>
private static void RequestRefund()
{
    RequestMessage request = new RequestMessage();
    request.merchantID = MERCHANT_ID;
    request.merchantReferenceCode = "123";
    request.clientLibrary = ".NET WCF";
    request.clientLibraryVersion = Environment.Version.ToString();
    request.clientEnvironment = Environment.OSVersion.Platform + Environment.OSVersion.Version.ToString();

    request.ccAuthService = new CCAuthService();
    request.ccAuthService.run = "true";

    //request.ccAuthReversalService = new CCAuthReversalService();
    //request.ccAuthReversalService.run = "true";
    //request.ccAuthReversalService.authRequestID = REQUEST_ID;
    //request.orderRequestToken = REQUEST_TOKEN;
    //request.purchaseTotals = new PurchaseTotals();
    //request.purchaseTotals.currency = "USD";
    //request.purchaseTotals.grandTotalAmount = "10";

    VoidService reqVoid = new VoidService();
    reqVoid.voidRequestID = REQUEST_ID;
    reqVoid.voidRequestToken = REQUEST_TOKEN;
    reqVoid.run = "true";
    request.voidService = reqVoid;

    try
    {
        TransactionProcessorClient proc = new TransactionProcessorClient();
        proc.ChannelFactory.Credentials.UserName.UserName = request.merchantID;
        proc.ChannelFactory.Credentials.UserName.Password = TRANSACTION_KEY;

        ReplyMessage reply = proc.runTransaction(request);

        Console.WriteLine("decision = " + reply.decision);
        Console.WriteLine("reasonCode = " + reply.reasonCode);
        Console.WriteLine("requestID = " + reply.requestID);
        Console.WriteLine("requestToken = " + reply.requestToken);

    }
    catch (TimeoutException e)
    {
        Console.WriteLine("TimeoutException: " + e.Message + "\n" + e.StackTrace);
    }
    catch (FaultException e)
    {
        Console.WriteLine("FaultException: " + e.Message + "\n" + e.StackTrace);
    }
    catch (CommunicationException e)
    {
        Console.WriteLine("CommunicationException: " + e.Message + "\n" + e.StackTrace);

    }
    Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

我得到了同样的错误。 basicHttpBinding中的默认安全模式在web.config的绑定中设置为Transport。一旦我将安全模式更改为TransportWithMessageCredential,此错误就消失了。我知道这个问题有点老了,但我希望这可以帮助别人。

答案 1 :(得分:1)

您需要设置

System.setProperty("axis.ClientConfigFile", 'path/to/cybs.wsdd');

示例:

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
  <globalConfiguration >
   <requestFlow >
    <handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
     <parameter name="action" value="UsernameToken"/>
     <parameter name="passwordType" value="PasswordText"/>
     <parameter name="passwordCallbackClass" value="org.broadleafcommerce.vendor.cybersource.service.CyberSourcePasswordCallbackDecorator"/>
    </handler>
   </requestFlow >
  </globalConfiguration >
</deployment>

我是从https://github.com/BroadleafCommerce/blc-cybersource/blob/master/src/main/resources/CyberSourceDeploy.wsdd

取的