使用.NET中的Cyber​​Source API取消/退款交易

时间:2011-05-26 14:33:30

标签: .net payment-gateway

我正在使用支付网关的Cyber​​Source API。我浏览了所有可用的文档,并提出了以下代码:

  1. 首先,我使用url向Cyber​​Source添加了服务引用 https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.60.wsdl

  2. 然后我添加了以下用于进行交易的代码,然后取消它。但不知何故,取消似乎不起作用。我不确定我做错了什么,因为网上的文件很少。

    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();
            }
        }
    }
    
  3. 在方法请求退款中,我实际上得到的响应为Request.ReasonCode = 102,这意味着错误。理想情况下,它应为100. 102表示“请求中的一个或多个字段包含无效数据。”。

    非常感谢帮助...

4 个答案:

答案 0 :(得分:3)

试试这个。它对我有用。

            RequestMessage request = new RequestMessage();
            request.ccAuthReversalService = new CCAuthReversalService();
            request.ccAuthReversalService.run = "true";
            request.ccAuthReversalService.authRequestID = order.Transactionno;
            request.ccAuthReversalService.authRequestToken = order.RequestToken;
            request.purchaseTotals = new PurchaseTotals();
            request.purchaseTotals.currency = "USD";
            request.purchaseTotals.grandTotalAmount = (order.Total).ToString("0.00");
            request.merchantID = MerchantId;
            request.merchantReferenceCode = order.OrderNumber;
            TransactionProcessorClient proc = new TransactionProcessorClient();
            proc.ChannelFactory.Credentials.UserName.UserName = request.merchantID;
            proc.ChannelFactory.Credentials.UserName.Password = Key;
            ReplyMessage reply = proc.runTransaction(request);

答案 1 :(得分:1)

如果您已经使用来自初始授权的requestId,则不需要请求令牌。 所以你可以安全地删除这一行:

request.ccAuthReversalService.authRequestToken = order.RequestToken;

此外,buyTotals.currency不是必需的,通过您的requestId Cyber​​Source将为您获取该信息。 保存了另一条线:

request.purchaseTotals.currency = "USD";

答案 2 :(得分:0)

发表作者后 您可以将信用额度过帐到用户帐户,该帐户将向他/她发送该笔款项。 为此,您需要指定request.ccCreditService并将其run字段设置为true。 可以在他们的业务中心UI中完成同样的事情。

然而,这与取消实际的授权/结算交易无关。

答案 3 :(得分:0)

request.ecDebitService = new ECDebitService();
request.ecDebitService.run = "true";

request.ecCreditService = new ECCreditService();
equest.ecCreditService.run = "true";

如果最终用户希望借记一些金额,您可以使用此信用卡和借记卡服务例如:$ 100.00他可以输入 - $ 100.00然后如果总金额小于零,您可以将Credit服务设置为true,反之亦然。< / p>

尝试此操作,如果您需要更多帮助,请随时通过邮件ID与我联系。