spree_adyen 0.11.0 → 6.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +16 -4
- data/README.md +9 -51
- data/Rakefile +7 -18
- data/app/jobs/spree_adyen/base_job.rb +1 -1
- data/app/jobs/spree_adyen/configure_job.rb +19 -0
- data/app/maintenance_tasks/spree_adyen/maintenance_tasks/migrate_payments.rb +129 -0
- data/app/models/spree/payment_sessions/adyen.rb +42 -35
- data/app/models/spree/payment_setup_sessions/adyen.rb +1 -25
- data/app/models/spree_adyen/amounts.rb +69 -0
- data/app/models/spree_adyen/gateway/payment_sessions.rb +228 -98
- data/app/models/spree_adyen/gateway/payment_setup_sessions.rb +36 -136
- data/app/models/spree_adyen/gateway/webhooks.rb +142 -0
- data/app/models/spree_adyen/gateway.rb +275 -399
- data/app/models/spree_adyen/payment_sources/ach_direct_debit.rb +1 -13
- data/app/models/spree_adyen/payment_sources/affirm.rb +1 -13
- data/app/models/spree_adyen/payment_sources/afterpay.rb +1 -13
- data/app/models/spree_adyen/payment_sources/alipay.rb +1 -15
- data/app/models/spree_adyen/payment_sources/alipay_hk.rb +1 -13
- data/app/models/spree_adyen/payment_sources/alma.rb +1 -13
- data/app/models/spree_adyen/payment_sources/ancv.rb +1 -13
- data/app/models/spree_adyen/payment_sources/atome.rb +1 -13
- data/app/models/spree_adyen/payment_sources/bacs.rb +1 -13
- data/app/models/spree_adyen/payment_sources/bancontact.rb +1 -15
- data/app/models/spree_adyen/payment_sources/bank_transfer.rb +1 -13
- data/app/models/spree_adyen/payment_sources/base.rb +9 -1
- data/app/models/spree_adyen/payment_sources/benefit.rb +1 -13
- data/app/models/spree_adyen/payment_sources/billie.rb +1 -13
- data/app/models/spree_adyen/payment_sources/bizum.rb +1 -13
- data/app/models/spree_adyen/payment_sources/blik.rb +1 -11
- data/app/models/spree_adyen/payment_sources/boleto.rb +1 -13
- data/app/models/spree_adyen/payment_sources/card.rb +14 -0
- data/app/models/spree_adyen/payment_sources/cash_app_afterpay.rb +1 -13
- data/app/models/spree_adyen/payment_sources/cashapp.rb +1 -13
- data/app/models/spree_adyen/payment_sources/clearpay.rb +1 -13
- data/app/models/spree_adyen/payment_sources/dana.rb +1 -13
- data/app/models/spree_adyen/payment_sources/doku.rb +1 -15
- data/app/models/spree_adyen/payment_sources/duitnow.rb +1 -13
- data/app/models/spree_adyen/payment_sources/eps.rb +1 -15
- data/app/models/spree_adyen/payment_sources/fastlane.rb +1 -15
- data/app/models/spree_adyen/payment_sources/fpx.rb +1 -15
- data/app/models/spree_adyen/payment_sources/gcash.rb +1 -15
- data/app/models/spree_adyen/payment_sources/gift_cards.rb +1 -15
- data/app/models/spree_adyen/payment_sources/giropay.rb +1 -15
- data/app/models/spree_adyen/payment_sources/grabpay.rb +1 -15
- data/app/models/spree_adyen/payment_sources/ideal.rb +1 -11
- data/app/models/spree_adyen/payment_sources/klarna.rb +1 -28
- data/app/models/spree_adyen/payment_sources/oney.rb +1 -9
- data/app/models/spree_adyen/payment_sources/online_banking_czech_republic.rb +1 -11
- data/app/models/spree_adyen/payment_sources/online_banking_poland.rb +1 -11
- data/app/models/spree_adyen/payment_sources/pay_by_bank.rb +1 -11
- data/app/models/spree_adyen/payment_sources/paypal.rb +1 -11
- data/app/models/spree_adyen/payment_sources/paypo.rb +1 -11
- data/app/models/spree_adyen/payment_sources/paysafecard.rb +1 -11
- data/app/models/spree_adyen/payment_sources/rate_pay_direct_debit.rb +1 -11
- data/app/models/spree_adyen/payment_sources/riverty.rb +1 -11
- data/app/models/spree_adyen/payment_sources/samsung_pay.rb +1 -11
- data/app/models/spree_adyen/payment_sources/scalapay.rb +1 -9
- data/app/models/spree_adyen/payment_sources/sepa_direct_debit.rb +1 -11
- data/app/models/spree_adyen/payment_sources/trustly.rb +1 -11
- data/app/models/spree_adyen/payment_sources/unknown.rb +1 -15
- data/app/models/spree_adyen/payment_sources/wechat_pay.rb +1 -11
- data/app/models/spree_adyen/result.rb +155 -0
- data/app/models/spree_adyen/variants.rb +230 -0
- data/app/presenters/spree_adyen/application_info_presenter.rb +4 -4
- data/app/presenters/spree_adyen/session_presenter.rb +153 -0
- data/app/services/spree_adyen/configure.rb +121 -0
- data/app/subscribers/spree_adyen/capture_held_payments_subscriber.rb +41 -0
- data/config/locales/en.yml +21 -6
- data/lib/generators/spree_adyen/install/install_generator.rb +12 -12
- data/lib/spree_adyen/engine.rb +6 -70
- data/lib/spree_adyen/factories.rb +1 -3
- data/lib/spree_adyen/testing_support/factories/gateway_factory.rb +10 -17
- data/lib/spree_adyen/testing_support/factories/payment_session_factory.rb +18 -34
- data/lib/spree_adyen.rb +5 -30
- data/lib/tasks/configure_adyen_gateways.rake +39 -0
- metadata +35 -177
- data/app/assets/config/spree_adyen_manifest.js +0 -5
- data/app/controllers/spree_adyen/apple_pay_domain_verification_controller.rb +0 -11
- data/app/controllers/spree_adyen/payment_sessions_controller.rb +0 -67
- data/app/controllers/spree_adyen/store_controller_decorator.rb +0 -9
- data/app/controllers/spree_adyen/webhooks_controller.rb +0 -46
- data/app/helpers/spree_adyen/base_helper.rb +0 -18
- data/app/javascript/spree_adyen/application.js +0 -17
- data/app/javascript/spree_adyen/controllers/checkout_adyen_controller.js +0 -80
- data/app/javascript/spree_adyen/controllers/redirect_adyen_controller.js +0 -35
- data/app/jobs/spree_adyen/add_allowed_origin_job.rb +0 -24
- data/app/jobs/spree_adyen/webhooks/process_authorisation_event_job.rb +0 -10
- data/app/jobs/spree_adyen/webhooks/process_cancellation_event_job.rb +0 -10
- data/app/jobs/spree_adyen/webhooks/process_capture_event_job.rb +0 -10
- data/app/models/spree_adyen/base.rb +0 -6
- data/app/models/spree_adyen/custom_domain_decorator.rb +0 -17
- data/app/models/spree_adyen/order_decorator.rb +0 -23
- data/app/models/spree_adyen/payment_decorator.rb +0 -66
- data/app/models/spree_adyen/payment_method_decorator.rb +0 -15
- data/app/models/spree_adyen/payment_session.rb +0 -121
- data/app/models/spree_adyen/payment_sources/apple_pay.rb +0 -19
- data/app/models/spree_adyen/payment_sources/bcmc.rb +0 -17
- data/app/models/spree_adyen/payment_sources/bcmc_mobile.rb +0 -17
- data/app/models/spree_adyen/payment_sources/ebanking_fi.rb +0 -19
- data/app/models/spree_adyen/payment_sources/eft_directdebit_ca.rb +0 -19
- data/app/models/spree_adyen/payment_sources/eftpos_australia.rb +0 -19
- data/app/models/spree_adyen/payment_sources/elo.rb +0 -19
- data/app/models/spree_adyen/payment_sources/givex.rb +0 -19
- data/app/models/spree_adyen/payment_sources/gopay_wallet.rb +0 -19
- data/app/models/spree_adyen/payment_sources/grabpay_paylater.rb +0 -19
- data/app/models/spree_adyen/payment_sources/hipercard.rb +0 -19
- data/app/models/spree_adyen/payment_sources/kakaopay.rb +0 -19
- data/app/models/spree_adyen/payment_sources/kcp_naverpay.rb +0 -19
- data/app/models/spree_adyen/store_decorator.rb +0 -17
- data/app/presenters/spree_adyen/address_presenter.rb +0 -21
- data/app/presenters/spree_adyen/cancel_payload_presenter.rb +0 -31
- data/app/presenters/spree_adyen/capture_payload_presenter.rb +0 -36
- data/app/presenters/spree_adyen/checkout_presenter.rb +0 -51
- data/app/presenters/spree_adyen/payment_sessions/request_payload_presenter.rb +0 -140
- data/app/presenters/spree_adyen/payment_setup_sessions/request_payload_presenter.rb +0 -81
- data/app/presenters/spree_adyen/payments/request_payload_presenter.rb +0 -85
- data/app/presenters/spree_adyen/refund_payload_presenter.rb +0 -38
- data/app/presenters/spree_adyen/webhook_payload_presenter.rb +0 -28
- data/app/presenters/spree_adyen/webhooks/credit_card_presenter.rb +0 -43
- data/app/services/spree_adyen/gateways/add_allowed_origin.rb +0 -36
- data/app/services/spree_adyen/gateways/configuration.rb +0 -41
- data/app/services/spree_adyen/gateways/configure.rb +0 -60
- data/app/services/spree_adyen/payment_sessions/find_or_create.rb +0 -51
- data/app/services/spree_adyen/payment_sessions/process_with_result.rb +0 -49
- data/app/services/spree_adyen/payment_setup_sessions/create_source_from_result.rb +0 -71
- data/app/services/spree_adyen/payment_setup_sessions/handle_authorisation.rb +0 -48
- data/app/services/spree_adyen/webhooks/actions/create_source.rb +0 -112
- data/app/services/spree_adyen/webhooks/actions/find_or_create_credit_card.rb +0 -33
- data/app/services/spree_adyen/webhooks/event.rb +0 -104
- data/app/services/spree_adyen/webhooks/event_processors/authorisation_event_processor.rb +0 -69
- data/app/services/spree_adyen/webhooks/event_processors/cancellation_event_processor.rb +0 -49
- data/app/services/spree_adyen/webhooks/event_processors/capture_event_processor.rb +0 -48
- data/app/services/spree_adyen/webhooks/handle_event.rb +0 -43
- data/app/services/spree_adyen/webhooks/standard_hmac_validator.rb +0 -37
- data/app/views/layouts/spree_adyen/default.html.erb +0 -14
- data/app/views/spree/admin/payment_methods/configuration_guides/_spree_adyen.html.erb +0 -14
- data/app/views/spree/admin/payment_methods/custom_form_fields/_spree_adyen.html.erb +0 -18
- data/app/views/spree/admin/payment_methods/descriptions/_spree_adyen.html.erb +0 -12
- data/app/views/spree/admin/payments/source_forms/_spree_adyen.html.erb +0 -7
- data/app/views/spree/checkout/payment/_spree_adyen.html.erb +0 -40
- data/app/views/spree_adyen/_drop_in.html.erb +0 -12
- data/app/views/spree_adyen/_head.html.erb +0 -2
- data/app/views/spree_adyen/payment_sessions/redirect.html.erb +0 -5
- data/config/importmap.rb +0 -7
- data/config/initializers/spree.rb +0 -34
- data/config/initializers/spree_permitted_attributes.rb +0 -5
- data/config/routes.rb +0 -35
- data/db/migrate/20250630150000_setup_spree_adyen_models.rb +0 -17
- data/db/migrate/20250811140113_add_channel_to_adyen_payment_sessions.rb +0 -10
- data/db/migrate/20250813152608_add_return_url_to_spree_adyen_payment_sessions.rb +0 -15
- data/lib/spree_adyen/configuration.rb +0 -42
- data/lib/spree_adyen/testing_support/factories/payment_setup_session_factory.rb +0 -15
- data/lib/spree_adyen/version.rb +0 -7
- data/lib/spree_api_v2/spree/api/v2/storefront/adyen/base_controller.rb +0 -21
- data/lib/spree_api_v2/spree/api/v2/storefront/adyen/payment_sessions_controller.rb +0 -72
- data/lib/spree_api_v2/spree_adyen/api/v2/storefront/payment_session_serializer.rb +0 -21
- data/vendor/javascript/@adyen--adyen-web.js +0 -4
- data/vendor/stylesheets/adyen.css +0 -106
|
@@ -1,495 +1,371 @@
|
|
|
1
1
|
module SpreeAdyen
|
|
2
|
+
# Adyen gateway built on the payment session API: Adyen's Sessions flow with
|
|
3
|
+
# Drop-in. A session payment's response code is always its Adyen session id,
|
|
4
|
+
# the key core links a session to its payment by; the PSP reference Adyen
|
|
5
|
+
# needs for a capture, cancel or refund lives in the payment's metadata.
|
|
2
6
|
class Gateway < ::Spree::Gateway
|
|
3
|
-
|
|
4
|
-
include PaymentSetupSessions
|
|
7
|
+
self.accepts_money_amounts = true
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
#
|
|
10
|
-
# Attributes
|
|
11
|
-
#
|
|
12
|
-
attribute :skip_auto_configuration, :boolean, default: false
|
|
13
|
-
attribute :skip_api_key_validation, :boolean, default: false
|
|
9
|
+
include SpreeAdyen::Gateway::PaymentSessions
|
|
10
|
+
include SpreeAdyen::Gateway::PaymentSetupSessions
|
|
11
|
+
include SpreeAdyen::Gateway::Webhooks
|
|
14
12
|
|
|
13
|
+
# Typed by the operator.
|
|
15
14
|
preference :api_key, :password
|
|
16
15
|
preference :merchant_account, :string
|
|
17
|
-
preference :client_key, :password
|
|
18
|
-
preference :hmac_key, :password
|
|
19
16
|
preference :test_mode, :boolean, default: true
|
|
20
|
-
|
|
17
|
+
# Adyen's live Checkout endpoints need it; the Management API does not.
|
|
21
18
|
preference :live_url_prefix, :string
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
# Written by configuration after save, never by an operator.
|
|
21
|
+
preference :client_key, :password, internal: true
|
|
22
|
+
preference :hmac_key, :password, internal: true
|
|
23
|
+
preference :previous_hmac_key, :password, internal: true
|
|
24
|
+
preference :webhook_id, :string, internal: true
|
|
25
|
+
|
|
26
|
+
# Explicit, instead of the library's 30 and 60 seconds; the read matches
|
|
27
|
+
# PayPal's gateway.
|
|
28
|
+
OPEN_TIMEOUT = 5
|
|
29
|
+
READ_TIMEOUT = 30
|
|
30
|
+
# A call that times out or gets a 5xx is retried this many times, with the
|
|
31
|
+
# same idempotency key.
|
|
32
|
+
RETRIES = 2
|
|
33
|
+
CONNECTION_ERRORS = [Faraday::TimeoutError, Faraday::ConnectionFailed].freeze
|
|
34
|
+
CREDENTIAL_PREFERENCES = %w[api_key merchant_account test_mode live_url_prefix].freeze
|
|
35
|
+
|
|
36
|
+
validates :preferred_api_key, :preferred_merchant_account, presence: true
|
|
30
37
|
validates :preferred_live_url_prefix, presence: true, unless: :preferred_test_mode
|
|
31
|
-
validate :
|
|
32
|
-
|
|
33
|
-
#
|
|
34
|
-
# Callbacks
|
|
35
|
-
#
|
|
36
|
-
after_commit :configure, if: :preferred_api_key_previously_changed?, unless: :skip_auto_configuration
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
# Associations
|
|
40
|
-
#
|
|
41
|
-
has_many :payment_sessions, class_name: 'SpreeAdyen::PaymentSession',
|
|
42
|
-
foreign_key: 'payment_method_id',
|
|
43
|
-
dependent: :delete_all,
|
|
44
|
-
inverse_of: :payment_method
|
|
45
|
-
|
|
46
|
-
# @param amount_in_cents [Integer] the amount in cents to capture
|
|
47
|
-
# @param payment_source [Spree::CreditCard | Spree::PaymentSource]
|
|
48
|
-
# @param gateway_options [Hash] this is an instance of Spree::Payment::GatewayOptions.to_hash
|
|
49
|
-
def purchase(amount_in_cents, payment_source, gateway_options = {})
|
|
50
|
-
handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# @param amount_in_cents [Integer] the amount in cents to capture
|
|
54
|
-
# @param payment_source [Spree::CreditCard | Spree::PaymentSource]
|
|
55
|
-
# @param gateway_options [Hash] this is an instance of Spree::Payment::GatewayOptions.to_hash
|
|
56
|
-
def authorize(amount_in_cents, payment_source, gateway_options = {})
|
|
57
|
-
handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options = {})
|
|
61
|
-
payload = SpreeAdyen::Payments::RequestPayloadPresenter.new(
|
|
62
|
-
source: payment_source,
|
|
63
|
-
amount_in_cents: amount_in_cents,
|
|
64
|
-
manual_capture: !auto_capture?,
|
|
65
|
-
gateway_options: gateway_options
|
|
66
|
-
).to_h
|
|
67
|
-
|
|
68
|
-
response = send_request do
|
|
69
|
-
client.checkout.payments_api.payments(payload, headers: { 'Idempotency-Key' => SecureRandom.uuid })
|
|
70
|
-
end
|
|
71
|
-
response_body = response.response
|
|
38
|
+
validate :validate_credential, unless: -> { Rails.env.test? }, if: :credentials_changed?
|
|
39
|
+
after_commit :configure_async, on: %i[create update]
|
|
72
40
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
else
|
|
76
|
-
failure(response_body.slice('pspReference', 'message').values.join(' - '))
|
|
77
|
-
end
|
|
41
|
+
def self.logo_url
|
|
42
|
+
'https://spreecommerce.org/docs/images/integrations/logos/adyen.png'
|
|
78
43
|
end
|
|
79
44
|
|
|
80
|
-
def
|
|
81
|
-
|
|
82
|
-
payment ||= Spree::Payment.find_by(response_code: id)
|
|
83
|
-
if payment.completed?
|
|
84
|
-
amount = payment.credit_allowed
|
|
85
|
-
return success(transaction_id, {}) if amount.zero?
|
|
86
|
-
# Don't create a refund if the payment is for a shipment, we will create a refund for the whole shipping cost instead
|
|
87
|
-
return success(transaction_id, {}) if payment.respond_to?(:for_shipment?) && payment.for_shipment?
|
|
88
|
-
|
|
89
|
-
refund = payment.refunds.create!(
|
|
90
|
-
amount: amount,
|
|
91
|
-
reason: Spree::RefundReason.order_canceled_reason,
|
|
92
|
-
refunder_id: payment.order.canceler_id
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
# Spree::Refund#response has the response from the `credit` action
|
|
96
|
-
# For the authorization ID we need to use the payment.response_code
|
|
97
|
-
# Otherwise we'll overwrite the payment authorization with the refund ID
|
|
98
|
-
success(transaction_id, refund.response.params)
|
|
99
|
-
else
|
|
100
|
-
payment.void!
|
|
101
|
-
success(transaction_id, {})
|
|
102
|
-
end
|
|
45
|
+
def self.docs_url
|
|
46
|
+
'https://spreecommerce.org/docs/integrations/payments/adyen'
|
|
103
47
|
end
|
|
104
48
|
|
|
105
|
-
def
|
|
106
|
-
|
|
107
|
-
payment = refund.present? ? refund.payment : Spree::Payment.find_by(response_code: payment_id)
|
|
108
|
-
|
|
109
|
-
return failure("#{payment_id} - Payment not found") unless payment
|
|
110
|
-
|
|
111
|
-
payload = SpreeAdyen::RefundPayloadPresenter.new(
|
|
112
|
-
payment: payment,
|
|
113
|
-
amount_in_cents: amount_in_cents,
|
|
114
|
-
payment_method: self,
|
|
115
|
-
currency: payment.currency,
|
|
116
|
-
refund: refund
|
|
117
|
-
).to_h
|
|
118
|
-
|
|
119
|
-
response = send_request do
|
|
120
|
-
client.checkout.modifications_api.refund_captured_payment(payload, payment.transaction_id, headers: { 'Idempotency-Key' => SecureRandom.uuid })
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
if response.status.to_i == 201
|
|
124
|
-
success(response.response['pspReference'], response)
|
|
125
|
-
else
|
|
126
|
-
failure(response.response.slice('pspReference', 'message').values.join(' - '))
|
|
127
|
-
end
|
|
49
|
+
def provider_class
|
|
50
|
+
self.class
|
|
128
51
|
end
|
|
129
52
|
|
|
130
|
-
def
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return failure("#{response_code} - Payment not found") if payment.blank?
|
|
134
|
-
return failure("#{response_code} - Payment is already captured") if payment.completed?
|
|
135
|
-
|
|
136
|
-
payload = SpreeAdyen::CapturePayloadPresenter.new(
|
|
137
|
-
amount_in_cents: amount_in_cents,
|
|
138
|
-
payment: payment,
|
|
139
|
-
payment_method: self
|
|
140
|
-
).to_h
|
|
141
|
-
|
|
142
|
-
response = send_request do
|
|
143
|
-
client.checkout.modifications_api.capture_authorised_payment(
|
|
144
|
-
payload,
|
|
145
|
-
payment.response_code,
|
|
146
|
-
headers: { 'Idempotency-Key' => SecureRandom.uuid }
|
|
147
|
-
)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
if response.status.to_i == 201
|
|
151
|
-
success(response.response['paymentPspReference'], response)
|
|
152
|
-
else
|
|
153
|
-
failure(response.response.slice('paymentPspReference', 'message').values.join(' - '))
|
|
154
|
-
end
|
|
53
|
+
def default_name
|
|
54
|
+
'Adyen'
|
|
155
55
|
end
|
|
156
56
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def capture(amount_in_cents, response_code, _gateway_options = {})
|
|
160
|
-
payment = Spree::Payment.find_by(response_code: response_code)
|
|
161
|
-
|
|
162
|
-
return failure("#{response_code} - Payment not found") if payment.blank?
|
|
163
|
-
return failure("#{response_code} - Payment is already captured") if payment.completed?
|
|
164
|
-
return failure("#{response_code} - Capture PSP reference not found") unless payment.has_metafield?(CAPTURE_PSP_REFERENCE_METAFIELD_KEY)
|
|
165
|
-
|
|
166
|
-
success(payment.response_code, {})
|
|
57
|
+
def method_type
|
|
58
|
+
'spree_adyen'
|
|
167
59
|
end
|
|
168
60
|
|
|
169
|
-
def
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return failure("#{response_code} - Payment not found") if payment.blank?
|
|
173
|
-
return failure("#{response_code} - Payment is already void") if payment.void?
|
|
174
|
-
|
|
175
|
-
payload = SpreeAdyen::CancelPayloadPresenter.new(
|
|
176
|
-
payment: payment,
|
|
177
|
-
payment_method: self
|
|
178
|
-
).to_h
|
|
179
|
-
|
|
180
|
-
response = send_request do
|
|
181
|
-
client.checkout.modifications_api.cancel_authorised_payment_by_psp_reference(
|
|
182
|
-
payload,
|
|
183
|
-
payment.response_code,
|
|
184
|
-
headers: { 'Idempotency-Key' => SecureRandom.uuid }
|
|
185
|
-
)
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
if response.status.to_i == 201
|
|
189
|
-
success(response.response['paymentPspReference'], response)
|
|
190
|
-
else
|
|
191
|
-
failure(response.response.slice('paymentPspReference', 'message').values.join(' - '))
|
|
192
|
-
end
|
|
61
|
+
def payment_icon_name
|
|
62
|
+
'adyen'
|
|
193
63
|
end
|
|
194
64
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
def void(response_code, _source, _gateway_options)
|
|
198
|
-
payment = Spree::Payment.find_by(response_code: response_code)
|
|
199
|
-
|
|
200
|
-
return failure("#{response_code} - Payment not found") if payment.blank?
|
|
201
|
-
return failure("#{response_code} - Payment is already void") if payment.void?
|
|
202
|
-
return failure("#{response_code} - Cancellation PSP reference not found") unless payment.has_metafield?(CANCELLATION_PSP_REFERENCE_METAFIELD_KEY)
|
|
203
|
-
|
|
204
|
-
success(payment.response_code, {})
|
|
65
|
+
def session_required?
|
|
66
|
+
true
|
|
205
67
|
end
|
|
206
68
|
|
|
207
|
-
def
|
|
208
|
-
|
|
209
|
-
return nil unless store
|
|
210
|
-
|
|
211
|
-
if SpreeAdyen::Config[:use_legacy_webhook_handlers]
|
|
212
|
-
"#{store.formatted_url}/adyen/webhooks"
|
|
213
|
-
else
|
|
214
|
-
"#{store.formatted_url}/api/v3/webhooks/payments/#{prefixed_id}"
|
|
215
|
-
end
|
|
69
|
+
def payment_session_class
|
|
70
|
+
Spree::PaymentSessions::Adyen
|
|
216
71
|
end
|
|
217
72
|
|
|
218
|
-
def
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
# Verify HMAC signature
|
|
223
|
-
webhook_request_item = payload.dig('notificationItems', 0, 'NotificationRequestItem') || {}
|
|
224
|
-
unless valid_hmac?(webhook_request_item)
|
|
225
|
-
raise Spree::PaymentMethod::WebhookSignatureError, 'Invalid HMAC signature'
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
return nil if event.session_id.blank?
|
|
73
|
+
def payment_setup_session_class
|
|
74
|
+
Spree::PaymentSetupSessions::Adyen
|
|
75
|
+
end
|
|
229
76
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return nil
|
|
236
|
-
end
|
|
77
|
+
# Cards are saved under the customer's shopper reference, and core passes
|
|
78
|
+
# the source to void and credit.
|
|
79
|
+
def payment_profiles_supported?
|
|
80
|
+
true
|
|
81
|
+
end
|
|
237
82
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
case event.code
|
|
242
|
-
when 'AUTHORISATION'
|
|
243
|
-
action = event.success? ? :authorized : :failed
|
|
244
|
-
{ action: action, payment_session: payment_session, metadata: { adyen_event: event } }
|
|
245
|
-
when 'CAPTURE'
|
|
246
|
-
{ action: :captured, payment_session: payment_session, metadata: { adyen_event: event } }
|
|
247
|
-
when 'CANCELLATION'
|
|
248
|
-
{ action: :canceled, payment_session: payment_session, metadata: { adyen_event: event } }
|
|
249
|
-
else
|
|
250
|
-
nil
|
|
251
|
-
end
|
|
83
|
+
# Not offered for a currency missing from Adyen's currency table.
|
|
84
|
+
def available_for_order?(order)
|
|
85
|
+
super && SpreeAdyen::Amounts.supported?(order.currency)
|
|
252
86
|
end
|
|
253
87
|
|
|
254
|
-
|
|
255
|
-
|
|
88
|
+
# Adyen's Customer Area page for the payment's PSP reference.
|
|
89
|
+
def gateway_dashboard_payment_url(payment)
|
|
90
|
+
psp_reference = payment.metadata['adyen_psp_reference'].presence
|
|
91
|
+
return if psp_reference.nil?
|
|
92
|
+
|
|
93
|
+
"https://ca-#{environment}.adyen.com/ca/ca/accounts/showTx.shtml?pspReference=#{psp_reference}&txType=Payment"
|
|
256
94
|
end
|
|
257
95
|
|
|
96
|
+
# @return [String] 'test' or 'live'
|
|
258
97
|
def environment
|
|
259
|
-
|
|
260
|
-
:test
|
|
261
|
-
else
|
|
262
|
-
:live
|
|
263
|
-
end
|
|
98
|
+
preferred_test_mode ? 'test' : 'live'
|
|
264
99
|
end
|
|
265
100
|
|
|
266
|
-
|
|
101
|
+
# Called by core when a session-created payment moves on. Adyen already
|
|
102
|
+
# holds the authorisation, so this makes no call.
|
|
103
|
+
#
|
|
104
|
+
# @return [Spree::PaymentResponse] authorized by the session id
|
|
105
|
+
def authorize(_money, _source, gateway_options = {})
|
|
106
|
+
payment = payment_from_options(gateway_options)
|
|
107
|
+
raise Spree::Core::GatewayError, I18n.t('spree.adyen.errors.session_required') if payment&.payment_session.nil?
|
|
267
108
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
client.checkout.payments_api.get_result_of_payment_session(payment_session_id, query_params: { sessionResult: session_result })
|
|
271
|
-
end
|
|
272
|
-
response_body = response.response
|
|
109
|
+
success(payment.response_code)
|
|
110
|
+
end
|
|
273
111
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
failure(response_body.slice('pspReference', 'message').values.join(' - '))
|
|
278
|
-
end
|
|
112
|
+
# @see #authorize
|
|
113
|
+
def purchase(money, source, gateway_options = {})
|
|
114
|
+
authorize(money, source, gateway_options)
|
|
279
115
|
end
|
|
280
116
|
|
|
281
|
-
#
|
|
282
|
-
#
|
|
117
|
+
# Requests a capture of the payment's PSP reference. Adyen answers by
|
|
118
|
+
# webhook, so the payment waits until it does.
|
|
283
119
|
#
|
|
284
|
-
# @param
|
|
285
|
-
# @param
|
|
286
|
-
# @
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
user: order.user,
|
|
294
|
-
merchant_account: preferred_merchant_account,
|
|
295
|
-
payment_method: self,
|
|
296
|
-
channel: channel,
|
|
297
|
-
return_url: return_url
|
|
298
|
-
).to_h
|
|
299
|
-
|
|
300
|
-
response = send_request do
|
|
301
|
-
client.checkout.payments_api.sessions(payload, headers: { 'Idempotency-Key' => SecureRandom.uuid })
|
|
120
|
+
# @param money [Spree::Money]
|
|
121
|
+
# @param session_id [String] the payment's response code
|
|
122
|
+
# @return [Spree::PaymentResponse] requested, with the capture's own PSP reference
|
|
123
|
+
def capture(money, session_id, _gateway_options = {})
|
|
124
|
+
payment = payment_for(session_id)
|
|
125
|
+
psp_reference = psp_reference!(payment)
|
|
126
|
+
body = { merchantAccount: preferred_merchant_account, amount: adyen_amount(money) }
|
|
127
|
+
result = money_moving_call(payment, 'capture', payment.capture_events.count) do |client, headers, reference|
|
|
128
|
+
client.checkout.modifications_api.capture_authorised_payment(body.merge(reference: reference), psp_reference, headers: headers)
|
|
302
129
|
end
|
|
303
|
-
response_body = response.response
|
|
304
130
|
|
|
305
|
-
|
|
306
|
-
success(response_body.id, response_body)
|
|
307
|
-
else
|
|
308
|
-
failure(response_body.slice('pspReference', 'message').values.join(' - '))
|
|
309
|
-
end
|
|
131
|
+
success(session_id, requested: true, reference: result['pspReference'])
|
|
310
132
|
end
|
|
311
133
|
|
|
312
|
-
#
|
|
313
|
-
#
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
134
|
+
# Requests a cancel of the payment's authorisation, recording the cancel's
|
|
135
|
+
# PSP reference so the next attempt gets a new idempotency key.
|
|
136
|
+
#
|
|
137
|
+
# @return [Spree::PaymentResponse] requested
|
|
138
|
+
def void(session_id, _source = nil, _gateway_options = {})
|
|
139
|
+
payment = payment_for(session_id)
|
|
140
|
+
psp_reference = psp_reference!(payment)
|
|
141
|
+
references = Array(payment.metadata['adyen_cancel_references'])
|
|
142
|
+
result = money_moving_call(payment, 'cancel', references.size) do |client, headers, reference|
|
|
143
|
+
client.checkout.modifications_api.cancel_authorised_payment_by_psp_reference(
|
|
144
|
+
{ merchantAccount: preferred_merchant_account, reference: reference }, psp_reference, headers: headers
|
|
145
|
+
)
|
|
146
|
+
end
|
|
317
147
|
|
|
318
|
-
|
|
319
|
-
|
|
148
|
+
payment.update_columns(metadata: payment.metadata.merge('adyen_cancel_references' => references + [result['pspReference']]))
|
|
149
|
+
success(session_id, requested: true)
|
|
320
150
|
end
|
|
321
151
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
152
|
+
# Requests a refund of the payment's PSP reference, keyed on the refund
|
|
153
|
+
# rows the payment had before this one.
|
|
154
|
+
#
|
|
155
|
+
# @param options [Hash] carries the Spree::Refund as +originator+
|
|
156
|
+
# @return [Spree::PaymentResponse] requested, authorized by the refund's PSP reference
|
|
157
|
+
def credit(money, _source, session_id, options = {})
|
|
158
|
+
payment = payment_for(session_id)
|
|
159
|
+
refund = options[:originator]
|
|
160
|
+
earlier_refunds = refund.is_a?(Spree::Refund) ? payment.refunds.where(id: ...refund.id).count : payment.refunds.count
|
|
161
|
+
psp_reference = psp_reference!(payment)
|
|
162
|
+
body = { merchantAccount: preferred_merchant_account, amount: adyen_amount(money) }
|
|
163
|
+
result = money_moving_call(payment, 'refund', earlier_refunds) do |client, headers, reference|
|
|
164
|
+
client.checkout.modifications_api.refund_captured_payment(body.merge(reference: reference), psp_reference, headers: headers)
|
|
165
|
+
end
|
|
325
166
|
|
|
326
|
-
|
|
327
|
-
'adyen'
|
|
167
|
+
success(result['pspReference'], requested: true)
|
|
328
168
|
end
|
|
329
169
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
170
|
+
# A completed payment is refunded, unless the operator asked to keep the
|
|
171
|
+
# money; anything else is voided.
|
|
172
|
+
def cancel(session_id, payment = nil, refund: true)
|
|
173
|
+
return void(session_id) unless payment&.completed?
|
|
174
|
+
return success(session_id) unless refund
|
|
333
175
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
176
|
+
amount = payment.credit_allowed
|
|
177
|
+
return success(session_id) if amount.zero?
|
|
178
|
+
return success(session_id) if payment.respond_to?(:for_shipment?) && payment.for_shipment?
|
|
337
179
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
180
|
+
# Store passed explicitly: cancellation runs from jobs and webhooks where
|
|
181
|
+
# Spree::Current.store is nil.
|
|
182
|
+
result = Spree.refund_create_workflow.call(
|
|
183
|
+
payment: payment,
|
|
184
|
+
reason: Spree::RefundReason.order_canceled_reason(payment.owner.store),
|
|
185
|
+
refunder: payment.order&.canceler
|
|
186
|
+
)
|
|
187
|
+
raise Spree::Core::GatewayError, result.error.value.to_s if result.failure?
|
|
344
188
|
|
|
345
|
-
|
|
189
|
+
success(session_id, requested: true)
|
|
346
190
|
end
|
|
347
191
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
elsif order.user.present?
|
|
352
|
-
credit_cards.where(user_id: order.user_id)
|
|
353
|
-
else
|
|
354
|
-
[]
|
|
355
|
-
end
|
|
192
|
+
# A captured payment is refunded instead.
|
|
193
|
+
def can_void?(payment)
|
|
194
|
+
!payment.completed?
|
|
356
195
|
end
|
|
357
196
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
197
|
+
# What is wrong with the credential, in words the operator can act on, or
|
|
198
|
+
# nil when it reaches the merchant account with the roles the gem needs.
|
|
199
|
+
#
|
|
200
|
+
# @return [String, nil]
|
|
201
|
+
def credential_problem
|
|
202
|
+
merchants = adyen_call(:list_merchants) do |client|
|
|
203
|
+
client.management.account_merchant_level_api.list_merchant_accounts(query_params: { pageSize: 100 })
|
|
365
204
|
end
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
failure(parse_adyen_error_response(e)['message'])
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
def add_allowed_origin(domain)
|
|
373
|
-
response = client.management.my_api_credential_api.add_allowed_origin({ domain: domain })
|
|
374
|
-
|
|
375
|
-
if response.status.to_i == 200
|
|
376
|
-
success(response.response.id, response.response)
|
|
377
|
-
else
|
|
378
|
-
failure(response.response)
|
|
205
|
+
reachable = Array(merchants['data']).pluck('id')
|
|
206
|
+
unless reachable.include?(preferred_merchant_account)
|
|
207
|
+
return I18n.t('spree.adyen.errors.merchant_account_unreachable', merchant_account: preferred_merchant_account)
|
|
379
208
|
end
|
|
380
|
-
rescue Adyen::AdyenError => e
|
|
381
|
-
failure(parse_adyen_error_response(e))
|
|
382
|
-
end
|
|
383
209
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
response = client.management.webhooks_merchant_level_api.set_up_webhook(payload, preferred_merchant_account)
|
|
387
|
-
|
|
388
|
-
if response.status.to_i == 200
|
|
389
|
-
success(response.response.id, response.response)
|
|
390
|
-
else
|
|
391
|
-
failure(response.response)
|
|
210
|
+
adyen_call(:list_webhooks) do |client|
|
|
211
|
+
client.management.webhooks_merchant_level_api.list_all_webhooks(preferred_merchant_account, query_params: { pageSize: 1 })
|
|
392
212
|
end
|
|
393
|
-
|
|
394
|
-
|
|
213
|
+
nil
|
|
214
|
+
rescue CredentialRejected
|
|
215
|
+
I18n.t('spree.adyen.errors.invalid_api_key')
|
|
216
|
+
rescue RoleMissing
|
|
217
|
+
I18n.t('spree.adyen.errors.role_missing')
|
|
395
218
|
end
|
|
396
219
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
220
|
+
# Adyen refused the API key.
|
|
221
|
+
class CredentialRejected < Spree::Core::GatewayError; end
|
|
222
|
+
# The API key lacks a role a call needs.
|
|
223
|
+
class RoleMissing < Spree::Core::GatewayError; end
|
|
400
224
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
225
|
+
# Calls Adyen through the library, retrying a timeout or a 5xx with the same
|
|
226
|
+
# idempotency key. Logs status lines only: bodies carry names, emails and
|
|
227
|
+
# addresses.
|
|
228
|
+
#
|
|
229
|
+
# @param operation [Symbol] named in the log line
|
|
230
|
+
# @param money_moving [Boolean] a call still unanswered after its retries
|
|
231
|
+
# raises AmbiguousGatewayError, since Adyen may have acted on it
|
|
232
|
+
# @yieldparam client [Adyen::Client]
|
|
233
|
+
# @return [Hash] Adyen's answer, string keys
|
|
234
|
+
def adyen_call(operation, money_moving: false)
|
|
235
|
+
attempt = 0
|
|
236
|
+
begin
|
|
237
|
+
attempt += 1
|
|
238
|
+
result = yield(adyen_client)
|
|
239
|
+
log_status(operation, result.status)
|
|
240
|
+
result.response.to_h.deep_stringify_keys
|
|
241
|
+
rescue *CONNECTION_ERRORS, Adyen::ServerError => e
|
|
242
|
+
log_status(operation, e.is_a?(Adyen::ServerError) ? e.code : e.class.name.demodulize)
|
|
243
|
+
if attempt <= RETRIES
|
|
244
|
+
wait_before_retry(attempt)
|
|
245
|
+
retry
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
error_class = money_moving ? Spree::Core::AmbiguousGatewayError : Spree::Core::GatewayError
|
|
249
|
+
raise error_class, I18n.t('spree.adyen.errors.no_answer', message: e.message.to_s.truncate(200))
|
|
250
|
+
rescue Adyen::AdyenError => e
|
|
251
|
+
log_status(operation, e.code)
|
|
252
|
+
raise refusal(e)
|
|
405
253
|
end
|
|
406
|
-
rescue Adyen::AdyenError => e
|
|
407
|
-
failure(parse_adyen_error_response(e)['message'])
|
|
408
254
|
end
|
|
409
255
|
|
|
410
|
-
|
|
411
|
-
response = client.management.webhooks_merchant_level_api.generate_hmac_key(preferred_merchant_account, preferred_webhook_id)
|
|
256
|
+
private
|
|
412
257
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
failure(parse_adyen_error_response(e)['message'])
|
|
258
|
+
# Configured when the method is created and whenever the credential or the
|
|
259
|
+
# merchant account changes. Not when configuration itself stores what it wrote.
|
|
260
|
+
def configure_async
|
|
261
|
+
return unless previously_new_record? || CREDENTIAL_PREFERENCES.any? { |key| public_send(:"saved_change_to_preferred_#{key}?") }
|
|
262
|
+
|
|
263
|
+
SpreeAdyen::ConfigureJob.perform_later(id)
|
|
420
264
|
end
|
|
421
265
|
|
|
422
|
-
def
|
|
423
|
-
|
|
266
|
+
def credentials_changed?
|
|
267
|
+
return false if preferred_api_key.blank? || preferred_merchant_account.blank?
|
|
424
268
|
|
|
425
|
-
|
|
426
|
-
success(response.response.clientKey, response.response)
|
|
427
|
-
else
|
|
428
|
-
failure(response.response.message)
|
|
429
|
-
end
|
|
430
|
-
rescue Adyen::AdyenError => e
|
|
431
|
-
failure(parse_adyen_error_response(e)['message'])
|
|
269
|
+
new_record? || CREDENTIAL_PREFERENCES.any? { |key| public_send(:"preferred_#{key}_changed?") }
|
|
432
270
|
end
|
|
433
271
|
|
|
434
|
-
def
|
|
435
|
-
|
|
272
|
+
def validate_credential
|
|
273
|
+
problem = credential_problem
|
|
274
|
+
errors.add(:base, problem) if problem
|
|
275
|
+
rescue Spree::Core::GatewayError => e
|
|
276
|
+
errors.add(:base, e.message)
|
|
436
277
|
end
|
|
437
278
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
errors.add(:preferred_api_key, "has insufficient permissions. Add missing roles to API credential. Response: #{e.msg}")
|
|
448
|
-
rescue Adyen::AdyenError => e
|
|
449
|
-
errors.add(:preferred_api_key, "An error occurred. Response: #{e.msg}")
|
|
279
|
+
def adyen_client
|
|
280
|
+
Adyen::Client.new(
|
|
281
|
+
api_key: preferred_api_key,
|
|
282
|
+
env: preferred_test_mode ? :test : :live,
|
|
283
|
+
live_url_prefix: preferred_live_url_prefix.presence,
|
|
284
|
+
connection_options: Faraday::ConnectionOptions.new(
|
|
285
|
+
request: Faraday::RequestOptions.new(open_timeout: OPEN_TIMEOUT, timeout: READ_TIMEOUT)
|
|
286
|
+
)
|
|
287
|
+
)
|
|
450
288
|
end
|
|
451
289
|
|
|
452
|
-
def
|
|
453
|
-
|
|
290
|
+
def wait_before_retry(attempt)
|
|
291
|
+
sleep(0.5 * (2**(attempt - 1)))
|
|
292
|
+
end
|
|
454
293
|
|
|
455
|
-
|
|
294
|
+
def log_status(operation, status)
|
|
295
|
+
Rails.logger.info("[spree_adyen] #{operation} #{status}")
|
|
456
296
|
end
|
|
457
297
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
298
|
+
# Adyen's own error code and message, never the library's summary.
|
|
299
|
+
def refusal(error)
|
|
300
|
+
body = begin
|
|
301
|
+
error.response.is_a?(String) ? JSON.parse(error.response) : error.response.to_h
|
|
302
|
+
rescue JSON::ParserError
|
|
303
|
+
{}
|
|
304
|
+
end
|
|
305
|
+
message = body['message'].presence || body['detail'].presence || error.msg.to_s
|
|
306
|
+
code = body['errorCode'].presence
|
|
307
|
+
text = I18n.t('spree.adyen.errors.refused', message: [message, code && "(#{code})"].compact.join(' '))
|
|
308
|
+
|
|
309
|
+
case error
|
|
310
|
+
when Adyen::AuthenticationError then CredentialRejected.new(text)
|
|
311
|
+
when Adyen::PermissionError then RoleMissing.new(text)
|
|
312
|
+
else Spree::Core::GatewayError.new(text)
|
|
463
313
|
end
|
|
464
314
|
end
|
|
465
315
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
316
|
+
# The payment a session id names, among this method's own.
|
|
317
|
+
def payment_for(session_id)
|
|
318
|
+
# A payment core split off a partial capture has no session, and so no
|
|
319
|
+
# PSP reference to ask Adyen about.
|
|
320
|
+
raise Spree::Core::GatewayError, I18n.t('spree.adyen.errors.psp_reference_missing') if session_id.blank?
|
|
321
|
+
|
|
322
|
+
payments.find_by!(response_code: session_id)
|
|
323
|
+
rescue ActiveRecord::RecordNotFound
|
|
324
|
+
raise Spree::Core::GatewayError, I18n.t('spree.adyen.errors.payment_not_found')
|
|
470
325
|
end
|
|
471
326
|
|
|
472
|
-
def
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
{ 'message' => error.msg }
|
|
327
|
+
def payment_from_options(gateway_options)
|
|
328
|
+
prefixed_id = gateway_options[:payment_prefixed_id].presence
|
|
329
|
+
payments.find_by_prefix_id(prefixed_id) if prefixed_id
|
|
476
330
|
end
|
|
477
331
|
|
|
478
|
-
def
|
|
479
|
-
|
|
480
|
-
|
|
332
|
+
def psp_reference!(payment)
|
|
333
|
+
payment.metadata['adyen_psp_reference'].presence ||
|
|
334
|
+
raise(Spree::Core::GatewayError, I18n.t('spree.adyen.errors.psp_reference_missing'))
|
|
335
|
+
end
|
|
481
336
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
end
|
|
337
|
+
def adyen_amount(money)
|
|
338
|
+
SpreeAdyen::Amounts.to_adyen_amount(money.to_d, money.currency.iso_code)
|
|
485
339
|
end
|
|
486
340
|
|
|
487
|
-
|
|
488
|
-
|
|
341
|
+
# A capture, cancel or refund keyed from Spree records, so the key repeats
|
|
342
|
+
# only while the previous attempt's outcome is unknown and stays under
|
|
343
|
+
# Adyen's 64 characters. Adyen answers a repeated key with its first answer
|
|
344
|
+
# for days, so an attempt it refused is counted on the payment: the next
|
|
345
|
+
# one gets a new key instead of the old refusal.
|
|
346
|
+
#
|
|
347
|
+
# The request's reference is the key behind the order number, so staff
|
|
348
|
+
# find Spree's requests in the Customer Area, as with 5.x.
|
|
349
|
+
#
|
|
350
|
+
# @param attempt [Integer] the records the earlier answered attempts left
|
|
351
|
+
# @yieldparam reference [String] for the request body's +reference+
|
|
352
|
+
def money_moving_call(payment, action, attempt)
|
|
353
|
+
refusals = payment.metadata.dig('adyen_refusals', action).to_i
|
|
354
|
+
key = "spree-#{payment.prefixed_id}-#{action}-#{attempt + refusals}"
|
|
355
|
+
reference = [payment.owner&.number, key].compact.join('-')
|
|
356
|
+
adyen_call(action.to_sym, money_moving: true) { |client| yield(client, { 'Idempotency-Key' => key }, reference) }
|
|
357
|
+
rescue Spree::Core::AmbiguousGatewayError
|
|
358
|
+
raise
|
|
359
|
+
rescue Spree::Core::GatewayError
|
|
360
|
+
counted = (payment.metadata['adyen_refusals'] || {}).merge(action => refusals + 1)
|
|
361
|
+
payment.update_columns(metadata: payment.metadata.merge('adyen_refusals' => counted))
|
|
362
|
+
raise
|
|
489
363
|
end
|
|
490
364
|
|
|
491
|
-
def
|
|
492
|
-
Spree::PaymentResponse.new(
|
|
365
|
+
def success(authorization, requested: false, reference: nil)
|
|
366
|
+
Spree::PaymentResponse.new(true, 'Adyen accepted the request', {},
|
|
367
|
+
authorization: authorization, requested: requested, reference: reference,
|
|
368
|
+
test: preferred_test_mode)
|
|
493
369
|
end
|
|
494
370
|
end
|
|
495
371
|
end
|