Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/checkout_sdk/checkout_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ module CheckoutSdk
# @return [CheckoutSdk::Payments::PaymentSessionsClient]
# @!attribute payments_setups
# @return [CheckoutSdk::Payments::PaymentSetupsClient]
# @!attribute flow
# @return [CheckoutSdk::Payments::FlowClient]
# @!attribute forward
# @return [CheckoutSdk::Forward::ForwardClient]
class CheckoutApi
Expand All @@ -56,6 +58,7 @@ class CheckoutApi
:links,
:payments,
:payments_setups,
:flow,
:reports,
:sessions,
:tokens,
Expand Down Expand Up @@ -97,6 +100,7 @@ def initialize(configuration)
@contexts = CheckoutSdk::Payments::PaymentContextsClient.new api_client, configuration
@payment_sessions = CheckoutSdk::Payments::PaymentSessionsClient.new api_client, configuration
@payments_setups = CheckoutSdk::Payments::PaymentSetupsClient.new api_client, configuration
@flow = CheckoutSdk::Payments::FlowClient.new api_client, configuration
@forward = CheckoutSdk::Forward::ForwardClient.new(api_client, configuration)
end

Expand Down
59 changes: 59 additions & 0 deletions lib/checkout_sdk/payments/flow/flow_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module CheckoutSdk
module Payments
# Client for Payment Flow API operations
# [Beta]
class FlowClient < Client
PAYMENT_SESSIONS_PATH = 'payment-sessions'
SUBMIT_PATH = 'submit'
COMPLETE_PATH = 'complete'

# @param [ApiClient] api_client
# @param [CheckoutConfiguration] configuration
def initialize(api_client, configuration)
super(api_client, configuration, CheckoutSdk::AuthorizationType::SECRET_KEY_OR_OAUTH)
end

# Creates a Payment Session.
# Use this endpoint to set up a payment session before collecting payment details from your customer.
# [Beta]
#
# @param [Hash] request_payment_session_request
def request_payment_session(request_payment_session_request)
api_client.invoke_post(
build_path(PAYMENT_SESSIONS_PATH),
sdk_authorization,
request_payment_session_request
)
end

# Submits a Payment Session.
# Use this endpoint to submit payment details and process the payment for an existing session.
# [Beta]
#
# @param [String] id - The unique identifier of the Payment Session
# @param [Hash] submit_payment_session_request
def submit_payment_session(id, submit_payment_session_request)
api_client.invoke_post(
build_path(PAYMENT_SESSIONS_PATH, id, SUBMIT_PATH),
sdk_authorization,
submit_payment_session_request
)
end

# Creates and submits a Payment Session in a single request.
# Use this endpoint to create a payment session and immediately process the payment.
# [Beta]
#
# @param [Hash] request_payment_session_with_payment_request
def request_payment_session_with_payment(request_payment_session_with_payment_request)
api_client.invoke_post(
build_path(PAYMENT_SESSIONS_PATH, COMPLETE_PATH),
sdk_authorization,
request_payment_session_with_payment_request
)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/checkout_sdk/payments/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,6 @@

# Payment Setups
require 'checkout_sdk/payments/setups/payment_setups_client'

# Payment Flow
require 'checkout_sdk/payments/flow/flow_client'
145 changes: 145 additions & 0 deletions spec/checkout_sdk/payments/flow/flow_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# frozen_string_literal: true

module FlowHelper
def request_payment_session_request(**opts)
amount = opts.fetch(:amount, 1000)
currency = opts.fetch(:currency, 'USD')

{
amount: amount,
currency: currency,
payment_type: 'Regular',
billing: {
address: {
address_line1: '123 High St.',
address_line2: 'Flat 456',
city: 'London',
state: 'London',
zip: 'SW1A 1AA',
country: 'GB'
},
phone: {
country_code: '+1',
number: '415 555 2671'
}
},
reference: "ORD-#{SecureRandom.uuid[0..8]}",
description: 'Integration test payment session',
customer: {
email: "jia.tsang+#{SecureRandom.uuid[0..5]}@example.com",
name: 'Jia Tsang',
phone: {
country_code: '+1',
number: '415 555 2671'
}
},
shipping: {
address: {
address_line1: '123 High St.',
address_line2: 'Flat 456',
city: 'London',
state: 'London',
zip: 'SW1A 1AA',
country: 'GB'
},
phone: {
country_code: '+1',
number: '415 555 2671'
}
},
processing_channel_id: ENV.fetch('CHECKOUT_PROCESSING_CHANNEL_ID'),
success_url: 'https://example.com/payments/success',
failure_url: 'https://example.com/payments/failure',
enabled_payment_methods: ['card'],
metadata: {
test_payment: 'flow_integration_test'
}
}
end

def submit_payment_session_request(**opts)
amount = opts.fetch(:amount, 1000)

{
session_data: 'string',
amount: amount,
reference: "SUBMIT-#{SecureRandom.uuid[0..8]}",
payment_type: 'Regular',
items: [{
reference: "item-#{SecureRandom.uuid[0..5]}",
name: 'Test Item',
quantity: 1,
unit_price: amount,
total_amount: amount
}],
ip_address: '90.197.169.245'
}
end

def request_payment_session_with_payment_request(**opts)
amount = opts.fetch(:amount, 1000)
currency = opts.fetch(:currency, 'USD')

{
session_data: 'string',
amount: amount,
currency: currency,
payment_type: 'Regular',
billing: {
address: {
address_line1: '123 High St.',
address_line2: 'Flat 456',
city: 'London',
state: 'London',
zip: 'SW1A 1AA',
country: 'GB'
},
phone: {
country_code: '+1',
number: '415 555 2671'
}
},
reference: "CREATE-SUBMIT-#{SecureRandom.uuid[0..8]}",
description: 'Integration test create and submit',
customer: {
email: "jia.tsang+#{SecureRandom.uuid[0..5]}@example.com",
name: 'Jia Tsang',
phone: {
country_code: '+1',
number: '415 555 2671'
}
},
processing_channel_id: ENV.fetch('CHECKOUT_PROCESSING_CHANNEL_ID'),
success_url: 'https://example.com/payments/success',
failure_url: 'https://example.com/payments/failure'
}
end

def request_payment_session(**opts)
amount = opts.fetch(:amount, 1000)
currency = opts.fetch(:currency, 'USD')

request = request_payment_session_request(amount: amount, currency: currency)
response = default_sdk.flow.request_payment_session(request)
expect(response).not_to be nil
expect(response.id).not_to be nil
response
end

def submit_payment_session(session_id, **opts)
request = submit_payment_session_request(**opts)
response = default_sdk.flow.submit_payment_session(session_id, request)
expect(response).not_to be nil
response
end

def request_payment_session_with_payment(**opts)
amount = opts.fetch(:amount, 1000)
currency = opts.fetch(:currency, 'USD')

request = request_payment_session_with_payment_request(amount: amount, currency: currency)
response = default_sdk.flow.request_payment_session_with_payment(request)
expect(response).not_to be nil
response
end
end
Loading