简体   繁体   English

使用 .NET 中的 CyberSource API 取消/退款交易

[英]Cancelling/refund a transaction using CyberSource API in .NET

I am using the CyberSource API for Payment Gateway.我正在使用 CyberSource API 作为支付网关。 I went through all the documentations that are available and came up with the following code:我浏览了所有可用的文档并提出了以下代码:

  1. First I added the service reference to CyberSource using url https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.60.wsdl首先,我使用 url https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.60.ZDD3BDA2F06064AEFDCC0EDE添加了对 CyberSource 的服务引用

  2. Then I added the following code for making a transaction and then cancelling it.然后我添加了以下代码来进行交易然后取消它。 But somehow, cancelling does not seem to work.但不知何故,取消似乎不起作用。 I am not sure what I am doing wrong as there is very little documentation available on the net.我不确定我做错了什么,因为网络上可用的文档很少。

     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(); } } }

In method request refund, i am actually getting the response as Request.ReasonCode = 102 which means an error.在方法请求退款中,我实际上得到了 Request.ReasonCode = 102 的响应,这意味着错误。 Ideally it should be 100. 102 means "One or more fields in the request contains invalid data.".理想情况下它应该是 100。102 表示“请求中的一个或多个字段包含无效数据。”。

Help is deeply appreciated...非常感谢您的帮助...

Try this.尝试这个。 It works for me.这个对我有用。

            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);

The Request Token is not necessary if you're already using the requestId coming from the initial Authorization.如果您已经在使用来自初始授权的 requestId,则不需要请求令牌。 So you can safely remove the line:因此,您可以安全地删除该行:

request.ccAuthReversalService.authRequestToken = order.RequestToken;

Also, purchaseTotals.currency is not necessary, through your requestId CyberSource will get that information for you.此外,purchaseTotals.currency 不是必需的,通过您的 requestId CyberSource 将为您获取该信息。 Another line saved:另一行保存:

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

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

You can use this Credit and Debit service, if end user wants to Debit some amount Eg: $100.00 he can enter - $100.00 and then if the total amount is less than zero you can set Credit service as true and vise versa.您可以使用此贷记和借记服务,如果最终用户想要借记一些金额,例如:100.00 美元,他可以输入 - 100.00 美元,然后如果总金额小于零,您可以将贷记服务设置为真,反之亦然。

Try this and if u need more help feel free to contact me on mail id.试试这个,如果您需要更多帮助,请随时通过邮件 ID 与我联系。

Once you posted author You can post a credit amount to users account which will send him/her that money.一旦您发布了作者,您可以向用户帐户发布信用金额,这将向他/她发送这笔钱。 To do that you need to specify request.ccCreditService and set it's run field to true.为此,您需要指定 request.ccCreditService 并将其运行字段设置为 true。 Same thing can be done in their Business Center UI.同样的事情也可以在他们的 Business Center UI 中完成。

However this has nothing to do with cancelling the actual authorization/settlement transaction.但是,这与取消实际授权/结算交易无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM