Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ pip install paysgator
## Usage

```python
import os
from paysgator import PaysgatorClient

client = PaysgatorClient(api_key="YOUR_API_KEY")
client = PaysgatorClient(api_key=os.getenv("PAYS_GATOR_API_KEY"))

# Create a payment
payment = client.payments.create(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers = [
]
dependencies = [
"requests>=2.25.1",
"pydantic>=2.0.0"
"pydantic==2.12.5"
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion src/paysgator/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def set_base_url(self, url: str):

def request(self, method: str, endpoint: str, data: Optional[dict] = None) -> dict:
url = f"{self.BASE_URL}{endpoint}"
response = self.session.request(method, url, json=data)
response = self.session.request(method, url, json=data, timeout=30)

if response.status_code >= 400:
raise APIError(response.status_code, response.text)
Expand Down
4 changes: 2 additions & 2 deletions src/paysgator/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class APIError(PaysgatorError):
"""Raised when the API returns an error"""
def __init__(self, status_code: int, message: str):
self.status_code = status_code
self.message = message
super().__init__(f"API Error {status_code}: {message}")
self._message = message # internal use only; not exposed in str representation
super().__init__(f"API Error {status_code}")
15 changes: 8 additions & 7 deletions src/paysgator/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Optional, Dict, Any, Union
from decimal import Decimal
from enum import Enum
from pydantic import BaseModel, Field

Expand All @@ -7,10 +8,10 @@ class Mode(str, Enum):
TEST = "TEST"

class PaymentCreateRequest(BaseModel):
amount: float
amount: Decimal
currency: str
external_transaction_id: Optional[str] = Field(None, alias="externalTransactionId")
payment_methods: Optional[List[str]] = Field(None, alias="payment_methods")
payment_methods: Optional[List[str]] = Field(None, alias="paymentMethods")
fields: Optional[List[str]] = None
return_url: Optional[str] = Field(None, alias="returnUrl")
metadata: Optional[Dict[str, Any]] = None
Expand All @@ -34,13 +35,13 @@ class Customer(BaseModel):
class PaymentConfirmRequest(BaseModel):
payment_link_id: str = Field(..., alias="paymentLinkId")
payment_method: str = Field(..., alias="paymentMethod")
payment_fields: Optional[Dict[str, Any]] = Field(None, alias="payment_fields")
payment_fields: Optional[Dict[str, Any]] = Field(None, alias="paymentFields")
customer: Optional[Customer] = None

class PaymentConfirmResponseData(BaseModel):
transaction_id: str = Field(..., alias="transactionId")
fee: float
net_amount: float = Field(..., alias="netAmount")
fee: Decimal
net_amount: Decimal = Field(..., alias="netAmount")

class PaymentConfirmResponse(BaseModel):
success: bool
Expand All @@ -62,7 +63,7 @@ class SubscriptionResponse(BaseModel):

class TransactionResponse(BaseModel):
id: str
amount: float
amount: Decimal
currency: str
status: str
method: Optional[str] = None
Expand All @@ -73,5 +74,5 @@ class TransactionResponse(BaseModel):
class WalletBalanceResponse(BaseModel):
wallet_id: str = Field(..., alias="walletId")
currency: str
balance: str
balance: Decimal
mode: str
18 changes: 7 additions & 11 deletions test_sdk.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from src.paysgator.client import PaysgatorClient
from paysgator import PaysgatorClient

#Mpesa direct charge test

api_key = "<Api Key>"
import os

wallet_id = "<Wallet Id>"
api_key = os.environ.get("PAYSGATOR_API_KEY", "<Api Key>")

client = PaysgatorClient(api_key, wallet_id)
client = PaysgatorClient(api_key=api_key)

link = client.payment_links.create(
title="Test Product",
link = client.payments.create(
amount=50.0,
currency="MZN",
methods=["MPESA"],
payment_fields={
"phoneNumber":""
},
confirm=True,
payment_methods=["MPESA"],
return_url="https://example.com/callback"
)

print(link)