Skip to content

Commit

Permalink
Add adjustModification request
Browse files Browse the repository at this point in the history
  • Loading branch information
zhutik committed Feb 18, 2018
1 parent c4e4b2d commit 83f7a5c
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 39 deletions.
29 changes: 29 additions & 0 deletions adyen_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package adyen

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -108,3 +111,29 @@ func randomString(l int) string {
}
return string(bytes)
}

// createTestResponse - create response object for tests
func createTestResponse(input, status string, code int) (*Response, error) {
body := strings.NewReader(input)

resp := &http.Response{
Status: status,
StatusCode: code,
ContentLength: int64(body.Len()),
Body: ioutil.NopCloser(body),
}

buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp.Body)

if err != nil {
return nil, err
}

response := &Response{
Response: resp,
Body: buf.Bytes(),
}

return response, nil
}
29 changes: 29 additions & 0 deletions modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ package adyen
* Cancel *
*********/

// Adjust authorisation reasons
//
// Link https://docs.adyen.com/developers/api-reference/payments-api/modificationrequest/adjustauthorisationmodificationrequest
const (
DelayedCharge = "DelayedCharge"
NoShow = "NoShow"
)

// Cancel structure for Cancel request
type Cancel struct {
Reference string `json:"reference"`
Expand Down Expand Up @@ -58,3 +66,24 @@ type RefundResponse struct {
PspReference string `json:"pspReference"`
Response string `json:"response"`
}

/***********************
* Adjust Authorisation *
***********************/

// AdjustAuthorisation structure for adjusting previously authorised amount
type AdjustAuthorisation struct {
ModificationAmount *Amount `json:"modificationAmount"`
Reference string `json:"reference"`
MerchantAccount string `json:"merchantAccount"`
OriginalReference string `json:"originalReference"`
AdditionalData struct {
IndustryUsage string `json:"industryUsage"`
} `json:"additionalData,omitempty"`
}

// AdjustAuthorisationResponse is a response for AdjustAuthorisation request
type AdjustAuthorisationResponse struct {
PspReference string `json:"pspReference"`
Response string `json:"response"`
}
24 changes: 20 additions & 4 deletions modification_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package adyen

// Adyen Modification actions
const (
captureType = "capture"
cancelType = "cancel"
cancelOrRefundType = "cancelOrRefund"
refundType = "refund"
captureType = "capture"
cancelType = "cancel"
cancelOrRefundType = "cancelOrRefund"
refundType = "refund"
adjustAuthorisation = "adjustAuthorisation"
)

// ModificationGateway - Adyen modification transaction logic, capture, cancel, refunds and e.t.c
Expand Down Expand Up @@ -65,3 +66,18 @@ func (a *ModificationGateway) Refund(req *Refund) (*RefundResponse, error) {

return resp.refund()
}

// AdjustAuthorisation - perform adjustAuthorisation request to modify already authorised amount
//
// Link - https://docs.adyen.com/developers/payment-modifications#adjustauthorisation
func (a *ModificationGateway) AdjustAuthorisation(req *AdjustAuthorisation) (*AdjustAuthorisationResponse, error) {
url := a.adyenURL(PaymentService, adjustAuthorisation, PaymentAPIVersion)

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

if err != nil {
return nil, err
}

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

// adjustAuthorisation - generate Adyen Refund API Response
func (r *Response) adjustAuthorisation() (*AdjustAuthorisationResponse, error) {
var a AdjustAuthorisationResponse
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
Loading

0 comments on commit 83f7a5c

Please sign in to comment.