Newer Version Available
YourGatewayNameUtils
YourGatewayNameUtils is a singleton utility class that interacts with an external
payment gateway. All user gateway API classes will be different based on your needs.
However, we've provided a template that you can use to help create your own.
| Available in: All Salesforce Billing Editions |
This code shows an example of how to generate a void token request for CyberSource.
The class contains several methods.
- A utility method to construct the void token request payload for CyberSource
- A utility method to validate gateway settings
- A utility to return a map of CyberSource Gateway Return code to a friendly description
- A utility method to pass the XML node and retrieve a map of element to value
1public class YourGatewayNameUtils
2{
3
4 // ============================================================================
5 // STATIC VARIABLES
6 // ============================================================================
7
8 // private Attribute to implement singleton pattern for YourGatewayNameUtils class
9 private static YourGatewayNameUtils yourGatewayNameUtilsInstance;
10
11
12
13 /**
14 * @name getInstance
15 * @description get an Instance of Service class
16 * @param NA
17 * @return YourGatewayNameUtils Generator Class Instance
18 */
19 public static YourGatewayNameUtils getInstance() {
20 if (NULL == yourGatewayNameUtilsInstance) {
21 yourGatewayNameUtilsInstance = new YourGatewayNameUtils();
22 }
23 return yourGatewayNameUtilsInstance;
24 }
25
26 /**
27 * @name validateGatewaySettings
28 * @description Validates Gateway Details
29 * @param TransactionParameter
30 * @return NA
31 * @exception NA
32 */
33 public static void validateGatewaySettings(blng.TransactionParameter transactionParameterToProcess) {
34 if (String.IsBlank(transactionParameterToProcess.getGateWay().MerchantId__c)) {
35 transactionParameterToProcess.transactionResult.setError('MerchantId is missing');
36 } else if (String.IsBlank(transactionParameterToProcess.getGateWay().MerchantReference__c)) {
37 transactionParameterToProcess.transactionResult.setError('MerchantReference is missing');
38 } else if (String.IsBlank(transactionParameterToProcess.getGateWay().TransactionSecurityKey__c)) {
39 transactionParameterToProcess.transactionResult.setError('TransactionSecurityKey is missing');
40 }
41 }
42
43 /**
44 * @name getErrorCyberSourceCode
45 * @description Return's Map of Cyber source error description By code
46 * @param NA
47 * @return Map [Key => String [Code] , Value => String [Error Message]]
48 */
49 /// include mapping of all codes based on gateway you are implementing
50 public static map<string, string> getGatewayReturnCode() {
51 Map<string, string> mapOfMessageByErrorCode = new Map<string, string>();
52 mapOfMessageByErrorCode.put('100', 'Successful transaction');
53 mapOfMessageByErrorCode.put('101', 'The request is missing one or more required fields.
54 Possible action: see the reply fields missingField_0...N for which fields are missing. Resend the request with the complete information. For information about missing or invalid fields');
55 mapOfMessageByErrorCode.put('102', 'Invalid data');
56 mapOfMessageByErrorCode.put('151', 'This error does not include timeouts between the client and the server.
57 To avoid duplicating the transaction, do not resend the request until you have reviewed the transaction status at the Business Center');
58 mapOfMessageByErrorCode.put('152', 'The request was received, but a service did not finish running in time.
59 To avoid duplicating the transaction, do not resend the request until you have reviewed the transaction status at the Business Center');
60 mapOfMessageByErrorCode.put('201', 'The issuing bank has questions about the request.
61 You will not receive an authorization code programmatically, but you can obtain one verbally by calling the processor');
62 /// include mapping of all error codes based on gateway you are implementing
63 return mapOfMessageByErrorCode;
64 }
65
66
67 /**
68 * @name getElements
69 * @description Populates's map Of Response Value By Key
70 * @param Dom XML Node
71 * @return Map [Key => String [Name] , Value => String [text]]
72 * @exception NA
73 */
74 public static Map<string, string> getElements(DOM.XMLNode node) {
75 if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
76 if (String.IsNotBlank(node.getText().trim())) {
77 mapOfResponseValueByKey.put(node.getName(), node.getText().trim());
78 }
79
80 for (Dom.XMLNode child : node.getChildElements()) {
81 getElements(child);
82 }
83 }
84 return mapOfResponseValueByKey;
85 }
86
87
88 public void generateToken(List<blng.TransactionParameter> listOfTransactionParameter)
89 {
90 //Frame xml/json for generate token api method
91 }
92
93
94 // Example from Cybersource to generate a voidToken Request
95 public void generateVoidToken(List<blng.TransactionParameter> listOfTransactionParameter)
96 {
97 //Frame xml/json for void token api method
98 XmlStreamWriter writer = new XmlStreamWriter();
99 // Populate Document start
100 writer.writeStartDocument('utf-8', '1.0');
101 // Populate Envelope start
102 writer.writeStartElement('s', 'Envelope', 'http://schemas.xmlsoap.org/soap/envelope/');
103 writer.writeAttribute('xmlns', 'http://schemas.xmlsoap.org/soap/envelope/', 's', 'http://schemas.xmlsoap.org/soap/envelope/');
104 // Populate Header start
105 writer.writeStartElement('s', 'Header', 'http://schemas.xmlsoap.org/soap/envelope/');
106 // Populate Security start
107 writer.writeStartElement('wsse', 'Security', 'http://schemas.xmlsoap.org/soap/envelope/');
108 writer.writeAttribute('xmlns', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
109 // Populate UsernameToken start
110 writer.writeStartElement('wsse', 'UsernameToken', '');
111 // Populate the Username start
112 writer.writeStartElement('wsse', 'Username', '');
113 writer.writeCharacters(transactionParameterToProcess.getGateWay().MerchantId__c);
114 writer.writeEndElement();
115 // Populate the Username end
116 // Populate the Password start
117 writer.writeStartElement('wsse', 'Password', '');
118 writer.writeAttribute(NULL, NULL, 'Type', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText');
119 writer.writeCharacters(transactionParameterToProcess.getGateWay().TransactionSecurityKey__c);
120 writer.writeEndElement();
121 // Populate the Password end
122 writer.writeEndElement();
123 // Populate UsernameToken end
124 writer.writeEndElement();
125 // Populate Security end
126 writer.writeEndElement();
127 // Populate Header end
128 // Populate Body start
129 writer.writeStartElement('s', 'Body', '');
130 // Populate RequestMessage start
131 writer.writeStartElement('', 'requestMessage', '');
132 writer.writeAttribute('', '', 'xmlns', 'urn:schemas-cybersource-com:transaction-data-1.137');
133 // Populate merchantID start
134 writer.writeStartElement('', 'merchantID', '');
135 writer.writeCharacters(transactionParameterToProcess.getGateWay().MerchantId__c);
136 writer.writeEndElement();
137 // Populate merchantID end
138 // Populate merchantReferenceCode start
139 writer.writeStartElement('', 'merchantReferenceCode', '');
140 writer.writeCharacters(transactionParameterToProcess.getGateWay().MerchantReference__c);
141 writer.writeEndElement();
142 // Populate merchantReferenceCode end
143 // Populate recurringSubscriptionInfo start
144 writer.writeStartElement('', 'recurringSubscriptionInfo', '');
145 // Populate subscriptionID start
146 writer.writeStartElement('', 'subscriptionID', '');
147 //Added by DL,W-3931366, for cardmapping
148 writer.writeCharacters(getCardMapping(transactionParameterToProcess.getPaymentMethod()));
149 writer.writeEndElement();
150 // Populate subscriptionID end
151 writer.writeEndElement();
152 // Populate recurringSubscriptionInfo end
153 // Populate paySubscriptionDeleteService start
154 writer.writeStartElement('', 'paySubscriptionDeleteService', '');
155 writer.writeAttribute('', '', 'run', 'true');
156 writer.writeEndElement();
157 // Populate paySubscriptionDeleteService end
158 // Populate RequestMessage end
159 writer.writeEndElement();
160 // Populate Body end
161 writer.writeEndElement();
162 // Populate Envelope end
163 writer.writeEndDocument();
164 transactionParameterToProcess.setRequestBody(writer.getXmlString());
165 writer.close();
166 }
167
168 public void generateAuthorize(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
169 {
170 //Frame xml/json for generate authorize api method
171 }
172
173 public void generateCapture(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
174 {
175 //Frame xml/json for generate capture api method
176 }
177
178 public void generateCharge(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
179 {
180 //Frame xml/json for generate charge api method
181 }
182
183 public void generateVoid(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
184 {
185 //Frame xml/json for generate void api method
186 }
187
188 public void generateRefund(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
189 {
190 //Frame xml/json for generate refund api method
191 }
192
193 public void generatenonReferencedRefund(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
194 {
195 //Frame xml/json for generate non referenced refund api method
196 }
197
198 public void generateVoidRefund(Map<String, blng.TransactionParameter> mapOfTransactionParameterByInvoiceId)
199 {
200 //Frame xml/json for generate void refund api method
201 }
202
203 public void generateGetPaymentStatus(List<blng.TransactionParameter> listOfTransactionParameter)
204 {
205 //Frame xml/json for generate get payment status api method
206 }
207
208 public void generateGetRefundStatus(List<blng.TransactionParameter> listOfTransactionParameter)
209 {
210 //Frame xml/json for generate get refund status api method
211 }
212}