示例#1
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
}
示例#2
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)
	}
}