From 59a48722d567cd6459a90a7f53158f33517aa9ce Mon Sep 17 00:00:00 2001 From: Lukas Rosenfeldt Date: Mon, 2 Sep 2024 16:04:40 +0200 Subject: [PATCH] refactor(apple-pay-transformer): move transform method into namespace --- .../transformer/apple_pay_transformer.rb | 49 ++++++++++--------- .../transformer/apple_pay_transformer_spec.rb | 6 +-- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer.rb b/lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer.rb index 4240d71..c694e72 100644 --- a/lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer.rb +++ b/lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer.rb @@ -10,30 +10,33 @@ require_relative '../models/network' require_relative '../models/payment_product320_specific_input' +module PCPServerSDK + module Transformer + # Transforms an ApplePayPayment to a MobilePaymentMethodSpecificInput + # @param [PCPServerSDK::Models::ApplePayPayment] payment + # @return [PCPServerSDK::Models::MobilePaymentMethodSpecificInput] + def self.transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) + token = payment.token || PCPServerSDK::Models::ApplePayPaymentToken.new + payment_data = token.payment_data || PCPServerSDK::Models::ApplePayPaymentData.new + header = payment_data.header || PCPServerSDK::Models::ApplePayPaymentDataHeader.new + payment_method = token.payment_method || PCPServerSDK::Models::ApplePayPaymentMethod.new -# Transforms an ApplePayPayment to a MobilePaymentMethodSpecificInput -# @param [PCPServerSDK::Models::ApplePayPayment] payment -# @return [PCPServerSDK::Models::MobilePaymentMethodSpecificInput] -def transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) - token = payment.token || PCPServerSDK::Models::ApplePayPaymentToken.new - payment_data = token.payment_data || PCPServerSDK::Models::ApplePayPaymentData.new - header = payment_data.header || PCPServerSDK::Models::ApplePayPaymentDataHeader.new - payment_method = token.payment_method || PCPServerSDK::Models::ApplePayPaymentMethod.new - - PCPServerSDK::Models::MobilePaymentMethodSpecificInput.new( - payment_product_id: 302, - public_key_hash: header.public_key_hash, - ephemeral_key: header.ephemeral_public_key, - payment_product302_specific_input: PCPServerSDK::Models::PaymentProduct320SpecificInput.new( - network: PCPServerSDK::Models::Network.from_string(payment_method.network.to_s), - token: PCPServerSDK::Models::ApplePaymentDataTokenInformation.new( - version: PCPServerSDK::Models::ApplePaymentTokenVersion.from_string(payment_data.version.to_s), - signature: payment_data.signature, - header: PCPServerSDK::Models::ApplePaymentDataTokenHeaderInformation.new( - transaction_id: header.transaction_id, - application_data: header.application_data + PCPServerSDK::Models::MobilePaymentMethodSpecificInput.new( + payment_product_id: 302, + public_key_hash: header.public_key_hash, + ephemeral_key: header.ephemeral_public_key, + payment_product302_specific_input: PCPServerSDK::Models::PaymentProduct320SpecificInput.new( + network: PCPServerSDK::Models::Network.from_string(payment_method.network.to_s), + token: PCPServerSDK::Models::ApplePaymentDataTokenInformation.new( + version: PCPServerSDK::Models::ApplePaymentTokenVersion.from_string(payment_data.version.to_s), + signature: payment_data.signature, + header: PCPServerSDK::Models::ApplePaymentDataTokenHeaderInformation.new( + transaction_id: header.transaction_id, + application_data: header.application_data + ) + ) ) ) - ) - ) + end + end end diff --git a/spec/transformer/apple_pay_transformer_spec.rb b/spec/transformer/apple_pay_transformer_spec.rb index 98a5f20..91c14d8 100644 --- a/spec/transformer/apple_pay_transformer_spec.rb +++ b/spec/transformer/apple_pay_transformer_spec.rb @@ -50,7 +50,7 @@ end it 'transforms ApplePayPayment to MobilePaymentMethodSpecificInput correctly' do - result = transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) + result = PCPServerSDK::Transformer::transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) expected_result = PCPServerSDK::Models::MobilePaymentMethodSpecificInput.new( payment_product_id: 302, @@ -73,11 +73,11 @@ it 'raises an error for unknown network' do payment.token.payment_method.network = 'UNKNOWN' - expect { transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) }.to raise_error(TypeError) + expect { PCPServerSDK::Transformer::transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) }.to raise_error(TypeError) end it 'raises an error for unknown version' do payment.token.payment_data.version = 'UNKNOWN' - expect { transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) }.to raise_error(TypeError) + expect { PCPServerSDK::Transformer::transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) }.to raise_error(TypeError) end end