survival / donation-system

:gem: Gem containing Survival's donation system logic
https://donation-system-staging.herokuapp.com/
MIT License
2 stars 3 forks source link

Calculate the web payment number #26

Open octopusinvitro opened 6 years ago

octopusinvitro commented 6 years ago

See our contributing guides.

The old site calls payment.number, which is defined in the Payment model as:

  def generate_payment_number                
    return unless number.blank?
    record = true
    while record
      random = "P#{Array.new(9){rand(9)}.join}"                                        
      record = Payment.find(:first, :conditions => ["number = ?", random])
    end          
    self.number = random
  end
  def generate_mandate_number                
    return unless number.blank?
    record = true
    while record
      random = "#{number_prefix}#{Array.new(9){rand(9)}.join}"                                        
      record = Mandate.find(:first, :conditions => ["number = ?", random])
    end          
    self.number = random
  end   

Credit card mandate:

  def number_prefix
    "MC"
  end

Direct debit mandate:

  def number_prefix
    "MD"
  end

Generic direct debit mandate:

  def number_prefix
    "MG"
  end

Paypal mandate:

  def number_prefix
    "MP"
  end

Spendenbank direct debit mandate:

  def number_prefix
    "MS"
  end

Investigate why it needs this format and if there is a better way to implement this code. Implement this in the StripeWrapper::FieldsGenerator class

octopusinvitro commented 6 years ago

At the moment this gem calculates the number just generating the random last 9 numbers plus the prefix.

      def generate_number(number_prefix)
        number_prefix + Array.new(9) { Random.rand(9) }.join
      end

The original code searches in the database to check that the generated code is not repeated. Rather than opening yet another connection to Salesforce to look up the payment number, coupling a Salesforce class with the generator, a simplest solution could be to use a text file containing all numbers used to date (this can be downloaded from the CMS finantials).

P123456789
MC123456789
... etc.

Everytime the generator generates a number it reads from this file to check that it is not in use. Then the generator or another class upstream could write down the new number if the payment went through successfully.

We would also need a file for development probably.