public static TransactionResult ProcessRefund(TransactionRequest refundRequest, SquareSettings squareSettings, ILogger logger) { var order = refundRequest.Order; var config = GetConfiguration(squareSettings); var paymentId = refundRequest.GetParameterAs <string>("paymentId"); var refundsApi = new RefundsApi(config); var refundResponse = refundsApi.RefundPayment(new RefundPaymentRequest() { IdempotencyKey = Guid.NewGuid().ToString(), AmountMoney = new Money() { Amount = (long)((refundRequest.IsPartialRefund ?refundRequest.Amount : order.OrderTotal) * 100) }, PaymentId = paymentId, Reason = "Refunded by administrator " + ApplicationEngine.CurrentUser.Name }); //perform the call var transactionResult = new TransactionResult() { OrderGuid = order.Guid, TransactionGuid = Guid.NewGuid().ToString(), }; if (refundResponse != null) { var refund = refundResponse.Refund; transactionResult.Success = true; transactionResult.TransactionAmount = (decimal)(refund.AmountMoney.Amount ?? 0) / 100; transactionResult.ResponseParameters = new Dictionary <string, object>() { { "refundId", refund.Id }, }; if (refund.Status == "PENDING") { transactionResult.NewStatus = PaymentStatus.RefundPending; } else if (refund.Status == "COMPLETE") { transactionResult.NewStatus = refundRequest.IsPartialRefund ? PaymentStatus.RefundedPartially : PaymentStatus.Refunded; } else { var errors = string.Join(",", refundResponse.Errors); logger.Log <TransactionResult>(LogLevel.Warning, "The refund for Order#" + order.Id + " by square failed." + errors); transactionResult.Exception = new Exception("An error occurred while refunding payment. Error Details: " + errors); transactionResult.Success = false; } } else { logger.Log <TransactionResult>(LogLevel.Warning, "The refund for Order#" + order.Id + " by square failed. No response received."); transactionResult.Success = false; transactionResult.Exception = new Exception("An error occurred while refunding payment"); } if (transactionResult.NewStatus == PaymentStatus.RefundPending) { //request again to get exact status var refund = refundsApi.GetPaymentRefund(transactionResult.GetParameterAs <string>("refundId")); if (refund.Refund.Status == "COMPLETE") { transactionResult.NewStatus = refundRequest.IsPartialRefund ? PaymentStatus.RefundedPartially : PaymentStatus.Refunded; } } return(transactionResult); }