Skip to content

Commit

Permalink
refactor: add transformer to module system
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackfaded committed Sep 2, 2024
1 parent 2d76232 commit bc80f76
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
2 changes: 2 additions & 0 deletions lib/PCP-server-Ruby-SDK.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
# Utils
require_relative 'PCP-server-Ruby-SDK/utils/server_meta_info'

require_relative 'PCP-server-Ruby-SDK/transformer/apple_pay_transformer'

# Common
require_relative 'PCP-server-Ruby-SDK/communicator_configuration'
require_relative 'PCP-server-Ruby-SDK/request_header_generator'
Expand Down
53 changes: 30 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 @@ -11,29 +11,36 @@
require_relative '../models/payment_product320_specific_input'


# 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
module PCPServerSDK
module Transformer
class ApplePayTransformer
# 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

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
end
22 changes: 6 additions & 16 deletions spec/transformer/apple_pay_transformer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
require 'rspec'
require_relative '../../lib/PCP-server-Ruby-SDK/models/apple_payment_data_token_header_information'
require_relative '../../lib/PCP-server-Ruby-SDK/models/apple_payment_data_token_information'
require_relative '../../lib/PCP-server-Ruby-SDK/models/apple_payment_token_version'
require_relative '../../lib/PCP-server-Ruby-SDK/models/applepay/apple_pay_payment'
require_relative '../../lib/PCP-server-Ruby-SDK/models/applepay/apple_pay_payment_token'
require_relative '../../lib/PCP-server-Ruby-SDK/models/applepay/apple_pay_payment_data'
require_relative '../../lib/PCP-server-Ruby-SDK/models/applepay/apple_pay_payment_data_header'
require_relative '../../lib/PCP-server-Ruby-SDK/models/applepay/apple_pay_payment_method'
require_relative '../../lib/PCP-server-Ruby-SDK/models/mobile_payment_method_specific_input'
require_relative '../../lib/PCP-server-Ruby-SDK/models/network'
require_relative '../../lib/PCP-server-Ruby-SDK/models/payment_product320_specific_input'
require_relative '../../lib/PCP-server-Ruby-SDK/transformer/apple_pay_transformer'
require_relative '../../lib/PCP-server-Ruby-SDK.rb'

RSpec.describe 'transform_apple_pay_payment_to_mobile_payment_method_specific_input' do

RSpec.describe PCPServerSDK::Transformer::ApplePayTransformer do
let(:header) do
PCPServerSDK::Models::ApplePayPaymentDataHeader.new(
public_key_hash: 'publicKeyHash123',
Expand Down Expand Up @@ -50,7 +40,7 @@
end

it 'transforms ApplePayPayment to MobilePaymentMethodSpecificInput correctly' do
result = transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment)
result = PCPServerSDK::Transformer::ApplePayTransformer.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 +63,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::ApplePayTransformer.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::ApplePayTransformer.transform_apple_pay_payment_to_mobile_payment_method_specific_input(payment) }.to raise_error(TypeError)
end
end

0 comments on commit bc80f76

Please sign in to comment.