Skip to content

Commit

Permalink
Add client_token endpoint in API
Browse files Browse the repository at this point in the history
  • Loading branch information
artursmet authored and Pacu2 committed Oct 30, 2018
1 parent 665078c commit 55aad20
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
9 changes: 4 additions & 5 deletions saleor/graphql/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
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 (
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -182,8 +181,8 @@ def resolve_orders(self, info, query=None, **kwargs):
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)
Expand Down
8 changes: 3 additions & 5 deletions saleor/graphql/payment/resolvers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -9,8 +10,6 @@
# @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

Expand All @@ -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)
40 changes: 22 additions & 18 deletions saleor/payment/providers/braintree/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
7 changes: 5 additions & 2 deletions saleor/payment/providers/dummy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
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,
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions saleor/payment/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion tests/test_payments.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 55aad20

Please sign in to comment.