Skip to content

Commit 83d2bd5

Browse files
Adds request and get payment contexts endpoints (#129)
1 parent 9f65b92 commit 83d2bd5

File tree

13 files changed

+125
-7
lines changed

13 files changed

+125
-7
lines changed

.github/workflows/build-master.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
run: bundle exec rubocop lib
3131
- id: build-and-analyse
3232
env:
33+
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
3334
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
3435
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
3536
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}

.github/workflows/build-pull-request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
run: bundle exec rubocop lib
3131
- id: build-and-analyse
3232
env:
33+
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
3334
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
3435
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
3536
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}

.github/workflows/build-release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
2020
- id: build-and-analyse
2121
env:
22+
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
2223
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
2324
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
2425
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}

lib/checkout_sdk/api_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def upload(path, authorization, file_request)
127127
end
128128

129129
def parse_response(response)
130-
raise CheckoutApiException, response if response.status < 200 || response.status >= 300
130+
raise CheckoutApiException, response if response.status < 200 || response.status >= 400
131131

132132
metadata = CheckoutUtils.map_to_http_metadata(response)
133133
body = parse_json_or_contents(response)

lib/checkout_sdk/checkout_api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class CheckoutApi
5858
:transfers,
5959
:metadata,
6060
:financial,
61-
:issuing
61+
:issuing,
62+
:contexts
6263

6364
# @param [CheckoutConfiguration] configuration
6465
def initialize(configuration)
@@ -82,6 +83,7 @@ def initialize(configuration)
8283
@metadata = CheckoutSdk::Metadata::MetadataClient.new api_client, configuration
8384
@financial = CheckoutSdk::Financial::FinancialClient.new api_client, configuration
8485
@issuing = CheckoutSdk::Issuing::IssuingClient.new api_client, configuration
86+
@contexts = CheckoutSdk::Payments::PaymentContextsClient.new api_client, configuration
8587
end
8688

8789
private
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module CheckoutSdk
4+
module Payments
5+
class PaymentContextsClient < Client
6+
PAYMENT_CONTEXTS = 'payment-contexts'
7+
8+
# @param [ApiClient] api_client
9+
# @param [CheckoutConfiguration] configuration
10+
def initialize(api_client, configuration)
11+
super api_client, configuration, CheckoutSdk::AuthorizationType::SECRET_KEY_OR_OAUTH
12+
end
13+
14+
# @param [Hash] payment_contexts
15+
def create_payment_contexts(payment_contexts)
16+
api_client.invoke_post(PAYMENT_CONTEXTS, sdk_authorization, payment_contexts)
17+
end
18+
19+
# @param [String] payment_context_id
20+
def get_payment_context_details(payment_context_id)
21+
api_client.invoke_get(build_path(PAYMENT_CONTEXTS, payment_context_id), sdk_authorization)
22+
end
23+
end
24+
end
25+
end

lib/checkout_sdk/payments/payments.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@
155155
# Payment Links
156156
require 'checkout_sdk/payments/links/payment_link'
157157
require 'checkout_sdk/payments/links/payments_links_client'
158+
159+
# Payment Contexts
160+
require 'checkout_sdk/payments/contexts/payment_contexts_client'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module ContextsHelper
4+
5+
def create_payment_contexts
6+
request = {
7+
'source' => {
8+
'type' => 'paypal'
9+
},
10+
'amount' => 2000,
11+
'currency' => CheckoutSdk::Common::Currency::EUR,
12+
'payment_type' => CheckoutSdk::Payments::PaymentType::REGULAR,
13+
'capture' => true,
14+
'processing_channel_id' => ENV.fetch('CHECKOUT_PROCESSING_CHANNEL_ID', nil),
15+
'success_url' => 'https://example.com/payments/success',
16+
'failure_url' => 'https://example.com/payments/fail',
17+
'items' => [
18+
{
19+
'name' => 'mask',
20+
'unit_price' => 2000,
21+
'quantity' => 1
22+
}
23+
]
24+
}
25+
26+
response = default_sdk.contexts.create_payment_contexts(request)
27+
expect(response).not_to be nil
28+
expect(response.id).not_to be nil
29+
response
30+
end
31+
32+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe CheckoutSdk::Payments do
4+
include ContextsHelper
5+
6+
before(:all) do
7+
@payment_context = create_payment_contexts
8+
end
9+
10+
describe '.create_payment_contexts' do
11+
context 'when creating a payment contexts with valid data' do
12+
it { is_valid_payment_context @payment_context }
13+
end
14+
end
15+
16+
describe '.get_payment_context_details' do
17+
context 'when retrieving existing payment context details' do
18+
it { is_valid_payment_context_details default_sdk.contexts.get_payment_context_details @payment_context.id }
19+
end
20+
end
21+
end
22+
23+
def is_valid_payment_context(payment_context)
24+
assert_response payment_context, %w[id
25+
partner_metadata
26+
partner_metadata.order_id]
27+
end
28+
29+
def is_valid_payment_context_details(payment_context_details_response)
30+
assert_response payment_context_details_response, %w[payment_request
31+
payment_request.amount
32+
payment_request.currency
33+
payment_request.payment_type
34+
payment_request.capture
35+
payment_request.items
36+
payment_request.success_url
37+
payment_request.failure_url
38+
partner_metadata
39+
partner_metadata.order_id]
40+
41+
expect(payment_context_details_response.payment_request.amount).to eq 2000
42+
expect(payment_context_details_response.payment_request.currency).to eq CheckoutSdk::Common::Currency::EUR
43+
expect(payment_context_details_response.payment_request.payment_type).to eq CheckoutSdk::Payments::PaymentType::REGULAR
44+
expect(payment_context_details_response.payment_request.capture).to eq true
45+
expect(payment_context_details_response.payment_request.items[0].name).to eq 'mask'
46+
expect(payment_context_details_response.payment_request.items[0].unit_price).to eq 2000
47+
expect(payment_context_details_response.payment_request.items[0].quantity).to eq 1
48+
expect(payment_context_details_response.payment_request.success_url).to eq 'https://example.com/payments/success'
49+
expect(payment_context_details_response.payment_request.failure_url).to eq 'https://example.com/payments/fail'
50+
expect(payment_context_details_response.partner_metadata.order_id).not_to be_nil
51+
end

spec/checkout_sdk/payments/request_apm_payments_integration_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
request.success_url = 'https://testing.checkout.com/sucess'
5252
request.failure_url = 'https://testing.checkout.com/failure'
5353

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

@@ -365,7 +366,7 @@
365366
request.failure_url = 'https://testing.checkout.com/failure'
366367

367368
expect { default_sdk.payments.request_payment(request) }
368-
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'apm_service_unavailable' }
369+
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'custom_data_required' }
369370
end
370371
end
371372

0 commit comments

Comments
 (0)