-
Notifications
You must be signed in to change notification settings - Fork 19
#27 added mtn money with test #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SEEDART007
wants to merge
1
commit into
AllDotPy:master
Choose a base branch
from
SEEDART007:feat/mtn-adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import httpx | ||
| from typing import Dict, Any, Optional | ||
|
|
||
| class MTNMobileMoneyAdapter: | ||
| """ | ||
| MTN Mobile Money API Adapter | ||
| -------------------------------- | ||
| Handles: | ||
| - Authentication (OAuth2) | ||
| - Request-to-Pay (Collections) | ||
| - Transfers (Disbursements) | ||
| - Transaction Status | ||
| - Refunds (if supported) | ||
| """ | ||
|
|
||
| BASE_URL = "https://api.mtn.com/v1/" # Change to sandbox for testing | ||
|
|
||
| def __init__(self, api_key: str, subscription_key: str, environment: str = "sandbox"): | ||
| """ | ||
| :param api_key: API Key from MTN Developer Portal | ||
| :param subscription_key: Subscription Key (Ocp-Apim-Subscription-Key) | ||
| :param environment: "sandbox" or "production" | ||
| """ | ||
| self.api_key = api_key | ||
| self.subscription_key = subscription_key | ||
| self.environment = environment | ||
| self.access_token = None | ||
|
|
||
| async def authenticate(self) -> str: | ||
| """ | ||
| Obtain OAuth2 access token from MTN API. | ||
| """ | ||
| url = f"{self.BASE_URL}token/" | ||
| headers = { | ||
| "Ocp-Apim-Subscription-Key": self.subscription_key, | ||
| } | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| resp = await client.post(url, headers=headers, auth=(self.api_key, "")) | ||
| resp.raise_for_status() | ||
| data = resp.json() | ||
| self.access_token = data.get("access_token") | ||
| return self.access_token | ||
|
|
||
| async def send_payment(self, amount: float, phone_number: str, currency: str, reference: str) -> Dict[str, Any]: | ||
| """ | ||
| Initiate a 'Request to Pay' (Collection) transaction. | ||
| """ | ||
| if not self.access_token: | ||
| await self.authenticate() | ||
|
|
||
| url = f"{self.BASE_URL}collection/request-to-pay" | ||
| headers = { | ||
| "Authorization": f"Bearer {self.access_token}", | ||
| "X-Reference-Id": reference, | ||
| "X-Target-Environment": self.environment, | ||
| "Content-Type": "application/json", | ||
| "Ocp-Apim-Subscription-Key": self.subscription_key, | ||
| } | ||
|
|
||
| payload = { | ||
| "amount": str(amount), | ||
| "currency": currency, | ||
| "externalId": reference, | ||
| "payer": { | ||
| "partyIdType": "MSISDN", | ||
| "partyId": phone_number, | ||
| }, | ||
| "payerMessage": "EasySwitch Payment", | ||
| "payeeNote": "Thank you for using EasySwitch", | ||
| } | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| resp = await client.post(url, headers=headers, json=payload) | ||
| return {"status_code": resp.status_code, "reference": reference} | ||
|
|
||
| async def check_transaction_status(self, reference: str) -> Dict[str, Any]: | ||
| """ | ||
| Check the status of a transaction by reference ID. | ||
| """ | ||
| if not self.access_token: | ||
| await self.authenticate() | ||
|
|
||
| url = f"{self.BASE_URL}transaction/status/{reference}" | ||
| headers = { | ||
| "Authorization": f"Bearer {self.access_token}", | ||
| "Ocp-Apim-Subscription-Key": self.subscription_key, | ||
| } | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| resp = await client.get(url, headers=headers) | ||
| resp.raise_for_status() | ||
| return resp.json() | ||
|
|
||
| async def process_refund(self, original_reference: str, amount: float, currency: str) -> Dict[str, Any]: | ||
| """ | ||
| Process refund for a completed transaction (if supported). | ||
| """ | ||
| if not self.access_token: | ||
| await self.authenticate() | ||
|
|
||
| url = f"{self.BASE_URL}disbursement/transfer" | ||
| headers = { | ||
| "Authorization": f"Bearer {self.access_token}", | ||
| "Ocp-Apim-Subscription-Key": self.subscription_key, | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| payload = { | ||
| "amount": str(amount), | ||
| "currency": currency, | ||
| "externalId": f"refund_{original_reference}", | ||
| "payee": {"partyIdType": "MSISDN", "partyId": "<customer_number>"}, | ||
| "payerMessage": "Refund Processed", | ||
| "payeeNote": "Refund from EasySwitch", | ||
| } | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| resp = await client.post(url, headers=headers, json=payload) | ||
| return {"status_code": resp.status_code, "refund_reference": original_reference} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import asyncio | ||
| from easyswitch.adapters.mtn import MTNMobileMoneyAdapter | ||
|
|
||
| async def main(): | ||
| mtn = MTNMobileMoneyAdapter( | ||
| api_key="YOUR_API_KEY", | ||
| subscription_key="YOUR_SUBSCRIPTION_KEY", | ||
| environment="sandbox" | ||
| ) | ||
|
|
||
| response = await mtn.send_payment( | ||
| amount=10.0, | ||
| phone_number="233541234567", | ||
| currency="GHS", | ||
| reference="order_123" | ||
| ) | ||
| print(response) | ||
|
|
||
| asyncio.run(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AdaptersRegistry.register()decorator to theMTNMobileMoneyAdapterclass.BASE_URLwithPRODUCTION_URLand addSANDBOX_URL.SUPPORTED_CURRENCIES,MIN_AMOUNT, andMAX_AMOUNT.__init__method.authenticatemethod withget_credentials.Please take an example from one of the files in
easyswitch/integrators/.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add the mtn.py file in the integrators/ folder not in adapters