# PhonePe Payment Gateway Python Package
This Python package provides a simple interface to the PhonePe Payment Gateway. It can be used to create PhonePe payment links, check the status of PhonePe transactions, and refund PhonePe transactions.
## Installation
To install the package, run the following command:
```
pip install phonepe
```
## Usage
#### The following code shows how to create a PhonePe payment link:
* Initiate PhonePe Object
```python
from phonepe import PhonePe
phonepe = PhonePe("MERCHANT_ID", "PHONEPE_SALT", "PHONEPE_HOST", "REDIRECT_URL", "WEBHOOK_URL")
```
```csv
Args:
merchant_id (str): The ID of the merchant.
phone_pe_salt (str): The PhonePe salt.
redirect_url (str): The redirect URL.
webhook_url (str): The webhook URL.
phone_pe_salt_index (int, optional): The PhonePe salt index. Defaults to 1.
redirect_mode (str, optional): The redirect mode. Defaults to "GET". Valid Values "GET/POST"
```
* Create Transaction With Order Data
```python
order_data= phonepe.create_txn("ORDER_ID", 100, "USER_ID")
#Extract Payment Link
link = order_data["data"]["instrumentResponse"]["redirectInfo"]["url"]
print(link)
```
* The payload that is going to be sent to the merchant on the specified callback URL will have a base64 encoded JSON.
* Upon base64 decoding the response, you should get a JSON with a format similar to the response returned by transaction status API.
Following are the response headers sent with a callback.
* Please follow [this documentation](http://developer.phonepe.com/v1/reference/server-to-server-callback-5) if you are updating the paymenty status via
webhook
```csv
Args:
order_id (str): The ID of the order.
amount (int): The transaction amount in paise.
user (str): The user ID of the payment maker.
```
#### Example Response:
```json
{
"success": true,
"code": "SUCCESS",
"message": "Your request has been successfully completed.",
"data": {
"merchantId": "MERCHANTUAT",
"merchantTransactionId": "b7aa2cc7-cc5e-4d71-b98f-63ebf549010c",
"instrumentResponse": {
"type": "PAY_PAGE",
"redirectInfo": {
"url": "https://mercury-uat.phonepe.com/transact/pg?token=NGVjMzhjOWMzMGI5ODI2OWMwYmQ2MzUzYWE2ZDYzZGM0M2M0NjZkNjVjMWRmNzlmODk1YWEwNjViMTUwNjYyOTI4NDY1OWExYzNmMjQzNjYzZjgxOTQzYjVjMGUyMmYyZGZhMTg5ODRlZDM2MzEzNWYyZDViOTdkZmU2NjFjOGU3ZTdiMzNlNzpmM2ZkZDYwY2JmNGFiYTUxM2Y3OGJhNGVjOTQ5OWU1NQ",
"method": "GET"
}
}
}
}
```
### Check Transaction Status
The following code shows how to check the status of a PhonePe transaction:
```python
status = phonepe.check_txn_status("MERCHANT_TXN_ID")
print(status)
```
#### Example Response:
```json
{
"success": true,
"code": "PAYMENT_SUCCESS",
"message": "Your request has been successfully completed.",
"data": {
"merchantId": "FKRT",
"merchantTransactionId": "MT7850590068188104",
"transactionId": "T2111221437456190170379",
"amount": 100,
"paymentState": "COMPLETED",
"responseCode": "PAYMENT_SUCCESS",
"paymentInstrument": {
"type": "UPI",
"utr": "206378866112"
}
}
}
```
* S2S and Check Status API Handling
Once the customer is redirected back to the merchant website/app, merchants should check with their server if they have received the Server-to-Server Callback response. If not, it is mandatory to make a Transaction Status API check with PhonePe backend systems to know the actual status of the payment and, then accordingly process the result.
The payment status can be Success, Failed or Pending. When Pending, merchants should retry until the status changes to Success or Failed.
```python
if phonepe.verify_webhook_checksum(check_sum_in_header:str, b64_encoded_order_data:dict):
update_payment_status_in_db()
```
```html
Check Status API - Reconciliation [MANDATORY]
If the payment status is Pending, then Check Status API should be called in the following interval:
The first status check at 20-25 seconds post transaction start, then
Every 3 seconds once for the next 30 seconds,
Every 6 seconds once for the next 60 seconds,
Every 10 seconds for the next 60 seconds,
Every 30 seconds for the next 60 seconds, and then
Every 1 min until timeout (15 mins).
```
#### Refund A Transaction
```python
from phonepe import RefundTxn
refund_txn = RefundTxn(
txn_user_id="USER_ID",
merchant_order_id="ORDER_ID",
phonepe_txn_id="PHONEPE_TXN_ID",
amount=100, ##in paise
)
status = phonepe.refund_txn(refund_txn)
print(status)
```
* Example Response:
```json
{
"success": true,
"code": "PAYMENT_SUCCESS",
"message": "Your request has been successfully completed.",
"data": {
"merchantId": "MERCHANTUAT",
"merchantTransactionId": "ROD620471739210623",
"transactionId": "TR620471739210623",
"amount": 10000,
"state": "COMPLETED",
"responseCode": "SUCCESS"
}
}
```
#### Full Example
```python
from phonepe import PhonePe
from phonepe import RefundTxn
phonepe = PhonePe(
merchant_id="PGTESTPAYUAT",
phone_pe_salt="099eb0cd-02cf-4e2a-8aca-3e6c6aff0399",
phone_pe_host="https://api-preprod.phonepe.com/apis/pg-sandbox",
redirect_url="https://webhook.site/42fb84f2-1831-4467-9199-6bf0b839dc69",
webhook_url="https://webhook.site/42fb84f2-1831-4467-9199-6bf0b839dc69"
)
# Create PhonePe Order
order_data = phonepe.create_txn("OTGTRT156", 1000, "bijay")
payment_link = order_data["data"]["instrumentResponse"]["redirectInfo"]["url"]
# Check Transaction Status
txn_status = phonepe.check_txn_status(resp['data']['merchantTransactionId'])
# Refund PhonePe Order
refund = RefundTxn(txn_user_id="bijay", merchant_order_id="OTGTRT156", phonepe_txn_id="T2307112141199670477007",
amount=1000)
ref_resp = phonepe.refund_txn(refund)
```
For more information, please see the official [documentation](https://developer.phonepe.com/v1/reference/pay-api-1).
I hope this is helpful! Let me know if you have any other questions.
```
Pending Feature
* Subscription Module
* Submerchant Onboarding
Raw data
{
"_id": null,
"home_page": "https://github.com/devbijay/phonepe-pg",
"name": "phonepe",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Bijay Nayak",
"author_email": "bijay6779@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9e/a8/9fbfcf6bb2c764ed6a1ac607e27959e66e398e6fec0a855f3cc0e60be7d2/phonepe-1.0.1.tar.gz",
"platform": null,
"description": "\n# PhonePe Payment Gateway Python Package\n\nThis Python package provides a simple interface to the PhonePe Payment Gateway. It can be used to create PhonePe payment links, check the status of PhonePe transactions, and refund PhonePe transactions.\n\n## Installation\n\nTo install the package, run the following command:\n\n```\npip install phonepe\n```\n\n## Usage\n\n#### The following code shows how to create a PhonePe payment link:\n* Initiate PhonePe Object\n\n```python\nfrom phonepe import PhonePe\n\nphonepe = PhonePe(\"MERCHANT_ID\", \"PHONEPE_SALT\", \"PHONEPE_HOST\", \"REDIRECT_URL\", \"WEBHOOK_URL\")\n```\n\n```csv\nArgs:\n merchant_id (str): The ID of the merchant.\n phone_pe_salt (str): The PhonePe salt.\n redirect_url (str): The redirect URL.\n webhook_url (str): The webhook URL.\n phone_pe_salt_index (int, optional): The PhonePe salt index. Defaults to 1.\n redirect_mode (str, optional): The redirect mode. Defaults to \"GET\". Valid Values \"GET/POST\"\n```\n\n* Create Transaction With Order Data\n```python\norder_data= phonepe.create_txn(\"ORDER_ID\", 100, \"USER_ID\")\n\n#Extract Payment Link\nlink = order_data[\"data\"][\"instrumentResponse\"][\"redirectInfo\"][\"url\"]\n\nprint(link)\n```\n* The payload that is going to be sent to the merchant on the specified callback URL will have a base64 encoded JSON.\n* Upon base64 decoding the response, you should get a JSON with a format similar to the response returned by transaction status API.\nFollowing are the response headers sent with a callback.\n\n* Please follow [this documentation](http://developer.phonepe.com/v1/reference/server-to-server-callback-5) if you are updating the paymenty status via \nwebhook\n\n```csv\nArgs:\n order_id (str): The ID of the order.\n amount (int): The transaction amount in paise.\n user (str): The user ID of the payment maker.\n```\n#### Example Response:\n```json\n{\n \"success\": true,\n \"code\": \"SUCCESS\",\n \"message\": \"Your request has been successfully completed.\",\n \"data\": {\n \"merchantId\": \"MERCHANTUAT\",\n \"merchantTransactionId\": \"b7aa2cc7-cc5e-4d71-b98f-63ebf549010c\",\n \"instrumentResponse\": {\n \"type\": \"PAY_PAGE\",\n \"redirectInfo\": {\n \"url\": \"https://mercury-uat.phonepe.com/transact/pg?token=NGVjMzhjOWMzMGI5ODI2OWMwYmQ2MzUzYWE2ZDYzZGM0M2M0NjZkNjVjMWRmNzlmODk1YWEwNjViMTUwNjYyOTI4NDY1OWExYzNmMjQzNjYzZjgxOTQzYjVjMGUyMmYyZGZhMTg5ODRlZDM2MzEzNWYyZDViOTdkZmU2NjFjOGU3ZTdiMzNlNzpmM2ZkZDYwY2JmNGFiYTUxM2Y3OGJhNGVjOTQ5OWU1NQ\",\n \"method\": \"GET\"\n }\n }\n }\n}\n```\n### Check Transaction Status\n\nThe following code shows how to check the status of a PhonePe transaction:\n\n```python\n\nstatus = phonepe.check_txn_status(\"MERCHANT_TXN_ID\")\n\nprint(status)\n```\n#### Example Response:\n```json\n{\n \"success\": true,\n \"code\": \"PAYMENT_SUCCESS\",\n \"message\": \"Your request has been successfully completed.\",\n \"data\": {\n \"merchantId\": \"FKRT\",\n \"merchantTransactionId\": \"MT7850590068188104\",\n \"transactionId\": \"T2111221437456190170379\",\n \"amount\": 100,\n \"paymentState\": \"COMPLETED\",\n \"responseCode\": \"PAYMENT_SUCCESS\",\n \"paymentInstrument\": {\n \"type\": \"UPI\",\n \"utr\": \"206378866112\"\n }\n }\n}\n```\n\n* S2S and Check Status API Handling\n\nOnce the customer is redirected back to the merchant website/app, merchants should check with their server if they have received the Server-to-Server Callback response. If not, it is mandatory to make a Transaction Status API check with PhonePe backend systems to know the actual status of the payment and, then accordingly process the result.\n\nThe payment status can be Success, Failed or Pending. When Pending, merchants should retry until the status changes to Success or Failed.\n```python\nif phonepe.verify_webhook_checksum(check_sum_in_header:str, b64_encoded_order_data:dict):\n update_payment_status_in_db()\n```\n\n```html\nCheck Status API - Reconciliation [MANDATORY]\n\nIf the payment status is Pending, then Check Status API should be called in the following interval:\nThe first status check at 20-25 seconds post transaction start, then\nEvery 3 seconds once for the next 30 seconds,\nEvery 6 seconds once for the next 60 seconds,\nEvery 10 seconds for the next 60 seconds,\nEvery 30 seconds for the next 60 seconds, and then\nEvery 1 min until timeout (15 mins).\n```\n#### Refund A Transaction\n\n```python\nfrom phonepe import RefundTxn\n\nrefund_txn = RefundTxn(\n txn_user_id=\"USER_ID\",\n merchant_order_id=\"ORDER_ID\",\n phonepe_txn_id=\"PHONEPE_TXN_ID\",\n amount=100, ##in paise\n)\n\nstatus = phonepe.refund_txn(refund_txn)\n\nprint(status)\n```\n* Example Response:\n\n```json\n{\n \"success\": true,\n \"code\": \"PAYMENT_SUCCESS\",\n \"message\": \"Your request has been successfully completed.\",\n \"data\": {\n \"merchantId\": \"MERCHANTUAT\",\n \"merchantTransactionId\": \"ROD620471739210623\",\n \"transactionId\": \"TR620471739210623\",\n \"amount\": 10000,\n \"state\": \"COMPLETED\",\n \"responseCode\": \"SUCCESS\"\n }\n}\n```\n\n#### Full Example\n```python\nfrom phonepe import PhonePe\nfrom phonepe import RefundTxn\n\nphonepe = PhonePe(\n merchant_id=\"PGTESTPAYUAT\",\n phone_pe_salt=\"099eb0cd-02cf-4e2a-8aca-3e6c6aff0399\",\n phone_pe_host=\"https://api-preprod.phonepe.com/apis/pg-sandbox\",\n redirect_url=\"https://webhook.site/42fb84f2-1831-4467-9199-6bf0b839dc69\",\n webhook_url=\"https://webhook.site/42fb84f2-1831-4467-9199-6bf0b839dc69\"\n)\n\n# Create PhonePe Order\norder_data = phonepe.create_txn(\"OTGTRT156\", 1000, \"bijay\")\npayment_link = order_data[\"data\"][\"instrumentResponse\"][\"redirectInfo\"][\"url\"]\n\n# Check Transaction Status\ntxn_status = phonepe.check_txn_status(resp['data']['merchantTransactionId'])\n\n# Refund PhonePe Order\nrefund = RefundTxn(txn_user_id=\"bijay\", merchant_order_id=\"OTGTRT156\", phonepe_txn_id=\"T2307112141199670477007\",\n amount=1000)\nref_resp = phonepe.refund_txn(refund)\n```\n\nFor more information, please see the official [documentation](https://developer.phonepe.com/v1/reference/pay-api-1).\n\n\nI hope this is helpful! Let me know if you have any other questions.\n```\nPending Feature\n* Subscription Module\n* Submerchant Onboarding\n",
"bugtrack_url": null,
"license": "",
"summary": "A Python library for interacting with PhonePe Payment Gateway API.",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/devbijay/phonepe-pg"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "88df0a27a3cf20b3f36309b186e967402430b0d7d999dbca37dae1d860e9a29c",
"md5": "d5c71799aded897c480f6545f15f2093",
"sha256": "37bf4196608933430856324013ba2ab67a484362c08f4d68e044f2b9ca84fdc9"
},
"downloads": -1,
"filename": "phonepe-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d5c71799aded897c480f6545f15f2093",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5893,
"upload_time": "2023-07-12T16:33:00",
"upload_time_iso_8601": "2023-07-12T16:33:00.194147Z",
"url": "https://files.pythonhosted.org/packages/88/df/0a27a3cf20b3f36309b186e967402430b0d7d999dbca37dae1d860e9a29c/phonepe-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9ea89fbfcf6bb2c764ed6a1ac607e27959e66e398e6fec0a855f3cc0e60be7d2",
"md5": "473d44665cc3527a97cd31734de96952",
"sha256": "94eccc1207784ea2a01cb9febecad1fae585548131a9995052c0cd5d18318d9a"
},
"downloads": -1,
"filename": "phonepe-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "473d44665cc3527a97cd31734de96952",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5710,
"upload_time": "2023-07-12T16:33:01",
"upload_time_iso_8601": "2023-07-12T16:33:01.635917Z",
"url": "https://files.pythonhosted.org/packages/9e/a8/9fbfcf6bb2c764ed6a1ac607e27959e66e398e6fec0a855f3cc0e60be7d2/phonepe-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-12 16:33:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "devbijay",
"github_project": "phonepe-pg",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "setuptools",
"specs": [
[
"~=",
"65.5.1"
]
]
},
{
"name": "requests",
"specs": [
[
"~=",
"2.31.0"
]
]
},
{
"name": "pydantic",
"specs": [
[
"~=",
"2.0.2"
]
]
}
],
"lcname": "phonepe"
}