func TestMerchantAccountCreate(t *testing.T) {
	acctId = testhelpers.RandomString()
	acct := MerchantAccount{
		MasterMerchantAccountId: testMerchantAccountId,
		TOSAccepted:             true,
		Id:                      acctId,
		Individual: &MerchantAccountPerson{
			FirstName:   "Kayle",
			LastName:    "Gishen",
			Email:       "*****@*****.**",
			Phone:       "5556789012",
			DateOfBirth: "1-1-1989",
			Address: &Address{
				StreetAddress:   "1 E Main St",
				ExtendedAddress: "Suite 404",
				Locality:        "Chicago",
				Region:          "IL",
				PostalCode:      "60622",
			},
		},
		FundingOptions: &MerchantAccountFundingOptions{
			Destination: FUNDING_DEST_MOBILE_PHONE,
			MobilePhone: "5552344567",
		},
	}

	x, _ := xml.Marshal(&acct)
	t.Log(string(x))

	merchantAccount, err := testGateway.MerchantAccount().Create(&acct)

	t.Log(merchantAccount)

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

	if merchantAccount.Id == "" {
		t.Fatal("invalid merchant account id")
	}

	ma2, err := testGateway.MerchantAccount().Find(merchantAccount.Id)

	t.Log(ma2)

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

	if ma2.Id != merchantAccount.Id {
		t.Fatal("ids do not match")
	}

}
func TestTransactionSearch(t *testing.T) {
	txg := testGateway.Transaction()
	createTx := func(amount *Decimal, customerName string) error {
		_, err := txg.Create(&Transaction{
			Type:   "sale",
			Amount: amount,
			Customer: &Customer{
				FirstName: customerName,
			},
			CreditCard: &CreditCard{
				Number:         testCreditCards["visa"].Number,
				ExpirationDate: "05/14",
			},
		})
		return err
	}

	unique := testhelpers.RandomString()

	name0 := "Erik-" + unique
	if err := createTx(randomAmount(), name0); err != nil {
		t.Fatal(err)
	}

	name1 := "Lionel-" + unique
	if err := createTx(randomAmount(), name1); err != nil {
		t.Fatal(err)
	}

	query := new(SearchQuery)
	f := query.AddTextField("customer-first-name")
	f.Is = name0

	result, err := txg.Search(query)
	if err != nil {
		t.Fatal(err)
	}

	if !result.TotalItems.Valid || result.TotalItems.Int64 != 1 {
		t.Fatal(result.Transactions)
	}

	tx := result.Transactions[0]
	if x := tx.Customer.FirstName; x != name0 {
		t.Log(name0)
		t.Fatal(x)
	}
}
// This test will fail unless you set up your Braintree sandbox account correctly. See TESTING.md for details.
func TestCustomer(t *testing.T) {
	oc := &Customer{
		FirstName: "Lionel",
		LastName:  "Barrow",
		Company:   "Braintree",
		Email:     "*****@*****.**",
		Phone:     "312.555.1234",
		Fax:       "614.555.5678",
		Website:   "http://www.example.com",
		CreditCard: &CreditCard{
			Number:         testCreditCards["visa"].Number,
			ExpirationDate: "05/14",
			CVV:            "200",
			Options: &CreditCardOptions{
				VerifyCard: true,
			},
		},
	}

	// Create with errors
	_, err := testGateway.Customer().Create(oc)
	if err == nil {
		t.Fatal("Did not receive error when creating invalid customer")
	}

	// Create
	oc.CreditCard.CVV = ""
	oc.CreditCard.Options = nil
	customer, err := testGateway.Customer().Create(oc)

	t.Log(customer)

	if err != nil {
		t.Fatal(err)
	}
	if customer.Id == "" {
		t.Fatal("invalid customer id")
	}
	if card := customer.DefaultCreditCard(); card == nil {
		t.Fatal("invalid credit card")
	}
	if card := customer.DefaultCreditCard(); card.Token == "" {
		t.Fatal("invalid token")
	}

	// Update
	unique := testhelpers.RandomString()
	newFirstName := "John" + unique
	c2, err := testGateway.Customer().Update(&Customer{
		Id:        customer.Id,
		FirstName: newFirstName,
	})

	t.Log(c2)

	if err != nil {
		t.Fatal(err)
	}
	if c2.FirstName != newFirstName {
		t.Fatal("first name not changed")
	}

	// Find
	c3, err := testGateway.Customer().Find(customer.Id)

	t.Log(c3)

	if err != nil {
		t.Fatal(err)
	}
	if c3.Id != customer.Id {
		t.Fatal("ids do not match")
	}

	// Search
	query := new(SearchQuery)
	f := query.AddTextField("first-name")
	f.Is = newFirstName
	searchResult, err := testGateway.Customer().Search(query)
	if err != nil {
		t.Fatal(err)
	}
	if len(searchResult.Customers) == 0 {
		t.Fatal("could not search for a customer")
	}
	if id := searchResult.Customers[0].Id; id != customer.Id {
		t.Fatalf("id from search does not match: got %s, wanted %s", id, customer.Id)
	}

	// Delete
	err = testGateway.Customer().Delete(customer.Id)
	if err != nil {
		t.Fatal(err)
	}

	// Test customer 404
	c4, err := testGateway.Customer().Find(customer.Id)
	if err == nil {
		t.Fatal("should return 404")
	}
	if err.Error() != "Not Found (404)" {
		t.Fatal(err)
	}
	if c4 != nil {
		t.Fatal(c4)
	}
}