Subversion Repositories SmartDukaan

Rev

Rev 2141 | Rev 2219 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2141 Rev 2199
Line 69... Line 69...
69
	public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId){
69
	public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId){
70
		PaymentServiceClient paymentServiceClient = null;
70
		PaymentServiceClient paymentServiceClient = null;
71
		try {
71
		try {
72
			paymentServiceClient = new PaymentServiceClient();
72
			paymentServiceClient = new PaymentServiceClient();
73
		} catch (Exception e) {
73
		} catch (Exception e) {
74
			log.error("Error while getting payment client");
74
			log.error("Error while getting payment client", e);
75
			e.printStackTrace();
-
 
76
			return PAYMENT_NOT_CREATED;
75
			return PAYMENT_NOT_CREATED;
77
		}
76
		}
78
		
77
		
79
		try {
78
		try {
80
			amount = calculatePaymentAmount(currentCartId);
79
			amount = calculatePaymentAmount(currentCartId);
81
		} catch (ShoppingCartException e1) {
80
		} catch (ShoppingCartException e1) {
82
			log.error("Not able to fetch payment amount from cart id." + e1.getId() + e1.getMessage());
81
			log.error("Unable to fetch payment amount from cart id.", e1);
83
			e1.printStackTrace();
-
 
84
			return PAYMENT_NOT_CREATED;
82
			return PAYMENT_NOT_CREATED;
85
		} catch (TException e1) {
83
		} catch (TException e1) {
86
			log.error("Not able to fetch payment amount." + e1.getMessage());
84
			log.error("Unable to fetch payment amount.", e1);
87
			e1.printStackTrace();
-
 
88
			return PAYMENT_NOT_CREATED;
85
			return PAYMENT_NOT_CREATED;
89
		}
86
		}
90
		
87
		
91
		try {
88
		try {
92
			paymentId = paymentServiceClient.getClient().createPayment(userId, amount, gatewayId, txnId);
89
			paymentId = paymentServiceClient.getClient().createPayment(userId, amount, gatewayId, txnId);
93
			// This is being done to ensure that the amount which we pass on to
90
			// This is being done to ensure that the amount which we pass on to
94
			// the PGs is same as what we have in the database.
91
			// the PGs is same as what we have in the database.
95
			Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
92
			Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
96
			amount = payment.getAmount();
93
			amount = payment.getAmount();
97
		} catch (PaymentException e1) {
94
		} catch (PaymentException e1) {
98
			log.error("Not able to create payment object." + e1.getError_code() + e1.getMessage());
95
			log.error("Unable to create payment object.", e1);
99
			e1.printStackTrace();
-
 
100
			return PAYMENT_NOT_CREATED;
96
			return PAYMENT_NOT_CREATED;
101
		} catch (TException e) {
97
		} catch (TException e) {
102
			log.error("Not able to create payment object." + e.getMessage());
98
			log.error("Not able to create payment object.", e);
103
			e.printStackTrace();
-
 
104
			return PAYMENT_NOT_CREATED;
99
			return PAYMENT_NOT_CREATED;
105
		}
100
		}
106
		
101
		
107
		return true;
102
		return true;
108
	}
103
	}
109
 
104
 
-
 
105
	// TODO: The service client parameters in the processSuccessfulTxn et al are
-
 
106
	// unnecessary but initializing them again when the caller has the necessary
-
 
107
	// references has a performance overhead. Need to think more about this.
-
 
108
 
110
	/**
109
	/**
-
 
110
	 * Processes a successful transaction by:
-
 
111
	 * <ol>
111
	 * Marks the given transaction as in process. Once the transaction is marked
112
	 * <li>Marking the given transaction as 'in process'.</li>
112
	 * as successful, the items in the cart for which this transaction was
113
	 * <li>Removing the items in the cart for which the given transaction was
-
 
114
	 * processed.</li>
-
 
115
	 * <li>Marking the coupon associated with this transaction, if any, as used
-
 
116
	 * for this user.</li>
-
 
117
	 * <li>Queuing the transaction successful email, containing transaction
113
	 * processed are removed from the cart.<br>
118
	 * info, to be sent later by a batch job.</li>
-
 
119
	 * </ol>
-
 
120
	 * <br>
114
	 * Please note that it's possible that a user has added items to the cart
121
	 * Please note that it's possible that a user has added items to the cart
115
	 * and so it's not possible to simply wipe out their cart.<br>
122
	 * and so it's not possible to simply wipe out their cart. Therefore, it's
116
	 * Again, it's important to ensure that we remove only as much quantity of
123
	 * important to ensure that we remove only as much quantity of items as for
117
	 * items as for which the order was processed.
124
	 * which the order was processed.
118
	 * 
125
	 * 
-
 
126
	 * @param txnId
119
	 * @param txnId The transaction which should be marked as successful.
127
	 *            The transaction which should be marked as successful.
-
 
128
	 * @param userServiceClient
120
	 * @param userServiceClient A user context service client to use.
129
	 *            A user context service client to use.
-
 
130
	 * @param transactionServiceClient
121
	 * @param transactionServiceClient A transaction service client to use.
131
	 *            A transaction service client to use.
122
	 */
132
	 */
123
	public static void processSuccessfulTxn(long txnId, UserContextServiceClient userServiceClient, TransactionServiceClient transactionServiceClient) {
133
	public static void processSuccessfulTxn(long txnId, UserContextServiceClient userServiceClient, TransactionServiceClient transactionServiceClient) {
124
		Transaction transaction = null;
134
		Transaction transaction = null;
125
		Map<Long,Double> items = new HashMap<Long, Double>();
-
 
126
		try {
135
		try {
127
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
136
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
128
			transactionClient.changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
137
			transactionClient.changeTransactionStatus(txnId, TransactionStatus.IN_PROCESS, "Payment received for the order");
129
			transaction = transactionClient.getTransaction(txnId);
138
			transaction = transactionClient.getTransaction(txnId);
130
			for(Order order: transaction.getOrders()){
139
			transactionClient.enqueueTransactionInfoEmail(txnId);
131
				for(LineItem lineitem: order.getLineitems()){
-
 
132
					Long itemId = lineitem.getItem_id();
-
 
133
					Double quantity = items.get(itemId);
-
 
134
					if(quantity==null){
-
 
135
						quantity = lineitem.getQuantity();
-
 
136
					}
-
 
137
					else{
-
 
138
						quantity= quantity + lineitem.getQuantity();
-
 
139
					}
-
 
140
					items.put(itemId, quantity);
-
 
141
				}
-
 
142
			}
-
 
143
		} catch (TException e1) {
140
		} catch (TException e1) {
144
			log.error("Unable to update status of transaction. Thrift Exception" + e1.getMessage());
141
			log.error("Unable to update status of transaction. Thrift Exception:", e1);
145
			e1.printStackTrace();
-
 
146
		} catch (TransactionServiceException e) {
142
		} catch (TransactionServiceException e) {
147
			log.error("Unable to update status of transaction. Thrift Exception" + e.getErrorCode() + e.getMessage());
143
			log.error("Unable to update status of transaction. Thrift Exception: ", e);
148
			e.printStackTrace();
-
 
149
		}
-
 
150
        
-
 
151
        try   {
-
 
152
            // Tracking Coupon Usage 
-
 
153
            UserContextService.Client userClient = userServiceClient.getClient();
-
 
154
            Cart cart = userClient.getCart(transaction.getShoppingCartid());
-
 
155
            
-
 
156
            PromotionServiceClient promotionServiceClient = new PromotionServiceClient();
-
 
157
            String couponCode = cart.getCouponCode();
-
 
158
 
-
 
159
            if (couponCode != null && !couponCode.isEmpty()) {
-
 
160
                promotionServiceClient.getClient().trackCouponUsage(couponCode, txnId, transaction.getCustomer_id());
-
 
161
            }
-
 
162
        } catch (ShoppingCartException e) {
-
 
163
            log.error("Error occurred in reading CardId for transaction");
-
 
164
            e.printStackTrace();
-
 
165
        } catch (PromotionException e) {
-
 
166
            log.error("Promotion Exception: " + e.getMessage());
-
 
167
            e.printStackTrace();
-
 
168
        } catch (TException e)  {
-
 
169
            log.error("Transport from Promotion Service failed");
-
 
170
            e.printStackTrace();
-
 
171
        } catch (Exception e) {
-
 
172
            e.printStackTrace();
-
 
173
        }
-
 
174
        
-
 
175
		try {
-
 
176
			//TODO Optimize the function to send less amount of data over network
-
 
177
			log.info("Transaction shopping cart id is: " + transaction.getShoppingCartid());
-
 
178
			log.info("Items to restr in cart are: " + items );
-
 
179
			           
-
 
180
            userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);
-
 
181
			
-
 
182
		}catch (TException e) {
-
 
183
			log.error("Error while updating information in payment database.");
-
 
184
			e.printStackTrace();
-
 
185
			//FIXME Even we get exception we should sent url back to payment gateway. And some thing back channel should be done
-
 
186
			
-
 
187
		} catch (ShoppingCartException e) {
-
 
188
			log.error("Error while reseting the cart in cart database.");
-
 
189
			e.printStackTrace();
-
 
190
		}catch (Exception e) {
-
 
191
            // TODO: handle exception
-
 
192
        }
-
 
193
	}
-
 
194
	
-
 
195
	/**
-
 
196
	 * Enqueues the transaction successful email, containing transaction info,
-
 
197
	 * to be sent later by batch job.
-
 
198
	 * 
-
 
199
	 * @param transactionClient
-
 
200
	 */
-
 
201
	public static void sendTxnEmail(long txnId, TransactionServiceClient transactionServiceClient) {
-
 
202
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
-
 
203
		try {
-
 
204
			transactionClient.enqueueTransactionInfoEmail(txnId);
-
 
205
		} catch (Exception e) {
-
 
206
			log.error("Error while adding email to dispatch queue.");
-
 
207
			e.printStackTrace();
-
 
208
		}
144
		}
-
 
145
		trackCouponUsage(transaction, userServiceClient);
-
 
146
        resetCart(transaction, userServiceClient);
209
	}
147
	}
210
 
148
 
211
	/**
149
	/**
212
	 * Marks a transaction as well as all its orders as failed.
150
	 * Marks a transaction as well as all its orders as failed.
213
	 * 
151
	 * 
Line 218... Line 156...
218
	public static void processFailedTxn(long txnId, TransactionServiceClient transactionServiceClient) {
156
	public static void processFailedTxn(long txnId, TransactionServiceClient transactionServiceClient) {
219
		try {
157
		try {
220
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
158
			in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
221
			transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
159
			transactionClient.changeTransactionStatus(txnId, TransactionStatus.FAILED, "Payment failed for the transaction.");
222
		} catch(TException e){
160
		} catch(TException e){
223
			log.error("Thrift exception while getting information from transaction service.");
161
			log.error("Thrift exception while getting information from transaction service.", e);
224
			e.printStackTrace();
-
 
225
		} catch (TransactionServiceException e) {
162
		} catch (TransactionServiceException e) {
226
			log.error("Error while updating status information in transaction database.");
163
			log.error("Error while updating status information in transaction database.", e);
227
			e.printStackTrace();
-
 
228
		}
164
		}
229
	}
165
	}
230
 
166
 
231
	/**
167
	/**
232
	 * Calculates the amount for the payment required for the given cart.
168
	 * Calculates the amount for the payment required for the given cart.
Line 249... Line 185...
249
			e.printStackTrace();
185
			e.printStackTrace();
250
		}
186
		}
251
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
187
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
252
		cart = userClient.getCart(cartId);
188
		cart = userClient.getCart(cartId);
253
		
189
		
254
		if(cart.getCouponCode() == null || cart.getDiscountedPrice() == 0.0)  {
190
		if(cart.getCouponCode() == null)  {
255
		    List<Line> lineItems = cart.getLines(); 
191
		    List<Line> lineItems = cart.getLines(); 
256
		    
192
		    
257
	        for (Line line : lineItems) {
193
	        for (Line line : lineItems) {
258
	            long productId = line.getItemId();
194
	            long productId = line.getItemId();
259
	            // FIXME: It's expensive to get the price of each item from the
195
	            // FIXME: It's expensive to get the price of each item from the
260
	            // catalog service. We maintain the pricing info in the line items
196
	            // catalog service. We maintain the pricing info in the line items
261
	            // themselves now.
197
	            // themselves now.
262
	            totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
198
	            totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
263
	        }
199
	        }
264
		} else    {
200
		} else {
265
		    totalAmount = cart.getDiscountedPrice();
201
		    totalAmount = cart.getDiscountedPrice();
266
		}
202
		}
267
		
203
		
268
		return totalAmount;
204
		return totalAmount;
269
	}
205
	}
270
 
206
 
-
 
207
	/**
271
	// TODO: The service client parameters are unnecessary but initializing them
208
	 * Removes the items processed through the given transaction from the
-
 
209
	 * shopping cart.
-
 
210
	 * 
-
 
211
	 * @param transaction
272
	// again when the caller has the necessary references has a performance
212
	 *            The transaction whose items have to be removed from the
-
 
213
	 *            shopping cart.
-
 
214
	 * @param userServiceClient
-
 
215
	 */
-
 
216
	private static void resetCart(Transaction transaction, UserContextServiceClient userServiceClient) {
-
 
217
		Map<Long, Double> items = new HashMap<Long, Double>();
-
 
218
		for(Order order: transaction.getOrders()){
-
 
219
			for(LineItem lineitem: order.getLineitems()){
-
 
220
				Long itemId = lineitem.getItem_id();
-
 
221
				Double quantity = items.get(itemId);
-
 
222
				if(quantity==null){
-
 
223
					quantity = lineitem.getQuantity();
-
 
224
				} else {
-
 
225
					quantity= quantity + lineitem.getQuantity();
-
 
226
				}
-
 
227
				items.put(itemId, quantity);
-
 
228
			}
-
 
229
		}
-
 
230
		
273
	// overhead. Need to think more about this.
231
		log.debug("Items to restr in cart are: " + items);
-
 
232
		
-
 
233
		try {
-
 
234
			//TODO Optimize the function to send less data over the wire
-
 
235
            userServiceClient.getClient().resetCart(transaction.getShoppingCartid(), items);
-
 
236
		}catch (TException e) {
-
 
237
			log.error("Error while updating information in payment database.", e);
-
 
238
		} catch (ShoppingCartException e) {
-
 
239
			log.error("Error while reseting the cart in cart database.", e);
-
 
240
		}catch (Exception e) {
-
 
241
			log.error("Unexpected exception", e);
-
 
242
        }
-
 
243
	}
-
 
244
 
-
 
245
	/**
-
 
246
	 * Mark the coupon associated with the given transaction as used.
-
 
247
	 * 
-
 
248
	 * @param transaction
-
 
249
	 *            The transaction to track coupon for.
-
 
250
	 * 
-
 
251
	 * @param userServiceClient
-
 
252
	 *            The user service client instance to use.
-
 
253
	 */
-
 
254
	private static void trackCouponUsage(Transaction transaction, UserContextServiceClient userServiceClient) {
-
 
255
        try {
-
 
256
            // Tracking Coupon Usage 
-
 
257
            UserContextService.Client userClient = userServiceClient.getClient();
-
 
258
            Cart cart = userClient.getCart(transaction.getShoppingCartid());
-
 
259
            String couponCode = cart.getCouponCode();
-
 
260
 
-
 
261
            if (couponCode != null && !couponCode.isEmpty()) {
-
 
262
            	PromotionServiceClient promotionServiceClient = new PromotionServiceClient();
-
 
263
                promotionServiceClient.getClient().trackCouponUsage(couponCode, transaction.getId(), transaction.getCustomer_id());
-
 
264
            }
-
 
265
        } catch (ShoppingCartException e) {
-
 
266
            log.error("Error occurred in reading CardId for transaction", e);
-
 
267
        } catch (PromotionException e) {
-
 
268
            log.error("Promotion Exception: " + e);
-
 
269
        } catch (TException e)  {
-
 
270
            log.error("Transport from Promotion Service failed:", e);
-
 
271
        } catch (Exception e) {
-
 
272
            log.error("Unexpected exception:", e);
-
 
273
        }
-
 
274
	}
274
}
275
}