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

Commit

Permalink
Merge pull request #15 from WhyNotHugo/multiple-items
Browse files Browse the repository at this point in the history
Add support for multiple items per preference
  • Loading branch information
Hugo Osvaldo Barrera authored Oct 13, 2019
2 parents dbdd0a4 + 6544120 commit 749398b
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 142 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

v6.0.0
------

* Add support for multiple items per preference.

v5.1.2
------

Expand Down
21 changes: 16 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,21 @@ Usage
MercadoPago lets you create preferences, for which you'll later receive
notifications (indicating if it was paid, or what happened)::

self.preference = Preference.objects.create(
title='the product name',
price=10.0,
preference = Preference.objects.create(
owner=my_account,
reference='order-38452',
success_url='http://example.com/mp_done',
account=account,
)

item = Item.objects.create(
preference=preference,
title='Candy box',
quanityty=2,
unit_price=10.0,
)

preference.submit()


If your app will only be using a single MercadoPago account, just use::

account = Account.objects.first()
Expand All @@ -172,6 +179,10 @@ To complete a full payment flow, you'd:
Backwards compatibility
-----------------------

Version v6.0.0 adds supports for multiple items and changes the preference
creation interface. Preferences and their Items must be paid manually, and then
``Preference.submit()`` must be called.

As of v5.0.0, the notification and callback URL formats generated by v4.2.0 and
earlier is no longer supported. Users must upgrade to v4.3.0, and run this
version until all pending payments are completed (or expire), and only then
Expand Down
14 changes: 12 additions & 2 deletions django_mercadopago/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class Meta:
model = models.Preference

owner = SubFactory(AccountFactory)
title = Sequence(lambda n: 'preference_%d' % n)
price = 120
mp_id = '2hLhPNlP3DJvMW48dAYn'
payment_url = 'http://localhost/post_payment'
sandbox_url = 'http://localhost:8000/post_payment'
Expand All @@ -36,6 +34,18 @@ def _create(cls, model_class, *args, **kwargs):
return manager.create(*args, **kwargs)


class ItemFactory(DjangoModelFactory):
class Meta:
model = models.Item

preference = SubFactory(PreferenceFactory)
title = Sequence(lambda n: 'preference_%d' % n)
currency_id = 'ARS'
description = 'A nice, high quality product.'
quantity = 1
unit_price = 120


class PaymentFactory(DjangoModelFactory):
class Meta:
model = models.Payment
Expand Down
79 changes: 79 additions & 0 deletions django_mercadopago/migrations/0019_split_preference_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mp', '0018_payment_mpid_biginteger'),
]

operations = [
migrations.CreateModel(
name='Item',
fields=[
('id',
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
)),
(
'title',
models.CharField(
max_length=256,
verbose_name='title',
),
),
(
'currency_id',
models.CharField(
default='ARS',
max_length=3,
verbose_name='currency id',
),
),
(
'description',
models.CharField(
max_length=256,
verbose_name='description',
),
),
(
'quantity',
models.PositiveSmallIntegerField(default=1),
),
(
'unit_price',
models.DecimalField(decimal_places=2, max_digits=9),
),
],
options={
'verbose_name': 'item',
'verbose_name_plural': 'items',
},
),
migrations.AlterField(
model_name='preference',
name='mp_id',
field=models.CharField(
help_text=(
'The id MercadoPago has assigned for this Preference'),
max_length=46,
null=True,
verbose_name='mercadopago id',
),
),
migrations.AddField(
model_name='item',
name='preference',
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name='items',
to='mp.Preference',
verbose_name='preference',
),
),
]
27 changes: 27 additions & 0 deletions django_mercadopago/migrations/0020_move_preference_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.db import migrations


def move_item_data(apps, schema_editor):
"""Move Item data from Preference into the new model."""
Preference = apps.get_model('mp', 'Preference')
Item = apps.get_model('mp', 'Item')

for preference in Preference.objects.all():
Item.objects.create(
preference=preference,
title=preference.title,
description=preference.description,
quantity=preference.quantity,
unit_price=preference.price,
)


class Migration(migrations.Migration):

dependencies = [
('mp', '0019_split_preference_items'),
]

operations = [
migrations.RunPython(move_item_data),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.1.5 on 2019-03-11 16:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('mp', '0020_move_preference_items'),
]

operations = [
migrations.RemoveField(
model_name='preference',
name='price',
),
migrations.RemoveField(
model_name='preference',
name='quantity',
),
migrations.RemoveField(
model_name='preference',
name='title',
),
]
Loading

0 comments on commit 749398b

Please sign in to comment.