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)
评论列表
文章目录