This SDK allows Python applications to accept payments from MasterCard/Visa cardholders via the PayMaya API. Tested on Python 3.6, 3.7, and 3.8
I've tried to pattern the flow on the existing PayMaya SDK libraries as much as possible (ref: PayMaya PHP SDK)
- Python 3.6+
- requests
- pytest
python -m pip install git+https://github.com/jorgec/PayMayaPythonSDK--Wheel
To use PayMaya Python SDK, you need to have a different API key for Sandbox and Production environment.
Sandbox credentials are useful for testing application integration. All transactions and money flow made in this environment are only simulated and does not reflect your production records. The following sandbox API key can be used for testing purposes:
Public-facing API Key: pk-iaioBC2pbY6d3BVRSebsJxghSHeJDW4n6navI7tYdrN
Secret API Key: sk-uh4ZFfx9i0rZpKN6CxJ826nVgJ4saGGVAH9Hk7WrY6Q
Upon successful integration testing, you can then request for production credentials. Upon receipt, just change your SDK initialization to use production environment to start accepting live transactions.
The data models have 2 convenience functions: as_dict()
and serialize()
:
as_dict()
returns a Dict with keys matching the ones expected by the PayMaya API, and converting all data into JSON-safe types (str mostly)serialize()
returns a JSON dump ofas_dict()
from paymaya_sdk import PayMayaSDK
from api.payment_api import PaymentAPI
from models.amount_model import AmountModel
from models.buyer_model import BuyerModel
from models.card_model import CardModel
# for amounts
from decimal import Decimal
2. Initialize SDK with public-facing API key, secret API key, and the intended environment ("SANDBOX" or "PRODUCTION)
paymaya = PayMayaSDK()
paymaya.set_keys(
public_api_key=<PUBLIC_KEY>,
secret_api_key=<SECRET_KEY>,
environment=<"SANDBOX/PRODUCTION">
)
payment = paymaya.payment()
payment.card = CardModel(
number="5123456789012346",
expiry_month="12",
expiry_year="2025",
cvc="111",
card_type="MASTERCARD",
)
payment.create_token()
payment.amount = AmountModel(total=Decimal(1000), currency_code='PHP')
payment.buyer = BuyerModel(
first_name="Juan",
middle_name="De La",
last_name="Cruz",
phone='+639171234567',
email='randomemail@randomdomain.com',
address1='48584 Johnson Trace Suite ',
address2='2612 Jessica Meadow',
city='Quezon City',
state='Metro Manila',
zip_code='1101',
country='PH'
)
payment.execute_payment()
# returns a list of dictionaries with the json responses
payment.payments()
payment.get_payment(payment_id=<uuid>)
from models.amount_models import AmountModel, TotalAmountModel
from models.buyer_models import BuyerModel
from models.checkout_data_models import CheckoutDataModel
from models.checkout_item_models import CheckoutItemModel
from paymaya_sdk import PayMayaSDK
paymaya = PayMayaSDK()
# The provided sample API keys don't seem to work for the PayMaya checkout sandbox,
# but this encoded string from their docs does:
# https://s3-us-west-2.amazonaws.com/developers.paymaya.com.pg/checkout/checkout.html
# so I provided a parameter to override the keys as needed, for testing
paymaya.set_keys(
public_api_key=<PUBLIC_KEY>,
secret_api_key=<SECRET_KEY>,
environment=<"SANDBOX/PRODUCTION">,
encoded_key='cGstZW80c0wzOTNDV1U1S212ZUpVYVc4VjczMFRUZWkyelk4ekU0ZEhKRHhrRjo='
)
checkout = paymaya.checkout()
buyer = BuyerModel(
first_name="Juan",
last_name="Dela Cruz",
)
item = CheckoutItemModel()
item.name = "Crispy Pata"
item.code = "FOOD-CP"
item.quantity = "1"
item.amount = Amount(total=Decimal(200), currency_code='PHP')
item.total_amount = TotalAmountModel(
amount=AmountModel(
total=int(item.quantity) * item.amount.total,
currency_code=item.amount.curency_code
)
)
checkout_data = CheckoutDataModel()
checkout_data.total_amount = item.total_amount
checkout_data.buyer = buyer
checkout_data.items = [item.as_dict()]
checkout_data.request_reference_number = "REF-MERCHANT-GENERATED"
# optionally:
# checkout_data.redirect_urls = {
# "success": "http://www.askthemaya.com/",
# "failure": "http://www.askthemaya.com/failure?id=6319921",
# "cancel": "http://www.askthemaya.com/cancel?id=6319921"
# }
# and
# checkout_data.metadata = {...}
checkout.initiate(checkout_data)
result = checkout.execute()
2019-10-25:
- Card Vault
2019-10-24:
- Customer Vault
2019-10-23:
- Checkout functionality
- Trying out non-Singleton implementation
2019-10-22: Payment functionality is available for:
- creating tokens
- executing payments
- querying payments
Feel free to fork and clone, add documentation, etc. Pull requests very much appreciated.
For security flaws/bugs, please email me at codingforcatfood [at] protonmail.com instead of creating an issue.
Help feed my cats by sending a donation via PayPal to jorge.cosgayon [at] gmail.com