Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Paypal discounts moved to django-payments

Krzysztof Wolski7e683bc4b1e0
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 295e264fa5..f18917b307 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -4,6 +4,7 @@ from decimal import Decimal
from uuid import uuid4
import emailit.api
+import payments
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
@@ -13,17 +14,16 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.timezone import now
from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
-from payments import PurchasedItem
from payments.models import BasePayment
-from prices import Price, FixedDiscount
+from prices import FixedDiscount, Price
from satchless.item import ItemLine, ItemSet
+from . import Status
from ..core.utils import build_absolute_uri
from ..discount.models import Voucher
from ..product.models import Product, Stock
-from ..userprofile.models import Address
from ..search import index
-from . import Status
+from ..userprofile.models import Address
class OrderManager(models.Manager):
@@ -431,24 +431,21 @@ class Payment(BasePayment):
from_email=settings.ORDER_FROM_EMAIL)
def get_purchased_items(self):
- items = [PurchasedItem(
+ return [payments.PurchasedItem(
name=item.product_name, sku=item.product_sku,
quantity=item.quantity,
price=item.unit_price_gross.quantize(Decimal('0.01')),
currency=settings.DEFAULT_CURRENCY)
for item in self.order.get_items()]
- # PayPal require discount info as item with negative price.
- provider, _ = settings.PAYMENT_VARIANTS.get(self.variant, (None, None))
+ def get_discounts(self):
voucher = self.order.voucher
- if provider and 'PaypalProvider' in provider and voucher is not None:
- items.append(PurchasedItem(
- name=self.order.discount_name,
- sku='DISCOUNT',
- quantity=1,
- price=-1 * self.order.discount_amount.net,
- currency=self.currency))
- return items
+ if voucher is None:
+ return []
+ return [payments.Discount(
+ name=self.order.discount_name,
+ amount=self.order.discount_amount.net.quantize(Decimal('0.01')),
+ currency=self.currency)]
def get_total_price(self):
net = self.total - self.tax
diff --git a/tests/test_payment.py b/tests/test_payment.py
index a4c7c59845..013785a2e3 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -3,33 +3,24 @@ from prices import Price
from saleor.order.models import Payment
-def test_get_purchased_items(order_with_items, settings, voucher):
+def test_get_purchased_items_and_discounts(order_with_items, settings, voucher):
+ settings.PAYMENT_VARIANTS = {'paypal': ('PaypalProvider', {})}
payment = Payment.objects.create(order=order_with_items, variant='paypal')
discount = Price('10.0', currency=settings.DEFAULT_CURRENCY)
-
- assert len(payment.get_purchased_items()) == len(order_with_items.get_items())
-
- for p, o in zip(payment.get_purchased_items(), order_with_items.get_items()):
- assert p.sku == o.product_sku
- assert p.quantity == o.quantity
+ payment_items = payment.get_purchased_items()
order_with_items.discount_name = 'Test'
order_with_items.discount_amount = discount
order_with_items.voucher = voucher
order_with_items.save()
+ assert len(payment_items) == len(order_with_items.get_items())
- settings.PAYMENT_VARIANTS = { 'paypal': ('PaypalProvider', {})}
-
- assert len(payment.get_purchased_items()) == len(order_with_items.get_items()) + 1
-
- for p, o in zip(payment.get_purchased_items()[:-1], order_with_items.get_items()):
- assert p.sku == o.product_sku
- assert p.quantity == o.quantity
-
- discounted = payment.get_purchased_items()[-1]
+ pairs = zip(payment_items, order_with_items.get_items())
+ for payment_item, order_item in pairs:
+ assert payment_item.sku == order_item.product_sku
+ assert payment_item.quantity == order_item.quantity
+ discounted = payment.get_discounts()[0]
assert discounted.name == order_with_items.discount_name
- assert discounted.sku == 'DISCOUNT'
- assert discounted.price == -1 * order_with_items.discount_amount.net
- assert discounted.quantity == 1
+ assert discounted.amount == order_with_items.discount_amount.net