-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcash_on_delivery.py
116 lines (89 loc) · 4.16 KB
/
cash_on_delivery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from decimal import Decimal
from typing import Optional, Any
from dataclasses import dataclass
from rest_framework import serializers
from nxtbn.payment.base import PaymentPlugin, PaymentResponse
class CodPayloadSerializer(serializers.Serializer):
""""Need to define at least a serialize, dummy as it is cod, no additional payload will come from payment gateway"""
pass
class CashOnDelivery(PaymentPlugin):
"""
Implementation of a cash on delivery payment gateway.
Cash on delivery allows customers to pay for their purchases in cash upon delivery.
"""
def authorize(self, amount: Decimal, order_id: str, **kwargs) -> PaymentResponse:
"""
Authorize a cash on delivery payment.
Since cash on delivery does not require authorization beforehand,
this method simply returns a PaymentResponse with success set to True.
Args:
amount (Decimal): The amount to be authorized.
order_id (str): The unique identifier for the order.
**kwargs: Additional keyword arguments (not used).
Returns:
PaymentResponse: A PaymentResponse object indicating successful authorization.
"""
return PaymentResponse(
success=True,
transaction_id=None, # cash on delivery has no transaction identifier
)
def capture(self, amount: Decimal, order_id: str, collected: bool = False, **kwargs) -> PaymentResponse:
"""
Capture a cash on delivery payment.
If the cash is collected upon delivery (collected=True), the capture is successful,
otherwise, it's unsuccessful.
Args:
amount (Decimal): The amount to be captured.
order_id (str): The unique identifier for the order.
collected (bool, optional): Whether the cash is collected upon delivery. Defaults to False.
**kwargs: Additional keyword arguments (not used).
Returns:
PaymentResponse: A PaymentResponse object indicating successful or unsuccessful capture.
"""
success = collected
return PaymentResponse(success=success)
def cancel(self, order_id: str, **kwargs) -> PaymentResponse:
"""
Cancel a cash on delivery payment.
Since cash on delivery payments are typically not cancellable,
this method returns a PaymentResponse with success set to False.
Args:
order_id (str): The unique identifier for the order.
**kwargs: Additional keyword arguments (not used).
Returns:
PaymentResponse: A PaymentResponse object indicating cancellation failure.
"""
return PaymentResponse(
success=False,
transaction_id=None, # cash on delivery has no transaction identifier
)
def refund(self, amount: Decimal, order_id: str, **kwargs) -> PaymentResponse:
"""
Refund a cash on delivery payment.
Cash on delivery payments are typically not refundable,
so this method returns a PaymentResponse with success set to False.
Args:
amount (Decimal): The amount to be refunded.
order_id (str): The unique identifier for the order.
**kwargs: Additional keyword arguments (not used).
Returns:
PaymentResponse: A PaymentResponse object indicating refund failure.
"""
return PaymentResponse(success=False)
def normalize_response(self, raw_response: Any) -> PaymentResponse:
"""
Normalize raw response to a consistent PaymentResponse.
Since cash on delivery payments do not involve external gateways,
this method returns a PaymentResponse with success set to True
and transaction_id set to None.
Args:
raw_response (Any): The raw response received (not used).
Returns:
PaymentResponse: A PaymentResponse object indicating success.
"""
return PaymentResponse(success=True)
def special_serializer(self):
"""This method will handle payload from client size will be used in api views"""
return CodPayloadSerializer()
def public_keys(self):
return {}