Skip to content

Commit 548f148

Browse files
committed
feat: Implemented routes for creation & charging(invoice)
1 parent 9fc0c20 commit 548f148

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

app/api/event_invoices.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
2+
from flask import jsonify, request
23

34
from app.api.bootstrap import api
45
from app.api.helpers.db import safe_query
@@ -10,6 +11,12 @@
1011
from app.models.discount_code import DiscountCode
1112
from app.models.event_invoice import EventInvoice
1213
from app.models.user import User
14+
from app.api.helpers.payment import PayPalPaymentsManager
15+
from app.api.helpers.errors import BadRequestError
16+
17+
18+
from app.api.helpers.permissions import jwt_required
19+
from app.api.orders import order_misc_routes
1320

1421

1522
class EventInvoiceList(ResourceList):
@@ -82,3 +89,59 @@ class EventInvoiceRelationshipOptional(ResourceRelationship):
8289
schema = EventInvoiceSchema
8390
data_layer = {'session': db.session,
8491
'model': EventInvoice}
92+
93+
94+
@order_misc_routes.route('/event-invoices/<string:invoice_identifier>/create-paypal-payment', methods=['POST'])
95+
@jwt_required
96+
def create_paypal_payment_invoice(invoice_identifier):
97+
"""
98+
Create a paypal payment.
99+
:return: The payment id of the created payment.
100+
"""
101+
try:
102+
return_url = request.json['data']['attributes']['return-url']
103+
cancel_url = request.json['data']['attributes']['cancel-url']
104+
except TypeError:
105+
return BadRequestError({'source': ''}, 'Bad Request Error').respond()
106+
107+
event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier')
108+
status, response = PayPalPaymentsManager.create_payment(event_invoice, return_url, cancel_url)
109+
110+
if status:
111+
return jsonify(status=True, payment_id=response)
112+
else:
113+
return jsonify(status=False, error=response)
114+
115+
116+
@order_misc_routes.route('/event-invoices/<string:invoice_identifier>/charge', methods=['POST'])
117+
@jwt_required
118+
def charge_paypal_payment_invoice(invoice_identifier):
119+
"""
120+
Create a paypal payment.
121+
:return: The payment id of the created payment.
122+
"""
123+
try:
124+
paypal_payment_id = request.json['data']['attributes']['paypal_payment_id']
125+
paypal_payer_id = request.json['data']['attributes']['paypal_payer_id']
126+
except:
127+
return BadRequestError({'source': ''}, 'Bad Request Error').respond()
128+
event_invoice = safe_query(db, EventInvoice, 'identifier', invoice_identifier, 'identifier')
129+
# save the paypal payment_id with the order
130+
event_invoice.paypal_token = paypal_payment_id
131+
save_to_db(event_invoice)
132+
133+
# create the transaction.
134+
status, error = PayPalPaymentsManager.execute_payment(paypal_payer_id, paypal_payment_id)
135+
136+
if status:
137+
# successful transaction hence update the order details.
138+
event_invoice.paid_via = 'paypal'
139+
event_invoice.status = 'completed'
140+
event_invoice.transaction_id = paypal_payment_id
141+
event_invoice.completed_at = datetime.utcnow()
142+
save_to_db(event_invoice)
143+
144+
return True, 'Charge successful'
145+
else:
146+
# return the error message from Paypal
147+
return False, error

0 commit comments

Comments
 (0)