public void ProcessTransactionResult(TransactionResult result, bool clearCart = false) { var order = result.Order ?? _orderService.GetByGuid(result.OrderGuid); if (!result.Success) { _logger.LogError <Order>(result.Exception, "Error occured while processing payment", order.User, result.ResponseParameters); return; } var paymentTransaction = new PaymentTransaction() { CreatedOn = DateTime.UtcNow, OrderGuid = order.Guid, PaymentMethodName = result.IsStoreCreditTransaction ? "Store Credits" : result.IsOfflineTransaction ? "Offline" : order.PaymentMethodName, PaymentStatus = result.NewStatus, UserIpAddress = order.UserIpAddress, TransactionAmount = result.TransactionAmount, TransactionGuid = result.TransactionGuid, TransactionCurrencyCode = result.TransactionCurrencyCode }; if (paymentTransaction.TransactionGuid.IsNullEmptyOrWhiteSpace()) { paymentTransaction.TransactionGuid = Guid.NewGuid().ToString(); } paymentTransaction.SetTransactionCodes(result.ResponseParameters); //save this _paymentTransactionService.Insert(paymentTransaction); if (order.CurrencyCode != result.TransactionCurrencyCode || order.PaymentStatus != result.NewStatus) { //update order if (result.TransactionCurrencyCode != null) { order.CurrencyCode = result.TransactionCurrencyCode; } if (result.IsSubscription && result.NewStatus == PaymentStatus.Complete) { order.IsSubscriptionActive = true; } order.PaymentStatus = result.NewStatus; _orderService.Update(order); } //update store credits if required if (result.NewStatus == PaymentStatus.Authorized || result.NewStatus == PaymentStatus.Complete) { //do we need to process credits? if (order.UsedStoreCredits) { Transaction.Initiate(transaction => { //unlock the store credits first _storeCreditService.UnlockCredits(order.StoreCredits, order.UserId, transaction); _storeCreditService.Insert(new StoreCredit() { AvailableOn = DateTime.UtcNow, CreatedOn = DateTime.UtcNow, Credit = -order.StoreCredits, Description = "Payment for order #" + order.Guid, UserId = order.UserId }, transaction); paymentTransaction = new PaymentTransaction() { CreatedOn = DateTime.UtcNow, OrderGuid = order.Guid, PaymentMethodName = "Store Credits - " + order.StoreCredits, PaymentStatus = PaymentStatus.Complete, UserIpAddress = order.UserIpAddress, TransactionAmount = order.StoreCreditAmount, TransactionGuid = Guid.NewGuid().ToString(), TransactionCurrencyCode = order.CurrencyCode }; if (paymentTransaction.TransactionGuid.IsNullEmptyOrWhiteSpace()) { paymentTransaction.TransactionGuid = Guid.NewGuid().ToString(); } paymentTransaction.SetTransactionCodes(result.ResponseParameters); //save this _paymentTransactionService.Insert(paymentTransaction, transaction); }); } } if (result.NewStatus == PaymentStatus.Refunded || result.NewStatus == PaymentStatus.RefundedPartially) { if (result.IsStoreCreditTransaction) { //and store credits _storeCreditService.Insert(new StoreCredit() { AvailableOn = DateTime.UtcNow, CreatedOn = DateTime.UtcNow, Credit = _affiliateSettings.StoreCreditsExchangeRate > 0 ? result.TransactionAmount / _affiliateSettings.StoreCreditsExchangeRate : 0, Description = "Refund for order #" + order.Guid, UserId = order.UserId }); } } //clear cart if (clearCart) { _cartService.ClearCart(order.UserId); } }
public void Capture(string eventName, object[] eventData = null) { if (!_affiliateSettings.EnableAffiliates || eventData == null) { return; } switch (eventName) { case nameof(NamedEvent.UserActivated): var user = (User)eventData[0]; if (_affiliateSettings.SignupCreditToAffiliate > 0) { if (user.ReferrerId > 0) { _storeCreditService.Insert(new StoreCredit() { CreatedOn = DateTime.UtcNow, AvailableOn = DateTime.UtcNow, Credit = _affiliateSettings.SignupCreditToAffiliate, Description = $"{user.Name} account was activated", UserId = user.ReferrerId }); } } if (_affiliateSettings.SignupCreditToNewUser > 0) { _storeCreditService.Insert(new StoreCredit() { CreatedOn = DateTime.UtcNow, AvailableOn = DateTime.UtcNow, Credit = _affiliateSettings.SignupCreditToNewUser, Description = $"Account activation credit", UserId = user.Id }); } break; case nameof(NamedEvent.OrderPaid): var order = (Order)eventData[1]; var amount = _affiliateSettings.ExcludeTaxFromCalculation ? order.OrderTotal - order.Tax : order.OrderTotal; var affiliateAmount = _affiliateSettings.GetAffiliateAmount(amount); var affiliateUserId = ApplicationEngine.CurrentAffiliate?.Id ?? 0; if (affiliateUserId > 0) { //the credit should be available only after return period is over var availableOn = DateTime.UtcNow; if (_orderSettings.AllowReturns) { var returnDays = order.OrderItems.Where(x => x.Product.AllowReturns) .Max(x => x.Product.DaysForReturn); if (returnDays == 0) { returnDays = _orderSettings.DefaultReturnsDays; } availableOn = availableOn.AddDays(returnDays); } _storeCreditService.Insert(new StoreCredit() { CreatedOn = DateTime.UtcNow, AvailableOn = availableOn, Credit = affiliateAmount, Description = $"Order # {order.Guid} paid by {order.User.Name}", UserId = affiliateUserId }); } break; } }