Beispiel #1
0
func withTestCoupon(amount uint64, f func(string)) {
	c, err := coupon.New(&stripe.CouponParams{
		Amount:   amount,
		Duration: "once",
		Currency: "usd",
	})
	So(err, ShouldBeNil)
	So(c, ShouldNotBeNil)
	f(c.ID)
	c1, err := coupon.Del(c.ID)
	So(err, ShouldBeNil)
	So(c1, ShouldNotBeNil)
	So(c1.Deleted, ShouldBeTrue)
}
Beispiel #2
0
func withTestCoupon(f func(string)) {
	c, err := coupon.New(&stripe.CouponParams{
		Amount:   100,
		Duration: "once",
		Currency: "usd",
		// id will be autogenerated
		// ID:       fmt.Sprintf("coupon_%s", bson.NewObjectId().Hex()),
	})
	So(err, ShouldBeNil)
	So(c, ShouldNotBeNil)
	f(c.ID)
	c1, err := coupon.Del(c.ID)
	So(err, ShouldBeNil)
	So(c1, ShouldNotBeNil)
	So(c1.Deleted, ShouldBeTrue)
}
Beispiel #3
0
func TestCustomerDiscount(t *testing.T) {
	couponParams := &stripe.CouponParams{
		Duration: coupon.Forever,
		ID:       "customer_coupon",
		Percent:  99,
	}

	coupon.New(couponParams)

	customerParams := &stripe.CustomerParams{
		Coupon: "customer_coupon",
	}

	target, err := New(customerParams)

	if err != nil {
		t.Error(err)
	}

	if target.Discount == nil {
		t.Errorf("Discount not found, but one was expected\n")
	}

	if target.Discount.Coupon == nil {
		t.Errorf("Coupon not found, but one was expected\n")
	}

	if target.Discount.Coupon.ID != customerParams.Coupon {
		t.Errorf("Coupon id %q does not match expected id %q\n", target.Discount.Coupon.ID, customerParams.Coupon)
	}

	discountDel, err := discount.Del(target.ID)

	if err != nil {
		t.Error(err)
	}

	if !discountDel.Deleted {
		t.Errorf("Discount expected to be marked as deleted on the returned resource\n")
	}

	Del(target.ID)
	coupon.Del("customer_coupon")
}
Beispiel #4
0
func TestSubscriptionDiscount(t *testing.T) {
	couponParams := &stripe.CouponParams{
		Duration: coupon.Forever,
		ID:       "sub_coupon",
		Percent:  99,
	}

	coupon.New(couponParams)

	customerParams := &stripe.CustomerParams{
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "378282246310005",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	cust, _ := customer.New(customerParams)

	planParams := &stripe.PlanParams{
		ID:       "test",
		Name:     "Test Plan",
		Amount:   99,
		Currency: currency.USD,
		Interval: plan.Month,
	}

	plan.New(planParams)

	subParams := &stripe.SubParams{
		Customer: cust.ID,
		Plan:     "test",
		Quantity: 10,
		Coupon:   "sub_coupon",
	}

	target, err := New(subParams)

	if err != nil {
		t.Error(err)
	}

	if target.Discount == nil {
		t.Errorf("Discount not found, but one was expected\n")
	}

	if target.Discount.Coupon == nil {
		t.Errorf("Coupon not found, but one was expected\n")
	}

	if target.Discount.Coupon.ID != subParams.Coupon {
		t.Errorf("Coupon id %q does not match expected id %q\n", target.Discount.Coupon.ID, subParams.Coupon)
	}

	_, err = discount.DelSub(target.ID)

	if err != nil {
		t.Error(err)
	}

	target, err = Get(target.ID, nil)

	if err != nil {
		t.Error(err)
	}

	if target.Discount != nil {
		t.Errorf("A discount %v was found, but was expected to have been deleted\n", target.Discount)
	}

	customer.Del(cust.ID)
	plan.Del("test")
	coupon.Del("sub_coupon")
}
Beispiel #5
0
func TestOrderUpdate(t *testing.T) {
	sku := CreateTestProductAndSku(t)

	o, err := New(&stripe.OrderParams{
		Currency: currency.USD,
		Items: []*stripe.OrderItemParams{
			{
				Type:   "sku",
				Parent: sku.ID,
			},
		},
		Shipping: &stripe.ShippingParams{
			Name: "Jenny Rosen",
			Address: &stripe.AddressParams{
				Line1:      "1234 Main Street",
				City:       "Anytown",
				Country:    "US",
				PostalCode: "123456",
			},
			Phone: "6504244242",
		},
		Email: "*****@*****.**",
		Params: stripe.Params{
			Meta: map[string]string{
				"foo": "bar",
			},
		},
	})

	if err != nil {
		t.Fatalf("%+v", err)
	}

	couponParams := &stripe.CouponParams{
		Amount:         99,
		Currency:       currency.USD,
		Duration:       coupon.Once,
		DurationPeriod: 3,
		Redemptions:    1,
		RedeemBy:       time.Now().AddDate(0, 0, 30).Unix(),
	}

	c, err := coupon.New(couponParams)

	updated, err := Update(
		o.ID,
		&stripe.OrderUpdateParams{
			Coupon: c.ID,
			Params: stripe.Params{
				Meta: map[string]string{"foo": "baz"},
			},
			Status: stripe.StatusCanceled,
		},
	)

	if err != nil {
		t.Fatalf("%+v", err)
	}

	if updated.Status != stripe.StatusCanceled {
		t.Errorf("Order status not updated: %v", updated.Status)
	}

	hasCoupon := false
	for _, item := range updated.Items {
		if item.Type == orderitem.Discount {
			hasCoupon = true
		}
	}
	if !hasCoupon {
		t.Errorf("Discount code not applied: %v", updated.Items)
	}

	if updated.Meta["foo"] != "baz" {
		t.Errorf("Order metadata not updated: %v", updated.Meta["foo"])
	}

	coupon.Del(c.ID)
}