Skip to content

Commit

Permalink
Add Klarna Payment Context Support (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko authored Apr 24, 2024
1 parent 21e48fe commit 8e04613
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
10 changes: 6 additions & 4 deletions spec/checkout_sdk/payments/contexts/contexts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module ContextsHelper

def create_payment_contexts
def create_payment_contexts_paypal
request = {
'source' => {
'type' => 'paypal'
},
'amount' => 2000,
'amount' => 1000,
'currency' => CheckoutSdk::Common::Currency::EUR,
'payment_type' => CheckoutSdk::Payments::PaymentType::REGULAR,
'capture' => true,
Expand All @@ -17,15 +17,17 @@ def create_payment_contexts
'items' => [
{
'name' => 'mask',
'unit_price' => 2000,
'quantity' => 1
'unit_price' => 1000,
'quantity' => 1,
'total_amount' => 1000
}
]
}

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

Expand Down
50 changes: 42 additions & 8 deletions spec/checkout_sdk/payments/contexts/contexts_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,50 @@
include ContextsHelper

before(:all) do
@payment_context = create_payment_contexts
@payment_context_paypal = create_payment_contexts_paypal
end

describe '.create_payment_contexts' do
context 'when creating a payment contexts with valid data' do
it { is_valid_payment_context @payment_context }
context 'when creating a payment contexts paypal with valid data' do
it { is_valid_payment_context @payment_context_paypal }
end
context 'when creating a payment contexts klarna with valid data' do
it 'raises an error (apm_service_unavailable)' do
request = {
'source' => {
'type' => 'klarna',
'account_holder' => {
'billing_address' => {
'country' => 'DE'
}
}
},
'amount' => 1000,
'currency' => CheckoutSdk::Common::Currency::EUR,
'payment_type' => CheckoutSdk::Payments::PaymentType::REGULAR,
'processing_channel_id' => ENV.fetch('CHECKOUT_PROCESSING_CHANNEL_ID', nil),
'items' => [
{
'name' => 'mask',
'unit_price' => 1000,
'quantity' => 1,
'total_amount' => 1000,
'reference' => 'BA67A'
}
],
'processing' => {
'locale' => 'en-GB'
}
}
expect { default_sdk.contexts.create_payment_contexts(request) }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.error_details[:error_codes].first).to eq 'apm_service_unavailable' }
end
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 }
it { is_valid_payment_context_details default_sdk.contexts.get_payment_context_details @payment_context_paypal.id }
end
end
end
Expand All @@ -27,23 +59,25 @@ def is_valid_payment_context(payment_context)
end

def is_valid_payment_context_details(payment_context_details_response)
assert_response payment_context_details_response, %w[payment_request
assert_response payment_context_details_response, %w[status
payment_request
payment_request.source
payment_request.source.type
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.amount).to eq 1000
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].unit_price).to eq 1000
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'
Expand Down

0 comments on commit 8e04613

Please sign in to comment.