public void SubscribeForPackage(PackageSubscribeDto model) { var packageDb = _packageRepository.GetById(model.PackageId); if (packageDb == null) { throw new ApplicationException("Nonexisting package!"); } var userDb = _userRepository.GetById(model.PackageId); if (packageDb == null) { throw new ApplicationException("Nonexisting package!"); } var subscription = new Subscription { User = userDb, Package = packageDb, TotalPrice = model.Amount, DateFrom = DateTime.Now, DateTo = model.SubscriptionType == (int)SubscriptionTypeEnum.Monthly ? DateTime.Now.AddMonths(1) : DateTime.Now.AddYears(1), PaymentId = model.PaymentId, IsPaid = false }; userDb.AddSubscription(subscription); _userRepository.Update(userDb); }
public async Task <DataResponse <string> > CreatePayment(int userId, int packageId, int subscriptionType, decimal amount) { try { var order = new OrderRequest { CheckoutPaymentIntent = "CAPTURE", PurchaseUnits = new List <PurchaseUnitRequest> { new PurchaseUnitRequest { AmountWithBreakdown = new AmountWithBreakdown { Value = 100.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture), CurrencyCode = "EUR", } } }, ApplicationContext = new ApplicationContext { ReturnUrl = _configuration["PayPal:SuccessUrl"], CancelUrl = _configuration["PayPal:FailUrl"] } }; var request = new OrdersCreateRequest(); request.Prefer("return=representation"); request.RequestBody(order); var responsepp = await PayPalClient.Client(_configuration["PayPal:ClientId"], _configuration["PayPal:ClientSecret"]).Execute(request); var result = responsepp.Result <Order>(); var response = new DataResponse <string> { Data = result.Links.FirstOrDefault(link => link.Rel.Equals("approve"))?.Href, Succeeded = true }; var subscribeModel = new PackageSubscribeDto { Id = 0, UserId = userId, PackageId = packageId, SubscriptionType = subscriptionType, Amount = amount, PaymentId = result.Id }; _packageService.SubscribeForPackage(subscribeModel); return(response); } catch (Exception ex) { _logger.LogError(ex, $"On create paypal payment: Exception thrown with error {ex.Message}"); throw new ApplicationException("Due to technical issues we are not able to create the payment!"); } }
public IActionResult SubscribeForPackage([FromBody] PackageSubscribeDto model) { try { _packageService.SubscribeForPackage(model); return(Ok(true)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }