Skip to content

Commit

Permalink
Merge pull request #52 from TimNekk/develop (v1.7.1)
Browse files Browse the repository at this point in the history
Add Charge Commission Param to BetaTransfer
  • Loading branch information
TimNekk authored Aug 5, 2023
2 parents f24e339 + 9559e18 commit 99cfa64
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 5 additions & 2 deletions docs/source/payment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Settings arguments:
- ``url_success`` - User will be redirected to this url after paying.
- ``url_fail`` - User will be redirected to this url if payment failed.
- ``locale`` - `BetaTransferLocale`_ enum.
- ``charge_commission`` - :ref:`ChargeCommission` enum.

.. note ::
Expand All @@ -277,7 +278,8 @@ Settings arguments:
url_result="my_result_url.com",
url_success="my_success_url.com",
url_fail="my_fail_url.com",
locale=BetaTransferLocale.RUSSIAN)
locale=BetaTransferLocale.RUSSIAN,
charge_commission=ChargeCommission.FROM_SELLER)
# Override setting for specific payment
payment = BetaTransferPayment(amount=100,
Expand All @@ -286,7 +288,8 @@ Settings arguments:
url_result="override_result_url.com",
url_success="override_success_url.com",
url_fail="override_fail_url.com",
locale=BetaTransferLocale.ENGLISH)
locale=BetaTransferLocale.ENGLISH,
charge_commission=ChargeCommission.FROM_CUSTOMER)
.. warning::

Expand Down
23 changes: 19 additions & 4 deletions pypayment/providers/betatransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests

from pypayment import Payment, PaymentStatus, AuthorizationError, PaymentCreationError, PaymentGettingError, \
PaymentNotFound
PaymentNotFound, ChargeCommission


class BetaTransferCurrency(Enum):
Expand Down Expand Up @@ -155,6 +155,7 @@ class BetaTransferPayment(Payment):
_url_success: Optional[str] = None
_url_fail: Optional[str] = None
_locale: Optional[BetaTransferLocale] = None
_charge_commission: Optional[ChargeCommission] = None
_BASE_URL = "https://merchant.betatransfer.io/api"
_PAYMENT_URL = _BASE_URL + "/payment"
_INFO_URL = _BASE_URL + "/info"
Expand Down Expand Up @@ -184,7 +185,8 @@ def __init__(self,
url_result: Optional[str] = None,
url_success: Optional[str] = None,
url_fail: Optional[str] = None,
locale: Optional[BetaTransferLocale] = None) -> None:
locale: Optional[BetaTransferLocale] = None,
charge_commission: Optional[ChargeCommission] = None) -> None:
"""
You need to BetaTransferPayment.authorize() first!
Expand All @@ -200,6 +202,7 @@ def __init__(self,
:param url_success: User will be redirected to this url after paying successfully.
:param url_fail: User will be redirected to this url after paying unsuccessfully.
:param locale: BetaTransferLocale enum.
:param charge_commission: ChargeCommission enum.
:raises NotAuthorizedError: When class was not authorized with BetaTransferPayment.authorize()
:raises PaymentCreationError: When payment creation failed.
Expand All @@ -209,6 +212,8 @@ def __init__(self,
self._url_success = BetaTransferPayment._url_success if url_success is None else url_success
self._url_fail = BetaTransferPayment._url_fail if url_fail is None else url_fail
self._locale = BetaTransferPayment._locale if locale is None else locale
self._charge_commission = BetaTransferPayment._charge_commission if charge_commission is None \
else charge_commission

super().__init__(amount, description, id)

Expand Down Expand Up @@ -236,7 +241,8 @@ def authorize(cls,
url_result: Optional[str] = None,
url_success: Optional[str] = None,
url_fail: Optional[str] = None,
locale: BetaTransferLocale = BetaTransferLocale.RUSSIAN) -> None:
locale: BetaTransferLocale = BetaTransferLocale.RUSSIAN,
charge_commission: ChargeCommission = ChargeCommission.FROM_SELLER) -> None:
"""
Must be called before the first use of the class!
Expand All @@ -250,6 +256,7 @@ def authorize(cls,
:param url_success: User will be redirected to this url after paying successfully.
:param url_fail: User will be redirected to this url after paying unsuccessfully.
:param locale: BetaTransferLocale enum.
:param charge_commission: ChargeCommission enum.
:raises AuthorizationError: When authorization fails.
"""
Expand All @@ -260,6 +267,7 @@ def authorize(cls,
BetaTransferPayment._url_success = url_success
BetaTransferPayment._url_fail = url_fail
BetaTransferPayment._locale = locale
BetaTransferPayment._charge_commission = charge_commission

cls._try_authorize()

Expand All @@ -272,7 +280,7 @@ def _create_url(self) -> str:
}

data = {
"amount": self.amount,
"amount": self._sum_with_commission,
"currency": self._payment_type.value.currency.value,
"orderId": self.id,
"paymentSystem": self._payment_type.value.name,
Expand Down Expand Up @@ -352,3 +360,10 @@ def _try_authorize(cls) -> None:
def _get_sign(cls, data: Mapping[str, str]) -> str:
sign = "".join(str(value) for value in data.values()) + str(BetaTransferPayment._private_key)
return hashlib.md5(sign.encode()).hexdigest()

@property
def _sum_with_commission(self) -> float:
if self._charge_commission == ChargeCommission.FROM_CUSTOMER and self._payment_type:
return self.amount + self.amount * self._payment_type.value.commission_in_percent / 100

return self.amount

0 comments on commit 99cfa64

Please sign in to comment.