Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Revert "Paypal discounts moved to django-payments"
Krzysztof Wolskid1112762b5f2
diff --git a/saleor/order/models.py b/saleor/order/models.pyindex f18917b307..3d36db8fb5 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -4,26 +4,26 @@ from decimal import Decimalfrom uuid import uuid4import emailit.api-import paymentsfrom django.conf import settingsfrom django.core.exceptions import ObjectDoesNotExistfrom django.core.urlresolvers import reversefrom django.core.validators import MaxValueValidator, MinValueValidatorfrom django.db import models-from django.utils.encoding import python_2_unicode_compatible+from django.utils.encoding import python_2_unicode_compatible, smart_textfrom 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 FixedDiscount, Price+from prices import Price, FixedDiscountfrom satchless.item import ItemLine, ItemSet-from . import Statusfrom ..core.utils import build_absolute_urifrom ..discount.models import Voucherfrom ..product.models import Product, Stock-from ..search import indexfrom ..userprofile.models import Address+from ..search import index+from . import Statusclass OrderManager(models.Manager):@@ -431,21 +431,24 @@ class Payment(BasePayment):from_email=settings.ORDER_FROM_EMAIL)def get_purchased_items(self):- return [payments.PurchasedItem(+ items = [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()]- def get_discounts(self):+ # PayPal require discount info as item with negative price.+ provider, _ = settings.PAYMENT_VARIANTS.get(self.variant, (None, None))voucher = self.order.voucher- 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)]+ 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 itemsdef get_total_price(self):net = self.total - self.taxdiff --git a/tests/test_payment.py b/tests/test_payment.pyindex 013785a2e3..a4c7c59845 100644--- a/tests/test_payment.py+++ b/tests/test_payment.py@@ -3,24 +3,33 @@ from prices import Pricefrom saleor.order.models import Payment-def test_get_purchased_items_and_discounts(order_with_items, settings, voucher):- settings.PAYMENT_VARIANTS = {'paypal': ('PaypalProvider', {})}+def test_get_purchased_items(order_with_items, settings, voucher):payment = Payment.objects.create(order=order_with_items, variant='paypal')discount = Price('10.0', currency=settings.DEFAULT_CURRENCY)- payment_items = payment.get_purchased_items()++ 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.quantityorder_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())- 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+ 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]- discounted = payment.get_discounts()[0]assert discounted.name == order_with_items.discount_name- assert discounted.amount == order_with_items.discount_amount.net+ assert discounted.sku == 'DISCOUNT'+ assert discounted.price == -1 * order_with_items.discount_amount.net+ assert discounted.quantity == 1