diff --git a/src/bambora-payment-starter/pom.xml b/src/bambora-payment-starter/pom.xml index 207794d..02316ac 100644 --- a/src/bambora-payment-starter/pom.xml +++ b/src/bambora-payment-starter/pom.xml @@ -41,6 +41,11 @@ commons-lang3 + + commons-codec + commons-codec + + diff --git a/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/AutoConfiguration.java b/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/AutoConfiguration.java index f411366..0edb517 100644 --- a/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/AutoConfiguration.java +++ b/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/AutoConfiguration.java @@ -3,8 +3,12 @@ import ca.bc.gov.open.bambora.payment.starter.managment.BamboraCardService; import ca.bc.gov.open.bambora.payment.starter.managment.BamboraCardServiceImpl; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +@Configuration +@EnableConfigurationProperties(BamboraProperties.class) public class AutoConfiguration { private final BamboraProperties bamboraProperties; diff --git a/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImpl.java b/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImpl.java index 3d692c4..8a46071 100644 --- a/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImpl.java +++ b/src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImpl.java @@ -5,7 +5,7 @@ import ca.bc.gov.open.bambora.payment.starter.BamboraProperties; import ca.bc.gov.open.bambora.payment.starter.managment.models.RecurringPaymentDetails; import com.sun.jndi.toolkit.url.Uri; -import org.springframework.util.DigestUtils; +import org.apache.commons.codec.digest.DigestUtils; import java.net.MalformedURLException; import java.text.MessageFormat; @@ -54,23 +54,18 @@ private Uri buildRecurringPaymentUrl(RecurringPaymentDetails recurringPaymentDet if (operationType.equals(BamboraConstants.OperationTypes.M.toString())) paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_CUSTOMER_CODE, recurringPaymentDetails.getEndUserId())); - //add hash key at end of params - paramString.append(bamboraProperties.getHashKey()); + paramString.append(MessageFormat.format("&{0}={1}&{2}={3}", BamboraConstants.PARAM_TRANS_HASH_VALUE, getHash(paramString.toString()), BamboraConstants.PARAM_TRANS_HASH_EXPIRY, getExpiry())); - String hashed = getHash(paramString.toString()); + return new Uri(MessageFormat.format("{0}?{1}", bamboraProperties.getHostedProfileUrl(), paramString.toString())); - // Calculate the expiry based on the minutesToExpire value. + } + + private String getExpiry() { SimpleDateFormat sdfDate = new SimpleDateFormat(BamboraConstants.PARAM_TRANS_HASH_EXPIRY_FORMAT); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.MINUTE, bamboraProperties.getMinutesToExpiry()); - String expiry = sdfDate.format(cal.getTime()); - - // Add hash and expiry to the redirect - String hashedParameter = paramString.toString().replace(bamboraProperties.getHashKey(), MessageFormat.format("&{0}={1}&{2}={3}", BamboraConstants.PARAM_TRANS_HASH_VALUE, hashed, BamboraConstants.PARAM_TRANS_HASH_EXPIRY, expiry)); - - return new Uri(MessageFormat.format("{0}?{1}", bamboraProperties.getHostedProfileUrl(), hashedParameter)); - + return sdfDate.format(cal.getTime()); } private String formatBamboraParam(String prefix, String key, String value) { @@ -78,7 +73,7 @@ private String formatBamboraParam(String prefix, String key, String value) { } private String getHash(String message) { - String digest = DigestUtils.md5DigestAsHex(message.getBytes()); + String digest = DigestUtils.md5Hex(MessageFormat.format("{0}{1}", message, bamboraProperties.getHashKey())); return digest.toUpperCase(); } } diff --git a/src/bambora-payment-starter/src/test/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImplTest.java b/src/bambora-payment-starter/src/test/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImplTest.java index 22d7c7a..f5faf43 100644 --- a/src/bambora-payment-starter/src/test/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImplTest.java +++ b/src/bambora-payment-starter/src/test/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceImplTest.java @@ -18,8 +18,8 @@ public class BamboraCardServiceImplTest { private static final String ECHODATA = "ECHODATA"; private static final String REDIRECTURL = "REDIRECTURL"; private static final String END_USER_ID = "123"; - private static final String BAMBORA_CLIENT_URL = "http://localhost?serviceVersion=HOSTEDPROFILE&merchantId=123&trnLanguage=eng&operationType=M&ref1=ECHODATA&trnReturnURL=REDIRECTURL&trnOrderNumber=REDIRECTURL&customerCode=123&hashValue=98C48E9A9C45B99698ED7D2ED7194A91&hashExpiry=202008241242"; - private static final String BAMBORA_NEW_URL = "http://localhost?serviceVersion=HOSTEDPROFILE&merchantId=123&trnLanguage=eng&operationType=M&ref1=ECHODATA&trnReturnURL=REDIRECTURL&trnOrderNumber=REDIRECTURL&customerCode=123&hashValue=98C48E9A9C45B99698ED7D2ED7194A91&hashExpiry=202008241242"; + private static final String BAMBORA_CLIENT_URL = "http://localhost?serviceVersion=HOSTEDPROFILE&merchantId=123&trnLanguage=eng&operationType=M&ref1=ECHODATA&trnReturnURL=REDIRECTURL&trnOrderNumber=REDIRECTURL&customerCode=123&hashValue=98C48E9A9C45B99698ED7D2ED7194A91"; + private static final String BAMBORA_NEW_URL = "http://localhost?serviceVersion=HOSTEDPROFILE&merchantId=123&trnLanguage=eng&operationType=N&ref1=ECHODATA&trnReturnURL=REDIRECTURL&trnOrderNumber=REDIRECTURL&hashValue=BB2616ADA96F7C72824835D484F23B9B"; private BamboraCardServiceImpl sut; @@ -45,7 +45,7 @@ public void withClientIdCreateUpdateUrl() { Uri actual = sut.setupRecurringPayment(createPaymentDetail(END_USER_ID)); Assertions.assertNotNull(actual); - Assertions.assertEquals(actual.toString(), BAMBORA_CLIENT_URL); + Assertions.assertTrue(actual.toString().contains(BAMBORA_CLIENT_URL)); } @@ -57,7 +57,7 @@ public void withoutClientIdCreateUpdateUrl() { Uri actual = sut.setupRecurringPayment(createPaymentDetail(null)); Assertions.assertNotNull(actual); - Assertions.assertEquals(actual.toString(), BAMBORA_NEW_URL); + Assertions.assertTrue(actual.toString().contains(BAMBORA_NEW_URL)); } @Test