static async Task <bool> ShareSubscriptionPaymentWithReferrer(BitcornContext dbContext, Subscription subInfo, decimal cost, SubTx subTx, Referrer referrer, User referrerUser, User subscriptionPaymentRecipient) { if (referrer.EnableSubscriptionRewards) { //get referrer user info if (!subInfo.RestrictReferralRewards || (subInfo.RestrictReferralRewards && (referrerUser.Level == "BEST" || referrerUser.Level == "BAIT" || referrerUser.IsAdmin() || referrer.Tier >= 3))) { //calculate amount that will be sent to the referrer var referralShare = cost * subInfo.ReferrerPercentage.Value; //prepare transaction to the referrer var referralShareTx = await TxUtils.PrepareTransaction(subscriptionPaymentRecipient, referrerUser, referralShare, "BITCORNFarms", "$sub referral share", dbContext); //make sure stat tracking values have been initialized if (referrerUser.UserStat.TotalReferralRewardsCorn == null) { referrerUser.UserStat.TotalReferralRewardsCorn = 0; } //make sure stat tracking values have been initialized if (referrerUser.UserStat.TotalReferralRewardsUsdt == null) { referrerUser.UserStat.TotalReferralRewardsUsdt = 0; } //increment total received corn rewards referrerUser.UserStat.TotalReferralRewardsCorn += referralShare; //inceremnt total received usdt rewards referrerUser.UserStat.TotalReferralRewardsUsdt += ((referralShare) * (await ProbitApi.GetCornPriceAsync(dbContext))); //execute transaction if (await TxUtils.ExecuteTransaction(referralShareTx, dbContext)) { //if transaction was made, log & update ytd total await ReferralUtils.UpdateYtdTotal(dbContext, referrer, referralShare); var referralTx = await ReferralUtils.LogReferralTx(dbContext, referrerUser.UserId, referralShare, "$sub referral share"); subTx.ReferralTxId = referralTx.ReferralTxId; await dbContext.SaveAsync(); return(true); } } } return(false); }
/// <summary> /// called from client server to create an order tht the user will authorize /// </summary> public async Task <ActionResult <object> > AuthorizeOrder([FromBody] AuthorizeOrderRequest orderRequest) { try { var user = (this.GetCachedUser()); if (this.GetUserMode() != null && this.GetUserMode() == 1) { throw new NotImplementedException(); } if (user != null) { if (user.IsBanned) { return(StatusCode(403)); } var checkOrder = await GetOrder(orderRequest.OrderId); if (checkOrder == null) { return(StatusCode(404)); } var(order, client) = checkOrder.Value; if (order.OrderState != 0) { return(StatusCode((int)HttpStatusCode.Gone)); } if (order.ClientId != orderRequest.ClientId) { return(StatusCode((int)HttpStatusCode.BadRequest)); } var orderItems = await _dbContext.OrderItem.Where(e => e.OrderId == order.OrderId).ToArrayAsync(); if (orderItems.Length > client.OrderMaxSize) { return(StatusCode((int)HttpStatusCode.NotAcceptable)); } if (orderItems.Sum(e => e.CornAmount) > client.OrderMaxCost) { return(StatusCode((int)HttpStatusCode.NotAcceptable)); } var recipientUser = await _dbContext.JoinUserModels() .FirstOrDefaultAsync((u) => u.UserId == client.RecipientUser); var cornPrice = await ProbitApi.GetCornPriceAsync(_dbContext); var cornOrderSum = orderItems.Select(e => e.CornAmount).Sum(); var cornCurrentSum = orderItems.Select(e => e.UsdAmount / cornPrice).Sum(); var costDiff = Math.Abs(cornCurrentSum - cornOrderSum); if (costDiff <= client.AcceptedCostDiff) { if (recipientUser != null) { var processInfo = await TxUtils.PrepareTransaction(user, recipientUser, cornOrderSum, client.ClientId, "app:order", _dbContext); var paymentSuccess = await processInfo.ExecuteTransaction(_dbContext); if (paymentSuccess) { var jwt = CreateJwt(client, order, orderItems, cornOrderSum, processInfo.Transactions[0].TxId.Value); order.TxId = processInfo.Transactions[0].TxId; order.OrderState = 1; order.CompletedAt = DateTime.Now; await _dbContext.SaveAsync(); if (string.IsNullOrEmpty(client.Capture)) { return(new { jwt, txId = order.TxId.Value, amount = cornOrderSum }); } else { var restClient = new RestClient(); var url = $"{client.Domain}/{client.Capture}"; var redirectUrl = $"{client.Domain}/{client.Redirect}"; var restRequest = new RestRequest(url, Method.POST); if (client.PostFormat == "application/x-www-form-urlencoded") { restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded"); restRequest.AddObject(new { jwt }); } else { restRequest.AddJsonBody(new { jwt }); } var restResponse = restClient.Execute(restRequest); await BITCORNLogger.LogError(_dbContext, new Exception("Order hook callback"), restResponse.Content); return(new { redirect = redirectUrl, txId = order.TxId.Value, amount = cornOrderSum }); } } else { return(new { txId = -1 }); } } } else { return(StatusCode((int)HttpStatusCode.PaymentRequired)); } } return(StatusCode((int)HttpStatusCode.BadRequest)); } catch (Exception e) { await BITCORNLogger.LogError(_dbContext, e, JsonConvert.SerializeObject(orderRequest)); return(StatusCode(500)); } }