Skip to content

Commit

Permalink
Add technical cancel API request
Browse files Browse the repository at this point in the history
  • Loading branch information
zhutik committed Sep 10, 2018
1 parent 243ec70 commit dd596b8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,22 @@ type AdjustAuthorisationResponse struct {
PspReference string `json:"pspReference"`
Response string `json:"response"`
}

/******************
* Techical Cancel *
******************/

// TechnicalCancel structure for performing technical cancellation
//
// Link - https://docs.adyen.com/developers/payment-modifications#technicalcancel
type TechnicalCancel struct {
MerchantAccount string `json:"merchantAccount"`
OriginalMerchantReference string `json:"originalMerchantReference"`
Reference string `json:"reference,omitempty"`
}

// TechnicalCancelResponse is a response for TechnicalCancel request
type TechnicalCancelResponse struct {
PspReference string `json:"pspReference"`
Response string `json:"response"`
}
16 changes: 16 additions & 0 deletions modification_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
cancelOrRefundType = "cancelOrRefund"
refundType = "refund"
adjustAuthorisation = "adjustAuthorisation"
technicalCancel = "technicalCancel"
)

// ModificationGateway - Adyen modification transaction logic, capture, cancel, refunds and e.t.c
Expand Down Expand Up @@ -81,3 +82,18 @@ func (a *ModificationGateway) AdjustAuthorisation(req *AdjustAuthorisation) (*Ad

return resp.adjustAuthorisation()
}

// TechnicalCancel - perform cancellation without knowing orinal payment reference (PSP), f.e. in case of technical error
//
// Link - https://docs.adyen.com/developers/payment-modifications#technicalcancel
func (a *ModificationGateway) TechnicalCancel(req *TechnicalCancel) (*TechnicalCancelResponse, error) {
url := a.adyenURL(PaymentService, technicalCancel, PaymentAPIVersion)

resp, err := a.execute(url, req)

if err != nil {
return nil, err
}

return resp.technicalCancel()
}
10 changes: 10 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func (r *Response) adjustAuthorisation() (*AdjustAuthorisationResponse, error) {
return &a, nil
}

// technicalCancel - generate Adyen Technical Cancel API Response
func (r *Response) technicalCancel() (*TechnicalCancelResponse, error) {
var a TechnicalCancelResponse
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}

return &a, nil
}

// directoryLookup - generate Adyen Directory Lookup response
func (r *Response) directoryLookup() (*DirectoryLookupResponse, error) {
var a DirectoryLookupResponse
Expand Down
50 changes: 50 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,53 @@ func TestAdjustAuthorisationResponse(t *testing.T) {
})
}
}

func TestTechnicalCancelResponse(t *testing.T) {
cases := []struct {
name string
input string
reference string
response string
expErr bool
}{
{
name: "technicalCancel response",
input: `{
"pspReference" : "8413547924770610",
"response" : "[technical-cancel-received]"
}`,
reference: "8413547924770610",
response: "[technical-cancel-received]",
},
{
name: "technicalCancel returns errors",
input: "some error string",
reference: "",
response: "",
expErr: true,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
response, err := createTestResponse(c.input, "OK 200", 200)

if err != nil {
t.Fatal(err)
}

res, err := response.technicalCancel()

if c.expErr {
if err == nil {
t.Fatal("expected error but didn't get one")
}

return
}

equals(t, c.reference, res.PspReference)
equals(t, c.response, res.Response)
})
}
}

0 comments on commit dd596b8

Please sign in to comment.