Package: nagadpy
Nagad Payment Package READMEThis README provides an overview of the Nagad Payment Package, its components, and usage examples. The package is designed to facilitate payment processing with the Nagad payment gateway using Python and FastAPI.Table of ContentsIntroductionInstallationConfigurationUsageInitiating a PaymentHandling Payment CallbacksVerifying a PaymentError HandlingContributingLicense1. IntroductionThe Nagad Payment Package allows you to easily integrate Nagad's payment gateway into your Python applications. It provides functionality for initiating payments, handling payment callbacks, and verifying payments. This package is designed to work with the Nagad payment gateway API.2. InstallationYou can install the Nagad Payment Package using pip:pipinstallnagadpy3. ConfigurationBefore using the package, you need to configure it with your Nagad merchant information. You'll need the following information:MERCHANT_ID: Your Nagad merchant ID.BASE_URL: The base URL of the Nagad payment gateway API.CALLBACK_URL: The URL where Nagad will send payment callbacks.PUBLIC_KEY: Your Nagad public key.PRIVATE_KEY: Your Nagad private key.You can set these values as environment variables or hardcode them in your application. Make sure to keep your private key secure.4. UsageInitiating a PaymentTo initiate a payment, you can create an instance of theNagadPaymentclass and call thecheckout_processmethod. Here's an example using FastAPI:fromfastapiimportFastAPIfromnagadpyimportNagadPaymentapp=FastAPI()# Initialize NagadPayment with your merchant informationnagad_payment=NagadPayment(merchant_id="YOUR_MERCHANT_ID",callback_url="YOUR_CALLBACK_URL",base_url="NAGAD_API_BASE_URL",public_key="YOUR_PUBLIC_KEY",private_key="YOUR_PRIVATE_KEY",)@app.get("/")definitiate_payment():try:payment_response=nagad_payment.checkout_process(amount="50.00",invoice_number="YOUR_INVOICE_NUMBER",)# Handle the payment response, which contains the payment URL and status.# you can redirect url or you can send url to frontend based on statusprint(payment_response)#  {'callBackUrl': 'https://sandbox-ssl.mynagad.com:10061/check-out/MDkyMjIzMD', 'status': 'Success'}exceptExceptionase:# Handle any exceptions that may occur during the payment processlogger.error(str(e))return{"payment_url":payment_response.get("callBackUrl"),"message":"success"}Handling Payment CallbacksNagad will send payment callbacks to theCALLBACK_URLyou provided when initiating the payment. You can create an endpoint in your FastAPI application to handle these callbacks. Here's an example:fromfastapiimportFastAPI,RequestfromnagadpyimportNagadPaymentVerifyapp=FastAPI()@app.get("/payment-response")@app.get("/payment-response",response_class=RedirectResponse,status_code=302):payment_response=dict(request.query_params)# or you can use payment_verify.parse_payment_response(query_string=params).# in that case you need to pass query string as str# handle payment_responseorder_id=payment_response.get("order_id")ifpayment_response.get("status")!="Success":print("Payment failed or cancel.Do the change your db")returnf"http://127.0.0.1:8000/payment-status/?status=failed&user_tnx_ref={order_id}"# Verify the payment using the payment reference ID received in the callbackpayment_verify=NagadPaymentVerify(base_url="NAGAD_API_BASE_URL")payment_reference_id=payment_response.get("payment_ref_id")verification_result=payment_verify.verify_payment(payment_reference_id)# Handle the payment verification resultif(verification_result.get("statusCode")=="000"andverification_result.get("status")=="Success"andverification_result.get("issuerPaymentRefNo")):print("Do your business logic")returnf"http://127.0.0.1:8000/payment-status/?status=success&user_tnx_ref={order_id}"else:print("Payment failed or cancel.Do the change your db")returnf"http://127.0.0.1:8000/payment-status/?status=failed&user_tnx_ref={order_id}"Verifying a PaymentTo verify a payment, you can use theverify_paymentmethod of theNagadPaymentVerifyclass. This method sends a GET request to the Nagad payment verification endpoint. Here's an example:fromnagadpyimportNagadPaymentVerify# Initialize NagadPaymentVerify with the Nagad API base URLpayment_verify=NagadPaymentVerify(base_url="NAGAD_API_BASE_URL")# Specify the payment reference ID to verifypayment_reference_id="YOUR_PAYMENT_REFERENCE_ID"# Verify the paymentverification_result=payment_verify.verify_payment(payment_reference_id)# Handle the payment verification resultprint(verification_result)5. Error HandlingThe Nagad Payment Package provides custom exceptions for different stages of the payment process. You can catch and handle these exceptions to manage errors gracefully. The available exceptions include:PaymentInitiationError: Raised when an error occurs during payment initiation.PaymentCompleteError: Raised when an error occurs during payment completion.PaymentVerificationError: Raised when an error occurs during payment verification.DecryptionError: Raised when an error occurs during data decryption.EncryptionError: Raised when an error occurs during data encryption.RequestError: Raised when there is an issue with the HTTP request.SignatureGenerationError: Raised when an error occurs during signature generation.CheckProcessError: Raised when an error occurs during checkout process.Make sure to handle these exceptions appropriately in your application.6. ContributingIf you'd like to contribute to the Nagad Payment Package, please follow the standard open-source contribution guidelines, including creating issues, submitting pull requests, and adhering to the project's coding standards.7. LicenseThe Nagad Payment Package is provided under theMIT License. You are free to use, modify, and distribute it according to the terms of the license.This README provides a basic overview of the Nagad Payment Package. For more detailed information and examples, refer to the package's documentation and source code.