Class: AdyenClient
- Inherits:
-
Object
- Object
- AdyenClient
- Includes:
- HTTParty
- Defined in:
- lib/adyen_client.rb,
lib/adyen_client/utils.rb,
lib/adyen_client/response.rb,
lib/adyen_client/configuration.rb
Defined Under Namespace
Modules: Utils Classes: Configuration, Response
Instance Attribute Summary collapse
-
#merchant_account ⇒ Object
readonly
Returns the value of attribute merchant_account.
Class Method Summary collapse
-
.configuration ⇒ Object
Internal: Access the configuration instance.
-
.configure(hash = nil) {|configuration| ... } ⇒ Object
Public: Configure the AdyenClient class.
-
.cse_public_key ⇒ Object
Public: Returns the configured CSE (client side encryption) public key.
-
.generation_time ⇒ Object
Public: Returns an ISO8601 formatted datetime string used in Adyens generationTime.
Instance Method Summary collapse
-
#authorise(encrypted_card:, amount:, reference:, merchant_account: @merchant_account, currency: @currency, shopper: {}) ⇒ Object
(also: #authorize)
Public: Charge a credit card.
-
#authorise_recurring_payment(reference:, shopper_reference:, amount:, recurring_reference: "LATEST", merchant_account: @merchant_account, currency: configuration.default_currency) ⇒ Object
(also: #authorize_recurring_payment)
Public: Charge a user by referencing his stored payment method.
-
#cancel(original_reference:, reference:, merchantAccount: @merchant_account) ⇒ Object
Public: Cancels a credit card transaction.
-
#cancel_or_refund(original_reference:, reference:, merchantAccount: @merchant_account) ⇒ Object
Public: Cancels or refunds a credit card transaction.
-
#configuration ⇒ Object
Internal: Returns the AdyenClient configuration singleton.
-
#create_recurring_contract(encrypted_card:, reference:, shopper:, merchant_account: @merchant_account, currency: @currency) ⇒ Object
Public: Store a payment method on a reference id for recurring/later use.
-
#initialize(merchant_account: configuration.default_merchant_account, currency: configuration.default_currency, response_class: Response) ⇒ AdyenClient
constructor
Public: Initializes a new instance of the AdyenClient.
-
#list_recurring_details(shopper_reference:, merchant_account: @merchant_account, contract: "RECURRING") ⇒ Object
Public: List the stored payment methods for a user.
-
#postJSON(path, data) ⇒ Object
Internal: Send a POST request to the Adyen API.
-
#refund(original_reference:, amount:, reference:, merchantAccount: @merchant_account, currency: @currency) ⇒ Object
Public: Refunds a credit card transaction.
-
#verify(encrypted_card:, reference:, amount: 0, merchant_account: @merchant_account, currency: @currency, shopper: {}) ⇒ Object
Public: Verify a credit card (does not create a charge, but may be verified for a specified amount).
Constructor Details
#initialize(merchant_account: configuration.default_merchant_account, currency: configuration.default_currency, response_class: Response) ⇒ AdyenClient
Public: Initializes a new instance of the AdyenClient.
You can override merchant_account and currency from the default configuration.
:merchant_account - Sets the default_merchant_account for this instance. (optional) :currency - Sets the default_currency for this instance. (optional) :response_class - Use a custom class for handling responses from Adyen. (optional)
Returns an AdyenClient::Response or your specific response implementation.
71 72 73 74 75 |
# File 'lib/adyen_client.rb', line 71 def initialize(merchant_account: configuration.default_merchant_account, currency: configuration.default_currency, response_class: Response) @merchant_account = merchant_account @currency = currency @response_class = response_class end |
Instance Attribute Details
#merchant_account ⇒ Object (readonly)
Returns the value of attribute merchant_account.
61 62 63 |
# File 'lib/adyen_client.rb', line 61 def merchant_account @merchant_account end |
Class Method Details
.configuration ⇒ Object
Internal: Access the configuration instance.
10 11 12 |
# File 'lib/adyen_client.rb', line 10 def self.configuration @configuration ||= Configuration.new end |
.configure(hash = nil) {|configuration| ... } ⇒ Object
Public: Configure the AdyenClient class.
hash - The configuration to apply. Will be evaluated before &block. (optional if &block is given) &block - Yields the configuration instance. (optional if hash is given)
Examples
# Block style
AdyenClient.configure do |c|
c.environment = :test
c.username = "[email protected]"
c.password = "correctbatteryhorsestaple"
c.cse_public_key = "10001|..."
c.default_merchant_account = "FooBar123"
c.default_currency = "EUR"
end
# Hash style works too, string or symbol keys
AdyenClient.configure(environment: :test, username: "[email protected]", ...)
# That comes in handy to configure the client from a YAML file
AdyenClient.configure(YAML.load_file(Rails.root.join("config", "adyen.yml"))[Rails.env.to_s])
# You can override all default options for each instance of a client
client = AdyenClient.new(merchant_account: "FooBarSubMerchant123")
eur_client = AdyenClient.new(currency: "EUR")
Yields the configuration singleton.
Returns the configuration singleton.
44 45 46 47 48 49 |
# File 'lib/adyen_client.rb', line 44 def self.configure(hash = nil) configuration.set(hash) if hash yield configuration if block_given? configuration.apply(self) configuration end |
.cse_public_key ⇒ Object
Public: Returns the configured CSE (client side encryption) public key.
57 58 59 |
# File 'lib/adyen_client.rb', line 57 def self.cse_public_key configuration.cse_public_key end |
.generation_time ⇒ Object
Public: Returns an ISO8601 formatted datetime string used in Adyens generationTime.
52 53 54 |
# File 'lib/adyen_client.rb', line 52 def self.generation_time Time.now.iso8601 end |
Instance Method Details
#authorise(encrypted_card:, amount:, reference:, merchant_account: @merchant_account, currency: @currency, shopper: {}) ⇒ Object Also known as:
Public: Charge a credit card.
:encrypted_card - The encrypted credit card information generated by the CSE (client side encryption) javascript integration. :amount - The integer amount in cents. :reference - Your reference id for this transaction. :merchant_account - Use a specific merchant account for this transaction. (default: set by the instance or configuration default merchant account) :currency - Use a specific 3-letter currency code. (default: set by the instance or configuration default currency) :shopper - The hash describing the shopper for this transaction, optional but recommended (default: {}):
:email - The shoppers email address (optional but recommended).
:ip - The shoppers last known ip address (optional but recommended).
:reference - Your reference id for this shopper/user (optional).
Returns an AdyenClient::Response or your specific response implementation.
155 156 157 158 159 160 161 162 |
# File 'lib/adyen_client.rb', line 155 def (encrypted_card:, amount:, reference:, merchant_account: @merchant_account, currency: @currency, shopper: {}) postJSON("/Payment/v12/authorise", reference: reference, amount: { value: amount, currency: currency }, merchantAccount: merchant_account, additionalData: { "card.encrypted.json": encrypted_card } ) end |
#authorise_recurring_payment(reference:, shopper_reference:, amount:, recurring_reference: "LATEST", merchant_account: @merchant_account, currency: configuration.default_currency) ⇒ Object Also known as:
Public: Charge a user by referencing his stored payment method.
:shopper_reference - The user reference id from your side. :amount - The amount to charge in cents. :reference - Your reference id for this transaction. :recurring_reference - Use when referencing a specific payment method stored for the user. (default: “LATEST”) :merchant_account - Use a specific merchant account for this transaction. (default: set by the instance or configuration default merchant account) :currency - Use a specific 3-letter currency code. (default: set by the instance or configuration default currency)
Returns an AdyenClient::Response or your specific response implementation.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/adyen_client.rb', line 87 def (reference:, shopper_reference:, amount:, recurring_reference: "LATEST", merchant_account: @merchant_account, currency: configuration.default_currency) postJSON("/Payment/v12/authorise", reference: reference, amount: { value: amount, currency: currency }, merchantAccount: merchant_account, shopperReference: shopper_reference, selectedRecurringDetailReference: recurring_reference, selectedBrand: "", recurring: { contract: "RECURRING" }, shopperInteraction: "ContAuth" ) end |
#cancel(original_reference:, reference:, merchantAccount: @merchant_account) ⇒ Object
Public: Cancels a credit card transaction.
:original_reference - The psp_reference from Adyen for this transaction. :reference - Your reference id for this transaction. :merchant_account - Use a specific merchant account for this transaction (default: set by the instance or configuration default merchant account).
Returns an AdyenClient::Response or your specific response implementation.
195 196 197 198 199 200 201 |
# File 'lib/adyen_client.rb', line 195 def cancel(original_reference:, reference:, merchantAccount: @merchant_account) postJSON("/Payment/v12/cancel", reference: reference, merchantAccount: merchant_account, originalReference: original_reference ) end |
#cancel_or_refund(original_reference:, reference:, merchantAccount: @merchant_account) ⇒ Object
Public: Cancels or refunds a credit card transaction. Use this if you don’t know the exact state of a transaction.
:original_reference - The psp_reference from Adyen for this transaction. :reference - Your reference id for this transaction. :merchant_account - Use a specific merchant account for this transaction (default: set by the instance or configuration default merchant account).
Returns an AdyenClient::Response or your specific response implementation.
228 229 230 231 232 233 234 |
# File 'lib/adyen_client.rb', line 228 def cancel_or_refund(original_reference:, reference:, merchantAccount: @merchant_account) postJSON("/Payment/v12/cancelOrRefund", reference: reference, merchantAccount: merchant_account, originalReference: original_reference ) end |
#configuration ⇒ Object
Internal: Returns the AdyenClient configuration singleton
248 249 250 |
# File 'lib/adyen_client.rb', line 248 def configuration self.class.configuration end |
#create_recurring_contract(encrypted_card:, reference:, shopper:, merchant_account: @merchant_account, currency: @currency) ⇒ Object
Public: Store a payment method on a reference id for recurring/later use.
Does verify the users payment method, but does not create a charge.
:encrypted_card - The encrypted credit card information generated by the CSE (client side encryption) javascript integration. :reference - Your reference id for this transaction. :shopper - The hash describing the shopper for this contract:
:reference - Your reference id for this shopper/user. (mandatory)
:email - The shoppers email address. (optional but recommended)
:ip - The shoppers last known ip address. (optional but recommended)
:merchant_account - Use a specific merchant account for this transaction. (default: set by the instance or configuration default merchant account) :currency - Use a specific 3-letter currency code. (default: set by the instance or configuration default currency)
Returns an AdyenClient::Response or your specific response implementation.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/adyen_client.rb', line 129 def create_recurring_contract(encrypted_card:, reference:, shopper:, merchant_account: @merchant_account, currency: @currency) postJSON("/Payment/v12/authorise", reference: reference, additionalData: { "card.encrypted.json": encrypted_card }, amount: { value: 0, currency: currency }, merchantAccount: merchant_account, shopperEmail: shopper[:email], shopperIP: shopper[:ip], shopperReference: shopper[:reference], recurring: { contract: "RECURRING" } ) end |
#list_recurring_details(shopper_reference:, merchant_account: @merchant_account, contract: "RECURRING") ⇒ Object
Public: List the stored payment methods for a user.
:shopper_reference - The user reference id from your side. :merchant_account - Use a specific merchant account for this transaction. (default: set by the instance or configuration default merchant account) :currency - Use a specific 3-letter currency code. (default: set by the instance or configuration default currency)
Returns an AdyenClient::Response or your specific response implementation.
108 109 110 111 112 113 114 |
# File 'lib/adyen_client.rb', line 108 def list_recurring_details(shopper_reference:, merchant_account: @merchant_account, contract: "RECURRING") postJSON("/Recurring/v12/listRecurringDetails", shopperReference: shopper_reference, recurring: { contract: contract }, merchantAccount: merchant_account ) end |
#postJSON(path, data) ⇒ Object
Internal: Send a POST request to the Adyen API.
path - The Adyen JSON API endpoint path. data - The Hash describing the JSON body for this request.
Returns an AdyenClient::Response or your specific response implementation.
242 243 244 245 |
# File 'lib/adyen_client.rb', line 242 def postJSON(path, data) response = self.class.post(path, body: data.to_json) @response_class ? @response_class.parse(response) : response end |
#refund(original_reference:, amount:, reference:, merchantAccount: @merchant_account, currency: @currency) ⇒ Object
Public: Refunds a credit card transaction.
:original_reference - The psp_reference from Adyen for this transaction. :amount - The amount in cents to be refunded. :reference - Your reference id for this transaction. :merchant_account - Use a specific merchant account for this transaction (default: set by the instance or configuration default merchant account). :currency - Use a specific 3-letter currency code (default: set by the instance or configuration default currency).
Returns an AdyenClient::Response or your specific response implementation.
212 213 214 215 216 217 218 219 |
# File 'lib/adyen_client.rb', line 212 def refund(original_reference:, amount:, reference:, merchantAccount: @merchant_account, currency: @currency) postJSON("/Payment/v12/refund", reference: reference, merchantAccount: merchant_account, modificationAmount: { value: amount, currency: currency }, originalReference: original_reference ) end |
#verify(encrypted_card:, reference:, amount: 0, merchant_account: @merchant_account, currency: @currency, shopper: {}) ⇒ Object
Public: Verify a credit card (does not create a charge, but may be verified for a specified amount).
:encrypted_card - The encrypted credit card information generated by the CSE (client side encryption) javascript integration. :reference - Your reference id for this transaction. :amount - The integer amount in cents. Will not be charged on the card. (default: 0) :merchant_account - Use a specific merchant account for this transaction (default: set by the instance or configuration default merchant account). :currency - Use a specific 3-letter currency code (default: set by the instance or configuration default currency). :shopper - The hash describing the shopper for this transaction, optional but recommended (default: {}):
:email - The shoppers email address (optional but recommended).
:ip - The shoppers last known ip address (optional but recommended).
:reference - Your reference id for this shopper/user (optional).
Returns an AdyenClient::Response or your specific response implementation.
178 179 180 181 182 183 184 185 186 |
# File 'lib/adyen_client.rb', line 178 def verify(encrypted_card:, reference:, amount: 0, merchant_account: @merchant_account, currency: @currency, shopper: {}) postJSON("/Payment/v12/authorise", reference: reference, amount: { value: 0, currency: currency }, additionalAmount: { value: amount, currency: currency }, merchantAccount: merchant_account, additionalData: { "card.encrypted.json": encrypted_card } ) end |