public IActionResult InstaMojoWebHook([FromForm] InstamojoWebhookRequestModel requestModel) { try { if (requestModel != null) { //Console.WriteLine("Payment Request:"+requestModel.amount+","+requestModel.status); helper.UpdateWebHook(requestModel); return(Ok("success")); } return(null); } catch (Exception ex) { } return(null); }
/// <summary> /// Webhook update /// </summary> /// <param name="instaMojoHookResponse"></param> public void UpdateWebHook(InstamojoWebhookRequestModel instaMojoHookResponse) { try { if (_server == null) { InitiateConnection(); } //update the InstamojoTransactionLog database var collection = _kitsuneDB.GetCollection <InstamojoTransactionLog>("InstamojoTransactionLogs"); var udb = new UpdateDefinitionBuilder <InstamojoTransactionLog>(); var update = udb.Set(x => x.status, instaMojoHookResponse.status) .Set(x => x.fees, instaMojoHookResponse.fees) .Set(x => x.mac, instaMojoHookResponse.mac) .Set(x => x.currency, instaMojoHookResponse.currency) .Set(x => x.shorturl, instaMojoHookResponse.shorturl) .Set(x => x.payment_id, instaMojoHookResponse.payment_id); var result = collection.UpdateOne(x => x.payment_request_id == instaMojoHookResponse.payment_request_id, update, new UpdateOptions() { IsUpsert = true }); string walletBalance = null; var userCollection = _kitsuneDB.GetCollection <UserModel>("users"); var pdb = new ProjectionDefinitionBuilder <UserModel>(); var pd = pdb.Include(x => x.Wallet).Include(x => x.Email).Include(x => x.UserName); //get user details var user = userCollection.Find(x => x.UserName == instaMojoHookResponse.buyer).Project <UserModel>(pd).FirstOrDefaultAsync().Result; //if successfully credited then update the wallet if (instaMojoHookResponse.status.ToLower() == "credit") { //update the wallet if (user != null) { var builder = new UpdateDefinitionBuilder <UserModel>(); var currentTime = DateTime.Now; double amount = Double.Parse(instaMojoHookResponse.amount); //if wallet is not present then create one if (user.Wallet != null) { user.Wallet.Balance += amount; user.Wallet.UpdatedOn = currentTime; } else if (user.Wallet == null) { user.Wallet = new Kitsune.Models.Wallet { Balance = amount, UpdatedOn = DateTime.Now } } ; userCollection.UpdateOne((x => x.UserName == instaMojoHookResponse.buyer), builder.Set(x => x.Wallet, user.Wallet)); walletBalance = user.Wallet.Balance.ToString(); //wallet stats var walletCollection = _kitsuneDB.GetCollection <WalletStats>(EnvConstants.Constants.WalletStats); WalletStats walletStats = new WalletStats(); walletStats.Amount = amount; walletStats.IsAdded = true; walletStats.Reason = "Money Added"; walletStats.UserEmail = user.Email; walletStats.CreatedOn = DateTime.Now; walletCollection.InsertOneAsync(walletStats); //KitsuneConversionAPI kitsuneApi = new KitsuneConversionAPI(); EmailHelper emailHelper = new EmailHelper(); Dictionary <string, string> optionalParameters = new Dictionary <string, string>(); optionalParameters.Add(EnvConstants.Constants.EmailParam_AmountAdded, walletStats.Amount.ToString()); optionalParameters.Add(EnvConstants.Constants.EmailParam_WalletAmount, (user.Wallet.Balance - user.Wallet.UnbilledUsage).ToString()); optionalParameters.Add(EnvConstants.Constants.EmailParam_PaymentId, instaMojoHookResponse.payment_id); optionalParameters.Add(EnvConstants.Constants.EmailParam_PaymentPartyName, instaMojoHookResponse.buyer_name); //SendKitsuneConvertEmailRequestModel emailcommand = new SendKitsuneConvertEmailRequestModel() { EmailID = user.Email, UserName = user.UserName, MailType = MailType.Payment_Success, Attachments = null, OptionalParams = optionalParameters }; //kitsuneApi.SendKitsuneConvertEmail(emailCommand); emailHelper.SendGetKitsuneEmail(user.Email, user.UserName, MailType.PAYMENT_SUCCESS, null, optionalParameters); } else { throw new Exception("Couldn't fetch user details"); } } else { //KitsuneConversionAPI kitsuneApi = new KitsuneConversionAPI(); EmailHelper emailHelper = new EmailHelper(); Dictionary <string, string> optionalParameters = new Dictionary <string, string>(); optionalParameters.Add(EnvConstants.Constants.EmailParam_PaymentId, instaMojoHookResponse.payment_id); //SDK.Models.SendKitsuneConvertEmailCommand emailCommand = new SDK.Models.SendKitsuneConvertEmailCommand() { EmailID = user.Email, UserName = user.UserName, MailType = SDK.Models.MailType.Payment_Error, Attachments = null, OptionalParams = optionalParameters }; //kitsuneApi.SendKitsuneConvertEmail(emailCommand); emailHelper.SendGetKitsuneEmail(user.Email, user.UserName, MailType.PAYMENT_ERROR, null, optionalParameters); } //update the PaymentTransactionLog database var mojoLogCollection = _kitsuneDB.GetCollection <PaymentTransactionLog>("PaymentTransactionLogs"); var mojoUdb = new UpdateDefinitionBuilder <PaymentTransactionLog>(); var mojoUpdate = mojoUdb.Set(x => x.Status, instaMojoHookResponse.status) .Set(x => x.UpdatedOn, DateTime.Now); mojoLogCollection.UpdateOne(x => x.PaymentRequestId == instaMojoHookResponse.payment_request_id, mojoUpdate, new UpdateOptions() { IsUpsert = true }); } catch (Exception ex) { //save the log } }