func TestCardUpdate(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", Name: "Original Name", }) cust, err := customer.New(customerParams) if err != nil { t.Error(err) } cardParams := &stripe.CardParams{ Customer: cust.ID, Name: "Updated Name", } target, err := Update(cust.DefaultSource.ID, cardParams) if err != nil { t.Error(err) } if target.Name != cardParams.Name { t.Errorf("Card name %q does not match expected name %q\n", target.Name, cardParams.Name) } customer.Del(cust.ID) }
func TestSourceCardGet(t *testing.T) { customerParams := &stripe.CustomerParams{ Email: "*****@*****.**", } customerParams.SetSource(&stripe.CardParams{ Number: "4242424242424242", Month: "06", Year: "20", }) cust, err := customer.New(customerParams) if err != nil { t.Error(err) } source, err := Get(cust.DefaultSource.ID, &stripe.CustomerSourceParams{Customer: cust.ID}) if err != nil { t.Error(err) } target := source.Card if target.LastFour != "4242" { t.Errorf("Unexpected last four %q for card number %v\n", target.LastFour, customerParams.Source.Card.Number) } if target.Brand != card.Visa { t.Errorf("Card brand %q does not match expected value\n", target.Brand) } customer.Del(cust.ID) }
// EnsureCustomerForGroup registers a customer for a group if it does not exist, // returns the existing one if created previously func EnsureCustomerForGroup(username string, groupName string, req *stripe.CustomerParams) (*stripe.Customer, error) { group, err := modelhelper.GetGroup(groupName) if err != nil { return nil, err } // if we already have the customer, return it. if group.Payment.Customer.ID != "" { return customer.Get(group.Payment.Customer.ID, nil) } req, err = populateCustomerParams(username, group.Slug, req) if err != nil { return nil, err } cus, err := customer.New(req) if err != nil { return nil, err } if err := modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ "$set": modelhelper.Selector{ "payment.customer.id": cus.ID, }, }, ); err != nil { return nil, err } return cus, nil }
func TestBankAccountDel(t *testing.T) { baTok, err := token.New(&stripe.TokenParams{ Bank: &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", AccountHolderName: "Jane Austen", AccountHolderType: "individual", }, }) if err != nil { t.Error(err) } customerParams := &stripe.CustomerParams{} customerParams.SetSource(baTok.ID) cust, _ := customer.New(customerParams) if err != nil { t.Error(err) } baDel, err := Del(cust.DefaultSource.ID, &stripe.BankAccountParams{Customer: cust.ID}) if err != nil { t.Error(err) } if !baDel.Deleted { t.Errorf("Bank account id %q expected to be marked as deleted on the returned resource\n", baDel.ID) } customer.Del(cust.ID) }
func TestBankAccountListByCustomer(t *testing.T) { baTok, err := token.New(&stripe.TokenParams{ Bank: &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", AccountHolderName: "Jane Austen", AccountHolderType: "individual", }, }) if err != nil { t.Error(err) } customerParams := &stripe.CustomerParams{} customerParams.SetSource(baTok.ID) cust, _ := customer.New(customerParams) if err != nil { t.Error(err) } iter := List(&stripe.BankAccountListParams{Customer: cust.ID}) if iter.Err() != nil { t.Error(err) } if !iter.Next() { t.Errorf("Expected to find one bank account in list\n") } Del(cust.DefaultSource.ID, &stripe.BankAccountParams{Customer: cust.ID}) customer.Del(cust.ID) }
func TestCardList(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) card := &stripe.CardParams{ Number: "4242424242424242", Month: "10", Year: "20", Customer: cust.ID, } New(card) i := List(&stripe.CardListParams{Customer: cust.ID}) for i.Next() { if i.Card() == nil { t.Error("No nil values expected") } if i.Meta() == nil { t.Error("No metadata returned") } } if err := i.Err(); err != nil { t.Error(err) } customer.Del(cust.ID) }
func TestSubscriptionNew(t *testing.T) { 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, TaxPercent: 20.0, BillingCycleAnchor: time.Now().AddDate(0, 0, 12).Unix(), } target, err := New(subParams) if err != nil { t.Error(err) } if target.Plan.ID != subParams.Plan { t.Errorf("Plan %v does not match expected plan %v\n", target.Plan, subParams.Plan) } if target.Quantity != subParams.Quantity { t.Errorf("Quantity %v does not match expected quantity %v\n", target.Quantity, subParams.Quantity) } if target.TaxPercent != subParams.TaxPercent { t.Errorf("TaxPercent %f does not match expected TaxPercent %f\n", target.TaxPercent, subParams.TaxPercent) } if target.PeriodEnd != subParams.BillingCycleAnchor { t.Errorf("PeriodEnd %v does not match expected BillingCycleAnchor %v\n", target.PeriodEnd, subParams.BillingCycleAnchor) } customer.Del(cust.ID) plan.Del("test") }
func createSubItem(t *testing.T) (*stripe.Sub, *stripe.SubItem, func()) { customerParams := &stripe.CustomerParams{ Source: &stripe.SourceParams{ Card: &stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }, }, } cust, err := customer.New(customerParams) if err != nil { t.Fatalf("customer creation: %s", err) } planID := fmt.Sprintf("test-%d", rand.Int63()) planParams := &stripe.PlanParams{ ID: planID, Name: "Test Plan", Amount: 99, Currency: currency.USD, Interval: plan.Month, } p, err := plan.New(planParams) if err != nil { t.Fatalf("plan creation: %s", err) } subParams := &stripe.SubParams{ Customer: cust.ID, Items: []*stripe.SubItemsParams{ { Plan: p.ID, Quantity: 1, }, }, } target, err := sub.New(subParams) if err != nil { t.Fatalf("subscription creation: %s", err) } if target.Items == nil { t.Fatalf("no items for sub %s", target.ID) } if len(target.Items.Values) != 1 { t.Fatalf("missing items: %#v", target) } return target, target.Items.Values[0], func() { sub.Cancel(target.ID, nil) plan.Del(p.ID) customer.Del(cust.ID) } }
func TestSourceBankAccountVerify(t *testing.T) { baTok, err := token.New(&stripe.TokenParams{ Bank: &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", AccountHolderName: "Jane Austen", AccountHolderType: "individual", }, }) if baTok.Bank.AccountHolderName != "Jane Austen" { t.Errorf("Bank account name %q was not Jane Austen as expected.", baTok.Bank.AccountHolderName) } if err != nil { t.Error(err) } customerParams := &stripe.CustomerParams{} cust, err := customer.New(customerParams) if err != nil { t.Error(err) } source, err := New(&stripe.CustomerSourceParams{ Customer: cust.ID, Source: &stripe.SourceParams{ Token: baTok.ID, }, }) amounts := [2]uint8{32, 45} verifyParams := &stripe.SourceVerifyParams{ Customer: cust.ID, Amounts: amounts, } sourceVerified, err := Verify(source.ID, verifyParams) if err != nil { t.Error(err) return } target := sourceVerified.BankAccount if target.Status != bankaccount.VerifiedAccount { t.Errorf("Status (%q) does not match expected (%q) ", target.Status, bankaccount.VerifiedAccount) } customer.Del(cust.ID) }
func TestSubscriptionUpdate(t *testing.T) { 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, TrialEndNow: true, } subscription, _ := New(subParams) updatedSub := &stripe.SubParams{ Customer: cust.ID, NoProrate: true, Quantity: 13, TaxPercent: 20.0, } target, err := Update(subscription.ID, updatedSub) if err != nil { t.Error(err) } if target.Quantity != updatedSub.Quantity { t.Errorf("Quantity %v does not match expected quantity %v\n", target.Quantity, updatedSub.Quantity) } if target.TaxPercent != updatedSub.TaxPercent { t.Errorf("TaxPercent %f does not match expected tax_percent %f\n", target.TaxPercent, updatedSub.TaxPercent) } customer.Del(cust.ID) plan.Del("test") }
func TestSubscriptionCancel(t *testing.T) { 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, } subscription, _ := New(subParams) s, err := Cancel(subscription.ID, nil) if err != nil { t.Error(err) } if s.Canceled == 0 { t.Errorf("Subscription.Canceled %v expected to be non 0\n", s.Canceled) } subscription, _ = New(subParams) s, err = Cancel(subscription.ID, &stripe.SubParams{Customer: cust.ID}) if err != nil { t.Error(err) } if s.Canceled == 0 { t.Errorf("Subscription.Canceled %v expected to be non 0\n", s.Canceled) } customer.Del(cust.ID) plan.Del("test") }
func TestSubscriptionGet(t *testing.T) { 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, } subscription, _ := New(subParams) target, err := Get(subscription.ID, nil) if err != nil { t.Error(err) } if target.ID != subscription.ID { t.Errorf("Subscription id %q does not match expected id %q\n", target.ID, subscription.ID) } target, err = Get(subscription.ID, &stripe.SubParams{Customer: cust.ID}) if err != nil { t.Error(err) } if target.ID != subscription.ID { t.Errorf("Subscription id %q does not match expected id %q\n", target.ID, subscription.ID) } customer.Del(cust.ID) plan.Del("test") }
func TestSubscriptionZeroQuantity(t *testing.T) { 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", QuantityZero: true, TaxPercentZero: true, } target, err := New(subParams) if err != nil { t.Error(err) } if target.Plan.ID != subParams.Plan { t.Errorf("Plan %v does not match expected plan %v\n", target.Plan, subParams.Plan) } if target.Quantity != 0 { t.Errorf("Quantity %v does not match expected quantity %v\n", target.Quantity, 0) } if target.TaxPercent != 0 { t.Errorf("Tax percent %v does not match expected tax percent %v\n", target.TaxPercent, 0) } customer.Del(cust.ID) plan.Del("test") }
func TestSubscriptionList(t *testing.T) { 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, } for i := 0; i < 5; i++ { New(subParams) } i := List(&stripe.SubListParams{Customer: cust.ID}) for i.Next() { if i.Sub() == nil { t.Error("No nil values expected") } if i.Meta() == nil { t.Error("No metadata returned") } } if err := i.Err(); err != nil { t.Error(err) } customer.Del(cust.ID) plan.Del("test") }
func TestThreeDSecureNew(t *testing.T) { // Test creating a 3D Secure auth customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Name: "Stripe Tester", Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) threeDSecureParams := &stripe.ThreeDSecureParams{ Amount: 1000, Currency: currency.USD, Customer: cust.ID, Card: cust.Sources.Values[0].Card.ID, ReturnURL: "https://test.com", } threeDSecure, err := New(threeDSecureParams) if err != nil { t.Error(err) } if threeDSecure.Amount != threeDSecureParams.Amount { t.Errorf("Amount %v does not match expected amount %v\n", threeDSecure.Amount, threeDSecureParams.Amount) } if threeDSecure.Currency != threeDSecureParams.Currency { t.Errorf("Currency %q does not match expected currency %q\n", threeDSecure.Currency, threeDSecureParams.Currency) } if threeDSecure.Card.ID != threeDSecureParams.Card { t.Errorf("Card ID %q does not match expected card ID %q\n", threeDSecure.Card.ID, threeDSecureParams.Card) } // Test retrieving a 3D Secure auth threeDSecure2, err := Get(threeDSecure.ID, nil) if err != nil { t.Error(err) } if threeDSecure2.ID != threeDSecure.ID { t.Errorf("ID %q does not match expected id %q\n", threeDSecure2.ID, threeDSecure.ID) } }
func TestSourceVerify(t *testing.T) { bankAccountParams := &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", } tokenParams := &stripe.TokenParams{ Bank: bankAccountParams, } token, err := token.New(tokenParams) if err != nil { t.Error(err) return } customerParams := &stripe.CustomerParams{} customerParams.SetSource(token.ID) cust, err := customer.New(customerParams) if err != nil { t.Error(err) return } amounts := [2]uint8{32, 45} verifyParams := &stripe.SourceVerifyParams{ Customer: cust.ID, Amounts: amounts, } source, err := Verify(cust.DefaultSource.ID, verifyParams) if err != nil { t.Error(err) return } target := source.BankAccount if target.Status != bankaccount.VerifiedAccount { t.Errorf("Status (%q) does not match expected (%q) ", target.Status, bankaccount.VerifiedAccount) } customer.Del(cust.ID) }
func TestSourceDel(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) err := Del(cust.DefaultSource.ID, &stripe.CustomerSourceParams{Customer: cust.ID}) if err != nil { t.Error(err) } customer.Del(cust.ID) }
func TestSourceCardNew(t *testing.T) { customerParams := &stripe.CustomerParams{} cust, err := customer.New(customerParams) if err != nil { t.Error(err) } sourceParams := &stripe.CustomerSourceParams{ Customer: cust.ID, } sourceParams.SetSource(&stripe.CardParams{ Number: "4242424242424242", Month: "10", Year: "20", CVC: "1234", }) source, err := New(sourceParams) if err != nil { t.Error(err) } target := source.Card if target.LastFour != "4242" { t.Errorf("Unexpected last four %q for card number %v\n", target.LastFour, sourceParams.Source.Card.Number) } if target.CVCCheck != card.Pass { t.Errorf("CVC check %q does not match expected status\n", target.ZipCheck) } targetCust, err := customer.Get(cust.ID, nil) if err != nil { t.Error(err) } if targetCust.Sources.Count != 1 { t.Errorf("Unexpected number of sources %v\n", targetCust.Sources.Count) } customer.Del(cust.ID) }
func TestSourceObjectNewGet(t *testing.T) { sourceParams := &stripe.SourceObjectParams{ Type: "bitcoin", Amount: 1000, Currency: currency.USD, Owner: &stripe.SourceOwnerParams{ Email: "*****@*****.**", }, } s, err := source.New(sourceParams) if err != nil { t.Fatalf("%+v", err) } customerParams := &stripe.CustomerParams{} customer, err := customer.New(customerParams) if err != nil { t.Error(err) } src, err := New(&stripe.CustomerSourceParams{ Customer: customer.ID, Source: &stripe.SourceParams{ Token: s.ID, }, }) if src.SourceObject.Owner.Email != "*****@*****.**" { t.Errorf("Source object owner email %q was not as expected.", src.SourceObject.Owner.Email) } src, err = Get(src.ID, &stripe.CustomerSourceParams{ Customer: customer.ID, }) if err != nil { t.Error(err) } if src.SourceObject.Owner.Email != "*****@*****.**" { t.Errorf("Source object owner email %q was not as expected.", src.SourceObject.Owner.Email) } }
func TestCardNew(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) cardParams := &stripe.CardParams{ Number: "4242424242424242", Month: "10", Year: "20", Customer: cust.ID, CVC: "1234", } target, err := New(cardParams) if err != nil { t.Error(err) } if target.LastFour != "4242" { t.Errorf("Unexpected last four %q for card number %v\n", target.LastFour, cardParams.Number) } if target.CVCCheck != Pass { t.Errorf("CVC check %q does not match expected status\n", target.ZipCheck) } targetCust, err := customer.Get(cust.ID, nil) if err != nil { t.Error(err) } if targetCust.Sources.Count != 2 { t.Errorf("Unexpected number of sources %v\n", targetCust.Sources.Count) } customer.Del(cust.ID) }
func TestCardUpdate(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", Name: "Original Name", }) cust, err := customer.New(customerParams) if err != nil { t.Error(err) } cardParams := &stripe.CardParams{ Customer: cust.ID, Name: "Updated Name", Month: "10", Year: "21", } target, err := Update(cust.DefaultSource.ID, cardParams) if err != nil { t.Error(err) } if target.Name != cardParams.Name { t.Errorf("Card name %q does not match expected name %q\n", target.Name, cardParams.Name) } if target.Month != 10 { t.Errorf("Unexpected expiration month %d for card where we set %q\n", target.Month, cardParams.Month) } if target.Year != 2021 { t.Errorf("Unexpected expiration year %d for card where we set %q\n", target.Year, cardParams.Year) } customer.Del(cust.ID) }
func TestChargeNewWithCustomerAndCard(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) chargeParams := &stripe.ChargeParams{ Amount: 1000, Currency: currency.USD, Customer: cust.ID, Statement: "statement", Email: "*****@*****.**", } chargeParams.SetSource(cust.Sources.Values[0].Card.ID) target, err := New(chargeParams) if err != nil { t.Error(err) } if target.Amount != chargeParams.Amount { t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, chargeParams.Amount) } if target.Currency != chargeParams.Currency { t.Errorf("Currency %q does not match expected currency %q\n", target.Currency, chargeParams.Currency) } if target.Customer.ID != cust.ID { t.Errorf("Customer ID %q doesn't match expected customer ID %q", target.Customer.ID, cust.ID) } if target.Source.Card.ID != cust.Sources.Values[0].Card.ID { t.Errorf("Card ID %q doesn't match expected card ID %q", target.Source.Card.ID, cust.Sources.Values[0].Card.ID) } }
func TestCardDel(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) cardDel, err := Del(cust.DefaultSource.ID, &stripe.CardParams{Customer: cust.ID}) if err != nil { t.Error(err) } if !cardDel.Deleted { t.Errorf("Card id %q expected to be marked as deleted on the returned resource\n", cardDel.ID) } customer.Del(cust.ID) }
func TestSourceBankAccountDel(t *testing.T) { baTok, err := token.New(&stripe.TokenParams{ Bank: &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", AccountHolderName: "Jane Austen", AccountHolderType: "individual", }, }) if baTok.Bank.AccountHolderName != "Jane Austen" { t.Errorf("Bank account name %q was not Jane Austen as expected.", baTok.Bank.AccountHolderName) } if err != nil { t.Error(err) } customerParams := &stripe.CustomerParams{} customer, err := customer.New(customerParams) if err != nil { t.Error(err) } source, err := New(&stripe.CustomerSourceParams{ Customer: customer.ID, Source: &stripe.SourceParams{ Token: baTok.ID, }, }) sourceDel, err := Del(source.ID, &stripe.CustomerSourceParams{Customer: customer.ID}) if !sourceDel.Deleted { t.Errorf("Source id %q expected to be marked as deleted on the returned resource\n", sourceDel.ID) } }
func TestSourceBankAccountGet(t *testing.T) { baTok, err := token.New(&stripe.TokenParams{ Bank: &stripe.BankAccountParams{ Country: "US", Currency: "usd", Routing: "110000000", Account: "000123456789", AccountHolderName: "Jane Austen", AccountHolderType: "individual", }, }) if baTok.Bank.AccountHolderName != "Jane Austen" { t.Errorf("Bank account token name %q was not Jane Austen as expected.", baTok.Bank.AccountHolderName) } if err != nil { t.Error(err) } customerParams := &stripe.CustomerParams{} customer, err := customer.New(customerParams) if err != nil { t.Error(err) } src, err := New(&stripe.CustomerSourceParams{ Customer: customer.ID, Source: &stripe.SourceParams{ Token: baTok.ID, }, }) source, err := Get(src.ID, &stripe.CustomerSourceParams{Customer: customer.ID}) if source.BankAccount.AccountHolderName != "Jane Austen" { t.Errorf("Bank account name %q was not Jane Austen as expected.", source.BankAccount.Name) } }
func SaveStripeToken(db *sql.DB, stripe_token string, last4 int64, guest_data *GuestData) error { if server_config.Version.V == "prod" { stripe.Key = "sk_live_6DdxsleLP40YnpsFATA1ZJCg" } else { stripe.Key = "sk_test_PsKvXuwitPqYwpR7hPse4PFk" } email, err := GetEmailForGuest(db, guest_data.Id) if err != nil { return err } guest_data.Email = email customerParams := &stripe.CustomerParams{ Desc: guest_data.First_name + " " + guest_data.Last_name, Email: guest_data.Email, } log.Println(stripe_token) customerParams.SetSource(stripe_token) c, err := customer.New(customerParams) if err != nil { log.Println(err) return err } log.Println(c) _, err = db.Exec(` INSERT INTO StripeToken (Stripe_token, Last4, Guest_id) VALUES (?, ?, ?) `, c.ID, last4, guest_data.Id, ) if err != nil { log.Println(err) } return err }
func TestCardNew(t *testing.T) { customerParams := &stripe.CustomerParams{} customerParams.SetSource(&stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }) cust, _ := customer.New(customerParams) cardParams := &stripe.CardParams{ Number: "4242424242424242", Month: "10", Year: "20", Customer: cust.ID, CVC: "1234", } target, err := New(cardParams) if err != nil { t.Error(err) } if target.LastFour != "4242" { t.Errorf("Unexpected last four %q for card number %v\n", target.LastFour, cardParams.Number) } if target.Meta == nil || len(target.Meta) > 0 { t.Errorf("Unexpected nil or non-empty metadata in card\n") } if target.Month != 10 { t.Errorf("Unexpected expiration month %d for card where we set %q\n", target.Month, cardParams.Month) } if target.Year != 2020 { t.Errorf("Unexpected expiration year %d for card where we set %q\n", target.Year, cardParams.Year) } if target.CVCCheck != Pass { t.Errorf("CVC check %q does not match expected status\n", target.ZipCheck) } targetCust, err := customer.Get(cust.ID, nil) if err != nil { t.Error(err) } if targetCust.Sources.Count != 2 { t.Errorf("Unexpected number of sources %v\n", targetCust.Sources.Count) } targetToken, err := token.New(&stripe.TokenParams{ Card: &stripe.CardParams{ Number: "4000056655665556", Month: "09", Year: "2021", CVC: "123", }, }) targetCard, err := New(&stripe.CardParams{ Customer: targetCust.ID, Token: targetToken.ID, }) if targetCard.LastFour != "5556" { t.Errorf("Unexpected last four %q for card number %v\n", targetCard.LastFour, cardParams.Number) } if targetCard.Month != 9 { t.Errorf("Unexpected expiration month %d for card where we set %q\n", targetCard.Month, targetToken.Card.Month) } if targetCard.Year != 2021 { t.Errorf("Unexpected expiration year %d for card where we set %q\n", targetCard.Year, targetToken.Card.Year) } customer.Del(cust.ID) }
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") }
func HandleUpdateStripe(c *Context, w http.ResponseWriter, req *http.Request) { // Parse the JSON POST body decoder := json.NewDecoder(req.Body) var form PaymentForm if err := decoder.Decode(&form); err != nil { msg := "Could not decode payment form" log.WithField("err", err).Error(msg) c.Render.JSON(w, http.StatusBadRequest, JsonErr(msg)) return } defer req.Body.Close() clog := log.WithFields(log.Fields{ "user_id": c.User.Id, "stripe_token": form.StripeToken, }) if utils.Conf.Production { stripe.Key = utils.Conf.StripeSecretLive } else { stripe.Key = utils.Conf.StripeSecretTest } // If the user already has a stripe customer id, something's wrong if c.User.StripeCustomerId != "" { c.Render.JSON(w, http.StatusBadRequest, JsonErr("Tried to send "+ "conflicting payment information")) return } // Associate the customer with their e-mail address using their token customerParams := &stripe.CustomerParams{ Desc: fmt.Sprintf("Customer for %s", c.User.Email), Email: c.User.Email, Source: &stripe.SourceParams{Token: form.StripeToken}, } // Associate the customer with their user id customerParams.AddMeta("user_id", c.User.Id) // Create the new customer customer, err := customer.New(customerParams) if err != nil { clog.WithField("err", err).Error("Could not create customer") c.Render.JSON(w, http.StatusBadGateway, JsonErr("Could not "+ "communicate with payment processor, please try again soon")) return } // Assign the id to the current user c.User.StripeCustomerId = customer.ID // Now update the user in the database if err := c.Api.User.Save(c.User); err != nil { clog.WithField("err", err).Error("Could not save user's stripe token") c.Render.JSON(w, http.StatusBadGateway, JsonErr("Could not save token")) return } user := c.User // Hydrate the user object if err = c.Api.User.Hydrate([]*models.User{user}); err != nil { clog.WithField("err", err).Error("Could not hydrate") return } c.Render.JSON(w, http.StatusOK, map[string]*models.User{"user": user}) }
// Invoices are somewhat painful to test since you need // to first have some items, so test everything together to // avoid unnecessary duplication func TestAllInvoicesScenarios(t *testing.T) { customerParams := &stripe.CustomerParams{ Source: &stripe.SourceParams{ Card: &stripe.CardParams{ Number: "378282246310005", Month: "06", Year: "20", }, }, } cust, _ := customer.New(customerParams) item := &stripe.InvoiceItemParams{ Customer: cust.ID, Amount: 100, Currency: currency.USD, Desc: "Test Item", } targetItem, err := invoiceitem.New(item) if err != nil { t.Error(err) } if targetItem.Customer.ID != item.Customer { t.Errorf("Item customer %q does not match expected customer %q\n", targetItem.Customer.ID, item.Customer) } if targetItem.Desc != item.Desc { t.Errorf("Item description %q does not match expected description %q\n", targetItem.Desc, item.Desc) } if targetItem.Amount != item.Amount { t.Errorf("Item amount %v does not match expected amount %v\n", targetItem.Amount, item.Amount) } if targetItem.Currency != item.Currency { t.Errorf("Item currency %q does not match expected currency %q\n", targetItem.Currency, item.Currency) } if targetItem.Date == 0 { t.Errorf("Item date is not set\n") } invoiceParams := &stripe.InvoiceParams{ Customer: cust.ID, Desc: "Desc", Statement: "Statement", TaxPercent: 20.0, } targetInvoice, err := New(invoiceParams) if err != nil { t.Error(err) } if targetInvoice.Customer.ID != invoiceParams.Customer { t.Errorf("Invoice customer %q does not match expected customer %q\n", targetInvoice.Customer.ID, invoiceParams.Customer) } if targetInvoice.TaxPercent != invoiceParams.TaxPercent { t.Errorf("Invoice tax percent %f does not match expected tax percent %f\n", targetInvoice.TaxPercent, invoiceParams.TaxPercent) } if targetInvoice.Tax != 20 { t.Errorf("Invoice tax %v does not match expected tax 20\n", targetInvoice.Tax) } if targetInvoice.Amount != targetItem.Amount+targetInvoice.Tax { t.Errorf("Invoice amount %v does not match expected amount %v + tax %v\n", targetInvoice.Amount, targetItem.Amount, targetInvoice.Tax) } if targetInvoice.Currency != targetItem.Currency { t.Errorf("Invoice currency %q does not match expected currency %q\n", targetInvoice.Currency, targetItem.Currency) } if targetInvoice.Date == 0 { t.Errorf("Invoice date is not set\n") } if targetInvoice.Start == 0 { t.Errorf("Invoice start is not set\n") } if targetInvoice.End == 0 { t.Errorf("Invoice end is not set\n") } if targetInvoice.Total != targetInvoice.Amount || targetInvoice.Subtotal != targetInvoice.Amount-targetInvoice.Tax { t.Errorf("Invoice total %v and subtotal %v do not match expected amount %v\n", targetInvoice.Total, targetInvoice.Subtotal, targetInvoice.Amount) } if targetInvoice.Desc != invoiceParams.Desc { t.Errorf("Invoice description %q does not match expected description %q\n", targetInvoice.Desc, invoiceParams.Desc) } if targetInvoice.Statement != invoiceParams.Statement { t.Errorf("Invoice statement %q does not match expected statement %q\n", targetInvoice.Statement, invoiceParams.Statement) } if targetInvoice.Lines == nil { t.Errorf("Invoice lines not found\n") } if targetInvoice.Lines.Count != 1 { t.Errorf("Invoice lines count %v does not match expected value\n", targetInvoice.Lines.Count) } if targetInvoice.Lines.Values == nil { t.Errorf("Invoice lines values not found\n") } if targetInvoice.Lines.Values[0].Amount != targetItem.Amount { t.Errorf("Invoice line amount %v does not match expected amount %v\n", targetInvoice.Lines.Values[0].Amount, targetItem.Amount) } if targetInvoice.Lines.Values[0].Currency != targetItem.Currency { t.Errorf("Invoice line currency %q does not match expected currency %q\n", targetInvoice.Lines.Values[0].Currency, targetItem.Currency) } if targetInvoice.Lines.Values[0].Desc != targetItem.Desc { t.Errorf("Invoice line description %q does not match expected description %q\n", targetInvoice.Lines.Values[0].Desc, targetItem.Desc) } if targetInvoice.Lines.Values[0].Type != TypeInvoiceItem { t.Errorf("Invoice line type %q does not match expected type\n", targetInvoice.Lines.Values[0].Type) } if targetInvoice.Lines.Values[0].Period == nil { t.Errorf("Invoice line period not found\n") } if targetInvoice.Lines.Values[0].Period.Start == 0 { t.Errorf("Invoice line period start is not set\n") } if targetInvoice.Lines.Values[0].Period.End == 0 { t.Errorf("Invoice line period end is not set\n") } updatedItem := &stripe.InvoiceItemParams{ Amount: 99, Desc: "Updated Desc", Discountable: true, } targetItemUpdated, err := invoiceitem.Update(targetItem.ID, updatedItem) if err != nil { t.Error(err) } if targetItemUpdated.Desc != updatedItem.Desc { t.Errorf("Updated item description %q does not match expected description %q\n", targetItemUpdated.Desc, updatedItem.Desc) } if targetItemUpdated.Amount != updatedItem.Amount { t.Errorf("Updated item amount %v does not match expected amount %v\n", targetItemUpdated.Amount, updatedItem.Amount) } if !targetItemUpdated.Discountable { t.Errorf("Updated item is not discountable") } updatedInvoice := &stripe.InvoiceParams{ Desc: "Updated Desc", Statement: "Updated", } targetInvoiceUpdated, err := Update(targetInvoice.ID, updatedInvoice) if err != nil { t.Error(err) } if targetInvoiceUpdated.Desc != updatedInvoice.Desc { t.Errorf("Updated invoice description %q does not match expected description %q\n", targetInvoiceUpdated.Desc, updatedInvoice.Desc) } if targetInvoiceUpdated.Statement != updatedInvoice.Statement { t.Errorf("Updated invoice statement %q does not match expected statement %q\n", targetInvoiceUpdated.Statement, updatedInvoice.Statement) } _, err = invoiceitem.Get(targetItem.ID, nil) if err != nil { t.Error(err) } ii := invoiceitem.List(&stripe.InvoiceItemListParams{Customer: cust.ID}) for ii.Next() { if ii.InvoiceItem() == nil { t.Error("No nil values expected") } if ii.Meta() == nil { t.Error("No metadata returned") } } if err := ii.Err(); err != nil { t.Error(err) } i := List(&stripe.InvoiceListParams{Customer: cust.ID}) for i.Next() { if i.Invoice() == nil { t.Error("No nil values expected") } if i.Meta() == nil { t.Error("No metadata returned") } } if err := i.Err(); err != nil { t.Error(err) } il := ListLines(&stripe.InvoiceLineListParams{ID: targetInvoice.ID, Customer: cust.ID}) for il.Next() { if il.InvoiceLine() == nil { t.Error("No nil values expected") } if il.Meta() == nil { t.Error("No metadata returned") } } if err := il.Err(); err != nil { t.Error(err) } iiDel, err := invoiceitem.Del(targetItem.ID) if err != nil { t.Error(err) } if !iiDel.Deleted { t.Errorf("Invoice Item id %q expected to be marked as deleted on the returned resource\n", iiDel.ID) } _, err = Get(targetInvoice.ID, nil) if err != nil { t.Error(err) } customer.Del(cust.ID) }