payments.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:payments 作者: devops-s17-payments 项目源码 文件源码
def charge_payment(user_id):
    try:
        if not request.data:
            raise DataValidationError('Invalid request: body of request contained no data')
        if not request.is_json:
            raise DataValidationError('Invalid request: request not json')
        data = request.get_json()
        if data['amount']:
            if(data['amount'] < 0):
                raise DataValidationError('Invalid request: Order amount is negative.')
            else:
                resp = payment_service.perform_payment_action(user_id,payment_attributes=data)
                if resp == True:
                    message = {'success' : 'Default payment method for user_id: %s has been charged $%.2f' % (str(user_id), data['amount'])}
                    rc = status.HTTP_200_OK
    except DataValidationError as e:
        message = {'error' : e.message}
        rc = status.HTTP_400_BAD_REQUEST
    except KeyError as e:
        message = {'error' : 'Invalid request: body of request does not have the amount to be charged'}
        rc = status.HTTP_400_BAD_REQUEST
    return make_response(jsonify(message), rc)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号