FILTER BY TAG

Using XML

This section explains how to request
Cybersource
services by using XML.

Requesting Cybersource Services

To request Cybersource services, write code that:
  • Collects information for the Cybersource services that you will use
  • Assembles the order information into requests
  • Sends the requests to the Cybersource server
  • Processes the reply information
    The Cybersource servers do not support persistent HTTP connections.
The instructions in this section explain how to write C# programs that request CyberSource services. For a list of API fields to use in your requests, see Related Documents.

Creating a Request Document

The XML client allows you to create an XML request document using any application, then send the request to
Cybersource
. For example, if you have a customer relationship management (CRM) system that uses XML to communicate with other systems, you can use the CRM system to generate request documents.
The request document must validate against the XML schema for
Cybersource
transactions. To view the schema, go to: https://ics2wsa.ic3.com/commerce/1.x/transactionProcessor and look at the
xsd
file for the version of the Simple Order API you are using.
Make sure that the elements in your document appear in the correct order. If they do not, your document will not validate, and your request will fail.
The example that is developed in the following sections shows a basic XML document for requesting
Cybersource
services. In this example, Jane Smith is buying an item for 29.95.
The XML document in this example is incomplete. For complete examples, see
sample.xml
in the client’s
samples\bin
directory.

Creating an Empty Request

Add the XML declaration and the document’s root element:
<?xml version="1.0" encoding="utf-8"?> <requestMessage xmlns="urn:schemas-cybersource-com:transaction-data-1.17"> </requestMessage>
Make sure that the API version specified at the end of the namespace is correct. For example, to communicate with version 1.19, you must use the namespace
urn:schemas-cybersource-com:transaction-data-1.19
. When you must update the API version, see Updating the Client to Use a Later API Version .
The XML document that you receive in the reply always has the prefix
c:
, for example:
xmlns:c="urn:schemas-cybersource-com:transaction-data-1.17"
. Make sure you use an XML parser that supports namespaces.

Adding the Merchant ID

Optionally, you can add the merchant ID to the request:
<?xml version="1.0" encoding="utf-8"?> <requestMessage xmlns="urn:schemas-cybersource-com:transaction-data-1.17">   <merchantID>infodev</merchantID> </requestMessage>
This value overrides any value that you set with the merchantID configuration setting. For more information about the merchantID configuration setting, see Fields in the Settings File. The merchant ID is case sensitive.

Adding Services to the Request

You next indicate the service that you want to use by creating an element for that service in the request, then setting the element’s
run
attribute to
true
. For example, to request a credit card authorization:
<?xml version="1.0" encoding="utf-8"?> <requestMessage xmlns="urn:schemas-cybersource-com:transaction-data-1.15">   <merchantID>infodev</merchantID>   <ccAuthService run="true"/> </requestMessage>

Requesting a Sale

You can request multiple services by creating additional elements. For example, if you fulfill the order immediately, you can request a credit card authorization and capture together (referred to as a “sale”):
<?xml version="1.0" encoding="utf-8"?> <requestMessage xmlns="urn:schemas-cybersource-com:transaction-data-1.17">   <merchantID>infodev</merchantID>   <ccAuthService run="true"/>   <ccCaptureService run="true"/> </requestMessage>

Adding Service-Specific Fields to the Request

You next add the fields that are used by the services you are requesting. Most fields are child elements of container elements; for example, a
&lt;card&gt;
element contains the consumer’s credit card information.
<?xml version="1.0" encoding="utf-8"?> <requestMessage xmlns="urn:schemas-cybersource-com:transaction-data-1.15">   <merchantID>infodev</merchantID>   <billTo>     <firstName>Jane</firstName>     <lastName>Smith</lastName>   </billTo>   <item id="0">   </card>     <unitPrice>29.95</unitPrice>   </item>   <card>     <accountNumber>4111111111111111</accountNumber>   <ccAuthService run="true"/> </requestMessage>
The example above shows only a partial list of the fields you must send. Refer to Related Documents, for information about the guides that list all of the fields for the services that you are requesting.

Sending the Request

Once you have created an XML request document, you can use a .NET application to send the request to
Cybersource
. The example that follows is written in C#.
The code in this section’s examples is incomplete. For complete sample programs, see the source code in the client’s
samples\src\xml
directory.

Creating a New Visual Studio .NET Project

To start, create a new project in Visual Studio .NET. Then you must add a reference to the client library,
CyberSource.Clients.dll
(located in the client’s lib directory) and to the .NET Framework
System.Security.dll
library.

Importing the Client Classes

In the code for your application, add the following import statements:
using CyberSource.Clients; using System; using System.Net; using System.Xml; using System.Security.Cryptography

Sending the Request

You next read the XML request document, send the request to
Cybersource
, store the reply in a new
XmlDocument
object, and catch several exceptions that you might receive:
try {   XmlDocument request = new XmlDocument();   request.Load( "MyXmlDocument.xml" );   XmlDocument reply = XmlClient.RunTransaction( request );   SaveOrderState();   // "Using the Using the Decision and Reason Code Fields describes the ProcessReply   // method.   ProcessReply( reply ); } catch (CryptographicException ce) {   SaveOrderState();   Console.WriteLine( ce.ToString() ); } catch (FaultException fe) {   SaveOrderState();   /*    * Some types of FaultException indicate that the transaction may have been    * completed by CyberSource. The sample code shows how to identify these    * exceptions. If you receive such an exception, and your request included a    * payment service, you should use the Cybersource transaction search screens to    * determine whether the transaction was processed.    */   Console.WriteLine( fe.ToString() ); } catch (WebException we) {   SaveOrderState();   /*    * Some types of WebException indicate that the transaction may have been completed    * by CyberSource. The sample code shows how to identify these exceptions. If you    * receive such an exception, and your request included a payment service, you    * should use the Cybersource transaction search screens to determine whether the    * transaction was processed.    */   Console.WriteLine( we.ToString() ); } private static void SaveOrderState() {   /*    * This is where you store the order state in your system for post-transaction    * analysis. Be sure to store the consumer information, the values of the reply    * fields, and the details of any exceptions that occurred.    */ }
In the preceding example, when an exception occurs, the exception is printed to the console. Your web store should also display a message to the consumer indicating that you were unable to process the order. The sample code for the XML client shows you how to provide feedback to the consumer.
Also, if the transaction fails, and the request did not include any payment services, you may be able to resend the transaction. The sample code for the XML client shows you how to do this.

Interpreting the Reply

After the
Cybersource
server processes your request, it sends a reply message that contains information about the services you requested. You receive different fields depending on the services you request and the outcome of each service.
To use the reply information, you must integrate it into your system and any other system that uses that data. For example, you can store the reply information in a database and send it to other back office applications.
You must write an error handler to process the reply information that you receive from
Cybersource
. Do not show the reply information directly to consumers. Instead, present an appropriate response that tells consumers the result.
Because we may add reply fields and reason codes at any time, you should parse the reply data according to the names of the fields instead of their order in the reply.
The most important reply fields to evaluate are the following:
  • decision
    : A one-word description of the results of your request. The decision is one of the following:
  • reasonCode
    : A numeric code that provides more specific information about the results of your request.
You also receive a reason code for each service in your request. You can use these reason codes to determine whether a specific service succeeded or failed. If a service fails, other services in your request may not run. For example, if you request a credit card authorization and capture, and the authorization fails, the capture does not run. The reason codes for each service are described in .
We reserve the right to add new reason codes at any time. If your error handler receives a reason code that it does not recognize, it should use the decision to interpret the reply.

Using the Decision and Reason Code

The following example shows how you can use the decision and the reason code to display an appropriate message to the consumer.
private static bool ProcessReply( XmlDocument reply ) {   // The following code allows you to use XPath with the Cybersource schema, which   // uses a non-empty default namespace.   XmlNamespaceManager nsmgr     = new XmlNamespaceManager( reply.NameTable );   nsmgr.AddNamespace( "cybs", Client.CYBS_NAMESPACE );   XmlNode replyMessage     = reply.SelectSingleNode( "cybs:replyMessage", nsmgr);   string decision = replyMessage.SelectSingleNode(      "cybs:decision/text()", nsmgr ).Value;   string template = GetTemplate( decision.ToUpper() );   string content  = GetContent( replyMessage, nsmgr );   // This example writes the message to the console. Choose an appropriate display   // method for your own application.   Console.WriteLine( template, content ); } private static string GetTemplate( string decision ) {   // Retrieves the text that corresponds to the decision.   if ("ACCEPT".Equals( decision )) {     return( "The order succeeded.{0}" );   } }   if ("REJECT".Equals( decision )) {     return( "Your order was not approved.{0}" );   }      // ERROR, or an unknown decision   return( "Your order could not be completed at this time.{0}" +           "\nPlease try again later." ); private static string GetContent(    XmlNode replyMessage, XmlNamespaceManager nsmgr ) {   /*    * Uses the reason code to retrieve more details to add to the template.    *    * The messages returned in this example are meant to demonstrate how to retrieve    * the reply fields. Your application should display user-friendly messages.    */   string textVal = replyMessage.SelectSingleNode(      "cybs:reasonCode/text()", nsmgr ).Value;   int reasonCode = int.Parse( textVal );   switch (reasonCode) {     // Success     case 100:       return( "\nRequest ID: " +         replyMessage.SelectSingleNode(            "cybs:requestID/text()", nsmgr ).Value );     // Missing field or fields     case 101:       return( "\nThe following required fields are missing: " +               EnumerateValues( replyMessage.SelectNodes(                 "cybs:missingField/text()", nsmgr ) ) );     // Invalid field or fields     case 102:       return( "\nThe following fields are invalid: " +               EnumerateValues( replyMessage.SelectNodes(                 "cybs:invalidField/text()", nsmgr ) ) );     // Insufficient funds     case 204:       return( "\nInsufficient funds in the account. Please use a " +               "different card or select another form of payment." );     // Add additional reason codes here that you must handle more specifically.     default:       // For all other reason codes (for example, unrecognized reason codes, or       // codes that do not require special handling), return an empty string.       return( String.Empty );   } } private static string EnumerateValues( XmlNodeList nodes ) {   System.Text.StringBuilder sb = new System.Text.StringBuilder();   foreach (XmlNode node in nodes) {     sb.Append( val + "\n" );   }   return( sb.ToString() ); }

For
Cybersource
Advanced Merchants: Handling
Decision Manager
Reviews

The information in this section applies only to
Cybersource
Advanced merchants.
If you use
Decision Manager
, you may also receive the
REVIEW
value in the
decision
field.
REVIEW
means that
Decision Manager
has marked the order for review based on how you configured the
Decision Manager
rules.
If you will be using
Decision Manager
, you have to determine how to handle the new
REVIEW
value. Ideally, you will update your order management system to recognize the
REVIEW
response and handle it according to your business rules. If you cannot update your system to handle the
REVIEW
response, we recommend that you choose one of these options:
  • If you authorize and capture the credit card payment at the same time, treat the
    REVIEW
    response like a
    REJECT
    response. Rejecting any orders that are marked for review may be appropriate if your product is a software download or access to a Web site. If supported by your processor, you may also want to reverse the authorization.
  • If you approve the order after reviewing it, convert the order status to
    ACCEPT
    in your order management system. You can request the credit card capture without requesting a new authorization.
  • If you approve the order after reviewing it but cannot convert the order status to
    ACCEPT
    in your system, request a new authorization for the order. When processing this new authorization, you must disable
    Decision Manager
    . Otherwise the order will be marked for review again. For details about the API field that disables
    Decision Manager
    , see the .
Alternately, you can specify a custom business rule in
Decision Manager
so that authorizations originating from a particular internal IP address at your company are automatically accepted.
If supported by your processor, you may want to reverse the original authorization.

Requesting Multiple Services

When you request multiple services in one request,
Cybersource
processes the services in a specific order. If a service fails,
Cybersource
does not process the subsequent services in the request.
For example, in the case of a sale (a credit card authorization and a capture requested together), if the authorization service fails,
Cybersource
does not process the capture service. The reply you receive only includes reply fields for the authorization.
Many
Cybersource
services include “ignore” fields that tell
Cybersource
to ignore the result from the first service when deciding whether to run the subsequent services. In the case of the sale, even though the issuing bank gives you an authorization code,
Cybersource
might decline the authorization based on the AVS or card verification results. Depending on your business needs, you might choose to capture these types of declined authorizations anyway. You can set the
businessRules_ignoreAVSResult
field to
"true"
in your combined authorization and capture request:
<businessRules>     <ignoreAVSResult>true</ignoreAVSResult> </businessRules>
This tells
Cybersource
to continue processing the capture even if the AVS result causes
Cybersource
to decline the authorization. In this case you would then get reply fields for both the authorization and the capture in your reply.
You are charged only for the services that
Cybersource
performs.

Retrying When System Errors Occur

You must design your transaction management system to include a way to correctly handle
Cybersource
system errors. Depending on which payment processor is handling the transaction, the error may indicate a valid
Cybersource
system error, or it may indicate a processor rejection because of some type of invalid data. In either case, we recommend that you do not design your system to retry sending a transaction many times in the case of a system error.
Instead, we recommend that you retry sending the request only two or three times with successively longer periods of time between each retry. For example, after the first system error response, wait 30 seconds and then retry sending the request. If you receive the same error a second time, wait one minute before you send the request again. Depending on the situation, you may decide you can retry sending the request after a longer time period. Determine what is most appropriate for your business situation.
If after several retry attempts you are still receiving a system error, it is possible that the error is actually being caused by a processor rejection and not a
Cybersource
system error. In that case, we suggest that you either:
  • Search for the transaction in the
    Business Center
    (depending on which one you normally use), look at the description of the error on the Transaction Detail page, and call your processor to determine if and why they are rejecting the transaction.
  • Contact
    Cybersource
    Customer Support to confirm whether your error is truly caused by a
    Cybersource
    system issue.
If
TSYS Acquiring Solutions
is your processor, you may want to follow the first suggestion as there are several common
TSYS Acquiring Solutions
processor responses that are returned to you as system errors and that only
TSYS Acquiring Solutions
can address.

Creating an Application Settings File

After you finish writing code for your integration, you must create an application settings file. This file must contain, at a minimum, the following information:
  • The directory that contains your security key
  • The location of the
    Cybersource
    server
See Table 17, "Fields in the Settings File," for a complete list of settings.
You can use the settings files that come with the sample applications as a starting point for your own settings file. See Configuring the Test Applications, for more information.