/// <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 } }
public void ProcessPaymentRequest(string emailId, double amount, string currency, string token, string redirectUrl) { if (_server == null) { InitiateConnection(); } this.emailId = emailId; UserModel developer_model = new UserModel(); try { if (amount < 10) { throw new Exception("amount too low, it must be atleast Rs 10"); } } catch (Exception e) { Console.WriteLine(e); throw new Exception(e.Message); } try { var collection = _kitsuneDB.GetCollection <BsonDocument>("users"); var filter = Builders <BsonDocument> .Filter.Eq("Email", emailId); if (collection.Find(filter).Count() != 1) { throw new Exception("dev_id doesn't exist"); } BsonDocument doc = collection.Find(filter).First(); developer_model = BsonSerializer.Deserialize <UserModel>(doc); } catch (Exception e) { Console.WriteLine(e); throw new Exception("Unable to fetch user details"); } #region Payment request to create the charge try { StripeConfiguration.SetApiKey(EnvConstants.Constants.StripeAPISecretKey); var options = new ChargeCreateOptions { Amount = Convert.ToInt64(amount), Currency = currency, StatementDescriptor = "kitsune Recharge", SourceId = token, ReceiptEmail = developer_model.Email, }; var service = new ChargeService(); Charge charge = service.Create(options); try { if (charge.Paid) { //Step1 : Set PaymentTransactionLog PaymentTransactionLog paymentTransactionLog = new PaymentTransactionLog() { _id = ObjectId.GenerateNewId().ToString(), PaymentRequestId = charge.Id, Status = "Credit", Currency = charge.Currency, Amount = options.Amount.Value / 100, UserProfileId = options.ReceiptEmail, InvoiceId = "KINV-" + GenerateInvoiceId(), CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now }; var json = JsonConvert.SerializeObject(paymentTransactionLog); var document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(json); var collection = _kitsuneDB.GetCollection <BsonDocument>("PaymentTransactionLogs"); collection.InsertOneAsync(document).Wait(); //Step2 : Update User wallet if (developer_model != null) { var builder = new UpdateDefinitionBuilder <UserModel>(); var currentTime = DateTime.Now; double paid_amount = options.Amount.Value / 100; if (String.Compare(charge.Currency, "usd", StringComparison.InvariantCultureIgnoreCase) == 0) { paid_amount = paid_amount * 69; } else if (String.Compare(charge.Currency, "bdt", StringComparison.InvariantCultureIgnoreCase) == 0) { paid_amount = paid_amount * 0.8; } else if (String.Compare(charge.Currency, "sgd", StringComparison.InvariantCultureIgnoreCase) == 0) { paid_amount = paid_amount * 50; } else if (String.Compare(charge.Currency, "thb", StringComparison.InvariantCultureIgnoreCase) == 0) { paid_amount = paid_amount * 2; } //if wallet is not present then create one if (developer_model.Wallet != null) { developer_model.Wallet.Balance += paid_amount; developer_model.Wallet.UpdatedOn = currentTime; } else if (developer_model.Wallet == null) { developer_model.Wallet = new Kitsune.Models.Wallet { Balance = paid_amount, UpdatedOn = DateTime.Now } } ; var userCollection = _kitsuneDB.GetCollection <UserModel>("users"); userCollection.UpdateOne((x => x.UserName == charge.ReceiptEmail), builder.Set(x => x.Wallet, developer_model.Wallet)); // walletBalance = developer_model.Wallet.Balance.ToString(); //wallet stats var walletCollection = _kitsuneDB.GetCollection <WalletStats>(EnvConstants.Constants.WalletStats); WalletStats walletStats = new WalletStats(); walletStats.Amount = paid_amount; walletStats.IsAdded = true; walletStats.Reason = "Money Added"; walletStats.UserEmail = developer_model.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, (developer_model.Wallet.Balance - developer_model.Wallet.UnbilledUsage).ToString()); optionalParameters.Add(EnvConstants.Constants.EmailParam_PaymentId, charge.Id); optionalParameters.Add(EnvConstants.Constants.EmailParam_PaymentPartyName, charge.ReceiptEmail); //SendKitsuneConvertEmailRequestModel emailcommand = new SendKitsuneConvertEmailRequestModel() { EmailID = user.Email, UserName = user.UserName, MailType = MailType.Payment_Success, Attachments = null, OptionalParams = optionalParameters }; //kitsuneApi.SendKitsuneConvertEmail(emailCommand); emailHelper.SendGetKitsuneEmail(developer_model.Email, developer_model.UserName, MailType.PAYMENT_SUCCESS, null, optionalParameters); } else { throw new Exception("Couldn't fetch user details"); } } else { //Error throw new Exception("error : " + charge.Status); } } catch (Exception e) { //CRITICAL ERROR : FAILED TO GET PAYMENT REQUEST throw new Exception("CRITICAL ERROR " + e.Message); } } catch (Exception e) { //CONNECTION FAILED throw new Exception("Error while charging the customer :" + token); } #endregion }