com.safecharge.util.ChecksumUtils Maven / Gradle / Ivy
Show all versions of safecharge-sdk-java Show documentation
package com.safecharge.util;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Copyright (C) 2007-2017 SafeCharge International Group Limited.
*
* Utility class used for calculating the Safecharge's request's checksum. Merchant key can be obtained by contacting
* SafeCharge’s Integration Team
*
* @author Nikola Dichev
* @since 3/21/2017
*/
public class ChecksumUtils {
private static final Log logger = LogFactory.getLog(RequestUtils.class);
private static final Map> requestChecksumOrder = new HashMap>();
private ChecksumUtils() {
}
static {
requestChecksumOrder.put(Constants.ChecksumOrderMapping.USER_DETAILS.name(),
Arrays.asList("firstName", "lastName", "address", "phone", "zip", "city", "countryCode", "state", "email", "county"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.SETTLE_GW_TRANSACTION.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "clientUniqueId", "amount", "currency", "relatedTransactionId", "authCode",
"descriptorMerchantName", "descriptorMerchantPhone", "comment", "urlDetails", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.REFUND_GW_TRANSACTION.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "clientUniqueId", "amount", "currency", "relatedTransactionId", "authCode",
"comment", "urlDetails", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.VOID_GW_TRANSACTION.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "clientUniqueId", "amount", "currency", "relatedTransactionId", "authCode",
"comment", "urlDetails", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.API_GENERIC_CHECKSUM_MAPPING.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "amount", "currency", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.ADD_CASHIER_APM.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "paymentMethodName", "apmData", "billingAddress",
"timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.ADD_CASHIER_CC_CARD.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "ccCardNumber", "ccExpMonth", "ccExpYear",
"ccNameOnCard", "billingAddress", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.URL_DETAILS_CHECKSUM_MAPPING.name(), Arrays.asList(
"successUrl", "failureUrl", "pendingUrl", "notificationUrl"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.CANCEL_CASHIER_SUBSCRIPTION.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "subscriptionId", "userTokenId", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.GET_CASHIER_SUBSCRIPTIONS.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "userTokenId", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.GET_CASHIER_SUBSCRIPTION_PLANS.name(),
Arrays.asList("merchantId", "merchantSiteId", "clientRequestId", "timeStamp"));
//GET_CASHIER_USER_INFO
requestChecksumOrder.put(Constants.ChecksumOrderMapping.GET_CASHIER_USER_INFO.name(),
Arrays.asList("merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.MODIFY_CASHIER_USER.name(),
Arrays.asList("merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "firstName", "lastName", "address", "state",
"city", "zip", "countryCode", "phone", "locale", "email", "county", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.ADD_CASHIER_USER.name(),
Arrays.asList("merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "firstName", "lastName",
"address", "state", "city", "zip", "countryCode", "phone", "locale", "email", "county", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.ADD_CASHIER_CC_CARD_DATA.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "ccExpMonth", "ccExpYear", "ccNameOnCard",
"ccToken", "brand", "uniqueCC", "bin", "last4Digits", "billingAddress", "lastDepositUse", "lastDepositSuccess",
"lastWithdrawalUse", "lastWithdrawalSuccess", "registrationDate", "expiryDate", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.EDIT_CASHIER_CC_CARD.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "userPaymentOptionId", "ccExpMonth",
"ccExpYear", "ccNameOnCard", "billingAddress", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.EDIT_CASHIER_APM.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "userPaymentOptionId", "apmData", "billingAddress", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.DELETE_UPO.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "userPaymentOptionId", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.SUSPEND_UPO.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "userPaymentOptionId", "timeStamp"));
requestChecksumOrder.put(Constants.ChecksumOrderMapping.ENABLE_UPO.name(), Arrays.asList(
"merchantId", "merchantSiteId", "userTokenId", "clientRequestId", "userPaymentOptionId", "timeStamp"));
}
/**
* Calculates the checksum for the provided {@code apiRequest} object.
*
* Note that the {@code apiRequest} object needs to be properly annotated with {@link ValidChecksum} annotation.
*
* @param apiRequest The object containing the request to generate the checksum for
* @param key The merchant key obtained by Safecharge
* @param encoding The character encoding to use when calculating the checksum
* @param hashAlgorithm The hash algorithm to use when calculating the checksum. Check {@link Constants.HashAlgorithm}
* @return The calculated checksum as {@code String} or null if the checksum can not be calculated
*/
public static String calculateChecksum(Object apiRequest, String key, String encoding, Constants.HashAlgorithm hashAlgorithm) {
ValidChecksum annotation = apiRequest.getClass()
.getAnnotation(ValidChecksum.class);
if (annotation == null) {
return null;
}
Constants.ChecksumOrderMapping mappingName = annotation.orderMappingName();
List paramsOrder = requestChecksumOrder.get(mappingName.name());
StringBuilder sb = new StringBuilder();
for (String parameterName : paramsOrder) {
Object fieldValue = getObject(apiRequest, parameterName);
if (fieldValue != null) {
ValidChecksum innerObjectAnnotation = fieldValue.getClass()
.getAnnotation(ValidChecksum.class);
if (innerObjectAnnotation != null) {
sb.append(appendInnerObjectParams(fieldValue, innerObjectAnnotation));
} else if (fieldValue instanceof List) {
sb.append(appendInnerObjectListValues(((List) fieldValue)));
} else if (fieldValue instanceof Map) {
sb.append(appendInnerObjectListValues(((Map