Skip to content

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

Drop django payments

Pacu208b7f12e26d0
diff --git a/Pipfile b/Pipfile
index 9a84234f17..41dc6e1def 100644
--- a/Pipfile
+++ b/Pipfile
@@ -24,7 +24,6 @@ django-graphql-jwt = ">=0.1.13"
django-impersonate = ">=1.3"
django-measurement = "==3.0.0"
django-mptt = ">=0.7.1"
-django-payments = ">=0.12.1"
django-phonenumber-field = ">=1.3.0"
django-prices = ">=1.0.2"
django-prices-openexchangerates = ">=1.0.0"
@@ -62,6 +61,7 @@ uwsgi = {version = ">=2.0.0", platform_system = "!= 'Windows'"}
celery = {version = "*", extras = ["redis"]}
weasyprint = ">=0.42.2"
graphene-django-optimizer = "*"
+braintree = "==3.49.0"
[dev-packages]
pytest = "*"
diff --git a/docs/architecture/payments.rst b/docs/architecture/payments.rst
index b057d18192..8649988807 100644
--- a/docs/architecture/payments.rst
+++ b/docs/architecture/payments.rst
@@ -1,30 +1,15 @@
Payments
========
-
Supported Gateways
------------------
-Saleor uses `django-payments <http://django-payments.readthedocs.io/en/latest/>`_ library to process payments.
-
Default configuration uses the *dummy* backend.
It's meant to allow developers to easily simulate different payment results.
Here is a list of supported payment providers:
-* Authorize.Net
* Braintree
-* Coinbase
-* Cybersource
-* Dotpay
-* Google Wallet
-* PayPal
-* Sage Pay
-* Sofort.com
-* Stripe
-
-Please note that this list is only provided here for reference.
-Please consult `django-payments documentation <http://django-payments.readthedocs.io/en/latest/modules.html>`_ for an up to date list and instructions.
.. note::
@@ -32,27 +17,10 @@ Please consult `django-payments documentation <http://django-payments.readthedoc
This is very useful for development but make sure you use production mode when deploying to a production server.
-3-D Secure
-----------
-
-3-D Secure is a card protection protocol that allows merchants to partially mitigate fraud responsibility.
-In practice it greatly lowers the probability of a chargeback.
-
-Saleor supports 3-D Secure but whether it's used depends on the payment processor and the card being used.
-
-
-Fraud Protection
-----------------
-
-Some of the payment backends provide automatic fraud protection heuristics.
-If such information is available Saleor will show it in the order management panel.
-
-
Authorisation and Capture
-------------------------
Some of the payment backends support pre-authorising payments.
-Please see `django-payments documentation <http://django-payments.readthedocs.io/en/latest/preauth.html>`_ for details.
Authorisation and capture is a two step process.
diff --git a/requirements.txt b/requirements.txt
index 4776349ef3..c39f8fea13 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,21 +1,19 @@
-i https://pypi.python.org/simple
amqp==2.3.2
aniso8601==3.0.2
-asn1crypto==0.24.0
autopep8==1.4
babel==2.6.0
billiard==3.5.0.4
bleach==2.1.4
boto3==1.9.19
botocore==1.12.19
-braintree==3.48.0
+braintree==3.49.0
cairocffi==0.9.0
cairosvg==2.2.1
celery==4.2.1
certifi==2018.8.24
cffi==1.11.5
chardet==3.0.4
-cryptography==2.3.1
cssselect2==0.2.1
defusedxml==0.5.0 ; python_version >= '3.0'
dj-database-url==0.5.0
@@ -36,7 +34,6 @@ django-impersonate==1.3
django-js-asset==1.1.0
django-measurement==3.0.0
django-mptt==0.9.1
-django-payments==0.13.0
django-phonenumber-field==2.0.1
django-prices-openexchangerates==1.0.1
django-prices-vatlayer==1.0.2
@@ -103,8 +100,6 @@ six==1.11.0
social-auth-app-django==2.1.0
social-auth-core==1.7.0
sqlparse==0.2.4
-stripe==2.10.1
-suds-jurko==0.6
sympy==1.3
text-unidecode==1.2
tinycss2==0.6.1
@@ -114,4 +109,3 @@ uwsgi==2.0.17.1 ; platform_system != 'Windows'
vine==1.1.4
weasyprint==0.42.3
webencodings==0.5.1
-xmltodict==0.11.0
diff --git a/saleor/dashboard/order/filters.py b/saleor/dashboard/order/filters.py
index 43f5b90b83..ce216c7551 100644
--- a/saleor/dashboard/order/filters.py
+++ b/saleor/dashboard/order/filters.py
@@ -13,14 +13,14 @@ from ..widgets import DateRangeWidget, MoneyRangeWidget
SORT_BY_FIELDS = [
('pk', 'pk'),
- ('payments__status', 'payment_status'),
+ ('payment_methods__charge_status', 'payment_status'),
('user__email', 'email'),
('created', 'created'),
('total_net', 'total')]
SORT_BY_FIELDS_LABELS = {
'pk': pgettext_lazy('Order list sorting option', '#'),
- 'payments__charge_status': pgettext_lazy(
+ 'payment_methods__charge_status': pgettext_lazy(
'Order list sorting option', 'payment'),
'user__email': pgettext_lazy('Order list sorting option', 'email'),
'created': pgettext_lazy('Order list sorting option', 'created'),
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 4a82049ae6..a26e86a34e 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -44,7 +44,7 @@ from .utils import (
@staff_member_required
@permission_required('order.manage_orders')
def order_list(request):
- orders = Order.objects.prefetch_related('payments', 'lines', 'user')
+ orders = Order.objects.prefetch_related('payment_methods', 'lines', 'user')
order_filter = OrderFilter(request.GET, queryset=orders)
orders = get_paginator_items(
order_filter.qs, settings.DASHBOARD_PAGINATE_BY,
diff --git a/saleor/order/migrations/0001_initial.py b/saleor/order/migrations/0001_initial.py
index 7d34431e81..39ff15d6bf 100644
--- a/saleor/order/migrations/0001_initial.py
+++ b/saleor/order/migrations/0001_initial.py
@@ -104,7 +104,7 @@ class Migration(migrations.Migration):
('billing_address_1', models.CharField(max_length=256, blank=True)),
('billing_address_2', models.CharField(max_length=256, blank=True)),
('billing_city', models.CharField(max_length=256, blank=True)),
- ('billing_postal_code', models.CharField(max_length=256, blank=True)),
+ ('billing_postcode', models.CharField(max_length=256, blank=True)),
('billing_country_code', models.CharField(max_length=2, blank=True)),
('billing_country_area', models.CharField(max_length=256, blank=True)),
('billing_email', models.EmailField(max_length=254, blank=True)),
diff --git a/saleor/order/migrations/0065_auto_20181017_1633.py b/saleor/order/migrations/0065_auto_20181017_1633.py
new file mode 100644
index 0000000000..ef3088284f
--- /dev/null
+++ b/saleor/order/migrations/0065_auto_20181017_1633.py
@@ -0,0 +1,21 @@
+# Generated by Django 2.1.2 on 2018-10-17 21:33
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('order', '0064_auto_20181017_1346'),
+ ('payment', '0002_transfer_payment_to_payment_method'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='payment',
+ name='order',
+ ),
+ migrations.DeleteModel(
+ name='Payment',
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 8c5b066382..5a79d93f80 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -13,8 +13,6 @@ from django.utils.translation import pgettext_lazy
from django_measurement.models import MeasurementField
from django_prices.models import MoneyField, TaxedMoneyField
from measurement.measures import Weight
-from payments import PurchasedItem
-from payments.models import BasePayment
from prices import Money, TaxedMoney
from . import FulfillmentStatus, OrderEvents, OrderStatus, display_order_event
@@ -299,51 +297,6 @@ class FulfillmentLine(models.Model):
quantity = models.IntegerField(validators=[MinValueValidator(1)])
-class Payment(BasePayment):
- order = models.ForeignKey(
- Order, related_name='payments', on_delete=models.PROTECT)
-
- class Meta:
- ordering = ('-pk',)
-
- def get_failure_url(self):
- return build_absolute_uri(
- reverse('order:details', kwargs={'token': self.order.token}))
-
- def get_success_url(self):
- return build_absolute_uri(
- reverse(
- 'order:payment-success', kwargs={'token': self.order.token}))
-
- def get_purchased_items(self):
- lines = [
- PurchasedItem(
- name=line.product_name, sku=line.product_sku,
- quantity=line.quantity,
- price=line.unit_price_net.quantize(Decimal('0.01')).amount,
- currency=line.unit_price.currency)
- for line in self.order]
-
- voucher = self.order.voucher
- if voucher is not None:
- lines.append(
- PurchasedItem(
- name=self.order.discount_name,
- sku='DISCOUNT',
- quantity=1,
- price=-self.order.discount_amount.amount,
- currency=self.order.discount_amount.currency))
- return lines
-
- def get_total(self):
- return TaxedMoney(
- net=Money(self.total - self.tax, self.currency),
- gross=Money(self.total, self.currency))
-
- def get_captured_price(self):
- return Money(self.captured_amount, self.currency)
-
-
class OrderEvent(models.Model):
"""Model used to store events that happened during the order lifecycle.
diff --git a/saleor/payment/migrations/0002_transfer_payment_to_payment_method.py b/saleor/payment/migrations/0002_transfer_payment_to_payment_method.py
new file mode 100644
index 0000000000..92bb4540d4
--- /dev/null
+++ b/saleor/payment/migrations/0002_transfer_payment_to_payment_method.py
@@ -0,0 +1,135 @@
+# Generated by Django 2.1.2 on 2018-10-17 19:42
+from django.db import migrations
+from prices import Money
+
+from .. import ChargeStatus, TransactionType
+from ..models import Transaction
+
+class PaymentStatus:
+ WAITING = 'waiting'
+ PREAUTH = 'preauth'
+ CONFIRMED = 'confirmed'
+ REJECTED = 'rejected'
+ REFUNDED = 'refunded'
+ ERROR = 'error'
+ INPUT = 'input'
+
+
+def is_fully_refunded(payment):
+ total = Money(payment.total, payment.currency)
+ return total == payment.order.total_gross
+
+
+def get_charge_status(payment):
+ if payment.status == PaymentStatus.CONFIRMED:
+ return ChargeStatus.CHARGED
+ if payment.status == PaymentStatus.REFUNDED:
+ if is_fully_refunded(payment):
+ return ChargeStatus.FULLY_REFUNDED
+ return ChargeStatus.CHARGED
+ return ChargeStatus.NOT_CHARGED
+
+
+def get_is_active(status):
+ non_active_statuses = [
+ PaymentStatus.WAITING, PaymentStatus.REFUNDED, PaymentStatus.INPUT,
+ PaymentStatus.REJECTED]
+ if status in non_active_statuses:
+ return False
+ return True
+
+
+def create_transaction(method, transaction_type, created, amount, is_success):
+ return method.transactions.create(
+ created=created,
+ transaction_type=transaction_type,
+ is_success=is_success,
+ amount=amount,
+ gateway_response={})
+
+
+def create_transactions(method, payment):
+ # Those payments are inactive and need no transactions
+ if payment.status in [
+ PaymentStatus.INPUT, PaymentStatus.WAITING, PaymentStatus.PREAUTH]:
+ method.is_active = False
+ method.save()
+ return
+
+ # Other payments needed to be authorized first
+ amount = Money(payment.total, payment.currency)
+ created = payment.created
+ create_transaction(
+ method=method, transaction_type=TransactionType.AUTH,
+ created=created, amount=amount, is_success=True)
+ # This kind of payment needs an unsuccessful capture transaction
+ if payment.status in [PaymentStatus.ERROR, PaymentStatus.REJECTED]:
+ create_transaction(
+ method=method, transaction_type=TransactionType.CAPTURE,
+ created=created, amount=amount, is_success=False)
+ return
+
+ # Two other payments left - CONFIRMED and REFUNDED needs to be captured
+ create_transaction(
+ method=method, transaction_type=TransactionType.CAPTURE,
+ created=created, amount=amount, is_success=True)
+
+ # If payment was refunded, we need to create a refund transaction for it
+ if payment.status == PaymentStatus.REFUNDED:
+ create_transaction(
+ method=method, transaction_type=TransactionType.REFUND,
+ created=created, amount=amount, is_success=True)
+ if is_fully_refunded(payment):
+ method.is_active = False
+ method.save()
+
+
+def transfer_payments_to_payment_methods(apps, schema_editor):
+ PaymentMethod = apps.get_model('payment', 'PaymentMethod')
+ Payment = apps.get_model('order', 'Payment')
+ payments = Payment.objects.all()
+
+ for pay in payments:
+ extra_data = {
+ 'fraud_status': pay.fraud_status,
+ 'fraud_message': pay.fraud_message,
+ 'transaction_id': pay.transaction_id,
+ 'delivery_fee': pay.delivery,
+ 'message': pay.message,
+ 'description': pay.description,
+ 'extra_data': pay.extra_data}
+ payment_method = PaymentMethod.objects.create(
+ order=pay.order,
+ variant=pay.variant,
+ created=pay.created,
+ modified=pay.modified,
+ billing_first_name=pay.billing_first_name,
+ billing_last_name=pay.billing_last_name,
+ billing_address_1=pay.billing_address_1,
+ billing_address_2=pay.billing_address_2,
+ billing_city=pay.billing_city,
+ billing_country_code=pay.billing_country_code,
+ billing_country_area=pay.billing_country_area,
+ billing_email=pay.billing_email,
+ customer_ip_address=pay.customer_ip_address,
+ extra_data=extra_data,
+ token=pay.token,
+ captured_amount=Money(pay.captured_amount, pay.currency),
+ total_gross=Money(pay.total, pay.currency),
+ total_net=Money(pay.total - pay.tax, pay.currency),
+ is_active=get_is_active(pay.status),
+ charge_status=get_charge_status(pay))
+ create_transactions(payment_method, pay)
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('payment', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ transfer_payments_to_payment_methods,
+ migrations.RunPython.noop),
+ ]
diff --git a/saleor/settings.py b/saleor/settings.py
index bfd807617b..c61335c2ff 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -227,7 +227,6 @@ INSTALLED_APPS = [
'django_prices_vatlayer',
'graphene_django',
'mptt',
- 'payments',
'webpack_loader',
'social_django',
'django_countries',
diff --git a/saleor/urls.py b/saleor/urls.py
index 38afe6ba1e..1c770e607d 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -47,8 +47,7 @@ translatable_urlpatterns = [
include((account_urls, 'account'), namespace='account')),
url(r'^feeds/',
include((feed_urls, 'data_feeds'), namespace='data_feeds')),
- url(r'^search/', include((search_urls, 'search'), namespace='search')),
- url(r'', include('payments.urls'))]
+ url(r'^search/', include((search_urls, 'search'), namespace='search'))]
urlpatterns = non_translatable_urlpatterns + i18n_patterns(
*translatable_urlpatterns)
diff --git a/tests/conftest.py b/tests/conftest.py
index acb37a91f8..91e4729070 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -13,7 +13,6 @@ from django.utils.encoding import smart_text
from django_countries import countries
from django_prices_vatlayer.models import VAT
from django_prices_vatlayer.utils import get_tax_for_rate
-from payments import FraudStatus, PaymentStatus
from PIL import Image
from prices import Money
from saleor.account.backends import BaseBackend
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index dcb30f2156..6671692da0 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -3,7 +3,6 @@ import json
import pytest
from django.conf import settings
from django.urls import reverse
-from payments import PaymentStatus
from prices import Money
from tests.utils import get_form_errors, get_redirect_location