onlinepayments-sdk-ruby 10.4.0 → 11.1.0
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/AGENTS.md +23 -0
- data/README.md +22 -0
- data/lib/onlinepayments/sdk/communication/metadata_provider.rb +1 -1
- data/lib/onlinepayments/sdk/domain/card_installment_details.rb +36 -0
- data/lib/onlinepayments/sdk/domain/card_payment_method_specific_input.rb +30 -0
- data/lib/onlinepayments/sdk/domain/card_payment_method_specific_input_base.rb +30 -0
- data/lib/onlinepayments/sdk/domain/create_hosted_tokenization_request.rb +13 -0
- data/lib/onlinepayments/sdk/domain/payment_product5407_auto_capture.rb +36 -0
- data/lib/onlinepayments/sdk/domain/redirect_payment_product5407_specific_input.rb +9 -9
- data/lib/onlinepayments/sdk/factory.rb +6 -0
- data/lib/onlinepayments/sdk/merchant/merchant_client.rb +4 -0
- data/onlinepayments-sdk-ruby.gemspec +2 -2
- data/skills/worldline-direct/SKILL.md +85 -0
- data/skills/worldline-direct/examples/after_payment_operations_example.rb +75 -0
- data/skills/worldline-direct/examples/app_types.rb +170 -0
- data/skills/worldline-direct/examples/client_integration_example.rb +76 -0
- data/skills/worldline-direct/examples/client_setup.rb +60 -0
- data/skills/worldline-direct/examples/create_payment_response_handler.rb +32 -0
- data/skills/worldline-direct/examples/customer_device.rb +32 -0
- data/skills/worldline-direct/examples/error_handling_example.rb +99 -0
- data/skills/worldline-direct/examples/hosted_checkout_example.rb +181 -0
- data/skills/worldline-direct/examples/hosted_fields_example.rb +82 -0
- data/skills/worldline-direct/examples/hosted_tokenization_example.rb +79 -0
- data/skills/worldline-direct/examples/payment_attempt.rb +30 -0
- data/skills/worldline-direct/examples/payment_links_example.rb +52 -0
- data/skills/worldline-direct/examples/payment_status_example.rb +62 -0
- data/skills/worldline-direct/examples/recurring_payments_example.rb +127 -0
- data/skills/worldline-direct/examples/server_to_server_example.rb +81 -0
- data/skills/worldline-direct/examples/webhook_event_processor.rb +40 -0
- data/skills/worldline-direct/examples/webhooks_endpoint.rb +73 -0
- data/skills/worldline-direct/examples/webhooks_setup.rb +41 -0
- data/skills/worldline-direct/references/errors.md +113 -0
- data/skills/worldline-direct/references/flows.md +966 -0
- data/skills/worldline-direct/references/ruby.md +208 -0
- data/skills/worldline-direct/references/webhooks.md +167 -0
- metadata +27 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'onlinepayments/sdk'
|
|
2
|
+
|
|
3
|
+
# Each method takes an idempotence key. Create it once per operation (e.g. when someone clicks "Refund") and store it.
|
|
4
|
+
# Pass the same key again if the request got no answer; see ErrorHandlingExample.
|
|
5
|
+
class AfterPaymentOperationsExample
|
|
6
|
+
# @param merchant [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
7
|
+
def initialize(merchant)
|
|
8
|
+
@merchant = merchant
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# [START capture-payment]
|
|
12
|
+
# Captures an authorized payment (status PENDING_CAPTURE). Without an amount, the full amount is captured.
|
|
13
|
+
# @param payment_id [String]
|
|
14
|
+
# @param amount_in_cents [Integer]
|
|
15
|
+
# @param is_final [Boolean] true: this capture is final, no further capture is accepted.
|
|
16
|
+
# @param idempotence_key [String]
|
|
17
|
+
# @return [OnlinePayments::SDK::Domain::CaptureResponse]
|
|
18
|
+
def capture(payment_id, amount_in_cents, is_final, idempotence_key)
|
|
19
|
+
details = merchant.payments.get_payment_details(payment_id)
|
|
20
|
+
raise "Payment #{payment_id} cannot be captured in status #{details.status}" if details.status != 'PENDING_CAPTURE'
|
|
21
|
+
|
|
22
|
+
request = OnlinePayments::SDK::Domain::CapturePaymentRequest.new
|
|
23
|
+
request.amount = amount_in_cents
|
|
24
|
+
request.is_final = is_final
|
|
25
|
+
context = OnlinePayments::SDK::CallContext.new(idempotence_key)
|
|
26
|
+
merchant.payments.capture_payment(payment_id, request, context)
|
|
27
|
+
end
|
|
28
|
+
# [END capture-payment]
|
|
29
|
+
|
|
30
|
+
# [START refund-payment]
|
|
31
|
+
# Refunds a captured payment, fully or partly.
|
|
32
|
+
# @param payment_id [String]
|
|
33
|
+
# @param amount_in_cents [Integer]
|
|
34
|
+
# @param currency_code [String]
|
|
35
|
+
# @param idempotence_key [String]
|
|
36
|
+
# @return [OnlinePayments::SDK::Domain::RefundResponse]
|
|
37
|
+
def refund(payment_id, amount_in_cents, currency_code, idempotence_key)
|
|
38
|
+
details = merchant.payments.get_payment_details(payment_id)
|
|
39
|
+
unless details.status_output&.is_refundable == true
|
|
40
|
+
raise "Payment #{payment_id} is not refundable in status #{details.status}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
amount_of_money = OnlinePayments::SDK::Domain::AmountOfMoney.new
|
|
44
|
+
amount_of_money.amount = amount_in_cents
|
|
45
|
+
amount_of_money.currency_code = currency_code
|
|
46
|
+
request = OnlinePayments::SDK::Domain::RefundRequest.new
|
|
47
|
+
request.amount_of_money = amount_of_money
|
|
48
|
+
context = OnlinePayments::SDK::CallContext.new(idempotence_key)
|
|
49
|
+
# REFUND_REQUESTED: the platform is processing the refund and waiting for the result. The status then becomes
|
|
50
|
+
# REFUNDED or REJECTED; treat the refund as done only when it is REFUNDED.
|
|
51
|
+
merchant.payments.refund_payment(payment_id, request, context)
|
|
52
|
+
end
|
|
53
|
+
# [END refund-payment]
|
|
54
|
+
|
|
55
|
+
# [START cancel-payment]
|
|
56
|
+
# Cancels (voids) an authorization that is not captured yet. No amount cancels the full amount.
|
|
57
|
+
# @param payment_id [String]
|
|
58
|
+
# @param idempotence_key [String]
|
|
59
|
+
# @return [OnlinePayments::SDK::Domain::CancelPaymentResponse]
|
|
60
|
+
def cancel(payment_id, idempotence_key)
|
|
61
|
+
details = merchant.payments.get_payment_details(payment_id)
|
|
62
|
+
unless details.status_output&.is_cancellable == true
|
|
63
|
+
raise "Payment #{payment_id} is not cancellable in status #{details.status}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
context = OnlinePayments::SDK::CallContext.new(idempotence_key)
|
|
67
|
+
merchant.payments.cancel_payment(payment_id, OnlinePayments::SDK::Domain::CancelPaymentRequest.new, context)
|
|
68
|
+
end
|
|
69
|
+
# [END cancel-payment]
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# @return [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
74
|
+
attr_reader :merchant
|
|
75
|
+
end
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# Stand-ins for your application's own types and persistence. The examples use them where your code must store or read
|
|
2
|
+
# data; replace them with your own code.
|
|
3
|
+
|
|
4
|
+
# The payment state of an order in your application.
|
|
5
|
+
module OrderPaymentState
|
|
6
|
+
PENDING = :pending
|
|
7
|
+
AUTHORIZED = :authorized
|
|
8
|
+
PAID = :paid
|
|
9
|
+
FAILED = :failed
|
|
10
|
+
CANCELLED = :cancelled
|
|
11
|
+
REFUNDED = :refunded
|
|
12
|
+
REVERSED = :reversed
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# What the web layer must do after a create payment call: redirect the customer to redirect_url, or show the state.
|
|
16
|
+
class PaymentStep
|
|
17
|
+
# @param redirect_url [String, nil]
|
|
18
|
+
# @param state [Symbol, nil] one of the OrderPaymentState values
|
|
19
|
+
def initialize(redirect_url: nil, state: nil)
|
|
20
|
+
@redirect_url = redirect_url
|
|
21
|
+
@state = state
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @return [String, nil]
|
|
25
|
+
attr_reader :redirect_url
|
|
26
|
+
|
|
27
|
+
# @return [Symbol, nil]
|
|
28
|
+
attr_reader :state
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class StoredHostedCheckout
|
|
32
|
+
# @param hosted_checkout_id [String]
|
|
33
|
+
# @param returnmac [String]
|
|
34
|
+
def initialize(hosted_checkout_id, returnmac)
|
|
35
|
+
@hosted_checkout_id = hosted_checkout_id
|
|
36
|
+
@returnmac = returnmac
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [String]
|
|
40
|
+
attr_reader :hosted_checkout_id
|
|
41
|
+
|
|
42
|
+
# @return [String]
|
|
43
|
+
attr_reader :returnmac
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Stand-in for your database code.
|
|
47
|
+
class OrderStore
|
|
48
|
+
# @param order_id [String]
|
|
49
|
+
# @param hosted_checkout_id [String]
|
|
50
|
+
# @param returnmac [String]
|
|
51
|
+
# @return [void]
|
|
52
|
+
def save_hosted_checkout(order_id, hosted_checkout_id, returnmac)
|
|
53
|
+
raise NotImplementedError
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param order_id [String]
|
|
57
|
+
# @return [StoredHostedCheckout]
|
|
58
|
+
def get_hosted_checkout(order_id)
|
|
59
|
+
raise NotImplementedError
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The idempotence key of the create call for this order. Store it before the call.
|
|
63
|
+
# @param order_id [String]
|
|
64
|
+
# @param idempotence_key [String]
|
|
65
|
+
# @return [void]
|
|
66
|
+
def save_idempotence_key(order_id, idempotence_key)
|
|
67
|
+
raise NotImplementedError
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param order_id [String]
|
|
71
|
+
# @return [String, nil]
|
|
72
|
+
def get_idempotence_key(order_id)
|
|
73
|
+
raise NotImplementedError
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @param order_id [String]
|
|
77
|
+
# @param payment_id [String]
|
|
78
|
+
# @return [void]
|
|
79
|
+
def save_payment_id(order_id, payment_id)
|
|
80
|
+
raise NotImplementedError
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param order_id [String]
|
|
84
|
+
# @return [String, nil] nil while no payment ID is stored yet
|
|
85
|
+
def get_payment_id(order_id)
|
|
86
|
+
raise NotImplementedError
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @param order_id [String]
|
|
90
|
+
# @param state [Symbol] one of the OrderPaymentState values
|
|
91
|
+
# @return [void]
|
|
92
|
+
def save_state(order_id, state)
|
|
93
|
+
raise NotImplementedError
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# @param customer_id [String]
|
|
97
|
+
# @param initial_payment_id [String]
|
|
98
|
+
# @return [void]
|
|
99
|
+
def save_recurring_payment_id(customer_id, initial_payment_id)
|
|
100
|
+
raise NotImplementedError
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @param customer_id [String]
|
|
104
|
+
# @return [String]
|
|
105
|
+
def get_recurring_payment_id(customer_id)
|
|
106
|
+
raise NotImplementedError
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Browser values collected with JavaScript on the checkout page.
|
|
111
|
+
class BrowserInfo
|
|
112
|
+
# @param color_depth [Integer]
|
|
113
|
+
# @param java_enabled [Boolean]
|
|
114
|
+
# @param screen_height [Integer]
|
|
115
|
+
# @param screen_width [Integer]
|
|
116
|
+
# @param timezone_offset_utc_minutes [Integer]
|
|
117
|
+
# @param locale [String]
|
|
118
|
+
def initialize(color_depth:, java_enabled:, screen_height:, screen_width:, timezone_offset_utc_minutes:, locale:)
|
|
119
|
+
@color_depth = color_depth
|
|
120
|
+
@java_enabled = java_enabled
|
|
121
|
+
@screen_height = screen_height
|
|
122
|
+
@screen_width = screen_width
|
|
123
|
+
@timezone_offset_utc_minutes = timezone_offset_utc_minutes
|
|
124
|
+
@locale = locale
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# @return [Integer]
|
|
128
|
+
attr_reader :color_depth
|
|
129
|
+
|
|
130
|
+
# @return [Boolean]
|
|
131
|
+
attr_reader :java_enabled
|
|
132
|
+
|
|
133
|
+
# @return [Integer]
|
|
134
|
+
attr_reader :screen_height
|
|
135
|
+
|
|
136
|
+
# @return [Integer]
|
|
137
|
+
attr_reader :screen_width
|
|
138
|
+
|
|
139
|
+
# @return [Integer]
|
|
140
|
+
attr_reader :timezone_offset_utc_minutes
|
|
141
|
+
|
|
142
|
+
# @return [String]
|
|
143
|
+
attr_reader :locale
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Card details as entered by the customer. Never log or store this object.
|
|
147
|
+
class CardInput
|
|
148
|
+
# @param number [String]
|
|
149
|
+
# @param holder_name [String]
|
|
150
|
+
# @param expiry_date [String] MMYY, e.g. 1230 for December 2030.
|
|
151
|
+
# @param cvv [String]
|
|
152
|
+
def initialize(number:, holder_name:, expiry_date:, cvv:)
|
|
153
|
+
@number = number
|
|
154
|
+
@holder_name = holder_name
|
|
155
|
+
@expiry_date = expiry_date
|
|
156
|
+
@cvv = cvv
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @return [String]
|
|
160
|
+
attr_reader :number
|
|
161
|
+
|
|
162
|
+
# @return [String]
|
|
163
|
+
attr_reader :holder_name
|
|
164
|
+
|
|
165
|
+
# @return [String]
|
|
166
|
+
attr_reader :expiry_date
|
|
167
|
+
|
|
168
|
+
# @return [String]
|
|
169
|
+
attr_reader :cvv
|
|
170
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require 'onlinepayments/sdk'
|
|
2
|
+
require_relative 'app_types'
|
|
3
|
+
require_relative 'create_payment_response_handler'
|
|
4
|
+
require_relative 'payment_attempt'
|
|
5
|
+
|
|
6
|
+
class ClientIntegrationExample
|
|
7
|
+
# @param merchant [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
8
|
+
# @param orders [OrderStore]
|
|
9
|
+
def initialize(merchant, orders)
|
|
10
|
+
@merchant = merchant
|
|
11
|
+
@orders = orders
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# [START client-session-create]
|
|
15
|
+
# Creates the session that a Client SDK (JavaScript, iOS, Android, Flutter...) needs to encrypt card data.
|
|
16
|
+
# @return [OnlinePayments::SDK::Domain::SessionResponse]
|
|
17
|
+
def create_client_session
|
|
18
|
+
session = merchant.sessions.create_session(OnlinePayments::SDK::Domain::SessionRequest.new)
|
|
19
|
+
# Send client_session_id, customer_id, client_api_url and asset_url to the app. Never send your API key.
|
|
20
|
+
session
|
|
21
|
+
end
|
|
22
|
+
# [END client-session-create]
|
|
23
|
+
|
|
24
|
+
# [START client-encrypted-payment]
|
|
25
|
+
# The Client SDK returns an encrypted string; the app posts it to your server.
|
|
26
|
+
# @param order_id [String]
|
|
27
|
+
# @param encrypted_customer_input [String]
|
|
28
|
+
# @param amount_in_cents [Integer]
|
|
29
|
+
# @param currency_code [String]
|
|
30
|
+
# @param device [OnlinePayments::SDK::Domain::CustomerDevice]
|
|
31
|
+
# @param return_url [String]
|
|
32
|
+
# @return [PaymentStep]
|
|
33
|
+
def pay(order_id, encrypted_customer_input, amount_in_cents, currency_code, device, return_url)
|
|
34
|
+
amount_of_money = OnlinePayments::SDK::Domain::AmountOfMoney.new
|
|
35
|
+
amount_of_money.amount = amount_in_cents
|
|
36
|
+
amount_of_money.currency_code = currency_code
|
|
37
|
+
|
|
38
|
+
references = OnlinePayments::SDK::Domain::OrderReferences.new
|
|
39
|
+
references.merchant_reference = order_id
|
|
40
|
+
|
|
41
|
+
customer = OnlinePayments::SDK::Domain::Customer.new
|
|
42
|
+
customer.device = device
|
|
43
|
+
|
|
44
|
+
order = OnlinePayments::SDK::Domain::Order.new
|
|
45
|
+
order.amount_of_money = amount_of_money
|
|
46
|
+
order.references = references
|
|
47
|
+
order.customer = customer
|
|
48
|
+
|
|
49
|
+
redirection_data = OnlinePayments::SDK::Domain::RedirectionData.new
|
|
50
|
+
redirection_data.return_url = return_url
|
|
51
|
+
three_d_secure = OnlinePayments::SDK::Domain::ThreeDSecure.new
|
|
52
|
+
three_d_secure.redirection_data = redirection_data
|
|
53
|
+
|
|
54
|
+
card_input = OnlinePayments::SDK::Domain::CardPaymentMethodSpecificInput.new
|
|
55
|
+
card_input.authorization_mode = 'SALE'
|
|
56
|
+
card_input.three_d_secure = three_d_secure
|
|
57
|
+
|
|
58
|
+
request = OnlinePayments::SDK::Domain::CreatePaymentRequest.new
|
|
59
|
+
request.encrypted_customer_input = encrypted_customer_input
|
|
60
|
+
request.order = order
|
|
61
|
+
request.card_payment_method_specific_input = card_input
|
|
62
|
+
|
|
63
|
+
context = OnlinePayments::SDK::CallContext.new(PaymentAttempt.key_for(order_id, orders))
|
|
64
|
+
response = merchant.payments.create_payment(request, context)
|
|
65
|
+
CreatePaymentResponseHandler.handle_create_payment_response(response, order_id, orders)
|
|
66
|
+
end
|
|
67
|
+
# [END client-encrypted-payment]
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
# @return [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
72
|
+
attr_reader :merchant
|
|
73
|
+
|
|
74
|
+
# @return [OrderStore]
|
|
75
|
+
attr_reader :orders
|
|
76
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require 'onlinepayments/sdk'
|
|
3
|
+
|
|
4
|
+
module ClientSetup
|
|
5
|
+
# [START client-setup]
|
|
6
|
+
# Create the client once when the application starts, reuse it for every request and close it on shutdown.
|
|
7
|
+
# Every value is validated here, so a wrong configuration stops the application at startup.
|
|
8
|
+
# @return [OnlinePayments::SDK::Client]
|
|
9
|
+
def self.create_client
|
|
10
|
+
configuration = OnlinePayments::SDK::CommunicatorConfiguration.new(
|
|
11
|
+
# Test or live host, e.g. payment.direct.ecom.preprod.worldline-solutions.com (without a path).
|
|
12
|
+
api_endpoint: "https://#{required_env('onlinePayments_api_endpoint_host')}",
|
|
13
|
+
api_key_id: required_env('onlinePayments_api_v1hmac_apiKeyId'),
|
|
14
|
+
secret_api_key: required_env('onlinePayments_api_v1hmac_secretApiKey'),
|
|
15
|
+
# Required: without it, creating the client raises RuntimeError "Unknown authorizationType".
|
|
16
|
+
authorization_type: OnlinePayments::SDK::Authentication::AuthorizationType::V1HMAC,
|
|
17
|
+
# Your company or application name; it identifies your requests in the platform logs.
|
|
18
|
+
# Required: without it, the SDK raises ArgumentError "integrator is required".
|
|
19
|
+
integrator: required_env('onlinePayments_api_integrator'),
|
|
20
|
+
# In seconds.
|
|
21
|
+
connect_timeout: 5,
|
|
22
|
+
socket_timeout: 300,
|
|
23
|
+
max_connections: 10
|
|
24
|
+
)
|
|
25
|
+
OnlinePayments::SDK::Factory.create_client_from_configuration(configuration)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The merchant ID is your PSPID. All API calls start from the merchant client.
|
|
29
|
+
# @param client [OnlinePayments::SDK::Client]
|
|
30
|
+
# @return [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
31
|
+
def self.create_merchant_client(client)
|
|
32
|
+
client.merchant(required_env('onlinePayments_api_merchantId'))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The absolute public base URL of your shop, e.g. https://shop.example. The return URL of a checkout is built from
|
|
36
|
+
# it, never from the Host header of a request, which the sender controls.
|
|
37
|
+
# @return [String] without a trailing slash
|
|
38
|
+
def self.public_base_url
|
|
39
|
+
value = required_env('shop_publicBaseUrl')
|
|
40
|
+
uri = URI.parse(value)
|
|
41
|
+
unless uri.is_a?(URI::HTTP) && uri.host && !uri.host.empty? && uri.query.nil? && uri.fragment.nil?
|
|
42
|
+
raise "shop_publicBaseUrl must be an absolute http(s) URL without query or fragment, e.g. https://shop.example"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
value.strip.chomp('/')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Read credentials from the environment or, better, a secret store of your platform, and never hard-code them.
|
|
49
|
+
# For local development keep them in your own environment or a local secret store, not in a file in the repository.
|
|
50
|
+
# A blank value counts as missing.
|
|
51
|
+
# @param name [String]
|
|
52
|
+
# @return [String]
|
|
53
|
+
def self.required_env(name)
|
|
54
|
+
value = ENV[name]
|
|
55
|
+
raise "Environment variable #{name} is not set" if value.nil? || value.strip.empty?
|
|
56
|
+
|
|
57
|
+
value
|
|
58
|
+
end
|
|
59
|
+
# [END client-setup]
|
|
60
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'onlinepayments/sdk'
|
|
2
|
+
require_relative 'app_types'
|
|
3
|
+
require_relative 'payment_status_example'
|
|
4
|
+
|
|
5
|
+
module CreatePaymentResponseHandler
|
|
6
|
+
# [START payment-response-handle]
|
|
7
|
+
# Use for every create_payment response: server-to-server, Hosted Tokenization, Hosted Fields and client integration.
|
|
8
|
+
# @param response [OnlinePayments::SDK::Domain::CreatePaymentResponse]
|
|
9
|
+
# @param order_id [String]
|
|
10
|
+
# @param orders [OrderStore]
|
|
11
|
+
# @return [PaymentStep]
|
|
12
|
+
def self.handle_create_payment_response(response, order_id, orders)
|
|
13
|
+
payment = response.payment
|
|
14
|
+
payment_id = payment&.id
|
|
15
|
+
raise "The create payment response for order #{order_id} has no payment" if payment.nil? || payment_id.nil?
|
|
16
|
+
|
|
17
|
+
orders.save_payment_id(order_id, payment_id)
|
|
18
|
+
|
|
19
|
+
redirect_url = response.merchant_action&.redirect_data&.redirect_url
|
|
20
|
+
if response.merchant_action&.action_type == 'REDIRECT' && redirect_url && !redirect_url.empty?
|
|
21
|
+
# 3-D Secure challenge or a third-party page. Redirect to the URL exactly as returned.
|
|
22
|
+
# The customer comes back on your return URL; then read the status with get_payment_details.
|
|
23
|
+
return PaymentStep.new(redirect_url: redirect_url)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# No customer action needed: the result is already known.
|
|
27
|
+
state = PaymentStatusExample.to_order_state(payment.status, payment.status_output)
|
|
28
|
+
orders.save_state(order_id, state)
|
|
29
|
+
PaymentStep.new(state: state)
|
|
30
|
+
end
|
|
31
|
+
# [END payment-response-handle]
|
|
32
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'onlinepayments/sdk'
|
|
2
|
+
require_relative 'app_types'
|
|
3
|
+
|
|
4
|
+
module CustomerDeviceBuilder
|
|
5
|
+
# [START customer-device]
|
|
6
|
+
# 3-D Secure needs details about the customer's browser. Collect the browser values with JavaScript on the checkout
|
|
7
|
+
# page and read the rest from the HTTP request (e.g. in Sinatra or Rails: request.env['HTTP_ACCEPT'],
|
|
8
|
+
# request.user_agent, request.ip).
|
|
9
|
+
# @param accept_header [String]
|
|
10
|
+
# @param user_agent [String]
|
|
11
|
+
# @param ip_address [String]
|
|
12
|
+
# @param browser [BrowserInfo]
|
|
13
|
+
# @return [OnlinePayments::SDK::Domain::CustomerDevice]
|
|
14
|
+
def self.build_customer_device(accept_header, user_agent, ip_address, browser)
|
|
15
|
+
browser_data = OnlinePayments::SDK::Domain::BrowserData.new
|
|
16
|
+
browser_data.color_depth = browser.color_depth
|
|
17
|
+
browser_data.java_enabled = browser.java_enabled
|
|
18
|
+
browser_data.java_script_enabled = true
|
|
19
|
+
browser_data.screen_height = browser.screen_height.to_s
|
|
20
|
+
browser_data.screen_width = browser.screen_width.to_s
|
|
21
|
+
|
|
22
|
+
device = OnlinePayments::SDK::Domain::CustomerDevice.new
|
|
23
|
+
device.accept_header = accept_header
|
|
24
|
+
device.user_agent = user_agent
|
|
25
|
+
device.ip_address = ip_address
|
|
26
|
+
device.locale = browser.locale
|
|
27
|
+
device.timezone_offset_utc_minutes = browser.timezone_offset_utc_minutes.to_s
|
|
28
|
+
device.browser_data = browser_data
|
|
29
|
+
device
|
|
30
|
+
end
|
|
31
|
+
# [END customer-device]
|
|
32
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'logger'
|
|
2
|
+
require 'onlinepayments/sdk'
|
|
3
|
+
require_relative 'app_types'
|
|
4
|
+
require_relative 'create_payment_response_handler'
|
|
5
|
+
require_relative 'payment_attempt'
|
|
6
|
+
require_relative 'payment_status_example'
|
|
7
|
+
|
|
8
|
+
class ErrorHandlingExample
|
|
9
|
+
# @param merchant [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
10
|
+
# @param orders [OrderStore]
|
|
11
|
+
# @param logger [Logger]
|
|
12
|
+
def initialize(merchant, orders, logger)
|
|
13
|
+
@merchant = merchant
|
|
14
|
+
@orders = orders
|
|
15
|
+
@logger = logger
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# [START error-handling]
|
|
19
|
+
# idempotence_key is the stored key of the payment attempt (PaymentAttempt.key_for). Reuse it when a request
|
|
20
|
+
# got no answer; replace it, stored again, only when the platform answered with an error marked retriable.
|
|
21
|
+
# @param order_id [String]
|
|
22
|
+
# @param request [OnlinePayments::SDK::Domain::CreatePaymentRequest]
|
|
23
|
+
# @param idempotence_key [String]
|
|
24
|
+
# @return [PaymentStep]
|
|
25
|
+
def create_payment(order_id, request, idempotence_key)
|
|
26
|
+
context = OnlinePayments::SDK::CallContext.new(idempotence_key)
|
|
27
|
+
attempt = 1
|
|
28
|
+
begin
|
|
29
|
+
response = merchant.payments.create_payment(request, context)
|
|
30
|
+
rescue OnlinePayments::SDK::DeclinedPaymentException => e
|
|
31
|
+
# Declined: the payment exists. Read its status before deciding anything.
|
|
32
|
+
return handle_declined(order_id, e)
|
|
33
|
+
rescue OnlinePayments::SDK::IdempotenceException
|
|
34
|
+
# The first request with this key is still running. Wait, then send the same request again.
|
|
35
|
+
raise if attempt >= 3
|
|
36
|
+
|
|
37
|
+
sleep(2 * attempt)
|
|
38
|
+
attempt += 1
|
|
39
|
+
retry
|
|
40
|
+
rescue OnlinePayments::SDK::ValidationException => e
|
|
41
|
+
# 400: the request is invalid. Fix the request; retrying does not help.
|
|
42
|
+
# Log the error codes and the error ID, never the request (it can hold card data).
|
|
43
|
+
codes = e.errors.map { |error| "#{error.error_code} #{error.property_name}" }.join('; ')
|
|
44
|
+
logger.error("create_payment rejected, error_id #{e.error_id}: #{codes}")
|
|
45
|
+
raise
|
|
46
|
+
rescue OnlinePayments::SDK::ApiException => e
|
|
47
|
+
# A technical error marked retriable (e.g. PlatformException, 500, 502 or 503): the request was not
|
|
48
|
+
# processed. Send it again with a new key. Other errors are not retried: AuthorizationException
|
|
49
|
+
# (403) for a wrong API key, secret or merchant ID, ReferenceException (404, 409,
|
|
50
|
+
# 410) for an object not found, and server errors without a retriable error.
|
|
51
|
+
raise if attempt >= 3 || e.errors.none? { |error| error.retriable == true }
|
|
52
|
+
|
|
53
|
+
# The new key is stored before the call, like the first one.
|
|
54
|
+
context = OnlinePayments::SDK::CallContext.new(PaymentAttempt.start(order_id, orders))
|
|
55
|
+
sleep(2 * attempt)
|
|
56
|
+
attempt += 1
|
|
57
|
+
retry
|
|
58
|
+
rescue OnlinePayments::SDK::Communication::CommunicationException
|
|
59
|
+
# No answer: timeout or connection error. The payment may or may not exist.
|
|
60
|
+
# Retry with the same key; the platform returns the original result instead of creating a second
|
|
61
|
+
# payment.
|
|
62
|
+
raise if attempt >= 3
|
|
63
|
+
|
|
64
|
+
sleep(2 * attempt)
|
|
65
|
+
attempt += 1
|
|
66
|
+
retry
|
|
67
|
+
end
|
|
68
|
+
CreatePaymentResponseHandler.handle_create_payment_response(response, order_id, orders)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# @param order_id [String]
|
|
74
|
+
# @param e [OnlinePayments::SDK::DeclinedPaymentException]
|
|
75
|
+
# @return [PaymentStep]
|
|
76
|
+
def handle_declined(order_id, e)
|
|
77
|
+
payment_id = e.payment_result&.payment&.id
|
|
78
|
+
if payment_id.nil?
|
|
79
|
+
orders.save_state(order_id, OrderPaymentState::FAILED)
|
|
80
|
+
return PaymentStep.new(state: OrderPaymentState::FAILED)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
orders.save_payment_id(order_id, payment_id)
|
|
84
|
+
details = merchant.payments.get_payment_details(payment_id)
|
|
85
|
+
state = PaymentStatusExample.to_order_state(details.status, details.status_output)
|
|
86
|
+
orders.save_state(order_id, state)
|
|
87
|
+
PaymentStep.new(state: state)
|
|
88
|
+
end
|
|
89
|
+
# [END error-handling]
|
|
90
|
+
|
|
91
|
+
# @return [OnlinePayments::SDK::Merchant::MerchantClient]
|
|
92
|
+
attr_reader :merchant
|
|
93
|
+
|
|
94
|
+
# @return [OrderStore]
|
|
95
|
+
attr_reader :orders
|
|
96
|
+
|
|
97
|
+
# @return [Logger]
|
|
98
|
+
attr_reader :logger
|
|
99
|
+
end
|