Skip to content

Commit

Permalink
Add trigger for 'payment_intent.canceled' event (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakurin authored and ob-stripe committed Oct 11, 2019
1 parent 1740886 commit e60f00d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func newTriggerCmd() *triggerCmd {
"payment_intent.created",
"payment_intent.payment_failed",
"payment_intent.succeeded",
"payment_intent.canceled",
"payment_method.attached",
},
Short: "Trigger test webhook events to fire",
Expand Down Expand Up @@ -79,6 +80,7 @@ needed to create the triggered event.
payment_intent.created
payment_intent.payment_failed
payment_intent.succeeded
payment_intent.canceled
payment_method.attached
You can also resend a past event using the --event flag:
Expand Down Expand Up @@ -146,6 +148,7 @@ func (tc *triggerCmd) runTriggerCmd(cmd *cobra.Command, args []string) error {
"payment_intent.created": examples.PaymentIntentCreated,
"payment_intent.payment_failed": examples.PaymentIntentFailed,
"payment_intent.succeeded": examples.PaymentIntentSucceeded,
"payment_intent.canceled": examples.PaymentIntentCanceled,
"payment_method.attached": examples.PaymentMethodAttached,
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/requests/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,26 @@ func (ex *Examples) PaymentIntentFailed() error {
return err
}

// PaymentIntentCanceled creates a canceled payment intent
func (ex *Examples) PaymentIntentCanceled() error {
paymentIntent, err := ex.paymentIntentCreated([]string{
"amount=2000",
"currency=usd",
})

if err != nil {
return err
}

req, params := ex.buildRequest(http.MethodPost, []string{
"cancellation_reason=requested_by_customer",
})
reqURL := fmt.Sprintf("/v1/payment_intents/%v/cancel", paymentIntent["id"])
_, err = ex.performStripeRequest(req, reqURL, params)

return err
}

func (ex *Examples) paymentMethodCreated(card string) (map[string]interface{}, error) {
req, params := ex.buildRequest(http.MethodPost, []string{
"type=card",
Expand Down
18 changes: 18 additions & 0 deletions pkg/requests/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,24 @@ func TestPaymentIntentSucceeded(t *testing.T) {
require.Nil(t, err)
}

func TestPaymentIntentCanceled(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
data := jsonBytes()
w.Write(data)
}))
defer ts.Close()

ex := Examples{
APIBaseURL: ts.URL,
APIVersion: "v1",
APIKey: "secret-key",
}

err := ex.PaymentIntentCanceled()
require.Nil(t, err)
}

func TestPaymentIntentFailed(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit e60f00d

Please sign in to comment.