Ejemplo n.º 1
0
func TestRefundGet(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "378282246310005",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	ch, err := charge.New(chargeParams)

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

	ref, err := New(&stripe.RefundParams{Charge: ch.ID})

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

	target, err := Get(ref.ID, &stripe.RefundParams{Charge: ch.ID})

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

	if target.Charge != ch.ID {
		t.Errorf("Refund charge %q does not match expected value %v\n", target.Charge, ch.ID)
	}
}
Ejemplo n.º 2
0
func chargeAccount(ctx context.Context, stripeToken string) error {
	// because we're on app engine, use a custom http client
	// this is being set globally, however
	// if we wanted to do it this way, we'd have to use a lock
	// https://youtu.be/KT4ki_ClX2A?t=1018
	hc := urlfetch.Client(ctx)
	stripe.SetHTTPClient(hc)

	id, _ := uuid.NewV4()

	for {
		chargeParams := &stripe.ChargeParams{
			Amount:   100 * 200000,
			Currency: "usd",
			Desc:     "Charge for [email protected]",
		}
		chargeParams.IdempotencyKey = id.String()
		chargeParams.SetSource(stripeToken)
		ch, err := charge.New(chargeParams)
		// https://youtu.be/KT4ki_ClX2A?t=1310
		if err != nil {
			if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
				time.Sleep(time.Second)
				continue
			}
			return err
		}
		log.Infof(ctx, "CHARGE: %v", ch)
		log.Infof(ctx, "IDEMPOTENCY: %v", chargeParams.IdempotencyKey)
		return nil
	}
}
Ejemplo n.º 3
0
func newDisputedCharge() (*stripe.Charge, error) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1001,
		Currency: currency.USD,
	}

	chargeParams.SetSource(&stripe.CardParams{
		Number: "4000000000000259",
		Month:  "06",
		Year:   "20",
	})

	res, err := charge.New(chargeParams)
	if err != nil {
		return nil, err
	}

	target, err := charge.Get(res.ID, nil)

	if err != nil {
		return target, err
	}

	for target.Dispute == nil {
		time.Sleep(time.Second * 10)
		target, err = charge.Get(res.ID, nil)
		if err != nil {
			return target, err
		}
	}
	return target, err
}
Ejemplo n.º 4
0
func buyPrint(responseWriter http.ResponseWriter, request *http.Request, requestParameters httprouter.Params) {
	successMessage := validBuyPrintPostVariables(request)
	if !successMessage.Success {
		json.NewEncoder(responseWriter).Encode(successMessage)
		return
	}
	buyPrintToken := request.PostFormValue("buy-print-token")
	costInCents := printCostInCents[request.PostFormValue("print-size")]
	if credentials.LiveOrDev == "live" {
		stripe.Key = credentials.StripeLiveSecretKey
	} else {
		stripe.Key = credentials.StripeTestSecretKey
	}
	chargeParams := &stripe.ChargeParams{
		Amount:   costInCents,
		Currency: "usd",
		Desc:     request.PostFormValue("shipping-email") + " " + request.PostFormValue("destination-link"),
	}
	chargeParams.SetSource(buyPrintToken)
	chargeResults, error := charge.New(chargeParams)
	if error != nil {
		successMessage.SetMessage(false, error.Error())
		json.NewEncoder(responseWriter).Encode(successMessage)
		return
	}
	json.NewEncoder(responseWriter).Encode(successMessage)
	sendBuyPrintSuccessEmail(request, chargeResults.ID)
}
Ejemplo n.º 5
0
func TestTransferList(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge.New(chargeParams)

	recipientParams := &stripe.RecipientParams{
		Name: "Recipient Name",
		Type: recipient.Individual,
		Card: &stripe.CardParams{
			Name:   "Test Debit",
			Number: "4000056655665556",
			Month:  "10",
			Year:   "20",
		},
	}

	rec, _ := recipient.New(recipientParams)

	transferParams := &stripe.TransferParams{
		Amount:    100,
		Currency:  currency.USD,
		Recipient: rec.ID,
	}

	for i := 0; i < 5; i++ {
		New(transferParams)
	}

	i := List(&stripe.TransferListParams{Recipient: rec.ID})
	for i.Next() {
		if i.Transfer() == 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)
	}

	recipient.Del(rec.ID)
}
Ejemplo n.º 6
0
func TestTransferUpdate(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge.New(chargeParams)

	recipientParams := &stripe.RecipientParams{
		Name: "Recipient Name",
		Type: recipient.Corp,
		Bank: &stripe.BankAccountParams{
			Country: "US",
			Routing: "110000000",
			Account: "000123456789",
		},
	}

	rec, _ := recipient.New(recipientParams)

	transferParams := &stripe.TransferParams{
		Amount:    100,
		Currency:  currency.USD,
		Recipient: rec.ID,
		Desc:      "Original",
	}

	trans, _ := New(transferParams)

	updated := &stripe.TransferParams{
		Desc: "Updated",
	}

	target, err := Update(trans.ID, updated)

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

	if target.Desc != updated.Desc {
		t.Errorf("Description %q does not match expected description %q\n", target.Desc, updated.Desc)
	}

	recipient.Del(rec.ID)
}
Ejemplo n.º 7
0
func TestTransferGet(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge.New(chargeParams)

	recipientParams := &stripe.RecipientParams{
		Name: "Recipient Name",
		Type: recipient.Individual,
		Card: &stripe.CardParams{
			Name:   "Test Debit",
			Number: "4000056655665556",
			Month:  "10",
			Year:   "20",
		},
	}

	rec, _ := recipient.New(recipientParams)

	transferParams := &stripe.TransferParams{
		Amount:    100,
		Currency:  currency.USD,
		Recipient: rec.ID,
	}

	trans, _ := New(transferParams)

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

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

	if target.Card == nil {
		t.Errorf("Card is not set\n")
	}

	if target.Type != Card {
		t.Errorf("Unexpected type %q\n", target.Type)
	}

	recipient.Del(rec.ID)
}
Ejemplo n.º 8
0
func TestTransferSourceType(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	_, err := charge.New(chargeParams)
	if err != nil {
		t.Error(err)
	}

	transferParams := &stripe.TransferParams{
		Amount:     100,
		Currency:   currency.USD,
		Dest:       "default_for_currency",
		SourceType: SourceCard,
	}

	target, err := New(transferParams)

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

	if target.Amount != transferParams.Amount {
		t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, transferParams.Amount)
	}

	if target.Currency != transferParams.Currency {
		t.Errorf("Curency %q does not match expected currency %q\n", target.Currency, transferParams.Currency)
	}

	if target.Created == 0 {
		t.Errorf("Created date is not set\n")
	}

	if target.Date == 0 {
		t.Errorf("Date is not set\n")
	}

	if target.Type != Bank {
		t.Errorf("Unexpected type %q\n", target.Type)
	}
}
Ejemplo n.º 9
0
func chargeAccount(ctx context.Context, stripeToken string) error {
	chargeParams := &stripe.ChargeParams{
		Amount:   100 * 200000,
		Currency: "usd",
		Desc:     "Charge for [email protected]",
	}
	chargeParams.SetSource(stripeToken)
	ch, err := charge.New(chargeParams)
	if err != nil {
		return err
	}
	log.Infof(ctx, "CHARGE: %v", ch)
	return nil
}
Ejemplo n.º 10
0
func TestRefundListByCharge(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "378282246310005",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	ch, err := charge.New(chargeParams)

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

	for i := 0; i < 4; i++ {
		_, err = New(&stripe.RefundParams{Charge: ch.ID, Amount: 200})
		if err != nil {
			t.Error(err)
		}
	}

	listParams := &stripe.RefundListParams{}
	listParams.Filters.AddFilter("charge", "", ch.ID)
	i := List(listParams)

	for i.Next() {
		target := i.Refund()

		if target.Amount != 200 {
			t.Errorf("Amount %v does not match expected value\n", target.Amount)
		}

		if target.Charge != ch.ID {
			t.Errorf("Refund charge %q does not match expected value %q\n", target.Charge, ch.ID)
		}

		if i.Meta() == nil {
			t.Error("No metadata returned")
		}
	}
	if err := i.Err(); err != nil {
		t.Error(err)
	}
}
Ejemplo n.º 11
0
func TestReversalGet(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge.New(chargeParams)

	recipientParams := &stripe.RecipientParams{
		Name: "Recipient Name",
		Type: recipient.Individual,
		Bank: &stripe.BankAccountParams{
			Country: "US",
			Routing: "110000000",
			Account: "000123456789",
		},
	}

	rec, _ := recipient.New(recipientParams)

	transferParams := &stripe.TransferParams{
		Amount:    100,
		Currency:  currency.USD,
		Recipient: rec.ID,
		Desc:      "Transfer Desc",
		Statement: "Transfer",
	}

	trans, _ := transfer.New(transferParams)
	rev, _ := New(&stripe.ReversalParams{Transfer: trans.ID})

	target, err := Get(rev.ID, &stripe.ReversalParams{Transfer: trans.ID})

	if err != nil {
		// TODO: replace this with t.Error when this can be tested
		fmt.Println(err)
	}

	fmt.Printf("%+v\n", target)
}
Ejemplo n.º 12
0
func chargeAccount(ctx context.Context, stripeToken string) error {
	// because we're on app engine, use a custom http client
	// this is being set globally, however
	// if we wanted to do it this way, we'd have to use a lock
	// https://youtu.be/KT4ki_ClX2A?t=1018
	hc := urlfetch.Client(ctx)
	stripe.SetHTTPClient(hc)
	chargeParams := &stripe.ChargeParams{
		Amount:   100 * 200000,
		Currency: "usd",
		Desc:     "Charge for [email protected]",
	}
	chargeParams.SetSource(stripeToken)
	ch, err := charge.New(chargeParams)
	if err != nil {
		return err
	}
	log.Infof(ctx, "CHARGE: %v", ch)
	return nil
}
Ejemplo n.º 13
0
// AuthCreditCard auths some many from given source. For more info:
// https://support.stripe.com/questions/does-stripe-support-authorize-and-capture
func AuthCreditCard(u *url.URL, h http.Header, req *stripe.ChargeParams, context *models.Context) (int, http.Header, interface{}, error) {
	if context.Client.SessionID == "" {
		return response.NewBadRequest(errors.New("does not have session id"))
	}

	if req.Email == "" {
		return http.StatusBadRequest, nil, nil, errors.New("email is not set")
	}

	chargeParams := &stripe.ChargeParams{
		Amount:   50, // fifty cent
		Currency: "usd",
		Desc:     "AUTH FOR KODING REGISTRATION",
		Source:   req.Source,
		Email:    req.Email,
		// this will help us with validating the request.
		NoCapture: true,
	}
	return response.HandleResultAndClientError(charge.New(chargeParams))
}
Ejemplo n.º 14
0
func ExampleCharge_new() {
	stripe.Key = "sk_key"

	params := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
	}
	params.SetSource(&stripe.CardParams{
		Name:   "Go Stripe",
		Number: "4242424242424242",
		Month:  "10",
		Year:   "20",
	})
	params.AddMeta("key", "value")

	ch, err := charge.New(params)

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

	log.Printf("%v\n", ch.ID)
}
Ejemplo n.º 15
0
func createDebit(token string, amount uint64, description string) *stripe.Charge {
	// get your secret test key from
	// https://dashboard.stripe.com/account/apikeys and place it here
	stripe.Key = "sk_test_goes_here"

	params := &stripe.ChargeParams{
		Amount:   amount,
		Currency: currency.USD,
		Desc:     "test charge",
	}

	// obtain token from stripe
	params.SetSource(token)

	ch, err := charge.New(params)

	if err != nil {
		log.Fatalf("error while trying to charge a cc", err)
	}

	log.Printf("debit created successfully %v\n", ch.ID)

	return ch
}
Ejemplo n.º 16
0
func TestTransferToAccount(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge, err := charge.New(chargeParams)
	if err != nil {
		t.Error(err)
	}

	params := &stripe.AccountParams{
		Managed: true,
		Country: "US",
		LegalEntity: &stripe.LegalEntity{
			Type: stripe.Individual,
			DOB: stripe.DOB{
				Day:   1,
				Month: 2,
				Year:  1990,
			},
		},
	}

	acc, err := account.New(params)
	if err != nil {
		t.Error(err)
	}

	transferParams := &stripe.TransferParams{
		Amount:   100,
		Currency: currency.USD,
		Dest:     acc.ID,
		SourceTx: charge.ID,
	}

	target, err := New(transferParams)

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

	if target.Amount != transferParams.Amount {
		t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, transferParams.Amount)
	}

	if target.Currency != transferParams.Currency {
		t.Errorf("Curency %q does not match expected currency %q\n", target.Currency, transferParams.Currency)
	}

	if target.Created == 0 {
		t.Errorf("Created date is not set\n")
	}

	if target.Date == 0 {
		t.Errorf("Date is not set\n")
	}

	if target.SourceTx != transferParams.SourceTx {
		t.Errorf("SourceTx %q does not match expected SourceTx %q\n", target.SourceTx, transferParams.SourceTx)
	}

	if target.Type != StripeAccount {
		t.Errorf("Unexpected type %q\n", target.Type)
	}

	account.Del(acc.ID)
}
Ejemplo n.º 17
0
func (s *Server) HandleConfirmation(w http.ResponseWriter, r *http.Request) {
	// POST /reservations/confirmation
	if r.Method != "POST" {
		http.Error(w, "Method must be POST", http.StatusBadRequest)
		return
	}
	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	var vars ConfirmationVars
	if err := s.decoder.Decode(&vars, r.PostForm); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Look up requested tours.
	var tourIDs []int32
	for _, item := range vars.Items {
		tourIDs = append(tourIDs, item.TourID)
	}
	tourDetails, err := s.store.GetTourDetailsByID(tourIDs)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Compute total.
	var total float64
	for _, item := range vars.Items {
		tourDetail, ok := tourDetails[item.TourID]
		if !ok {
			http.Error(w, fmt.Sprintf("Invalid tour ID: %d", item.TourID), http.StatusBadRequest)
			return
		}
		total += float64(item.Quantity) * tourDetail.Price
	}
	// Convert float64(13.57) to uint64(1357).
	stripeAmount := uint64(total*100 + 0.5)

	// Add order to database.
	orderID, err := s.store.CreateOrder(vars.Name, vars.Email, vars.Hotel, vars.Mobile, vars.Items)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Charge to Stripe.
	stripe.Key = s.stripeSecretKey
	ch, err := charge.New(&stripe.ChargeParams{
		Amount:   stripeAmount,
		Currency: "USD",
		Source:   &stripe.SourceParams{Token: vars.StripeToken},
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Update order in database to record payment.
	if err := s.store.UpdateOrderPaymentRecorded(orderID); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Email the customer.
	if err := s.emailCustomer(&vars); err != nil {
		log.Print(err)
	}

	// Render confirmation page.
	data := &ConfirmationData{
		Charge:        ch,
		DisplayAmount: fmt.Sprintf("%d.%02d", ch.Amount/100, ch.Amount%100),
	}
	tmpl, err := template.ParseFiles(path.Join(s.templatesDir, "confirmation.html"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tmpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Ejemplo n.º 18
0
//Invoked by dispatch to make a purchase
func Test(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	r.ParseForm()

	var name string
	var numberOfGuests string
	var date string
	var email string
	var tok string
	var hotel string
	used := false
	for k, v := range r.Form {
		if k == "reservationName" {
			name = strings.Join(v, "")
		} else if k == "numberOfGuests" {
			numberOfGuests = strings.Join(v, "")
		} else if k == "token[used]" {
			used = true
		} else if k == "date" {
			date = strings.Join(v, "")
		} else if k == "token[email]" {
			email = strings.Join(v, "")
		} else if k == "token[id]" {
			tok = strings.Join(v, "")
		} else if k == "hotel" {
			hotel = strings.Join(v, "")
		} else {
			fmt.Println(k + ":" + strings.Join(v, ""))
		}
	}

	if !used {
		fmt.Fprintf(w, "false")
		return
	}

	fmt.Println(name + " " + numberOfGuests)

	hotel = strings.ToLower(hotel)
	stripe.Key = "sk_live_mMpPNXsrCR28xchnYnMXuv0J"
	cost, _ := strconv.Atoi(numberOfGuests)
	params := &stripe.ChargeParams{
		Amount:   S.Prices()[hotel] * uint64(cost),
		Currency: "usd",
		Desc:     "Daycation Daypass at " + hotel,
		Email:    email,
	}
	params.SetSource(tok)

	ch, err := charge.New(params)

	if err != nil {
		fmt.Println("error while trying to charge a cc", err)
		fmt.Fprintf(w, "false")
		return
	}

	fmt.Println("debit created successfully %d\n", ch.ID)

	fmt.Println(db.Purchase(hotel, name, date, numberOfGuests, email))

	/*	body, err := ioutil.ReadAll(r.Body)
		    	if err != nil {
		        	panic(err)
		    	}

			var parsed map[string]interface{}
			err = json.Unmarshal([]byte(string(body)), &parsed)

			if err != nil {
				panic(err)
			}

			data := parsed["data"].(map[string]interface{})
			object := data["object"].(map[string]interface{})
			metadata := object["metadata"].(map[string]interface{})
			amount := object["amount"].(float64)
			num := int64(amount) / 10

			fmt.Println(metadata)
			fmt.Println(amount)
			fmt.Println(num)

			fmt.Println("called")*/
	fmt.Fprintf(w, "true")
}
Ejemplo n.º 19
0
func TestTransferNew(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "4000000000000077",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	charge.New(chargeParams)

	recipientParams := &stripe.RecipientParams{
		Name: "Recipient Name",
		Type: recipient.Individual,
		Bank: &stripe.BankAccountParams{
			Country: "US",
			Routing: "110000000",
			Account: "000123456789",
		},
	}

	rec, _ := recipient.New(recipientParams)

	transferParams := &stripe.TransferParams{
		Amount:    100,
		Currency:  currency.USD,
		Recipient: rec.ID,
		Desc:      "Transfer Desc",
		Statement: "Transfer",
	}

	target, err := New(transferParams)

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

	if target.Amount != transferParams.Amount {
		t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, transferParams.Amount)
	}

	if target.Currency != transferParams.Currency {
		t.Errorf("Curency %q does not match expected currency %q\n", target.Currency, transferParams.Currency)
	}

	if target.Created == 0 {
		t.Errorf("Created date is not set\n")
	}

	if target.Date == 0 {
		t.Errorf("Date is not set \n")
	}

	if target.Desc != transferParams.Desc {
		t.Errorf("Description %q does not match expected description %q\n", target.Desc, transferParams.Desc)
	}

	if target.Recipient.ID != transferParams.Recipient {
		t.Errorf("Recipient %q does not match expected recipient %q\n", target.Recipient.ID, transferParams.Recipient)
	}

	if target.Statement != transferParams.Statement {
		t.Errorf("Statement %q does not match expected statement %q\n", target.Statement, transferParams.Statement)
	}

	if target.Bank == nil {
		t.Errorf("Bank account is not set\n")
	}

	if target.Status != Pending {
		t.Errorf("Unexpected status %q\n", target.Status)
	}

	if target.Type != Bank {
		t.Errorf("Unexpected type %q\n", target.Type)
	}

	recipient.Del(rec.ID)
}
Ejemplo n.º 20
0
func TestRefundNew(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1000,
		Currency: currency.USD,
		Source: &stripe.SourceParams{
			Card: &stripe.CardParams{
				Number: "378282246310005",
				Month:  "06",
				Year:   "20",
			},
		},
	}

	res, err := charge.New(chargeParams)

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

	// full refund
	ref, err := New(&stripe.RefundParams{Charge: res.ID})

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

	if ref.Charge != res.ID {
		t.Errorf("Refund charge %q does not match expected value %v\n", ref.Charge, res.ID)
	}

	target, err := charge.Get(res.ID, nil)

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

	if !target.Refunded || target.Refunds == nil {
		t.Errorf("Expected to have refunded this charge\n")
	}

	if len(target.Refunds.Values) != 1 {
		t.Errorf("Expected to have a refund, but instead have %v\n", len(target.Refunds.Values))
	}

	if target.Refunds.Values[0].Amount != target.AmountRefunded {
		t.Errorf("Refunded amount %v does not match amount refunded %v\n", target.Refunds.Values[0].Amount, target.AmountRefunded)
	}

	if target.Refunds.Values[0].Currency != target.Currency {
		t.Errorf("Refunded currency %q does not match charge currency %q\n", target.Refunds.Values[0].Currency, target.Currency)
	}

	if len(target.Refunds.Values[0].Tx.ID) == 0 {
		t.Errorf("Refund transaction not set\n")
	}

	if target.Refunds.Values[0].Charge != target.ID {
		t.Errorf("Refund charge %q does not match expected value %v\n", target.Refunds.Values[0].Charge, target.ID)
	}

	res, err = charge.New(chargeParams)

	// partial refund
	refundParams := &stripe.RefundParams{
		Charge: res.ID,
		Amount: 253,
	}

	New(refundParams)

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

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

	if target.Refunded {
		t.Errorf("Partial refund should not be marked as Refunded\n")
	}

	if target.AmountRefunded != refundParams.Amount {
		t.Errorf("Refunded amount %v does not match expected amount %v\n", target.AmountRefunded, refundParams.Amount)
	}

	// refund with reason
	res, err = charge.New(chargeParams)

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

	New(&stripe.RefundParams{Charge: res.ID, Reason: RefundFraudulent})
	target, err = charge.Get(res.ID, nil)

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

	if target.FraudDetails.UserReport != "fraudulent" {
		t.Errorf("Expected a fraudulent UserReport for charge refunded with reason=fraudulent but got: %s",
			target.FraudDetails.UserReport)
	}
}
Ejemplo n.º 21
0
func TestBalanceGetTx(t *testing.T) {
	chargeParams := &stripe.ChargeParams{
		Amount:   1002,
		Currency: currency.USD,
		Desc:     "charge transaction",
	}
	chargeParams.SetSource(&stripe.CardParams{
		Number: "378282246310005",
		Month:  "06",
		Year:   "20",
	})

	res, _ := charge.New(chargeParams)

	target, err := GetTx(res.Tx.ID, nil)

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

	if uint64(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.Desc != chargeParams.Desc {
		t.Errorf("Description %q does not match expected description %q\n", target.Desc, chargeParams.Desc)
	}

	if target.Available == 0 {
		t.Errorf("Available date is not set\n")
	}

	if target.Created == 0 {
		t.Errorf("Created date is not set\n")
	}

	if target.Fee == 0 {
		t.Errorf("Fee is not set\n")
	}

	if target.FeeDetails == nil || len(target.FeeDetails) != 1 {
		t.Errorf("Fee details are not set")
	}

	if target.FeeDetails[0].Amount == 0 {
		t.Errorf("Fee detail amount is not set\n")
	}

	if len(target.FeeDetails[0].Currency) == 0 {
		t.Errorf("Fee detail currency is not set\n")
	}

	if len(target.FeeDetails[0].Desc) == 0 {
		t.Errorf("Fee detail description is not set\n")
	}

	if target.Net == 0 {
		t.Errorf("Net is not set\n")
	}

	if target.Status != TxPending {
		t.Errorf("Status %v does not match expected value\n", target.Status)
	}

	if target.Type != TxCharge {
		t.Errorf("Type %v does not match expected value\n", target.Type)
	}

	if target.Src != res.ID {
		t.Errorf("Source %q does not match expeted value %q\n", target.Src, res.ID)
	}
}