public async Task CreateSubscription() { var plan = new PayPalPlan { Currency = "RUB", Total = 100m, Name = "Basic annual subscription", PeriodType = PeriodType.Month, Description = "Annual subscription plan (Russia 100r.)", Frequency = 1, CancelUrl = "http://localhost:1234", ReturnUrl = "http://localhost:1234", }; await _payPalService.CreatePlanAsync(plan); await _payPalService.ActivatePlanAsync(plan); var agreement = new PayPalAgreement { Name = "Bandmap pro-account", Description = "Annual subscription giving special features to the user on the Bandmap service", PayPalPlanId = plan.Id }; var agreementResult = await _payPalService.CreateAgreementAsync(agreement); }
//[Authorize] public async Task <IActionResult> CreateSubscription(SubscriptionRequest request) { var validationResult = await _validationService.ValidateProductAsync(request.ProductId, request.ProductId); if (!String.IsNullOrEmpty(validationResult?.ErrorMessage)) { return(BadRequest(validationResult.ErrorMessage)); } var product = await _paymentsUnitOfWork.ProductRepository.GetAsync(request.ProductId); var currentHost = $"{Request.Scheme}://{Request.Host}"; var parameter = new CreatePlanParameter { ProductName = request.ProductId, ReturnUrl = $"{currentHost}/api/payment/paypal/subscription/confirm", CancelUrl = $"{currentHost}/api/payment/paypal/subscription/cancel" }; var payPalPlanId = await _paymentsService.GetOrCreatePayPalPlanIdAsync(parameter); var agreement = new PayPalAgreement { Name = product.Title, Description = product.Description, PayPalPlanId = payPalPlanId }; var agreementResponse = await _payPalService.CreateAgreementAsync(agreement); var subscription = new Subscription { Status = SubscriptionStatus.Pending, InfluencedLogin = request.Login, ProductName = product.Name, Currency = product.Currency, PricePerPeriod = product.Price, PayPalAgreementId = agreementResponse.Id, }; await _paymentsUnitOfWork.SubscriptionRepository.CreateAsync(subscription); await _paymentsUnitOfWork.SaveAsync(); return(Ok(agreementResponse)); }
public async Task <PayPalAgreementResult> CreateAgreementAsync(PayPalAgreement agreement) { var payPalAgreementRequest = new PayPalAgreementRequest(agreement.PayPalPlanId) { Name = agreement.Name, Description = agreement.Description }; var jsonContent = JsonConvert.SerializeObject(payPalAgreementRequest); var requestContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(HttpMethod.Post, AgreementUrl); request.Content = requestContent; var response = await _http.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); var createdAgreement = JsonConvert.DeserializeObject <PayPalAgreementCreateResponse>(content); if (!String.IsNullOrWhiteSpace(createdAgreement.ErrorMessage)) { throw new PaypalException(createdAgreement.ErrorMessage, createdAgreement.ErrorDetailes.ToModels(), createdAgreement.InformationLink); } var redirectUrl = createdAgreement.Links.SingleOrDefault(x => x.Rel == "approval_url")?.Href; if (String.IsNullOrEmpty(redirectUrl)) { throw new PaypalException("There is no redirect url."); } return(new PayPalAgreementResult { Id = redirectUrl.Split(new[] { "token=" }, StringSplitOptions.None).LastOrDefault(), RedirectUrl = redirectUrl, }); }