Skip to content

Commit

Permalink
refactor(apple-pay-transformer): move transform method into namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
lrosenfeldt committed Sep 2, 2024
1 parent fa60da4 commit 59a4872
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
49 changes: 26 additions & 23 deletions lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions spec/transformer/apple_pay_transformer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit 59a4872

Please sign in to comment.