Skip to content

Commit

Permalink
Add support to the Recurring service, need to change adyen.go and env…
Browse files Browse the repository at this point in the history
…ironnement.go to accept other endpoint than /Payment
  • Loading branch information
lbailly committed Oct 3, 2017
1 parent f6cac4f commit c4995df
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
19 changes: 15 additions & 4 deletions adyen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ const (
APIVersion = "v25"
)

// Enpoint service to use
const (
PaymentService = "Payment"
RecurringService = "Recurring"
)

// New - creates Adyen instance
//
// Description:
Expand Down Expand Up @@ -71,8 +77,8 @@ func (a *Adyen) ClientURL() string {
}

// adyenURL returns Adyen backend URL
func (a *Adyen) adyenURL(requestType string) string {
return a.Credentials.Env.BaseURL(APIVersion) + "/" + requestType
func (a *Adyen) adyenURL(service string, requestType string) string {
return a.Credentials.Env.BaseURL(service, APIVersion) + "/" + requestType
}

// createHPPUrl returns Adyen HPP url
Expand All @@ -97,13 +103,13 @@ func (a *Adyen) SetCurrency(currency string) {
}

// execute request on Adyen side, transforms "requestEntity" into JSON representation
func (a *Adyen) execute(method string, requestEntity interface{}) (*Response, error) {
func (a *Adyen) execute(method string, service string, requestEntity interface{}) (*Response, error) {
body, err := json.Marshal(requestEntity)
if err != nil {
return nil, err
}

url := a.adyenURL(method)
url := a.adyenURL(service, method) + "/" + service + "/"
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, err
Expand Down Expand Up @@ -195,3 +201,8 @@ func (a *Adyen) Payment() *PaymentGateway {
func (a *Adyen) Modification() *ModificationGateway {
return &ModificationGateway{a}
}

// Recurring - returns RecurringGateway
func (a *Adyen) Recurring() *RecurringGateway {
return &RecurringGateway{a}
}
8 changes: 4 additions & 4 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func newEnvironment(apiURL, clientURL, hppURL string) environment {
}

// BaseURL returns api base url
func (e environment) BaseURL(version string) string {
return e.apiURL + "/" + version
func (e environment) BaseURL(service string, version string) string {
return e.apiURL + "/" + service + "/" + version
}

// ClientURL returns Adyen Client URL to load external scripts
Expand All @@ -29,14 +29,14 @@ func (e environment) HppURL(request string) string {

// Testing - instance of testing environment
var Testing = newEnvironment(
"https://pal-test.adyen.com/pal/servlet/Payment",
"https://pal-test.adyen.com/pal/servlet",
"https://test.adyen.com/hpp/cse/js/",
"https://test.adyen.com/hpp/",
)

// Production - instance of production environment
var Production = newEnvironment(
"https://pal-live.adyen.com/pal/servlet/Payment",
"https://pal-live.adyen.com/pal/servlet",
"https://live.adyen.com/hpp/cse/js/",
"https://live.adyen.com/hpp/",
)
8 changes: 4 additions & 4 deletions modification_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ModificationGateway struct {

// Capture - Perform capture payment in Adyen
func (a *ModificationGateway) Capture(req *Capture) (*CaptureResponse, error) {
resp, err := a.execute(captureType, req)
resp, err := a.execute(captureType, PaymentService, req)

if err != nil {
return nil, err
Expand All @@ -28,7 +28,7 @@ func (a *ModificationGateway) Capture(req *Capture) (*CaptureResponse, error) {

// Cancel - Perform cancellation of the authorised transaction
func (a *ModificationGateway) Cancel(req *Cancel) (*CancelResponse, error) {
resp, err := a.execute(cancelType, req)
resp, err := a.execute(cancelType, PaymentService, req)

if err != nil {
return nil, err
Expand All @@ -40,7 +40,7 @@ func (a *ModificationGateway) Cancel(req *Cancel) (*CancelResponse, error) {
// CancelOrRefund - Perform cancellation for not captured transaction
// otherwise perform refund action
func (a *ModificationGateway) CancelOrRefund(req *Cancel) (*CancelOrRefundResponse, error) {
resp, err := a.execute(cancelOrRefundType, req)
resp, err := a.execute(cancelOrRefundType, PaymentService, req)

if err != nil {
return nil, err
Expand All @@ -51,7 +51,7 @@ func (a *ModificationGateway) CancelOrRefund(req *Cancel) (*CancelOrRefundRespon

// Refund - perform refund for already captured request
func (a *ModificationGateway) Refund(req *Refund) (*RefundResponse, error) {
resp, err := a.execute(refundType, req)
resp, err := a.execute(refundType, PaymentService, req)

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion payment3d_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const authorise3DType = "authorise3d"

// Authorise3D - Perform authorise payment in Adyen
func (a *PaymentGateway) Authorise3D(req *Authorise3D) (*AuthoriseResponse, error) {
resp, err := a.execute(authorise3DType, req)
resp, err := a.execute(authorise3DType, PaymentService, req)

if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions payment_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const directoryLookupURL = "directory/v2"

// AuthoriseEncrypted - Perform authorise payment in Adyen
func (a *PaymentGateway) AuthoriseEncrypted(req *AuthoriseEncrypted) (*AuthoriseResponse, error) {
resp, err := a.execute(authoriseType, req)
resp, err := a.execute(authoriseType, PaymentService, req)

if err != nil {
return nil, err
Expand All @@ -24,7 +24,7 @@ func (a *PaymentGateway) AuthoriseEncrypted(req *AuthoriseEncrypted) (*Authorise

// Authorise - Perform authorise payment in Adyen
func (a *PaymentGateway) Authorise(req *Authorise) (*AuthoriseResponse, error) {
resp, err := a.execute(authoriseType, req)
resp, err := a.execute(authoriseType, PaymentService, req)

if err != nil {
return nil, err
Expand Down
39 changes: 39 additions & 0 deletions recurring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package adyen

// RecurringDetailsRequest structure to list all recurring payment associated to a shopperReference
type RecurringDetailsRequest struct {
MerchantAccount string `json:"merchantAccount"`
ShopperReference string `json:"shopperReference,omitempty"`
// Not mandatory
Recurring *Recurring `json:"recurring,omitempty"`
}

// RecurringDetailsResult structure to hold the RecurringDetails
type RecurringDetailsResult struct {
CreationDate string `json:"creationDate"`
Details []struct {
RecurringDetail struct {
Acquirer string `json:"acquirer"`
AcquirerAccount string `json:"acquirerAccount"`
AdditionalData struct {
CardBin string `json:"cardBin"`
} `json:"additionalData"`
Alias string `json:"alias"`
AliasType string `json:"aliasType"`
Card struct {
ExpiryMonth string `json:"expiryMonth"`
ExpiryYear string `json:"expiryYear"`
HolderName string `json:"holderName"`
Number string `json:"number"`
} `json:"card"`
ContractTypes []string `json:"contractTypes"`
CreationDate string `json:"creationDate"`
FirstPspReference string `json:"firstPspReference"`
PaymentMethodVariant string `json:"paymentMethodVariant"`
RecurringDetailReference string `json:"recurringDetailReference"`
Variant string `json:"variant"`
} `json:"RecurringDetail"`
} `json:"details"`
InvalidOneclickContracts string `json:"invalidOneclickContracts"`
ShopperReference string `json:"shopperReference"`
}
20 changes: 20 additions & 0 deletions recurring_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package adyen

// RecurringGateway - Adyen recurring transaction logic
type RecurringGateway struct {
*Adyen
}

// listRecurringDetailsType - listRecurringDetails type request, @TODO: move to enums
const listRecurringDetailsType = "listRecurringDetails"

// AuthoriseEncrypted - Perform authorise payment in Adyen
func (a *RecurringGateway) ListRecurringDetails(req *RecurringDetailsRequest) (*RecurringDetailsResult, error) {
resp, err := a.execute(listRecurringDetailsType, RecurringService, req)

if err != nil {
return nil, err
}

return resp.listRecurringDetails()
}
10 changes: 10 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ func (r *Response) directoryLookup() (*DirectoryLookupResponse, error) {

return &a, nil
}

// listRecurringDetails - generate Adyen List Recurring Details response
func (r *Response) listRecurringDetails() (*RecurringDetailsResult, error) {
var a RecurringDetailsResult
if err := json.Unmarshal(r.Body, &a); err != nil {
return nil, err
}

return &a, nil
}

0 comments on commit c4995df

Please sign in to comment.