|
1 | 1 | from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship |
| 2 | +from flask import jsonify, request |
2 | 3 |
|
3 | 4 | from app.api.bootstrap import api |
4 | 5 | from app.api.helpers.db import safe_query |
|
10 | 11 | from app.models.discount_code import DiscountCode |
11 | 12 | from app.models.event_invoice import EventInvoice |
12 | 13 | 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 |
13 | 20 |
|
14 | 21 |
|
15 | 22 | class EventInvoiceList(ResourceList): |
@@ -82,3 +89,59 @@ class EventInvoiceRelationshipOptional(ResourceRelationship): |
82 | 89 | schema = EventInvoiceSchema |
83 | 90 | data_layer = {'session': db.session, |
84 | 91 | '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