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

Alternate provider factory #121

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/alternate_provider_factory.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Alternate provider_factory
==========================

You might want to be able to provide the ``PAYMENT_VARIANTS`` on a different fashion, such as per entity in your DB.
Here's a quick exemple scenario, involving two DB entities for which provider settings has to be different:

Entity1 would have it's own PAYMENT_VARIANTS::

PAYMENT_VARIANTS = {
'stripe': ('payments.stripe.StripeProvider', {
'secret_key': 'entity1secretkey',
'public_key': 'entity1publickey'}),}

Entity2 also it's own, conflicting with Entity1::

PAYMENT_VARIANTS = {
'stripe': ('payments.stripe.StripeProvider', {
'secret_key': 'entity2secretkey',
'public_key': 'entity2publickey'}),}

How to solve this problem? We would be able to configure those values, per entity, through Django backend for exemple.


#. First define the alternate provider_factory method in the settings::

PAYMENTS_ALTERNATE_PROVIDER_FACTORY = 'mypaymentapp.utils.alternate_payments_provider_factory'

#. Then define and code your alternate method logic::
#mypaymentapp/utils.py
def alternate_payments_provider_factory(variant):
...

12 changes: 11 additions & 1 deletion payments/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ def provider_factory(variant):
'''
Return the provider instance based on variant
'''
variants = getattr(settings, 'PAYMENT_VARIANTS', PAYMENT_VARIANTS)
PROVIDER_FACTORY = getattr(
settings, 'PAYMENTS_ALTERNATE_PROVIDER_FACTORY', None)
if PROVIDER_FACTORY is None:
variants = getattr(settings, 'PAYMENT_VARIANTS', PAYMENT_VARIANTS)
else:
module_path, class_name = PROVIDER_FACTORY.rsplit('.', 1)
module = __import__(
str(module_path), globals(), locals(), [str(class_name)])
class_ = getattr(module, class_name)
return class_(variant)

handler, config = variants.get(variant, (None, None))
if not handler:
raise ValueError('Payment variant does not exist: %s' %
Expand Down