private bool UpdateUserBagsRefund(ProductEN pProduct, List <PersonBagOperatorEN> pPersonBags, int pPersonID) { bool finished = false; int counter = 0; var Operator = ""; try { var empty = pPersonBags.SingleOrDefault(e => e.MobileOperatorID <= 0); pPersonBags.Remove(empty); counter = pPersonBags.Count; var contador = 0; foreach (var item in pPersonBags) { if (contador == 0) { Operator = item.MobileOperatorID.ToString(); } else { Operator = Operator + "," + item.MobileOperatorID.ToString(); } contador++; #region Logs EventViewerLogger.LogInformation(String.Format("UpdateUserBags: Current User Bag: {0}, Operator: {1}, PersonID: {2}", item.UserBalance, item.MobileOperatorName, Convert.ToString(pPersonID))); #endregion } // Decimal newAmount = (!pIsRefund) ? item.UserBalanceAmount - pProduct.PersonDiscount : item.UserBalanceAmount + pProduct.PersonDiscount; int result = topupDAL.UpdateUserBagRefund(pProduct.PersonDiscount, pPersonID, Operator, counter); if (result > 1) { finished = true; } else { finished = false; } } catch (Exception ex) { Console.WriteLine(ex.InnerException); EventViewerLogger.LogError("UpdateUserBags: " + ex.Message); } return(finished); }
public TopupTransactionEN AttemptSendTopup(string pPhone, decimal pAmount, int pPackCode, int MNOId, int CategoryId) { TopupTransactionEN topupResult = new TopupTransactionEN(); string amount; string topupUrl; string textResult; amount = String.Format("{0:0.##}", pAmount); //topupUrl = values.TopupUrl() + pPhone + "/" + amount + "/" + pPackCode; topupUrl = values.TopupUrl() + pPhone + "/" + amount + "/" + MNOId + "/" + CategoryId; try { var request = (HttpWebRequest)WebRequest.Create(topupUrl); request.Timeout = Int32.Parse(ConfigurationManager.AppSettings["SocketTimeout"].ToString()); HttpWebResponse response = request.GetResponse() as HttpWebResponse; EventViewerLogger.LogInformation("SendTopup GATS Response: " + response.ToString()); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream); textResult = streamReader.ReadToEnd(); var serializer = new JavaScriptSerializer(); var serializedResult = serializer.Deserialize <ApiResponse>(textResult); topupResult.Code = serializedResult.resultCode; topupResult.ServiceTransactionID = serializedResult.serviceTransactionId; topupResult.Message = serializedResult.errorMessage; topupResult.RequestURL = topupUrl; topupResult.Response = textResult; if (serializedResult.ExtraParameters == null) { topupResult.Msisdn = pPhone; topupResult.Mno = ""; } else { topupResult.Msisdn = pPhone; topupResult.Mno = serializedResult.ExtraParameters[0].value; } } catch (WebException ex) { if (ex.Response != null) { textResult = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); var serializer = new JavaScriptSerializer(); var serializedResult = serializer.Deserialize <ApiResponse>(textResult); topupResult.Code = serializedResult.resultCode; topupResult.ServiceTransactionID = serializedResult.serviceTransactionId; topupResult.Message = serializedResult.errorMessage; topupResult.RequestURL = topupUrl; topupResult.Response = textResult; EventViewerLogger.LogError("SendTopup WebException: " + ex.ToString()); EventViewerLogger.LogInformation("SendTopup serializedResult: " + serializedResult.errorMessage); } else { topupResult.Code = "NoServerResponse"; topupResult.Message = ""; topupResult.RequestURL = topupUrl; topupResult.Response = ""; } } catch (Exception ex) { topupResult.Code = "Error"; topupResult.Message = ex.Message; topupResult.RequestURL = topupUrl; Console.WriteLine(ex.InnerException); EventViewerLogger.LogError("AttemptSendTopup: " + ex.Message); } return(topupResult); }
/// <summary> /// Answers to a specific topup request by a consumer. If accepted, sends a topup and adds a coin to consumer's game progress. /// </summary> /// <param name="pPerson">Vendor data who makes the topup</param> /// <param name="pRequestID">Topup request ID as in database</param> /// <param name="pAccept">Answer: true if 'accepted' and false if 'denied'</param> /// <returns>Boolean: True if succeed and false if something went wrong</returns> public bool AnswerTopupRequest(PersonEN pPerson, int pRequestID, bool pAccept) { bool result = false; try { //Gets Topup request data var requestData = topupDAL.GetTopupRequestData(pRequestID); requestData.ModDate = DateTime.Now; if (pAccept) { requestData.StatusCode = 1; //Updates request to 'Status = 1' (accepted) if (topupDAL.UpdateTopupRequest(requestData) > 0) { //Validates and fires topup var topupTransaction = SendTopup(pPerson, requestData.OperatorName, requestData.Amount, requestData.TargetPhone, requestData.PackageCode); if (topupTransaction != null) { if (String.Equals(topupTransaction.Message, Values.Ok) && requestData.OperatorName == "Claro") { bool AddCoinByTopUp = gameBL.AddCoinByTopup(requestData.ConsumerID); if (AddCoinByTopUp == true) { var content = CreateEarnedCoinContent(); EventViewerLogger.LogInformation("AnswerTopupRequest: Consumer earned coin by accepted Topup request"); if (content != null) { string title = (content.ContainsKey("title")) ? content["title"] : ""; string message = (content.ContainsKey("message")) ? content["message"] : ""; string pImageURL = ""; //Send Notification var SendNotification = campaignBL.SendEarnedCoinNotification(title, message, pImageURL, requestData.ConsumerPhone); if (SendNotification != null) { result = SendNotification.Where(n => n.Result).FirstOrDefault().Result; } } } else { EventViewerLogger.LogError("AnswerTopupRequest: Coin earned was not added to player tracking"); } result = true; } } } } else { requestData.StatusCode = 2; //Updates request to 'Status = 2' (denied) if (topupDAL.UpdateTopupRequest(requestData) > 0) { //Creates notification content var content = CreateTopupDeniedContent(requestData); if (content != null) { string title = (content.ContainsKey("title")) ? content["title"] : ""; string message = (content.ContainsKey("message")) ? content["message"] : ""; string pImageURL = ""; //Fires notificiation var notifications = campaignBL.SendTopupDeniedNotification(title, message, pImageURL, requestData.ConsumerPhone); if (notifications != null) { result = notifications.Where(n => n.Result).FirstOrDefault().Result; } } } } } catch (Exception ex) { Console.WriteLine(ex.InnerException); EventViewerLogger.LogError("AnswerTopupRequest: " + ex.Message); } return(result); }
public TopupTransactionEN SendTopup(PersonEN pPerson, string pOperatorName, decimal pAmount, string pPhone, int pPackCode) { TopupTransactionEN topupResult = new TopupTransactionEN(); TopupEN topup = new TopupEN(); ProductEN product = new ProductEN(); topup.Operator = pOperatorName; topup.Amount = pAmount; topup.Phone = pPhone; topup.PackageCode = pPackCode; try { var topupData = ValidateTopupData(pPerson, topup, ref product); if (topupData.IsValid) { List <PersonBagOperatorEN> operatorUserBags = topupDAL.GetUserOperatorBags(pPerson.PersonID); //Register initial bags state // var states = RegisterInitialUserBagState(operatorUserBags, product.ProductID); try { //Discounts product amount from user's bag if (UpdateUserBags(product, operatorUserBags, pPerson.PersonID)) { #region AzureSearch //if (!azureSearch.CheckExistingEntry(topupData.Phone)) // azureSearch.InsertEntry(pPerson.PersonID, pOperatorName, pPhone, pPerson.CountryID); //else // azureSearch.ComparePhoneNumber(pPhone, pOperatorName, pPerson.PersonID, pPerson.CountryID); #endregion //Gets response from Topup Service topupResult = topupClient.AttemptSendTopup(pPhone, pAmount, pPackCode, topupData.OperatorID, topupData.CategoryID); GatsTransactionEN transaction = new GatsTransactionEN(); transaction.Request = topupResult.RequestURL; transaction.Response = topupResult.Response; transaction.TransactionID = topupResult.ServiceTransactionID; transaction.ProviderTransactionID = topupResult.ServiceTransactionID; transaction.Amount = pAmount; transaction.CountryID = pPerson.CountryID; transaction.PhoneNumber = long.Parse(pPhone.ToString()); transaction.RegDate = DateTime.Now; transaction.ResponseCode = topupResult.Code; transaction.Paid = true; if (!String.Equals(topupResult.Code, "Success") && !String.Equals(topupResult.Code, "02")) { if (product.PersonDiscount > 0) { //Refunds user balance from updated values var updatedBags = topupDAL.GetUserOperatorBags(pPerson.PersonID); UpdateUserBagsRefund(product, updatedBags, pPerson.PersonID); } } else { topupResult.Message = topupData.Result; } //Updates users bags history //RegisterFinalUserBagState(states, pPerson.PersonID); topupDAL.LogTopupTransaction(transaction, pPerson.PersonID, pPerson.CountryID, topupData.Operator, product.InventoryDiscount, product.PersonDiscount); EventViewerLogger.LogInformation("SendTopup: " + transaction.Response); } } catch (Exception ex) { topupResult.Message = ex.Message; topupResult.Code = "Error"; if (product.PersonDiscount > 0) { //Gets user bags with updated values operatorUserBags = topupDAL.GetUserOperatorBags(pPerson.PersonID); //Refunds user bag if any exception ocurred UpdateUserBagsRefund(product, operatorUserBags, pPerson.PersonID); EventViewerLogger.LogError(String.Format("SendTopup - Inner exception: Refund user bags. PersonID {0}. Amount {1}. Message: {2}", Convert.ToString(pPerson.PersonID), product.PersonDiscount, ex.Message)); //Updates users bags history //RegisterFinalUserBagState(states, pPerson.PersonID); } } } else { //Sets the results according to topup data validation topupResult.Message = topupData.Result; topupResult.Code = topupData.Result; } } catch (Exception ex) { Console.WriteLine(ex.InnerException); EventViewerLogger.LogError("SendTopup: " + ex.Message); } return(topupResult); }