Example #1
0
// UpdateCustomerForGroup updates customer data of a group
func UpdateCustomerForGroup(username, groupName string, params *stripe.CustomerParams) (*stripe.Customer, error) {
	if _, err := EnsureCustomerForGroup(username, groupName, params); err != nil {
		return nil, err
	}

	group, err := modelhelper.GetGroup(groupName)
	if err != nil {
		return nil, err
	}

	if group.Payment.Customer.ID == "" {
		return nil, ErrCustomerNotExists
	}

	params, err = populateCustomerParams(username, groupName, params)
	if err != nil {
		return nil, err
	}

	// if the update request has a new CC, delete old ones.
	if params != nil && params.Source != nil && params.Source.Token != "" {
		if err := DeleteCreditCardForGroup(groupName); err != nil {
			return nil, err
		}
	}

	return customer.Update(group.Payment.Customer.ID, params)
}
Example #2
0
// TestBalanceApply does not test anything on our end. And i am not trying to be
// clever with testing stripe. This test is here only for making sure about the
// logic of Amount, Subtotal And the Total. This is the third time i am
// forgetting the logic and wanted to document it here with code.
func TestBalanceApply(t *testing.T) {
	Convey("Given a user who subscribed to a paid plan", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withNonFreeTestPlan(func(planID string) {
					addCreditCardToUserWithChecks(endpoint, sessionID)
					withSubscription(endpoint, groupName, sessionID, planID, func(subscriptionID string) {
						withTestCoupon(func(couponID string) {
							Convey("After adding balance to the user", func() {
								group, err := modelhelper.GetGroup(groupName)
								tests.ResultedWithNoErrorCheck(group, err)
								var subtotal int64 = 12345
								// A negative amount represents a credit that
								// decreases the amount due on an invoice; a
								// positive amount increases the amount due on
								// an invoice.
								var balance int64 = -150
								var coupon int64 = 100

								expectedAmount := subtotal - coupon - (-balance) // negate the balance
								cp := &stripe.CustomerParams{
									Balance: balance,
									Coupon:  couponID,
								}

								c, err := customer.Update(group.Payment.Customer.ID, cp)
								tests.ResultedWithNoErrorCheck(c, err)

								Convey("Customer should have discount", func() {
									So(c, ShouldNotBeNil)
									So(c.Balance, ShouldEqual, balance)

									Convey("Invoice should the discount", func() {
										i, err := invoice.GetNext(&stripe.InvoiceParams{Customer: c.ID})
										tests.ResultedWithNoErrorCheck(i, err)
										So(i.Subtotal, ShouldEqual, subtotal)
										So(i.Subtotal, ShouldBeGreaterThan, i.Total)
										So(i.Subtotal, ShouldEqual, i.Total+coupon) // dont forget to negate

										So(i.Total, ShouldEqual, subtotal-coupon)
										So(i.Total, ShouldBeGreaterThan, i.Amount)
										So(i.Total, ShouldEqual, i.Amount+(-balance))

										So(i.Amount, ShouldEqual, i.Total-(-balance))
										So(i.Amount, ShouldEqual, expectedAmount)
										// Subtotal = amount + coupon + balance
										// Total    = amount + coupon
										// Amount   = the final price that customer will pay.

										// Subtotal:     12345,
										// Total:        12245,
										// Amount:       12145,

										// Expected: '12245'
										// Actual:   '12445'
									})
								})
							})
						})
					})
				})
			})
		})
	})
}