Skip to content

Commit

Permalink
COMP-71 AP recurring braintree_blue (#5336)
Browse files Browse the repository at this point in the history
* COMP-71 AP recurring braintree_blue

* COMP-71 Fix applied review suggestions

* COMP-71 Fix applied test suggestion

* changelog

---------

Co-authored-by: aenand <[email protected]>
  • Loading branch information
bdcano and aenand authored Nov 26, 2024
1 parent c289403 commit 90bc1c7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* Update Rubocop 1.26.0 [almalee24] #5325
* RedsysREST: Add Network Tokens [gasb150] #5333
* DLocal: Update the success_from for Void [almalee24] #5337
* Braintree: Add support for stored credentials with Apple Pay [bdcano] #5336

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
30 changes: 20 additions & 10 deletions lib/active_merchant/billing/gateways/braintree_blue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def add_payment_method(parameters, credit_card_or_vault_id, options)
if credit_card_or_vault_id.is_a?(NetworkTokenizationCreditCard)
case credit_card_or_vault_id.source
when :apple_pay
add_apple_pay(parameters, credit_card_or_vault_id)
add_apple_pay(parameters, credit_card_or_vault_id, options)
when :google_pay
add_google_pay(parameters, credit_card_or_vault_id)
else
Expand Down Expand Up @@ -991,15 +991,25 @@ def add_credit_card(parameters, payment_method)
}
end

def add_apple_pay(parameters, payment_method)
parameters[:apple_pay_card] = {
number: payment_method.number,
expiration_month: payment_method.month.to_s.rjust(2, '0'),
expiration_year: payment_method.year.to_s,
cardholder_name: payment_method.name,
cryptogram: payment_method.payment_cryptogram,
eci_indicator: payment_method.eci
}
def add_apple_pay(parameters, payment_method, options)
if options.dig(:stored_credential, :initiator) == 'merchant'
parameters[:apple_pay_card] = {
number: payment_method.number,
expiration_month: payment_method.month.to_s.rjust(2, '0'),
expiration_year: payment_method.year.to_s,
cardholder_name: payment_method.name,
cryptogram: 'cryptogram'
}
else
parameters[:apple_pay_card] = {
number: payment_method.number,
expiration_month: payment_method.month.to_s.rjust(2, '0'),
expiration_year: payment_method.year.to_s,
cardholder_name: payment_method.name,
cryptogram: payment_method.payment_cryptogram,
eci_indicator: payment_method.eci
}
end
end

def add_google_pay(parameters, payment_method)
Expand Down
32 changes: 32 additions & 0 deletions test/remote/gateways/remote_braintree_blue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,38 @@ def test_successful_apple_pay_purchase_with_prepaid_debit_issuing_bank
assert_equal 'Unknown', response.params['braintree_transaction']['apple_pay_details']['issuing_bank']
end

def test_successful_apple_pay_recurring_purchase
network_tokenized_credit_card = network_tokenization_credit_card(
'4111111111111111',
brand: 'visa',
transaction_id: '123',
source: :apple_pay,
payment_cryptogram: 'EHuWW9PiBkWvqE5juRwDzAUFBAk='
)
apple_pay_options = @options.merge(stored_credential: { initiator: 'customer', reason_type: 'recurring' })

assert response = @gateway.purchase(@amount, network_tokenized_credit_card, apple_pay_options)
assert_success response
assert_equal true, response.params['braintree_transaction']['recurring']
assert_equal 'apple_pay_card', response.params['braintree_transaction']['payment_instrument_type']
end

def test_successful_apple_pay_recurring_purchase_mit
network_tokenized_credit_card = network_tokenization_credit_card(
'4111111111111111',
brand: 'visa',
transaction_id: '123',
source: :apple_pay,
payment_cryptogram: 'EHuWW9PiBkWvqE5juRwDzAUFBAk='
)
apple_pay_options = @options.merge(stored_credential: { initiator: 'merchant', reason_type: 'recurring' })

assert response = @gateway.purchase(@amount, network_tokenized_credit_card, apple_pay_options)
assert_success response
assert_equal true, response.params['braintree_transaction']['recurring']
assert_equal 'apple_pay_card', response.params['braintree_transaction']['payment_instrument_type']
end

def test_unsuccessful_apple_pay_purchase_and_return_payment_details
credit_card = network_tokenization_credit_card(
'4111111111111111',
Expand Down
36 changes: 36 additions & 0 deletions test/unit/gateways/braintree_blue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,42 @@ def test_apple_pay_card
assert_equal 'transaction_id', response.authorization
end

def test_apple_pay_card_recurring
Braintree::TransactionGateway.any_instance.expects(:sale).
with(
amount: '1.00',
order_id: '1',
customer: { id: nil, email: nil, phone: nil,
first_name: 'Longbob', last_name: 'Longsen' },
options: { store_in_vault: false, submit_for_settlement: nil, hold_in_escrow: nil },
custom_fields: nil,
apple_pay_card: {
number: '4111111111111111',
expiration_month: '09',
expiration_year: (Time.now.year + 1).to_s,
cardholder_name: 'Longbob Longsen',
cryptogram: 'cryptogram'
},
external_vault: {
status: 'vaulted',
previous_network_transaction_id: '123ABC'
},
transaction_source: 'recurring'
).
returns(braintree_result(id: 'transaction_id'))

apple_pay = network_tokenization_credit_card(
'4111111111111111',
brand: 'visa',
transaction_id: '123',
payment_cryptogram: 'some_other_value',
source: :apple_pay
)

response = @gateway.authorize(100, apple_pay, { test: true, order_id: '1', stored_credential: stored_credential(:merchant, :recurring, id: '123ABC') })
assert_equal 'transaction_id', response.authorization
end

def test_google_pay_card
Braintree::TransactionGateway.any_instance.expects(:sale).
with(
Expand Down

0 comments on commit 90bc1c7

Please sign in to comment.