From 156400047e9f5c33c2545ca4444ffb9842093a51 Mon Sep 17 00:00:00 2001 From: Victor Hugo Avelar Ossorio Date: Sun, 23 Jun 2024 10:08:33 +0000 Subject: [PATCH] feat(payment_links): add support to delete payment links --- mollie/payment_links.go | 12 ++++++ mollie/payment_links_test.go | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/mollie/payment_links.go b/mollie/payment_links.go index 1ac3972..33e256e 100644 --- a/mollie/payment_links.go +++ b/mollie/payment_links.go @@ -144,3 +144,15 @@ func (pls *PaymentLinksService) Update(ctx context.Context, id string, p UpdateP 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 +} diff --git a/mollie/payment_links_test.go b/mollie/payment_links_test.go index fe17028..0c48812 100644 --- a/mollie/payment_links_test.go +++ b/mollie/payment_links_test.go @@ -419,3 +419,83 @@ func TestPaymentLinkService_Update(t *testing.T) { }) } } + +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) + } + }) + } +}