public async Task <StripeRefund> RefundChargeAsync(string chargeId, StripeRefundCreateOptions options, string apiKey) { var refundService = new StripeRefundService(apiKey); var refund = refundService.Create(chargeId, options); return(refund); }
private async Task PerformTest(string apiKey) { this.apiKeyRepository.Setup(v => v.GetApiKey(UserType.StandardUser)).Returns(apiKey); var expectedOptions = new StripeRefundCreateOptions { Amount = TotalRefundAmount.Value, Reason = "requested_by_customer", Metadata = new Dictionary <string, string> { { CreateStripeRefund.EnactingUserIdMetadataKey, UserId.ToString() }, } }; var stripeRefund = new StripeRefund { Id = CustomerId }; this.stripeService.Setup(v => v.RefundChargeAsync( StripeChargeId, It.Is <StripeRefundCreateOptions>( x => JsonConvert.SerializeObject(x, Formatting.None) == JsonConvert.SerializeObject(expectedOptions, Formatting.None)), apiKey)) .ReturnsAsync(stripeRefund) .Verifiable(); await this.target.ExecuteAsync(UserId, StripeChargeId, TotalRefundAmount, RefundCreditReason.RequestedByCustomer, UserType.StandardUser); this.stripeService.Verify(); }
public async Task ExecuteAsync( UserId enactingUserId, string stripeChargeId, PositiveInt totalRefundAmount, RefundCreditReason reason, UserType userType) { enactingUserId.AssertNotNull("enactingUserId"); stripeChargeId.AssertNotNull("stripeChargeId"); totalRefundAmount.AssertNotNull("totalRefundAmount"); var apiKey = this.apiKeyRepository.GetApiKey(userType); var options = new StripeRefundCreateOptions { Amount = totalRefundAmount.Value, Reason = this.GetReason(reason), Metadata = new Dictionary <string, string> { { EnactingUserIdMetadataKey, enactingUserId.ToString() }, } }; await this.stripeService.RefundChargeAsync(stripeChargeId, options, apiKey); }
public static string RefundToUser(string chargeId, bool isFullRefund, int specificAmount) { try { string stripekey = ConfigurationManager.AppSettings["AdminStripeApiKey"]; StripeConfiguration.SetApiKey(stripekey); StripeRefundCreateOptions refundOptions; if (isFullRefund == true) { refundOptions = new StripeRefundCreateOptions() { Reason = StripeRefundReasons.RequestedByCustomer, //RefundApplicationFee = true }; } else { refundOptions = new StripeRefundCreateOptions() { Reason = StripeRefundReasons.RequestedByCustomer, //RefundApplicationFee = true, Amount = specificAmount * 100 }; } var refundService = new StripeRefundService(); StripeRefund refund = refundService.Create(chargeId, refundOptions); return(refund.Id); } catch (Exception ex) { Common.ExcepLog(ex); return(null); } }
private StripeRefund CreateStripeRefund(long Amount, string ChargeID) { var refundOptions = new StripeRefundCreateOptions() { Amount = (int)Amount, Reason = StripeRefundReasons.Unknown }; var refundService = new StripeRefundService(); return(refundService.Create(ChargeID, refundOptions)); }
public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest) { var result = new RefundPaymentResult(); try { var options = new StripeRefundCreateOptions(); var charge = _chargeService.Get(refundPaymentRequest.Order.CaptureTransactionId); var maximumRefund = charge.Amount - charge.AmountRefunded; options.Amount = (int)Math.Ceiling(refundPaymentRequest.AmountToRefund * 100); if (maximumRefund == 0 && !refundPaymentRequest.IsPartialRefund) //if it's a partial refund the user would not expect to see the order switch to the Refund status { result.NewPaymentStatus = PaymentStatus.Refunded; // this means it has been previously refunded from Stripe admin return(result); } if (options.Amount > maximumRefund) { if (maximumRefund > 0) { result.AddError("This charge has already been partially refunded. Maximum refund amount is: " + (decimal)maximumRefund / 100); } else { result.AddError("This charge has already been fully refunded."); } return(result); } refundPaymentRequest.IsPartialRefund = options.Amount != maximumRefund; _refundService.Create(refundPaymentRequest.Order.CaptureTransactionId, options); result.NewPaymentStatus = refundPaymentRequest.IsPartialRefund ? PaymentStatus.PartiallyRefunded : PaymentStatus.Refunded; return(result); } catch (StripeException exception) { if (!string.IsNullOrEmpty(exception.StripeError.Code)) { result.AddError(exception.StripeError.Message + " Error code: " + exception.StripeError.Code); } else { result.AddError(exception.StripeError.Message); } return(result); } }
public static StripeRefund RefundCharge(string token, int amount) { // Set your secret key: remember to change this to your live secret key in production // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.SetApiKey(MasterStrings.StripeSecretKey); var refundOptions = new StripeRefundCreateOptions() { Amount = amount, Reason = "requested_by_customer" }; var refundService = new StripeRefundService(); StripeRefund refund = refundService.Create(token, refundOptions); return(refund); }
public bool CreateStripeRefund() { try { StripeRefundCreateOptions refundcreat = new StripeRefundCreateOptions(); refundcreat.Amount = this.Amount; // refundcreat.Reason = StripeParams.StripeRefundReason; // var service = new StripeRefundService(StripeParams.ApiKey); // var creatingService = service.Create(this.ChargeId, refundcreat);//later will add some functionality } catch (Exception ex) { // ErrorLog.Log(ex); return(false); } return(true); }
private PaymentProcessingResult ProcessPaymentRefund(IOrderGroup orderGroup, ICreditCardPayment creditCardPayment) { var refundAmount = creditCardPayment.Amount; var purchaseOrder = (IPurchaseOrder)orderGroup; if (purchaseOrder == null || refundAmount <= 0 || string.IsNullOrEmpty(creditCardPayment.ProviderPaymentId)) { return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate("RefundError"))); } try { var refundOptions = new StripeRefundCreateOptions() { Amount = (int)refundAmount * GetMultiplier(orderGroup.Currency), Reason = StripeRefundReasons.RequestedByCustomer }; var refund = _stripeRefundService.Create(creditCardPayment.ProviderPaymentId, refundOptions); // Extract the response details. creditCardPayment.TransactionID = refund.Id; var message = $"[{creditCardPayment.PaymentMethodName}] [RefundTransaction-{refund.Id}] " + $"Response: {refund.Status} at Timestamp={refund.Created.ToString()}: {refund.Amount}{refund.Currency}"; // add a new order note about this refund AddNoteToPurchaseOrder("REFUND", message, purchaseOrder.CustomerId, purchaseOrder); _orderRepository.Service.Save(purchaseOrder); return(PaymentProcessingResult.CreateSuccessfulResult(message)); } catch (StripeException e) { switch (e.StripeError.ErrorType) { case "card_error": return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(e.StripeError.Code))); default: return(PaymentProcessingResult.CreateUnsuccessfulResult(e.StripeError.Message)); } } }
public StripeRefundServiceTest() { this.service = new StripeRefundService(); this.createOptions = new StripeRefundCreateOptions() { Amount = 123, }; this.updateOptions = new StripeRefundUpdateOptions() { Metadata = new Dictionary <string, string>() { { "key", "value" }, }, }; this.listOptions = new StripeRefundListOptions() { Limit = 1, }; }
private void CreateRefund(Transaction t) { StripeConfiguration.SetApiKey(Settings.StripeApiKey); var refundService = new StripeRefundService(); var refundOptions = new StripeRefundCreateOptions(); refundOptions.Amount = (int)(t.Amount * 100); var refund = refundService.Create(t.PreviousTransactionNumber, refundOptions); if (refund.Id.Length > 0) { t.Result.Succeeded = true; t.Result.ReferenceNumber = refund.Id; } else { t.Result.Succeeded = false; t.Result.ResponseCode = "FAIL"; t.Result.ResponseCodeDescription = "Stripe Failure"; } }
public int Add(AddSubscriptionBookingParams param) { try { using (var transaction = new TransactionScope()) { var bookingCode = Helper.RandomString(Constant.BookingCodeLength); while (IsBookingCodeExists(bookingCode)) { bookingCode = Helper.RandomString(Constant.BookingCodeLength); } param.SubscriptionBookingsObject.BookingCode = bookingCode; // Insert New Bookings DayaxeDbContext.SubscriptionBookings.InsertOnSubmit(param.SubscriptionBookingsObject); Commit(); if (param.SubscriptionBookingDiscounts != null && param.SubscriptionBookingDiscounts.Id > 0) { var subscriptionDiscount = new SubscriptionBookingDiscounts { DiscountId = param.SubscriptionBookingDiscounts.Id, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId }; DayaxeDbContext.SubscriptionBookingDiscounts.InsertOnSubmit(subscriptionDiscount); } // Insert to History for First Cycle var cycle = new SubscriptionCycles { SubscriptionBookingId = param.SubscriptionBookingsObject.Id, StartDate = param.SubscriptionBookingsObject.StartDate, EndDate = param.SubscriptionBookingsObject.EndDate, CancelDate = param.SubscriptionBookingsObject.CancelDate, LastUpdatedBy = param.SubscriptionBookingsObject.LastUpdatedBy, LastUpdatedDate = param.SubscriptionBookingsObject.LastUpdatedDate, Status = param.SubscriptionBookingsObject.Status, Price = param.ActualPrice, MerchantPrice = param.MerchantPrice, PayByCredit = param.PayByCredit, TotalPrice = param.TotalPrice, Quantity = param.SubscriptionBookingsObject.Quantity, StripeChargeId = string.Empty, StripeCouponId = param.SubscriptionBookingsObject.StripeCouponId, StripeInvoiceId = string.Empty, CycleNumber = 1 }; DayaxeDbContext.SubscriptionCycles.InsertOnSubmit(cycle); // Insert Discount for Current Customer active Subscription var discount = new Discounts { DiscountName = string.Format("{0} - {1} {2}", param.SubscriptionName, param.FirstName, param.LastName), Code = string.Format("SUP{0}", Helper.RandomString(8)), StartDate = param.SubscriptionBookingsObject.StartDate, EndDate = param.SubscriptionBookingsObject.EndDate, CodeRequired = true, PercentOff = 100, PromoType = (byte)Enums.PromoType.SubscriptionPromo, MinAmount = 0, IsAllProducts = true, MaxPurchases = (byte)param.MaxPurchases }; DayaxeDbContext.Discounts.InsertOnSubmit(discount); Commit(); // Add Invoices var subscriptionInvoice = new SubscriptionInvoices { SubscriptionCyclesId = cycle.Id, BookingStatus = cycle.Status, Quantity = cycle.Quantity, Price = cycle.Price, MerchantPrice = cycle.MerchantPrice, PayByCredit = cycle.PayByCredit, TotalPrice = cycle.TotalPrice, InvoiceStatus = (int)Enums.InvoiceStatus.Charge, StripeChargeId = cycle.StripeChargeId, ChargeAmount = cycle.Price, StripeRefundId = string.Empty, RefundAmount = 0, RefundCreditAmount = 0, StripeCouponId = cycle.StripeCouponId, CreatedDate = DateTime.UtcNow, CreatedBy = 1 }; DayaxeDbContext.SubscriptionInvoices.InsertOnSubmit(subscriptionInvoice); var discountUsed = new SubsciptionDiscountUseds { CustomerId = param.SubscriptionBookingsObject.CustomerId, DiscountId = discount.Id, SubscriptionBookingId = param.SubscriptionBookingsObject.Id }; DayaxeDbContext.SubsciptionDiscountUseds.InsertOnSubmit(discountUsed); var cusCredits = DayaxeDbContext.CustomerCredits .SingleOrDefault(cc => cc.CustomerId == param.CustomerCreditsObject.CustomerId); // Add Logs when refund by Upgrade to Subscription if (param.RefundCreditByUpgrade > 0) { var creditLogs = new CustomerCreditLogs { Amount = param.RefundCreditByUpgrade, ProductId = 0, BookingId = 0, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId, CreatedBy = param.CustomerCreditsObject.CustomerId, CreatedDate = DateTime.UtcNow, CreditType = (byte)Enums.CreditType.PartialPuchaseRefund, Description = param.RefundCreditDescription, CustomerId = param.CustomerCreditsObject.CustomerId, ReferralId = param.CustomerCreditsObject.ReferralCustomerId, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, Status = true, GiftCardId = 0 }; DayaxeDbContext.CustomerCreditLogs.InsertOnSubmit(creditLogs); if (cusCredits != null) { cusCredits.Amount += param.RefundCreditByUpgrade; } } // Add Logs when pay by DayAxe Credit if (param.PayByCredit > 0) { var creditLogs = new CustomerCreditLogs { Amount = param.PayByCredit, ProductId = 0, BookingId = 0, SubscriptionId = param.SubscriptionBookingsObject.SubscriptionId, CreatedBy = param.CustomerCreditsObject.CustomerId, CreatedDate = DateTime.UtcNow, CreditType = (byte)Enums.CreditType.Charge, Description = param.Description, CustomerId = param.CustomerCreditsObject.CustomerId, ReferralId = param.CustomerCreditsObject.ReferralCustomerId, SubscriptionBookingId = param.SubscriptionBookingsObject.Id, Status = true, GiftCardId = 0 }; DayaxeDbContext.CustomerCreditLogs.InsertOnSubmit(creditLogs); if (cusCredits != null) { cusCredits.Amount -= param.PayByCredit; } } // First Buy of referral if (param.CustomerCreditsObject != null && param.CustomerCreditsObject.ReferralCustomerId > 0 && ( DayaxeDbContext.Bookings.Count(x => x.CustomerId == param.SubscriptionBookingsObject.CustomerId) == 1 || DayaxeDbContext.SubscriptionBookings.Count(x => x.CustomerId == param.SubscriptionBookingsObject.CustomerId) == 1)) { var logs = DayaxeDbContext.CustomerCreditLogs .Where(ccl => ccl.CustomerId == param.CustomerCreditsObject.ReferralCustomerId && ccl.ReferralId == param.SubscriptionBookingsObject.CustomerId && !ccl.Status) .ToList(); if (logs.Any()) { logs.ForEach(log => { var cus = DayaxeDbContext.CustomerCredits .FirstOrDefault(cc => cc.CustomerId == log.CustomerId); if (cus != null) { cus.Amount += log.Amount; } log.Status = true; }); Commit(); } } // Add to Customer Credit Log var logsReferred = DayaxeDbContext.CustomerCreditLogs .Where(ccl => ccl.ReferralId == param.SubscriptionBookingsObject.CustomerId && !ccl.Status) .ToList(); if (logsReferred.Any()) { logsReferred.ForEach(log => { var cus = DayaxeDbContext.CustomerCredits .FirstOrDefault(cc => cc.CustomerId == log.CustomerId); if (cus != null) { cus.Amount += log.Amount; } log.Status = true; }); } // Insert Schedule var schedules = new Schedules { ScheduleSendType = (int)Enums.ScheduleSendType.IsEmailConfirmSubscription, Name = "Send Email confirm Subscription", Status = (int)Enums.ScheduleType.NotRun, SubscriptionBookingId = param.SubscriptionBookingsObject.Id }; DayaxeDbContext.Schedules.InsertOnSubmit(schedules); // Maybe not use here because flow upgrade to subscription do not use this if (param.BookingId > 0) { var bookings = DayaxeDbContext.Bookings.FirstOrDefault(b => b.BookingId == param.BookingId); if (bookings != null) { var chargePrice = (int)((bookings.TotalPrice - bookings.HotelPrice) * 100); try { bookings.TotalPrice -= bookings.HotelPrice; bookings.PassStatus = (int)Enums.BookingStatus.Active; var discounts = new DiscountBookings { DiscountId = discount.Id, ProductId = bookings.ProductId, BookingId = bookings.BookingId }; DayaxeDbContext.DiscountBookings.InsertOnSubmit(discounts); bookings.IsActiveSubscription = true; string responseString; if (chargePrice > 0) { var chargeService = new StripeChargeService(); StripeCharge charge = chargeService.Capture(bookings.StripeChargeId, chargePrice); responseString = charge.StripeResponse.ResponseJson; } else { var refundOptions = new StripeRefundCreateOptions { Reason = StripeRefundReasons.RequestedByCustomer }; var refundService = new StripeRefundService(); StripeRefund refund = refundService.Create(bookings.StripeChargeId, refundOptions); responseString = refund.StripeResponse.ResponseJson; } var logs = new Logs { LogKey = "UpgradeSubscriptionCharge", UpdatedContent = string.Format("Params: {0} - Response: {1}", JsonConvert.SerializeObject(param, CustomSettings.SerializerSettings()), responseString), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); } catch (Exception ex) { var logs = new Logs { LogKey = "UpgradeSubscriptionChargeError", UpdatedContent = string.Format("Params: {0} - {1} - {2} - {3}", JsonConvert.SerializeObject(param, CustomSettings.SerializerSettings()), ex.Message, ex.StackTrace, ex.Source), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); } } } Commit(); transaction.Complete(); } } catch (Exception ex) { var logs = new Logs { LogKey = "AddBookingError", UpdatedContent = string.Format("{0} - {1} - {2} - {3}", param.SubscriptionBookingsObject.CustomerId, ex.Message, ex.StackTrace, ex.Source), UpdatedBy = param.SubscriptionBookingsObject.CustomerId, UpdatedDate = DateTime.UtcNow }; AddLog(logs); throw new Exception(ex.Message, ex); } return(param.SubscriptionBookingsObject.Id); }
public RefundRequestBuilder Create() { _request = new StripeRefundCreateOptions(); return(this); }