Ejemplo n.º 1
0
        public async Task GetPayPalPlanTest()
        {
            var allProducts = await _unitOfWork.ProductRepository.GetAllAsync();

            foreach (var product in allProducts)
            {
                var parameter = new CreatePlanParameter()
                {
                    ProductName = product.Name,
                    ReturnUrl   = "http://localhost:1234",
                    CancelUrl   = "http://localhost:1234",
                };
                var plan = await _paymentsService.GetOrCreatePayPalPlanIdAsync(parameter);

                Assert.NotEqual(plan, String.Empty);
            }
        }
Ejemplo n.º 2
0
        //[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));
        }
Ejemplo n.º 3
0
        public async Task <string> GetOrCreatePayPalPlanIdAsync(CreatePlanParameter parameter)
        {
            var planId = await _paymentsUnitOfWork.PayPalPlanRepository.GetPlanIdAsync(parameter.ProductName);

            if (!string.IsNullOrEmpty(planId))
            {
                return(planId);
            }

            var product = await _paymentsUnitOfWork.ProductRepository.GetAsync(parameter.ProductName);

            var payPalPlan = new PayPalPlan
            {
                Name        = product.Title,
                Description = product.Description,
                Currency    = product.Currency,
                Frequency   = 1,
                PeriodType  = ToPeriodType(product.Period),
                Total       = product.Price,
                ReturnUrl   = parameter.ReturnUrl,
                CancelUrl   = parameter.CancelUrl
            };
            await _payPalService.CreatePlanAsync(payPalPlan);

            await _payPalService.ActivatePlanAsync(payPalPlan);

            var plan = new Core.Models.PayPalPlan
            {
                Id          = payPalPlan.Id,
                ProductName = parameter.ProductName,
            };
            await _paymentsUnitOfWork.PayPalPlanRepository.CreateAsync(plan);

            await _paymentsUnitOfWork.SaveAsync();

            return(payPalPlan.Id);
        }