Skip to content

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

Payments API work in progress

Artur Smętc2b1b9f42855
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 19b97f5fe7..e704a29449 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -46,6 +46,9 @@ from .product.schema import ProductMutations, ProductQueries
from .shipping.mutations import (
ShippingPriceCreate, ShippingPriceDelete, ShippingPriceUpdate,
ShippingZoneCreate, ShippingZoneDelete, ShippingZoneUpdate)
+from .payment.types import Payment
+from .payment.resolvers import resolve_payments, resolve_payment_client_token
+from .payment.mutations import CompleteCheckoutWithCreditCard
from .shipping.resolvers import resolve_shipping_zones
from .shipping.types import ShippingZone
from .shop.mutations import (
@@ -98,6 +101,12 @@ class Query(ProductQueries):
Page, query=graphene.String(
description=DESCRIPTIONS['page']),
description='List of the shop\'s pages.')
+ payment = graphene.Field(Payment, id=graphene.Argument(graphene.ID))
+ payment_client_token = graphene.Field(
+ graphene.String, customer_id=graphene.String(description=''))
+ payments = DjangoFilterConnectionField(
+ Payment,
+ description='List of payments')
sale = graphene.Field(
Sale, id=graphene.Argument(graphene.ID, required=True),
description='Lookup a sale by ID.')
@@ -166,6 +175,19 @@ class Query(ProductQueries):
def resolve_orders_total(self, info, period, **kwargs):
return resolve_orders_total(info, period)
+ @login_required
+ def resolve_orders(self, info, query=None, **kwargs):
+ return resolve_orders(info, query)
+
+ def resolve_payment_method(self, info, id):
+ return graphene.Node.get_node_from_global_id(info, id, PaymentMethod)
+
+ def resolve_payment_client_token(self, info, customer_id=None):
+ return resolve_payment_client_token(customer_id)
+
+ def resolve_payments(self, info, query=None, **kwargs):
+ return resolve_payments(info, query)
+
@login_required
def resolve_orders(
self, info, created=None, status=None, query=None, **kwargs):
diff --git a/saleor/graphql/payment/__init__.py b/saleor/graphql/payment/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
new file mode 100644
index 0000000000..57f334cb9f
--- /dev/null
+++ b/saleor/graphql/payment/mutations.py
@@ -0,0 +1,82 @@
+import graphene
+from ..core.mutations import BaseMutation
+from ..core.types.common import Decimal
+from .types import PaymentGatewayEnum
+from ...payment.utils import create_payment_method, gateway_authorize
+
+class BraintreeBillingAddressInput(graphene.InputObjectType):
+ company = graphene.String(description='Company name')
+ country_code_alpha2 = graphene.String(description='2 letter ountry code')
+ country_name = graphene.String(desccription='Country name')
+ first_name = graphene.String(desccription='First name')
+ last_name = graphene.String(desccription='Last name')
+ locality = graphene.String(desccription='Locality/city')
+ postal_code = graphene.String(desccription='Postal code')
+ street_address = graphene.String(desccription='Street address')
+
+
+class BraintreeCustomerInput(graphene.InputObjectType):
+ company = graphene.String(description='Company name')
+ email = graphene.String(description='Email')
+
+
+class CompleteCheckoutWithCreditCard(BaseMutation):
+ class Arguments:
+ gateway = PaymentGatewayEnum()
+ transaction_token = graphene.Argument(
+ graphene.String,
+ required=True,
+ description='One time transaction token')
+ amount = graphene.Argument(
+ Decimal, required=True, description='Transaction amount')
+ store_payment_method = graphene.Argument(
+ graphene.Boolean,
+ default_value=False,
+ description='Store card as payment method')
+
+ transaction_id = graphene.String()
+ transaction_status = graphene.String()
+ transaction_rejection_reason = graphene.String()
+
+ class Meta:
+ description = 'Creates credit card transaction.'
+
+ def mutate(
+ self,
+ info,
+ client_token,
+ amount,
+ store_payment_method,
+ billing_address=None,
+ customer=None):
+
+ # create payment method
+ payment_method = create_payment_method(
+
+ )
+ # authorize payment
+ transaction = gateway_authorize(payment_method)
+
+ return CompleteCheckoutWithCreditCard(
+ transaction_id='',
+ transaction_status='',
+ transaction_rejection_reason='')
+
+
+# class PaymentMethodCreate(BaseMutation):
+# pass
+
+# class PaymentMethodUpdate(BaseMutation):
+# pass
+
+# class PaymentMethodDelete(BaseMutation):
+# pass
+
+# class TransactionRefund(BaseMutation):
+# pass
+
+# class TransactionVoid(BaseMutation):
+# pass
+
+# class PaymentMethodNonceCreate(BaseMutation):
+# pass
diff --git a/saleor/graphql/payment/resolvers.py b/saleor/graphql/payment/resolvers.py
new file mode 100644
index 0000000000..54a6a956a2
--- /dev/null
+++ b/saleor/graphql/payment/resolvers.py
@@ -0,0 +1,26 @@
+from graphql_jwt.decorators import login_required
+
+from ...order import models
+from ..utils import filter_by_query_param, get_node
+from .types import Payment
+from ...payments import braintree
+
+PAYMENT_SEARCH_FIELDS = ['id']
+
+# @login_required
+def resolve_payments(info, query):
+ queryset = models.Payment.objects.all().distinct()
+ # if user.get_all_permissions() & {'order.manage_orders'}:
+ # queryset = models.Order.objects.all().distinct()
+ queryset = filter_by_query_param(queryset, query, PAYMENT_SEARCH_FIELDS)
+ return queryset
+
+
+# @login_required
+def resolve_payment(info, id):
+ payment = get_node(info, id, only_type=Payment)
+ return payment
+
+
+def resolve_payment_client_token(customer_id=None):
+ return braintree.create_client_token(customer_id=customer_id)
diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.py
new file mode 100644
index 0000000000..d32dc95a83
--- /dev/null
+++ b/saleor/graphql/payment/types.py
@@ -0,0 +1,17 @@
+import graphene
+from graphene import relay
+
+from ...order import models
+from ..core.types.common import CountableDjangoObjectType
+from ...payment import PROVIDERS_ENUM
+from graphene import types
+
+
+class Payment(CountableDjangoObjectType):
+ class Meta:
+ description = 'Represents payment.'
+ interfaces = [relay.Node]
+ model = models.Payment
+ exclude_fields = ['order']
+
+PaymentGatewayEnum = graphene.Enum.from_enum(PROVIDERS_ENUM)
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 10d7302418..3b0f716f47 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -1,4 +1,5 @@
import importlib
+from enum import Enum
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import pgettext_lazy
@@ -33,7 +34,7 @@ class PaymentMethodChargeStatus:
# FIXME: move to settings
PROVIDERS_MAP = {
- 'default': {
+ 'dummy': {
'module': 'saleor.payment.providers.dummy',
'params': {}},
@@ -49,6 +50,8 @@ PROVIDERS_MAP = {
}
+PROVIDERS_ENUM = Enum(
+ 'ProvidersEnum', {key.upper(): key.lower() for key in PROVIDERS_MAP})
def get_provider(provider_name):
if provider_name not in PROVIDERS_MAP:
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ed77b95fa5..ff127e72b6 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -5,7 +5,6 @@ from django.contrib.postgres.fields import JSONField
from . import PaymentMethodChargeStatus, TransactionType
from ..checkout.models import Cart
from ..order.models import Order
-from . import utils
class PaymentMethod(models.Model):
@@ -64,15 +63,27 @@ class PaymentMethod(models.Model):
return self._get_money(self.tax)
def authorize(self):
+ # FIXME
+ from . import utils
+
return utils.gateway_authorize(payment_method=self)
def void(self):
+ # FIXME
+ from . import utils
+
return utils.gateway_void(payment_method=self)
def charge(self, amount=None):
+ # FIXME
+ from . import utils
+
return utils.gateway_charge(payment_method=self, amount=amount)
def refund(self, amount=None):
+ # FIXME
+ from . import utils
+
return utils.gateway_refund(payment_method=self, amount=amount)
diff --git a/saleor/payment/providers/dummy/__init__.py b/saleor/payment/providers/dummy/__init__.py
index 3d0338593b..4a5a93f985 100644
--- a/saleor/payment/providers/dummy/__init__.py
+++ b/saleor/payment/providers/dummy/__init__.py
@@ -2,56 +2,49 @@ import uuid
from django.db import transaction
from ... import TransactionType, PaymentMethodChargeStatus
-from ...models import Transaction
-
+from ...utils import create_transaction
def dummy_success():
return True
def authorize(payment_method):
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
amount=payment_method.total,
gateway_response={},
- defaults={
- 'token': str(uuid.uuid4()),
- 'is_success': dummy_success()})[0]
+ token=str(uuid.uuid4()),
+ is_success=dummy_success())
return txn
def void(payment_method):
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
amount=payment_method.total,
gateway_response={},
- defaults={
- 'token': str(uuid.uuid4()),
- 'is_success': dummy_success()})[0]
+ token=str(uuid.uuid4()),
+ is_success=dummy_success())
return txn
def charge(payment_method, amount=None):
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.CHARGE,
amount=amount,
- defaults={
- 'token': str(uuid.uuid4()),
- 'gateway_response': {},
- 'is_success': dummy_success()})[0]
+ token=str(uuid.uuid4()),
+ is_success=dummy_success())
return txn
def refund(payment_method, amount=None):
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.REFUND,
amount=amount,
- defaults={
- 'token': str(uuid.uuid4()),
- 'gateway_response': {},
- 'is_success': dummy_success()})[0]
+ token=str(uuid.uuid4()),
+ is_success=dummy_success())
return txn
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index e1d8d86d9f..98a265fd1d 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -1,7 +1,34 @@
+from typing import Optional, Dict
+from decimal import Decimal
from django.db import transaction
+from .models import PaymentMethod, Transaction
from . import PaymentError, PaymentMethodChargeStatus, get_provider
+def create_payment_method(**payment_method_kwargs):
+ payment_method, dummy_created = PaymentMethod.objects.get_or_create(
+ **payment_method_kwargs)
+ return payment_method
+
+def create_transaction(
+ payment_method: PaymentMethod,
+ token: str,
+ transaction_type: str,
+ is_success: bool,
+ amount: Decimal,
+ gateway_response: Optional[Dict] = None) -> Transaction:
+ if not gateway_response:
+ gateway_response = {}
+
+ txn, dummy_created = Transaction.objects.get_or_create(
+ payment_method=payment_method,
+ token=token,
+ transaction_type=transaction_type,
+ is_success=is_success,
+ amount=amount,
+ gateway_response=gateway_response)
+ return txn
+
def gateway_authorize(payment_method):
if not payment_method.is_active: