Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Paypal discounts moved to django-payments
Krzysztof Wolski7e683bc4b1e0
diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 295e264fa5..f18917b307 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -4,6 +4,7 @@ from decimal import Decimalfrom uuid import uuid4import emailit.api+import paymentsfrom django.conf import settingsfrom django.core.exceptions import ObjectDoesNotExistfrom django.core.urlresolvers import reverse@@ -13,17 +14,16 @@ from django.utils.encoding import python_2_unicode_compatiblefrom django.utils.timezone import nowfrom django.utils.translation import pgettext_lazyfrom django_prices.models import PriceField-from payments import PurchasedItemfrom payments.models import BasePayment-from prices import Price, FixedDiscount+from prices import FixedDiscount, Pricefrom satchless.item import ItemLine, ItemSet+from . import Statusfrom ..core.utils import build_absolute_urifrom ..discount.models import Voucherfrom ..product.models import Product, Stock-from ..userprofile.models import Addressfrom ..search import index-from . import Status+from ..userprofile.models import Addressclass 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.taxdiff --git a/tests/test_payment.py b/tests/test_payment.pyindex a4c7c59845..013785a2e3 100644--- a/tests/test_payment.py+++ b/tests/test_payment.py@@ -3,33 +3,24 @@ from prices import Pricefrom 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 = discountorder_with_items.voucher = voucherorder_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