Esempio n. 1
0
func TestTransaction_Network(t *testing.T) {
	// TODO: There is no way to approve a recipient and verify transfer programmatically so
	//   we can't create transaction programmtically, yet. Thus this is skipped, for now.
	//   You can try this test by manually completing one transfer on the test dashboard
	//   which will cause a `Transaction` object to be created.
	t.Skip()

	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// list transactions
	transactions := &omise.TransactionList{}
	client.MustDo(transactions, &ListTransactions{
		List{Limit: 100, From: time.Now().Add(-1 * time.Hour)},
	})

	if !a.True(t, len(transactions.Data) > 0, "no transactions was created!") {
		return
	}

	// retrieve a sample transaction
	transaction := &omise.Transaction{}
	client.MustDo(transaction, &RetrieveTransaction{
		TransactionID: transactions.Data[0].ID,
	})

	a.Equal(t, transactions.Data[0].ID, transaction.ID)
	a.Equal(t, transactions.Data[0].Amount, transaction.Amount)
}
Esempio n. 2
0
func TestCharge_Network_Uncaptured(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	token := &omise.Token{}
	client.MustDo(token, CreateTokenOp)

	// create uncaptured charge
	charge, create := &omise.Charge{}, &CreateCharge{
		Amount:      409669,
		Currency:    "thb",
		DontCapture: true,
		Card:        token.ID,
	}
	client.MustDo(charge, create)

	a.Equal(t, create.Amount, charge.Amount)
	a.False(t, charge.Captured, "charge unintentionally captured!")

	// then capture it
	charge2 := &omise.Charge{}
	client.MustDo(charge2, &CaptureCharge{ChargeID: charge.ID})

	a.Equal(t, charge.ID, charge2.ID)
	a.True(t, charge2.Captured, "charge not captured!")
}
Esempio n. 3
0
func TestSearch_Network(t *testing.T) {
	const ChargeID = "chrg_test_54i01932u4ts67cop81"

	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// using query
	result := &omise.ChargeSearchResult{}
	client.MustDo(result, &Search{
		Scope: omise.ChargeScope,
		Query: ChargeID,
	})

	a.Equal(t, 1, result.Total)
	a.Equal(t, 1, result.TotalPages)

	charge := result.Data[0]
	a.Equal(t, ChargeID, charge.ID)
	a.Equal(t, int64(100000), charge.Amount)

	// using filters
	result = &omise.ChargeSearchResult{}
	client.MustDo(result, &Search{
		Scope: omise.ChargeScope,
		Query: "id:" + ChargeID,
	})

	a.Equal(t, 1, result.Total)
	a.Equal(t, 1, result.TotalPages)

	charge = result.Data[0]
	a.Equal(t, ChargeID, charge.ID)
	a.Equal(t, int64(100000), charge.Amount)
}
Esempio n. 4
0
func TestRefund_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// create a charge so we can refund
	// TODO: DRY with TestCharge
	token := &omise.Token{}
	client.MustDo(token, CreateTokenOp)

	charge := &omise.Charge{}
	client.MustDo(charge, &CreateCharge{
		Amount:      819229,
		Currency:    "thb",
		Description: "should be refunded soon.",
		Card:        token.ID,
	})

	a.Equal(t, int64(819229), charge.Amount)
	a.Equal(t, "thb", charge.Currency)

	// list refunds on the charge
	list := &ListRefunds{
		charge.ID,
		List{Limit: 100, From: time.Now().Add(-1 * time.Hour)},
	}

	refunds := &omise.RefundList{}
	client.MustDo(refunds, list)

	a.Len(t, refunds.Data, 0)

	// create a half refund on the charge
	refund := &omise.Refund{}
	client.MustDo(refund, &CreateRefund{
		ChargeID: charge.ID,
		Amount:   charge.Amount >> 1,
	})

	a.Equal(t, refund.Amount, charge.Amount>>1)
	a.Equal(t, refund.Currency, charge.Currency)

	// list refunds again which now contains the created refunds
	client.MustDo(refunds, list)

	if a.Len(t, refunds.Data, 1) {
		a.Equal(t, refunds.Data[0].ID, refund.ID)
	} else {
		return
	}

	// retrieve refund by id, which should match what we already have.
	client.MustDo(refund, &RetrieveRefund{
		ChargeID: charge.ID,
		RefundID: refund.ID,
	})

	a.Equal(t, refunds.Data[0].ID, refund.ID)
	a.Equal(t, refunds.Data[0].Amount, refund.Amount)
}
Esempio n. 5
0
func TestToken_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	tok1, tok2 := createTestToken(client), &omise.Token{}
	client.MustDo(tok2, &RetrieveToken{ID: tok1.ID})

	a.Equal(t, *tok1, *tok2)
}
Esempio n. 6
0
func TestBalance_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	balance := &omise.Balance{}
	client.MustDo(balance, &RetrieveBalance{})

	a.Equal(t, balance.Object, "balance")
	testutil.LogObj(t, balance)
}
Esempio n. 7
0
func TestAccount_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	account := &omise.Account{}
	client.MustDo(account, &RetrieveAccount{})
	a.Equal(t, account.Object, "account")

	testutil.LogObj(t, account)
}
Esempio n. 8
0
func TestToken_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	t1, t2 := &omise.Token{}, &omise.Token{}
	client.MustDo(t1, CreateTokenOp)
	client.MustDo(t2, &RetrieveToken{ID: t1.ID})

	a.Equal(t, *t1, *t2)
}
Esempio n. 9
0
func TestCharge_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	token := &omise.Token{}
	client.MustDo(token, CreateTokenOp)

	// create
	charge, create := &omise.Charge{}, &CreateCharge{
		Amount:      204842,
		Currency:    "thb",
		Description: "initial charge.",
		Card:        token.ID,
	}
	client.MustDo(charge, create)

	a.Equal(t, create.Amount, charge.Amount)
	a.Equal(t, create.Currency, charge.Currency)

	// retrieve created charge
	charge2 := &omise.Charge{}
	client.MustDo(charge2, &RetrieveCharge{ChargeID: charge.ID})

	a.Equal(t, charge.ID, charge2.ID)
	a.Equal(t, charge.Amount, charge2.Amount)
	a.Equal(t, charge.Description, charge2.Description)

	// list created charges from the last hour
	charges, list := &omise.ChargeList{}, &ListCharges{
		List{Limit: 100, From: time.Now().Add(-1 * time.Hour)},
	}
	client.MustDo(&charges, list)

	a.True(t, len(charges.Data) > 0, "charges list empty!")
	charge2 = charges.Find(charge.ID)
	if !a.NotNil(t, charge2, "could not find recent charges in list.") {
		return
	}

	a.Equal(t, charge.ID, charge2.ID, "charge not in returned list.")
	a.Equal(t, charge.Amount, charge2.Amount, "listed charge has wrong amount.")

	// update charge
	charge2 = &omise.Charge{}
	update := &UpdateCharge{
		ChargeID:    charge.ID,
		Description: "updated charge.",
	}
	client.MustDo(charge2, update)

	a.Equal(t, charge.ID, charge2.ID)
	if a.NotNil(t, charge2.Description) {
		a.Equal(t, update.Description, *charge2.Description)
	}
}
Esempio n. 10
0
func TestDispute_Network(t *testing.T) {
	// TODO: No way to programmatically generates Dispute against the API yet.
	//   so not sure how we can test this thoroughly.
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// only test JSON bindings for now.
	disputes, list := &omise.DisputeList{}, &ListDisputes{}
	client.MustDo(disputes, list)
	if len(disputes.Data) > 0 {
		a.True(t, disputes.Data[0].Status != omise.Pending)
	}
}
Esempio n. 11
0
func TestEvent_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	events := &omise.EventList{}
	client.MustDo(events, &ListEvents{})
	a.True(t, len(events.Data) > 1)

	event, retrieve := &omise.Event{}, &RetrieveEvent{events.Data[0].ID}
	client.MustDo(event, retrieve)
	a.True(t, strings.HasPrefix(event.ID, "evnt_test_"))
	a.Equal(t, events.Data[0].ID, event.ID)
}
Esempio n. 12
0
func TestTransfer_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// make a transfer to default recipient. (empty RecipientID)
	transfer := &omise.Transfer{}
	client.MustDo(transfer, &CreateTransfer{Amount: 32100})

	a.Equal(t, int64(32100), transfer.Amount)
	a.NotNil(t, transfer.BankAccount)

	// gets created transfer
	transfer2 := &omise.Transfer{}
	client.MustDo(transfer2, &RetrieveTransfer{
		TransferID: transfer.ID,
	})

	// list transfers
	transfers := &omise.TransferList{}
	client.MustDo(transfers, &ListTransfers{
		List{Limit: 100, From: time.Now().Add(-1 * time.Hour)},
	})

	a.True(t, len(transfers.Data) > 0, "no transfer was created.")

	transfer2 = transfers.Find(transfer.ID)
	a.Equal(t, transfer.ID, transfer2.ID)
	a.Equal(t, transfer.Amount, transfer2.Amount)

	// update transfer
	transfer2 = &omise.Transfer{}
	client.MustDo(transfer2, &UpdateTransfer{
		TransferID: transfer.ID,
		Amount:     12300,
	})

	a.Equal(t, transfer.ID, transfer2.ID)
	a.Equal(t, int64(12300), transfer2.Amount)

	// destroy transfer
	del, destroy := &omise.Deletion{}, &DestroyTransfer{TransferID: transfer.ID}
	client.MustDo(del, destroy)

	a.Equal(t, transfer.Object, del.Object)
	a.Equal(t, transfer.ID, del.ID)
	a.Equal(t, transfer.Live, del.Live)
	a.True(t, del.Deleted)
}
Esempio n. 13
0
func TestRefund_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// create a charge so we can refund
	token := createTestToken(client)
	charge := createTestCharge(client, token)

	// list refunds on the charge
	list := &ListRefunds{
		charge.ID,
		List{Limit: 100, From: time.Now().Add(-1 * time.Hour)},
	}

	refunds := &omise.RefundList{}
	client.MustDo(refunds, list)

	a.Len(t, refunds.Data, 0)

	// create a half refund on the charge
	refund := &omise.Refund{}
	client.MustDo(refund, &CreateRefund{
		ChargeID: charge.ID,
		Amount:   charge.Amount >> 1,
	})

	a.Equal(t, refund.Amount, charge.Amount>>1)
	a.Equal(t, refund.Currency, charge.Currency)

	// list refunds again which now contains the created refunds
	client.MustDo(refunds, list)

	if a.Len(t, refunds.Data, 1) {
		a.Equal(t, refunds.Data[0].ID, refund.ID)
	} else {
		return
	}

	// retrieve refund by id, which should match what we already have.
	client.MustDo(refund, &RetrieveRefund{
		ChargeID: charge.ID,
		RefundID: refund.ID,
	})

	a.Equal(t, refunds.Data[0].ID, refund.ID)
	a.Equal(t, refunds.Data[0].Amount, refund.Amount)
}
Esempio n. 14
0
func TestCharge_Network_Invalid(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	token := &omise.Token{}
	client.MustDo(token, CreateTokenOp)

	e := client.Do(nil, &CreateCharge{
		Amount:   12345,
		Currency: "omd", // OMISE DOLLAR, why not?
		Card:     token.ID,
	})
	a.EqualError(t, e, "(400/invalid_charge) currency is currently not supported")

	e = client.Do(nil, &CreateCharge{
		Amount:   12345,
		Currency: "thb", // OMISE DOLLAR, why not?
		Card:     "tok_asdf",
	})
	a.EqualError(t, e, "(404/not_found) token tok_asdf was not found")
}
Esempio n. 15
0
func TestCustomer_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	token := &omise.Token{}
	client.MustDo(token, CreateTokenOp)

	// create a customer
	jack := &omise.Customer{}
	client.MustDo(jack, &CreateCustomer{
		Email:       "*****@*****.**",
		Description: "I'm JACK",
		Card:        token.ID,
	})
	if !a.NotNil(t, jack) {
		return
	}

	t.Log("created customer:", jack.ID)
	a.Equal(t, "*****@*****.**", jack.Email)
	a.Equal(t, "I'm JACK", jack.Description)
	a.Len(t, jack.Cards.Data, 1)

	// list created customers
	customers := &omise.CustomerList{}
	client.MustDo(customers, &ListCustomers{
		List{
			From:  time.Now().Add(-1 * time.Hour),
			Limit: 100,
		},
	})

	a.True(t, len(customers.Data) > 0, "no created customers in list!")

	jane := customers.Find(jack.ID)
	a.Equal(t, jack.ID, jane.ID)
	a.Equal(t, jack.Email, jane.Email)

	// update
	john := &omise.Customer{}
	client.MustDo(john, &UpdateCustomer{
		CustomerID:  jack.ID,
		Description: "I'm JOHN now.",
	})

	a.Equal(t, jack.ID, john.ID)
	a.Equal(t, "I'm JOHN now.", john.Description)

	// fetch
	jill, retrieve := &omise.Customer{}, &RetrieveCustomer{john.ID}
	client.MustDo(jill, retrieve)

	a.Equal(t, john.ID, jill.ID)
	a.Equal(t, john.Email, jill.Email)
	a.Equal(t, john.Description, jill.Description)

	// delete
	del, destroy := &omise.Deletion{}, &DestroyCustomer{CustomerID: jill.ID}
	client.MustDo(del, destroy)

	a.Equal(t, jill.Object, del.Object)
	a.Equal(t, jill.ID, del.ID)
	a.Equal(t, jill.Live, del.Live)
	a.True(t, del.Deleted)
}
Esempio n. 16
0
func TestRecipient_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	// create a recipient
	// sample from: https://www.omise.co/bank-account-api
	jun, bankAccount := &omise.Recipient{}, &omise.BankAccount{
		Brand:  "bbl",
		Number: "1234567890",
		Name:   "Somchai Prasert",
	}
	client.MustDo(jun, &CreateRecipient{
		Name:        "Jun Hasegawa",
		Email:       "*****@*****.**",
		Description: "Owns Omise",
		Type:        omise.Individual,
		BankAccount: bankAccount,
	})

	t.Log("created recipient:", jun.ID)
	a.Equal(t, "*****@*****.**", jun.Email)
	if a.NotNil(t, jun.Description) {
		a.Equal(t, "Owns Omise", *jun.Description)
	}
	a.Equal(t, jun.BankAccount.Name, bankAccount.Name)

	// list created customers
	recipients := &omise.RecipientList{}
	client.MustDo(recipients, &ListRecipients{
		List{From: time.Now().Add(-1 * time.Hour), Limit: 100},
	})

	a.True(t, len(recipients.Data) > 0, "no created customers in list!")

	jim := recipients.Find(jun.ID)
	a.Equal(t, jun.ID, jim.ID)
	a.Equal(t, jun.Email, jim.Email)

	// update
	jones := &omise.Recipient{}
	client.MustDo(jones, &UpdateRecipient{
		RecipientID: jim.ID,
		Description: "I'm JONES now.",
	})

	a.Equal(t, jim.ID, jones.ID)
	if a.NotNil(t, jones.Description) {
		a.Equal(t, "I'm JONES now.", *jones.Description)
	}

	// fetch
	josh := &omise.Recipient{}
	client.MustDo(josh, &RetrieveRecipient{jones.ID})

	a.Equal(t, jones.ID, josh.ID)
	a.Equal(t, jones.Email, josh.Email)
	a.Equal(t, jones.Description, josh.Description)

	// delete
	del := &omise.Deletion{}
	client.MustDo(del, &DestroyRecipient{jones.ID})

	a.Equal(t, jones.Object, del.Object)
	a.Equal(t, jones.ID, del.ID)
	a.Equal(t, jones.Live, del.Live)
	a.True(t, del.Deleted)
}
Esempio n. 17
0
func TestCard_Network(t *testing.T) {
	testutil.Require(t, "network")
	client := testutil.NewTestClient(t)

	customer := &omise.Customer{}
	client.MustDo(customer, &CreateCustomer{
		Email:       "*****@*****.**",
		Description: "Omise",
		Card:        "",
	})

	defer client.MustDo(nil, &DestroyCustomer{customer.ID})

	// list empty collection
	cards := &omise.CardList{}
	client.MustDo(cards, &ListCards{CustomerID: customer.ID})
	if !a.NotNil(t, cards) {
		return
	}

	a.Len(t, cards.Data, 0)

	// add some cards
	tok1, tok2 := createTestToken(client), createTestToken(client)
	client.MustDo(customer, &UpdateCustomer{CustomerID: customer.ID, Card: tok1.ID})
	client.MustDo(customer, &UpdateCustomer{CustomerID: customer.ID, Card: tok2.ID})

	// see added cards (using pagination API to call twice)
	firstHalf, secondHalf := &omise.CardList{}, &omise.CardList{}
	client.MustDo(firstHalf, &ListCards{customer.ID, List{Offset: 0, Limit: 1}})
	client.MustDo(secondHalf, &ListCards{customer.ID, List{Offset: 1, Limit: 1}})

	if !(a.Len(t, firstHalf.Data, 1) && a.Len(t, secondHalf.Data, 1)) {
		return
	} else if !a.NotEqual(t, firstHalf.Data[0].ID, secondHalf.Data[0].ID) {
		return
	}

	cards = &omise.CardList{
		List: omise.List{Offset: 0, Limit: 0},
		Data: append(firstHalf.Data, secondHalf.Data...),
	}
	a.Len(t, cards.Data, 2)

	// update a card
	card, card2 := cards.Data[0], &omise.Card{}
	client.MustDo(card2, &UpdateCard{
		CustomerID: customer.ID,
		CardID:     card.ID,
		Name:       "Changed my name",
	})

	a.Equal(t, card.ID, card2.ID)
	a.Equal(t, "Changed my name", card2.Name)

	// retrieve added cards
	card = &omise.Card{}
	client.MustDo(card, &RetrieveCard{CustomerID: customer.ID, CardID: cards.Data[0].ID})

	a.Equal(t, cards.Data[0].ID, card.ID)
	a.Equal(t, cards.Data[0].LastDigits, card.LastDigits)

	// remove cards
	del1, del2 := &omise.Deletion{}, &omise.Deletion{}
	client.MustDo(del1, &DestroyCard{CustomerID: customer.ID, CardID: cards.Data[0].ID})
	client.MustDo(del2, &DestroyCard{CustomerID: customer.ID, CardID: cards.Data[1].ID})

	a.Equal(t, cards.Data[0].ID, del1.ID)
	a.Equal(t, cards.Data[1].ID, del2.ID)
	a.True(t, del1.Deleted)
	a.True(t, del2.Deleted)

	// list should be empty again
	client.MustDo(cards, &ListCards{CustomerID: customer.ID})
	a.Len(t, cards.Data, 0)
}