Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/api/helpers/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import paypalrestsdk
import requests
import stripe
import omise
from forex_python.converter import CurrencyRates

from app.api.helpers.cache import cache
Expand Down Expand Up @@ -243,3 +244,29 @@ def charge_source(order_identifier):
source=order.order_notes,
)
return charge


class OmisePaymentsManager(object):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

"""
Class to manage Omise Payments
"""

@staticmethod
def charge_payment(order_identifier, token):
if get_settings()['app_environment'] == Environment.PRODUCTION:
omise.api_secret = get_settings()['omise_test_secret']
omise.api_public = get_settings()['omise_test_public']
else:
omise.api_secret = get_settings()['omise_test_secret']
omise.api_public = get_settings()['omise_test_public']
order = safe_query(db, Order, 'identifier', order_identifier, 'identifier')
charge = omise.Charge.create(
amount=int(round(order.amount)),
currency=order.event.payment_currency,
card=token,
metadata={
"order_id": str(order_identifier),
"status": True
},
)
return charge
34 changes: 32 additions & 2 deletions app/api/orders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime

from flask import request, jsonify, Blueprint, url_for, redirect
import omise
import logging

from flask_jwt import current_identity as current_user
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
Expand Down Expand Up @@ -33,7 +34,7 @@
from app.models.order import Order, OrderTicket, get_updatable_fields
from app.models.ticket_holder import TicketHolder
from app.models.user import User
from app.api.helpers.payment import AliPayPaymentsManager
from app.api.helpers.payment import AliPayPaymentsManager, OmisePaymentsManager


order_misc_routes = Blueprint('order_misc', __name__, url_prefix='/v1')
Expand Down Expand Up @@ -499,3 +500,32 @@ def alipay_return_uri(order_identifier):
return jsonify(status=False, error='Charge object failure')
except TypeError:
return jsonify(status=False, error='Source object status error')


@order_misc_routes.route('/orders/<string:order_identifier>/omise-checkout', methods=['POST', 'GET'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

def omise_checkout(order_identifier):
"""
Charging the user and returning payment response for Omise Gateway
:param order_identifier:
:return: JSON response of the payment status.
"""
token = request.form.get('omiseToken')
order = safe_query(db, Order, 'identifier', order_identifier, 'identifier')
order.status = 'completed'
save_to_db(order)
try:
charge = OmisePaymentsManager.charge_payment(order_identifier, token)
print(charge.status)
except omise.errors.BaseError as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'omise'

logging.error(f"""OmiseError: {repr(e)}. See https://www.omise.co/api-errors""")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the syntax python 3.6 compatible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade your python please

return jsonify(status=False, error="Omise Failure Message: {}".format(str(e)))
except Exception as e:
logging.error(repr(e))
if charge.failure_code is not None:
logging.warning("Omise Failure Message: {} ({})".format(charge.failure_message, charge.failure_code))
return jsonify(status=False, error="Omise Failure Message: {} ({})".
format(charge.failure_message, charge.failure_code))
else:
logging.info(f"Successful charge: {charge.id}. Order ID: {order_identifier}")

return redirect(make_frontend_url('orders/{}/view'.format(order_identifier)))
1 change: 1 addition & 0 deletions requirements/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Flask-Login~=0.4
Flask-Scrypt~=0.1.3
Flask-JWT~=0.3.2
flask-celeryext~=0.3
omise~=0.8.1
requests-oauthlib~=0.8
icalendar~=3.12
requests[security]~=2.22
Expand Down