|
| 1 | +package com.payzippy.sdk; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import com.payzippy.sdk.utils.HashUtil; |
| 7 | +import com.payzippy.sdk.utils.RequestUtil; |
| 8 | +import com.payzippy.sdk.utils.ValidityCheck; |
| 9 | + |
| 10 | +public class ChargingRequest |
| 11 | +{ |
| 12 | + private final Map<String, Object> requestParams; |
| 13 | + |
| 14 | + public ChargingRequest(Map<String, Object> requestParams) |
| 15 | + { |
| 16 | + this.requestParams = requestParams; |
| 17 | + } |
| 18 | + |
| 19 | + public static class Builder implements ChargingRequestBuilder |
| 20 | + { |
| 21 | + private Map<String, Object> requestParams = new HashMap<String, Object>(); |
| 22 | + |
| 23 | + public ChargingRequestBuilder setMerchantId(String merchantId) |
| 24 | + { |
| 25 | + requestParams.put(Constants.MERCHANT_ID, merchantId); |
| 26 | + return this; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + public ChargingRequestBuilder setBuyerEmailId(String buyerEmailId) |
| 31 | + { |
| 32 | + requestParams.put(Constants.BUYER_EMAIL_ADDRESS, buyerEmailId); |
| 33 | + return this; |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public ChargingRequestBuilder setMerchantTransactionId(String merchantTransactionId) |
| 38 | + { |
| 39 | + requestParams.put(Constants.MERCHANT_TRANSACTION_ID, merchantTransactionId); |
| 40 | + return this; |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public ChargingRequestBuilder setPaymentMethod(String paymentMethod) |
| 45 | + { |
| 46 | + requestParams.put(Constants.PAYMENT_METHOD, paymentMethod); |
| 47 | + return this; |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public ChargingRequestBuilder setEmiMonths(String emiMonths) |
| 52 | + { |
| 53 | + requestParams.put(Constants.EMI_MONTHS, emiMonths); |
| 54 | + return this; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public ChargingRequestBuilder setCurrency(String currency) |
| 59 | + { |
| 60 | + requestParams.put(Constants.CURRENCY, currency); |
| 61 | + return this; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public ChargingRequestBuilder setMerchantKeyId(String merchantKeyId) |
| 66 | + { |
| 67 | + requestParams.put(Constants.MERCHANT_KEY_ID, merchantKeyId); |
| 68 | + return this; |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + public ChargingRequestBuilder setBankName(String bankName) |
| 73 | + { |
| 74 | + requestParams.put(Constants.BANK_NAME, bankName); |
| 75 | + return this; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public ChargingRequestBuilder putParams(String key, Object value) |
| 80 | + { |
| 81 | + requestParams.put(key, value); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public ChargingRequestBuilder setUiMode(String uiMode) |
| 86 | + { |
| 87 | + requestParams.put(Constants.UI_MODE, uiMode); |
| 88 | + return this; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + public ChargingRequestBuilder setTransactionAmount(String transactionAmount) |
| 93 | + { |
| 94 | + requestParams.put(Constants.TRANSACTION_AMOUNT, transactionAmount); |
| 95 | + return this; |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public ChargingRequestBuilder setHashMethod(String hashMethod) |
| 100 | + { |
| 101 | + requestParams.put(Constants.HASH_METHOD, hashMethod.toUpperCase()); |
| 102 | + return this; |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public ChargingRequestBuilder setTransactionType(String transactionType) |
| 107 | + { |
| 108 | + requestParams.put(Constants.TRANSACTION_TYPE, transactionType); |
| 109 | + return this; |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + public ChargingRequest build(String secretKey) throws Exception |
| 114 | + { |
| 115 | + // TODO: validity check |
| 116 | + ValidityCheck.validateChargeParams(this.requestParams); |
| 117 | + |
| 118 | + if (!requestParams.containsKey(Constants.HASH)) |
| 119 | + { |
| 120 | + requestParams.put(Constants.HASH, HashUtil.generateHash(requestParams, secretKey, this.requestParams |
| 121 | + .get(Constants.HASH_METHOD).toString())); |
| 122 | + } |
| 123 | + return new ChargingRequest(this.requestParams); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + public static ChargingRequestBuilder getBuilder() |
| 129 | + { |
| 130 | + return new Builder(); |
| 131 | + } |
| 132 | + |
| 133 | + public Map<String, Object> getRequestParams() |
| 134 | + { |
| 135 | + return requestParams; |
| 136 | + } |
| 137 | + |
| 138 | + public String getMerchantId() |
| 139 | + { |
| 140 | + return requestParams.get(Constants.MERCHANT_ID).toString(); |
| 141 | + } |
| 142 | + |
| 143 | + public String getBuyerEmailId() |
| 144 | + { |
| 145 | + return requestParams.get(Constants.BUYER_EMAIL_ADDRESS).toString(); |
| 146 | + } |
| 147 | + |
| 148 | + public String getMerchantTransactionId() |
| 149 | + { |
| 150 | + return requestParams.get(Constants.MERCHANT_TRANSACTION_ID).toString(); |
| 151 | + } |
| 152 | + |
| 153 | + public String getTransactionType() |
| 154 | + { |
| 155 | + return requestParams.get(Constants.TRANSACTION_TYPE).toString(); |
| 156 | + } |
| 157 | + |
| 158 | + // Transaction Amount in paise |
| 159 | + |
| 160 | + public String getTransactionAmount() |
| 161 | + { |
| 162 | + return requestParams.get(Constants.TRANSACTION_AMOUNT).toString(); |
| 163 | + } |
| 164 | + |
| 165 | + public String getPaymentMethod() |
| 166 | + { |
| 167 | + return requestParams.get(Constants.PAYMENT_METHOD).toString(); |
| 168 | + } |
| 169 | + |
| 170 | + public String getBankName() |
| 171 | + { |
| 172 | + return requestParams.get(Constants.BANK_NAME).toString(); |
| 173 | + } |
| 174 | + |
| 175 | + public String getEmiMonths() |
| 176 | + { |
| 177 | + return requestParams.get(Constants.EMI_MONTHS).toString(); |
| 178 | + } |
| 179 | + |
| 180 | + public String getCurrency() |
| 181 | + { |
| 182 | + return requestParams.get(Constants.CURRENCY).toString(); |
| 183 | + } |
| 184 | + |
| 185 | + public String getUiMode() |
| 186 | + { |
| 187 | + return requestParams.get(Constants.UI_MODE).toString(); |
| 188 | + } |
| 189 | + |
| 190 | + public String getHashMethod() |
| 191 | + { |
| 192 | + return requestParams.get(Constants.HASH_METHOD).toString(); |
| 193 | + } |
| 194 | + |
| 195 | + public String getHash() |
| 196 | + { |
| 197 | + return requestParams.get(Constants.HASH).toString(); |
| 198 | + } |
| 199 | + |
| 200 | + public String getMerchantKeyId() |
| 201 | + { |
| 202 | + return requestParams.get(Constants.MERCHANT_KEY_ID).toString(); |
| 203 | + } |
| 204 | + |
| 205 | + public boolean isValid() throws Exception |
| 206 | + { |
| 207 | + return ValidityCheck.validateChargeParams(requestParams); |
| 208 | + } |
| 209 | + |
| 210 | + public String getUrl(String baseUrl) |
| 211 | + { |
| 212 | + return RequestUtil.getUrl(requestParams, baseUrl); |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments