Skip to content

Commit 82a54c7

Browse files
committed
Adding tests
1 parent de6b08c commit 82a54c7

File tree

6 files changed

+170
-45
lines changed

6 files changed

+170
-45
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
package ca.bc.gov.open.bambora.payment.starter;
22

33
public class BamboraConstants {
4+
public static final String PARAM_PPRDIR_SERVICE_VERSION = "serviceVersion";
5+
public static final String PARAM_PPRDIR_MERCHANT_ID = "merchantId";
6+
public static final String PARAM_PPRDIR_LANGUAGE = "trnLanguage";
7+
public static final String PARAM_PPRDIR_OPERATION_TYPE = "operationType";
8+
public static final String PARAM_PPRDIR_RETURN_URL = "trnReturnURL";
9+
public static final String PARAM_PPRDIR_ORDER_NUMBER = "trnOrderNumber";
10+
public static final String PARAM_PPRDIR_CUSTOMER_CODE = "customerCode";
11+
public static final String PARAM_PPRDIR_REF1 = "ref1";
12+
public static final String LANGUAGE_TYPE = "eng";
13+
public static final String PARAM_TRANS_HASH_VALUE = "hashValue";
14+
public static final String PARAM_TRANS_HASH_EXPIRY = "hashExpiry";
15+
public static final String PARAM_TRANS_HASH_EXPIRY_FORMAT = "yyyyMMddkkmm";
16+
17+
18+
public enum OperationTypes {
19+
N, M
20+
}
21+
22+
423
}

src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/BamboraProperties.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class BamboraProperties {
1212
private String hostedProfileUrl;
1313
private String hostedProfileServiceVersion;
1414
private String key;
15+
private int minutesToExpiry;
1516

1617
public String getMerchantId() {
1718
return merchantId;
@@ -28,4 +29,28 @@ public String getHostedProfileUrl() {
2829
public void setHostedProfileUrl(String hostedProfileUrl) {
2930
this.hostedProfileUrl = hostedProfileUrl;
3031
}
32+
33+
public String getHostedProfileServiceVersion() {
34+
return hostedProfileServiceVersion;
35+
}
36+
37+
public void setHostedProfileServiceVersion(String hostedProfileServiceVersion) {
38+
this.hostedProfileServiceVersion = hostedProfileServiceVersion;
39+
}
40+
41+
public String getKey() {
42+
return key;
43+
}
44+
45+
public void setKey(String key) {
46+
this.key = key;
47+
}
48+
49+
public int getMinutesToExpiry() {
50+
return minutesToExpiry;
51+
}
52+
53+
public void setMinutesToExpiry(int minutesToExpiry) {
54+
this.minutesToExpiry = minutesToExpiry;
55+
}
3156
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package ca.bc.gov.open.bambora.payment.starter.managment;
22

3+
import ca.bc.gov.open.bambora.payment.starter.BamboraConstants;
34
import ca.bc.gov.open.bambora.payment.starter.BamboraException;
45
import ca.bc.gov.open.bambora.payment.starter.BamboraProperties;
56
import ca.bc.gov.open.bambora.payment.starter.managment.models.RecurringPaymentDetails;
67
import com.sun.jndi.toolkit.url.Uri;
8+
import org.springframework.util.DigestUtils;
79

810
import java.net.MalformedURLException;
11+
import java.text.MessageFormat;
12+
import java.text.SimpleDateFormat;
13+
import java.util.Calendar;
14+
import java.util.Date;
915

1016
public class BamboraCardServiceImpl implements BamboraCardService {
1117

@@ -26,57 +32,53 @@ public Uri setupRecurringPayment(RecurringPaymentDetails recurringPaymentDetails
2632

2733

2834
private Uri buildRecurringPaymentUrl(RecurringPaymentDetails recurringPaymentDetails) throws MalformedURLException {
29-
String paramString = BeanstreamConstants.PARAM_PPRDIR_SERVICE_VERSION +
30-
"=" + this.hostedProfileServiceVersion +
31-
"&" + BeanstreamConstants.PARAM_PPRDIR_MERCHANT_ID +
32-
"=" + this.merchantId +
33-
"&" + BeanstreamConstants.PARAM_PPRDIR_LANGUAGE +
34-
"=" + BeanstreamConstants.LanguageType.eng.toString() +
35-
"&" + BeanstreamConstants.PARAM_PPRDIR_OPERATION_TYPE +
36-
"=" + opType.toString() +
37-
"&" + BeanstreamConstants.PARAM_PPRDIR_REF1 +
38-
"=" + echoData +
39-
"&" + BeanstreamConstants.PARAM_PPRDIR_RETURN_URL +
40-
"=" + redirectURL +
41-
"&" + BeanstreamConstants.PARAM_PPRDIR_ORDER_NUMBER +
42-
"=" + orderNumber;
43-
44-
// if doing an update, supply tyhe customer code.
45-
if ( (null != opType) && (opType == BeanstreamConstants.OperationTypes.M) ) {
46-
paramString = paramString + "&" + BeanstreamConstants.PARAM_PPRDIR_CUSTOMER_CODE +
47-
"=" + endUserId;
48-
}
4935

50-
//replace spaces with "%20" (do not do a full URL encoding. Doesn't work with beanstream.
51-
paramString = paramString.replace(" ", "%20");
36+
String operationType = (recurringPaymentDetails.getEndUserId() != null ? BamboraConstants.OperationTypes.M.toString() : BamboraConstants.OperationTypes.N.toString());
37+
38+
StringBuilder paramString = new StringBuilder();
39+
40+
paramString.append(formatBamboraParam("", BamboraConstants.PARAM_PPRDIR_SERVICE_VERSION, bamboraProperties.getHostedProfileServiceVersion()));
41+
42+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_MERCHANT_ID, bamboraProperties.getMerchantId()));
43+
44+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_LANGUAGE, BamboraConstants.LANGUAGE_TYPE));
45+
46+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_OPERATION_TYPE, operationType));
47+
48+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_REF1, recurringPaymentDetails.getEchoData()));
49+
50+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_RETURN_URL, recurringPaymentDetails.getRedirectURL()));
51+
52+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_ORDER_NUMBER, recurringPaymentDetails.getRedirectURL()));
53+
54+
if (operationType.equals(BamboraConstants.OperationTypes.M.toString()))
55+
paramString.append(formatBamboraParam("&", BamboraConstants.PARAM_PPRDIR_CUSTOMER_CODE, recurringPaymentDetails.getEndUserId()));
5256

5357
//add hash key at end of params
54-
paramString = paramString + this.key;
55-
56-
// Calculate the MD5 value using the Hash Key set on the Order Settings page (Within Beanstream account).
57-
// How:
58-
// Place the hash key after the last parameter.
59-
// Perform an SHA-1 hash on the text up to the end of the key, then
60-
// Replace the hash key with hashValue=[hash result].
61-
// Add the result to the hosted service url.
62-
// Note: Hash is calculated on the params ONLY.. Does NOT include the hosted payment page url.
63-
// See http://support.beanstream.com/#docs/about-hash-validation.htm?Highlight=hash for more info.
64-
log.log(Level.INFO, "Calculating MD5 on paramString " + paramString);
65-
String hashed = getHash(paramString);
58+
paramString.append(bamboraProperties.getKey());
59+
60+
String hashed = getHash(paramString.toString());
6661

6762
// Calculate the expiry based on the minutesToExpire value.
68-
SimpleDateFormat sdfDate = new SimpleDateFormat(BeanstreamConstants.PARAM_TRANS_HASH_EXPIRY_FORMAT);
63+
SimpleDateFormat sdfDate = new SimpleDateFormat(BamboraConstants.PARAM_TRANS_HASH_EXPIRY_FORMAT);
6964
Calendar cal = Calendar.getInstance();
7065
cal.setTime(new Date());
71-
cal.add(Calendar.MINUTE, this.minutesToExpire);
66+
cal.add(Calendar.MINUTE, bamboraProperties.getMinutesToExpiry());
7267
String expiry = sdfDate.format(cal.getTime());
7368

7469
// Add hash and expiry to the redirect
75-
paramString = paramString.replace(this.key,
76-
"&" + BeanstreamConstants.PARAM_TRANS_HASH_VALUE + "=" + hashed +
77-
"&" + BeanstreamConstants.PARAM_TRANS_HASH_EXPIRY + "=" + expiry);
70+
String hashedParameter = paramString.toString().replace(bamboraProperties.getKey(), MessageFormat.format("&{0}={1}&{2}={3}", BamboraConstants.PARAM_TRANS_HASH_VALUE, hashed, BamboraConstants.PARAM_TRANS_HASH_EXPIRY, expiry));
71+
72+
return new Uri(MessageFormat.format("{0}?{1}", bamboraProperties.getHostedProfileUrl(), hashedParameter));
73+
74+
}
75+
76+
private String formatBamboraParam(String prefix, String key, String value) {
77+
return MessageFormat.format("{0}{1}={2}", prefix, key, value).replace(" ", "%20");
78+
}
7879

79-
String redirect = this.hostedProfileURL + "?" + paramString;
80-
return new Uri("blarg");
80+
private String getHash(String message) {
81+
String digest = DigestUtils.md5DigestAsHex(message.getBytes());
82+
return digest.toUpperCase();
8183
}
8284
}

src/bambora-payment-starter/src/main/java/ca/bc/gov/open/bambora/payment/starter/managment/models/RecurringPaymentDetails.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ protected RecurringPaymentDetails(Builder builder) {
2929
this.redirectURL = builder.redirectURL;
3030
}
3131

32+
public static RecurringPaymentDetails.Builder builder() {
33+
return new RecurringPaymentDetails.Builder();
34+
}
35+
3236
public static class Builder {
3337

3438
private String orderNumber;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ca.bc.gov.open.bambora.payment.starter.managment;
2+
3+
import ca.bc.gov.open.bambora.payment.starter.BamboraException;
4+
import ca.bc.gov.open.bambora.payment.starter.BamboraProperties;
5+
import ca.bc.gov.open.bambora.payment.starter.managment.models.RecurringPaymentDetails;
6+
import com.sun.jndi.toolkit.url.Uri;
7+
import org.junit.jupiter.api.*;
8+
9+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
10+
@DisplayName("Test BamboraCardServiceImpl")
11+
public class BamboraCardServiceImplTest {
12+
private static final String PROFILE_SERVICE_VERSION = "HOSTEDPROFILE";
13+
private static final String HOSTED_PROFILE_URL = "http://localhost";
14+
private static final String KEY = "XTN123TNV123";
15+
private static final String MERCHANT_ID = "123";
16+
private static final int MINUTES_TO_EXPIRY = 12;
17+
private static final String ORDERNUM = "ORDERNUM";
18+
private static final String ECHODATA = "ECHODATA";
19+
private static final String REDIRECTURL = "REDIRECTURL";
20+
private static final String END_USER_ID = "123";
21+
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";
22+
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";
23+
24+
private BamboraCardServiceImpl sut;
25+
26+
private BamboraProperties bamboraProperties;
27+
28+
@BeforeEach
29+
public void init() {
30+
31+
bamboraProperties = new BamboraProperties();
32+
bamboraProperties.setHostedProfileServiceVersion(PROFILE_SERVICE_VERSION);
33+
bamboraProperties.setHostedProfileUrl(HOSTED_PROFILE_URL);
34+
bamboraProperties.setKey(KEY);
35+
bamboraProperties.setMerchantId(MERCHANT_ID);
36+
bamboraProperties.setMinutesToExpiry(MINUTES_TO_EXPIRY);
37+
38+
}
39+
40+
@Test
41+
@DisplayName("With client create update url")
42+
public void withClientIdCreateUpdateUrl() {
43+
sut = new BamboraCardServiceImpl(bamboraProperties);
44+
45+
Uri actual = sut.setupRecurringPayment(createPaymentDetail(END_USER_ID));
46+
47+
Assertions.assertNotNull(actual);
48+
Assertions.assertEquals(actual.toString(), BAMBORA_CLIENT_URL);
49+
}
50+
51+
52+
@Test
53+
@DisplayName("Without client create update url")
54+
public void withoutClientIdCreateUpdateUrl() {
55+
sut = new BamboraCardServiceImpl(bamboraProperties);
56+
57+
Uri actual = sut.setupRecurringPayment(createPaymentDetail(null));
58+
59+
Assertions.assertNotNull(actual);
60+
Assertions.assertEquals(actual.toString(), BAMBORA_NEW_URL);
61+
}
62+
63+
@Test
64+
@DisplayName("With invalid paramter throw invalid url exception")
65+
public void withInvalidParamterThrowException() {
66+
bamboraProperties.setHostedProfileUrl("NOTAURL");
67+
sut = new BamboraCardServiceImpl(bamboraProperties);
68+
Assertions.assertThrows(BamboraException.class, () -> sut.setupRecurringPayment(createPaymentDetail(END_USER_ID)));
69+
}
70+
71+
private RecurringPaymentDetails createPaymentDetail(String endUserId) {
72+
return RecurringPaymentDetails.builder()
73+
.orderNumber(ORDERNUM)
74+
.echoData(ECHODATA)
75+
.redirectURL(REDIRECTURL)
76+
.endUserId(endUserId)
77+
.create();
78+
}
79+
}

src/bambora-payment-starter/src/test/java/ca/bc/gov/open/bambora/payment/starter/managment/BamboraCardServiceTest.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)