Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Could the MercadoPagoService class be customizable through settings.py ? #21

Open
pwqw opened this issue Nov 4, 2019 · 5 comments
Open

Comments

@pwqw
Copy link
Contributor

pwqw commented Nov 4, 2019

class MercadoPagoService(MP):

I need to personalize some of this..

@pwqw pwqw changed the title Could the MercadoPagoService class be customizable through settings.py Could the MercadoPagoService class be customizable through settings.py ? Nov 4, 2019
@WhyNotHugo
Copy link
Owner

WhyNotHugo commented Nov 4, 2019 via email

@pwqw
Copy link
Contributor Author

pwqw commented Jan 21, 2020

This issue is related to a capability for create a MercadoPago Marketplace, in my case.

First of all, I need to use another token (obtained from a link to my account with the account of a third party).

The most practical, it would be to be able to use a custom class, set from the settings.py, as most libraries do.

Alternatively,
I show you my solution, an inject-wrapper:

def apply_monkeypatch_to_mp_account(mp_account):
    def get_service(self):
        service = MP(self.third_party_related_account.mp_access_token)
        service.sandbox_mode(self.sandbox)
        return service
    setattr(mp_account.__class__, 'service', property(get_service))
    return mp_account

But.. I still can't size the amount of possibilities that would open just by admitting this configuration.

@WhyNotHugo
Copy link
Owner

Including it in settings is problematic since it would override all accounts in all environments (so payments made to any account would all go to the same one).

I've been looking at the docs, and it seems the simplest clean approach would be to:

  • Include an is_marketplace flag in Account.

  • Include a redirect_url field in Account.

  • Include a marketplace field in Account, to link subaccounts to a parent marketplace.

  • For accounts that have a parent marketplace, add logic to fetch the code using the MP flow.

  • Add an endpoint to receive the code at the end of the flow, and exchange it for access tokens.

  • Access tokens need to be store in the DB, since they can't be re-fetched. It might also make sense to save token for normal accounts too, since right now we fetch them way too often and could throttle down on this.

I'll start fiddling with some bits of this. Does the general idea make sense to you? Do you see any problems with it, or me missing anything?

@pwqw
Copy link
Contributor Author

pwqw commented Jan 22, 2020

Then it would be necessary to implement the task for the token refresh, which expires every 6 months.
If it serves you as data, I am currently saving all this:

class MyAccount(models.Model):
    mp_account = models.OneToOneField(
        django_mercadopago.models.Account,
        on_delete=models.CASCADE,
        related_name='wallet_account',
        blank=True,
        null=True,
    )
    mp_public_key = models.CharField(
        verbose_name='Clave pública',
        help_text='La "Public key" dada por Mercado Pago',
        max_length=255,
        blank=True,
        null=True,
    )
    owner = models.ForeignKey(
        User,
        help_text='A quién le corresponde esta cuenta de Mercado Pago',
        related_name='accounts',
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )
    mp_user_id = models.IntegerField(
        verbose_name='Mercado Pago user_id',
        blank=True,
        null=True,
    )
    mp_access_token = models.CharField(
        verbose_name='Token de acceso',
        help_text='Token de acceso para marketplace',
        max_length=255,
        blank=True,
        null=True,
    )
    mp_token_type = models.CharField(
        verbose_name='Tipo de token',
        help_text='Tipo de access token',
        max_length=32,
        blank=True,
        null=True,
    )
    mp_expires_in = models.PositiveIntegerField(
        verbose_name='Expira en',
        help_text='Tiempo que expira el token',
        blank=True,
        null=True,
    )
    mp_scope = models.CharField(
        verbose_name='Permisos del token',
        help_text='Accesos brindados por el marketplace',
        max_length=255,
        blank=True,
        null=True,
    )
    mp_refresh_token = models.CharField(
        verbose_name='Token de refresco',
        help_text='Token para actualizar el token de acceso. Cada 6 meses se vence.',
        max_length=255,
        blank=True,
        null=True,
    )

@WhyNotHugo
Copy link
Owner

Haciéndo eso sigue siendo compatible con no-marketplaces?

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

Successfully merging a pull request may close this issue.

2 participants