Ejemplo n.º 1
0
func ExampleInvoice_update() {
	stripe.Key = "sk_key"

	params := &stripe.InvoiceParams{
		Desc: "updated description",
	}

	inv, err := invoice.Update("sub_example_id", params)

	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%v\n", inv.Desc)
}
Ejemplo n.º 2
0
func invoiceCreatedHandler(raw []byte) error {
	var invoice stripe.Invoice
	err := json.Unmarshal(raw, &invoice)
	if err != nil {
		return err
	}

	// we cant do anything to the closed invoices.
	if invoice.Closed {
		return nil
	}

	if invoice.Paid {
		return nil
	}

	cus, err := customer.Get(invoice.Customer.ID, nil)
	if err != nil {
		return err
	}

	group, err := modelhelper.GetGroup(cus.Meta["groupName"])
	// we might get events from other environments where we might not have the
	// group in this env.
	if err == mgo.ErrNotFound {
		return nil
	}

	if err != nil {
		return err
	}

	// if customer id and subscription id is not set, we dont have the
	// appropriate data in our system, dont bother with the rest
	if group.Payment.Customer.ID == "" {
		return nil
	}

	if group.Payment.Subscription.ID == "" {
		return nil
	}

	info, err := GetInfoForGroup(group)
	if err == mgo.ErrNotFound {
		return nil
	}

	if err != nil {
		return err
	}

	if strings.HasPrefix(info.Subscription.Plan.ID, customPlanPrefix) {
		return (&models.PresenceDaily{}).ProcessByGroupName(group.Slug)
	}

	// if the amount that stripe will withdraw and what we want is same, so we
	// are done. Subtotal -> Total of all subscriptions, invoice items, and
	// prorations on the invoice before any discount is applied
	if invoice.Subtotal == int64(info.Due) {
		// clean up waiting deleted users
		return (&models.PresenceDaily{}).ProcessByGroupName(group.Slug)
	}

	// if this in the tests, just skip cancellation of the invoice, because
	// there is no way to create an invoice and have it open for a while.
	if invoice.ID != "in_00000000000000" {
		// first close the invoice
		_, err = stripeinvoice.Update(invoice.ID, &stripe.InvoiceParams{Closed: true})
		if err != nil {
			return err
		}
	}

	return switchToNewSub(info)
}