Skip to content

Commit 79ff3e5

Browse files
mrsaicharan1uds5501
authored andcommitted
feat: Implemented routes for creation & charging(invoice) through PayPal (#6296)
* feat: Implemented routes for creation & charging(invoice) * Edit - Status code * Hound issues resolution
1 parent ba3f0dc commit 79ff3e5

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

app/api/event_invoices.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import datetime
12
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
3+
from flask import jsonify, request
24

35
from app.api.bootstrap import api
46
from app.api.helpers.db import safe_query
@@ -10,6 +12,13 @@
1012
from app.models.discount_code import DiscountCode
1113
from app.models.event_invoice import EventInvoice
1214
from app.models.user import User
15+
from app.api.helpers.payment import PayPalPaymentsManager
16+
from app.api.helpers.errors import BadRequestError
17+
from app.api.helpers.db import save_to_db
18+
19+
20+
from app.api.helpers.permissions import jwt_required
21+
from app.api.orders import order_misc_routes
1322

1423

1524
class EventInvoiceList(ResourceList):
@@ -127,3 +136,59 @@ def before_get_object(self, view_kwargs):
127136
'methods': {
128137
'before_get_object': before_get_object
129138
}}
139+
140+
141+
@order_misc_routes.route('/event-invoices/<string:invoice_identifier>/create-paypal-payment', methods=['POST', 'GET'])
142+
@jwt_required
143+
def create_paypal_payment_invoice(invoice_identifier):
144+
"""
145+
Create a paypal payment.
146+
:return: The payment id of the created payment.
147+
"""
148+
try:
149+
return_url = request.json['data']['attributes']['return-url']
150+
cancel_url = request.json['data']['attributes']['cancel-url']
151+
except TypeError:
152+
return BadRequestError({'source': ''}, 'Bad Request Error').respond()
153+
154+
event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier')
155+
status, response = PayPalPaymentsManager.create_payment(event_invoice, return_url, cancel_url)
156+
157+
if status:
158+
return jsonify(status=True, payment_id=response)
159+
else:
160+
return jsonify(status=False, error=response)
161+
162+
163+
@order_misc_routes.route('/event-invoices/<string:invoice_identifier>/charge', methods=['POST', 'GET'])
164+
@jwt_required
165+
def charge_paypal_payment_invoice(invoice_identifier):
166+
"""
167+
Create a paypal payment.
168+
:return: The payment id of the created payment.
169+
"""
170+
try:
171+
paypal_payment_id = request.json['data']['attributes']['paypal_payment_id']
172+
paypal_payer_id = request.json['data']['attributes']['paypal_payer_id']
173+
except Exception as e:
174+
return BadRequestError({'source': e}, 'Bad Request Error').respond()
175+
event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier')
176+
# save the paypal payment_id with the order
177+
event_invoice.paypal_token = paypal_payment_id
178+
save_to_db(event_invoice)
179+
180+
# execute the invoice transaction.
181+
status, error = PayPalPaymentsManager.execute_payment(paypal_payer_id, paypal_payment_id)
182+
183+
if status:
184+
# successful transaction hence update the order details.
185+
event_invoice.paid_via = 'paypal'
186+
event_invoice.status = 'paid'
187+
event_invoice.transaction_id = paypal_payment_id
188+
event_invoice.completed_at = datetime.datetime.now()
189+
save_to_db(event_invoice)
190+
191+
return jsonify(status="Charge Successful", payment_id=paypal_payment_id)
192+
else:
193+
# return the error message from Paypal
194+
return jsonify(status="Charge Unsuccessful", error=error)

0 commit comments

Comments
 (0)