public IHttpActionResult Patch([FromODataUri] int key, Delta <Organization> patch)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUser = CurrentUser();

            var organization = db.Organizations.Find(key);

            // Ensure that object was found
            if (organization == null)
            {
                return(NotFound());
            }

            // Ensure that user is authorized
            if (!currentUser.CanModifyOrganizationDetails ||
                currentUser.OrganizationId != key)
            {
                return(BadRequest());
            }

            // Peform the update
            patch.Patch(organization);

            // Update the Stripe payment source if it is provided
            if (patch.GetChangedPropertyNames().Contains("StripeSourceId"))
            {
                var customerService = new CustomerService();
                var sourceService   = new SourceService();

                // Attach the card source id to the customer
                var attachOptions = new SourceAttachOptions()
                {
                    Source = organization.StripeSourceId
                };
                sourceService.Attach(organization.StripeCustomerId, attachOptions);

                // Update the customer's default source
                var customerOptions = new CustomerUpdateOptions()
                {
                    DefaultSource = organization.StripeSourceId
                };
                Stripe.Customer customer = customerService.Update(organization.StripeCustomerId, customerOptions);

                var source = sourceService.Get(organization.StripeSourceId);

                // Record the card details
                organization.StripeSourceCardLast4           = source.Card.Last4;
                organization.StripeSourceCardBrand           = source.Card.Brand;
                organization.StripeSourceCardExpirationMonth = source.Card.ExpMonth.ToString();
                organization.StripeSourceCardExpirationYear  = source.Card.ExpYear.ToString();
            }

            db.SaveChanges();

            return(Updated(organization));
        }
        public SourceServiceTest()
        {
            this.service = new SourceService();

            this.attachOptions = new SourceAttachOptions
            {
                Source = SourceId,
            };

            this.createOptions = new SourceCreateOptions
            {
                Type     = SourceType.AchCreditTransfer,
                Currency = "usd",
                Mandate  = new SourceMandateOptions
                {
                    Acceptance = new SourceMandateAcceptanceOptions
                    {
                        Date = DateTime.Parse("Mon, 01 Jan 2001 00:00:00Z"),
                        Ip   = "127.0.0.1",
                        NotificationMethod = "manual",
                        Status             = "accepted",
                        UserAgent          = "User-Agent",
                    },
                },
                Owner = new SourceOwnerOptions
                {
                    Address = new AddressOptions
                    {
                        State      = "CA",
                        City       = "City",
                        Line1      = "Line1",
                        Line2      = "Line2",
                        PostalCode = "90210",
                        Country    = "US",
                    },
                    Email = "*****@*****.**",
                    Name  = "Owner Name",
                    Phone = "5555555555",
                },
                Receiver = new SourceReceiverOptions
                {
                    RefundAttributesMethod = "manual",
                },
            };

            this.updateOptions = new SourceUpdateOptions
            {
                Metadata = new Dictionary <string, string>
                {
                    { "key", "value" },
                },
            };

            this.listOptions = new SourceListOptions
            {
                Limit = 1,
            };
        }
        public async Task <Source> AttachAsync(string customerId, string token)
        {
            var options = new SourceAttachOptions
            {
                Source = token,
            };
            var service = new Stripe.SourceService();

            return(await service.AttachAsync(customerId, options));
        }
Example #4
0
        // PATCH: odata/Organizations(5)
        public IActionResult Patch([FromODataUri] int key, Delta <Organization> patch)
        {
            var currentUser = CurrentUser();

            var organization = _context.Organizations.Find(key);

            // Ensure that object was found
            if (organization == null)
            {
                return(NotFound());
            }

            // Ensure that user is authorized
            if (!currentUser.CanModifyOrganizationDetails ||
                currentUser.OrganizationId != key)
            {
                return(BadRequest());
            }

            // Do not allow modifying some properties.
            if (patch.GetChangedPropertyNames().Contains("StripeCustomerId") ||
                patch.GetChangedPropertyNames().Contains("StripeSourceCardBrand") ||
                patch.GetChangedPropertyNames().Contains("StripeSourceCardLast4") ||
                patch.GetChangedPropertyNames().Contains("StripeSourceCardExpirationMonth") ||
                patch.GetChangedPropertyNames().Contains("StripeSourceCardExpirationYear") ||
                patch.GetChangedPropertyNames().Contains("StripeSubscriptionId") ||
                patch.GetChangedPropertyNames().Contains("CreatedAt") ||
                patch.GetChangedPropertyNames().Contains("Id"))
            {
                return(BadRequest("Not authorized to modify CreatedAt, Id, StripeCustomerId, StripeSourceCardBrand, StripeSourceCardLast4, StripeSourceCardExpirationMonth, StripeSourceCardExpirationYear, or StripeSubscriptionId."));
            }

            // Peform the update
            patch.Patch(organization);

            // Validate the model.
            ModelState.ClearValidationState(nameof(organization));
            if (!TryValidateModel(organization, nameof(organization)))
            {
                return(BadRequest());
            }

            // Update the Stripe payment source if it is provided
            if (patch.GetChangedPropertyNames().Contains("StripeSourceId"))
            {
                var customerService = new CustomerService();
                var sourceService   = new SourceService();

                // Attach the card source id to the customer
                var attachOptions = new SourceAttachOptions()
                {
                    Source = organization.StripeSourceId
                };
                sourceService.Attach(organization.StripeCustomerId, attachOptions);

                // Update the customer's default source
                var customerOptions = new CustomerUpdateOptions()
                {
                    DefaultSource = organization.StripeSourceId
                };
                Stripe.Customer customer = customerService.Update(organization.StripeCustomerId, customerOptions);

                var source = sourceService.Get(organization.StripeSourceId);

                // Record the card details
                organization.StripeSourceCardLast4           = source.Card.Last4;
                organization.StripeSourceCardBrand           = source.Card.Brand;
                organization.StripeSourceCardExpirationMonth = source.Card.ExpMonth.ToString();
                organization.StripeSourceCardExpirationYear  = source.Card.ExpYear.ToString();
            }

            _context.SaveChanges();

            return(NoContent());
        }