Skip to content

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

Add method for braintree gateway to get customer source

Jakub Draganekf73fc5759dc5
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index 087a0cf9ce..a475b2fa5a 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -2,11 +2,19 @@ import logging
from typing import Dict, List
import braintree as braintree_sdk
+from autopep8 import extract_code_from_function
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import pgettext_lazy
from ... import TransactionKind
-from ...interface import GatewayConfig, GatewayResponse, PaymentData, TokenConfig
+from ...interface import (
+ CustomerSource,
+ CreditCardInfo,
+ GatewayConfig,
+ GatewayResponse,
+ PaymentData,
+ TokenConfig,
+)
from .errors import DEFAULT_ERROR_MESSAGE, BraintreeException
from .forms import BraintreePaymentForm
@@ -269,3 +277,25 @@ def process_payment(
) -> GatewayResponse:
auth_resp = authorize(payment_information, config)
return auth_resp
+
+
+def list_client_sources(
+ config: GatewayConfig, customer_id: str
+) -> List[CustomerSource]:
+ gateway = get_braintree_gateway(**config.connection_params)
+ customer = gateway.customer.find(customer_id)
+ if not customer:
+ return []
+ return [extract_credit_card_data(card) for card in customer.credit_cards]
+
+
+def extract_credit_card_data(card):
+ credit_card = CreditCardInfo(
+ exp_year=card.expiration_year,
+ exp_month=card.expiration_month,
+ last4=card.last_4,
+ name_on_card=card.cardholder_name,
+ )
+ return CustomerSource(
+ id=card.unique_number_identifier, credit_card_info=credit_card
+ )
diff --git a/tests/gateway/cassettes/test_list_customer_sources.yaml b/tests/gateway/cassettes/test_list_customer_sources.yaml
new file mode 100644
index 0000000000..0b5e698572
--- /dev/null
+++ b/tests/gateway/cassettes/test_list_customer_sources.yaml
@@ -0,0 +1,74 @@
+interactions:
+- request:
+ body: null
+ headers:
+ Accept:
+ - application/xml
+ Accept-Encoding:
+ - gzip
+ Connection:
+ - keep-alive
+ Content-type:
+ - application/xml
+ User-Agent:
+ - Braintree Python 3.49.0
+ X-ApiVersion:
+ - '5'
+ method: GET
+ uri: https://api.sandbox.braintreegateway.com/merchants/9m6qhfxsqzm3cgzw/customers/595109854
+ response:
+ body:
+ string: !!binary |
+ H4sIALIJGl0AA6RUTW/jOAy991cEvqv+aBI4hePOALsNsMC2h207214GtEXH2ujDleQ06a8f2Y5j
+ p2kPi7mJ7z1SFEUyudkJPtmiNkzJpRdeBt4EZa4ok+ul9/hwS2LvJr1I8tpYJVCnF5NJwmg6W8zC
+ YBHPponvrAZ0ZF6CtMTZCzF/LYudeX0XV/n6/S3xx2yjLpg2lkgQOJGMLz2ra/T8luLwFZMrUYHc
+ n+EogPHUorHfcAei4njppInf4Y2iKpU8j1fA7gx7w8ww+8ndGsEiJWAndl/h0qPOtEygl0ZBuCDB
+ nETzh3B6PYuvo8VL4g8OrX9d0f/nPzh097cfQAqGnJoupTVXGfCmos9XT+90tbAv//5V3D1swr8f
+ vs/u/3gMEn/QHB5BmSU5aGoOaYDWsPca9pTvEIdlTKbTIIziOPGbc483ItLESJ+YAffeoz1WlIpT
+ 1J9+Z6dRomkNBjx9lBup3qSLNGCDrJZW74kqCDOmBpnjWH9OHh1/79u6GIfeJ6d9P4bPpFzlYN1I
+ pY//jJRHtNdTzJgdXtKZA1lAzfvEM6U4gvTSpoKNtCUHca3d7xCN65o3DxgF/cj0LrirmG7zIUJJ
+ W6Zh5GbmI/iJeo+gXfmi4ETeoidqpB9zL4AbPHiNMhm6NF/dzrIfTzVdhZyuyu3L7eK/5zAO7ld3
+ 4k7++aGhW+cSgdvSNduoJUZYL2MC1khqzdPS2spc+z4Yg9ZcZhqY6x/EtavOG+yb3eFXsBco7U+B
+ tlT0J1dr5W9dn19Wcn2Dcsu0ko1gaUDSTO3cGjzGP97omtGtUZKB3AypnaC9tN150zSM4zDxD0bP
+ uVS04qP56IGjQGMFriB3ynGH88ApWuft2h38B6yXmTozuWZV85Gnm2EYVas26FZBnBfz18TvrJ6r
+ JXutkchaZO1AuMowt6p0SoNsFuVxkM2nMWCMMwinSGkORTSlUTNGX7oeY//e5mxjbFEKRQzdfNGQ
+ R37koV0a3bh+WpF2VYy35QlgWgQo1ei67CzAsBPSi18AAAD//wMAbbQ9a4MHAAA=
+ headers:
+ Cache-Control:
+ - max-age=0, private, must-revalidate
+ Content-Encoding:
+ - gzip
+ Content-Security-Policy:
+ - frame-ancestors 'self'
+ Content-Type:
+ - application/xml; charset=utf-8
+ Date:
+ - Mon, 01 Jul 2019 13:25:06 GMT
+ ETag:
+ - W/"fa7463946d8a584cba7f76b35320371b"
+ Strict-Transport-Security:
+ - max-age=31536000; includeSubDomains
+ Transfer-Encoding:
+ - chunked
+ Vary:
+ - Accept-Encoding
+ X-Authentication:
+ - basic_auth
+ X-BroxyId:
+ - 01-1561987505.981-79.110.196.19-688015
+ X-Content-Type-Options:
+ - nosniff
+ X-Frame-Options:
+ - SAMEORIGIN
+ X-Request-Id:
+ - 01-1561987505.981-79.110.196.19-688015
+ X-Runtime:
+ - '0.148342'
+ X-User:
+ - wgs3tg8h39pyypyc
+ X-XSS-Protection:
+ - 1; mode=block
+ status:
+ code: 200
+ message: OK
+version: 1
diff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.py
index a56d796cc5..0a4b794801 100644
--- a/tests/gateway/test_braintree.py
+++ b/tests/gateway/test_braintree.py
@@ -18,6 +18,7 @@ from saleor.payment.gateways.braintree import (
get_client_token,
get_customer_data,
get_error_for_client,
+ list_client_sources,
refund,
void,
)
@@ -26,7 +27,12 @@ from saleor.payment.gateways.braintree.errors import (
BraintreeException,
)
from saleor.payment.gateways.braintree.forms import BraintreePaymentForm
-from saleor.payment.interface import GatewayConfig, TokenConfig
+from saleor.payment.interface import (
+ GatewayConfig,
+ TokenConfig,
+ CustomerSource,
+ CreditCardInfo,
+)
from saleor.payment.utils import create_payment_information
DEFAULT_ERROR = "Unable to process transaction. Please try again in a moment"
@@ -394,3 +400,17 @@ def test_void_incorrect_token(payment_txn_preauth, sandbox_braintree_gateway_con
with pytest.raises(BraintreeException) as e:
void(payment_info, sandbox_braintree_gateway_config)
assert str(e.value) == DEFAULT_ERROR_MESSAGE
+
+
[email protected](filter_headers=["authorization"])
+def test_list_customer_sources(sandbox_braintree_gateway_config):
+ CUSTOMER_ID = "595109854" # retrieved from sandbox
+ expected_credit_card = CreditCardInfo(
+ last4="1881", exp_year=2020, exp_month=12, name_on_card=""
+ )
+ expected_customer_source = CustomerSource(
+ id=CUSTOMER_ID, credit_card_info=expected_credit_card
+ )
+ sources = list_client_sources(sandbox_braintree_gateway_config, CUSTOMER_ID)
+ assert len(sources) == 1