1
1
package ca .bc .gov .open .bambora .payment .starter .managment ;
2
2
3
+ import ca .bc .gov .open .bambora .payment .starter .BamboraConstants ;
3
4
import ca .bc .gov .open .bambora .payment .starter .BamboraException ;
4
5
import ca .bc .gov .open .bambora .payment .starter .BamboraProperties ;
5
6
import ca .bc .gov .open .bambora .payment .starter .managment .models .RecurringPaymentDetails ;
6
7
import com .sun .jndi .toolkit .url .Uri ;
8
+ import org .springframework .util .DigestUtils ;
7
9
8
10
import java .net .MalformedURLException ;
11
+ import java .text .MessageFormat ;
12
+ import java .text .SimpleDateFormat ;
13
+ import java .util .Calendar ;
14
+ import java .util .Date ;
9
15
10
16
public class BamboraCardServiceImpl implements BamboraCardService {
11
17
@@ -26,57 +32,53 @@ public Uri setupRecurringPayment(RecurringPaymentDetails recurringPaymentDetails
26
32
27
33
28
34
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
- }
49
35
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 ()));
52
56
53
57
//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 ());
66
61
67
62
// 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 );
69
64
Calendar cal = Calendar .getInstance ();
70
65
cal .setTime (new Date ());
71
- cal .add (Calendar .MINUTE , this . minutesToExpire );
66
+ cal .add (Calendar .MINUTE , bamboraProperties . getMinutesToExpiry () );
72
67
String expiry = sdfDate .format (cal .getTime ());
73
68
74
69
// 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
+ }
78
79
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 ();
81
83
}
82
84
}
0 commit comments