├── .gitignore ├── LICENSE ├── README.md ├── SECURITY.md ├── poetry.lock ├── pypep ├── __init__.py ├── client.py └── compat.py ├── pyproject.toml ├── setup.py └── tests └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | .idea 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # IPython Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # dotenv 80 | .env 81 | 82 | # virtualenv 83 | venv/ 84 | ENV/ 85 | 86 | # Spyder project settings 87 | .spyderproject 88 | 89 | # Rope project settings 90 | .ropeproject 91 | .vscode 92 | cert.xml 93 | main.py -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python SDK for Pasargad IPG 2 | 3 | Python SDK for Pasargad Internet Payment Gateway (RESTful API) 4 | 5 | ## Installation 6 | 7 | ```bash 8 | pip install pypep-pepco 9 | ``` 10 | 11 | ## Usage 12 | 13 | - Read API 14 | Documentation, [Click Here! (دانلود مستندات کامل درگاه پرداخت)](https://www.pep.co.ir/wp-content/uploads/2019/06/1-__PEP_IPG_REST-13971020.Ver3_.00.pdf) 15 | - Save your private key into an `.xml` file inside your project directory. 16 | 17 | ## Redirect User to Payment Gateway 18 | 19 | ```python 20 | from pypep import PasargadPaymentGateway 21 | 22 | # Create an object from PasargadPaymentGateway client 23 | # e.q: pasargad = PasargadPaymentGateway(123123,444444,"https://pep.co.ir/ipgtest","cert.xml") 24 | pasargad = PasargadPaymentGateway('', '', '', '') 25 | payment_url = pasargad.redirect( 26 | amount="15000", 27 | invoice_number="15001", 28 | invoice_date="2021/08/23 15:51:00", 29 | # mobile="091111", #optional 30 | # email="test@test.local" #optional 31 | ) 32 | ``` 33 | 34 | This method, will return a `string` like this: 35 | 36 | ```bash 37 | // output: 38 | // https://pep.shaparak.ir/payment.aspx?n=LySl+5tYkxL5qNMBRthW7DWzV8e3ALnTJUqiCS0V/io= 39 | // Redirect User to the generated URL to make payment 40 | ``` 41 | 42 | ## Checking and Verifying Transaction 43 | 44 | After Payment Process, User is going to be returned to your redirect_url 45 | 46 | payment gateway is going to answer the payment result with sending below parameters to your redirectURL ( 47 | as `QueryString` parameters): 48 | 49 | - InvoiceNumber (iN field) 50 | - InvoiceDate (iD field) 51 | - TransactionReferenceID (tref field) 52 | 53 | Store this information in a proper data storage and let's check transaction result by sending a check api request to the 54 | Bank: 55 | 56 | ```python 57 | from pypep import PasargadPaymentGateway 58 | 59 | # Create an object from PasargadPaymentGateway client 60 | # e.q: pasargad = PasargadPaymentGateway(123123,444444,"https://pep.co.ir/ipgtest","cert.xml") 61 | pasargad = PasargadPaymentGateway('', '', '', '') 62 | 63 | response = pasargad.check_transaction( 64 | reference_id="637653306794022509", 65 | invoice_number="15001", 66 | invoice_date="2021/08/23 15:51:00", 67 | ) 68 | ``` 69 | 70 | Successful result: 71 | 72 | ```json 73 | { 74 | "TraceNumber": 13, 75 | "ReferenceNumber": 100200300400500, 76 | "TransactionDate": "2021/08/08 11:58:23", 77 | "Action": "1003", 78 | "TransactionReferenceID": "636843820118990203", 79 | "InvoiceNumber": "4029", 80 | "InvoiceDate": "2021/08/08 11:54:03", 81 | "MerchantCode": 100123, 82 | "TerminalCode": 200123, 83 | "Amount": 15000, 84 | "IsSuccess": true, 85 | "Message": " " 86 | } 87 | ``` 88 | 89 | If you got `IsSuccess` with `true` value, so everything is O.K! 90 | 91 | Now, for your successful transaction, you should call `verifyPayment()` method to keep the money and Bank makes sure the 92 | checking process done properly: 93 | 94 | ```python 95 | from pypep import Pasargad 96 | 97 | # Create an object from Pasargad client 98 | # e.q: pasargad = Pasargad(123123,444444,"https://pep.co.ir/ipgtest","cert.xml") 99 | pasargad = PasargadPaymentGateway('', '', '', '') 100 | 101 | response = pasargad.verify_payment( 102 | amount="15000", 103 | invoice_number="15001", 104 | invoice_date="2021/08/23 15:51:00", 105 | ) 106 | ``` 107 | 108 | ...and the successful response looks like this response: 109 | 110 | ```json 111 | { 112 | "IsSuccess": true, 113 | "Message": " ", 114 | "MaskedCardNumber": "5022-29**-****-2328", 115 | "HashedCardNumber": "2DDB1E270C598677AE328AA37C2970E3075E1DB....", 116 | "ShaparakRefNumber": "100200300400500" 117 | } 118 | ``` 119 | 120 | ## Payment Refund 121 | 122 | If for any reason, you decided to cancel an order in early hours after taking the order (maximum 2 hours later), you can 123 | refund the client payment to his/her bank card. 124 | 125 | for this, use `refund()` method: 126 | 127 | ```python 128 | from pypep import PasargadPaymentGateway 129 | 130 | # Create an object from PasargadPaymentGateway client 131 | # e.q: pasargad = PasargadPaymentGateway(123123,444444,"https://pep.co.ir/ipgtest","cert.xml") 132 | pasargad = PasargadPaymentGateway('', '', '', '') 133 | 134 | response = pasargad.refund( 135 | invoice_number="15001", 136 | invoice_date="2021/08/23 15:51:00", 137 | ) 138 | ``` 139 | 140 | # Support 141 | 142 | Please use your credentials to login into [Support Panel](https://my.pep.co.ir) 143 | 144 | # Maintainers 145 | - [Amir Lesani](https://github.com/xenups) 146 | - [Reza Seyf](https://github.com/rseyf) 147 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | 5.1.x | :white_check_mark: | 11 | | 5.0.x | :x: | 12 | | 4.0.x | :white_check_mark: | 13 | | < 4.0 | :x: | 14 | 15 | ## Reporting a Vulnerability 16 | 17 | Reporting a Vulnerability 18 | Use this section to guide users on how to report a vulnerability. 19 | If you discover a security vulnerability within this project, please follow these steps to report it: 20 | Go to the Security Section: Visit the security section of this project's GitHub repository. 21 | Create a New Issue: Open a new issue and provide a detailed description of the vulnerability. Please include steps to reproduce, potential impact, and any relevant information to assist in understanding and resolving the issue. 22 | Expectation on Updates: You can expect an initial acknowledgment of your report within 48 hours. Subsequent updates on the status of the vulnerability and any actions taken will be provided regularly until the issue is resolved. 23 | Vulnerability Assessment: Upon receiving the report, it will be thoroughly assessed by the project maintainers to determine its validity and severity. 24 | Outcome: Depending on the assessment, the vulnerability may be accepted, leading to immediate action to remedy it, or declined if it does not pose a significant risk or does not align with the project's scope. 25 | Security Fixes: If the vulnerability is accepted, appropriate fixes will be developed and released in a timely manner. 26 | Thank you for helping to improve the security of this project. Your contributions are greatly appreciated. 27 | -------------------------------------------------------------------------------- /poetry.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. 2 | 3 | [[package]] 4 | name = "colorama" 5 | version = "0.4.6" 6 | description = "Cross-platform colored terminal text." 7 | optional = false 8 | python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" 9 | files = [ 10 | {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, 11 | {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, 12 | ] 13 | 14 | [[package]] 15 | name = "iniconfig" 16 | version = "2.0.0" 17 | description = "brain-dead simple config-ini parsing" 18 | optional = false 19 | python-versions = ">=3.7" 20 | files = [ 21 | {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, 22 | {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, 23 | ] 24 | 25 | [[package]] 26 | name = "packaging" 27 | version = "24.1" 28 | description = "Core utilities for Python packages" 29 | optional = false 30 | python-versions = ">=3.8" 31 | files = [ 32 | {file = "packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"}, 33 | {file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"}, 34 | ] 35 | 36 | [[package]] 37 | name = "pluggy" 38 | version = "1.5.0" 39 | description = "plugin and hook calling mechanisms for python" 40 | optional = false 41 | python-versions = ">=3.8" 42 | files = [ 43 | {file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"}, 44 | {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, 45 | ] 46 | 47 | [package.extras] 48 | dev = ["pre-commit", "tox"] 49 | testing = ["pytest", "pytest-benchmark"] 50 | 51 | [[package]] 52 | name = "pycryptodome" 53 | version = "3.21.0" 54 | description = "Cryptographic library for Python" 55 | optional = false 56 | python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7" 57 | files = [ 58 | {file = "pycryptodome-3.21.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:dad9bf36eda068e89059d1f07408e397856be9511d7113ea4b586642a429a4fd"}, 59 | {file = "pycryptodome-3.21.0-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:a1752eca64c60852f38bb29e2c86fca30d7672c024128ef5d70cc15868fa10f4"}, 60 | {file = "pycryptodome-3.21.0-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:3ba4cc304eac4d4d458f508d4955a88ba25026890e8abff9b60404f76a62c55e"}, 61 | {file = "pycryptodome-3.21.0-cp27-cp27m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7cb087b8612c8a1a14cf37dd754685be9a8d9869bed2ffaaceb04850a8aeef7e"}, 62 | {file = "pycryptodome-3.21.0-cp27-cp27m-musllinux_1_1_aarch64.whl", hash = "sha256:26412b21df30b2861424a6c6d5b1d8ca8107612a4cfa4d0183e71c5d200fb34a"}, 63 | {file = "pycryptodome-3.21.0-cp27-cp27m-win32.whl", hash = "sha256:cc2269ab4bce40b027b49663d61d816903a4bd90ad88cb99ed561aadb3888dd3"}, 64 | {file = "pycryptodome-3.21.0-cp27-cp27m-win_amd64.whl", hash = "sha256:0fa0a05a6a697ccbf2a12cec3d6d2650b50881899b845fac6e87416f8cb7e87d"}, 65 | {file = "pycryptodome-3.21.0-cp27-cp27mu-manylinux2010_i686.whl", hash = "sha256:6cce52e196a5f1d6797ff7946cdff2038d3b5f0aba4a43cb6bf46b575fd1b5bb"}, 66 | {file = "pycryptodome-3.21.0-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:a915597ffccabe902e7090e199a7bf7a381c5506a747d5e9d27ba55197a2c568"}, 67 | {file = "pycryptodome-3.21.0-cp27-cp27mu-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a4e74c522d630766b03a836c15bff77cb657c5fdf098abf8b1ada2aebc7d0819"}, 68 | {file = "pycryptodome-3.21.0-cp27-cp27mu-musllinux_1_1_aarch64.whl", hash = "sha256:a3804675283f4764a02db05f5191eb8fec2bb6ca34d466167fc78a5f05bbe6b3"}, 69 | {file = "pycryptodome-3.21.0-cp36-abi3-macosx_10_9_universal2.whl", hash = "sha256:2480ec2c72438430da9f601ebc12c518c093c13111a5c1644c82cdfc2e50b1e4"}, 70 | {file = "pycryptodome-3.21.0-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:de18954104667f565e2fbb4783b56667f30fb49c4d79b346f52a29cb198d5b6b"}, 71 | {file = "pycryptodome-3.21.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2de4b7263a33947ff440412339cb72b28a5a4c769b5c1ca19e33dd6cd1dcec6e"}, 72 | {file = "pycryptodome-3.21.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0714206d467fc911042d01ea3a1847c847bc10884cf674c82e12915cfe1649f8"}, 73 | {file = "pycryptodome-3.21.0-cp36-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7d85c1b613121ed3dbaa5a97369b3b757909531a959d229406a75b912dd51dd1"}, 74 | {file = "pycryptodome-3.21.0-cp36-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:8898a66425a57bcf15e25fc19c12490b87bd939800f39a03ea2de2aea5e3611a"}, 75 | {file = "pycryptodome-3.21.0-cp36-abi3-musllinux_1_2_i686.whl", hash = "sha256:932c905b71a56474bff8a9c014030bc3c882cee696b448af920399f730a650c2"}, 76 | {file = "pycryptodome-3.21.0-cp36-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:18caa8cfbc676eaaf28613637a89980ad2fd96e00c564135bf90bc3f0b34dd93"}, 77 | {file = "pycryptodome-3.21.0-cp36-abi3-win32.whl", hash = "sha256:280b67d20e33bb63171d55b1067f61fbd932e0b1ad976b3a184303a3dad22764"}, 78 | {file = "pycryptodome-3.21.0-cp36-abi3-win_amd64.whl", hash = "sha256:b7aa25fc0baa5b1d95b7633af4f5f1838467f1815442b22487426f94e0d66c53"}, 79 | {file = "pycryptodome-3.21.0-pp27-pypy_73-manylinux2010_x86_64.whl", hash = "sha256:2cb635b67011bc147c257e61ce864879ffe6d03342dc74b6045059dfbdedafca"}, 80 | {file = "pycryptodome-3.21.0-pp27-pypy_73-win32.whl", hash = "sha256:4c26a2f0dc15f81ea3afa3b0c87b87e501f235d332b7f27e2225ecb80c0b1cdd"}, 81 | {file = "pycryptodome-3.21.0-pp310-pypy310_pp73-macosx_10_15_x86_64.whl", hash = "sha256:d5ebe0763c982f069d3877832254f64974139f4f9655058452603ff559c482e8"}, 82 | {file = "pycryptodome-3.21.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7ee86cbde706be13f2dec5a42b52b1c1d1cbb90c8e405c68d0755134735c8dc6"}, 83 | {file = "pycryptodome-3.21.0-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0fd54003ec3ce4e0f16c484a10bc5d8b9bd77fa662a12b85779a2d2d85d67ee0"}, 84 | {file = "pycryptodome-3.21.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:5dfafca172933506773482b0e18f0cd766fd3920bd03ec85a283df90d8a17bc6"}, 85 | {file = "pycryptodome-3.21.0-pp39-pypy39_pp73-macosx_10_15_x86_64.whl", hash = "sha256:590ef0898a4b0a15485b05210b4a1c9de8806d3ad3d47f74ab1dc07c67a6827f"}, 86 | {file = "pycryptodome-3.21.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f35e442630bc4bc2e1878482d6f59ea22e280d7121d7adeaedba58c23ab6386b"}, 87 | {file = "pycryptodome-3.21.0-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ff99f952db3db2fbe98a0b355175f93ec334ba3d01bbde25ad3a5a33abc02b58"}, 88 | {file = "pycryptodome-3.21.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:8acd7d34af70ee63f9a849f957558e49a98f8f1634f86a59d2be62bb8e93f71c"}, 89 | {file = "pycryptodome-3.21.0.tar.gz", hash = "sha256:f7787e0d469bdae763b876174cf2e6c0f7be79808af26b1da96f1a64bcf47297"}, 90 | ] 91 | 92 | [[package]] 93 | name = "pytest" 94 | version = "8.3.3" 95 | description = "pytest: simple powerful testing with Python" 96 | optional = false 97 | python-versions = ">=3.8" 98 | files = [ 99 | {file = "pytest-8.3.3-py3-none-any.whl", hash = "sha256:a6853c7375b2663155079443d2e45de913a911a11d669df02a50814944db57b2"}, 100 | {file = "pytest-8.3.3.tar.gz", hash = "sha256:70b98107bd648308a7952b06e6ca9a50bc660be218d53c257cc1fc94fda10181"}, 101 | ] 102 | 103 | [package.dependencies] 104 | colorama = {version = "*", markers = "sys_platform == \"win32\""} 105 | iniconfig = "*" 106 | packaging = "*" 107 | pluggy = ">=1.5,<2" 108 | 109 | [package.extras] 110 | dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] 111 | 112 | [metadata] 113 | lock-version = "2.0" 114 | python-versions = "^3.13" 115 | content-hash = "a3ebb30cf00f301adacfb6f5cc9558e1fe3538ece3eff9a8f04068bff4cedce4" 116 | -------------------------------------------------------------------------------- /pypep/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | from __future__ import absolute_import, unicode_literals 3 | from .client import Pasargad, ApiError 4 | 5 | 6 | def get_version(): 7 | return '.'.join(map(str, VERSION)) 8 | 9 | 10 | VERSION = (0, 1, 1) 11 | __version__ = get_version() 12 | -------------------------------------------------------------------------------- /pypep/client.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | from __future__ import absolute_import 3 | 4 | import json 5 | from base64 import b64decode, b64encode 6 | from datetime import datetime 7 | from typing import Any, Dict 8 | from xml.dom import minidom 9 | 10 | from Crypto.Cipher import PKCS1_OAEP 11 | from Crypto.Hash import SHA1 12 | from Crypto.PublicKey import RSA 13 | from Crypto.Util import number 14 | 15 | from .compat import HTTPError, Request, urlopen 16 | 17 | 18 | # API Error for catching Exceptions 19 | class ApiError(Exception): 20 | def __init__(self, code, description=""): 21 | super(ApiError, self).__init__("Result code: %s (%s)" % (code, description)) 22 | self.code = code 23 | 24 | 25 | class PasargadApiError(Exception): 26 | def __init__(self, code, description=""): 27 | super(PasargadApiError, self).__init__( 28 | "Result code: %s (%s)" % (code, description) 29 | ) 30 | self.code = code 31 | 32 | 33 | class PasargadCryptor: 34 | ACTION_PURCHASE = "1003" 35 | 36 | def __init__(self, key_pair: RSA): 37 | self.key_pair = key_pair 38 | 39 | def make_signature(self, data: Dict[str, Any]) -> bytes: 40 | serialized_data = json.dumps(data).encode("utf-8") 41 | hash_value = SHA1.new(serialized_data) 42 | 43 | # Use OAEP padding with RSA for secure encryption and signing 44 | cipher = PKCS1_OAEP.new(self.key_pair) 45 | 46 | # Generate the signature with OAEP padding 47 | signature = cipher.encrypt(hash_value.digest()) 48 | 49 | return b64encode(signature) 50 | 51 | 52 | class PasargadXmlKeyLoader: 53 | @staticmethod 54 | def _process_xml_node(nodelist): 55 | text_data = [] 56 | for node in nodelist: 57 | if node.nodeType == node.TEXT_NODE: 58 | text_data.append(node.data) 59 | combined_text = "".join(text_data) 60 | return number.bytes_to_long(b64decode(combined_text)) 61 | 62 | @staticmethod 63 | def _convert_xml_key_to_pem(xml_private_key_file: str) -> RSA: 64 | with open(xml_private_key_file, "rb") as file: 65 | xml_key = file.read() 66 | doc = minidom.parseString(xml_key) 67 | modulus = PasargadXmlKeyLoader._get_element_value(doc, "Modulus") 68 | exponent = PasargadXmlKeyLoader._get_element_value(doc, "Exponent") 69 | d = PasargadXmlKeyLoader._get_element_value(doc, "D") 70 | p = PasargadXmlKeyLoader._get_element_value(doc, "P") 71 | q = PasargadXmlKeyLoader._get_element_value(doc, "Q") 72 | return RSA.construct((modulus, exponent, d, p, q)) 73 | 74 | @staticmethod 75 | def _get_element_value(doc, element_name: str) -> int: 76 | element = doc.getElementsByTagName(element_name)[0] 77 | return number.bytes_to_long(b64decode(element.childNodes[0].data)) 78 | 79 | 80 | class PasargadPaymentGateway: 81 | URL_GET_TOKEN = "https://pep.shaparak.ir/Api/v1/Payment/GetToken" 82 | URL_PAYMENT_GATEWAY = "https://pep.shaparak.ir/payment.aspx" 83 | URL_CHECK_TRANSACTION = ( 84 | "https://pep.shaparak.ir/Api/v1/Payment/CheckTransactionResult" 85 | ) 86 | URL_VERIFY_PAYMENT = "https://pep.shaparak.ir/Api/v1/Payment/VerifyPayment" 87 | URL_REFUND = "https://pep.shaparak.ir/Api/v1/Payment/RefundPayment" 88 | 89 | def __init__(self, cryptor: PasargadCryptor): 90 | self.cryptor = cryptor 91 | 92 | @staticmethod 93 | def _generate_timestamp(): 94 | return datetime.today().strftime("%Y/%m/%d %H:%M:%S") 95 | 96 | def _request_builder(self, url, data, method="POST"): 97 | request = Request( 98 | url, 99 | headers={ 100 | "Accept": "application/json", 101 | "Sign": self.cryptor.make_signature(data), 102 | }, 103 | ) 104 | payload = json.dumps(data) 105 | request.data = payload.encode("utf-8") 106 | request.add_header("Content-Type", "application/json") 107 | request.get_method = lambda: method 108 | try: 109 | response = urlopen(request) 110 | except HTTPError as e: 111 | response = e 112 | response = json.loads(response.read().decode("utf-8")) 113 | if not response["IsSuccess"]: 114 | raise PasargadApiError(400, response["Message"]) 115 | return response 116 | 117 | def generate_payment_url( 118 | self, 119 | amount: str, 120 | invoice_number: str, 121 | invoice_date: str, 122 | mobile: str = "", 123 | email: str = "", 124 | ) -> str: 125 | params = { 126 | "amount": amount, 127 | "invoice_number": invoice_number, 128 | "invoice_date": invoice_date, 129 | "mobile": mobile, 130 | "email": email, 131 | "action": PasargadCryptor.ACTION_PURCHASE, 132 | "time_stamp": self._generate_timestamp(), 133 | } 134 | response = self._request_builder(self.URL_GET_TOKEN, params, "POST") 135 | return f"{self.URL_PAYMENT_GATEWAY}?n={response['token']}" 136 | 137 | def check_transaction( 138 | self, reference_id: str, invoice_number: str, invoice_date: str 139 | ) -> dict: 140 | params = { 141 | "transactionReferenceID": reference_id, 142 | "invoiceNumber": invoice_number, 143 | "invoiceDate": invoice_date, 144 | } 145 | return self._request_builder(self.URL_CHECK_TRANSACTION, params, "POST") 146 | 147 | def verify_payment( 148 | self, amount: str, invoice_number: str, invoice_date: str 149 | ) -> dict: 150 | params = { 151 | "amount": amount, 152 | "invoiceNumber": invoice_number, 153 | "invoiceDate": invoice_date, 154 | "timeStamp": self._generate_timestamp(), 155 | } 156 | response = self._request_builder(self.URL_VERIFY_PAYMENT, params, "POST") 157 | return response 158 | 159 | def refund(self, invoice_number: str, invoice_date: str) -> dict: 160 | params = { 161 | "invoiceNumber": invoice_number, 162 | "invoiceDate": invoice_date, 163 | "timeStamp": self._generate_timestamp(), 164 | } 165 | response = self._request_builder(self.URL_REFUND, params, "POST") 166 | return response 167 | -------------------------------------------------------------------------------- /pypep/compat.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | import sys 3 | 4 | if sys.version_info[0] == 3: 5 | from urllib.parse import urlencode 6 | from urllib.request import Request, urlopen 7 | from urllib.error import HTTPError 8 | else: 9 | from urllib import urlencode 10 | from urllib2 import HTTPError, Request, urlopen 11 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "python-rest-sdk" 3 | version = "0.1.1" 4 | description = "" 5 | authors = ["xenups "] 6 | readme = "README.md" 7 | 8 | [tool.poetry.dependencies] 9 | python = "^3.13" 10 | pycryptodome = "^3.21.0" 11 | pytest = "^8.3.3" 12 | 13 | 14 | [build-system] 15 | requires = ["poetry-core"] 16 | build-backend = "poetry.core.masonry.api" 17 | 18 | [tool.pytest.ini_options] 19 | testpaths = ["tests"] -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup 5 | from pypep import __version__ 6 | 7 | setup( 8 | name='pypep-pepco', 9 | version=__version__, 10 | author='Reza Seyf', 11 | author_email='rseyf@hotmail.com', 12 | description='Python SDK for Pasargad Internet Payment Gateway', 13 | license='Apache-2', 14 | url='https://github.com/pepco-api/python-rest-sdk ', 15 | packages=['pypep'], 16 | test_suite='tests', 17 | tests_require=[ 18 | 'httpretty' 19 | ], 20 | classifiers=[ 21 | 'Development Status :: 5 - Production/Stable', 22 | 'Intended Audience :: Developers', 23 | 'License :: OSI Approved :: Apache-2 License', 24 | 'Operating System :: OS Independent', 25 | 'Programming Language :: Python', 26 | 'Programming Language :: Python :: 2', 27 | 'Programming Language :: Python :: 2.7', 28 | 'Programming Language :: Python :: 3', 29 | 'Programming Language :: Python :: 3.5', 30 | 'Programming Language :: Python :: 3.6', 31 | 'Topic :: Software Development :: Libraries :: Python Modules', 32 | ], 33 | ) 34 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------