|
| 1 | +import typing as t |
| 2 | + |
| 3 | +import unzer |
| 4 | +from unzer.model import PaymentType |
| 5 | + |
| 6 | +from viur.core.skeleton import SkeletonInstance |
| 7 | +from .unzer_abstract import UnzerAbstract |
| 8 | +from ..globals import SHOP_LOGGER |
| 9 | + |
| 10 | +logger = SHOP_LOGGER.getChild(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class UnzerGooglepay(UnzerAbstract): |
| 14 | + """ |
| 15 | + Unzer Google Pay payment method integration for the ViUR Shop. |
| 16 | +
|
| 17 | + Enables customers to pay Google Pay through the Unzer payment gateway. |
| 18 | + """ |
| 19 | + |
| 20 | + name: t.Final[str] = "unzer-googlepay" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + *, |
| 25 | + merchant_id: str | t.Callable[[], str], |
| 26 | + merchant_name: str | t.Callable[[], str], |
| 27 | + allow_credit_cards: bool = True, |
| 28 | + allow_prepaid_cards: bool = True, |
| 29 | + **kwargs: t.Any, |
| 30 | + ) -> None: |
| 31 | + super().__init__(**kwargs) |
| 32 | + self._merchant_id = merchant_id |
| 33 | + self._merchant_name = merchant_name |
| 34 | + self.allow_credit_cards = allow_credit_cards |
| 35 | + self.allow_prepaid_cards = allow_prepaid_cards |
| 36 | + |
| 37 | + @property |
| 38 | + def merchant_id(self) -> str: |
| 39 | + if callable(self._merchant_id): |
| 40 | + return self._merchant_id() |
| 41 | + return self._merchant_id |
| 42 | + |
| 43 | + @property |
| 44 | + def merchant_name(self) -> str: |
| 45 | + if callable(self._merchant_name): |
| 46 | + return self._merchant_name() |
| 47 | + return self._merchant_name |
| 48 | + |
| 49 | + def get_payment_type( |
| 50 | + self, |
| 51 | + order_skel: SkeletonInstance, |
| 52 | + ) -> PaymentType: |
| 53 | + type_id = order_skel["payment"]["payments"][-1]["type_id"] |
| 54 | + return unzer.Googlepay(key=type_id) |
| 55 | + |
| 56 | + def get_checkout_start_data( |
| 57 | + self, |
| 58 | + order_skel: SkeletonInstance, |
| 59 | + ) -> t.Any: |
| 60 | + res = super().get_checkout_start_data(order_skel) |
| 61 | + configuration = unzer.Googlepay(client=self.client).get_configuration()["supports"][0] |
| 62 | + return res | { |
| 63 | + "sandbox": self.sandbox, |
| 64 | + "brands": configuration["brands"], |
| 65 | + "channel": configuration["channel"], |
| 66 | + "merchant_id": self.merchant_id, |
| 67 | + "merchant_name": self.merchant_name, |
| 68 | + "allow_credit_cards": self.allow_credit_cards, |
| 69 | + "allow_prepaid_cards": self.allow_prepaid_cards, |
| 70 | + } |
0 commit comments