{ "info": { "author": "Omar Ryhan", "author_email": "omarryhan@gmail.com", "bugtrack_url": null, "classifiers": [ "Operating System :: OS Independent", "Programming Language :: Python :: 3.7" ], "description": "

\n \"Logo\"\n

\n \"Software\n \"Downloads\"\n \"Monthly\n \"Code\n

\n

\n\n# Aiopaypal\n\nAsync Wrapper for Paypal's REST API\n\n## Setup \u2699\ufe0f\n\n```bash\n$ pip install aiopaypal\n```\n\n## Dependencies\n\n- aiohttp\n- aiofiles\n- pyopenssl\n\n## Usage\n\n### Init\n\n```python 3.7\nfrom aiopaypal import Paypal\n\naiopaypal = Paypal(\n mode='live',\n client_id='client_id',\n client_secret='client_secret',\n)\n```\n\n### Create a user subscription\n\n**1. Create a payment experience (Optional) (Do only once)**:\n\n```python 3.7\npayment_experience = await aiopaypal.post(\n url='/v1/payment-experience/web-profiles'\n json={\n 'name': 'Payment profile name',\n 'presentation': {\n 'logo_image': 'https://brand-logo.png,\n 'brand_name': 'Brand Name'\n },\n 'flow_config': {\n 'landing_page_type': 'Billing',\n 'user_action': 'commit',\n 'return_uri_http_method': 'GET'\n },\n 'input_fields': {\n 'no_shipping': 1, # No shipping address (digital goods)\n },\n 'temporary': False\n }\n\n)\n```\n\n**2. Create a billing plan (Where you specify the details of your plan) (Do only once)**:\n\n```python 3.7\nbilling_plan = await aiopaypal.post(\n url='/v1/payments/billing-plans',\n json={\n \"name\": 'Name of the plan',\n \"description\": 'Description of the plan',\n \"type\": \"INFINITE\",\n \"payment_definitions\": [\n {\n \"name\": 'Name of the payment,\n \"cycles\": \"0\",\n \"frequency\": \"MONTH\",\n \"frequency_interval\": \"1\",\n \"type\": \"REGULAR\",\n \"amount\": {\n \"value\": str(123),\n \"currency\": 'usd'\n },\n }\n ],\n \"merchant_preferences\": {\n \"setup_fee\": {\n \"value\": str(123),\n \"currency\": currency\n },\n \"auto_bill_amount\": \"yes\", # Default \"NO\",\n 'return_url': 'https://example.com/payment/success-callback',\n 'cancel_url': 'https://example.com/payment/cancel-callback,\n \"initial_fail_amount_action\": \"cancel\", # Default CONTINUE\n \"max_fail_attempts\": \"3\",\n \"auto_bill_amount\": \"YES\",\n }\n }\n)\n```\n\n**3. Create webhooks to listen for subscription events (Do only once)**:\n\n```python 3.7\nhook_profile = await aiopaypal.post(\n url='/v1/notifications/webhooks',\n json={\n url='https://example.com/webhook/',\n event_types=[\n {'name': 'BILLING.SUBSCRIPTION.CANCELLED'},\n {'name': 'BILLING.SUBSCRIPTION.SUSPENDED'},\n {'name': 'BILLING.SUBSCRIPTION.RE-ACTIVATED'},\n ]\n }\n)\n```\n\n**4. Create a billing agreement (Where you bind a user to the billing plan created at \"2.\") and execute it**:\n\n```python 3.7\nasync def create_agreement():\n return await aiopaypal.post(\n url='',\n json={\n 'name': 'Agreement name',\n 'description': 'Agreement Description',\n 'start_date': (\n datetime.datetime.utcnow() + \\\n datetime.timedelta(days=1)\n ).isoformat()[:-7] + 'Z' # The start date must be no less than 24 hours after the current date as the agreement can take up to 24 hours to activate.\n 'plan': {\n 'id': billing_plan['id']\n },\n 'payer': {\n 'payment_method': 'paypal',\n 'payer_info': {\n 'email': 'email@email.email'\n }\n }\n }\n )\n\ndef get_execute_from_response(response):\n for link in response['links']:\n if link['rel'] == 'execute':\n return link['href']\n```\n\n**4.1 Create an agreement**:\n\n```python 3.7\n@app.route('/create-agreement)\nasync def create_agreement():\n billing_agreement = await create_agreement()\n return make_user_open(get_execute_from_response(billing_agreement))\n```\n\n**4.2 Activate on success**:\n\n```python 3.7\n# Second step (user callback)\n@app.route('/success-callback', methods=['GET'])\nasync def finalize_agreement(request):\n token = request.args.get('token')\n\n user_id = request['session']['user_id']\n\n active_agreement = await aiopaypal.post(\n '/v1/payments/billing-agreements/{}/agreement-execute'.format(\n token\n ),\n extra_headers={'Content-Type': 'application/json'}\n )\n\n if active_agreement['state'].lower() != 'active' and \\\n active_agreement['state'].lower() != 'pending':\n else:\n await store_user_agreement_id(user_id, active_agreement['id'])\n activate_premium_product(user_id)\n\n return_to_user('Payment {}'.format(active_agreement['state']))\n```\n\n**5. Listen to agreement changes**:\n\n```python 3.7\n@app.route('/webhook', methods=['POST', 'GET'])\nasync def hook(request):\n try:\n await aiopaypal.verify_from_headers(\n webhook_id=webhook['id'], # webhook response from \"3.\"\n event_body=request.body.decode(),\n headers=headers\n )\n except PaypalError as e:\n logger.exception(e)\n return\n else:\n event = request.json\n\n event_type = event.get('event_type')\n\n agreement_id = event['resource']['id']\n\n if event_type == 'BILLING.SUBSCRIPTION.SUSPENDED':\n logger.info('Billing agreement {} suspended'.format(agreement_id))\n await suspend_agreement_by_agreement_id(\n agreement_id\n )\n\n elif event_type == 'BILLING.SUBSCRIPTION.CANCELLED':\n logger.info('Billing agreement {} cancelled'.format(agreement_id))\n await cancel_agreement_by_id(\n agreement_id\n )\n\n elif event_type == 'BILLING.SUBSCRIPTION.RE-ACTIVATED':\n logger.info(\n 'Agreement with ID: {} REACTIVATED'.format(\n agreement_id\n )\n )\n await reactivate_agreement_by_id(\n agreement_id\n )\n\n elif event_type == 'PAYMENT.SALE.PENDING' or \\\n event_type == 'PAYMENT.ORDER.CREATED' or \\\n event_type == 'BILLING.SUBSCRIPTION.CREATED':\n logger.info('Payment/Subscription Created')\n\n else:\n logger.critical(\n 'Got unexpected event type {}'.format(event['resource']['id'])\n )\n\n finally:\n # must return 200, else Paypal won't stop sending\n return response.text('OK')\n```\n\n### Create a user payment\n\n ... Figured it out? Help others and make a pull request :)\n\n## Contact \ud83d\udce7\n\nI currently work as a freelance software devloper. Like my work and got a gig for me?\n\nWant to hire me fulltime? Send me an email @ omarryhan@gmail.com\n\n## Buy me a coffee \u2615\n\n**Bitcoin:** 3NmywNKr1Lzo8gyNXFUnzvboziACpEa31z\n\n**Ethereum:** 0x1E1400C31Cd813685FE0f6D29E0F91c1Da4675aE\n\n**Bitcoin Cash:** qqzn7rsav6hr3zqcp4829s48hvsvjat4zq7j42wkxd\n\n**Litecoin:** MB5M3cE3jE4E8NwGCWoFjLvGqjDqPyyEJp\n\n**Paypal:** https://paypal.me/omarryhan", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/omarryhan/aiopaypal", "keywords": "", "license": "GNU", "maintainer": "", "maintainer_email": "", "name": "aiopaypal", "package_url": "https://pypi.org/project/aiopaypal/", "platform": "", "project_url": "https://pypi.org/project/aiopaypal/", "project_urls": { "Homepage": "https://github.com/omarryhan/aiopaypal" }, "release_url": "https://pypi.org/project/aiopaypal/0.1.4/", "requires_dist": null, "requires_python": "", "summary": "Async Paypal client", "version": "0.1.4" }, "last_serial": 5362641, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "870ce7a2f382a293add6cc121546b375", "sha256": "c3909d3c70535cfc29b95bdf224cb1d98fb0918143d7b0dcc4bd494b76efdf54" }, "downloads": -1, "filename": "aiopaypal-0.1.0.tar.gz", "has_sig": false, "md5_digest": "870ce7a2f382a293add6cc121546b375", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4945, "upload_time": "2019-03-30T08:21:58", "url": "https://files.pythonhosted.org/packages/62/fb/5e5c5246be1f7b2a153ee09ac2a05f8b998a06acc1c96d49687592b55bf7/aiopaypal-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "47322d47ea943b2deaf21b88a781b3ba", "sha256": "de7b8b7a0911286ff21dd164029c855ba1a5148af7cda61a7c6c1309d89e69a4" }, "downloads": -1, "filename": "aiopaypal-0.1.1.tar.gz", "has_sig": false, "md5_digest": "47322d47ea943b2deaf21b88a781b3ba", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4970, "upload_time": "2019-03-30T08:31:55", "url": "https://files.pythonhosted.org/packages/e8/e8/0c0ed5f88ad8a9f5f877b31143c4d880994d046075bf0fd81167c8545cff/aiopaypal-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "fba7d7b162f2d935432d9e6c95982380", "sha256": "43a33befd20b74e7be3eecb0fbdaadca46c0667f0ac9f07763e1348972dbc3c5" }, "downloads": -1, "filename": "aiopaypal-0.1.2.tar.gz", "has_sig": false, "md5_digest": "fba7d7b162f2d935432d9e6c95982380", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5047, "upload_time": "2019-03-31T14:21:42", "url": "https://files.pythonhosted.org/packages/f4/07/0210863312b9d9a9fecd63b420a3bd851ddf0cf03a226cd01895a9cb49f4/aiopaypal-0.1.2.tar.gz" } ], "0.1.3": [ { "comment_text": "", "digests": { "md5": "cfafcf7a7eca20adbfee99c8f9a50a90", "sha256": "9612529e7875a7fd28bec481b29b3309591b9e434fad8b02d7bd634b674b1c9d" }, "downloads": -1, "filename": "aiopaypal-0.1.3.tar.gz", "has_sig": false, "md5_digest": "cfafcf7a7eca20adbfee99c8f9a50a90", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5051, "upload_time": "2019-03-31T14:27:24", "url": "https://files.pythonhosted.org/packages/85/78/c08567640a2939692bc71c7fdc824a5e782624c1ff64156da8e730ae4d86/aiopaypal-0.1.3.tar.gz" } ], "0.1.4": [ { "comment_text": "", "digests": { "md5": "73a9974a3b265e02d4e2c6f433823c22", "sha256": "f9199d78f55a0fcea5c3831f7770c3e57cdd762e85e743dc1224038af45089e3" }, "downloads": -1, "filename": "aiopaypal-0.1.4.tar.gz", "has_sig": false, "md5_digest": "73a9974a3b265e02d4e2c6f433823c22", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10382, "upload_time": "2019-06-05T14:00:40", "url": "https://files.pythonhosted.org/packages/43/4b/4434949bf84542e2ca79c20e5bfb65b26d4442d288e6c03ea5cd76e3e995/aiopaypal-0.1.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "73a9974a3b265e02d4e2c6f433823c22", "sha256": "f9199d78f55a0fcea5c3831f7770c3e57cdd762e85e743dc1224038af45089e3" }, "downloads": -1, "filename": "aiopaypal-0.1.4.tar.gz", "has_sig": false, "md5_digest": "73a9974a3b265e02d4e2c6f433823c22", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10382, "upload_time": "2019-06-05T14:00:40", "url": "https://files.pythonhosted.org/packages/43/4b/4434949bf84542e2ca79c20e5bfb65b26d4442d288e6c03ea5cd76e3e995/aiopaypal-0.1.4.tar.gz" } ] }