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