Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

how_to_configure_surcharges.rst 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. .. _how_to_surcharges:
  2. =========================
  3. How to configure surcharges
  4. =========================
  5. A surcharge, also known as checkout fee, is an extra fee charged by a merchant when receiving a payment by cheque, credit card, charge card or debit card (but not cash) which at least covers the cost to the merchant of accepting that means of payment, such as the merchant service fee imposed by a credit card company.
  6. Surcharges in Oscar
  7. ~~~~~~~~~~~~~~~~~
  8. Configuring surcharges requires overriding Oscar's core 'checkout' app
  9. and providing your own ``SurchargeApplicator`` class (see :doc:`/topics/customisation`) that
  10. returns your chosen surcharge instances.
  11. The primary responsibility of the
  12. ``SurchargeApplicator`` class is to provide the available surcharge methods for a
  13. particular scenario. This is done via the
  14. :func:`~oscar.apps.checkout.applicator.SurchargeApplicator.get_applicable_surcharges` method,
  15. which returns the surcharges available to the customer.
  16. This method is called in several places:
  17. * To look up the "default" surcharges so that sample surcharges can be
  18. shown on the basket detail page.
  19. * To give the applicable surcharges to the order total calculator so wo can show the correct price breakdown.
  20. The ``get_applicable_surcharges`` method takes the basket and any other kwargs.
  21. These kwargs can later be determined when setting up your own surcharges.
  22. Note that you can also implement surcharges as models just like shipping methods.
  23. Custom applicators
  24. -------------------
  25. If the available surcharges are the same for all customers and payment
  26. methods, then override the ``get_surcharges`` method of the repository:
  27. .. code-block:: python
  28. from decimal import Decimal as D
  29. from oscar.apps.checkout import applicator
  30. from . import surcharges
  31. class SurchargeApplicator(applicator.SurchargeApplicator):
  32. def get_surcharges(self, basket, **kwargs):
  33. return (
  34. surcharges.PercentageCharge(percentage=D("2.00")),
  35. )
  36. For more complex logic, override the ``is_applicable`` method:
  37. .. code-block:: python
  38. from oscar.apps.checkout import applicator
  39. class SurchargeApplicator(applicator.SurchargeApplicator):
  40. def is_applicable(self, surcharge, basket, **kwargs):
  41. payment_method_code = kwargs.get("payment_method_code", None)
  42. if payment_method is not None and payment_method_code == "paypal":
  43. return True
  44. else:
  45. return False
  46. Surcharges
  47. ----------------
  48. Surcharges need to implement a certain API. They need to have the
  49. following properties which define the metadata about the surcharge:
  50. * ``name`` - The name of the surcharges. This will be visible to the
  51. customer during checkout and is translatable
  52. * ``code`` - The code of the surcharge. This could be the slugified name or anything else.
  53. The code is used as a non-translatable identifier for a charge.
  54. Further, each surcharge must implement a ``calculate`` method which accepts the
  55. basket instance as a parameter and returns a ``Price`` instance. Most surcharges
  56. subclass
  57. :class:`~oscar.apps.checkout.surcharges.BaseSurcharge`, which stubs this API.
  58. Core surcharges
  59. ~~~~~~~~~~~~~~~~~~~~~
  60. Oscar ships with several re-usable surcharges which can be used as-is, or
  61. subclassed and customised:
  62. * :class:`~oscar.apps.checkout.surcharges.PercentageCharge` - percentage based charge
  63. * :class:`~oscar.apps.checkout.surcharges.FlatCharge` - flat surcharge
  64. Example usage:
  65. .. code-block:: python
  66. from decimal import Decimal as D
  67. from oscar.apps.checkout import surcharges
  68. percentage_charge = surcharges.PercentageCharge(percentage=D("2.00"))
  69. flat_charge = surcharges.FlatCharge(excl_tax=D("10.00"), incl_tax=D("12.10"))