Test Setup Failed
Push — master ( 768280...836a7a )
by Oluwole
01:56
created

integrityChecksum()   B

Complexity

Conditions 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 37
rs 8.9919
1
2
/*
3
 * To change this license header, choose License Headers in Project Properties.
4
 * To change this template file, choose Tools | Templates
5
 * and open the template in the editor.
6
 */
7
package com.github.theresasogunle;
8
9
import java.nio.charset.StandardCharsets;
10
import java.security.MessageDigest;
11
import java.util.Arrays;
12
import java.util.Base64;
13
14
import java.util.HashMap;
15
16
/**
17
 *
18
 * @author Theresa
19
 */
20
public class IntegrityChecksum {
21
      
22
        RaveConstant key=new RaveConstant();
23
    private String amount, payment_method,custom_description;
0 ignored issues
show
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring payment_method on a separate line.
Loading history...
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring custom_description on a separate line.
Loading history...
24
    private String custom_logo,country,currency,customer_email; 
0 ignored issues
show
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring country on a separate line.
Loading history...
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring currency on a separate line.
Loading history...
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring customer_email on a separate line.
Loading history...
25
    private String customer_lastname,customer_firstname,customer_phone,txref;
0 ignored issues
show
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring txref on a separate line.
Loading history...
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring customer_firstname on a separate line.
Loading history...
Coding Style introduced by
Declaring several variables on the same line makes your code hard to read. Consider delaring customer_phone on a separate line.
Loading history...
26
     /**
27
    *
28
    * 
29
    * @return String
30
    *
31
    */
32
    
33
    
34
    public String integrityChecksum(){
35
         HashMap payload=new HashMap();
36
         
37
        payload.put("PBFPubKey" , RaveConstant.PUBLIC_KEY);
38
        payload.put("amount" ,this.getAmount());
39
        payload.put("payment_method", this.getPayment_method());
40
        payload.put("custom_description",this.getCustom_description());
41
        payload.put("custom_logo",this.getCustom_logo());
42
        payload.put("country", this.getCountry());
43
        payload.put("currency", this.getCurrency());
44
        payload.put ("customer_email", this.getCustomer_email());
45
        payload.put(  "customer_firstname", this.getCustomer_firstname());
46
        payload.put("customer_lastname", this.getCustomer_lastname());
47
        payload.put("customer_phone", this.getCustomer_phone());
48
        payload.put( "txref",this.getTxref());
49
50
     
51
     
52
       Object[] keys=payload.keySet().toArray();
53
       Arrays.sort(keys);
54
       String hashedPayload = "";
55
56
57
            for (Object key : keys) {
0 ignored issues
show
Comprehensibility introduced by
The variable keyshadows a field with the same name declared in line 22. Consider renaming it.
Loading history...
58
                hashedPayload+=payload.get(key);
0 ignored issues
show
Performance introduced by
String concatenation with + is inefficient. Doing so in a loop may incur a significant performance penalty. Consider using a StringBuilder instead
Loading history...
59
            }
60
              String  hashString  = hashedPayload +  RaveConstant.SECRET_KEY;
61
                String hash_string="";
62
63
                try{
64
                        MessageDigest digest = MessageDigest.getInstance("SHA-256");
65
                        byte[]  hash = digest.digest(hashString.getBytes(StandardCharsets.UTF_8));
66
                        hash_string=Base64.getEncoder().encodeToString(hash);
67
                      //  System.out.println(hash_string);
68
                    }catch(Exception ex){}
0 ignored issues
show
Bug introduced by
Consider removing the empty block or filling it with code. You can also add a comment to explain the empty block.
Loading history...
Best Practice introduced by
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.

When instantiating a new Exception, you can set another Exception as its cause.

See the Oracle documentation on Throwables.

Usage example

throw new Exception("Exception Message", originalException);

Complete Example:

class ReThrowException {
  public static void throwsException() {
        try {
            throw new Exception("I am the original exception");
        } catch (final Exception e) {
            throw new RuntimeException("I am the new exception", e);
        }
    }
    public static void main(String[] args) {
        try {
            throwsException();
        }
        catch (final RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("and my cause is: " + e.getCause().getMessage());
            e.printStackTrace();
        }
    }
}
Loading history...
69
70
                return hash_string;
71
    }
72
73
    /**
74
     * @return the amount
75
     */
76
    public String getAmount() {
77
        return amount;
78
    }
79
80
    /**
81
     * @param amount the amount to set
82
     * @return IntegrityChecksum
83
     */
84
    public IntegrityChecksum setAmount(String amount) {
85
        this.amount = amount;
86
        return this;
87
    }
88
89
    /**
90
     * @return the payment_method
91
     */
92
    public String getPayment_method() {
93
        return payment_method;
94
    }
95
96
    /**
97
     * @param payment_method the payment_method to set
98
     * @return IntegrityChecksum
99
     */
100
    public IntegrityChecksum setPayment_method(String payment_method) {
101
        this.payment_method = payment_method;
102
        return this;
103
    }
104
105
    /**
106
     * @return the custom_description
107
     */
108
    public String getCustom_description() {
109
        return custom_description;
110
    }
111
112
    /**
113
     * @param custom_description the custom_description to set
114
     * @return IntegrityChecksum
115
     */
116
    public IntegrityChecksum setCustom_description(String custom_description) {
117
        this.custom_description = custom_description;
118
        return this;
119
    }
120
121
    /**
122
     * @return the custom_logo
123
     * 
124
     */
125
    public String getCustom_logo() {
126
        return custom_logo;
127
    }
128
129
    /**
130
     * @param custom_logo the custom_logo to set
131
     * @return IntegrityChecksum
132
     */
133
    public IntegrityChecksum setCustom_logo(String custom_logo) {
134
        this.custom_logo = custom_logo;
135
        return this;
136
    }
137
138
    /**
139
     * @return the country
140
     */
141
    public String getCountry() {
142
        return country;
143
    }
144
145
    /**
146
     * @param country the country to set
147
     * @return IntegrityChecksum
148
     */
149
    public IntegrityChecksum setCountry(String country) {
150
        this.country = country;
151
        return this;
152
    }
153
154
    /**
155
     * @return the currency
156
     */
157
    public String getCurrency() {
158
        return currency;
159
    }
160
161
    /**
162
     * @param currency the currency to set
163
     * @return IntegrityChecksum
164
     */
165
    public IntegrityChecksum setCurrency(String currency) {
166
        this.currency = currency;
167
        return this;
168
    }
169
170
    /**
171
     * @return the customer_email
172
     */
173
    public String getCustomer_email() {
174
        return customer_email;
175
    }
176
177
    /**
178
     * @param customer_email the customer_email to set
179
     * @return IntegrityChecksum
180
     */
181
    public IntegrityChecksum setCustomer_email(String customer_email) {
182
        this.customer_email = customer_email;
183
        return this;
184
    }
185
186
    /**
187
     * @return the customer_lastname
188
     */
189
    public String getCustomer_lastname() {
190
        return customer_lastname;
191
    }
192
193
    /**
194
     * @param customer_lastname the customer_lastname to set
195
     * @return IntegrityChecksum
196
     */
197
    public IntegrityChecksum setCustomer_lastname(String customer_lastname) {
198
        this.customer_lastname = customer_lastname;
199
        return this;
200
    }
201
202
    /**
203
     * @return the customer_firstname
204
     */
205
    public String getCustomer_firstname() {
206
        return customer_firstname;
207
    }
208
209
    /**
210
     * @param customer_firstname the customer_firstname to set
211
     * @return IntegrityChecksum
212
     */
213
    public IntegrityChecksum setCustomer_firstname(String customer_firstname) {
214
        this.customer_firstname = customer_firstname;
215
        return this;
216
    }
217
218
    /**
219
     * @return the customer_phone
220
     */
221
    public String getCustomer_phone() {
222
        return customer_phone;
223
    }
224
225
    /**
226
     * @param customer_phone the customer_phone to set
227
     * @return IntegrityChecksum
228
     */
229
    public IntegrityChecksum setCustomer_phone(String customer_phone) {
230
        this.customer_phone = customer_phone;
231
        return this;
232
    }
233
234
    /**
235
     * @return the txref
236
     */
237
    public String getTxref() {
238
        return txref;
239
    }
240
241
    /**
242
     * @param txref the txref to set
243
     * @return IntegrityChecksum
244
     */
245
    public IntegrityChecksum setTxref(String txref) {
246
        this.txref = txref;
247
        return this;
248
    }
249
    
250
  
251
}
252