Categorygithub.com/zhutik/adyen-api-go
modulepackage
0.0.0-20200508174437-ae55db9f1015
Repository: https://github.com/zhutik/adyen-api-go.git
Documentation: pkg.go.dev

# README

[Deprecated] Adyen API for Go

This library is deprecated in favor of official one from Adyen, now it's finally released!

Thanks a lot for everyone who contributed and used this library!


Build Status GoDoc Maintainability Test Coverage Go Report Card

A Go client library for Adyen payments platform.

This is not an official client library. Adyen has official libraries for multiple platforms Github, but not Go yet.

This package provides core functionality to perform most common types of a Payment requests to an API. If you see some functionality is missing, please, open an issue (or better yet, a pull request).

Installation

go get github.com/zhutik/adyen-api-go

Playground and examples

Please check separate repository with Adyen API playground, where you can test API and get some usage examples for Adyen API library

https://github.com/zhutik/adyen-api-go-example

Or you can visit Wiki page for more details and examples

Supported API Calls

  • Authorise (Encrypted in recommended)
  • Authorise 3D
  • Recurring payments and retrieving stored payment methods
  • Capture
  • Cancel
  • Refund (CancelOrRefund)
  • Notifications

Usage

import "github.com/zhutik/adyen-api-go"

// Configure Adyen API
instance := adyen.New(
  adyen.Testing,
  os.Getenv("ADYEN_USERNAME"),
  os.Getenv("ADYEN_PASSWORD"),
)

amount := &adyen.Amount{
    Value:    1000, // amount * 100, f.e. 10,30 EUR = 1030
    Currency: "EUR" // or use instance.Currency
}

// or amount := adyen.NewAmount("EUR", 10), in this case decimal points would be adjusted automatically

req := &adyen.AuthoriseEncrypted{
  Amount: amount,
  MerchantAccount: os.Getenv("ADYEN_ACCOUNT"), // your merchant account in Adyen
  AdditionalData:  &adyen.AdditionalData{Content: "encryptedData"}, // encrypted data from a form
  Reference:       "your-order-number",
}

// Perform authorise transaction
g, err := instance.Payment().AuthoriseEncrypted(req)

Load Client Side JS for form encryption to include on credit card form page

// Configure Adyen API
instance := adyen.New(
  adyen.Testing,
  os.Getenv("ADYEN_USERNAME"),
  os.Getenv("ADYEN_PASSWORD"),
)

url := &adyen.ClientURL(os.Getenv("ADYEN_CLIENT_TOKEN"))

Currently, MerchantAccount and Currency need to be set for every request manually

To shortcut configuration, additional methods could be used to set and retrieve those settings.

// Configure Adyen API
instance := adyen.New(
  adyen.Testing,
  os.Getenv("ADYEN_USERNAME"),
  os.Getenv("ADYEN_PASSWORD"),
)

// set parameters once for current instance
instance.Currency = "USD"
instance.MerchantAccount = "TEST_MERCHANT_ACCOUNT"

// futher, information could be retrieved to populate request 
req := &adyen.AuthoriseEncrypted{
  Amount: adyen.NewAmount(instance.Currency, 10.00),
  MerchantAccount: instance.MerchantAccount,
  AdditionalData:  &adyen.AdditionalData{Content: "encryptedData"}, // encrypted data from a form
  Reference:       "your-order-number",
}

Environment configuration

Adyen's Production environment requires additional configuration to the Test environment for security reasons. Namely, this includes a random hexadecimal string that's generated for your account and the company account name.

In the following examples, the environmenst have been hard-coded for clarity. They would typically come from environments variables instead.

To target the Test environment:

env := adyen.TestEnvironment()

To target the Production environment:

env, err := adyen.ProductionEnvironment("5409c4fd1cc98a4e", "AcmeAccount123")

To run example

Expose your settings for Adyen API configuration.

$ export ADYEN_CLIENT_TOKEN="YOUR_ADYEN_CLIENT_TOKEN"
$ export ADYEN_USERNAME="YOUR_ADYEN_API_USERNAME"
$ export ADYEN_PASSWORD="YOUR_API_PASSWORD"
$ export ADYEN_ACCOUNT="YOUR_MERCHANT_ACCOUNT"

Settings explanation:

  • ADYEN_CLIENT_TOKEN - Library token in Adyen, used to load external JS file from Adyen to validate Credit Card information
  • ADYEN_USERNAME - Adyen API username, usually starts with ws@
  • ADYEN_PASSWORD - Adyen API password for username
  • ADYEN_ACCOUNT - Selected Merchant Account

Hosted Payment Pages

Update your settings to include

$ export ADYEN_HMAC="YOUR_HMAC_KEY"
$ export ADYEN_SKINCODE="YOUR_SKINCODE_ID"
$ export ADYEN_SHOPPER_LOCALE="YOUR_SHOPPER_LOCALE"

Use HPP constructor to initialize new Adyen API instance

import "github.com/zhutik/adyen-api-go"

// Configure Adyen API
instance := adyen.NewWithHMAC(
  adyen.Testing,
  os.Getenv("ADYEN_USERNAME"),
  os.Getenv("ADYEN_PASSWORD"),
  os.Getenv("ADYEN_HMAC"),
)

Perform requests as usual:

timeIn := time.Now().Local().Add(time.Minute * time.Duration(60))

req := &adyen.DirectoryLookupRequest{
    CurrencyCode:      "EUR",
    MerchantAccount:   os.Getenv("ADYEN_ACCOUNT"),
    PaymentAmount:     1000,
    SkinCode:          os.Getenv("ADYEN_SKINCODE"),
    MerchantReference: "your-order-number",
    SessionsValidity:  timeIn.Format(time.RFC3339),
}

g, err := instance.Payment().DirectoryLookup(req)

or generate redirect URL for selected payment method

Example with iDEAL for Netherlands:

timeIn := time.Now().Local().Add(time.Minute * time.Duration(60))

req := &adyen.SkipHppRequest{
    MerchantReference: "your-order-number",
    PaymentAmount:     1000,
    CurrencyCode:      "EUR",
    ShipBeforeDate:    timeIn.Format(time.RFC3339),
    SkinCode:          os.Getenv("ADYEN_SKINCODE"),
    MerchantAccount:   os.Getenv("ADYEN_ACCOUNT"),
    ShopperLocale:     "nl",
    SessionsValidity:  timeIn.Format(time.RFC3339),
    CountryCode:       "NL",
    BrandCode:         "ideal",
    IssuerID:          "1121",
}

url, err := instance.Payment().GetHPPRedirectURL(req)

http.Redirect(w, r, url, http.StatusTemporaryRedirect)

Supported Calls:

  • Directory Lookup
  • Locale payment methods redirect

Setup playgroup

Please check separate repository with Adyen API playgroup, where you can test API and get some usage example for Adyen API library

https://github.com/zhutik/adyen-api-go-example

Perform payments

Open http://localhost:8080 in your browser and check implemented actions.

Test credit cards could be found https://docs.adyen.com/support/integration#testcardnumbers

TODOs

  • Move some constants into enum files.
  • Parse URLs for environment's BaseURL, ClientURL and HppURL methods instead of string concatenation (needs to return an error as well).
  • Reduced API surface by making most types and functions unexported.

# Functions

New - creates Adyen instance Description: - env - Environment for next API calls - username - API username for authentication - password - API password for authentication - opts - an optional collection of functions that allow you to tweak configurations.
NewAmount - creates Amount instance Automatically adjust decimal points for the float value Link - https://docs.adyen.com/developers/development-resources/currency-codes.
NewStringBool returns an instance of StringBool representing a given bool.
NewWithCredentials - create new Adyen instance with pre-configured credentials.
NewWithHMAC - create new Adyen instance with HPP credentials Use this constructor when you need to use Adyen HPP API.
ProductionEnvironment returns production environment configuration.
TestEnvironment returns test environment configuration.
WithCurrency allows for custom currencies to be provided to the Adyen.
WithTimeout allows for a custom timeout to be provided to the underlying HTTP client that's used to communicate with Adyen.
WithTransport allows customer HTTP transports to be provider to the Adyen.

# Constants

AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
AVSResponse hard-coded for easy comparison checking later.
CheckoutAPIVersion - API version of current checkout API.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
Constants represented by numerical code they are assigned.
DefaultClientTimeout is the default timeout used when making HTTP requests to Adyen.
DefaultCurrency is the default currency for transactions.
Adjust authorisation reasons Link https://docs.adyen.com/developers/api-reference/payments-api/modificationrequest/adjustauthorisationmodificationrequest.
Female to indicate "female" gender.
Male to indicate "male" gender.
Adjust authorisation reasons Link https://docs.adyen.com/developers/api-reference/payments-api/modificationrequest/adjustauthorisationmodificationrequest.
PaymentAPIVersion - API version of current payment API.
PaymentService is used to identify the standard payment workflow.
RecurringAPIVersion - API version of current recurring API.
One-click functionality gives the shopper the option to store their payment details with the merchant, within the Adyen environment.
One-click functionality gives the shopper the option to store their payment details with the merchant, within the Adyen environment.
RecurringService is used to identify the recurring payment workflow.
One-click functionality gives the shopper the option to store their payment details with the merchant, within the Adyen environment.
One-click functionality gives the shopper the option to store their payment details with the merchant, within the Adyen environment.
Unknown to indicate "unknown" gender.

# Variables

CurrencyDecimals - https://docs.adyen.com/developers/currency-codes currencies with 2 decimals stripped out.
DefaultCurrencyDecimals - default currency decimals.
Production - instance of production environment.
Testing - instance of testing environment.

# Structs

AdditionalData stores encrypted information about customer's credit card.
Address - base address type for customer billing and delivery addresses Link - https://docs.adyen.com/developers/api-reference/common-api#address.
AdjustAuthorisation structure for adjusting previously authorised amount.
AdjustAuthorisationResponse is a response for AdjustAuthorisation request.
Adyen - base structure with configuration options - Credentials instance of API creditials to connect to Adyen API - Currency is a default request currency.
Amount value/currency representation.
APIError - handle error (non 200 status) response from Adyen.
Authorise structure for Authorisation request (card is not encrypted) Link - https://docs.adyen.com/developers/api-reference/payments-api#paymentrequest.
Authorise3D structure for Authorisation request (card is not encrypted) https://docs.adyen.com/developers/api-reference/payments-api#paymentrequest3d.
AuthoriseEncrypted structure for Authorisation request (with encrypted card information) Link - https://docs.adyen.com/developers/api-reference/payments-api#paymentrequest.
AuthoriseResponse is a response structure for Adyen Link - https://docs.adyen.com/developers/api-reference/payments-api#paymentresult.
BrowserInfo hold information on the user browser.
Cancel structure for Cancel request.
CancelOrRefundResponse is a response structure for Adyen cancelOrRefund.
CancelResponse is a response structure for Adyen cancellation.
Capture structure for Capture request.
CaptureResponse is a response structure for Adyen capture.
Card structure representation.
CheckoutGateway - allows you to accept all of Adyen's payment methods and flows.
DirectoryLookupRequest - get list of available payment methods based on skin, country and order details Description - https://docs.adyen.com/developers/api-reference/hosted-payment-pages-api#directoryrequest CountryCode could be used to test local payment methods, if client's IP is from different country.
DirectoryLookupResponse - api response for DirectoryLookupRequest Description - https://docs.adyen.com/developers/api-reference/hosted-payment-pages-api#directoryresponse.
Environment allows clients to be configured for Testing and Production environments.
FraudCheckResult hold information of fraud score detail.
FraudResult hold the fraud score of transaction.
ModificationGateway - Adyen modification transaction logic, capture, cancel, refunds and e.t.c.
Name - generic name structure Link - https://docs.adyen.com/developers/api-reference/common-api#name.
NotificationRequest contains environment specification and list of notifications to process Link - https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest.
NotificationRequestItem contains notification details Depending on notification type, different fields can be populated and send from Adyen Link - https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequestitem.
NotificationRequestItemData contains the NotificationRequestItem data.
OneClickPaymentMethodDetails describes the OneClickPayment part of a PaymentMethods response.
PaymentGateway - Adyen payment transaction logic.
PaymentMethod - structure for single payment method in directory look up response Part of DirectoryLookupResponse.
PaymentMethodCard describes the card information associated with a OneClick payment.
PaymentMethodDetails describes the PaymentMethods part of a PaymentMethodsResponse.
PaymentMethodDetailsInfo describes the collection of all payment methods.
PaymentMethodItems describes a single payment method.
PaymentMethods contains the fields required by the checkout API's /paymentMethods endpoint.
PaymentMethodsResponse is returned by Adyen in response to a PaymentMethods request.
PaymentMethodStoredDetails describes the information stored for a OneClick payment.
PaymentMethodTypes describes any additional information associated with a OneClick payment.
Recurring hold the behavior for a future payment : could be ONECLICK or RECURRING.
RecurringDetail structure to hold information associated to a recurring payment Link - https://docs.adyen.com/developers/api-reference/recurring-api#recurringdetail.
RecurringDetailsRequest structure to list all recurring payment associated to a shopperReference Link - https://docs.adyen.com/developers/api-reference/recurring-api#recurringdetailsrequest.
RecurringDetailsResult structure to hold the RecurringDetails Link - https://docs.adyen.com/developers/api-reference/recurring-api#recurringdetailsresult.
RecurringDisableRequest structure to hold information regarding disable recurring request If `RecurringDetailReference` is specified, specific payment ID will be disabled otherwise all customer saved payment methods will be disabled Link - https://docs.adyen.com/developers/api-reference/recurring-api#disablerequest.
RecurringDisableResponse structure to hold response for disable recurring request Link - https://docs.adyen.com/developers/api-reference/recurring-api#disableresult.
RecurringGateway - Adyen recurring transaction logic.
Refund structure for refund request.
RefundResponse is a response structure for Adyen refund request.
Response - Adyen API response structure.
Result hold the fraud score detail.
SkipHppRequest contains data that would be used to create Adyen HPP redirect URL Link: https://docs.adyen.com/developers/ecommerce-integration/local-payment-methods Request description: https://docs.adyen.com/developers/api-reference/hosted-payment-pages-api#skipdetailsrequest.
TechnicalCancel structure for performing technical cancellation Link - https://docs.adyen.com/developers/payment-modifications#technicalcancel.
TechnicalCancelResponse is a response for TechnicalCancel request.

# Type aliases

AVSResponse is a type definition for all possible responses from Adyen's AVS system https://docs.adyen.com/risk-management/avs-checks.
CVCResult represents the Adyen translation of CVC codes from issuer https://docs.adyen.com/development-resources/test-cards/cvc-cvv-result-testing.
Option allows for custom configuration overrides.
StringBool allows us to unmarhsal Adyen Boolean values which appear as strings instead of bools.