Checkout

Checkout reference.

Payment

To use the payment methods:

from salesman.checkout.payment import payment_methods_pool

basket_payments = payment_methods_pool.get_payments('basket')
order_payments = payment_methods_pool.get_payments('order')

payment = payment_methods_pool.get_payment('pay-in-advance')
exception salesman.checkout.payment.PaymentError[source]

Payment error for raising payment related exceptions.

class salesman.checkout.payment.PaymentMethod[source]

Base payment method, all payment methods defined in SALESMAN_PAYMENT_METHODS must extend this class.

get_urls() list[django.urls.resolvers.URLPattern | django.urls.resolvers.URLResolver][source]

Hook for adding extra url patterns for payment method. Urls will be included as child patterns under the defined identifier namespace => /payment/{identifier}/{urls}.

is_enabled(request: HttpRequest) bool[source]

Method used to check that payment method is enabled for a given request.

Parameters:

request (HttpRequest) – Django request

Returns:

True if payment method is enabled

Return type:

bool

validate_basket(basket: BaseBasket, request: HttpRequest) None[source]

Method used to validate that payment method is valid for the given basket.

Parameters:
  • basket (Basket) – Basket instance

  • request (HttpRequest) – Django request

Raises:

ValidationError – If payment is not valid for basket

validate_order(order: BaseOrder, request: HttpRequest) None[source]

Method used to validate that payment method is valid for the given order.

Parameters:
  • order (Order) – Order instance

  • request (HttpRequest) – Django request

Raises:

ValidationError – If payment is not valid for order

basket_payment(basket: BaseBasket, request: HttpRequest) str | dict[str, Any][source]

This method gets called when new checkout is submitted and is responsible for creating a new order from given basket.

Parameters:
  • basket (Basket) – Basket instance

  • request (HttpRequest) – Django request

Raises:

PaymentError – If error with payment occurs

Returns:

Redirect URL string or JSON serializable data dictionary

Return type:

Union[str, dict]

order_payment(order: BaseOrder, request: HttpRequest) str | dict[str, Any][source]

This method gets called when payment for an existing order is requested.

Parameters:
  • order (Order) – Order instance

  • request (HttpRequest) – Django request

Raises:

PaymentError – If error with payment occurs

Returns:

Redirect URL string or JSON serializable data dictionary

Return type:

Union[str, dict]

refund_payment(payment: BaseOrderPayment) bool[source]

This method gets called when orders payment refund is requested. Should return True if refund was completed.

Parameters:

payment (OrderPayment) – Order payment instance

Returns:

True if refund was completed

Return type:

bool

class salesman.checkout.payment.PaymentMethodsPool[source]

Pool for storing payment method instances.

get_payments(kind: str | None = None, request: HttpRequest = None) List[PaymentMethod][source]

Returns payment method instances.

Parameters:

kind (Optional[str], optional) – Either basket or order. Defaults to None.

Returns:

Payment method instances

Return type:

List[PaymentMethod]

get_urls() list[django.urls.resolvers.URLPattern | django.urls.resolvers.URLResolver][source]

Returns a list of url patterns for payments to be included.

get_choices(kind: str | None = None, request: HttpRequest = None) list[tuple[str, str]][source]

Return payments formated as choices list of tuples.

Parameters:

kind (Optional[str], optional) – Either basket or order. Defaults to None.

Returns:

List of choices

Return type:

list

get_payment(identifier: str, kind: str | None = None, request: HttpRequest = None) PaymentMethod | None[source]

Returns payment with given identifier.

Parameters:
  • identifier (str) – Payment identifier

  • kind (Optional[str], optional) – Either basket or order. Defaults to None.

Returns:

Payment method instance

Return type:

PaymentMethod

Serializers

class salesman.checkout.serializers.PaymentMethodSerializer(*args, **kwargs)[source]

Serializer to display payment method with error.

to_representation(payment_method: PaymentMethod) Any[source]

Object instance -> Dict of primitive datatypes.

class salesman.checkout.serializers.CheckoutSerializer(*args, **kwargs)[source]

Serializer for processing a basket payment.

Utils

salesman.checkout.utils.validate_address(value: str, context: dict[str, Any] = {}) str[source]

Default address validator function. Can be overriden by providing a dotted path to a function in SALESMAN_ADDRESS_VALIDATOR setting.

Parameters:
  • value (str) – Address text to be validated

  • context (dict, optional) – Validator context data.

Raises:

ValidationError – In case address is not valid

Returns:

Validated value

Return type:

str

Views

class salesman.checkout.views.CheckoutViewSet(**kwargs)[source]

Checkout API endpoint.

serializer_class

alias of CheckoutSerializer

get_view_name() str[source]

Return the view name, as used in OPTIONS responses and in the browsable API.

get_queryset() None[source]

Get the list of items for this view. This must be an iterable, and may be a queryset. Defaults to using self.queryset.

This method should always be used rather than accessing self.queryset directly, as self.queryset gets evaluated only once, and those results are cached for all subsequent requests.

You may want to override this if you need to provide different querysets depending on the incoming request.

(Eg. return a list of items that is specific to the user)

get_serializer_context() dict[str, Any][source]

Extra context provided to the serializer class.

check_permissions(request: Request) None[source]

Check if the request should be permitted. Raises an appropriate exception if the request is not permitted.

dispatch(request: HttpRequest, *args: Any, **kwargs: Any) HttpResponseBase[source]

.dispatch() is pretty much the same as Django’s regular dispatch, but with extra hooks for startup, finalize, and exception handling.

create(request: Request, *args: Any, **kwargs: Any) Response[source]

Process the checkout, handle PaymentError.

list(request: Request, *args: Any, **kwargs: Any) Response[source]

Show a list of payment methods with errors if they exist.