Subversion Repositories SmartDukaan

Rev

Rev 6443 | Rev 7862 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 chandransh 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
7293 anupam.sin 5
import in.shop2020.model.v1.order.OrderSource;
6050 anupam.sin 6
import in.shop2020.model.v1.order.RechargeOrder;
1905 chandransh 7
import in.shop2020.model.v1.order.Transaction;
8
import in.shop2020.model.v1.order.TransactionServiceException;
9
import in.shop2020.model.v1.order.TransactionStatus;
5527 anupam.sin 10
import in.shop2020.model.v1.order.OrderType;
1981 varun.gupt 11
import in.shop2020.model.v1.user.PromotionException;
1905 chandransh 12
import in.shop2020.model.v1.user.ShoppingCartException;
5527 anupam.sin 13
import in.shop2020.logistics.PickUpType;
2137 chandransh 14
import in.shop2020.payments.Payment;
1905 chandransh 15
import in.shop2020.payments.PaymentException;
6318 rajveer 16
import in.shop2020.payments.PaymentStatus;
3126 rajveer 17
import in.shop2020.thrift.clients.PaymentClient;
18
import in.shop2020.thrift.clients.PromotionClient;
19
import in.shop2020.thrift.clients.TransactionClient;
20
import in.shop2020.thrift.clients.UserClient;
1905 chandransh 21
 
22
import java.util.HashMap;
23
import java.util.Map;
24
 
25
import org.apache.log4j.Logger;
26
import org.apache.thrift.TException;
27
 
28
/**
29
 * This class has methods to be used to process non-gateway-specific aspects of
30
 * payments and transactions.
31
 * 
32
 * @author Chandranshu
33
 * 
34
 */
35
public class CommonPaymentService {
36
 
37
	private static final boolean PAYMENT_NOT_CREATED = false;
38
 
39
	private static Logger log = Logger.getLogger(Class.class);
6318 rajveer 40
	private static long gvGatewayId = 9;
1905 chandransh 41
 
42
	private long paymentId;
43
	private double amount;
6318 rajveer 44
	private boolean isGv = false;
45
	private double gvAmount = 0;
1905 chandransh 46
 
47
	public long getPaymentId() {
48
		return paymentId;
49
	}
50
 
51
	public double getAmount() {
52
		return amount;
53
	}
54
 
55
	/**
56
	 * Creates a payment for the given cart of the given user for the given
57
	 * transaction. Stores the id of the newly created payment and the amount
58
	 * for which this payment was created. They can be retrieved later on using
59
	 * {@link #getPaymentId()}getPaymentId() and {@link #getAmount()}getAmount()
60
	 * methods respectively later on.
61
	 * 
62
	 * @param userId
63
	 *            The user for whom the payment has to be created.
64
	 * @param txnId
65
	 *            The transaction against which the payment has to be created.
66
	 * @param gatewayId
67
	 * @return True if the payment object is successfully created, False
68
	 *         otherwise.
69
	 */
6390 rajveer 70
	public boolean createPayment(long userId, long txnId, int gatewayId){
3126 rajveer 71
		PaymentClient paymentServiceClient = null;
6318 rajveer 72
		in.shop2020.payments.PaymentService.Client pclient;
1905 chandransh 73
		try {
3126 rajveer 74
			paymentServiceClient = new PaymentClient();
6318 rajveer 75
			pclient = paymentServiceClient.getClient();
1905 chandransh 76
		} catch (Exception e) {
2199 chandransh 77
			log.error("Error while getting payment client", e);
1905 chandransh 78
			return PAYMENT_NOT_CREATED;
79
		}
80
 
81
		try {
6390 rajveer 82
			amount = calculatePaymentAmount(txnId);
83
		} catch (TransactionServiceException e1) {
84
			log.error("Unable to fetch payment amount from txn id.", e1);
1905 chandransh 85
			return PAYMENT_NOT_CREATED;
86
		} catch (TException e1) {
2199 chandransh 87
			log.error("Unable to fetch payment amount.", e1);
1905 chandransh 88
			return PAYMENT_NOT_CREATED;
89
		}
90
 
91
		try {
6318 rajveer 92
			if(isGv){
93
				paymentId = pclient.createPayment(userId, gvAmount, gvGatewayId, txnId, false);
94
				pclient.updatePaymentDetails(paymentId, "", "", "SUCCESS", "Payment Received", "", "", "", "", PaymentStatus.SUCCESS, "", null);
95
			}
6050 anupam.sin 96
			paymentId = paymentServiceClient.getClient().createPayment(userId, amount, gatewayId, txnId, false);
2137 chandransh 97
			// This is being done to ensure that the amount which we pass on to
98
			// the PGs is same as what we have in the database.
99
			Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
100
			amount = payment.getAmount();
1905 chandransh 101
		} catch (PaymentException e1) {
2199 chandransh 102
			log.error("Unable to create payment object.", e1);
1905 chandransh 103
			return PAYMENT_NOT_CREATED;
104
		} catch (TException e) {
2199 chandransh 105
			log.error("Not able to create payment object.", e);
1905 chandransh 106
			return PAYMENT_NOT_CREATED;
107
		}
108
 
109
		return true;
110
	}
111
 
2199 chandransh 112
	// TODO: The service client parameters in the processSuccessfulTxn et al are
113
	// unnecessary but initializing them again when the caller has the necessary
114
	// references has a performance overhead. Need to think more about this.
115
 
1905 chandransh 116
	/**
2199 chandransh 117
	 * Processes a successful transaction by:
118
	 * <ol>
3063 chandransh 119
	 * <li>Marking the given transaction as 'authorized'.</li>
2199 chandransh 120
	 * <li>Removing the items in the cart for which the given transaction was
121
	 * processed.</li>
122
	 * <li>Marking the coupon associated with this transaction, if any, as used
123
	 * for this user.</li>
124
	 * <li>Queuing the transaction successful email, containing transaction
125
	 * info, to be sent later by a batch job.</li>
126
	 * </ol>
127
	 * <br>
1905 chandransh 128
	 * Please note that it's possible that a user has added items to the cart
2199 chandransh 129
	 * and so it's not possible to simply wipe out their cart. Therefore, it's
130
	 * important to ensure that we remove only as much quantity of items as for
131
	 * which the order was processed.
1905 chandransh 132
	 * 
2199 chandransh 133
	 * @param txnId
134
	 *            The transaction which should be marked as successful.
135
	 * @param userServiceClient
136
	 *            A user context service client to use.
137
	 * @param transactionServiceClient
138
	 *            A transaction service client to use.
4246 rajveer 139
	 * @param isFlagged
140
	 * 			  If payment is flagged it will be true else false 
1905 chandransh 141
	 */
4246 rajveer 142
	public static void processSuccessfulTxn(long txnId, UserClient userServiceClient, TransactionClient transactionServiceClient, boolean isFlagged) {
1905 chandransh 143
		Transaction transaction = null;
4246 rajveer 144
		TransactionStatus tStatus = TransactionStatus.AUTHORIZED;
145
        String description = "Payment authorized for the order";
146
		// if flag is set, status to be changed to flagged
147
		if(isFlagged){
148
			tStatus = TransactionStatus.FLAGGED;
149
			description = "Payment flagged for the order";
150
		}
1905 chandransh 151
		try {
152
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
7293 anupam.sin 153
			transactionClient.changeTransactionStatus(txnId, tStatus, description, PickUpType.COURIER.getValue(), OrderType.B2C, OrderSource.WEBSITE);
1905 chandransh 154
			transaction = transactionClient.getTransaction(txnId);
2199 chandransh 155
			transactionClient.enqueueTransactionInfoEmail(txnId);
1905 chandransh 156
		} catch (TException e1) {
2199 chandransh 157
			log.error("Unable to update status of transaction. Thrift Exception:", e1);
1905 chandransh 158
		} catch (TransactionServiceException e) {
2199 chandransh 159
			log.error("Unable to update status of transaction. Thrift Exception: ", e);
1905 chandransh 160
		}
2219 varun.gupt 161
		trackCouponUsage(transaction);
2199 chandransh 162
        resetCart(transaction, userServiceClient);
1905 chandransh 163
	}
164
 
165
	/**
166
	 * Marks a transaction as well as all its orders as failed.
167
	 * 
168
	 * @param txnId
169
	 *            The id of the transaction which has to be marked as failed.
170
	 * @param transactionServiceClient
171
	 */
3126 rajveer 172
	public static void processFailedTxn(long txnId, TransactionClient transactionServiceClient) {
1905 chandransh 173
		try {
174
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
7293 anupam.sin 175
			transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.", PickUpType.COURIER.getValue(), OrderType.B2C, OrderSource.WEBSITE);
1905 chandransh 176
		} catch(TException e){
2199 chandransh 177
			log.error("Thrift exception while getting information from transaction service.", e);
1905 chandransh 178
		} catch (TransactionServiceException e) {
2199 chandransh 179
			log.error("Error while updating status information in transaction database.", e);
1905 chandransh 180
		}
181
	}
182
 
3063 chandransh 183
    /**
184
     * Processes a COD transaction by:
185
     * <ol>
186
     * <li>Setting the COD flag of all the orders and moving them to the INIT
187
     * state.
188
     * <li>Marking the given transaction to be in COD_IN_PROCESS state
189
     * <li>Marking the coupon associated with this transaction, if any, as used
190
     * for this user.</li>
191
     * <li>Queuing the transaction successful email, containing transaction
192
     * info, to be sent later by a batch job.</li>
193
     * </ol>
194
     * <br>
195
     * Please note that it's possible that a user has added items to the cart
196
     * and so it's not possible to simply wipe out their cart. Therefore, it's
197
     * important to ensure that we remove only as much quantity of items as for
198
     * which the order was processed.
199
     * 
200
     * @param txnId
201
     *            The COD transaction which should be marked as verification
202
     *            pending.
203
     */
204
	public static void processCodTxn(long txnId){
205
        try {            
3126 rajveer 206
            TransactionClient transactionServiceClient = new TransactionClient();
3063 chandransh 207
            in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
7293 anupam.sin 208
            transactionClient.changeTransactionStatus(txnId, TransactionStatus.COD_IN_PROCESS, "COD payment awaited", PickUpType.COURIER.getValue(), OrderType.B2C, OrderSource.WEBSITE);
3063 chandransh 209
            Transaction transaction = transactionClient.getTransaction(txnId);
210
            transactionClient.enqueueTransactionInfoEmail(txnId);
211
 
3126 rajveer 212
            UserClient userServiceClient = new UserClient();
3063 chandransh 213
            trackCouponUsage(transaction);
214
            resetCart(transaction, userServiceClient);
215
        } catch (TException e1) {
216
            log.error("Unable to update status of transaction. Thrift Exception:", e1);
217
        } catch (TransactionServiceException e) {
218
            log.error("Unable to update status of transaction. Thrift Exception: ", e);
219
        } catch (Exception e) {
220
            log.error("Unable to update status of transaction. Thrift Exception: ", e);
221
        }
222
	}
223
 
1905 chandransh 224
	/**
6390 rajveer 225
	 * Calculates the amount for the payment required for the given transaction.
1905 chandransh 226
	 * 
6390 rajveer 227
	 * @param txnId
228
	 *            Id of the transaction for which this payment amount has to be
1905 chandransh 229
	 *            calculated.
230
	 * @return The total amount for which a payment should be created.
6390 rajveer 231
	 * @throws TransactionServiceException 
1905 chandransh 232
	 * @throws TException
233
	 */
6390 rajveer 234
	private double calculatePaymentAmount(long txnId) throws TransactionServiceException, TException{
1905 chandransh 235
		double totalAmount = 0;
6390 rajveer 236
		TransactionClient transactionServiceClient = null;
1905 chandransh 237
		try {
6390 rajveer 238
			transactionServiceClient = new TransactionClient();
1905 chandransh 239
		} catch (Exception e) {
2942 chandransh 240
			log.error("Unable to initialize user context service client", e);
6390 rajveer 241
			throw new TransactionServiceException(100, "Unable to initialize the user service client");
1905 chandransh 242
		}
6390 rajveer 243
		in.shop2020.model.v1.order.TransactionService.Client tClient = transactionServiceClient.getClient();
244
		Transaction transaction = tClient.getTransaction(txnId);
6412 rajveer 245
		Map<Long, Double> miscCharges = tClient.getMiscCharges(txnId);
246
		System.out.println(miscCharges);
6415 rajveer 247
		if(miscCharges != null & !miscCharges.isEmpty()){
248
			totalAmount = totalAmount + miscCharges.get(1L);
1905 chandransh 249
		}
6390 rajveer 250
		for(Order order: transaction.getOrders()){
251
			totalAmount = totalAmount + order.getTotal_amount();
252
			gvAmount = gvAmount + order.getGvAmount();
253
		}
254
		if(gvAmount>0){
255
			isGv = true;	
256
		}
6431 rajveer 257
		return (totalAmount-gvAmount);
1905 chandransh 258
	}
259
 
2199 chandransh 260
	/**
261
	 * Removes the items processed through the given transaction from the
262
	 * shopping cart.
263
	 * 
264
	 * @param transaction
265
	 *            The transaction whose items have to be removed from the
266
	 *            shopping cart.
267
	 * @param userServiceClient
268
	 */
3126 rajveer 269
	private static void resetCart(Transaction transaction, UserClient userServiceClient) {
2199 chandransh 270
		Map<Long, Double> items = new HashMap<Long, Double>();
271
		for(Order order: transaction.getOrders()){
272
			for(LineItem lineitem: order.getLineitems()){
273
				Long itemId = lineitem.getItem_id();
274
				Double quantity = items.get(itemId);
275
				if(quantity==null){
276
					quantity = lineitem.getQuantity();
277
				} else {
278
					quantity= quantity + lineitem.getQuantity();
279
				}
280
				items.put(itemId, quantity);
281
			}
282
		}
283
 
3063 chandransh 284
		log.debug("Items to reset in cart are: " + items);
2199 chandransh 285
 
286
		try {
287
			//TODO Optimize the function to send less data over the wire
288
            userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);
289
		}catch (TException e) {
290
			log.error("Error while updating information in payment database.", e);
291
		} catch (ShoppingCartException e) {
292
			log.error("Error while reseting the cart in cart database.", e);
293
		}catch (Exception e) {
294
			log.error("Unexpected exception", e);
295
        }
296
	}
297
 
298
	/**
299
	 * Mark the coupon associated with the given transaction as used.
300
	 * 
301
	 * @param transaction
302
	 *            The transaction to track coupon for.
303
	 * 
304
	 * @param userServiceClient
305
	 *            The user service client instance to use.
306
	 */
2219 varun.gupt 307
	private static void trackCouponUsage(Transaction transaction) {
2199 chandransh 308
        try {
2219 varun.gupt 309
            String couponCode = transaction.getCoupon_code();
310
 
2199 chandransh 311
            if (couponCode != null && !couponCode.isEmpty()) {
3126 rajveer 312
            	PromotionClient promotionServiceClient = new PromotionClient();
2199 chandransh 313
                promotionServiceClient.getClient().trackCouponUsage(couponCode, transaction.getId(), transaction.getCustomer_id());
314
            }
315
        } catch (PromotionException e) {
316
            log.error("Promotion Exception: " + e);
317
        } catch (TException e)  {
318
            log.error("Transport from Promotion Service failed:", e);
319
        } catch (Exception e) {
320
            log.error("Unexpected exception:", e);
321
        }
322
	}
6050 anupam.sin 323
 
324
    public boolean createPayment(RechargeOrder rechargeOrder, int gatewayId) {
325
        PaymentClient paymentServiceClient = null;
326
        try {
327
            paymentServiceClient = new PaymentClient();
328
        } catch (Exception e) {
329
            log.error("Error while getting payment client", e);
330
            return PAYMENT_NOT_CREATED;
331
        }
332
 
6443 anupam.sin 333
        amount = rechargeOrder.getTotalAmount() - rechargeOrder.getWalletAmount() - rechargeOrder.getCouponAmount();
6050 anupam.sin 334
 
335
        try {
336
            paymentId = paymentServiceClient.getClient().createPayment(rechargeOrder.getUserId(), amount, gatewayId, rechargeOrder.getTransactionId(), true);
337
            // This is being done to ensure that the amount which we pass on to
338
            // the PGs is same as what we have in the database.
339
            Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
340
            amount = payment.getAmount();
341
        } catch (PaymentException e1) {
342
            log.error("Unable to create payment object.", e1);
343
            return PAYMENT_NOT_CREATED;
344
        } catch (TException e) {
345
            log.error("Not able to create payment object.", e);
346
            return PAYMENT_NOT_CREATED;
347
        }
348
 
349
        return true;
350
 
351
    }
6415 rajveer 352
    public static void main(String[] args) {
353
    	Map<Long, Double> miscCharges = new HashMap<Long, Double>();
354
    	miscCharges.put(1L, 3.988);
355
    	System.out.println(miscCharges);
356
		if(miscCharges != null & !miscCharges.isEmpty()){
357
			System.out.println( miscCharges.get(1L));;
358
		}
359
	}
1905 chandransh 360
}