Ejemplo n.º 1
0
        /*
         * Here we're initializing a stripe SEPA subscription on a source with sepa/iban data. This subscription should auto-charge.
         */
        public async Task InitializeSepaDirect(SepaDirectCheckout checkout, CancellationToken token)
        {
            logger.LogInformation("Initializing sepa direct");
            IEnumerable <ValidationResult> validationResults = ValidationHelper.Validate(checkout, serviceProvider);

            if (validationResults.Any())
            {
                throw new InvalidOperationException(string.Join(",", validationResults.Select(v => $"{string.Join(", ", v.MemberNames)}: {v.ErrorMessage}")));
            }

            ApplicationUser user = await userManager.FindByEmailAsync(checkout.Email).ConfigureAwait(false);

            Customer customer = await GetOrCreateCustomer(checkout.Name, checkout.Email, token).ConfigureAwait(false);

            Source source = await sourceService.AttachAsync(
                customer.Id,
                new SourceAttachOptions()
            {
                Source = checkout.SourceId
            },
                cancellationToken : token).ConfigureAwait(false);

            Plan plan = await CreateRecurringPlan(checkout.Amount, "eur", token).ConfigureAwait(false);

            Subscription subscription = await subscriptionService.CreateAsync(
                new SubscriptionCreateOptions()
            {
                DefaultSource = source.Id,
                Billing       = Billing.ChargeAutomatically,
                CustomerId    = customer.Id,
                Items         = new List <SubscriptionItemOption>()
                {
                    new SubscriptionItemOption()
                    {
                        PlanId   = plan.Id,
                        Quantity = 1
                    }
                }
            },
                cancellationToken : token).ConfigureAwait(false);

            context.DonationEventLog.Add(new DonationEventLog(userId: user?.Id, type: DonationEventType.Internal, eventData: subscription.ToJson()));
            await context.SaveChangesAsync(token).ConfigureAwait(false);

            logger.LogInformation("Done initializing sepa direct");
        }
Ejemplo n.º 2
0
        public DonationMutationGraph()
        {
            FieldAsync <NonNullGraphType <StringGraphType>, string>(
                "initializeCreditCardCheckout",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <CreditCardCheckoutInputGraph> >()
            {
                Name = "checkout"
            }),
                resolve: c =>
            {
                CreditCardCheckout checkout = c.GetArgument <CreditCardCheckout>("checkout");
                var provider = c.GetUserContext().ServiceProvider;
                return(provider.GetRequiredService <IDonationService>()
                       .InitializeCreditCardCheckout(checkout, c.CancellationToken));
            });

            FieldAsync <NonNullGraphType <StringGraphType>, string>(
                "initializeSepaDirect",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <SepaDirectCheckoutInputGraph> >()
            {
                Name = "checkout"
            }),
                resolve: async c =>
            {
                SepaDirectCheckout checkout = c.GetArgument <SepaDirectCheckout>("checkout");
                var provider = c.GetUserContext().ServiceProvider;
                await provider.GetRequiredService <IDonationService>()
                .InitializeSepaDirect(checkout, c.CancellationToken)
                .ConfigureAwait(false);
                return(checkout.SourceId);
            });

            FieldAsync <NonNullGraphType <StringGraphType>, string>(
                "initializeIDealCheckout",
                arguments: new QueryArguments(new QueryArgument <NonNullGraphType <IDealCheckoutInputGraph> >()
            {
                Name = "checkout"
            }),
                resolve: async c =>
            {
                IDealCheckout checkout = c.GetArgument <IDealCheckout>("checkout");
                var provider           = c.GetUserContext().ServiceProvider;
                await provider.GetRequiredService <IDonationService>()
                .InitializeIDealCheckout(checkout, c.CancellationToken)
                .ConfigureAwait(false);
                return(checkout.SourceId);
            });

            FieldAsync <NonNullGraphType <StringGraphType>, string>(
                "cancelSubscription",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> >()
            {
                Name = "subscriptionId"
            }),
                resolve: async c =>
            {
                string subscriptionId = c.GetArgument <string>("subscriptionId");
                var context           = c.GetUserContext();
                await context.ServiceProvider
                .GetRequiredService <IDonationService>()
                .CancelSubscription(subscriptionId, context.User, c.CancellationToken)
                .ConfigureAwait(false);
                return(subscriptionId);
            });
        }