Skip to content

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

Add client_token endpoint in API

Artur Smęt55aad20cdd76
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index e704a29449..711e46cba6 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -46,9 +46,8 @@ from .product.schema import ProductMutations, ProductQueries
from .shipping.mutations import (
ShippingPriceCreate, ShippingPriceDelete, ShippingPriceUpdate,
ShippingZoneCreate, ShippingZoneDelete, ShippingZoneUpdate)
-from .payment.types import Payment
+from .payment.types import Payment, PaymentGatewayEnum
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 (
@@ -103,7 +102,7 @@ class Query(ProductQueries):
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=''))
+ graphene.String, args={'gateway': PaymentGatewayEnum()})
payments = DjangoFilterConnectionField(
Payment,
description='List of payments')
@@ -182,8 +181,8 @@ class Query(ProductQueries):
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_payment_client_token(self, info, gateway=None):
+ return resolve_payment_client_token(gateway)
def resolve_payments(self, info, query=None, **kwargs):
return resolve_payments(info, query)
diff --git a/saleor/graphql/payment/resolvers.py b/saleor/graphql/payment/resolvers.py
index 11928ee36e..af00140711 100644
--- a/saleor/graphql/payment/resolvers.py
+++ b/saleor/graphql/payment/resolvers.py
@@ -1,6 +1,7 @@
from graphql_jwt.decorators import login_required
from ...order import models
+from ...payment.utils import gateway_get_client_token
from ..utils import filter_by_query_param, get_node
from .types import Payment
@@ -9,8 +10,6 @@ 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
@@ -21,6 +20,5 @@ def resolve_payment(info, id):
return payment
-def resolve_payment_client_token(customer_id=None):
- return
- return braintree.create_client_token(customer_id=customer_id)
+def resolve_payment_client_token(gateway=None):
+ return gateway_get_client_token(gateway)
diff --git a/saleor/payment/providers/braintree/__init__.py b/saleor/payment/providers/braintree/__init__.py
index a44fa40f21..4c3f069dc6 100644
--- a/saleor/payment/providers/braintree/__init__.py
+++ b/saleor/payment/providers/braintree/__init__.py
@@ -1,10 +1,12 @@
-from typing import Dict
import uuid
+from typing import Dict
+
+import braintree as braintree_sdk
from django.db import transaction
-import braintree
-from ... import TransactionType, PaymentMethodChargeStatus
+from ... import PaymentMethodChargeStatus, TransactionType
from ...models import Transaction
+from ...utils import create_transaction
#FIXME: Move to SiteSettings
@@ -37,12 +39,12 @@ def extract_gateway_response(braintree_result) -> Dict:
return gateway_response
def get_gateway(sandbox_mode, merchant_id, public_key, private_key):
- environment = braintree.Environment.Sandbox
+ environment = braintree_sdk.Environment.Sandbox
if not sandbox_mode:
- environment = braintree.Environment.Production
+ environment = braintree_sdk.Environment.Production
- gateway = braintree.BraintreeGateway(
- braintree.Configuration(
+ gateway = braintree_sdk.BraintreeGateway(
+ braintree_sdk.Configuration(
environment=environment,
merchant_id=merchant_id,
public_key=public_key,
@@ -51,6 +53,10 @@ def get_gateway(sandbox_mode, merchant_id, public_key, private_key):
)
return gateway
+def get_client_token(**client_kwargs):
+ gateway = get_gateway(**client_kwargs)
+ client_token = gateway.client_token.generate()
+ return client_token
def authorize(payment_method, transaction_token, **client_kwargs):
gateway = get_gateway(**client_kwargs)
@@ -63,7 +69,7 @@ def authorize(payment_method, transaction_token, **client_kwargs):
'required': THREE_D_SECURE_REQUIRED
}}})
gateway_response = extract_gateway_response(result)
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
amount=payment_method.total,
@@ -83,7 +89,7 @@ def charge(payment_method, amount=None, **client_kwargs):
amount=amount)
gateway_response = extract_gateway_response(result)
- txn = Transaction.objects.get_or_create(
+ txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.CHARGE,
amount=amount,
@@ -95,25 +101,23 @@ def charge(payment_method, amount=None, **client_kwargs):
def void(payment_method, **client_kwargs):
gateway = get_gateway(**client_kwargs)
- 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='',
+ is_success=False)
return txn
def refund(payment_method, amount=None, **client_kwargs):
gateway = get_gateway(**client_kwargs)
- 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="",
+ is_success=False,
+ gateway_response={})
return txn
diff --git a/saleor/payment/providers/dummy/__init__.py b/saleor/payment/providers/dummy/__init__.py
index 13ba6d46af..cefa9deb99 100644
--- a/saleor/payment/providers/dummy/__init__.py
+++ b/saleor/payment/providers/dummy/__init__.py
@@ -7,8 +7,11 @@ from ...utils import create_transaction
def dummy_success():
return True
+def get_client_token(**client_kwargs):
+ return str(uuid.uuid4())
-def authorize(payment_method, transaction_token):
+
+def authorize(payment_method, transaction_token, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
@@ -19,7 +22,7 @@ def authorize(payment_method, transaction_token):
return txn
-def void(payment_method):
+def void(payment_method, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index a14a4ea2b1..9f02d06086 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -30,6 +30,11 @@ def create_transaction(
return txn
+def gateway_get_client_token(provider_name):
+ provider, provider_params = get_provider(provider_name)
+ return provider.get_client_token(**provider_params)
+
+
def gateway_authorize(payment_method, transaction_token) -> Transaction:
if not payment_method.is_active:
raise PaymentError('This payment method is no longer active')
diff --git a/tests/test_payments.py b/tests/test_payments.py
index 18d0d4651d..0cf97993b5 100644
--- a/tests/test_payments.py
+++ b/tests/test_payments.py
@@ -1,5 +1,7 @@
import pytest
-from saleor.payment import PaymentError, TransactionType, PaymentMethodChargeStatus
+
+from saleor.payment import (
+ PaymentError, PaymentMethodChargeStatus, TransactionType)
def test_authorize(payment_method_dummy):