How to rescue Mangopay error caused by incorrect Iban or Bic

362 Views Asked by At

Hi I installed Mangopay payment solution on my rails app. Even though I check the user's Iban and Bic when they update their profile with regular expressions, sometimes when I send the data to Mangopay I get an error message (I guess because even though iban's and bic have a correct format, mangopay finds that they dont actually exist): "One or several required parameters are missing or incorrect. An incorrect resource ID also raises this kind of error. IBAN: IBAN or BIC not valid"

My code for sending theses infos to Mangopay looks like this:

  def create_mangopay_bank_account
    bank_account = MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
    current_user.bank_account_id = bank_account["Id"]
    current_user.save
  end

How can I rescue this error ? I tried something like:

 def create_mangopay_bank_account
    if MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
      bank_account = MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
      current_user.bank_account_id = bank_account["Id"]
      current_user.save
    else
      redirect_to user_path
  end

but it doesnt work...

2

There are 2 best solutions below

1
David Geismar On

Found the solution :

 def create_mangopay_bank_account

      bank_account = MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
      current_user.bank_account_id = bank_account["Id"]
      current_user.save
      rescue MangoPay::ResponseError => e
        redirect_to root_path
        flash[:alert] = "L'Iban ou le Bic que vous avez fourni n'est pas valide. Veuillez vérifier les informations fournies. Si le problème persiste n'hésitez pas à contacter l'équipe TennisMatch."

   end

calling rescue enables to deal with the error from mangopay

0
Asim Adnan On

Here's how I do it, by enclosing the code ins a begin rescue block.

    def create_mangopay_bank_account 
        begin
         MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
          bank_account = MangoPay::BankAccount.create(current_user.mangopay_natural_user_id, mangopay_user_bank_attributes)
          current_user.bank_account_id = bank_account["Id"]
          current_user.save
        rescue MangoPay::ResponseError => exception
            #handle your error here
          redirect_to user_path
        end
    end