public static bool Refund(decimal Amount, string PaymentId) { try { Money AmountMoney = new Money(); AmountMoney.Amount = (long)(Amount * 100); AmountMoney.Currency = "USD"; string IdempotencyKey2 = Guid.NewGuid().ToString(); RefundPaymentRequest refundPaymentRequest = new RefundPaymentRequest(IdempotencyKey: IdempotencyKey2, AmountMoney: AmountMoney, PaymentId: PaymentId); RefundsApi refundsApi = new RefundsApi(); refundsApi.Configuration.AccessToken = ConfigurationManager.AppSettings["SquareAccessToken"]; refundsApi.Configuration.ApiClient = new ApiClient(GetSquareApiUrl()); var response = refundsApi.RefundPayment(refundPaymentRequest); return(true); } catch (Exception ex) { throw Utility.ThrowException(ex); } }
public void Init() { instance = new RefundsApi(); }
public void CreateRefundAPaymentTest() { string paymentId = "40006143732"; //Amount Details AmountDetail amountDetail = new AmountDetail("100.00", "USD"); // Solution Details Solution solution = new Solution { Name = "SolDemo", VendorName = "VenodeName" }; // Credit Card CreditCard creditCard = new CreditCard("1111", "XXXX", "VISA", "123"); // Bank Account BankAccount bankAccount = new BankAccount(null, "325070760", "4111111", "demo acount", null, "bank anme", null); //Base AddressBillTo BaseAddress baseAdressBillTo = new BaseAddress { Address1 = " Demo Adress", Company = "XXXXCOMAPNAY", Country = "India", FirstName = "Demo first", LastName = "Demo Second", PhoneNumber = "1123456987", PostalCode = "192123" }; //Base AddressShipTo BaseAddress baseAdressShipTo = new BaseAddress { Address1 = " Demo Adress", Company = "XXXXCOMAPNAY", Country = "India", FirstName = "Demo first", LastName = "Demo Second", PhoneNumber = "1123456987", PostalCode = "192123" }; // Ip Address String Ip = null; // Order Order order = new Order("12365"); // Extended Amount Tax ExtendedAmount extendedAmountTax = new ExtendedAmount("100.00", "Tax", "Tax"); // Extended Amount Duty ExtendedAmount extendedAmountDuty = new ExtendedAmount("100.00", "duty", "Duty"); // Extended Amount Ship ExtendedAmount extendedAmountShip = new ExtendedAmount("100.00", "ship", "Ship"); //Payment Instrunment // PaymentInstrument paymentInstrument = new PaymentInstrument(null, bankAccount, null, null, true); PaymentInstrument paymentInstrument = new PaymentInstrument(creditCard, null, null, null, true); List <LineItem> lineItems = new List <LineItem>(); lineItems.Add(new LineItem("1", "tshir", "tshir desc", 1, "100", true)); RefundRequest body = new RefundRequest("50.00", order, lineItems, extendedAmountTax, extendedAmountShip, extendedAmountDuty, paymentInstrument, baseAdressBillTo, baseAdressShipTo); string authorization = "Basic asdadsa"; mockRestClient.Expects.One.Method(v => v.Execute(new RestRequest())).With(NMock.Is.TypeOf(typeof(RestRequest))).WillReturn(paymentResponse); ApiClient apiClient = new ApiClient(mockRestClient.MockObject); apiClient.Configuration = null; Configuration configuration = new Configuration { ApiClient = apiClient, Username = "******", Password = "******", AccessToken = null, ApiKey = null, ApiKeyPrefix = null, TempFolderPath = null, DateTimeFormat = null, Timeout = 60000, UserAgent = "asdasd" }; instance = new RefundsApi(configuration); var response = instance.CreateRefundAPayment(paymentId, body, authorization); Assert.IsInstanceOf <Payment>(response, "response is Payment"); }
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); }