Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Expose CreditCard overview in the API
Pacu28c601db201e1
diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.pyindex fae97d7c22..92f47dca12 100644--- a/saleor/graphql/payment/types.py+++ b/saleor/graphql/payment/types.py@@ -49,6 +49,21 @@ class Transaction(CountableDjangoObjectType):return self.get_amount()+class CreditCard(graphene.ObjectType):+ brand = graphene.String(+ description='Card brand.', required=True)+ first_digits = graphene.String(+ description='The host name of the domain.', required=True)+ last_digits = graphene.Boolean(+ description='Last 4 digits of the card number.', required=True)+ exp_month = graphene.Int(+ description='Two-digit number representing the card’s expiration month.',+ required=True)+ exp_year = graphene.Int(+ description='Four-digit number representing the card’s expiration year.',+ required=True)++class Payment(CountableDjangoObjectType):# FIXME gateway_response field should be resolved into query-readable format# if we want to use it on the frontend@@ -71,6 +86,9 @@ class Payment(CountableDjangoObjectType):Money, description='Maximum amount of money that can be captured.')available_refund_amount = graphene.Field(Money, description='Maximum amount of money that can be refunded.')+ credit_card = graphene.Field(+ CreditCard,+ description='The details of the card used for this payment.')class Meta:description = 'Represents a payment of a given type.'@@ -126,3 +144,14 @@ class Payment(CountableDjangoObjectType):if not self.can_capture():return Nonereturn self.get_total() - self.get_captured_amount()++ def resolve_credit_card(self, info):+ data = {+ 'first_digits': self.cc_first_digits,+ 'last_digits': self.cc_last_digits,+ 'brand': self.cc_brand,+ 'exp_month': self.cc_exp_month,+ 'exp_year': self.cc_exp_year}+ if not any(data.values()):+ return None+ return CreditCard(**data)diff --git a/saleor/order/views.py b/saleor/order/views.pyindex b44880305b..88da4f67c2 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -115,8 +115,7 @@ def start_payment(request, order, gateway):ctx = {'form': form, 'payment': payment,'client_token': client_token, 'order': order}- return TemplateResponse(- request, [template, 'order/payment/default.html'], ctx)+ return TemplateResponse(request, template, ctx)@check_order_statusdiff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.pyindex 3188d50f71..8e80ccd02d 100644--- a/saleor/payment/gateways/braintree/__init__.py+++ b/saleor/payment/gateways/braintree/__init__.py@@ -91,7 +91,6 @@ def transaction_and_incorrect_token_error(def extract_gateway_response(braintree_result) -> Dict:"""Extract data from Braintree response that will be stored locally."""- import pdb; pdb.set_trace()errors = []if not braintree_result.is_success:errors = [@@ -159,11 +158,11 @@ def authorize(payment.cc_first_digits = credit_card_data.binpayment.cc_last_digits = credit_card_data.last_4payment.cc_brand = credit_card_data.card_type- payment.cc_expiry_month = credit_card_data.expiration_month- payment.cc_expiry_year = credit_card_data.expiration_year+ payment.cc_exp_month = credit_card_data.expiration_month+ payment.cc_exp_year = credit_card_data.expiration_yearpayment.save(update_fields=['cc_first_digits', 'cc_last_digits', 'cc_brand',- 'cc_expiry_month', 'cc_expiry_year'])+ 'cc_exp_month', 'cc_exp_year'])txn = create_transaction(payment=payment,diff --git a/saleor/payment/migrations/0001_initial.py b/saleor/payment/migrations/0001_initial.pyindex 7e56bf1cd9..3ca37f7f51 100644--- a/saleor/payment/migrations/0001_initial.py+++ b/saleor/payment/migrations/0001_initial.py@@ -6,6 +6,7 @@ import django.db.models.deletionimport django_prices.modelsimport saleor.corefrom decimal import Decimal+import django.core.validatorsclass Migration(migrations.Migration):@@ -39,6 +40,11 @@ class Migration(migrations.Migration):('billing_country_area', models.CharField(blank=True, max_length=256)),('billing_email', models.EmailField(blank=True, max_length=254)),('customer_ip_address', models.GenericIPAddressField(blank=True, null=True)),+ ('cc_brand', models.CharField(blank=True, default='', max_length=40)),+ ('cc_exp_month', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(12)])),+ ('cc_exp_year', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1000)])),+ ('cc_first_digits', models.CharField(blank=True, default='', max_length=6)),+ ('cc_last_digits', models.CharField(blank=True, default='', max_length=4)),('extra_data', models.TextField(blank=True, default='')),('token', models.CharField(blank=True, default='', max_length=36)),('currency', models.CharField(max_length=10)),diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 9f1bc07259..76335399a5 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -3,6 +3,7 @@ from operator import attrgetterfrom django.conf import settingsfrom django.contrib.postgres.fields import JSONField+from django.core.validators import MaxValueValidator, MinValueValidatorfrom django.db import modelsfrom django_prices.models import MoneyFieldfrom prices import Money@@ -75,8 +76,12 @@ class Payment(models.Model):cc_first_digits = models.CharField(max_length=6, blank=True, default='')cc_last_digits = models.CharField(max_length=4, blank=True, default='')cc_brand = models.CharField(max_length=40, blank=True, default='')- cc_expiry_month = models.CharField(max_length=2, blank=True, default='')- cc_expiry_year = models.CharField(max_length=4, blank=True, default='')+ cc_exp_month = models.PositiveIntegerField(+ validators=[MinValueValidator(1), MaxValueValidator(12)],+ null=True, blank=True)+ # exp year should be in 4 digits format+ cc_exp_year = models.PositiveIntegerField(+ validators=[MinValueValidator(1000)], null=True, blank=True)def __repr__(self):return 'Payment(gateway=%s, is_active=%s, created=%s, charge_status=%s)' % (diff --git a/templates/order/payment/default.html b/templates/order/payment/default.htmldeleted file mode 100644index 0c9a1a3ec4..0000000000--- a/templates/order/payment/default.html+++ /dev/null@@ -1,26 +0,0 @@-{% extends "order/payment/details.html" %}-{% load bootstrap_form from bootstrap4 %}-{% load i18n %}--{% block forms %}-<form method="POST" id="payment-form" class="form-horizontal"{% if form.action %} action="{{ form.action }}"{% endif %} novalidate>- {% csrf_token %}- {{ form.media }}- {% bootstrap_form form layout="vertical" %}- {% if form.errors %}- <a class="btn" href="{% url "order:details" token=payment.order.token %}">- {% trans "Change payment" context "Payment form (Stripe) secondary action" %}- </a>- {% endif %}- {% if not form.autosubmit %}- <div class="form-group">- <div class="col-sm-2"></div>- <div class="col-sm-10">- <button type="submit" class="btn primary">- {% trans "Make payment" context "Payment form primary action" %}- </button>- </div>- </div>- {% endif %}-</form>-{% endblock forms %}diff --git a/tests/api/test_payment.py b/tests/api/test_payment.pyindex a87a3955ac..c7e0b16f78 100644--- a/tests/api/test_payment.py+++ b/tests/api/test_payment.py@@ -291,6 +291,13 @@ def test_payments_query(amount}}+ creditCard {+ expMonth+ expYear+ brand+ firstDigits+ lastDigits+ }}}}@@ -327,3 +334,9 @@ def test_payments_query('amount': {'currency': pay.currency,'amount': float(str(txn.amount))}}]+ assert data['creditCard'] == {+ 'expMonth': pay.cc_exp_month,+ 'expYear': pay.cc_exp_year,+ 'brand': pay.cc_brand,+ 'firstDigits': pay.cc_first_digits,+ 'lastDigits': pay.cc_last_digits}diff --git a/tests/conftest.py b/tests/conftest.pyindex de808bf2b2..7319d9fa7e 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -733,6 +733,11 @@ def payment_dummy(db, settings, order_with_lines):gateway=settings.DUMMY,order=order_with_lines,is_active=True,+ cc_first_digits='4111',+ cc_last_digits='1111',+ cc_brand='VISA',+ cc_exp_month=12,+ cc_exp_year=2027,total=order_with_lines.total.gross.amount,currency=order_with_lines.total.gross.currency,billing_first_name=order_with_lines.billing_address.first_name,diff --git a/tests/test_payment.py b/tests/test_payment.pyindex 1b0000963e..edc69a0f18 100644--- a/tests/test_payment.py+++ b/tests/test_payment.py@@ -427,5 +427,10 @@ def test_gateway_refund_errors(payment_txn_captured):payment.charge_status = ChargeStatus.NOT_CHARGEDwith pytest.raises(PaymentError) as exc:gateway_refund(payment, Decimal('1'))- assert exc.value.message == (- 'Refund is possible only when transaction is captured.')+ assert exc.value.message == 'This payment cannot be captured.'+++def test_payment_provider_templates_exists(payment_dummy):+ # FIXME test if for each payment provider there's corresponding+ # module and template for the old checkout+ pass