Skip to content

Commit

Permalink
feat(payment_links): add support to update payment link details
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorAvelar committed Jun 23, 2024
1 parent 522f4f9 commit 5b8a699
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mollie/payment_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ type PaymentLinksList struct {
} `json:"_embedded,omitempty"`
}

// UpdatePaymentLinks describes certain details of an existing payment link
// that can be updated.
type UpdatePaymentLinks struct {
Description string `json:"description,omitempty"`
Archived bool `json:"archived,omitempty"`
}

// PaymentLinksService operates over the payment link resource.
type PaymentLinksService service

Expand Down Expand Up @@ -117,3 +124,23 @@ func (pls *PaymentLinksService) List(ctx context.Context, opts *PaymentLinkOptio

return
}

// Update changes certain details of an existing payment link.
//
// See: https://docs.mollie.com/reference/update-payment-link
func (pls *PaymentLinksService) Update(ctx context.Context, id string, p UpdatePaymentLinks) (
res *Response,
pl *PaymentLink,
err error,
) {
res, err = pls.client.patch(ctx, fmt.Sprintf("v2/payment-links/%s", id), p)
if err != nil {
return
}

if err = json.Unmarshal(res.content, &pl); err != nil {
return
}

return
}
99 changes: 99 additions & 0 deletions mollie/payment_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,102 @@ func TestPaymentLinkService_List(t *testing.T) {
})
}
}

func TestPaymentLinkService_Update(t *testing.T) {
setEnv()
defer unsetEnv()

type args struct {
ctx context.Context
paymentLink string
pl UpdatePaymentLinks
}

cases := []struct {
name string
args args
wantErr bool
err error
pre func()
handler http.HandlerFunc
}{
{
"update payment links works as expected.",
args{
context.Background(),
"pl_ka21123129",
UpdatePaymentLinks{
Archived: true,
},
},
false,
nil,
noPre,
func(w http.ResponseWriter, r *http.Request) {
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
testMethod(t, r, "PATCH")

if _, ok := r.Header[AuthHeader]; !ok {
w.WriteHeader(http.StatusUnauthorized)
}
_, _ = w.Write([]byte(testdata.UpdatePaymentLinksResponse))
},
},
{
"update payment links, an error is returned from the server",
args{
context.Background(),
"pl_ka21123129",
UpdatePaymentLinks{},
},
true,
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
noPre,
errorHandler,
},
{
"update payment links, an error occurs when parsing json",
args{
context.Background(),
"pl_ka21123129",
UpdatePaymentLinks{},
},
true,
fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
noPre,
encodingHandler,
},
{
"update payment links, invalid url when building request",
args{
context.Background(),
"pl_ka21123129",
UpdatePaymentLinks{},
},
true,
errBadBaseURL,
crashSrv,
errorHandler,
},
}

for _, c := range cases {
setup()
defer teardown()

t.Run(c.name, func(t *testing.T) {
c.pre()
tMux.HandleFunc(fmt.Sprintf("/v2/payment-links/%s", c.args.paymentLink), c.handler)

res, m, err := tClient.PaymentLinks.Update(c.args.ctx, c.args.paymentLink, c.args.pl)
if c.wantErr {
assert.NotNil(t, err)
assert.EqualError(t, err, c.err.Error())
} else {
assert.Nil(t, err)
assert.IsType(t, &PaymentLink{}, m)
assert.IsType(t, &http.Response{}, res.Response)
}
})
}
}
32 changes: 32 additions & 0 deletions testdata/payment_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,35 @@ const ListPaymentLinksResponse = `{
}
}
}`

// UpdatePaymentLinksResponse example.
const UpdatePaymentLinksResponse = `{
"resource": "payment-link",
"id": "pl_4Y0eZitmBnQ6IDoMqZQKh",
"mode": "live",
"description": "Bicycle tires",
"amount": {
"currency": "EUR",
"value": "24.95"
},
"archived": true,
"redirectUrl": "https://webshop.example.org/thanks",
"webhookUrl": "https://webshop.example.org/payment-links/webhook",
"profileId": "pfl_QkEhN94Ba",
"createdAt": "2021-03-20T09:29:56+00:00",
"expiresAt": "2023-06-06T11:00:00+00:00",
"_links": {
"self": {
"href": "...",
"type": "application/hal+json"
},
"paymentLink": {
"href": "https://paymentlink.mollie.com/payment/4Y0eZitmBnQ6IDoMqZQKh",
"type": "text/html"
},
"documentation": {
"href": "...",
"type": "text/html"
}
}
}`

0 comments on commit 5b8a699

Please sign in to comment.