def post(self): form = OrderForm.from_json(request.get_json(force=True, silent=True)) if not form.validate(): return bad_request("Please complete input form") if form.payment_methods.data not in app.config['PAYMENT_METHODS']: return bad_request("Invalid payment method") # save to db order = OrderModel() order.id = random_digits(8) order.status = ORDER_PENDING order.total_price = form.total_price.data order.payment_method = form.payment_methods.data # do payment urls = app.config['MIDTRANS_BASE_URL'] + '/charge' payload = generate_payment(order, customer) resp = requests.post(url=urls, headers=header_request(), json=payload).json() print('charge : ', resp) # handle response charge if resp['status_code'] == '201': payment_info = collect_payment_info(resp, order) order.va = payment_info.va order.deeplink = payment_info.deeplink order.qr_code = payment_info.qr_code # save to db db.session.add(order) db.session.commit() return make_response(201, 'created', order_response(order))
def get(self, id): order = OrderModel.query.filter(OrderModel.id == id).first() return make_response(200, 'success', order_response(order))
def get(self): query = [ order_response(x) for x in OrderModel.query.order_by( OrderModel.created_at.desc()).all() ] return make_response(200, 'success', query)
def internal_error(message=None): msg = message if message is not None else 'Internal Server Error' return make_response(code=500, message=msg)
def not_found(message=None): msg = message if message is not None else 'Not Found' return make_response(code=404, message=msg)
def forbidden(message=None): msg = message if message is not None else 'Forbidden' return make_response(code=403, message=msg)
def unauthorized(message=None): msg = message if message is not None else 'Unauthorized' return make_response(code=401, message=msg)
def bad_request(message=None, errors=None, code=400): msg = message if message is not None else 'Bad Request' data = {} if not errors else {'errors': errors} return make_response(code=code, message=msg, data=data)
def method_not_allowed(error): return make_response(code=405, message='method not allowed')