Skip to content

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

Use new version of django-payments

Michal Ociepkac0df18a7fb0c
diff --git a/payment/__init__.py b/payment/__init__.py
index ef607c67e4..87816fa9ee 100644
--- a/payment/__init__.py
+++ b/payment/__init__.py
@@ -5,6 +5,6 @@ from django.dispatch import receiver
@receiver(status_changed)
def order_status_change(sender, instance, **kwargs):
order = instance.order
- if order.is_full_paid():
- order.change_status('fully-paid')
- instance.send_confirmation_email()
+# if order.is_full_paid():
+# order.change_status('fully-paid')
+# instance.send_confirmation_email()
diff --git a/payment/migrations/0003_auto__del_field_payment_city__del_field_payment_first_name__del_field_.py b/payment/migrations/0003_auto__del_field_payment_city__del_field_payment_first_name__del_field_.py
new file mode 100644
index 0000000000..53e9caf8e0
--- /dev/null
+++ b/payment/migrations/0003_auto__del_field_payment_city__del_field_payment_first_name__del_field_.py
@@ -0,0 +1,199 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Deleting field 'Payment.city'
+ db.delete_column(u'payment_payment', 'city')
+
+ # Deleting field 'Payment.first_name'
+ db.delete_column(u'payment_payment', 'first_name')
+
+ # Deleting field 'Payment.last_name'
+ db.delete_column(u'payment_payment', 'last_name')
+
+ # Deleting field 'Payment.zip'
+ db.delete_column(u'payment_payment', 'zip')
+
+ # Deleting field 'Payment.country'
+ db.delete_column(u'payment_payment', 'country')
+
+ # Deleting field 'Payment.country_area'
+ db.delete_column(u'payment_payment', 'country_area')
+
+ # Adding field 'Payment.billing_first_name'
+ db.add_column(u'payment_payment', 'billing_first_name',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_last_name'
+ db.add_column(u'payment_payment', 'billing_last_name',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_address_1'
+ db.add_column(u'payment_payment', 'billing_address_1',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_address_2'
+ db.add_column(u'payment_payment', 'billing_address_2',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_city'
+ db.add_column(u'payment_payment', 'billing_city',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_postcode'
+ db.add_column(u'payment_payment', 'billing_postcode',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_country_code'
+ db.add_column(u'payment_payment', 'billing_country_code',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=2, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.billing_country_area'
+ db.add_column(u'payment_payment', 'billing_country_area',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+
+ def backwards(self, orm):
+ # Adding field 'Payment.city'
+ db.add_column(u'payment_payment', 'city',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.first_name'
+ db.add_column(u'payment_payment', 'first_name',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.last_name'
+ db.add_column(u'payment_payment', 'last_name',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.zip'
+ db.add_column(u'payment_payment', 'zip',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.country'
+ db.add_column(u'payment_payment', 'country',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Adding field 'Payment.country_area'
+ db.add_column(u'payment_payment', 'country_area',
+ self.gf('django.db.models.fields.CharField')(default='', max_length=256, blank=True),
+ keep_default=False)
+
+ # Deleting field 'Payment.billing_first_name'
+ db.delete_column(u'payment_payment', 'billing_first_name')
+
+ # Deleting field 'Payment.billing_last_name'
+ db.delete_column(u'payment_payment', 'billing_last_name')
+
+ # Deleting field 'Payment.billing_address_1'
+ db.delete_column(u'payment_payment', 'billing_address_1')
+
+ # Deleting field 'Payment.billing_address_2'
+ db.delete_column(u'payment_payment', 'billing_address_2')
+
+ # Deleting field 'Payment.billing_city'
+ db.delete_column(u'payment_payment', 'billing_city')
+
+ # Deleting field 'Payment.billing_postcode'
+ db.delete_column(u'payment_payment', 'billing_postcode')
+
+ # Deleting field 'Payment.billing_country_code'
+ db.delete_column(u'payment_payment', 'billing_country_code')
+
+ # Deleting field 'Payment.billing_country_area'
+ db.delete_column(u'payment_payment', 'billing_country_area')
+
+
+ models = {
+ u'order.order': {
+ 'Meta': {'ordering': "('-last_status_change',)", 'object_name': 'Order'},
+ 'anonymous_user_email': ('django.db.models.fields.EmailField', [], {'default': "''", 'max_length': '75', 'blank': 'True'}),
+ 'billing_address': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'to': u"orm['userprofile.Address']"}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_status_change': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'new'", 'max_length': '32'}),
+ 'token': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '36', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'orders'", 'null': 'True', 'to': u"orm['userprofile.User']"})
+ },
+ u'payment.payment': {
+ 'Meta': {'object_name': 'Payment'},
+ 'billing_address_1': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_address_2': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_city': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_country_area': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_country_code': ('django.db.models.fields.CharField', [], {'max_length': '2', 'blank': 'True'}),
+ 'billing_first_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_last_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'billing_postcode': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'currency': ('django.db.models.fields.CharField', [], {'max_length': '10'}),
+ 'delivery': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'max_digits': '9', 'decimal_places': '2'}),
+ 'description': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
+ 'extra_data': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'order': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'payments'", 'to': u"orm['order.Order']"}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'waiting'", 'max_length': '10'}),
+ 'tax': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'max_digits': '9', 'decimal_places': '2'}),
+ 'token': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '36', 'blank': 'True'}),
+ 'total': ('django.db.models.fields.DecimalField', [], {'default': "'0.0'", 'max_digits': '9', 'decimal_places': '2'}),
+ 'transaction_id': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'variant': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ u'userprofile.address': {
+ 'Meta': {'object_name': 'Address'},
+ 'city': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
+ 'company_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'country': ('django.db.models.fields.CharField', [], {'max_length': '2'}),
+ 'country_area': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'phone': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'postal_code': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
+ 'street_address_1': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
+ 'street_address_2': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'})
+ },
+ u'userprofile.addressbook': {
+ 'Meta': {'unique_together': "(('user', 'alias'),)", 'object_name': 'AddressBook'},
+ 'address': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'unique': 'True', 'to': u"orm['userprofile.Address']"}),
+ 'alias': ('django.db.models.fields.CharField', [], {'default': "u'Home'", 'max_length': '30'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'address_book'", 'to': u"orm['userprofile.User']"})
+ },
+ u'userprofile.user': {
+ 'Meta': {'object_name': 'User'},
+ 'addresses': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['userprofile.Address']", 'through': u"orm['userprofile.AddressBook']", 'symmetrical': 'False'}),
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'default_billing_address': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'+'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': u"orm['userprofile.AddressBook']"}),
+ 'default_shipping_address': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'+'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': u"orm['userprofile.AddressBook']"}),
+ 'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75'}),
+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'})
+ }
+ }
+
+ complete_apps = ['payment']
\ No newline at end of file
diff --git a/payment/models.py b/payment/models.py
index 3952ef40b8..8a14c7c5a3 100644
--- a/payment/models.py
+++ b/payment/models.py
@@ -1,9 +1,11 @@
from communication.mail import send_email
+from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from order.models import Order
from payments.models import BasePayment
from saleor.utils import build_absolute_uri
+from payments import PurchasedItem
class Payment(BasePayment):
@@ -24,3 +26,10 @@ class Payment(BasePayment):
reverse('order:details', kwargs={'token': self.order.token}))
context = {'order_url': order_url}
send_email(email, 'payment/emails/confirm_email.txt', context)
+
+ def get_purchased_items(self):
+ items = [PurchasedItem(name=item.product_name, quantity=item.quantity,
+ price=item.unit_price_gross, sku=item.product.sku,
+ currency=settings.SATCHLESS_DEFAULT_CURRENCY)
+ for item in self.order.get_items()]
+ return items
diff --git a/payment/views.py b/payment/views.py
index 08595ce50d..b906f6aee2 100644
--- a/payment/views.py
+++ b/payment/views.py
@@ -1,42 +1,13 @@
-from django.conf import settings
from django.http.response import Http404, HttpResponseForbidden
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from order import check_order_status
from payment.forms import PaymentMethodsForm, PaymentDeledeForm
-from payments import RedirectNeeded, PaymentItem, get_payment_model
+from payments import RedirectNeeded, get_payment_model
Payment = get_payment_model()
-def get_payment_items_from_order(order):
- items = [PaymentItem(name=item.product_name, quantity=item.quantity,
- price=item.unit_price_gross, sku=item.product.sku,
- currency=settings.SATCHLESS_DEFAULT_CURRENCY)
- for item in order.get_items()]
- return items
-
-
-def get_payment_from_order(variant, order):
- total = order.get_total()
- try:
- return order.payments.get(variant=variant, status='waiting')
- except Payment.DoesNotExist:
- billing = order.billing_address
- return Payment(
- order=order, variant=variant,
- total=total.gross, tax=total.tax, currency=total.currency,
- delivery=order.get_delivery_total().gross,
- billing_first_name=billing.first_name,
- billing_last_name=billing.last_name,
- billing_address_1=billing.street_address_1,
- billing_address_2=billing.street_address_2,
- billing_city=billing.city,
- billing_postcode=billing.postal_code,
- billing_country_code=billing.country,
- billing_country_area=billing.country_area)
-
-
@check_order_status
def index(request, order):
form = PaymentMethodsForm(request.POST or None)
@@ -61,20 +32,31 @@ def index(request, order):
@check_order_status
def details(request, order, variant):
order.change_status('payment-pending')
- items = get_payment_items_from_order(order)
- awaiting_payments = order.payments.filter(status='waiting').exists()
- payment = get_payment_from_order(variant, order)
- if awaiting_payments:
+ waiting_payments = order.payments.filter(status='waiting').exists()
+ billing = order.billing_address
+ total = order.get_total()
+ defaults = {'order': order, 'variant': variant, 'total': total.gross,
+ 'tax': total.tax, 'currency': total.currency,
+ 'delivery': order.get_delivery_total().gross,
+ 'billing_first_name': billing.first_name,
+ 'billing_last_name': billing.last_name,
+ 'billing_address_1': billing.street_address_1,
+ 'billing_address_2': billing.street_address_2,
+ 'billing_city': billing.city,
+ 'billing_postcode': billing.postal_code,
+ 'billing_country_code': billing.country,
+ 'billing_country_area': billing.country_area}
+ payment, _created = Payment.objects.get_or_create(variant=variant,
+ status='waiting',
+ defaults=defaults)
+ if waiting_payments:
return redirect('order:payment:index', token=order.token)
try:
- form = payment.get_form(data=request.POST or None, ordered_items=items)
+ form = payment.get_form(data=request.POST or None)
except ValueError as e:
raise Http404(e)
except RedirectNeeded as redirect_to:
return redirect(str(redirect_to))
- if form.is_valid():
- payment.save()
- return redirect(form.cleaned_data['next'])
template = 'payment/%s.html' % variant
return TemplateResponse(request, [template, 'payment/default.html'],
{'form': form, 'payment': payment})