Skip to content

Commit

Permalink
Merge pull request #132 from rjjatson/feature/payout-idempotency-key
Browse files Browse the repository at this point in the history
Add idempotency key parameter to payout creation
  • Loading branch information
sekaranglila authored Jul 4, 2023
2 parents 8a3d6f2 + 8c97da6 commit 5272909
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 6 additions & 1 deletion payout/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package payout
import (
"context"
"fmt"
"net/http"

"github.com/xendit/xendit-go"
"github.com/xendit/xendit-go/utils/validator"
Expand All @@ -26,13 +27,17 @@ func (c *Client) CreateWithContext(ctx context.Context, data *CreateParams) (*xe
}

response := &xendit.Payout{}
header := http.Header{}
if data.IdempotencyKey != "" {
header.Add("x-idempotency-key", data.IdempotencyKey)
}

err := c.APIRequester.Call(
ctx,
"POST",
fmt.Sprintf("%s/payouts", c.Opt.XenditURL),
c.Opt.SecretKey,
nil,
header,
data,
response,
)
Expand Down
7 changes: 4 additions & 3 deletions payout/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package payout

// CreateParams contains parameters for Create
type CreateParams struct {
ExternalID string `json:"external_id" validate:"required"`
Amount float64 `json:"amount" validate:"required"`
Email string `json:"email" validate:"required"`
IdempotencyKey string `json:"-"`
ExternalID string `json:"external_id" validate:"required"`
Amount float64 `json:"amount" validate:"required"`
Email string `json:"email" validate:"required"`
}

// GetParams contains parameters for Get
Expand Down
3 changes: 2 additions & 1 deletion payout/payout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestCreatePayout(t *testing.T) {
{
desc: "should create an payout",
data: &payout.CreateParams{
IdempotencyKey: "unique-idempotency-key",
ExternalID: "payout-external-id",
Amount: 200000,
Email: "customer@customer.com",
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestCreatePayout(t *testing.T) {
"POST",
xendit.Opt.XenditURL+"/payouts",
xendit.Opt.SecretKey,
nil,
mock.Anything,
tC.data,
&xendit.Payout{},
).Return(nil)
Expand Down

0 comments on commit 5272909

Please sign in to comment.