Skip to content

Commit

Permalink
fixed code climate issues and failed test
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-soni1104 committed Mar 4, 2024
1 parent f06065f commit 4173d32
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
66 changes: 32 additions & 34 deletions app/lib/payment_gateways/stripe_crypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,17 @@ def customer
end

def update_billing_address(billing_address)
begin
Stripe.api_key = api_key # Set actual Stripe secret key

# Retrieve the latest payment method (card) for the customer
latest_payment_method = Stripe::PaymentMethod.list(
customer: customer.id,
type: 'card',
limit: 1
).data.first.id

# Retrieve the payment method
payment_method = Stripe::PaymentMethod.retrieve(latest_payment_method)

# Update the billing details
payment_method.billing_details = {
address: {
line1: billing_address[:address1],
line2: billing_address[:address2],
city: billing_address[:city],
state: billing_address[:state],
postal_code: billing_address[:zip],
country: billing_address[:country]
}
}
Stripe.api_key = api_key

# Save the updated payment method
payment_method.save
latest_payment_method = latest_payment_method_id
payment_method = Stripe::PaymentMethod.retrieve(latest_payment_method)

return true
rescue Stripe::StripeError => e
report_error("Failed to update billing address on Stripe: #{e.message}")
return false
ensure
# Reset the Stripe API key to avoid potential issues elsewhere in your code
Stripe.api_key = nil
end
update_billing_details(payment_method, billing_address)
rescue Stripe::StripeError => e
report_error("Failed to update billing address on Stripe: #{e.message}")
false
ensure
Stripe.api_key = nil
end

private
Expand Down Expand Up @@ -106,6 +81,29 @@ def api_key
payment_gateway_options.fetch(:login)
end

def latest_payment_method_id
Stripe::PaymentMethod.list(
customer: customer.id,
type: 'card',
limit: 1
).data.first&.id
end

def update_billing_details(payment_method, billing_address)
payment_method.billing_details = {
address: {
line1: billing_address[:address1],
line2: billing_address[:address2],
city: billing_address[:city],
state: billing_address[:state],
postal_code: billing_address[:zip],
country: billing_address[:country]
}
}

payment_method.save
end

def report_error(message)
@errors.add(:base, message)
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class DeveloperPortal::Admin::Account::PaymentDetailsBaseTest < ActionDispatch::

@buyer = FactoryBot.create(:buyer_account, provider_account: @provider.reload)
login_buyer @buyer

# Ensure that :login is set in payment_gateway_options
@gateway_setting = PaymentGatewaySetting.where(account_id: @provider).last
@gateway_setting.gateway_type = :stripe
@gateway_setting.gateway_settings = { login: 'Secret Key', publishable_key: '', endpoint_secret: 'some-secret' }
@gateway_setting.save
end

test '#update should only accept billing address params' do
Expand Down

0 comments on commit 4173d32

Please sign in to comment.