Ejemplo n.º 1
0
        /// <summary>
        /// Creates a token as per
        /// https://github.com/jaymedavis/stripe.net#creating-a-token
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public static StripeToken CreateTestToken(ICustomerEntity customer = null)
        {
            var myToken = new StripeTokenCreateOptions();

            // if you need this...
            myToken.Card = new StripeCreditCardOptions()
            {
                // set these properties if passing full card details (do not
                // set these properties if you set TokenId)
                Number          = "4242424242424242",
                ExpirationYear  = "2063",
                ExpirationMonth = "10",
                AddressCountry  = "US",               // optional
                AddressLine1    = "24 Beef Flank St", // optional
                AddressLine2    = "Apt 24",           // optional
                AddressCity     = "Biggie Smalls",    // optional
                AddressState    = "NC",               // optional
                AddressZip      = "27617",            // optional
                Name            = "Joe Meatballs",    // optional
                Cvc             = "1223"              // optional
            };

            if (customer != null)
            {
                myToken.CustomerId = customer.PaymentSystemId;
            }

            var         tokenService = new StripeTokenService();
            StripeToken stripeToken  = tokenService.Create(myToken);

            return(stripeToken);
        }
Ejemplo n.º 2
0
        public void TestCustomers()
        {
            // SUBSCRIBE

            // Arrange
            ICustomerEntity customer = CreateMockCustomer();

            // Act
            StripeManager.CreateCustomer(customer);

            // Assert
            Assert.IsNotNull(customer.PaymentSystemId);

            // RETRIEVE

            // Act
            StripeCustomer stripeCustomer = StripeManager.RetrieveCustomer(customer);

            // Assert
            Assert.IsNotNull(stripeCustomer);

            // UPDATE

            // Arrange
            customer.Email = "*****@*****.**";

            // act
            StripeCustomer updatedStripeCustomer = StripeManager.UpdateCustomer(customer);

            // Assert
            Assert.IsNotNull(updatedStripeCustomer);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves the StripeCustomer associated with the given IStripeUser instance
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public static StripeCustomer RetrieveCustomer(ICustomerEntity customer)
        {
            var            customerService = new StripeCustomerService();
            StripeCustomer stripeCustomer  = customerService.Get(customer.PaymentSystemId);

            return(stripeCustomer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new customer record in Stripe for the given user
        /// This will set the "PaymentSystemId" property on the given IStripeUser instance if the user was successfully created
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="paymentToken"></param>
        public static StripeCustomer CreateCustomer(ICustomerEntity customer, string paymentToken = null)
        {
            // Do not overwrite the user, ever
            if (customer.HasPaymentInfo())
            {
                return(null);
            }

            var newCustomer = new StripeCustomerCreateOptions();

            newCustomer.Email = customer.Email;

            if (paymentToken != null)
            {
                newCustomer.Source = new StripeSourceOptions()
                {
                    TokenId = paymentToken
                }
            }
            ;

            var            customerService = new StripeCustomerService();
            StripeCustomer stripeCustomer  = customerService.Create(newCustomer);

            // Set the accounting info
            customer.PaymentSystemId = stripeCustomer.Id;

            Logger.Log <StripeManager>("Created customer in stripe: '{0}' with id '{1}", LogLevel.Information, customer.Email, customer.PaymentSystemId);

            return(stripeCustomer);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Charges the given user one time for the given price in USD
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="price"></param>
        /// <param name="chargeDescription"></param>
        /// <returns></returns>
        public static string Charge(ICustomerEntity customer, float price, string chargeDescription = "")
        {
            var charge = CreateChargeOptions(price, chargeDescription);

            // setting up the card
            charge.CustomerId = customer.PaymentSystemId;

            return(ExecuteCharge(charge));
        }
Ejemplo n.º 6
0
        public ICustomerEntity Add(ICustomerEntity entity)
        {
            var collection = _ds.GetCollection <Customer>();

            entity.Id = collection.AsQueryable().Max(a => a.Id) + 1;

            collection.InsertOne((Customer)entity);

            return(entity);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates or update a customer
 /// </summary>
 /// <param name="customer"></param>
 /// <param name="paymentToken"></param>
 public static void CreateOrUpdateCustomer(ICustomerEntity customer, string paymentToken = null)
 {
     if (customer.HasPaymentInfo())
     {
         UpdateCustomer(customer, paymentToken);
     }
     else
     {
         CreateCustomer(customer, paymentToken);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Changes the given subscription to use the new plan
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <param name="newPlan"></param>
        /// <returns></returns>
        public static StripeSubscription ChangeSubscriptionPlan(ICustomerEntity customer, ISubscriptionEntity subscription, IPlanEntity newPlan)
        {
            StripeSubscriptionUpdateOptions options = new StripeSubscriptionUpdateOptions()
            {
                PlanId = newPlan.PaymentSystemId
            };

            var subscriptionService = new StripeSubscriptionService();
            StripeSubscription changedSubscription = subscriptionService.Update(customer.PaymentSystemId, subscription.PaymentSystemId, options);

            Logger.Log <StripeManager>("Changed subscription for customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);

            return(changedSubscription);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Unsubscribes the given subscription
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <returns></returns>
        public static StripeSubscription Unsubscribe(ICustomerEntity customer, ISubscriptionEntity subscription)
        {
            if (string.IsNullOrEmpty(subscription.PaymentSystemId) || string.IsNullOrEmpty(customer.PaymentSystemId))
            {
                return(null);
            }

            var subscriptionService = new StripeSubscriptionService();
            StripeSubscription sub  = subscriptionService.Cancel(customer.PaymentSystemId, subscription.PaymentSystemId);

            subscription.PaymentSystemId = null;

            Logger.Log <StripeManager>("Unsuscribed customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);
            return(sub);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Subscribes the given user to the given plan, using the payment information already in stripe for that user
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="subscription"></param>
        /// <param name="plan"></param>
        /// <returns></returns>
        public static StripeSubscription Subscribe(ICustomerEntity customer, ISubscriptionEntity subscription, IPlanEntity plan)
        {
            if (!string.IsNullOrEmpty(subscription.PaymentSystemId))
            {
                return(null);
            }

            var subscriptionService            = new StripeSubscriptionService();
            StripeSubscription newSubscription = subscriptionService.Create(customer.PaymentSystemId, plan.PaymentSystemId);

            subscription.PaymentSystemId = newSubscription.Id;

            Logger.Log <StripeManager>("Subscribed customer in stripe: '{0}' with new subscription id '{1}", LogLevel.Information, customer.Email, subscription.PaymentSystemId);
            return(newSubscription);
        }
Ejemplo n.º 11
0
        public void TestChargeWithCustomer()
        {
            // Arrange
            ICustomerEntity customer = CreateMockCustomer();
            IChargeEntity   charge   = CreateMockCharge();

            StripeToken token = CreateTestToken(customer);

            StripeManager.CreateCustomer(customer, token.Id);

            // Act - charge customer
            string chargeId = StripeManager.Charge(customer, charge);

            Assert.IsNotNull(chargeId);

            chargeId = StripeManager.Charge(customer, 12.34f, "Test charge with customer");
            Assert.IsNotNull(chargeId);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Updates a customer record, using the given payment token
        /// NOTE: Save changes on the underlying context for the model after calling this method
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="paymentToken"></param>
        public static StripeCustomer UpdateCustomer(ICustomerEntity customer, string paymentToken = null)
        {
            var customerUpdate = new StripeCustomerUpdateOptions()
            {
                Email = customer.Email
            };

            // Create a token for this payment token
            customerUpdate.Source = new StripeSourceOptions()
            {
                TokenId = paymentToken
            };

            var            customerService = new StripeCustomerService();
            StripeCustomer updatedCustomer = customerService.Update(customer.PaymentSystemId, customerUpdate);

            Logger.Log <StripeManager>("Updated customer in stripe: '{0}' with id '{1}", LogLevel.Information, customer.Email, customer.PaymentSystemId);

            return(updatedCustomer);
        }
Ejemplo n.º 13
0
        public void TestSubscriptions()
        {
            // Arrange
            EnsureTestPlansDeleted();

            // NOTE: Due to the reliance on the API, we must create these for real
            IPlanEntity planA = CreateMockPlanA();

            StripeManager.CreatePlan(planA);

            ICustomerEntity customer = CreateMockCustomer();

            StripeManager.CreateCustomer(customer);

            ISubscriptionEntity subscription = CreateMockSubscription();

            // CREATE
            // Subscribe

            // Act
            StripeSubscription newSub = StripeManager.Subscribe(customer, subscription, planA);

            Assert.IsNotNull(newSub);

            // CHANGE
            // ChangeSubscriptionPlan

            IPlanEntity planB = CreateMockPlanB();

            StripeManager.CreatePlan(planB);

            StripeSubscription changedSub = StripeManager.ChangeSubscriptionPlan(customer, subscription, planB);

            Assert.IsNotNull(changedSub);

            // DELETE
            StripeSubscription cancelledSub = StripeManager.Unsubscribe(customer, subscription);

            Assert.IsNotNull(cancelledSub);
            Assert.IsTrue(cancelledSub.Status == "canceled");
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Charges the given user for the given product
 /// </summary>
 /// <param name="user"></param>
 /// <param name="product"></param>
 /// <returns></returns>
 public static string Charge(ICustomerEntity user, IChargeEntity product)
 {
     return(Charge(user, product.Price, product.Title));
 }
Ejemplo n.º 15
0
 public CustomerService(ICustomerEntity entity)
 {
     Entity = entity;
 }
Ejemplo n.º 16
0
        public bool Update(ICustomerEntity entity)
        {
            var collection = _ds.GetCollection <Customer>();

            return(collection.UpdateOne(entity.Id, (Customer)entity));
        }
Ejemplo n.º 17
0
 public CustomerModel(ICustomerEntity customerEntity)
 {
     _customerEntity = customerEntity;
 }