Skip to content

Commit

Permalink
Update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TayGov committed Aug 25, 2020
1 parent 5dfc7bf commit 47e5074
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/bambora-payment-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,31 +54,26 @@ 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) {
return MessageFormat.format("{0}{1}={2}", prefix, key, value).replace(" ", "%20");
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}


Expand All @@ -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
Expand Down

0 comments on commit 47e5074

Please sign in to comment.