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

Commit 749398b

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request #15 from WhyNotHugo/multiple-items
Add support for multiple items per preference
2 parents dbdd0a4 + 6544120 commit 749398b

File tree

8 files changed

+292
-142
lines changed

8 files changed

+292
-142
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
v6.0.0
5+
------
6+
7+
* Add support for multiple items per preference.
8+
49
v5.1.2
510
------
611

README.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,21 @@ Usage
138138
MercadoPago lets you create preferences, for which you'll later receive
139139
notifications (indicating if it was paid, or what happened)::
140140

141-
self.preference = Preference.objects.create(
142-
title='the product name',
143-
price=10.0,
141+
preference = Preference.objects.create(
142+
owner=my_account,
144143
reference='order-38452',
145-
success_url='http://example.com/mp_done',
146-
account=account,
147144
)
148145

146+
item = Item.objects.create(
147+
preference=preference,
148+
title='Candy box',
149+
quanityty=2,
150+
unit_price=10.0,
151+
)
152+
153+
preference.submit()
154+
155+
149156
If your app will only be using a single MercadoPago account, just use::
150157

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

182+
Version v6.0.0 adds supports for multiple items and changes the preference
183+
creation interface. Preferences and their Items must be paid manually, and then
184+
``Preference.submit()`` must be called.
185+
175186
As of v5.0.0, the notification and callback URL formats generated by v4.2.0 and
176187
earlier is no longer supported. Users must upgrade to v4.3.0, and run this
177188
version until all pending payments are completed (or expire), and only then

django_mercadopago/fixtures.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Meta:
2323
model = models.Preference
2424

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

3836

37+
class ItemFactory(DjangoModelFactory):
38+
class Meta:
39+
model = models.Item
40+
41+
preference = SubFactory(PreferenceFactory)
42+
title = Sequence(lambda n: 'preference_%d' % n)
43+
currency_id = 'ARS'
44+
description = 'A nice, high quality product.'
45+
quantity = 1
46+
unit_price = 120
47+
48+
3949
class PaymentFactory(DjangoModelFactory):
4050
class Meta:
4151
model = models.Payment
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import django.db.models.deletion
2+
from django.db import migrations, models
3+
4+
5+
class Migration(migrations.Migration):
6+
7+
dependencies = [
8+
('mp', '0018_payment_mpid_biginteger'),
9+
]
10+
11+
operations = [
12+
migrations.CreateModel(
13+
name='Item',
14+
fields=[
15+
('id',
16+
models.AutoField(
17+
auto_created=True,
18+
primary_key=True,
19+
serialize=False,
20+
verbose_name='ID',
21+
)),
22+
(
23+
'title',
24+
models.CharField(
25+
max_length=256,
26+
verbose_name='title',
27+
),
28+
),
29+
(
30+
'currency_id',
31+
models.CharField(
32+
default='ARS',
33+
max_length=3,
34+
verbose_name='currency id',
35+
),
36+
),
37+
(
38+
'description',
39+
models.CharField(
40+
max_length=256,
41+
verbose_name='description',
42+
),
43+
),
44+
(
45+
'quantity',
46+
models.PositiveSmallIntegerField(default=1),
47+
),
48+
(
49+
'unit_price',
50+
models.DecimalField(decimal_places=2, max_digits=9),
51+
),
52+
],
53+
options={
54+
'verbose_name': 'item',
55+
'verbose_name_plural': 'items',
56+
},
57+
),
58+
migrations.AlterField(
59+
model_name='preference',
60+
name='mp_id',
61+
field=models.CharField(
62+
help_text=(
63+
'The id MercadoPago has assigned for this Preference'),
64+
max_length=46,
65+
null=True,
66+
verbose_name='mercadopago id',
67+
),
68+
),
69+
migrations.AddField(
70+
model_name='item',
71+
name='preference',
72+
field=models.ForeignKey(
73+
on_delete=django.db.models.deletion.PROTECT,
74+
related_name='items',
75+
to='mp.Preference',
76+
verbose_name='preference',
77+
),
78+
),
79+
]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.db import migrations
2+
3+
4+
def move_item_data(apps, schema_editor):
5+
"""Move Item data from Preference into the new model."""
6+
Preference = apps.get_model('mp', 'Preference')
7+
Item = apps.get_model('mp', 'Item')
8+
9+
for preference in Preference.objects.all():
10+
Item.objects.create(
11+
preference=preference,
12+
title=preference.title,
13+
description=preference.description,
14+
quantity=preference.quantity,
15+
unit_price=preference.price,
16+
)
17+
18+
19+
class Migration(migrations.Migration):
20+
21+
dependencies = [
22+
('mp', '0019_split_preference_items'),
23+
]
24+
25+
operations = [
26+
migrations.RunPython(move_item_data),
27+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 2.1.5 on 2019-03-11 16:28
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('mp', '0020_move_preference_items'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='preference',
15+
name='price',
16+
),
17+
migrations.RemoveField(
18+
model_name='preference',
19+
name='quantity',
20+
),
21+
migrations.RemoveField(
22+
model_name='preference',
23+
name='title',
24+
),
25+
]

0 commit comments

Comments
 (0)