/// <summary> /// Partially refunds an order (from admin panel) /// </summary> /// <param name="order">Order</param> /// <param name="amountToRefund">Amount to refund</param> /// <returns>A list of errors; empty list if no errors</returns> public virtual IList<string> PartiallyRefund(Order order, decimal amountToRefund) { if (order == null) throw new ArgumentNullException("order"); if (!CanPartiallyRefund(order, amountToRefund)) throw new NasException("Cannot do partial refund for order."); var request = new RefundPaymentRequest(); RefundPaymentResult result = null; try { request.Order = order; request.AmountToRefund = amountToRefund; request.IsPartialRefund = true; result = _paymentService.Refund(request); if (result.Success) { //total amount refunded decimal totalAmountRefunded = order.RefundedAmount + amountToRefund; //update order info order.RefundedAmount = totalAmountRefunded; order.PaymentStatus = result.NewPaymentStatus; _orderService.UpdateOrder(order); //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format("Order has been partially refunded. Amount = {0}", _priceFormatter.FormatPrice(amountToRefund, true, false)), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //check order status CheckOrderStatus(order); } } catch (Exception exc) { if (result == null) result = new RefundPaymentResult(); result.AddError(string.Format("Error: {0}. Full exception: {1}", exc.Message, exc.ToString())); } //process errors string error = ""; for (int i = 0; i < result.Errors.Count; i++) { error += string.Format("Error {0}: {1}", i, result.Errors[i]); if (i != result.Errors.Count - 1) error += ". "; } if (!String.IsNullOrEmpty(error)) { //add a note order.OrderNotes.Add(new OrderNote() { Note = string.Format("Unable to partially refund order. {0}", error), DisplayToCustomer = false, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); //log it string logError = string.Format("Error refunding order #{0}. Error: {1}", order.Id, error); _logger.InsertLog(LogLevel.Error, logError, logError); } return result.Errors; }
/// <summary> /// Refunds a payment /// </summary> /// <param name="refundPaymentRequest">Request</param> /// <returns>Result</returns> public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest) { var result = new RefundPaymentResult(); result.AddError("Refund method not supported"); return result; }
/// <summary> /// Refunds a payment /// </summary> /// <param name="refundPaymentRequest">Request</param> /// <returns>Result</returns> public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest) { var result = new RefundPaymentResult(); string transactionId = refundPaymentRequest.Order.CaptureTransactionId; var req = new RefundTransactionReq(); req.RefundTransactionRequest = new RefundTransactionRequestType(); //NOTE: Specify amount in partial refund req.RefundTransactionRequest.RefundType = RefundType.Full; req.RefundTransactionRequest.RefundTypeSpecified = true; req.RefundTransactionRequest.Version = GetApiVersion(); req.RefundTransactionRequest.TransactionID = transactionId; using (var service1 = new PayPalAPISoapBinding()) { if (!_paypalDirectPaymentSettings.UseSandbox) service1.Url = "https://api-3t.paypal.com/2.0/"; else service1.Url = "https://api-3t.sandbox.paypal.com/2.0/"; service1.RequesterCredentials = new CustomSecurityHeaderType(); service1.RequesterCredentials.Credentials = new UserIdPasswordType(); service1.RequesterCredentials.Credentials.Username = _paypalDirectPaymentSettings.ApiAccountName; service1.RequesterCredentials.Credentials.Password = _paypalDirectPaymentSettings.ApiAccountPassword; service1.RequesterCredentials.Credentials.Signature = _paypalDirectPaymentSettings.Signature; service1.RequesterCredentials.Credentials.Subject = ""; RefundTransactionResponseType response = service1.RefundTransaction(req); string error = string.Empty; bool Success = PaypalHelper.CheckSuccess(response, out error); if (Success) { result.NewPaymentStatus = PaymentStatus.Refunded; //cancelPaymentResult.RefundTransactionID = response.RefundTransactionID; } else { result.AddError(error); } } return result; }