Skip to content

Commit

Permalink
Adds request and get payment contexts endpoints (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Dec 22, 2023
1 parent 9f65b92 commit 83d2bd5
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-master.yml
Expand Up @@ -30,6 +30,7 @@ jobs:
run: bundle exec rubocop lib
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-pull-request.yml
Expand Up @@ -30,6 +30,7 @@ jobs:
run: bundle exec rubocop lib
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-release.yml
Expand Up @@ -19,6 +19,7 @@ jobs:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkout_sdk/api_client.rb
Expand Up @@ -127,7 +127,7 @@ def upload(path, authorization, file_request)
end

def parse_response(response)
raise CheckoutApiException, response if response.status < 200 || response.status >= 300
raise CheckoutApiException, response if response.status < 200 || response.status >= 400

metadata = CheckoutUtils.map_to_http_metadata(response)
body = parse_json_or_contents(response)
Expand Down
4 changes: 3 additions & 1 deletion lib/checkout_sdk/checkout_api.rb
Expand Up @@ -58,7 +58,8 @@ class CheckoutApi
:transfers,
:metadata,
:financial,
:issuing
:issuing,
:contexts

# @param [CheckoutConfiguration] configuration
def initialize(configuration)
Expand All @@ -82,6 +83,7 @@ def initialize(configuration)
@metadata = CheckoutSdk::Metadata::MetadataClient.new api_client, configuration
@financial = CheckoutSdk::Financial::FinancialClient.new api_client, configuration
@issuing = CheckoutSdk::Issuing::IssuingClient.new api_client, configuration
@contexts = CheckoutSdk::Payments::PaymentContextsClient.new api_client, configuration
end

private
Expand Down
25 changes: 25 additions & 0 deletions lib/checkout_sdk/payments/contexts/payment_contexts_client.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module CheckoutSdk
module Payments
class PaymentContextsClient < Client
PAYMENT_CONTEXTS = 'payment-contexts'

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

# @param [Hash] payment_contexts
def create_payment_contexts(payment_contexts)
api_client.invoke_post(PAYMENT_CONTEXTS, sdk_authorization, payment_contexts)
end

# @param [String] payment_context_id
def get_payment_context_details(payment_context_id)
api_client.invoke_get(build_path(PAYMENT_CONTEXTS, payment_context_id), sdk_authorization)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/checkout_sdk/payments/payments.rb
Expand Up @@ -155,3 +155,6 @@
# Payment Links
require 'checkout_sdk/payments/links/payment_link'
require 'checkout_sdk/payments/links/payments_links_client'

# Payment Contexts
require 'checkout_sdk/payments/contexts/payment_contexts_client'
32 changes: 32 additions & 0 deletions spec/checkout_sdk/payments/contexts/contexts_helper.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module ContextsHelper

def create_payment_contexts
request = {
'source' => {
'type' => 'paypal'
},
'amount' => 2000,
'currency' => CheckoutSdk::Common::Currency::EUR,
'payment_type' => CheckoutSdk::Payments::PaymentType::REGULAR,
'capture' => true,
'processing_channel_id' => ENV.fetch('CHECKOUT_PROCESSING_CHANNEL_ID', nil),
'success_url' => 'https://example.com/payments/success',
'failure_url' => 'https://example.com/payments/fail',
'items' => [
{
'name' => 'mask',
'unit_price' => 2000,
'quantity' => 1
}
]
}

response = default_sdk.contexts.create_payment_contexts(request)
expect(response).not_to be nil
expect(response.id).not_to be nil
response
end

end
51 changes: 51 additions & 0 deletions spec/checkout_sdk/payments/contexts/contexts_integration_spec.rb
@@ -0,0 +1,51 @@
# frozen_string_literal: true

RSpec.describe CheckoutSdk::Payments do
include ContextsHelper

before(:all) do
@payment_context = create_payment_contexts
end

describe '.create_payment_contexts' do
context 'when creating a payment contexts with valid data' do
it { is_valid_payment_context @payment_context }
end
end

describe '.get_payment_context_details' do
context 'when retrieving existing payment context details' do
it { is_valid_payment_context_details default_sdk.contexts.get_payment_context_details @payment_context.id }
end
end
end

def is_valid_payment_context(payment_context)
assert_response payment_context, %w[id
partner_metadata
partner_metadata.order_id]
end

def is_valid_payment_context_details(payment_context_details_response)
assert_response payment_context_details_response, %w[payment_request
payment_request.amount
payment_request.currency
payment_request.payment_type
payment_request.capture
payment_request.items
payment_request.success_url
payment_request.failure_url
partner_metadata
partner_metadata.order_id]

expect(payment_context_details_response.payment_request.amount).to eq 2000
expect(payment_context_details_response.payment_request.currency).to eq CheckoutSdk::Common::Currency::EUR
expect(payment_context_details_response.payment_request.payment_type).to eq CheckoutSdk::Payments::PaymentType::REGULAR
expect(payment_context_details_response.payment_request.capture).to eq true
expect(payment_context_details_response.payment_request.items[0].name).to eq 'mask'
expect(payment_context_details_response.payment_request.items[0].unit_price).to eq 2000
expect(payment_context_details_response.payment_request.items[0].quantity).to eq 1
expect(payment_context_details_response.payment_request.success_url).to eq 'https://example.com/payments/success'
expect(payment_context_details_response.payment_request.failure_url).to eq 'https://example.com/payments/fail'
expect(payment_context_details_response.partner_metadata.order_id).not_to be_nil
end
Expand Up @@ -51,8 +51,9 @@
request.success_url = 'https://testing.checkout.com/sucess'
request.failure_url = 'https://testing.checkout.com/failure'

expect { default_sdk.payments.request_payment(request) }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'payee_not_onboarded' }
response = default_sdk.payments.request_payment(request)
expect(response).not_to be nil
expect(response.id).not_to be nil
end
end

Expand Down Expand Up @@ -365,7 +366,7 @@
request.failure_url = 'https://testing.checkout.com/failure'

expect { default_sdk.payments.request_payment(request) }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'apm_service_unavailable' }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'custom_data_required' }
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/checkout_sdk/sessions/sessions_integration_spec.rb
Expand Up @@ -68,7 +68,7 @@
end
end

context 'when fetching inexistent session' do
context 'when fetching inexistent session', skip: 'returns Access denied status when updating' do
it 'raises an error' do
expect { oauth_sdk.sessions.get_session_details 'not_found' }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.http_metadata.status_code).to eq 404 }
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -11,6 +11,7 @@
require './spec/checkout_sdk/payments/previous/payments_helper'
require './spec/checkout_sdk/workflows/workflows_helper'
require './spec/checkout_sdk/webhooks/webhooks_helper'
require './spec/checkout_sdk/payments/contexts/contexts_helper'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
2 changes: 1 addition & 1 deletion spec/support/data_factory.rb
Expand Up @@ -42,7 +42,7 @@ def address
if @address.nil?
address = CheckoutSdk::Common::Address.new
address.address_line1 = 'CheckoutSdk.com'
address.address_line2 = '90 Tottenham Court Road'
address.address_line2 = '90'
address.city = 'London'
address.state = 'London'
address.zip = 'W1T 4TJ'
Expand Down

0 comments on commit 83d2bd5

Please sign in to comment.