-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Implemented routes for creation & charging(invoice) through PayPal #6296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6a8cdf1
feat: Implemented routes for creation & charging(invoice)
mrsaicharan1 e06a201
Edit - Status code
mrsaicharan1 3470ba1
Hound issues resolution
mrsaicharan1 cbb7037
Merge branch 'development' into paypal-invoice
mrsaicharan1 538e981
Merge branch 'development' into paypal-invoice
uds5501 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import datetime | ||
| from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship | ||
| from flask import jsonify, request | ||
|
|
||
| from app.api.bootstrap import api | ||
| from app.api.helpers.db import safe_query | ||
|
|
@@ -10,6 +12,13 @@ | |
| from app.models.discount_code import DiscountCode | ||
| from app.models.event_invoice import EventInvoice | ||
| from app.models.user import User | ||
| from app.api.helpers.payment import PayPalPaymentsManager | ||
| from app.api.helpers.errors import BadRequestError | ||
| from app.api.helpers.db import save_to_db | ||
|
|
||
|
|
||
| from app.api.helpers.permissions import jwt_required | ||
| from app.api.orders import order_misc_routes | ||
|
|
||
|
|
||
| class EventInvoiceList(ResourceList): | ||
|
|
@@ -127,3 +136,59 @@ def before_get_object(self, view_kwargs): | |
| 'methods': { | ||
| 'before_get_object': before_get_object | ||
| }} | ||
|
|
||
|
|
||
| @order_misc_routes.route('/event-invoices/<string:invoice_identifier>/create-paypal-payment', methods=['POST', 'GET']) | ||
| @jwt_required | ||
| def create_paypal_payment_invoice(invoice_identifier): | ||
| """ | ||
| Create a paypal payment. | ||
| :return: The payment id of the created payment. | ||
| """ | ||
| try: | ||
| return_url = request.json['data']['attributes']['return-url'] | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cancel_url = request.json['data']['attributes']['cancel-url'] | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except TypeError: | ||
| return BadRequestError({'source': ''}, 'Bad Request Error').respond() | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier') | ||
| status, response = PayPalPaymentsManager.create_payment(event_invoice, return_url, cancel_url) | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if status: | ||
| return jsonify(status=True, payment_id=response) | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| return jsonify(status=False, error=response) | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @order_misc_routes.route('/event-invoices/<string:invoice_identifier>/charge', methods=['POST', 'GET']) | ||
| @jwt_required | ||
mrsaicharan1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def charge_paypal_payment_invoice(invoice_identifier): | ||
| """ | ||
| Create a paypal payment. | ||
| :return: The payment id of the created payment. | ||
| """ | ||
| try: | ||
| paypal_payment_id = request.json['data']['attributes']['paypal_payment_id'] | ||
| paypal_payer_id = request.json['data']['attributes']['paypal_payer_id'] | ||
| except Exception as e: | ||
| return BadRequestError({'source': e}, 'Bad Request Error').respond() | ||
| event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier') | ||
| # save the paypal payment_id with the order | ||
| event_invoice.paypal_token = paypal_payment_id | ||
| save_to_db(event_invoice) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'save_to_db' |
||
|
|
||
| # execute the invoice transaction. | ||
| status, error = PayPalPaymentsManager.execute_payment(paypal_payer_id, paypal_payment_id) | ||
|
|
||
| if status: | ||
| # successful transaction hence update the order details. | ||
| event_invoice.paid_via = 'paypal' | ||
| event_invoice.status = 'paid' | ||
| event_invoice.transaction_id = paypal_payment_id | ||
| event_invoice.completed_at = datetime.datetime.now() | ||
| save_to_db(event_invoice) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'save_to_db' |
||
|
|
||
| return jsonify(status="Charge Successful", payment_id=paypal_payment_id) | ||
| else: | ||
| # return the error message from Paypal | ||
| return jsonify(status="Charge Unsuccessful", error=error) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.