private bool ProcessPayment(Order order, decimal creditAmount, Dictionary <string, object> paymentMethodData, out CustomResponse response) { var isSubscription = OrderHelper.IsSubscription(order); var transactionResult = isSubscription ? _paymentProcessor.ProcessCreateSubscription(order, creditAmount, paymentMethodData) : _paymentProcessor.ProcessPayment(order, creditAmount, paymentMethodData); if (transactionResult.Success) { response = R.Success; if (transactionResult.RequiresRedirection) { response.Redirect(transactionResult.RedirectionUrl); } else { //if we are here, payment has been done, so we can get the transaction data //create payment transaction object and save it to database _paymentAccountant.ProcessTransactionResult(transactionResult); } return(true); } else { //unlock the credits _storeCreditService.UnlockCredits(order.StoreCredits, order.UserId); } response = R.Fail.With("error", T("An error occurred while checking out")); return(false); }
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); } }