|
| 1 | +import datetime |
1 | 2 | from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship |
| 3 | +from flask import jsonify, request |
2 | 4 |
|
3 | 5 | from app.api.bootstrap import api |
4 | 6 | from app.api.helpers.db import safe_query |
|
10 | 12 | from app.models.discount_code import DiscountCode |
11 | 13 | from app.models.event_invoice import EventInvoice |
12 | 14 | 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 |
13 | 22 |
|
14 | 23 |
|
15 | 24 | class EventInvoiceList(ResourceList): |
@@ -127,3 +136,59 @@ def before_get_object(self, view_kwargs): |
127 | 136 | 'methods': { |
128 | 137 | 'before_get_object': before_get_object |
129 | 138 | }} |
| 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