Skip to content

Commit

Permalink
Merge pull request #380 from VictorAvelar/feat/add-payment-links-new-…
Browse files Browse the repository at this point in the history
…features

feat(payment_links): add support to update payment link details
  • Loading branch information
VictorAvelar authored Jun 23, 2024
2 parents 522f4f9 + 5771280 commit 2bfe6a8
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 4 deletions.
45 changes: 41 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ REST also implies a nice and clean structure for URLs or endpoints. This means y
- [type PaymentLinksList](<#PaymentLinksList>)
- [type PaymentLinksService](<#PaymentLinksService>)
- [func \(pls \*PaymentLinksService\) Create\(ctx context.Context, p PaymentLink, opts \*PaymentLinkOptions\) \(res \*Response, np \*PaymentLink, err error\)](<#PaymentLinksService.Create>)
- [func \(pls \*PaymentLinksService\) Delete\(ctx context.Context, id string\) \(res \*Response, err error\)](<#PaymentLinksService.Delete>)
- [func \(pls \*PaymentLinksService\) Get\(ctx context.Context, id string\) \(res \*Response, pl \*PaymentLink, err error\)](<#PaymentLinksService.Get>)
- [func \(pls \*PaymentLinksService\) List\(ctx context.Context, opts \*PaymentLinkOptions\) \(res \*Response, pl \*PaymentLinksList, err error\)](<#PaymentLinksService.List>)
- [func \(pls \*PaymentLinksService\) Update\(ctx context.Context, id string, p UpdatePaymentLinks\) \(res \*Response, pl \*PaymentLink, err error\)](<#PaymentLinksService.Update>)
- [type PaymentList](<#PaymentList>)
- [type PaymentMethod](<#PaymentMethod>)
- [type PaymentMethodDetails](<#PaymentMethodDetails>)
Expand Down Expand Up @@ -383,6 +385,7 @@ REST also implies a nice and clean structure for URLs or endpoints. This means y
- [type UpdateOrder](<#UpdateOrder>)
- [type UpdateOrderLine](<#UpdateOrderLine>)
- [type UpdatePayment](<#UpdatePayment>)
- [type UpdatePaymentLinks](<#UpdatePaymentLinks>)
- [type UpdateShipment](<#UpdateShipment>)
- [type UpdateSubscription](<#UpdateSubscription>)
- [type UsedGiftCard](<#UsedGiftCard>)
Expand Down Expand Up @@ -4178,7 +4181,7 @@ type PaymentLinksList struct {
```

<a name="PaymentLinksService"></a>
## type [PaymentLinksService](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L62>)
## type [PaymentLinksService](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L69>)

PaymentLinksService operates over the payment link resource.

Expand All @@ -4187,7 +4190,7 @@ type PaymentLinksService service
```

<a name="PaymentLinksService.Create"></a>
### func \(\*PaymentLinksService\) [Create](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L83-L87>)
### func \(\*PaymentLinksService\) [Create](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L90-L94>)

```go
func (pls *PaymentLinksService) Create(ctx context.Context, p PaymentLink, opts *PaymentLinkOptions) (res *Response, np *PaymentLink, err error)
Expand All @@ -4197,8 +4200,19 @@ Create generates payment links that by default, unlike regular payments, do not

See: https://docs.mollie.com/reference/v2/payment-links-api/create-payment-link

<a name="PaymentLinksService.Delete"></a>
### func \(\*PaymentLinksService\) [Delete](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L151>)

```go
func (pls *PaymentLinksService) Delete(ctx context.Context, id string) (res *Response, err error)
```

Delete removes a payment link from the website profile.

See: https://docs.mollie.com/reference/delete-payment-link

<a name="PaymentLinksService.Get"></a>
### func \(\*PaymentLinksService\) [Get](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L67>)
### func \(\*PaymentLinksService\) [Get](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L74>)

```go
func (pls *PaymentLinksService) Get(ctx context.Context, id string) (res *Response, pl *PaymentLink, err error)
Expand All @@ -4209,7 +4223,7 @@ Get retrieves a single payment link object by its id/token.
See: https://docs.mollie.com/reference/v2/payment-links-api/get-payment-link

<a name="PaymentLinksService.List"></a>
### func \(\*PaymentLinksService\) [List](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L104-L108>)
### func \(\*PaymentLinksService\) [List](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L111-L115>)

```go
func (pls *PaymentLinksService) List(ctx context.Context, opts *PaymentLinkOptions) (res *Response, pl *PaymentLinksList, err error)
Expand All @@ -4219,6 +4233,17 @@ List retrieves all payments links created with the current website profile, orde

See: https://docs.mollie.com/reference/v2/payment-links-api/list-payment-links

<a name="PaymentLinksService.Update"></a>
### func \(\*PaymentLinksService\) [Update](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L131-L135>)

```go
func (pls *PaymentLinksService) Update(ctx context.Context, id string, p UpdatePaymentLinks) (res *Response, pl *PaymentLink, err error)
```

Update changes certain details of an existing payment link.

See: https://docs.mollie.com/reference/update-payment-link

<a name="PaymentList"></a>
## type [PaymentList](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payments.go#L385-L391>)

Expand Down Expand Up @@ -6038,6 +6063,18 @@ type UpdatePayment struct {
}
```

<a name="UpdatePaymentLinks"></a>
## type [UpdatePaymentLinks](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/payment_links.go#L63-L66>)

UpdatePaymentLinks describes certain details of an existing payment link that can be updated.

```go
type UpdatePaymentLinks struct {
Description string `json:"description,omitempty"`
Archived bool `json:"archived,omitempty"`
}
```

<a name="UpdateShipment"></a>
## type [UpdateShipment](<https://github.com/VictorAvelar/mollie-api-go/blob/master/mollie/shipments.go#L37-L40>)

Expand Down
39 changes: 39 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,35 @@ 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
}

// Delete removes a payment link from the website profile.
//
// See: https://docs.mollie.com/reference/delete-payment-link
func (pls *PaymentLinksService) Delete(ctx context.Context, id string) (res *Response, err error) {
res, err = pls.client.delete(ctx, fmt.Sprintf("v2/payment-links/%s", id))
if err != nil {
return
}

return
}
179 changes: 179 additions & 0 deletions mollie/payment_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,182 @@ 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)
}
})
}
}

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

type args struct {
ctx context.Context
paymentLink string
}

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

if _, ok := r.Header[AuthHeader]; !ok {
w.WriteHeader(http.StatusUnauthorized)
}
w.WriteHeader(http.StatusNoContent)
},
},
{
"delete payment links, an error is returned from the server",
args{
context.Background(),
"pl_ka21123129",
},
true,
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
noPre,
errorHandler,
},
{
"delete payment links, invalid url when building request",
args{
context.Background(),
"pl_ka21123129",
},
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, err := tClient.PaymentLinks.Delete(c.args.ctx, c.args.paymentLink)
if c.wantErr {
assert.NotNil(t, err)
assert.EqualError(t, err, c.err.Error())
} else {
assert.Nil(t, err)
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 2bfe6a8

Please sign in to comment.