Newer Version Available
YourGatewayName
YourGateWayName is a base class. 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 class shows an example of a void token calling the CyberSource payment gateway. The gateway receives a request and then provides a response through the TransactionResult object. Salesforce Billing evaluates the response and filters it into one of several fields shown in the payment transaction record's Gateway Status field. For more information on payment transactions and managing gateway responses in Salesforce Billing, check out Managing Payment Transactions.
1Note : Map<String, blng.TransactionParameter> is the parameter in all methods of this class
2 blng.TransactionParameter class is a global class which list of global method exposed detailed in next section
3 and String in the map is a Unique Id which could be Invoice or Account Id
4
5 Map<String, blng.TransactionResult> is the return for all the Methods in this class
6 blng.TransactionResult class is a global class with a list of global method detailed in next section
7 the string should be the same string of parameter
8
9 public class YourGatewayName
10{
11 //Add variables here
12 //Gateway Status related variables
13 private static Map<string, blng.TransactionResult.GatewayStatusType> mapGatewayStatusEnumTypesByStrings =
14 new Map<string, blng.TransactionResult.GatewayStatusType>();
15 static {
16 List<blng.TransactionResult.GatewayStatusType> enumValues = blng.TransactionResult.GatewayStatusType.values();
17 for (Integer i = 0; i < enumValues.size(); i++) {
18 mapGatewayStatusEnumTypesByStrings.put(enumValues.get(i).name(), enumValues.get(i));
19 }
20 }
21
22
23 //
24 // ============================================================================
25 // CONSTANT
26 // ============================================================================
27
28 private static final String ACCEPT = 'ACCEPT';
29 private static final String REJECT = 'REJECT';
30 private static final String SUCCESS = 'SUCCESS';
31 private static final String FAILURE = 'FAILURE';
32 private static final String ERROR = 'ERROR';
33 private static final String DECISION = 'decision';
34
35 /**
36 * @name handleError
37 * @description populates TransactionResult with error values
38 * @Param
39 * @return NA
40 * @exception NA
41 */
42 private void handleError(blng.TransactionResult transactionResult,
43 String pointOfFailure,
44 blng.TransactionResult.GatewayStatusType gatewayStatus,
45 Exception e) {
46 String newErrorMessage = pointOfFailure + (e != null ? ': ' + e.getMessage() : '');
47
48 // if there are previous errors, set the response message by concatenating them in the right order
49 if (!transactionResult.getErrors().isEmpty()) {
50 String prevErrorMessage = '';
51 for (String err : transactionResult.getErrors()) {
52 prevErrorMessage += (String.isEmpty(prevErrorMessage) ? '' : '. ') + err;
53 }
54 transactionResult.setResponseMessage(prevErrorMessage);
55 } else {
56 transactionResult.setResponseMessage(newErrorMessage);
57 }
58
59 // set the rest of the values
60 transactionResult.setIsSuccess(false);
61 transactionResult.setError(newErrorMessage);
62 transactionResult.setResponseToValidate(FAILURE);
63 if (transactionResult.getResponseCode() != null) {
64 populateGatewayStatus(transactionResult);
65 } else {
66 // set the response code to ERROR if it does not exist, which will be in all cases except when the response returns from the gateway
67 transactionResult.setResponseCode(ERROR);
68 transactionResult.setGatewayStatus(gatewayStatus);
69 }
70 }
71
72 /**
73 * @name populateTransactionResultForVoidToken
74 * @description Method to populate Transaction Result for CyberSource Void Token
75 * @param Map[Key => String [unique Id],Value => TransactionParameter]
76 * @return Map[Key => String [unique Id],Value => TransactionResult]
77 */
78 public Map<String, blng.TransactionResult> populateTransactionResultForVoidToken(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
79 {
80 Map<String, blng.TransactionResult> mapOfTransactionResultById = new Map<String, blng.TransactionResult>();
81 for(String idToProcess : mapOfTransactionParameterById.KeySet())
82 {
83 blng.TransactionParameter transactionParameterToProcess;
84 try {
85 transactionParameterToProcess = mapOfTransactionParameterById.get(idToProcess);
86 if (NULL != transactionParameterToProcess.transactionResult.getGatewayStatus()) {
87 mapOfTransactionResultById.put(idToProcess, transactionParameterToProcess.transactionResult);
88 continue;
89 }
90 //Populating the transaction Parameter class based on the response is success or failure
91 if(transactionParameterToProcess.getResponseValueByKey().get(DECISION) == ACCEPT)
92 {
93 //The response is success
94 transactionParameterToProcess.transactionResult.setResponseToValidate(SUCCESS);
95 transactionParameterToProcess.transactionResult.setIsSuccess(true);
96 }
97 else if(transactionParameterToProcess.getResponseValueByKey().get(DECISION) == REJECT)
98 {
99 //The response is failure
100 transactionParameterToProcess.transactionResult.setResponseToValidate(FAILURE);
101 transactionParameterToProcess.transactionResult.setIsSuccess(false);
102 }
103 //Populating payment gateway response
104 transactionParameterToProcess.transactionResult.setId(idToProcess);
105 transactionParameterToProcess.transactionResult.setResponseCode
106 (transactionParameterToProcess.getResponseValueByKey().get(REASONCODE));
107
108 if(NULL != transactionParameterToProcess.transactionResult.getResponseCode())
109 {
110 transactionParameterToProcess.transactionResult.setResponseCodeMessage
111 (YourGatewayNameUtils.getGatewayReturnCode().get(transactionParameterToProcess.transactionResult.getResponseCode()));
112 }
113 //Populating payment gateway response
114 transactionParameterToProcess.transactionResult.setPaymentToken
115 (transactionParameterToProcess.getResponseValueByKey().get(SUBSCRIPTIONID));
116 //Populate gateway status
117 populateGatewayStatus(transactionParameterToProcess.transactionResult);
118 mapOfTransactionResultById.put(idToProcess,transactionParameterToProcess.transactionResult);
119 } catch (Exception e) {
120 handleError(transactionParameterToProcess.transactionResult, FAILED_POPULATING_RESULT + IN_VOID_TOKEN, blng.TransactionResult.GatewayStatusType.SystemError, e);
121 }
122 }
123 //Return map of transaction results
124 return mapOfTransactionResultById;
125 }
126
127
128
129 // default Gateway Status
130 private static final blng.TransactionResult.GatewayStatusType defaultGatewayStatus = blng.TransactionResult.GatewayStatusType.Indeterminate;
131
132 //Generate Token method Salesforce billing interface class will use this class to Generate token
133 public static Map<String, blng.TransactionResult> generateToken(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
134 {
135 //Actual Implementation for Token takes place here
136 }
137
138 //Void Token method Salesforce billing interface class will use this class to void Token Transaction
139 public static Map<String, blng.TransactionResult> voidTokenTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
140 {
141 //Actual Implementation for Void Token takes place here
142 if(!mapOfTransactionParameterById.IsEmpty())
143 {
144 for(blng.TransactionParameter transactionParameterToProcess : mapOfTransactionParameterById.Values())
145 {
146 // validate gateway settings
147 try {
148 YourGatewayNameUtils.validateGatewaySettings(transactionParameterToProcess);
149 if (!transactionParameterToProcess.transactionResult.getErrors().IsEmpty()) {
150 handleError(transactionParameterToProcess.transactionResult, FAILED_GATEWAY_SETTINGS + IN_VOID_TOKEN, blng.TransactionResult.GatewayStatusType.ValidationError, null);
151 continue;
152 }
153 } catch (Exception e) {
154 handleError(transactionParameterToProcess.transactionResult, FAILED_GATEWAY_SETTINGS + IN_VOID_TOKEN, blng.TransactionResult.GatewayStatusType.ValidationError, e);
155 continue;
156 }
157 // Calling YourGatewayNameUtils class to populate the Request for generate void token XML
158 try {
159 YourGatewayNameUtils.getInstance().generateVoidTokenXML(transactionParameterToProcess);
160 } catch (Exception e) {
161 handleError(transactionParameterToProcess.transactionResult, FAILED_XML_GENERATION + IN_VOID_TOKEN, blng.TransactionResult.GatewayStatusType.SystemError, e);
162 continue;
163 }
164 try {
165 // Calling YourHttpService class to send a Request
166 YourHttpService sendHttpRequest = YourHttpService.getInstance();
167 sendHttpRequest.addHeader('Content-type', 'text/xml');
168 sendHttpRequest.setTokenisationHeader(transactionParameterToProcess.getGateWay().MerchantId__c, transactionParameterToProcess.getGateWay().TransactionSecurityKey__c);
169 // Sends the request to payment gateway
170 sendHttpRequest.post(transactionParameterToProcess.getGateWay().TestMode__c ? YOUR_GATEWAY_TEST_ENDPOINT_URL_SANDBOX : YOUR_GATEWAY_TEST_ENDPOINT_PRODUCTION,transactionParameterToProcess.getRequestBody());
171 if(!Test.isRunningTest())
172 {
173 // Populating the map of Response for transaction parameter class from the response received by payment gateway
174 transactionParameterToProcess.mapOfResponseValueByKey.putAll
175 (YourGatewayNameUtils.getElements(sendHttpRequest.getResponse().getBodyDocument().getRootElement()));
176 }
177 else
178 {
179 Dom.Document doc = new Dom.Document();
180 doc.load(TEST_RESPONSE_BODY); // You can provide a string for TEST_RESPONSE_BODY in test mode
181 transactionParameterToProcess.mapOfResponseValueByKey.putAll
182 (YourGatewayNameUtils.getElements(doc.getRootElement()));
183 }
184 } catch (Exception e) {
185 handleError(transactionParameterToProcess.transactionResult, FAILED_CALLOUT + IN_VOID_TOKEN, blng.TransactionResult.GatewayStatusType.SystemError, e);
186 }
187 }
188 }
189 // Calling populate Transaction Result For Void Token class to Return map of transaction results
190 return populateTransactionResultForVoidToken(mapOfTransactionParameterById);
191 }
192
193 //Authorize Transaction method Salesforce billing interface class will use this class to Authorize Transaction
194 public static Map<String, blng.TransactionResult> authorizeTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
195 {
196 //Actual Implementation for Authorize takes place here
197 }
198
199 //Capture Transaction method Salesforce billing interface class will use this class to capture Transaction
200 public static Map<String, blng.TransactionResult> captureTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
201 {
202 //Actual Implementation for capture takes place here
203 }
204
205 //Charge Transaction method Salesforce billing interface class will use this class to Charge Transaction
206 public static Map<String, blng.TransactionResult> chargeTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
207 {
208 //Actual Implementation for Charge takes place here
209 }
210
211 //void Transaction method Salesforce billing interface class will use this class to void Transaction
212 public static Map<String, blng.TransactionResult> voidTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
213 {
214 //Actual Implementation for void Transaction takes place here
215 }
216
217 //RefundTransaction method Salesforce billing interface class will use this class to Refund Transaction
218 public static Map<String, blng.TransactionResult> refundTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
219 {
220 //Actual Implementation for Refund takes place here
221 }
222
223 //Non Referenced Refund method Salesforce billing interface class will use this class to non referenced refund Transaction
224 public static Map<String, blng.TransactionResult> nonReferencedRefund(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
225 {
226 //Actual Implementation for non referenced refund takes place here
227 }
228
229 //void Refund method Salesforce billing interface class will use this class to non void Refund Transaction
230 public static Map<String, blng.TransactionResult> voidRefundTransaction(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
231 {
232 //Actual Implementation for void Refund Transaction takes place here
233 }
234
235 //Get payment status method Salesforce billing interface class will use this class to get payment status Transaction
236 public static Map<String, blng.TransactionResult> getPaymentStatus(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
237 {
238 //Actual Implementation for get payment status takes place here
239 }
240
241 //Get refund status method Salesforce billing interface class will use this class to get refund status Transaction
242 public static Map<String, blng.TransactionResult> getRefundStatus(Map<String, blng.TransactionParameter> mapOfTransactionParameterById)
243 {
244 //Actual Implementation for get refund status takes place here
245 }
246 /**
247 * Does a SOQL lookup on the mapper table and gets the gateway status mapped to the return code
248 * Returns the default enum if no match is found
249 * @param transactionResult
250 */
251 public void populateGatewayStatus(blng.TransactionResult transactionResult) {
252 blng.TransactionResult.GatewayStatusType gatewayStatus = defaultGatewayStatus;
253 // actual implementation of mapping
254 transactionResult.setGatewayStatus(gatewayStatus);
255 }
256}