Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using multiple merchant accounts in a single project #66

Open
rubish opened this issue Jun 25, 2018 · 2 comments
Open

Allow using multiple merchant accounts in a single project #66

rubish opened this issue Jun 25, 2018 · 2 comments
Assignees

Comments

@rubish
Copy link

rubish commented Jun 25, 2018

Its convenient that all the API calls and entities of razorpay can be accessed using class methods, but the design makes heavy use of class variables. Class variables should be frowned upon because they are not thread safe. Moreover, despite everything implemented as classes; usage is mostly as if these were modules. Currently no instances can be created which can be configured at runtime.

There can be many different ways to implement thread safe code, which can be configured at runtime. One such implementation can be as follows:

class Razorpay::Client
  attr_accessor :auth, :custom_headers
  def initialize(auth=nil, custom_headers=nil)
    self.auth = auth || Razorpay.auth
    self.custom_header = custom_headers || Razorpay.custom_headers
  end

  def self.payment
    new.payment
  end

  def self.refund
    new.refund
  end

  # ...... other methods

  def payment
     @payment ||= Razorpay::Payment.new(self)
  end
  
  def refund
    @refund ||= Razorpay::Refund.new(self)
  end

  # ..... other similar methods
end

class Razorpay::Payment
  attr_accessor :request
  def initialize(client=nil)
    self.request = Razorpay::Request.new(client || Razorpay::Client.new, "payments")
  end

  def fetch(id)
    self.request.fetch id
  end

  # ..... other methods
end

class Request
  def initialize(client=nil, entity_name=nil)
    # similar to current implementation, but use client.auth and client.custom_headers, instead of Razorpay.auth and Razorpay.custom_headers
  end

  # ... other request methods
end



##### Basic usage #####
Razorpay::Client.payment.fetch(id)


##### Advanced usage with multiple merchant accounts #####
account1 = Razorpay::Client.new(auth1)
account1.payment.fetch(id)

account2 = Razorpay::Client.new(auth2)
account2.payment.fetch(id)

I am open to feedback and other suggestions to be able use multiple merchant accounts in a single project.

@harman28
Copy link
Contributor

Thanks for the suggestion @rubish! This is actually a pretty sound idea, and we would like to implement it. It would naturally involve a bit of a refactor and new major release, but it seems worth it. We're not going to be picking this up immediately, but you can feel free to raise a pull request if you're interested in working on this!

@megatux
Copy link

megatux commented Jun 8, 2022

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants