Beispiel #1
0
        public async Task <DomainValidationResult <PaymentMethod> > AttachPaymentMethodAsync(
            string paymentMethodId,
            string customerId,
            bool defaultPaymentMethod = false
            )
        {
            var result = new DomainValidationResult <PaymentMethod>();

            var paymentMethodCardLimit = Options.PaymentMethod.Card.Limit;

            if (await this.PaymentMethodCountAsync(customerId) >= paymentMethodCardLimit)
            {
                result.AddFailedPreconditionError(
                    $"You can have a maximum of {paymentMethodCardLimit} card{(paymentMethodCardLimit > 1 ? "s" : string.Empty)} as a payment method");
            }

            if (result.IsValid)
            {
                var options = new PaymentMethodAttachOptions
                {
                    Customer = customerId
                };

                var paymentMethod = await this.AttachAsync(paymentMethodId, options);

                if (defaultPaymentMethod || !await _stripeCustomerService.HasDefaultPaymentMethodAsync(customerId))
                {
                    await _stripeCustomerService.SetDefaultPaymentMethodAsync(customerId, paymentMethodId);
                }

                return(paymentMethod);
            }

            return(result);
        }
Beispiel #2
0
        public ActionResult <Invoice> RetryInvoice([FromBody] RetryInvoiceRequest req)
        {
            // Attach payment method
            var options = new PaymentMethodAttachOptions
            {
                Customer = req.Customer,
            };
            var service       = new PaymentMethodService();
            var paymentMethod = service.Attach(req.PaymentMethod, options);

            // Update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                },
            };
            var customerService = new CustomerService();

            customerService.Update(req.Customer, customerOptions);

            var invoiceOptions = new InvoiceGetOptions();

            invoiceOptions.AddExpand("payment_intent");
            var     invoiceService = new InvoiceService();
            Invoice invoice        = invoiceService.Get(req.Invoice, invoiceOptions);

            return(invoice);
        }
        public ActionResult <Subscription> CreateSubscription([FromBody] CreateSubscriptionRequest req)
        {
            // Attach payment method
            PaymentMethod paymentMethod;

            try
            {
                var options = new PaymentMethodAttachOptions
                {
                    Customer = req.Customer,
                };
                var service = new PaymentMethodService();
                paymentMethod = service.Attach(req.PaymentMethod, options);
            }
            catch (StripeException e)
            {
                return(Ok(new { error = new { message = e.Message } }));
            }

            // Update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                },
            };
            var customerService = new CustomerService();

            customerService.Update(req.Customer, customerOptions);

            // Create subscription
            var subscriptionOptions = new SubscriptionCreateOptions
            {
                Customer = req.Customer,
                Items    = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = Environment.GetEnvironmentVariable(req.Price),
                    },
                },
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");
            var subscriptionService = new SubscriptionService();

            try
            {
                Subscription subscription = subscriptionService.Create(subscriptionOptions);
                return(subscription);
            }
            catch (StripeException e)
            {
                Console.WriteLine($"Failed to create subscription.{e}");
                return(BadRequest());
            }
        }
Beispiel #4
0
    public void AttachPaymentMethodToCustomer(string paymentMethodId, string customerId)
    {
        var options = new PaymentMethodAttachOptions
        {
            Customer = customerId,
        };

        _paymentMethodService.Attach(paymentMethodId, options);
    }
Beispiel #5
0
        public static void AttachPaymentMethod(Customer existingCustomer, PaymentMethod paymentMethod, PaymentMethodService paymentMethodService)
        {
            // Attach a payment method to a customer
            var paymentMethodAttachOptions = new PaymentMethodAttachOptions {
                Customer = existingCustomer.Id
            };

            paymentMethodService.Attach(paymentMethod.Id, paymentMethodAttachOptions);
        }
Beispiel #6
0
        public async Task <IActionResult> SaveCard([FromBody] SaveCardDto dto)
        {
            var options = new PaymentMethodAttachOptions
            {
                Customer = dto.CustomerId,
            };

            var service = new PaymentMethodService();

            return(Ok(await service.AttachAsync(dto.PaymentMethodIds, options)));
        }
Beispiel #7
0
        public string SignUserUpToSubscription(string paymentMethodId, User user, ClubSubscription clubSubscription)
        {
            var options = new PaymentMethodAttachOptions
            {
                Customer = user.StripeUserId,
            };
            var service       = new PaymentMethodService();
            var paymentMethod = service.Attach(paymentMethodId, options);

            // Update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                },
            };
            var customerService = new CustomerService();

            customerService.Update(user.StripeUserId, customerOptions);

            // Create subscription
            var subscriptionOptions = new SubscriptionCreateOptions
            {
                Customer = user.StripeUserId,
                Items    = new List <SubscriptionItemOptions>()
                {
                    new SubscriptionItemOptions
                    {
                        Price = clubSubscription.StripePriceId,
                    },
                },
                Metadata = new Dictionary <string, string>()
                {
                    { "UserId", user.UserId.ToString() },
                    { "ClubSubscriptionId", clubSubscription.ClubSubscriptionId.ToString() },
                }
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");
            var subscriptionService = new SubscriptionService();

            try
            {
                Subscription subscription = subscriptionService.Create(subscriptionOptions);
                return("Went good");
            }
            catch (StripeException e)
            {
                Console.WriteLine($"Failed to create subscription.{e}");
                return("Went bad");
                // return BadRequest();
            }
        }
Beispiel #8
0
        public ActionResult <SubscriptionResponse> CreateSubscription([FromBody] CreateSubscriptionRequest req)
        {
            var customerId = HttpContext.Request.Cookies["customer"];

            // Attach payment method
            PaymentMethod paymentMethod;

            try
            {
                var options = new PaymentMethodAttachOptions
                {
                    Customer = customerId,
                };
                var service = new PaymentMethodService();
                paymentMethod = service.Attach(req.PaymentMethod, options);
            }
            catch (StripeException e)
            {
                return(BadRequest(new { error = new { message = e.Message } }));
            }

            // Create subscription
            var subscriptionOptions = new SubscriptionCreateOptions
            {
                DefaultPaymentMethod = paymentMethod.Id,
                Customer             = customerId,
                Items = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = Environment.GetEnvironmentVariable(req.Price.ToUpper()),
                    },
                },
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");
            var subscriptionService = new SubscriptionService();

            try
            {
                Subscription subscription = subscriptionService.Create(subscriptionOptions);

                return(new SubscriptionResponse
                {
                    Subscription = subscription
                });
            }
            catch (StripeException e)
            {
                Console.WriteLine($"Failed to create subscription.{e}");
                return(BadRequest());
            }
        }
        public async Task <IActionResult> CreateSubscription([FromBody] CreateSubscription sub)
        {
            var option = new PaymentMethodAttachOptions
            {
                Customer = sub.Customer,
            };

            var service = new PaymentMethodService();

            var paymentMethod = service.Attach(sub.PaymentMethod, option);

            // Update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                },
            };
            var customerService = new CustomerService();
            await customerService.UpdateAsync(sub.Customer, customerOptions);

            var subscriptionOptions = new SubscriptionCreateOptions
            {
                Customer = sub.Customer,
                Items    = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = Environment.GetEnvironmentVariable(sub.Price),
                    }
                },
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");

            var subscriptionService = new SubscriptionService();

            try
            {
                Subscription subscription = subscriptionService.Create(subscriptionOptions);
                return(Ok(new ResponseViewModel <Subscription>
                {
                    Data = subscription,
                    Message = StripeConstants.SubscriptionAdded
                }));
            }
            catch (StripeException e)
            {
                Console.WriteLine($"Failed to create subscription.{e}");
                return(BadRequest());
            }
        }
Beispiel #10
0
        public ActionResult <Subscription> CreateSubscription(SubscriptionCreateRequest req)
        {
            var myCustomer      = req.CustomerId;
            var myPaymentMethod = req.PaymentMethodId;
            var myPrice         = req.PriceId;

            // attach payment method
            var options = new PaymentMethodAttachOptions
            {
                Customer = myCustomer,
            };

            _paymentMethodService.Attach(myPaymentMethod, options);

            // update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = myPaymentMethod,
                },
            };

            _customerService.Update(myCustomer, customerOptions);

            //create subscription
            var subscriptionOptions = new SubscriptionCreateOptions
            {
                Customer = myCustomer,
                Items    = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price = myPrice,
                    },
                },
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");
            try
            {
                Subscription subscription = _subscriptionService.Create(subscriptionOptions);
                return(subscription);
            }
            catch (StripeException e)
            {
                return(BadRequest(e));
            }
        }
Beispiel #11
0
        private PaymentMethod AttachStripePaymentMethod(string customerId, string paymentMethodId)
        {
            StripeConfiguration.ApiKey = _appKeys.StripeApiKey;

            var options = new PaymentMethodAttachOptions
            {
                Customer = customerId,
            };
            var service       = new PaymentMethodService();
            var paymentMethod = service.Attach(
                paymentMethodId,
                options
                );

            return(paymentMethod);
        }
        public async Task AddCreditCardAsync(string customerId, string cardholderName, string number, string cvc, long expMonth, long expYear, string zipCode)
        {
            var paymentMethodCreateOptions = new PaymentMethodCreateOptions
            {
                Type = "card",
                Card = new PaymentMethodCardOptions
                {
                    Number   = number,
                    Cvc      = cvc,
                    ExpMonth = expMonth,
                    ExpYear  = expYear
                },

                BillingDetails = new PaymentMethodBillingDetailsOptions
                {
                    Name    = cardholderName,
                    Address = new AddressOptions
                    {
                        PostalCode = zipCode
                    }
                }
            };

            var paymentMethodService = new PaymentMethodService();

            var paymentMethod = await paymentMethodService.CreateAsync(paymentMethodCreateOptions);

            var cards = await paymentMethodService.ListAsync(new PaymentMethodListOptions { Customer = customerId, Type = "card" });

            foreach (var card in cards)
            {
                await paymentMethodService.DetachAsync(card.Id);
            }

            var paymentMethodAttachOptions = new PaymentMethodAttachOptions
            {
                Customer = customerId
            };

            await paymentMethodService.AttachAsync(paymentMethod.Id, paymentMethodAttachOptions);
        }
Beispiel #13
0
        public bool AddCardToExistingCustomer(SetupIntent intent, string StripeCustomerID)
        {
            bool result = true;

            try
            {
                var options = new PaymentMethodAttachOptions
                {
                    Customer = StripeCustomerID,
                };
                var service       = new PaymentMethodService();
                var paymentMethod = service.Attach(intent.PaymentMethodId, options);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                result = false;
            }

            return(result);
        }
        public PaymentMethodServiceTest(
            StripeMockFixture stripeMockFixture,
            MockHttpClientFixture mockHttpClientFixture)
            : base(stripeMockFixture, mockHttpClientFixture)
        {
            this.service = new PaymentMethodService(this.StripeClient);

            this.attachOptions = new PaymentMethodAttachOptions
            {
                Customer = "cus_123",
            };

            this.createOptions = new PaymentMethodCreateOptions
            {
                Card = new PaymentMethodCardOptions
                {
                    Token = "tok_123",
                },
                Type = "card",
            };

            this.detachOptions = new PaymentMethodDetachOptions
            {
            };

            this.listOptions = new PaymentMethodListOptions
            {
                Customer = "cus_123",
                Type     = "card",
            };

            this.updateOptions = new PaymentMethodUpdateOptions
            {
                Metadata = new Dictionary <string, string>
                {
                    { "key", "value" },
                },
            };
        }
Beispiel #15
0
        public static void AddNewPaymentOption(string cardNumber, int expiryMonth, int expiryYear, string cvc)
        {
            StripeConfiguration.ApiKey = Constants.StripeKey;
            var options = new PaymentMethodCreateOptions
            {
                Type = "card",
                Card = new PaymentMethodCardCreateOptions
                {
                    Number   = cardNumber,
                    ExpMonth = expiryMonth,
                    ExpYear  = expiryYear,
                    Cvc      = cvc
                }
            };
            var           service       = new PaymentMethodService();
            PaymentMethod pmMethod      = service.Create(options);
            var           attachOptions = new PaymentMethodAttachOptions
            {
                Customer = App.PaymentId
            };
            var attachService = new PaymentMethodService();

            attachService.Attach(pmMethod.Id, attachOptions);
        }
Beispiel #16
0
        public static async Task <dynamic> CreateClientAsync(string email, string name, string cardNumber, int month, int year, string cvv)
        {
            var optionstoken = new TokenCreateOptions
            {
                Card = new CreditCardOptions
                {
                    Number   = cardNumber,
                    ExpMonth = month,
                    ExpYear  = year,
                    Cvc      = cvv
                }
            };

            var   servicetoken = new TokenService();
            Token stripetoken  = await servicetoken.CreateAsync(optionstoken);

            var customer = new CustomerCreateOptions
            {
                Email  = email,
                Name   = name,
                Source = stripetoken.Id,
            };

            Console.WriteLine(" stripetoken attributes :" + stripetoken);

            var services = new CustomerService();
            var created  = services.Create(customer);


            var option = new PaymentMethodCreateOptions
            {
                Type = "card",
                Card = new PaymentMethodCardCreateOptions
                {
                    Number   = cardNumber,
                    ExpMonth = month,
                    ExpYear  = year,
                    Cvc      = cvv,
                },
            };

            var service = new PaymentMethodService();
            var result  = service.Create(option);

            Console.WriteLine(" PaymentMethodService attributes :" + result);

            var options = new PaymentMethodAttachOptions
            {
                Customer = created.Id,
            };
            var method = new PaymentMethodService();

            method.Attach(
                result.Id,
                options
                );

            if (created.Id == null)
            {
                return("Failed");
            }
            else
            {
                return(created.Id);
            }
        }
        public ActionResult <Subscription> CreateSubscription([FromBody] CreateSubscriptionRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(this.FailWithMessage("invalid params"));
            }
            var newPrice = Environment.GetEnvironmentVariable(req.Price.ToUpper());

            if (newPrice is null || newPrice == "")
            {
                return(this.FailWithMessage($"No price with the new price ID ({req.Price}) found in .env"));
            }

            // Attach payment method
            var options = new PaymentMethodAttachOptions
            {
                Customer = req.Customer,
            };
            var service = new PaymentMethodService();

            PaymentMethod paymentMethod;

            try
            {
                paymentMethod = service.Attach(req.PaymentMethod, options);
            }
            catch (Exception e)
            {
                return(this.FailWithMessage($"Failed to attach payment method {e}"));
            }

            // Update customer's default invoice payment method
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                },
            };
            var customerService = new CustomerService();

            try
            {
                customerService.Update(req.Customer, customerOptions);
            }
            catch (StripeException e)
            {
                return(this.FailWithMessage($"Failed to attach payment method {e}"));
            }

            // Create subscription
            var subscriptionOptions = new SubscriptionCreateOptions
            {
                Customer = req.Customer,
                Items    = new List <SubscriptionItemOptions>
                {
                    new SubscriptionItemOptions
                    {
                        Price    = Environment.GetEnvironmentVariable(req.Price),
                        Quantity = req.Quantity,
                    },
                },
            };

            subscriptionOptions.AddExpand("latest_invoice.payment_intent");
            var subscriptionService = new SubscriptionService();

            try
            {
                return(subscriptionService.Create(subscriptionOptions));
            }
            catch (StripeException e)
            {
                return(this.FailWithMessage($"Failed to attach payment method: {e}"));
            }
        }
Beispiel #18
0
        async public Task <string> AttachPaymentMethodAsync(string id, [FromBody] PaymentMethodAttachOptions paymentMethodAttachOptions)
        {
            var res = await _paymentMethodService.AttachAsync(id, paymentMethodAttachOptions, _requestOptions);

            return(res.Id);
        }
        public async Task <Invoice> CreateInvoice(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "invoice/create")]
            CreatePaymentRequest request,
            ILogger log)
        {
            // このサンプルでは input validation をしていません。


            // 既存の Customer に紐づけることもできますが、今回は毎回 Customer を作成します。
            var customerCreateOptions = new CustomerCreateOptions
            {
                Email           = request.Email,
                PaymentMethod   = request.PaymentMethodId,
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = request.PaymentMethodId
                }
            };

            var customer = await _customerService.CreateAsync(customerCreateOptions);


            // Attach payment method:
            // 新規作成した Customer に PaymentMethod を紐づけます。
            // paymentMethodMethod は既に別の customer に紐づけられている場合エラーになります。
            var options = new PaymentMethodAttachOptions
            {
                Customer = customer.Id
            };

            var paymentMethod = await _paymentMethodService.AttachAsync(request.PaymentMethodId, options);

            // Update customer's default payment method.
            var customerOptions = new CustomerUpdateOptions
            {
                InvoiceSettings = new CustomerInvoiceSettingsOptions
                {
                    DefaultPaymentMethod = paymentMethod.Id,
                }
            };

            await _customerService.UpdateAsync(customer.Id, customerOptions);


            // Create InvoiceItem.
            var createOptions = new InvoiceItemCreateOptions
            {
                Customer = customer.Id,
                Price    = request.PriceId
            };

            await _invoiceItemService.CreateAsync(createOptions);


            // Create Invoice
            var invoiceOptions = new InvoiceCreateOptions
            {
                Customer    = customer.Id,
                AutoAdvance = true,
            };

            var invoiceResult = await _invoiceService.CreateAsync(invoiceOptions);

            //ここで、Stripe の Dashboard > Billing > インボイスにインボイスが追加されます。

            return(invoiceResult);
        }