Skip to content

Commit

Permalink
Add support for processing refunds
Browse files Browse the repository at this point in the history
Signed-off-by: Musale Martin <martinmshale@gmail.com>
  • Loading branch information
musale committed Aug 31, 2023
1 parent 2ca8efe commit 003e694
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 72 deletions.
4 changes: 4 additions & 0 deletions pesapal_v3/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ class PesapalListIPNsError(PesapalAPIError):

class PesapalGetTransactionStatusError(PesapalAPIError):
"""Pesapal get a transaction status error."""


class PesapalRefundError(PesapalAPIError):
"""Pesapa refund request error."""
54 changes: 45 additions & 9 deletions pesapal_v3/_pesapal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@

import httpx

from pesapal_v3._exceptions import (PesapalAuthError,
PesapalGetTransactionStatusError,
PesapalIPNURLRegError,
PesapalListIPNsError,
PesapalSubmitOrderRequestError)
from pesapal_v3._types import (AccessToken, Environment, IPNRegistration,
OrderRequest, OrderRequestResponse,
PesapalError, SubscriptionDetails,
TransactionStatus)
from pesapal_v3._exceptions import (
PesapalAuthError,
PesapalGetTransactionStatusError,
PesapalIPNURLRegError,
PesapalListIPNsError,
PesapalSubmitOrderRequestError,
PesapalRefundError,
)
from pesapal_v3._types import (
AccessToken,
Environment,
IPNRegistration,
OrderRequest,
OrderRequestResponse,
PesapalError,
TransactionStatus,
Refund,
RefundResponse,
)


class Pesapal:
Expand Down Expand Up @@ -199,3 +209,29 @@ def get_transaction_status(self, *, order_tracking_id: str) -> TransactionStatus
error_msg = PesapalError(**error)
raise PesapalGetTransactionStatusError(error=error_msg, status=status)
return TransactionStatus(**response)

def refund_request(self, *, refund: Refund) -> None:
"""Perform a refund request.
Arguments:
refund(Refund): the refund details.
"""
if not refund:
raise ValueError("refund cannot be empty.")

self._refresh_token()
with httpx.Client(base_url=self._base_url) as client:
client_resp = client.post(
"/Transactions/RefundRequest",
headers=self._headers,
json=refund,
)
response = client_resp.json()
error = response.get("error", None)
status = response.get("status", "500")
print(response)

if status != "200" and error:
error_msg = PesapalError(**error)
raise PesapalRefundError(error=error_msg, status=status)
return RefundResponse(**response)
16 changes: 16 additions & 0 deletions pesapal_v3/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ class TransactionStatus(NamedTuple):
merchant_reference: str
error: Optional[PesapalError]
status: str


class Refund(NamedTuple):
"""Refund request payload"""

confirmation_code: str
amount: float
username: str
remarks: str


class RefundResponse(NamedTuple):
"""A refund response."""

status: str
message: str
63 changes: 0 additions & 63 deletions tester.py

This file was deleted.

0 comments on commit 003e694

Please sign in to comment.