Skip to content

Commit

Permalink
Add last_payment_error on PaymentIntent and fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Nov 14, 2018
1 parent cd7193f commit fd31501
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
25 changes: 1 addition & 24 deletions charge/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,7 @@ func TestChargeNew(t *testing.T) {
charge, err := New(&stripe.ChargeParams{
Amount: stripe.Int64(11700),
Currency: stripe.String(string(stripe.CurrencyUSD)),
Level3: &stripe.ChargeLevel3Params{
LineItems: []*stripe.ChargeLevel3LineItemsParams{
{
DiscountAmount: stripe.Int64(200),
ProductCode: stripe.String("1234"),
ProductDescription: stripe.String("description 1"),
Quantity: stripe.Int64(2),
TaxAmount: stripe.Int64(200),
UnitCost: stripe.Int64(1000),
},
{
DiscountAmount: stripe.Int64(300),
ProductCode: stripe.String("1235"),
ProductDescription: stripe.String("description 2"),
Quantity: stripe.Int64(3),
TaxAmount: stripe.Int64(300),
UnitCost: stripe.Int64(3000),
},
},
MerchantReference: stripe.String("123"),
ShippingAddressZip: stripe.String("94110"),
ShippingAmount: stripe.Int64(700),
},
Source: &stripe.SourceParams{Token: stripe.String("src_123")},
Source: &stripe.SourceParams{Token: stripe.String("src_123")},
Shipping: &stripe.ShippingDetailsParams{
Address: &stripe.AddressParams{
Line1: stripe.String("line1"),
Expand Down
7 changes: 5 additions & 2 deletions invoice/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func TestInvoicePay(t *testing.T) {

func TestInvoiceUpdate(t *testing.T) {
invoice, err := Update("in_123", &stripe.InvoiceParams{
Forgiven: stripe.Bool(true),
Closed: stripe.Bool(true),
Params: stripe.Params{
Metadata: map[string]string{
"foo": "bar",
},
},
})
assert.Nil(t, err)
assert.NotNil(t, invoice)
Expand Down
13 changes: 13 additions & 0 deletions paymentintent.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ type PaymentIntentListParams struct {
ListParams `form:"*"`
}

// PaymentIntentLastPaymentError represents the last error happening on a payment intent.
type PaymentIntentLastPaymentError struct {
Charge string `json:"charge"`
Code string `json:"code"`
DeclineCode string `json:"decline_code"`
DocURL string `json:"doc_url"`
Message string `json:"message"`
Param string `json:"param"`
Source *PaymentSource `json:"source"`
Type ErrorType `json:"type"`
}

// PaymentIntentSourceActionAuthorizeWithURL represents the resource for the next action of type
// "authorize_with_url".
type PaymentIntentSourceActionAuthorizeWithURL struct {
Expand Down Expand Up @@ -146,6 +158,7 @@ type PaymentIntent struct {
Currency string `json:"currency"`
Customer *Customer `json:"customer"`
Description string `json:"description"`
LastPaymentError *PaymentIntentLastPaymentError `json:"last_payment_error"`
Livemode bool `json:"livemode"`
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Expand Down
34 changes: 34 additions & 0 deletions paymentintent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ import (
assert "github.com/stretchr/testify/require"
)

func TestPaymentIntentLastPaymentError_UnmarshalJSON(t *testing.T) {
errorData := map[string]interface{}{
"charge": "ch_123",
"code": "card_declined",
"decline_code": "generic_decline",
"doc_url": "https://stripe.com/docs/error-codes/card-declined",
"message": "Your card was declined.",
"source": map[string]interface{}{
"id": "card_123",
"object": "card",
"brand": "Visa",
"country": "US",
"customer": "cus_123",
"exp_month": 9,
"exp_year": 2019,
"fingerprint": "fingerprint",
"last4": "0341",
},
"type": "card_error",
}
bytes, err := json.Marshal(&errorData)
assert.NoError(t, err)

var lastPaymentError PaymentIntentLastPaymentError
err = json.Unmarshal(bytes, &lastPaymentError)
assert.NoError(t, err)

assert.Equal(t, ErrorTypeCard, lastPaymentError.Type)
assert.Equal(t, "ch_123", lastPaymentError.Charge)
assert.Equal(t, "https://stripe.com/docs/error-codes/card-declined", lastPaymentError.DocURL)
assert.Equal(t, PaymentSourceTypeCard, lastPaymentError.Source.Type)
assert.Equal(t, "card_123", lastPaymentError.Source.Card.ID)
}

func TestPaymentIntentSourceAction_UnmarshalJSON(t *testing.T) {
actionData := map[string]interface{}{
"authorize_with_url": map[string]interface{}{
Expand Down

0 comments on commit fd31501

Please sign in to comment.